aboutsummaryrefslogtreecommitdiff
path: root/src/internal/fuzz/encoding_test.go
diff options
context:
space:
mode:
authorKatie Hockman <katie@golang.org>2021-09-10 11:05:29 -0400
committerKatie Hockman <katie@golang.org>2021-09-10 19:27:54 +0000
commitd106089fa6aa69cc1b547c68ca19d84f28062c71 (patch)
treebc6c1328910788843d8adaff1d9ce3c974e5a1b1 /src/internal/fuzz/encoding_test.go
parent363f2f3df99f3edd15609cc6bea2a2c6f423ce2c (diff)
downloadgo-d106089fa6aa69cc1b547c68ca19d84f28062c71.tar.xz
[dev.fuzz] internal/fuzz: write a newline to the end of a corpus file
If someone manually adds/alters a corpus file to add extra spacing or remove the final newline, the file can still be decoded. However, this change ensures that the fuzzing engine correctly writes the final newline. Fixes golang/go#48130 Change-Id: Ib5556d4a6e4e0bfd9bc2edab357b7c25bedfd176 Reviewed-on: https://go-review.googlesource.com/c/go/+/349055 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.go20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/internal/fuzz/encoding_test.go b/src/internal/fuzz/encoding_test.go
index 314f82a995..b429d429c6 100644
--- a/src/internal/fuzz/encoding_test.go
+++ b/src/internal/fuzz/encoding_test.go
@@ -66,16 +66,21 @@ byte('☃')`,
},
{
in: `go test fuzz v1
+string("has final newline")
+`,
+ ok: true, // has final newline
+ },
+ {
+ in: `go test fuzz v1
string("extra")
[]byte("spacing")
`,
- ok: true,
+ ok: true, // extra spaces in the final newline
},
{
in: `go test fuzz v1
float64(0)
-float32(0)
-`,
+float32(0)`,
ok: true, // will be an integer literal since there is no decimal
},
{
@@ -114,9 +119,12 @@ float32(2.5)`,
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)
+ if newB[len(newB)-1] != '\n' {
+ t.Error("didn't write final newline to corpus file")
+ }
+ before, after := strings.TrimSpace(test.in), strings.TrimSpace(string(newB))
+ if before != after {
+ t.Errorf("values changed after unmarshal then marshal\nbefore: %q\nafter: %q", before, after)
}
})
}