diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-11-17 17:30:05 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-11-17 17:30:05 +0100 |
commit | 40727808b3be0427272d174e0180ca93c5ba6295 (patch) | |
tree | ebbbbe3249f14046bf35160d63d34f49fa84a979 /type_test.go | |
parent | 24c9cba5648e9b9c5913a2a88af78dcca768b36f (diff) |
added unmarshal tests
Diffstat (limited to 'type_test.go')
-rw-r--r-- | type_test.go | 57 |
1 files changed, 57 insertions, 0 deletions
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") + } +} |