Skip to content

Commit

Permalink
configarg{,_test}, header_test: fix crasher found by go-fuzz, add mar…
Browse files Browse the repository at this point in the history
…shal/unmarshal tests to ensure consistency going forward
  • Loading branch information
mdlayher committed Jul 30, 2015
1 parent 962d643 commit 286c877
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
8 changes: 0 additions & 8 deletions configarg.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,12 @@ const (

// MarshalBinary allocates a byte slice containing the data from a ConfigArg.
//
// If c.Version does not equal Version (1), ErrorUnsupportedVersion is
// returned.
//
// If any of the following conditions occur, ErrorBadArgumentParameter is
// returned:
// - c.Command is larger than a 4-bit integer (0xf)
// - c.StringLength does not indicate the actual length of c.String
// - c.StringLength is greater than 1024
func (c *ConfigArg) MarshalBinary() ([]byte, error) {
// Version must be 1
if c.Version != Version {
return nil, ErrorUnsupportedVersion
}

// Command must be a 4-bit integer
if c.Command > 0xf {
return nil, ErrorBadArgumentParameter
Expand Down
5 changes: 0 additions & 5 deletions configarg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ func TestConfigArgMarshalBinary(t *testing.T) {
b []byte
err error
}{
{
desc: "empty ConfigArg",
c: &ConfigArg{},
err: ErrorUnsupportedVersion,
},
{
desc: "command greater than 4-bit integer",
c: &ConfigArg{
Expand Down
59 changes: 59 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,65 @@ func TestHeaderMarshalBinary(t *testing.T) {
}
}

func TestHeaderUnmarshalAndMarshalBinary(t *testing.T) {
var tests = []struct {
desc string
b []byte
}{
{
desc: "go-fuzz crasher: ConfigArg.Version accepted in unmarshal, but rejected in marshal",
b: []byte("\x100000\x010000000000\x00\x00"),
},
{
desc: "header with CommandIssueATACommand, ATAArg OK",
b: []byte{
0x10, 0, 0, 1, 2, 0, 0, 0, 0, 10,
0x53, 1, 2, 3, 6, 6, 6, 6, 6, 6, 0, 0, 'f', 'o', 'o',
},
},
{
desc: "header with CommandQueryConfigInformation, ConfigArg OK",
b: []byte{
0x10, 0, 0, 1, 2, 1, 0, 0, 0, 10,
0, 10, 0, 1, 2, 0x11, 0, 3, 'f', 'o', 'o',
},
},
{
desc: "header with CommandMACMaskList, MACMaskArg OK",
b: []byte{
0x10, 0, 0, 1, 2, 2, 0, 0, 0, 10,
0, 0, 0, 1,
0, 1, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
},
},
{
desc: "header with CommandReserveRelease, ReserveReleaseArg OK",
b: []byte{
0x10, 0, 0, 1, 2, 3, 0, 0, 0, 10,
0, 1,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
},
},
}

for i, tt := range tests {
h := new(Header)
if err := h.UnmarshalBinary(tt.b); err != nil {
t.Fatalf("[%02d] unmarshal test %q, %v", i, tt.desc, err)
}

b, err := h.MarshalBinary()
if err != nil {
t.Fatalf("[%02d] marshal test %q, %v", i, tt.desc, err)
}

if want, got := tt.b, b; !bytes.Equal(want, got) {
t.Fatalf("[%02d] test %q, unexpected bytes:\n- want: %v\n- got: %v",
i, tt.desc, want, got)
}
}
}

func TestHeaderUnmarshalBinary(t *testing.T) {
var tests = []struct {
desc string
Expand Down

0 comments on commit 286c877

Please sign in to comment.