Debian Codegraph @

pack/packfile_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package pack
2
3 import (
4 "bytes"
5 "crypto/sha1"
6 "encoding/binary"
7 "encoding/hex"
8 "fmt"
9 "sort"
10 "strings"
11 "sync/atomic"
12 "testing"
13
14 "github.com/stretchr/testify/assert"
15 )
16
17 func TestPackObjectReturnsObjectWithSingleBaseAtLowOffset(t *testing.T) {
18 const original = "Hello, world!\n"
19 compressed, _ := compress(original)
20
21 p := &Packfile{
22 idx: IndexWith(map[string]uint32{
23 "cccccccccccccccccccccccccccccccccccccccc": 32,
24 }),
25 r: bytes.NewReader(append([]byte{
26 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
27 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
28 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
29 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
30
31 // (0001 1000) (msb=0, type=commit, size=14)
32 0x1e}, compressed...),
33 ),
34 hash: sha1.New(),
35 }
36
37 o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
38 assert.NoError(t, err)
39
40 assert.Equal(t, TypeCommit, o.Type())
41
42 unpacked, err := o.Unpack()
43 assert.Equal(t, []byte(original), unpacked)
44 assert.NoError(t, err)
45 }
46
47 func TestPackObjectReturnsObjectWithSingleBaseAtHighOffset(t *testing.T) {
48 original := strings.Repeat("four", 64)
49 compressed, _ := compress(original)
50
51 p := &Packfile{
52 idx: IndexWith(map[string]uint32{
53 "cccccccccccccccccccccccccccccccccccccccc": 32,
54 }),
55 r: bytes.NewReader(append([]byte{
56 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
57 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
58 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
59 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
60
61 // (1001 0000) (msb=1, type=commit, size=0)
62 0x90,
63 // (1000 0000) (msb=0, size=1 -> size=256)
64 0x10},
65
66 compressed...,
67 )),
68 hash: sha1.New(),
69 }
70
71 o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
72 assert.NoError(t, err)
73
74 assert.Equal(t, TypeCommit, o.Type())
75
76 unpacked, err := o.Unpack()
77 assert.Equal(t, []byte(original), unpacked)
78 assert.NoError(t, err)
79 }
80
81 func TestPackObjectReturnsObjectWithDeltaBaseOffset(t *testing.T) {
82 const original = "Hello"
83 compressed, _ := compress(original)
84
85 delta, err := compress(string([]byte{
86 0x05, // Source size: 5.
87 0x0e, // Destination size: 14.
88
89 0x91, // (1000 0001) (instruction=copy, bitmask=0001)
90 0x00, // (0000 0000) (offset=0)
91 0x05, // (0000 0101) (size=5)
92
93 0x09, // (0000 0111) (instruction=add, size=7)
94
95 // Contents: ...
96 ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n',
97 }))
98
99 p := &Packfile{
100 idx: IndexWith(map[string]uint32{
101 "cccccccccccccccccccccccccccccccccccccccc": uint32(32 + 1 + len(compressed)),
102 }),
103 r: bytes.NewReader(append(append([]byte{
104 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
105 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
106 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
107 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
108
109 0x35, // (0011 0101) (msb=0, type=blob, size=5)
110 }, compressed...), append([]byte{
111 0x6e, // (0110 1010) (msb=0, type=obj_ofs_delta, size=10)
112 0x12, // (0001 0001) (ofs_delta=-17, len(compressed))
113 }, delta...)...)),
114 hash: sha1.New(),
115 }
116
117 o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
118 assert.NoError(t, err)
119
120 assert.Equal(t, TypeBlob, o.Type())
121
122 unpacked, err := o.Unpack()
123 assert.Equal(t, []byte(original+", world!\n"), unpacked)
124 assert.NoError(t, err)
125 }
126
127 func TestPackfileObjectReturnsObjectWithDeltaBaseReference(t *testing.T) {
128 const original = "Hello!\n"
129 compressed, _ := compress(original)
130
131 delta, _ := compress(string([]byte{
132 0x07, // Source size: 7.
133 0x0e, // Destination size: 14.
134
135 0x91, // (1001 0001) (copy, smask=0001, omask=0001)
136 0x00, // (0000 0000) (offset=0)
137 0x05, // (0000 0101) (size=5)
138
139 0x7, // (0000 0111) (add, length=6)
140 ',', ' ', 'w', 'o', 'r', 'l', 'd', // (data ...)
141
142 0x91, // (1001 0001) (copy, smask=0001, omask=0001)
143 0x05, // (0000 0101) (offset=5)
144 0x02, // (0000 0010) (size=2)
145 }))
146
147 p := &Packfile{
148 idx: IndexWith(map[string]uint32{
149 "cccccccccccccccccccccccccccccccccccccccc": 32,
150 "dddddddddddddddddddddddddddddddddddddddd": 52,
151 }),
152 r: bytes.NewReader(append(append([]byte{
153 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
154 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
155 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
156 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
157
158 0x37, // (0011 0101) (msb=0, type=blob, size=7)
159 }, compressed...), append([]byte{
160 0x7f, // (0111 1111) (msb=0, type=obj_ref_delta, size=15)
161
162 // SHA-1 "cccccccccccccccccccccccccccccccccccccccc",
163 // original blob contents is "Hello!\n"
164 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
165 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
166 }, delta...)...)),
167 hash: sha1.New(),
168 }
169
170 o, err := p.Object(DecodeHex(t, "dddddddddddddddddddddddddddddddddddddddd"))
171 assert.NoError(t, err)
172
173 assert.Equal(t, TypeBlob, o.Type())
174
175 unpacked, err := o.Unpack()
176 assert.Equal(t, []byte("Hello, world!\n"), unpacked)
177 assert.NoError(t, err)
178 }
179
180 func TestPackfileClosesReadClosers(t *testing.T) {
181 r := new(ReaderAtCloser)
182 p := &Packfile{
183 r: r,
184 }
185
186 assert.NoError(t, p.Close())
187 assert.EqualValues(t, 1, r.N)
188 }
189
190 func TestPackfileClosePropogatesCloseErrors(t *testing.T) {
191 e := fmt.Errorf("gitobj/pack: testing")
192 p := &Packfile{
193 r: &ReaderAtCloser{E: e},
194 }
195
196 assert.Equal(t, e, p.Close())
197 }
198
199 type ReaderAtCloser struct {
200 E error
201 N uint64
202 }
203
204 func (r *ReaderAtCloser) ReadAt(p []byte, at int64) (int, error) {
205 return 0, nil
206 }
207
208 func (r *ReaderAtCloser) Close() error {
209 atomic.AddUint64(&r.N, 1)
210 return r.E
211 }
212
213 func IndexWith(offsets map[string]uint32) *Index {
214 header := []byte{
215 0xff, 0x74, 0x4f, 0x63,
216 0x00, 0x00, 0x00, 0x02,
217 }
218
219 ns := make([][]byte, 0, len(offsets))
220 for name, _ := range offsets {
221 x, _ := hex.DecodeString(name)
222 ns = append(ns, x)
223 }
224 sort.Slice(ns, func(i, j int) bool {
225 return bytes.Compare(ns[i], ns[j]) < 0
226 })
227
228 fanout := make([]uint32, 256)
229 for i := 0; i < len(fanout); i++ {
230 var n uint32
231
232 for _, name := range ns {
233 if name[0] <= byte(i) {
234 n++
235 }
236 }
237
238 fanout[i] = n
239 }
240
241 crcs := make([]byte, 4*len(offsets))
242 for i, _ := range ns {
243 binary.BigEndian.PutUint32(crcs[i*4:], 0)
244 }
245
246 offs := make([]byte, 4*len(offsets))
247 for i, name := range ns {
248 binary.BigEndian.PutUint32(offs[i*4:], offsets[hex.EncodeToString(name)])
249 }
250
251 buf := make([]byte, 0)
252 buf = append(buf, header...)
253 for _, f := range fanout {
254 x := make([]byte, 4)
255 binary.BigEndian.PutUint32(x, f)
256
257 buf = append(buf, x...)
258 }
259 for _, n := range ns {
260 buf = append(buf, n...)
261 }
262 buf = append(buf, crcs...)
263 buf = append(buf, offs...)
264
265 return &Index{
266 fanout: fanout,
267 r: bytes.NewReader(buf),
268
269 version: &V2{hash: sha1.New()},
270 }
271 }
272
273 func DecodeHex(t *testing.T, str string) []byte {
274 b, err := hex.DecodeString(str)
275 if err != nil {
276 t.Fatalf("gitobj/pack: unexpected hex.DecodeString error: %s", err)
277 }
278
279 return b
280 }