github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/packfile_test.go (about) 1 package git 2 3 import ( 4 "os" 5 "testing" 6 //"bufio" 7 "bytes" 8 "fmt" 9 "io/ioutil" 10 ) 11 12 type PackfileTestCase struct { 13 Packfile []byte 14 ExpectedObjects map[string]bool 15 } 16 17 func runCase(label string, tc PackfileTestCase, t *testing.T) { 18 // Create a new gitdir/client for each case so that we don't have stuff left 19 // over from the previous one. 20 // We don't care about the working directory, because Unpack doesn't use it. 21 gitdir, err := ioutil.TempDir("", "gittest") 22 if err != nil { 23 t.Fatal(err) 24 } 25 defer os.RemoveAll(gitdir) 26 os.MkdirAll(gitdir+"/objects/pack/", 0700) 27 28 c, err := NewClient(gitdir, "") 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 shas, err := UnpackObjects(c, UnpackObjectsOptions{Quiet: true}, bytes.NewReader(tc.Packfile)) 34 if err != nil { 35 //panic(err) 36 t.Fatalf("%s: %v", label, err) 37 } 38 if g := len(shas); g != len(tc.ExpectedObjects) { 39 t.Errorf("%s: Unexpected number of objects: got %v want %v", label, g, len(tc.ExpectedObjects)) 40 return 41 } 42 43 // Keep track of everything that we found while iterating through in a 44 // map, so that we can easily compare that we got everything we expected 45 // after. 46 foundMap := make(map[string]bool) 47 48 // Make sure everything we got was expected. 49 for i, sha := range shas { 50 foundMap[sha.String()] = true 51 contains, ok := tc.ExpectedObjects[sha.String()] 52 if !contains || !ok { 53 t.Errorf("%s Unexpected SHA at index %d: %s", label, i, sha.String()) 54 } 55 } 56 57 // Make sure we got everything that was expected (nothing was missed) 58 for sha, _ := range tc.ExpectedObjects { 59 if contains, ok := foundMap[sha]; !contains || !ok { 60 t.Errorf("%s: Did not get expected SHA1: %s", label, sha) 61 } 62 } 63 } 64 65 func TestPackfileUnpack(t *testing.T) { 66 tests := []PackfileTestCase{ 67 // This is a small packfile generated with the official 68 // git client by taking a blob with a bunch of lines, 69 // hashing it, then deleting a couple and hashing that 70 // and packing them into a packfile. 71 // It should be close to the minimal packfile that has 72 // a REF_DELTA delta. 73 { 74 []byte{0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0xbc, 0x08, 0x78, 0x9c, 75 0x73, 0xe4, 0x72, 0xc4, 0x09, 0x9d, 0xb8, 0x9c, 0xb9, 0x5c, 0xb8, 0x5c, 0xe9, 0x46, 0x03, 0x00, 76 0xcc, 0xc9, 0x15, 0x0f, 0x75, 0xbe, 0x22, 0xa5, 0xc7, 0xd7, 0xb2, 0x5c, 0x99, 0x0d, 0x89, 0xd7, 77 0xc1, 0x83, 0x82, 0xf0, 0x81, 0x5f, 0x68, 0x3f, 0x17, 0x78, 0x9c, 0xeb, 0x61, 0x2c, 0x9a, 0x50, 78 0x04, 0x00, 0x05, 0xad, 0x02, 0x02, 0x75, 0xb8, 0x48, 0x6c, 0x4d, 0xda, 0x43, 0x46, 0x17, 0x0b, 79 0x29, 0x86, 0x95, 0x51, 0x0a, 0x29, 0x29, 0x68, 0x86, 0xe4}, 80 map[string]bool{ 81 "84dfc6fb0e86cf29049d53041e2d55f863eacfd8": true, 82 "be22a5c7d7b25c990d89d7c18382f0815f683f17": true, 83 }, 84 }, 85 { 86 // Same as the above, but with a chain of length 2 by adding a third 87 // modified blob. 88 []byte{ 89 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0xbc, 0x08, 0x78, 0x9c, 90 0x73, 0xe4, 0x72, 0xc4, 0x09, 0x9d, 0xb8, 0x9c, 0xb9, 0x5c, 0xb8, 0x5c, 0xe9, 0x46, 0x03, 0x00, 91 0xcc, 0xc9, 0x15, 0x0f, 0x75, 0xbe, 0x22, 0xa5, 0xc7, 0xd7, 0xb2, 0x5c, 0x99, 0x0d, 0x89, 0xd7, 92 0xc1, 0x83, 0x82, 0xf0, 0x81, 0x5f, 0x68, 0x3f, 0x17, 0x78, 0x9c, 0xeb, 0x61, 0x2c, 0x9a, 0x50, 93 0x04, 0x00, 0x05, 0xad, 0x02, 0x02, 0x75, 0x84, 0xdf, 0xc6, 0xfb, 0x0e, 0x86, 0xcf, 0x29, 0x04, 94 0x9d, 0x53, 0x04, 0x1e, 0x2d, 0x55, 0xf8, 0x63, 0xea, 0xcf, 0xd8, 0x78, 0x9c, 0x2b, 0x4a, 0x9a, 95 0x28, 0x90, 0x04, 0x00, 0x05, 0xfc, 0x01, 0xd8, 0x2d, 0xec, 0xe2, 0xa0, 0x76, 0x47, 0xdd, 0xad, 96 0xd9, 0xae, 0xb3, 0x07, 0x4f, 0x8d, 0x9e, 0x62, 0x1b, 0xec, 0x69, 0x79, 97 }, 98 map[string]bool{ 99 "84dfc6fb0e86cf29049d53041e2d55f863eacfd8": true, 100 "be22a5c7d7b25c990d89d7c18382f0815f683f17": true, 101 "bbd835f67c0ef19084d9b97e9219c1b38e66bd80": true, 102 }, 103 }, 104 105 { 106 // Same as the above, but using OFS_DELTA instead of 107 // REF_DELTA 108 []byte{ 109 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0xbc, 0x08, 0x78, 0x9c, 110 0x73, 0xe4, 0x72, 0xc4, 0x09, 0x9d, 0xb8, 0x9c, 0xb9, 0x5c, 0xb8, 0x5c, 0xe9, 0x46, 0x03, 0x00, 111 0xcc, 0xc9, 0x15, 0x0f, 0x65, 0x18, 0x78, 0x9c, 0xeb, 0x61, 0x2c, 0x9a, 0x50, 0x04, 0x00, 0x05, 112 0xad, 0x02, 0x02, 0x25, 0x15, 0xc5, 0xe5, 0xae, 0xc7, 0x2b, 0x3a, 0xc9, 0x80, 0xfc, 0x8b, 0x7f, 113 0x61, 0xc8, 0xd0, 0x6d, 0xf0, 0x62, 0xf2, 114 }, 115 map[string]bool{ 116 "84dfc6fb0e86cf29049d53041e2d55f863eacfd8": true, 117 "be22a5c7d7b25c990d89d7c18382f0815f683f17": true, 118 }, 119 }, 120 { 121 // OFS_DELTA with a chain of length > 1 122 []byte{0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0xbc, 0x08, 0x78, 0x9c, 123 0x73, 0xe4, 0x72, 0xc4, 0x09, 0x9d, 0xb8, 0x9c, 0xb9, 0x5c, 0xb8, 0x5c, 0xe9, 0x46, 0x03, 0x00, 124 0xcc, 0xc9, 0x15, 0x0f, 0x65, 0x18, 0x78, 0x9c, 0xeb, 0x61, 0x2c, 0x9a, 0x50, 0x04, 0x00, 0x05, 125 0xad, 0x02, 0x02, 0x65, 0x0f, 0x78, 0x9c, 0x2b, 0x4a, 0x9a, 0x28, 0x90, 0x04, 0x00, 0x05, 0xfc, 126 0x01, 0xd8, 0x75, 0xcc, 0x90, 0x92, 0xc3, 0xd9, 0x93, 0xba, 0xcf, 0xe4, 0x1d, 0x7c, 0xed, 0x5d, 127 0x8f, 0x46, 0xdf, 0xc2, 0x19, 0x0f, 128 }, 129 map[string]bool{ 130 "84dfc6fb0e86cf29049d53041e2d55f863eacfd8": true, 131 "be22a5c7d7b25c990d89d7c18382f0815f683f17": true, 132 "bbd835f67c0ef19084d9b97e9219c1b38e66bd80": true, 133 }, 134 }, 135 } 136 for i, tc := range tests { 137 runCase(fmt.Sprintf("Test %d", i), tc, t) 138 } 139 }