[dev.fuzz] internal/fuzz: support corpus file encoding/decoding

Change-Id: Id245ce5f154557f1744210e7d7f061d08901c746
Reviewed-on: https://go-review.googlesource.com/c/go/+/290951
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Katie Hockman 2021-02-10 13:05:59 -05:00
parent 7743f60b5a
commit 9cbf92c52b
2 changed files with 355 additions and 0 deletions

View file

@ -0,0 +1,115 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fuzz
import (
"strings"
"testing"
)
func TestUnmarshalMarshal(t *testing.T) {
var tests = []struct {
in string
ok bool
}{
{
in: "int(1234)",
ok: false, // missing version
},
{
in: `version 1
string("a"bcad")`,
ok: false, // malformed
},
{
in: `version 1
int()`,
ok: false, // empty value
},
{
in: `version 1
uint(-32)`,
ok: false, // invalid negative uint
},
{
in: `version 1
int8(1234456)`,
ok: false, // int8 too large
},
{
in: `version 1
int(20*5)`,
ok: false, // expression in int value
},
{
in: `version 1
int(--5)`,
ok: false, // expression in int value
},
{
in: `version 1
bool(0)`,
ok: false, // malformed bool
},
{
in: `version 1
byte('aa)`,
ok: false, // malformed byte
},
{
in: `version 1
byte('☃')`,
ok: false, // byte out of range
},
{
in: `version 1
string("extra")
[]byte("spacing")
`,
ok: true,
},
{
in: `version 1
int(-23)
int8(-2)
int64(2342425)
uint(1)
uint16(234)
uint32(352342)
uint64(123)
rune('œ')
byte('K')
byte('ÿ')
[]byte("hello¿")
[]byte("a")
bool(true)
string("hello\\xbd\\xb2=\\xbc ⌘")
float64(-12.5)
float32(2.5)`,
ok: true,
},
}
for _, test := range tests {
t.Run(test.in, func(t *testing.T) {
vals, err := unmarshalCorpusFile([]byte(test.in))
if test.ok && err != nil {
t.Fatalf("unmarshal unexpected error: %v", err)
} else if !test.ok && err == nil {
t.Fatalf("unmarshal unexpected success")
}
if !test.ok {
return // skip the rest of the test
}
newB := marshalCorpusFile(vals...)
if err != nil {
t.Fatalf("marshal unexpected error: %v", err)
}
want := strings.TrimSpace(test.in)
if want != string(newB) {
t.Errorf("values changed after unmarshal then marshal\nbefore: %q\nafter: %q", want, newB)
}
})
}
}