Debian Codegraph @

plumbing/protocol/packp/ulreq_decode_test.go

raw
1 package packp
2
3 import (
4 "bytes"
5 "io"
6 "sort"
7 "time"
8
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/format/pktline"
11 "github.com/go-git/go-git/v5/plumbing/hash"
12 "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
13
14 . "gopkg.in/check.v1"
15 )
16
17 type UlReqDecodeSuite struct{}
18
19 var _ = Suite(&UlReqDecodeSuite{})
20
21 func (s *UlReqDecodeSuite) TestEmpty(c *C) {
22 ur := NewUploadRequest()
23 var buf bytes.Buffer
24 d := newUlReqDecoder(&buf)
25
26 err := d.Decode(ur)
27 c.Assert(err, ErrorMatches, "pkt-line 1: EOF")
28 }
29
30 func (s *UlReqDecodeSuite) TestNoWant(c *C) {
31 payloads := []string{
32 "foobar",
33 pktline.FlushString,
34 }
35 r := toPktLines(c, payloads)
36 s.testDecoderErrorMatches(c, r, ".*missing 'want '.*")
37 }
38
39 func (s *UlReqDecodeSuite) testDecoderErrorMatches(c *C, input io.Reader, pattern string) {
40 ur := NewUploadRequest()
41 d := newUlReqDecoder(input)
42
43 err := d.Decode(ur)
44 c.Assert(err, ErrorMatches, pattern)
45 }
46
47 func (s *UlReqDecodeSuite) TestInvalidFirstHash(c *C) {
48 payloads := []string{
49 "want 6ecf0ef2c2dffb796alberto2219af86ec6584e5\n",
50 pktline.FlushString,
51 }
52 r := toPktLines(c, payloads)
53 s.testDecoderErrorMatches(c, r, ".*invalid hash.*")
54 }
55
56 func (s *UlReqDecodeSuite) TestWantOK(c *C) {
57 payloads := []string{
58 "want 1111111111111111111111111111111111111111",
59 pktline.FlushString,
60 }
61 ur := s.testDecodeOK(c, payloads)
62
63 c.Assert(ur.Wants, DeepEquals, []plumbing.Hash{
64 plumbing.NewHash("1111111111111111111111111111111111111111"),
65 })
66 }
67
68 func (s *UlReqDecodeSuite) testDecodeOK(c *C, payloads []string) *UploadRequest {
69 var buf bytes.Buffer
70 e := pktline.NewEncoder(&buf)
71 err := e.EncodeString(payloads...)
72 c.Assert(err, IsNil)
73
74 ur := NewUploadRequest()
75 d := newUlReqDecoder(&buf)
76
77 err = d.Decode(ur)
78 c.Assert(err, IsNil)
79
80 return ur
81 }
82
83 func (s *UlReqDecodeSuite) TestWantWithCapabilities(c *C) {
84 payloads := []string{
85 "want 1111111111111111111111111111111111111111 ofs-delta multi_ack",
86 pktline.FlushString,
87 }
88 ur := s.testDecodeOK(c, payloads)
89 c.Assert(ur.Wants, DeepEquals, []plumbing.Hash{
90 plumbing.NewHash("1111111111111111111111111111111111111111")})
91
92 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
93 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
94 }
95
96 func (s *UlReqDecodeSuite) TestManyWantsNoCapabilities(c *C) {
97 payloads := []string{
98 "want 3333333333333333333333333333333333333333",
99 "want 4444444444444444444444444444444444444444",
100 "want 1111111111111111111111111111111111111111",
101 "want 2222222222222222222222222222222222222222",
102 pktline.FlushString,
103 }
104 ur := s.testDecodeOK(c, payloads)
105
106 expected := []plumbing.Hash{
107 plumbing.NewHash("1111111111111111111111111111111111111111"),
108 plumbing.NewHash("2222222222222222222222222222222222222222"),
109 plumbing.NewHash("3333333333333333333333333333333333333333"),
110 plumbing.NewHash("4444444444444444444444444444444444444444"),
111 }
112
113 sort.Sort(byHash(ur.Wants))
114 sort.Sort(byHash(expected))
115 c.Assert(ur.Wants, DeepEquals, expected)
116 }
117
118 type byHash []plumbing.Hash
119
120 func (a byHash) Len() int { return len(a) }
121 func (a byHash) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
122 func (a byHash) Less(i, j int) bool {
123 ii := [hash.Size]byte(a[i])
124 jj := [hash.Size]byte(a[j])
125 return bytes.Compare(ii[:], jj[:]) < 0
126 }
127
128 func (s *UlReqDecodeSuite) TestManyWantsBadWant(c *C) {
129 payloads := []string{
130 "want 3333333333333333333333333333333333333333",
131 "want 4444444444444444444444444444444444444444",
132 "foo",
133 "want 2222222222222222222222222222222222222222",
134 pktline.FlushString,
135 }
136 r := toPktLines(c, payloads)
137 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
138 }
139
140 func (s *UlReqDecodeSuite) TestManyWantsInvalidHash(c *C) {
141 payloads := []string{
142 "want 3333333333333333333333333333333333333333",
143 "want 4444444444444444444444444444444444444444",
144 "want 1234567890abcdef",
145 "want 2222222222222222222222222222222222222222",
146 pktline.FlushString,
147 }
148 r := toPktLines(c, payloads)
149 s.testDecoderErrorMatches(c, r, ".*malformed hash.*")
150 }
151
152 func (s *UlReqDecodeSuite) TestManyWantsWithCapabilities(c *C) {
153 payloads := []string{
154 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
155 "want 4444444444444444444444444444444444444444",
156 "want 1111111111111111111111111111111111111111",
157 "want 2222222222222222222222222222222222222222",
158 pktline.FlushString,
159 }
160 ur := s.testDecodeOK(c, payloads)
161
162 expected := []plumbing.Hash{
163 plumbing.NewHash("1111111111111111111111111111111111111111"),
164 plumbing.NewHash("2222222222222222222222222222222222222222"),
165 plumbing.NewHash("3333333333333333333333333333333333333333"),
166 plumbing.NewHash("4444444444444444444444444444444444444444"),
167 }
168
169 sort.Sort(byHash(ur.Wants))
170 sort.Sort(byHash(expected))
171 c.Assert(ur.Wants, DeepEquals, expected)
172
173 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
174 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
175 }
176
177 func (s *UlReqDecodeSuite) TestSingleShallowSingleWant(c *C) {
178 payloads := []string{
179 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
180 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
181 pktline.FlushString,
182 }
183 ur := s.testDecodeOK(c, payloads)
184
185 expectedWants := []plumbing.Hash{
186 plumbing.NewHash("3333333333333333333333333333333333333333"),
187 }
188
189 expectedShallows := []plumbing.Hash{
190 plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
191 }
192
193 c.Assert(ur.Wants, DeepEquals, expectedWants)
194 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
195 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
196
197 c.Assert(ur.Shallows, DeepEquals, expectedShallows)
198 }
199
200 func (s *UlReqDecodeSuite) TestSingleShallowManyWants(c *C) {
201 payloads := []string{
202 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
203 "want 4444444444444444444444444444444444444444",
204 "want 1111111111111111111111111111111111111111",
205 "want 2222222222222222222222222222222222222222",
206 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
207 pktline.FlushString,
208 }
209 ur := s.testDecodeOK(c, payloads)
210
211 expectedWants := []plumbing.Hash{
212 plumbing.NewHash("1111111111111111111111111111111111111111"),
213 plumbing.NewHash("2222222222222222222222222222222222222222"),
214 plumbing.NewHash("3333333333333333333333333333333333333333"),
215 plumbing.NewHash("4444444444444444444444444444444444444444"),
216 }
217 sort.Sort(byHash(expectedWants))
218
219 expectedShallows := []plumbing.Hash{
220 plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
221 }
222
223 sort.Sort(byHash(ur.Wants))
224 c.Assert(ur.Wants, DeepEquals, expectedWants)
225 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
226 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
227
228 c.Assert(ur.Shallows, DeepEquals, expectedShallows)
229 }
230
231 func (s *UlReqDecodeSuite) TestManyShallowSingleWant(c *C) {
232 payloads := []string{
233 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
234 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
235 "shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
236 "shallow cccccccccccccccccccccccccccccccccccccccc",
237 "shallow dddddddddddddddddddddddddddddddddddddddd",
238 pktline.FlushString,
239 }
240 ur := s.testDecodeOK(c, payloads)
241
242 expectedWants := []plumbing.Hash{
243 plumbing.NewHash("3333333333333333333333333333333333333333"),
244 }
245
246 expectedShallows := []plumbing.Hash{
247 plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
248 plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
249 plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"),
250 plumbing.NewHash("dddddddddddddddddddddddddddddddddddddddd"),
251 }
252 sort.Sort(byHash(expectedShallows))
253
254 c.Assert(ur.Wants, DeepEquals, expectedWants)
255 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
256 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
257
258 sort.Sort(byHash(ur.Shallows))
259 c.Assert(ur.Shallows, DeepEquals, expectedShallows)
260 }
261
262 func (s *UlReqDecodeSuite) TestManyShallowManyWants(c *C) {
263 payloads := []string{
264 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
265 "want 4444444444444444444444444444444444444444",
266 "want 1111111111111111111111111111111111111111",
267 "want 2222222222222222222222222222222222222222",
268 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
269 "shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
270 "shallow cccccccccccccccccccccccccccccccccccccccc",
271 "shallow dddddddddddddddddddddddddddddddddddddddd",
272 pktline.FlushString,
273 }
274 ur := s.testDecodeOK(c, payloads)
275
276 expectedWants := []plumbing.Hash{
277 plumbing.NewHash("1111111111111111111111111111111111111111"),
278 plumbing.NewHash("2222222222222222222222222222222222222222"),
279 plumbing.NewHash("3333333333333333333333333333333333333333"),
280 plumbing.NewHash("4444444444444444444444444444444444444444"),
281 }
282 sort.Sort(byHash(expectedWants))
283
284 expectedShallows := []plumbing.Hash{
285 plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
286 plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
287 plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"),
288 plumbing.NewHash("dddddddddddddddddddddddddddddddddddddddd"),
289 }
290 sort.Sort(byHash(expectedShallows))
291
292 sort.Sort(byHash(ur.Wants))
293 c.Assert(ur.Wants, DeepEquals, expectedWants)
294 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
295 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
296
297 sort.Sort(byHash(ur.Shallows))
298 c.Assert(ur.Shallows, DeepEquals, expectedShallows)
299 }
300
301 func (s *UlReqDecodeSuite) TestMalformedShallow(c *C) {
302 payloads := []string{
303 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
304 "shalow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
305 pktline.FlushString,
306 }
307 r := toPktLines(c, payloads)
308 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
309 }
310
311 func (s *UlReqDecodeSuite) TestMalformedShallowHash(c *C) {
312 payloads := []string{
313 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
314 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
315 pktline.FlushString,
316 }
317 r := toPktLines(c, payloads)
318 s.testDecoderErrorMatches(c, r, ".*malformed hash.*")
319 }
320
321 func (s *UlReqDecodeSuite) TestMalformedShallowManyShallows(c *C) {
322 payloads := []string{
323 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
324 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
325 "shalow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
326 "shallow cccccccccccccccccccccccccccccccccccccccc",
327 pktline.FlushString,
328 }
329 r := toPktLines(c, payloads)
330 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
331 }
332
333 func (s *UlReqDecodeSuite) TestMalformedDeepenSpec(c *C) {
334 payloads := []string{
335 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
336 "deepen-foo 34",
337 pktline.FlushString,
338 }
339 r := toPktLines(c, payloads)
340 s.testDecoderErrorMatches(c, r, ".*unexpected deepen.*")
341 }
342
343 func (s *UlReqDecodeSuite) TestMalformedDeepenSingleWant(c *C) {
344 payloads := []string{
345 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
346 "depth 32",
347 pktline.FlushString,
348 }
349 r := toPktLines(c, payloads)
350 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
351 }
352
353 func (s *UlReqDecodeSuite) TestMalformedDeepenMultiWant(c *C) {
354 payloads := []string{
355 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
356 "want 2222222222222222222222222222222222222222",
357 "depth 32",
358 pktline.FlushString,
359 }
360 r := toPktLines(c, payloads)
361 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
362 }
363
364 func (s *UlReqDecodeSuite) TestMalformedDeepenWithSingleShallow(c *C) {
365 payloads := []string{
366 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
367 "shallow 2222222222222222222222222222222222222222",
368 "depth 32",
369 pktline.FlushString,
370 }
371 r := toPktLines(c, payloads)
372 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
373 }
374
375 func (s *UlReqDecodeSuite) TestMalformedDeepenWithMultiShallow(c *C) {
376 payloads := []string{
377 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
378 "shallow 2222222222222222222222222222222222222222",
379 "shallow 5555555555555555555555555555555555555555",
380 "depth 32",
381 pktline.FlushString,
382 }
383 r := toPktLines(c, payloads)
384 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
385 }
386
387 func (s *UlReqDecodeSuite) TestDeepenCommits(c *C) {
388 payloads := []string{
389 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
390 "deepen 1234",
391 pktline.FlushString,
392 }
393 ur := s.testDecodeOK(c, payloads)
394
395 c.Assert(ur.Depth, FitsTypeOf, DepthCommits(0))
396 commits, ok := ur.Depth.(DepthCommits)
397 c.Assert(ok, Equals, true)
398 c.Assert(int(commits), Equals, 1234)
399 }
400
401 func (s *UlReqDecodeSuite) TestDeepenCommitsInfiniteImplicit(c *C) {
402 payloads := []string{
403 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
404 "deepen 0",
405 pktline.FlushString,
406 }
407 ur := s.testDecodeOK(c, payloads)
408
409 c.Assert(ur.Depth, FitsTypeOf, DepthCommits(0))
410 commits, ok := ur.Depth.(DepthCommits)
411 c.Assert(ok, Equals, true)
412 c.Assert(int(commits), Equals, 0)
413 }
414
415 func (s *UlReqDecodeSuite) TestDeepenCommitsInfiniteExplicit(c *C) {
416 payloads := []string{
417 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
418 pktline.FlushString,
419 }
420 ur := s.testDecodeOK(c, payloads)
421
422 c.Assert(ur.Depth, FitsTypeOf, DepthCommits(0))
423 commits, ok := ur.Depth.(DepthCommits)
424 c.Assert(ok, Equals, true)
425 c.Assert(int(commits), Equals, 0)
426 }
427
428 func (s *UlReqDecodeSuite) TestMalformedDeepenCommits(c *C) {
429 payloads := []string{
430 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
431 "deepen -32",
432 pktline.FlushString,
433 }
434 r := toPktLines(c, payloads)
435 s.testDecoderErrorMatches(c, r, ".*negative depth.*")
436 }
437
438 func (s *UlReqDecodeSuite) TestDeepenCommitsEmpty(c *C) {
439 payloads := []string{
440 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
441 "deepen ",
442 pktline.FlushString,
443 }
444 r := toPktLines(c, payloads)
445 s.testDecoderErrorMatches(c, r, ".*invalid syntax.*")
446 }
447
448 func (s *UlReqDecodeSuite) TestDeepenSince(c *C) {
449 payloads := []string{
450 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
451 "deepen-since 1420167845", // 2015-01-02T03:04:05+00:00
452 pktline.FlushString,
453 }
454 ur := s.testDecodeOK(c, payloads)
455
456 expected := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC)
457
458 c.Assert(ur.Depth, FitsTypeOf, DepthSince(time.Now()))
459 since, ok := ur.Depth.(DepthSince)
460 c.Assert(ok, Equals, true)
461 c.Assert(time.Time(since).Equal(expected), Equals, true,
462 Commentf("obtained=%s\nexpected=%s", time.Time(since), expected))
463 }
464
465 func (s *UlReqDecodeSuite) TestDeepenReference(c *C) {
466 payloads := []string{
467 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
468 "deepen-not refs/heads/master",
469 pktline.FlushString,
470 }
471 ur := s.testDecodeOK(c, payloads)
472
473 expected := "refs/heads/master"
474
475 c.Assert(ur.Depth, FitsTypeOf, DepthReference(""))
476 reference, ok := ur.Depth.(DepthReference)
477 c.Assert(ok, Equals, true)
478 c.Assert(string(reference), Equals, expected)
479 }
480
481 func (s *UlReqDecodeSuite) TestAll(c *C) {
482 payloads := []string{
483 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
484 "want 4444444444444444444444444444444444444444",
485 "want 1111111111111111111111111111111111111111",
486 "want 2222222222222222222222222222222222222222",
487 "shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
488 "shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
489 "shallow cccccccccccccccccccccccccccccccccccccccc",
490 "shallow dddddddddddddddddddddddddddddddddddddddd",
491 "deepen 1234",
492 pktline.FlushString,
493 }
494 ur := s.testDecodeOK(c, payloads)
495
496 expectedWants := []plumbing.Hash{
497 plumbing.NewHash("1111111111111111111111111111111111111111"),
498 plumbing.NewHash("2222222222222222222222222222222222222222"),
499 plumbing.NewHash("3333333333333333333333333333333333333333"),
500 plumbing.NewHash("4444444444444444444444444444444444444444"),
501 }
502 sort.Sort(byHash(expectedWants))
503 sort.Sort(byHash(ur.Wants))
504 c.Assert(ur.Wants, DeepEquals, expectedWants)
505 c.Assert(ur.Capabilities.Supports(capability.OFSDelta), Equals, true)
506 c.Assert(ur.Capabilities.Supports(capability.MultiACK), Equals, true)
507
508 expectedShallows := []plumbing.Hash{
509 plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
510 plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
511 plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"),
512 plumbing.NewHash("dddddddddddddddddddddddddddddddddddddddd"),
513 }
514 sort.Sort(byHash(expectedShallows))
515 sort.Sort(byHash(ur.Shallows))
516 c.Assert(ur.Shallows, DeepEquals, expectedShallows)
517
518 c.Assert(ur.Depth, FitsTypeOf, DepthCommits(0))
519 commits, ok := ur.Depth.(DepthCommits)
520 c.Assert(ok, Equals, true)
521 c.Assert(int(commits), Equals, 1234)
522 }
523
524 func (s *UlReqDecodeSuite) TestExtraData(c *C) {
525 payloads := []string{
526 "want 3333333333333333333333333333333333333333 ofs-delta multi_ack",
527 "deepen 32",
528 "foo",
529 pktline.FlushString,
530 }
531 r := toPktLines(c, payloads)
532 s.testDecoderErrorMatches(c, r, ".*unexpected payload.*")
533 }