Skip to content

Commit

Permalink
Better tests for types package (#1070)
Browse files Browse the repository at this point in the history
* Add nil check to types.UnmarshalBinary

* types nits
  • Loading branch information
gdbelvin authored Mar 26, 2018
1 parent 734f47a commit facffc9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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)
}
}

0 comments on commit facffc9

Please sign in to comment.