From 40727808b3be0427272d174e0180ca93c5ba6295 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 17 Nov 2020 17:30:05 +0100 Subject: added unmarshal tests --- type_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'type_test.go') diff --git a/type_test.go b/type_test.go index 0033ec8..cdebff3 100644 --- a/type_test.go +++ b/type_test.go @@ -327,3 +327,60 @@ func TestEncDecStItem(t *testing.T) { } } } + +// TestStItemUnmarshal tests that invalid ST items cannot be unmarshaled +func TestStItemUnmarshalFailure(t *testing.T) { + b, err := NewChecksumV1(testPackage, testChecksum).Marshal() + if err != nil { + t.Errorf("must marshal ChecksumV1 StItem: %v", err) + return + } + + var checksum StItem + if err := checksum.Unmarshal(append(b[:], []byte{0}...)); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: one extra byte") + } + if err := checksum.Unmarshal(append(b[:], b[:]...)); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: one extra b") + } + if err := checksum.Unmarshal([]byte{0}); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: just a single byte") + } + + b[0] = byte(len(testPackage)) + 1 // will mess up the first length specifier + if err := checksum.Unmarshal(b); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: bad length") + } + + if err := checksum.UnmarshalB64("@" + b64(b[1:])); err == nil { + t.Errorf("succeded unmarshaling base64 but wanted error: bad byte") + } +} + +// TestAppendixUnmarshal tests that invalid appendices cannot be unmarshaled +func TestAppendixUnmarshalFailure(t *testing.T) { + chain, err := x509util.NewCertificateList(testdata.PemChain) + if err != nil { + t.Fatalf("must decode certificate chain: %v", err) + } + b, err := NewAppendix(chain, testSignature, uint16(testSignatureScheme)).Marshal() + if err != nil { + t.Fatalf("must marshal Appendix: %v", err) + } + + var appendix Appendix + if err := appendix.Unmarshal(append(b[:], []byte{0}...)); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: one extra byte") + } + if err := appendix.Unmarshal(append(b[:], b[:]...)); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: one extra item") + } + if err := appendix.Unmarshal([]byte{0}); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: just a single byte") + } + + b[0] = byte(len(testSignature)) + 1 // will mess up the first length specifier + if err := appendix.Unmarshal(b); err == nil { + t.Errorf("succeeded unmarshaling but wanted error: bad length") + } +} -- cgit v1.2.3