push_test.go
⎇
Raw
1package ci
2
3import (
4 "fmt"
5 "testing"
6)
7
8// pkt builds one pkt-line with its 4-digit hex length prefix.
9func pkt(payload string) string {
10 return fmt.Sprintf("%04x%s", len(payload)+4, payload)
11}
12
13func TestParsePktLineRefUpdates(t *testing.T) {
14 const zero = "0000000000000000000000000000000000000000"
15 const shaA = "5f4dcc3b5aa765d61d8327deb882cf99aaaaaaaa"
16 const shaB = "aabbccddeeff00112233445566778899aabbccdd"
17
18 // A real push sends capabilities after a NUL on the first line, then a
19 // flush packet, then the pack data.
20 stream := pkt(zero+" "+shaA+" refs/heads/main\x00report-status side-band-64k agent=git/2.43.0\n") +
21 pkt(shaA+" "+shaB+" refs/tags/v1.0\n") +
22 pkt(shaB+" "+zero+" refs/heads/old\n") +
23 "0000PACK\x00\x02\x00junk"
24
25 got := parsePktLineRefUpdates(stream)
26 want := []refUpdate{
27 {zero, shaA, "refs/heads/main"},
28 {shaA, shaB, "refs/tags/v1.0"},
29 {shaB, zero, "refs/heads/old"},
30 }
31 if len(got) != len(want) {
32 t.Fatalf("got %d refs, want %d: %+v", len(got), len(want), got)
33 }
34 for i := range want {
35 if got[i] != want[i] {
36 t.Errorf("ref %d = %+v, want %+v", i, got[i], want[i])
37 }
38 }
39}
40
41func TestParsePktLineRefUpdatesTruncated(t *testing.T) {
42 const shaA = "5f4dcc3b5aa765d61d8327deb882cf99aaaaaaaa"
43 full := pkt(shaA + " " + shaA + " refs/heads/main\n")
44 // Cut the stream mid-line, as the 4 KB preamble cap can do.
45 got := parsePktLineRefUpdates(full[:len(full)-5])
46 if len(got) != 0 {
47 t.Fatalf("truncated line should be dropped, got %+v", got)
48 }
49}
50
51func TestParsePktLineRefUpdatesGarbage(t *testing.T) {
52 for _, in := range []string{"", "zz", "zzzzhello", "0001", "0002ab"} {
53 if got := parsePktLineRefUpdates(in); len(got) != 0 {
54 t.Errorf("input %q: got %+v, want none", in, got)
55 }
56 }
57}
58