aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/fuzz.go
diff options
context:
space:
mode:
authorRomain Baugue <romain.baugue@gmail.com>2019-04-26 11:55:38 +0200
committerBrad Fitzpatrick <bradfitz@golang.org>2019-04-29 13:20:30 +0000
commit45ed3dbddf98eb421bc3aefd4c29c016a69c9ab1 (patch)
tree1e3d96876bf73777ca6145789eebb09cdc9dad1f /src/encoding/json/fuzz.go
parent3cf1d770809453fed8754cf3ddf4028cdd7716fe (diff)
downloadgo-45ed3dbddf98eb421bc3aefd4c29c016a69c9ab1.tar.xz
encoding/json: add a Fuzz function
Adds a sample Fuzz test function to package encoding/json following the guidelines defined in #31309, based on https://github.com/dvyukov/go-fuzz-corpus/blob/master/json/json.go Fixes #31309 Updates #19109 Change-Id: I5fe04d9a5f41c0de339f8518dae30896ec14e356 Reviewed-on: https://go-review.googlesource.com/c/go/+/174058 Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Dmitry Vyukov <dvyukov@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/fuzz.go')
-rw-r--r--src/encoding/json/fuzz.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/encoding/json/fuzz.go b/src/encoding/json/fuzz.go
new file mode 100644
index 0000000000..4872b6f7ee
--- /dev/null
+++ b/src/encoding/json/fuzz.go
@@ -0,0 +1,42 @@
+// Copyright 2019 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.
+
+// +build gofuzz
+
+package json
+
+import (
+ "fmt"
+)
+
+func Fuzz(data []byte) (score int) {
+ for _, ctor := range []func() interface{}{
+ func() interface{} { return new(interface{}) },
+ func() interface{} { return new(map[string]interface{}) },
+ func() interface{} { return new([]interface{}) },
+ } {
+ v := ctor()
+ err := Unmarshal(data, v)
+ if err != nil {
+ continue
+ }
+ score = 1
+
+ m, err := Marshal(v)
+ if err != nil {
+ fmt.Printf("v=%#v\n", v)
+ panic(err)
+ }
+
+ u := ctor()
+ err = Unmarshal(m, u)
+ if err != nil {
+ fmt.Printf("v=%#v\n", v)
+ fmt.Println("m=%s\n", string(m))
+ panic(err)
+ }
+ }
+
+ return
+}