Commit dde2a732 authored by Rod Vagg's avatar Rod Vagg

chore: refactor header tests to iterate over a struct

parent 2876c180
......@@ -237,6 +237,58 @@ func TestEOFHandling(t *testing.T) {
}
func TestBadHeaders(t *testing.T) {
testCases := []struct {
name string
hex string
errStr string // either the whole error string
errPfx string // or just the prefix
}{
{
"{version:2}",
"0aa16776657273696f6e02",
"invalid car version: 2",
"",
},
{
// an unfortunate error because we don't use a pointer
"{roots:[baeaaaa3bmjrq]}",
"13a165726f6f747381d82a480001000003616263",
"invalid car version: 0",
"",
}, {
"{version:\"1\",roots:[baeaaaa3bmjrq]}",
"1da265726f6f747381d82a4800010000036162636776657273696f6e6131",
"", "invalid header: ",
}, {
"{version:1}",
"0aa16776657273696f6e01",
"empty car, no roots",
"",
}, {
"{version:1,roots:{cid:baeaaaa3bmjrq}}",
"20a265726f6f7473a163636964d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"{version:1,roots:[baeaaaa3bmjrq],blip:true}",
"22a364626c6970f565726f6f747381d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"[1,[]]",
"03820180",
"",
"invalid header: ",
}, {
// this is an unfortunate error, it'd be nice to catch it better but it's
// very unlikely we'd ever see this in practice
"null",
"01f6",
"",
"invalid car version: 0",
},
}
makeCar := func(t *testing.T, byts string) error {
fixture, err := hex.DecodeString(byts)
if err != nil {
......@@ -253,62 +305,18 @@ func TestBadHeaders(t *testing.T) {
}
})
t.Run("{version:2}", func(t *testing.T) {
err := makeCar(t, "0aa16776657273696f6e02")
if err.Error() != "invalid car version: 2" {
t.Fatalf("bad error: %v", err)
}
})
// an unfortunate error because we don't use a pointer
t.Run("{roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "13a165726f6f747381d82a480001000003616263")
if err.Error() != "invalid car version: 0" {
t.Fatalf("bad error: %v", err)
}
})
t.Run("{version:\"1\",roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "1da265726f6f747381d82a4800010000036162636776657273696f6e6131")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})
t.Run("{version:1}", func(t *testing.T) {
err := makeCar(t, "0aa16776657273696f6e01")
if err.Error() != "empty car, no roots" {
t.Fatalf("bad error: %v", err)
}
})
t.Run("{version:1,roots:{cid:baeaaaa3bmjrq}}", func(t *testing.T) {
err := makeCar(t, "20a265726f6f7473a163636964d82a4800010000036162636776657273696f6e01")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})
t.Run("{version:1,roots:[baeaaaa3bmjrq],blip:true}", func(t *testing.T) {
err := makeCar(t, "22a364626c6970f565726f6f747381d82a4800010000036162636776657273696f6e01")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})
t.Run("[1,[]]", func(t *testing.T) {
err := makeCar(t, "03820180")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})
// this is an unfortunate error, it'd be nice to catch it better but it's
// very unlikely we'd ever see this in practice
t.Run("null", func(t *testing.T) {
err := makeCar(t, "01f6")
if !strings.HasPrefix(err.Error(), "invalid car version: 0") {
t.Fatalf("bad error: %v", err)
}
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := makeCar(t, tc.hex)
if tc.errStr != "" {
if err.Error() != tc.errStr {
t.Fatalf("bad error: %v", err)
}
} else {
if !strings.HasPrefix(err.Error(), tc.errPfx) {
t.Fatalf("bad error: %v", err)
}
}
})
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment