|
| 1 | +package gitproto |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParsePktLine_Empty(t *testing.T) { |
| 9 | + pkts, ok := parsePktLine(nil) |
| 10 | + if !ok { |
| 11 | + t.Error("expected ok=true for empty input") |
| 12 | + } |
| 13 | + if len(pkts) != 0 { |
| 14 | + t.Fatalf("expected 0 packets, got %d", len(pkts)) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestParsePktLine_SpecialPackets(t *testing.T) { |
| 19 | + cases := map[string]pktType{ |
| 20 | + "0000": pktFlush, |
| 21 | + "0001": pktDelim, |
| 22 | + "0002": pktResponseEnd, |
| 23 | + } |
| 24 | + for input, want := range cases { |
| 25 | + pkts, ok := parsePktLine([]byte(input)) |
| 26 | + if !ok || len(pkts) != 1 || pkts[0].typ != want { |
| 27 | + t.Errorf("input %q: got %+v ok=%v, want type %d", input, pkts, ok, want) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestParsePktLine_DataPacket(t *testing.T) { |
| 33 | + // "000ahello\n" = length 0x000a (10), payload "hello\n" |
| 34 | + pkts, ok := parsePktLine([]byte("000ahello\n")) |
| 35 | + if !ok || len(pkts) != 1 || pkts[0].typ != pktData || string(pkts[0].payload) != "hello\n" { |
| 36 | + t.Errorf("got %+v ok=%v", pkts, ok) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestParsePktLine_MalformedAndTruncated(t *testing.T) { |
| 41 | + // Bad hex prefix. |
| 42 | + if _, ok := parsePktLine([]byte("gggghi")); ok { |
| 43 | + t.Error("expected ok=false for malformed length prefix") |
| 44 | + } |
| 45 | + // Length claims 0x0020 but only 9 bytes available. |
| 46 | + if _, ok := parsePktLine([]byte("0020short")); ok { |
| 47 | + t.Error("expected ok=false for truncated packet") |
| 48 | + } |
| 49 | + // Length 3 is reserved; we treat as malformed. |
| 50 | + if _, ok := parsePktLine([]byte("00030000")); ok { |
| 51 | + t.Error("expected ok=false for reserved length 3") |
| 52 | + } |
| 53 | + // Less than 4 bytes. |
| 54 | + if _, ok := parsePktLine([]byte("ab")); ok { |
| 55 | + t.Error("expected ok=false for sub-prefix input") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestParsePktLine_RealV1Body(t *testing.T) { |
| 60 | + // Realistic v1 upload-pack body from github.com/octocat/Hello-World |
| 61 | + input := "00a4want 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d multi_ack_detailed no-done side-band-64k thin-pack no-progress ofs-delta deepen-since deepen-not agent=git/2.43.0\n" + |
| 62 | + "0032want b1b3f9723831141a31a1a7252a213e216ea76e56\n" + |
| 63 | + "0000" + |
| 64 | + "0032have 553c2077f0edc3d5dc5d17262f6aa498e69d6f8e\n" + |
| 65 | + "0009done\n" |
| 66 | + pkts, ok := parsePktLine([]byte(input)) |
| 67 | + if !ok { |
| 68 | + t.Fatal("expected ok=true for well-formed v1 body") |
| 69 | + } |
| 70 | + wantTypes := []pktType{pktData, pktData, pktFlush, pktData, pktData} |
| 71 | + if len(pkts) != len(wantTypes) { |
| 72 | + t.Fatalf("got %d packets, want %d", len(pkts), len(wantTypes)) |
| 73 | + } |
| 74 | + for i, want := range wantTypes { |
| 75 | + if pkts[i].typ != want { |
| 76 | + t.Errorf("packet %d: got type %d, want %d", i, pkts[i].typ, want) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestParsePktLine_RealV2Body(t *testing.T) { |
| 82 | + input := "0012command=fetch\n" + |
| 83 | + "0015agent=git/2.43.0\n" + |
| 84 | + "0001" + |
| 85 | + "000ddeepen 1\n" + |
| 86 | + "0032want 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d\n" + |
| 87 | + "0009done\n" + |
| 88 | + "0000" |
| 89 | + pkts, ok := parsePktLine([]byte(input)) |
| 90 | + if !ok || len(pkts) != 7 { |
| 91 | + t.Fatalf("got %d packets ok=%v, want 7 ok=true", len(pkts), ok) |
| 92 | + } |
| 93 | + if pkts[2].typ != pktDelim || pkts[6].typ != pktFlush { |
| 94 | + t.Error("special packets misidentified") |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestEncodePktLine_RoundTrip(t *testing.T) { |
| 99 | + input := []byte("000ahello\n" + "0000" + "0001" + "000aworld\n" + "0002") |
| 100 | + pkts, ok := parsePktLine(input) |
| 101 | + if !ok { |
| 102 | + t.Fatal("parse failed on well-formed input") |
| 103 | + } |
| 104 | + if got := encodePktLine(pkts); !bytes.Equal(got, input) { |
| 105 | + t.Errorf("round-trip mismatch:\n in: %q\n out: %q", input, got) |
| 106 | + } |
| 107 | +} |
0 commit comments