-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitobjects_test.go
More file actions
362 lines (342 loc) · 9.65 KB
/
Copy pathgitobjects_test.go
File metadata and controls
362 lines (342 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/binary"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"testing"
)
const (
shaA = "1111111111111111111111111111111111111111"
shaB = "2222222222222222222222222222222222222222"
shaC = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
)
func zlibBytes(data []byte) []byte {
var buf bytes.Buffer
w := zlib.NewWriter(&buf)
_, _ = w.Write(data)
_ = w.Close()
return buf.Bytes()
}
func TestParseObjType(t *testing.T) {
tests := []struct {
name string
in string
want objType
}{
{"commit", "commit", objCommit},
{"tree", "tree", objTree},
{"blob", "blob", objBlob},
{"tag", "tag", objTag},
{"empty", "", objInvalid},
{"unknown", "wat", objInvalid},
{"trailing space", "commit ", objInvalid},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseObjType(tt.in); got != tt.want {
t.Errorf("parseObjType(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}
func TestRefsFromCommit(t *testing.T) {
valid := "tree " + shaC + "\nparent " + shaA + "\nparent " + shaB + "\n\nauthor A <a@b> 1 +0000\n"
tests := []struct {
name string
in []byte
want []string
}{
{"tree and parents", []byte(valid), []string{shaC, shaA, shaB}},
{"stops at blank line", []byte("tree " + shaA + "\n\nparent " + shaB + "\n"), []string{shaA}},
{"short sha ignored", []byte("tree 1234\n\n"), nil},
{"no matching field", []byte("committer A <a@b> 1 +0000\n"), nil},
{"single field line", []byte("tree\n"), nil},
{"empty", nil, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := refsFromCommit(tt.in)
if err != nil {
t.Fatalf("refsFromCommit() error = %v", err)
}
if !equalStrings(got, tt.want) {
t.Errorf("refsFromCommit() = %v, want %v", got, tt.want)
}
})
}
}
func TestRefsFromTree(t *testing.T) {
entry := func(mode, name string, sha []byte) []byte {
var b bytes.Buffer
b.WriteString(mode)
b.WriteByte(' ')
b.WriteString(name)
b.WriteByte(0)
b.Write(sha)
return b.Bytes()
}
shaBytes := func(s string) []byte {
b, _ := hex.DecodeString(s)
return b
}
tests := []struct {
name string
in []byte
want []string
}{
{"single entry", entry("100644", "a.txt", shaBytes(shaA)), []string{shaA}},
{"multiple entries", append(entry("100644", "a.txt", shaBytes(shaA)), entry("40000", "sub", shaBytes(shaB))...), []string{shaA, shaB}},
{"missing nul", []byte("100644 a.txt" + string(shaBytes(shaA)[:19])), nil},
{"short sha", append([]byte("100644 a.txt\x00"), shaBytes(shaA)[:19]...), nil},
{"no space", []byte("junkdata"), nil},
{"empty", nil, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := refsFromTree(tt.in); !equalStrings(got, tt.want) {
t.Errorf("refsFromTree() = %v, want %v", got, tt.want)
}
})
}
}
func TestReadLooseObject(t *testing.T) {
commitData := []byte("tree " + shaC + "\n\nauthor A <a@b> 1 +0000\n")
commitObj := append([]byte(fmt.Sprintf("commit %d\x00", len(commitData))), commitData...)
tests := []struct {
name string
in []byte
wantTyp objType
wantErr bool
}{
{"valid commit", zlibBytes(commitObj), objCommit, false},
{"valid blob", zlibBytes([]byte("blob 4\x00data")), objBlob, false},
{"unknown type", zlibBytes([]byte("wat 0\x00x")), objInvalid, true},
{"no header nul", zlibBytes([]byte("no nul here")), objInvalid, true},
{"not zlib", []byte{0xff, 0xee}, objInvalid, true},
{"oversized header", zlibBytes([]byte("blob 99999999999\x00" + string(make([]byte, 100)))), objBlob, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, data, err := readLooseObject(bytes.NewReader(tt.in))
if (err != nil) != tt.wantErr {
t.Fatalf("readLooseObject() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
if got != tt.wantTyp {
t.Errorf("readLooseObject() type = %q, want %q", got, tt.wantTyp)
}
if tt.wantTyp == objCommit && !bytes.Equal(data, commitData) {
t.Errorf("readLooseObject() data = %q, want %q", data, commitData)
}
})
}
}
func TestReadOfsOffset(t *testing.T) {
tests := []struct {
name string
in []byte
want int64
}{
{"zero", []byte{0x00}, 0},
{"one", []byte{0x01}, 1},
{"max single byte", []byte{0x7f}, 127},
{"two bytes basic", []byte{0x80, 0x00}, 128},
{"two bytes", []byte{0x80, 0x01}, 129},
{"two bytes carry", []byte{0x81, 0x01}, 257},
{"three bytes", []byte{0xff, 0xff, 0x7f}, 2113663},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readOfsOffset(bufio.NewReader(bytes.NewReader(tt.in)))
if err != nil {
t.Fatalf("readOfsOffset() error = %v", err)
}
if got != tt.want {
t.Errorf("readOfsOffset() = %d, want %d", got, tt.want)
}
})
}
}
func buildIdx(names []string) []byte {
var buf bytes.Buffer
buf.Write([]byte{0xff, 't', 'O', 'c'})
_ = binary.Write(&buf, binary.BigEndian, uint32(2))
first := make([]byte, len(names))
for i, n := range names {
b, _ := hex.DecodeString(n)
first[i] = b[0]
}
for i := 0; i < 256; i++ {
var c uint32
for _, b := range first {
if int(b) <= i {
c++
}
}
_ = binary.Write(&buf, binary.BigEndian, c)
}
for _, n := range names {
b, _ := hex.DecodeString(n)
buf.Write(b)
}
return buf.Bytes()
}
func buildPack(objects [][2]any) []byte {
var buf bytes.Buffer
buf.WriteString("PACK")
_ = binary.Write(&buf, binary.BigEndian, uint32(2))
_ = binary.Write(&buf, binary.BigEndian, uint32(len(objects)))
for _, o := range objects {
typ := o[0].(byte)
data := o[1].([]byte)
writePackObjectHeader(&buf, typ, len(data))
buf.Write(zlibBytes(data))
}
return buf.Bytes()
}
// writePackObjectHeader encodes a pack entry type and size varint matching
// git's format: type in the high bits of the first byte, size starting in
// the low nibble, then 7-bit groups with a continuation bit.
func writePackObjectHeader(buf *bytes.Buffer, typ byte, size int) {
first := byte(typ<<4) | byte(size&0x0f)
size >>= 4
if size > 0 {
first |= 0x80
}
buf.WriteByte(first)
for size > 0 {
b := byte(size & 0x7f)
size >>= 7
if size > 0 {
b |= 0x80
}
buf.WriteByte(b)
}
}
func buildIndexEntry(sha string, name string) []byte {
var buf bytes.Buffer
buf.Write(make([]byte, 40))
b, _ := hex.DecodeString(sha)
buf.Write(b)
_ = binary.Write(&buf, binary.BigEndian, uint16(len(name)))
buf.WriteString(name)
entryLen := 62 + len(name)
padding := (8 - (entryLen % 8)) % 8
buf.Write(make([]byte, padding))
return buf.Bytes()
}
func TestRefsFromIdx(t *testing.T) {
names := []string{shaA, shaB, shaC}
tests := []struct {
name string
in func() []byte
want []string
wantErr bool
}{
{"valid", func() []byte { return buildIdx(names) }, names, false},
{"bad magic", func() []byte { return append([]byte{0xde, 0xad, 0xbe, 0xef}, buildIdx(names)[4:]...) }, nil, true},
{"bad version", func() []byte {
b := buildIdx(names)
b[4] = 0
b[5] = 0
b[6] = 0
b[7] = 3
return b
}, nil, true},
{"too short", func() []byte { return buildIdx(names)[:40] }, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "pack-"+shaA+".idx")
if err := os.WriteFile(path, tt.in(), 0o644); err != nil {
t.Fatal(err)
}
got, err := refsFromIdx(path)
if (err != nil) != tt.wantErr {
t.Fatalf("refsFromIdx() error = %v, wantErr %v", err, tt.wantErr)
}
if !equalStrings(got, tt.want) {
t.Errorf("refsFromIdx() = %v, want %v", got, tt.want)
}
})
}
}
func TestRefsFromPack(t *testing.T) {
commitData := []byte("tree " + shaC + "\nparent " + shaA + "\n\nauthor A <a@b> 1 +0000\n")
treeData := append([]byte("100644 a.txt\x00"), bytes.Repeat([]byte{0x11}, 20)...)
dir := t.TempDir()
packPath := filepath.Join(dir, "pack-"+shaA+".pack")
idxPath := filepath.Join(dir, "pack-"+shaA+".idx")
pack := buildPack([][2]any{
{packCommit, commitData},
{packTree, treeData},
})
if err := os.WriteFile(packPath, pack, 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(idxPath, buildIdx([]string{shaA, shaB}), 0o644); err != nil {
t.Fatal(err)
}
packed, all, err := refsFromPack(packPath, idxPath)
if err != nil {
t.Fatalf("refsFromPack() error = %v", err)
}
if want := []string{shaA, shaB}; !equalStrings(packed, want) {
t.Errorf("packed = %v, want %v", packed, want)
}
if want := []string{shaC, shaA, shaA}; !equalStrings(all, want) {
t.Errorf("all = %v, want %v", all, want)
}
}
func TestRefsFromIndex(t *testing.T) {
tests := []struct {
name string
in func() []byte
want []string
wantErr bool
}{
{"valid", func() []byte {
var buf bytes.Buffer
buf.WriteString("DIRC")
_ = binary.Write(&buf, binary.BigEndian, uint32(2))
_ = binary.Write(&buf, binary.BigEndian, uint32(1))
buf.Write(buildIndexEntry("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "hello"))
return buf.Bytes()
}, []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, false},
{"too short", func() []byte { return []byte("DIRC") }, nil, true},
{"bad signature", func() []byte { return []byte("XXXX1234") }, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "index")
if err := os.WriteFile(path, tt.in(), 0o644); err != nil {
t.Fatal(err)
}
got, err := refsFromIndex(path)
if (err != nil) != tt.wantErr {
t.Fatalf("refsFromIndex() error = %v, wantErr %v", err, tt.wantErr)
}
if !equalStrings(got, tt.want) {
t.Errorf("refsFromIndex() = %v, want %v", got, tt.want)
}
})
}
}
func equalStrings(got, want []string) bool {
if len(got) != len(want) {
return false
}
for i := range got {
if got[i] != want[i] {
return false
}
}
return true
}