forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfuzz_test.go
More file actions
63 lines (52 loc) · 1.34 KB
/
fuzz_test.go
File metadata and controls
63 lines (52 loc) · 1.34 KB
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
package jsonpatch_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"gomodules.xyz/jsonpatch/v2"
jp "gopkg.in/evanphx/json-patch.v4"
)
func FuzzCreatePatch(f *testing.F) {
add := func(a, b string) {
f.Add([]byte(a), []byte(b))
}
add(simpleA, simpleB)
add(superComplexBase, superComplexA)
add(hyperComplexBase, hyperComplexA)
add(arraySrc, arrayDst)
add(empty, simpleA)
add(point, lineString)
f.Fuzz(func(t *testing.T, a, b []byte) {
checkFuzz(t, a, b)
})
}
func checkFuzz(t *testing.T, src, dst []byte) {
t.Logf("Test: %v -> %v", string(src), string(dst))
patch, err := jsonpatch.CreatePatch(src, dst)
if err != nil {
// Ok to error, src or dst may be invalid
t.Skip()
}
// Applying library only works with arrays and structs, no primitives
// We still do CreatePatch to make sure it doesn't panic
if isPrimitive(src) || isPrimitive(dst) {
return
}
for _, p := range patch {
if p.Path == "" {
// json-patch doesn't handle this properly, but it is valid
return
}
}
data, err := json.Marshal(patch)
assert.Nil(t, err)
t.Logf("Applying patch %v", string(data))
p2, err := jp.DecodePatch(data)
assert.Nil(t, err)
d2, err := p2.Apply(src)
assert.Nil(t, err)
assert.JSONEq(t, string(dst), string(d2))
}
func isPrimitive(data []byte) bool {
return data[0] != '{' && data[0] != '['
}