forked from segmentio/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz.go
133 lines (119 loc) · 3.07 KB
/
fuzz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 go-fuzz project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package fuzz
import (
"bytes"
encodingJSON "encoding/json"
"fmt"
"reflect"
"github.com/dvyukov/go-fuzz-corpus/fuzz"
"github.com/cloudentity/encoding/json"
)
func fixS(v interface{}) {
if s, ok := v.(*S); ok {
if len(s.P) == 0 {
s.P = []byte(`""`)
}
}
}
func Fuzz(data []byte) int {
score := 0
for _, ctor := range []func() interface{}{
func() interface{} { return nil },
func() interface{} { return new([]interface{}) },
func() interface{} { m := map[string]string{}; return &m },
func() interface{} { m := map[string]interface{}{}; return &m },
func() interface{} { return new(S) },
} {
// Note: we modified the test to verify that we behavior like the
// standard encoding/json package, whether it's right or wrong.
v1 := ctor()
v2 := ctor()
err1 := encodingJSON.Unmarshal(data, v1)
err2 := json.Unmarshal(data, v2)
if err1 != nil {
if err2 != nil {
// both implementations report an error
if reflect.TypeOf(err1) != reflect.TypeOf(err2) {
fmt.Printf("input: %s\n", string(data))
fmt.Printf("encoding/json.Unmarshal(%T): %T: %s\n", v1, err1, err1)
fmt.Printf("cloudentity/encoding/json.Unmarshal(%T): %T: %s\n", v2, err2, err2)
panic("error types mismatch")
}
continue
} else {
fmt.Printf("input: %s\n", string(data))
fmt.Printf("encoding/json.Unmarshal(%T): %T: %s\n", v1, err1, err1)
fmt.Printf("cloudentity/encoding/json.Unmarshal(%T): <nil>\n")
panic("error values mismatch")
}
} else {
if err2 != nil {
fmt.Printf("input: %s\n", string(data))
fmt.Printf("encoding/json.Unmarshal(%T): <nil>\n")
fmt.Printf("cloudentity/encoding/json.Unmarshal(%T): %T: %s\n", v2, err2, err2)
panic("error values mismatch")
} else {
// both implementations pass
}
}
score = 1
fixS(v1)
fixS(v2)
if !fuzz.DeepEqual(v1, v2) {
fmt.Printf("input: %s\n", string(data))
fmt.Printf("encoding/json: %#v\n", v1)
fmt.Printf("cloudentity/encoding: %#v\n", v2)
panic("not equal")
}
data1, err := encodingJSON.Marshal(v1)
if err != nil {
panic(err)
}
data2, err := json.Marshal(v2)
if err != nil {
panic(err)
}
if !bytes.Equal(data1, data2) {
fmt.Printf("input: %s\n", string(data))
fmt.Printf("encoding/json: %s\n", string(data1))
fmt.Printf("cloudentity/encoding: %s\n", string(data2))
panic("not equal")
}
}
return score
}
type S struct {
A int `json:",omitempty"`
B string `json:"B1,omitempty"`
C float64
D bool
E uint8
F []byte
G interface{}
H map[string]interface{}
I map[string]string
J []interface{}
K []string
L S1
M *S1
N *int
O **int
P json.RawMessage
Q Marshaller
R int `json:"-"`
S int `json:",string"`
}
type S1 struct {
A int
B string
}
type Marshaller struct {
v string
}
func (m *Marshaller) MarshalJSON() ([]byte, error) {
return json.Marshal(m.v)
}
func (m *Marshaller) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.v)
}