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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
package packp
import (
"bytes"
"io"
"strings"
"srcd.works/go-git.v4/plumbing"
"srcd.works/go-git.v4/plumbing/format/pktline"
"srcd.works/go-git.v4/plumbing/protocol/packp/capability"
. "gopkg.in/check.v1"
)
type AdvRefsDecodeSuite struct{}
var _ = Suite(&AdvRefsDecodeSuite{})
func (s *AdvRefsDecodeSuite) TestEmpty(c *C) {
var buf bytes.Buffer
ar := NewAdvRefs()
c.Assert(ar.Decode(&buf), Equals, ErrEmptyInput)
}
func (s *AdvRefsDecodeSuite) TestEmptyFlush(c *C) {
var buf bytes.Buffer
e := pktline.NewEncoder(&buf)
e.Flush()
ar := NewAdvRefs()
c.Assert(ar.Decode(&buf), Equals, ErrEmptyAdvRefs)
}
func (s *AdvRefsDecodeSuite) TestEmptyPrefixFlush(c *C) {
var buf bytes.Buffer
e := pktline.NewEncoder(&buf)
e.EncodeString("# service=git-upload-pack")
e.Flush()
e.Flush()
ar := NewAdvRefs()
c.Assert(ar.Decode(&buf), Equals, ErrEmptyAdvRefs)
}
func (s *AdvRefsDecodeSuite) TestShortForHash(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*too short.*")
}
func (s *AdvRefsDecodeSuite) testDecoderErrorMatches(c *C, input io.Reader, pattern string) {
ar := NewAdvRefs()
c.Assert(ar.Decode(input), ErrorMatches, pattern)
}
func (s *AdvRefsDecodeSuite) TestInvalidFirstHash(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796alberto2219af86ec6584e5 HEAD\x00multi_ack thin-pack\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*invalid hash.*")
}
func (s *AdvRefsDecodeSuite) TestZeroId(c *C) {
payloads := []string{
"0000000000000000000000000000000000000000 capabilities^{}\x00multi_ack thin-pack\n",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(ar.Head, IsNil)
}
func (s *AdvRefsDecodeSuite) testDecodeOK(c *C, payloads []string) *AdvRefs {
var buf bytes.Buffer
e := pktline.NewEncoder(&buf)
err := e.EncodeString(payloads...)
c.Assert(err, IsNil)
ar := NewAdvRefs()
c.Assert(ar.Decode(&buf), IsNil)
return ar
}
func (s *AdvRefsDecodeSuite) TestMalformedZeroId(c *C) {
payloads := []string{
"0000000000000000000000000000000000000000 wrong\x00multi_ack thin-pack\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*malformed zero-id.*")
}
func (s *AdvRefsDecodeSuite) TestShortZeroId(c *C) {
payloads := []string{
"0000000000000000000000000000000000000000 capabi",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*too short zero-id.*")
}
func (s *AdvRefsDecodeSuite) TestHead(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(*ar.Head, Equals,
plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
}
func (s *AdvRefsDecodeSuite) TestFirstIsNotHead(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master\x00",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(ar.Head, IsNil)
c.Assert(ar.References["refs/heads/master"], Equals,
plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
}
func (s *AdvRefsDecodeSuite) TestShortRef(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 H",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*too short.*")
}
func (s *AdvRefsDecodeSuite) TestNoNULL(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEADofs-delta multi_ack",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*NULL not found.*")
}
func (s *AdvRefsDecodeSuite) TestNoSpaceAfterHash(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5-HEAD\x00",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*no space after hash.*")
}
func (s *AdvRefsDecodeSuite) TestNoCaps(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(ar.Capabilities.IsEmpty(), Equals, true)
}
func (s *AdvRefsDecodeSuite) TestCaps(c *C) {
type entry struct {
Name capability.Capability
Values []string
}
for _, test := range [...]struct {
input []string
capabilities []entry
}{{
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00",
pktline.FlushString,
},
capabilities: []entry{},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00\n",
pktline.FlushString,
},
capabilities: []entry{},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta",
pktline.FlushString,
},
capabilities: []entry{
{
Name: capability.OFSDelta,
Values: []string(nil),
},
},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta multi_ack",
pktline.FlushString,
},
capabilities: []entry{
{Name: capability.OFSDelta, Values: []string(nil)},
{Name: capability.MultiACK, Values: []string(nil)},
},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta multi_ack\n",
pktline.FlushString,
},
capabilities: []entry{
{Name: capability.OFSDelta, Values: []string(nil)},
{Name: capability.MultiACK, Values: []string(nil)},
},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:refs/heads/master agent=foo=bar\n",
pktline.FlushString,
},
capabilities: []entry{
{Name: capability.SymRef, Values: []string{"HEAD:refs/heads/master"}},
{Name: capability.Agent, Values: []string{"foo=bar"}},
},
}} {
ar := s.testDecodeOK(c, test.input)
for _, fixCap := range test.capabilities {
c.Assert(ar.Capabilities.Supports(fixCap.Name), Equals, true,
Commentf("input = %q, capability = %q", test.input, fixCap.Name))
c.Assert(ar.Capabilities.Get(fixCap.Name), DeepEquals, fixCap.Values,
Commentf("input = %q, capability = %q", test.input, fixCap.Name))
}
}
}
func (s *AdvRefsDecodeSuite) TestWithPrefix(c *C) {
payloads := []string{
"# this is a prefix\n",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta\n",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(len(ar.Prefix), Equals, 1)
c.Assert(ar.Prefix[0], DeepEquals, []byte("# this is a prefix"))
}
func (s *AdvRefsDecodeSuite) TestWithPrefixAndFlush(c *C) {
payloads := []string{
"# this is a prefix\n",
pktline.FlushString,
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta\n",
pktline.FlushString,
}
ar := s.testDecodeOK(c, payloads)
c.Assert(len(ar.Prefix), Equals, 2)
c.Assert(ar.Prefix[0], DeepEquals, []byte("# this is a prefix"))
c.Assert(ar.Prefix[1], DeepEquals, []byte(pktline.FlushString))
}
func (s *AdvRefsDecodeSuite) TestOtherRefs(c *C) {
for _, test := range [...]struct {
input []string
references map[string]plumbing.Hash
peeled map[string]plumbing.Hash
}{{
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
pktline.FlushString,
},
references: make(map[string]plumbing.Hash),
peeled: make(map[string]plumbing.Hash),
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"1111111111111111111111111111111111111111 ref/foo",
pktline.FlushString,
},
references: map[string]plumbing.Hash{
"ref/foo": plumbing.NewHash("1111111111111111111111111111111111111111"),
},
peeled: make(map[string]plumbing.Hash),
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"1111111111111111111111111111111111111111 ref/foo\n",
pktline.FlushString,
},
references: map[string]plumbing.Hash{
"ref/foo": plumbing.NewHash("1111111111111111111111111111111111111111"),
},
peeled: make(map[string]plumbing.Hash),
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"1111111111111111111111111111111111111111 ref/foo\n",
"2222222222222222222222222222222222222222 ref/bar",
pktline.FlushString,
},
references: map[string]plumbing.Hash{
"ref/foo": plumbing.NewHash("1111111111111111111111111111111111111111"),
"ref/bar": plumbing.NewHash("2222222222222222222222222222222222222222"),
},
peeled: make(map[string]plumbing.Hash),
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"1111111111111111111111111111111111111111 ref/foo^{}\n",
pktline.FlushString,
},
references: make(map[string]plumbing.Hash),
peeled: map[string]plumbing.Hash{
"ref/foo": plumbing.NewHash("1111111111111111111111111111111111111111"),
},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"1111111111111111111111111111111111111111 ref/foo\n",
"2222222222222222222222222222222222222222 ref/bar^{}",
pktline.FlushString,
},
references: map[string]plumbing.Hash{
"ref/foo": plumbing.NewHash("1111111111111111111111111111111111111111"),
},
peeled: map[string]plumbing.Hash{
"ref/bar": plumbing.NewHash("2222222222222222222222222222222222222222"),
},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"51b8b4fb32271d39fbdd760397406177b2b0fd36 refs/pull/10/head\n",
"02b5a6031ba7a8cbfde5d65ff9e13ecdbc4a92ca refs/pull/100/head\n",
"c284c212704c43659bf5913656b8b28e32da1621 refs/pull/100/merge\n",
"3d6537dce68c8b7874333a1720958bd8db3ae8ca refs/pull/101/merge\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
pktline.FlushString,
},
references: map[string]plumbing.Hash{
"refs/heads/master": plumbing.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
"refs/pull/10/head": plumbing.NewHash("51b8b4fb32271d39fbdd760397406177b2b0fd36"),
"refs/pull/100/head": plumbing.NewHash("02b5a6031ba7a8cbfde5d65ff9e13ecdbc4a92ca"),
"refs/pull/100/merge": plumbing.NewHash("c284c212704c43659bf5913656b8b28e32da1621"),
"refs/pull/101/merge": plumbing.NewHash("3d6537dce68c8b7874333a1720958bd8db3ae8ca"),
"refs/tags/v2.6.11": plumbing.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"),
"refs/tags/v2.6.11-tree": plumbing.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"),
},
peeled: map[string]plumbing.Hash{
"refs/tags/v2.6.11": plumbing.NewHash("c39ae07f393806ccf406ef966e9a15afc43cc36a"),
"refs/tags/v2.6.11-tree": plumbing.NewHash("c39ae07f393806ccf406ef966e9a15afc43cc36a"),
},
}} {
ar := s.testDecodeOK(c, test.input)
comment := Commentf("input = %v\n", test.input)
c.Assert(ar.References, DeepEquals, test.references, comment)
c.Assert(ar.Peeled, DeepEquals, test.peeled, comment)
}
}
func (s *AdvRefsDecodeSuite) TestMalformedOtherRefsNoSpace(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00multi_ack thin-pack\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8crefs/tags/v2.6.11\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*malformed ref data.*")
}
func (s *AdvRefsDecodeSuite) TestMalformedOtherRefsMultipleSpaces(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00multi_ack thin-pack\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags v2.6.11\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*malformed ref data.*")
}
func (s *AdvRefsDecodeSuite) TestShallow(c *C) {
for _, test := range [...]struct {
input []string
shallows []plumbing.Hash
}{{
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
pktline.FlushString,
},
shallows: []plumbing.Hash{},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
"shallow 1111111111111111111111111111111111111111\n",
pktline.FlushString,
},
shallows: []plumbing.Hash{plumbing.NewHash("1111111111111111111111111111111111111111")},
}, {
input: []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
"shallow 1111111111111111111111111111111111111111\n",
"shallow 2222222222222222222222222222222222222222\n",
pktline.FlushString,
},
shallows: []plumbing.Hash{
plumbing.NewHash("1111111111111111111111111111111111111111"),
plumbing.NewHash("2222222222222222222222222222222222222222"),
},
}} {
ar := s.testDecodeOK(c, test.input)
comment := Commentf("input = %v\n", test.input)
c.Assert(ar.Shallows, DeepEquals, test.shallows, comment)
}
}
func (s *AdvRefsDecodeSuite) TestInvalidShallowHash(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
"shallow 11111111alcortes111111111111111111111111\n",
"shallow 2222222222222222222222222222222222222222\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*invalid hash text.*")
}
func (s *AdvRefsDecodeSuite) TestGarbageAfterShallow(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
"shallow 1111111111111111111111111111111111111111\n",
"shallow 2222222222222222222222222222222222222222\n",
"b5be40b90dbaa6bd337f3b77de361bfc0723468b refs/tags/v4.4",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*malformed shallow prefix.*")
}
func (s *AdvRefsDecodeSuite) TestMalformedShallowHash(c *C) {
payloads := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
"5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n",
"c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n",
"shallow 1111111111111111111111111111111111111111\n",
"shallow 2222222222222222222222222222222222222222 malformed\n",
pktline.FlushString,
}
r := toPktLines(c, payloads)
s.testDecoderErrorMatches(c, r, ".*malformed shallow hash.*")
}
func (s *AdvRefsDecodeSuite) TestEOFRefs(c *C) {
input := strings.NewReader("" +
"005b6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n" +
"003fa6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n" +
"00355dc01c595e6c6ec9ccda4f6ffbf614e4d92bb0c7 refs/foo\n",
)
s.testDecoderErrorMatches(c, input, ".*invalid pkt-len.*")
}
func (s *AdvRefsDecodeSuite) TestEOFShallows(c *C) {
input := strings.NewReader("" +
"005b6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00ofs-delta symref=HEAD:/refs/heads/master\n" +
"003fa6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n" +
"00445dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n" +
"0047c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11-tree^{}\n" +
"0035shallow 1111111111111111111111111111111111111111\n" +
"0034shallow 222222222222222222222222")
s.testDecoderErrorMatches(c, input, ".*unexpected EOF.*")
}
|