MCPcopy Create free account
hub / github.com/segmentio/encoding / Fuzz

Function Fuzz

json/fuzz/fuzz.go:27–97  ·  view source on GitHub ↗
(data []byte)

Source from the content-addressed store, hash-verified

25}
26
27func Fuzz(data []byte) int {
28 score := 0
29 for _, ctor := range []func() any{
30 func() any { return nil },
31 func() any { return new([]any) },
32 func() any { m := map[string]string{}; return &m },
33 func() any { m := map[string]any{}; return &m },
34 func() any { return new(S) },
35 } {
36 // Note: we modified the test to verify that we behavior like the
37 // standard encoding/json package, whether it's right or wrong.
38 v1 := ctor()
39 v2 := ctor()
40
41 err1 := encodingJSON.Unmarshal(data, v1)
42 err2 := json.Unmarshal(data, v2)
43
44 if err1 != nil {
45 if err2 != nil {
46 // both implementations report an error
47 if reflect.TypeOf(err1) != reflect.TypeOf(err2) {
48 fmt.Printf("input: %s\n", string(data))
49 fmt.Printf("encoding/json.Unmarshal(%T): %T: %s\n", v1, err1, err1)
50 fmt.Printf("segmentio/encoding/json.Unmarshal(%T): %T: %s\n", v2, err2, err2)
51 panic("error types mismatch")
52 }
53 continue
54 } else {
55 fmt.Printf("input: %s\n", string(data))
56 fmt.Printf("encoding/json.Unmarshal(%T): %T: %s\n", v1, err1, err1)
57 fmt.Printf("segmentio/encoding/json.Unmarshal(%T): <nil>\n")
58 panic("error values mismatch")
59 }
60 } else {
61 if err2 != nil {
62 fmt.Printf("input: %s\n", string(data))
63 fmt.Printf("encoding/json.Unmarshal(%T): <nil>\n")
64 fmt.Printf("segmentio/encoding/json.Unmarshal(%T): %T: %s\n", v2, err2, err2)
65 panic("error values mismatch")
66 } else {
67 // both implementations pass
68 }
69 }
70
71 score = 1
72 fixS(v1)
73 fixS(v2)
74 if !fuzz.DeepEqual(v1, v2) {
75 fmt.Printf("input: %s\n", string(data))
76 fmt.Printf("encoding/json: %#v\n", v1)
77 fmt.Printf("segmentio/encoding: %#v\n", v2)
78 panic("not equal")
79 }
80
81 data1, err := encodingJSON.Marshal(v1)
82 if err != nil {
83 panic(err)
84 }

Callers

nothing calls this directly

Calls 5

UnmarshalFunction · 0.92
MarshalFunction · 0.92
fixSFunction · 0.85
UnmarshalMethod · 0.65
MarshalMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…