From 9cbf92c52bd7da4fb80c86dc0f17f00ca76fd038 Mon Sep 17 00:00:00 2001 From: Katie Hockman Date: Wed, 10 Feb 2021 13:05:59 -0500 Subject: [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 Run-TryBot: Katie Hockman TryBot-Result: Go Bot Reviewed-by: Jay Conrod --- src/internal/fuzz/encoding_test.go | 115 +++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/internal/fuzz/encoding_test.go (limited to 'src/internal/fuzz/encoding_test.go') 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) + } + }) + } +} -- cgit v1.3