-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_fuzz_test.go
157 lines (139 loc) · 4.07 KB
/
ext_fuzz_test.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package jsoniterpb_test
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
gofuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
"github.com/molon/jsoniterpb"
testv1 "github.com/molon/jsoniterpb/internal/gen/go/test/v1"
"github.com/molon/jsoniterpb/internal/gen/go/test/v1/testv1fuzz"
"github.com/srikrsna/goprotofuzz"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/structpb"
)
func fuzzNullValue(x *structpb.NullValue, f gofuzz.Continue) {
*x = structpb.NullValue_NULL_VALUE
}
func fuzzListValue(msg *structpb.ListValue, c gofuzz.Continue) {
fc := c.Rand.Intn(21)
msg.Values = make([]*structpb.Value, fc)
for i := 0; i < fc; i++ {
var v *structpb.Value
switch c.Int() % 3 {
case 0:
v = structpb.NewNumberValue(c.Float64())
case 1:
v = structpb.NewBoolValue(c.RandBool())
case 2:
v = structpb.NewStringValue(c.RandString())
}
msg.Values[i] = v
}
}
func fuzzValue(x *structpb.Value, c gofuzz.Continue) {
var v *structpb.Value
switch c.Int() % 3 {
case 0:
v = structpb.NewNumberValue(c.Float64())
case 1:
v = structpb.NewBoolValue(c.RandBool())
case 2:
v = structpb.NewStringValue(c.RandString())
}
x.Kind = v.Kind
}
func fuzzFieldMask(x *fieldmaskpb.FieldMask, c gofuzz.Continue) {
fc := c.Rand.Intn(21) + 1
x.Paths = make([]string, fc)
for i := 0; i < fc; i++ {
x.Paths[i] = "a" //jsoniterpb.JSONSnakeCase(c.RandString())
}
}
func appendFuzzFuncs(f *gofuzz.Fuzzer) *gofuzz.Fuzzer {
return f.Funcs(testv1fuzz.FuzzFuncs()...).Funcs(goprotofuzz.FuzzWKT[:]...).Funcs(fuzzNullValue, fuzzValue, fuzzListValue, fuzzFieldMask)
}
func FuzzReadWrite(f *testing.F) {
cfg := jsoniter.Config{}.Froze()
cfg.RegisterExtension(&jsoniterpb.ProtoExtension{})
f.Add([]byte("0"))
f.Add([]byte("7"))
f.Add([]byte("8"))
f.Add([]byte("9"))
f.Add([]byte(""))
f.Add([]byte("\u007f\x922"))
f.Fuzz(func(t *testing.T, data []byte) {
f := appendFuzzFuncs(gofuzz.NewFromGoFuzz(data))
var before testv1.All
f.Fuzz(&before)
jsn, err := cfg.Marshal(&before)
if err != nil {
t.Fatal("marshal error: ", err)
}
if !json.Valid(jsn) {
t.Fatal("invalid json: ", string(jsn))
}
var after testv1.All
err = cfg.Unmarshal(jsn, &after)
if err != nil {
t.Fatal("unmarshal error: ", err)
}
if diff := cmp.Diff(&before, &after, protocmp.Transform(), cmpopts.EquateNaNs()); diff != "" {
t.Errorf("before and after did not match:\n %s", diff)
t.Error(string(jsn))
}
})
}
func FuzzReadFromProtoJson(f *testing.F) {
cfg := jsoniter.Config{}.Froze()
cfg.RegisterExtension(&jsoniterpb.ProtoExtension{})
f.Add([]byte("0"))
f.Fuzz(func(t *testing.T, data []byte) {
f := appendFuzzFuncs(gofuzz.NewFromGoFuzz(data))
var before testv1.All
f.Fuzz(&before)
jsonData, err := protojson.Marshal(&before)
if err != nil {
t.Fatal("marshal error: ", err)
}
if !json.Valid(jsonData) {
t.Fatal("invalid json: ", string(jsonData))
}
var after testv1.All
err = cfg.Unmarshal(jsonData, &after)
if err != nil {
t.Fatal("unmarshal error: ", err)
}
if diff := cmp.Diff(&before, &after, protocmp.Transform(), cmpopts.EquateNaNs()); diff != "" {
t.Error("before and after did not match", diff)
t.Error(string(jsonData))
}
})
}
func FuzzWriteToProtoJson(f *testing.F) {
cfg := jsoniter.Config{}.Froze()
cfg.RegisterExtension(&jsoniterpb.ProtoExtension{})
f.Fuzz(func(t *testing.T, data []byte) {
f := appendFuzzFuncs(gofuzz.NewFromGoFuzz(data))
var before testv1.All
f.Fuzz(&before)
jsn, err := cfg.Marshal(&before)
if err != nil {
t.Fatal("marshal error: ", err)
}
if !json.Valid(jsn) {
t.Fatal("invalid json: ", string(jsn))
}
var after testv1.All
if err := protojson.Unmarshal(jsn, &after); err != nil {
t.Fatal(err, string(jsn))
}
if diff := cmp.Diff(&before, &after, protocmp.Transform(), cmpopts.EquateNaNs()); diff != "" {
t.Error("before and after did not match", diff)
t.Error(string(jsn))
}
})
}