common: added Hash unmarshal json length validation

This commit is contained in:
Jeffrey Wilcke
2016-04-01 12:03:06 +02:00
parent 10d3466c93
commit d63e29241d
2 changed files with 35 additions and 0 deletions

View File

@ -29,3 +29,25 @@ func TestBytesConversion(t *testing.T) {
t.Errorf("expected %x got %x", exp, hash)
}
}
func TestHashJsonValidation(t *testing.T) {
var h Hash
var tests = []struct {
Prefix string
Size int
Error error
}{
{"", 2, hashJsonLengthErr},
{"", 62, hashJsonLengthErr},
{"", 66, hashJsonLengthErr},
{"", 65, hashJsonLengthErr},
{"0X", 64, nil},
{"0x", 64, nil},
{"0x", 62, hashJsonLengthErr},
}
for i, test := range tests {
if err := h.UnmarshalJSON(append([]byte(test.Prefix), make([]byte, test.Size)...)); err != test.Error {
t.Error(i, "expected", test.Error, "got", err)
}
}
}