diff options
| author | Katie Hockman <katie@golang.org> | 2021-02-10 13:05:59 -0500 |
|---|---|---|
| committer | Katie Hockman <katie@golang.org> | 2021-02-17 16:04:01 +0000 |
| commit | 9cbf92c52bd7da4fb80c86dc0f17f00ca76fd038 (patch) | |
| tree | 12310ff76ca48ca397e844e9997e100ce7bdcdad /src/internal/fuzz/encoding_test.go | |
| parent | 7743f60b5a01b5892edda7311484a2c2bc207ea2 (diff) | |
| download | go-9cbf92c52bd7da4fb80c86dc0f17f00ca76fd038.tar.xz | |
[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>
Diffstat (limited to 'src/internal/fuzz/encoding_test.go')
| -rw-r--r-- | src/internal/fuzz/encoding_test.go | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/internal/fuzz/encoding_test.go b/src/internal/fuzz/encoding_test.go new file mode 100644 index 0000000000..98d3e21c1f --- /dev/null +++ b/src/internal/fuzz/encoding_test.go @@ -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) + } + }) + } +} |
