package ci import ( "fmt" "testing" ) // pkt builds one pkt-line with its 4-digit hex length prefix. func pkt(payload string) string { return fmt.Sprintf("%04x%s", len(payload)+4, payload) } func TestParsePktLineRefUpdates(t *testing.T) { const zero = "0000000000000000000000000000000000000000" const shaA = "5f4dcc3b5aa765d61d8327deb882cf99aaaaaaaa" const shaB = "aabbccddeeff00112233445566778899aabbccdd" // A real push sends capabilities after a NUL on the first line, then a // flush packet, then the pack data. stream := pkt(zero+" "+shaA+" refs/heads/main\x00report-status side-band-64k agent=git/2.43.0\n") + pkt(shaA+" "+shaB+" refs/tags/v1.0\n") + pkt(shaB+" "+zero+" refs/heads/old\n") + "0000PACK\x00\x02\x00junk" got := parsePktLineRefUpdates(stream) want := []refUpdate{ {zero, shaA, "refs/heads/main"}, {shaA, shaB, "refs/tags/v1.0"}, {shaB, zero, "refs/heads/old"}, } if len(got) != len(want) { t.Fatalf("got %d refs, want %d: %+v", len(got), len(want), got) } for i := range want { if got[i] != want[i] { t.Errorf("ref %d = %+v, want %+v", i, got[i], want[i]) } } } func TestParsePktLineRefUpdatesTruncated(t *testing.T) { const shaA = "5f4dcc3b5aa765d61d8327deb882cf99aaaaaaaa" full := pkt(shaA + " " + shaA + " refs/heads/main\n") // Cut the stream mid-line, as the 4 KB preamble cap can do. got := parsePktLineRefUpdates(full[:len(full)-5]) if len(got) != 0 { t.Fatalf("truncated line should be dropped, got %+v", got) } } func TestParsePktLineRefUpdatesGarbage(t *testing.T) { for _, in := range []string{"", "zz", "zzzzhello", "0001", "0002ab"} { if got := parsePktLineRefUpdates(in); len(got) != 0 { t.Errorf("input %q: got %+v, want none", in, got) } } }