-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_fuzz_test.go
72 lines (66 loc) · 1.86 KB
/
split_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
package protoparts
import (
"bytes"
"encoding/base64"
"os/exec"
"testing"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
"github.com/obeattie/protoparts/internal/testproto"
"github.com/stretchr/testify/assert"
)
// FuzzSplit works off the basic premise that if a message is considered valid by the Protobuf library, then Split()
// should be able to handle it. If that's ever not the case, then the test fails.
func FuzzSplit(f *testing.F) {
msgs := []*dynamicpb.Message{
testMsg(f, nil, nil, nil, nil, nil, nil),
testMsg(f, p(""), nil, nil, nil, nil, nil),
testMsg(f, p("Oliver Beattie"), nil, nil, nil, nil, nil),
testMsg(f, p("Lindy Bishop"), nil, nil, nil, nil, nil),
testMsg(f,
p("Ryan Gosling"),
p("3532 Hayden Ave"),
p("Culver City"),
[]string{"The Driver", "Sebastian Wilder", ""},
[][]byte{[]byte("🕺"), []byte("🏍")},
map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
}),
testMsg(f,
p("Ryan Gosling"),
p("3532 Hayden Ave"),
p("Culver City"),
[]string{"The Driver", "Sebastian Wilder"},
[][]byte{[]byte("🕺"), []byte("🏍")},
map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
}),
}
for _, msg := range msgs {
f.Add(marshalProto(f, msg))
}
f.Fuzz(func(t *testing.T, b []byte) {
pb := &testproto.Person{}
if err := proto.Unmarshal(b, pb); err != nil {
t.Skip()
}
md := pb.ProtoReflect().Descriptor()
_, err := Split(b, md)
if !assert.NoError(t, err) {
t.Logf("base64 protobuf: %s", base64.StdEncoding.EncodeToString(b))
t.Logf("parsed protobuf: %s", pb.String())
protocCmd := exec.Command("protoc", "--decode_raw")
protocCmd.Stdin = bytes.NewReader(b)
protocOutput, err := protocCmd.Output()
if err == nil {
t.Logf("protoc --decode_raw: %s", string(protocOutput))
}
}
})
}