Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better tests for types package #1070

Merged
merged 2 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion types/logroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ type LogRoot struct {
// the LOG_ROOT_FORMAT_V1 tag, and populates the caller with the deserialized
// *LogRootV1.
func (l *LogRootV1) UnmarshalBinary(logRootBytes []byte) error {
if logRootBytes == nil {
if len(logRootBytes) < 3 {
return fmt.Errorf("logRootBytes too short")
}
if l == nil {
return fmt.Errorf("nil log root")
}
version := binary.BigEndian.Uint16(logRootBytes)
Expand Down
11 changes: 11 additions & 0 deletions types/logroot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestUnmarshalLogRoot(t *testing.T) {
logRoot: []byte{0, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
wantErr: true,
},
{
// Incorrect type.
logRoot: []byte{0},
wantErr: true,
},
{logRoot: []byte("foo"), wantErr: true},
{logRoot: nil, wantErr: true},
} {
Expand All @@ -75,6 +80,12 @@ func TestUnmarshalLogRoot(t *testing.T) {
t.Errorf("UnmarshalBinary(): %v, wantErr %v", err, want)
}
}

// Unmarshaling to a nil should throw an error.
var nilPtr *LogRootV1
if err := nilPtr.UnmarshalBinary(MustMarshalLogRoot(&LogRootV1{})); err == nil {
t.Errorf("nil.UnmarshalBinary(): %v, want err", err)
}
}

func MustMarshalLogRoot(root *LogRootV1) []byte {
Expand Down
5 changes: 4 additions & 1 deletion types/maproot.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ type MapRoot struct {
// UnmarshalBinary verifies that mapRootBytes is a TLS serialized MapRoot,
// has the MAP_ROOT_FORMAT_V1 tag, and returns the deserialized *MapRootV1.
func (m *MapRootV1) UnmarshalBinary(mapRootBytes []byte) error {
if mapRootBytes == nil {
if len(mapRootBytes) < 3 {
return fmt.Errorf("mapRootBytes too short")
}
if m == nil {
return fmt.Errorf("nil map root")
}
version := binary.BigEndian.Uint16(mapRootBytes)
Expand Down
10 changes: 10 additions & 0 deletions types/maproot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func TestUnmarshalMapRoot(t *testing.T) {
mapRoot: []byte{0, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
wantErr: true,
},
{
// Incorrect type
mapRoot: []byte{0},
wantErr: true,
},
{mapRoot: []byte("foo"), wantErr: true},
{mapRoot: nil, wantErr: true},
} {
Expand All @@ -86,4 +91,9 @@ func TestUnmarshalMapRoot(t *testing.T) {
t.Errorf("UnmarshalBinary(): \n%#v, want: \n%#v", got, want)
}
}
// Unmarshaling to a nil should throw an error.
var nilPtr *MapRootV1
if err := nilPtr.UnmarshalBinary(MustMarshalMapRoot(&MapRootV1{})); err == nil {
t.Errorf("nil.UnmarshalBinary(): %v, want err", err)
}
}