-
Notifications
You must be signed in to change notification settings - Fork 186
/
fuzz_helpers_test.go
149 lines (121 loc) · 3.72 KB
/
fuzz_helpers_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
package pubsub
import (
"encoding/binary"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
)
func generateU16(data *[]byte) uint16 {
if len(*data) < 2 {
return 0
}
out := binary.LittleEndian.Uint16((*data)[:2])
*data = (*data)[2:]
return out
}
func generateBool(data *[]byte) bool {
if len(*data) < 1 {
return false
}
out := (*data)[0]&1 == 1
*data = (*data)[1:]
return out
}
func generateMessage(data []byte, limit int) *pb.Message {
msgSize := int(generateU16(&data)) % limit
return &pb.Message{Data: make([]byte, msgSize)}
}
func generateSub(data []byte, limit int) *pb.RPC_SubOpts {
topicIDSize := int(generateU16(&data)) % limit
subscribe := generateBool(&data)
str := string(make([]byte, topicIDSize))
return &pb.RPC_SubOpts{Subscribe: &subscribe, Topicid: &str}
}
func generateControl(data []byte, limit int) *pb.ControlMessage {
numIWANTMsgs := int(generateU16(&data)) % (limit / 2)
numIHAVEMsgs := int(generateU16(&data)) % (limit / 2)
ctl := &pb.ControlMessage{}
ctl.Iwant = make([]*pb.ControlIWant, 0, numIWANTMsgs)
for i := 0; i < numIWANTMsgs; i++ {
msgSize := int(generateU16(&data)) % limit
msgCount := int(generateU16(&data)) % limit
ctl.Iwant = append(ctl.Iwant, &pb.ControlIWant{})
ctl.Iwant[i].MessageIDs = make([]string, 0, msgCount)
for j := 0; j < msgCount; j++ {
ctl.Iwant[i].MessageIDs = append(ctl.Iwant[i].MessageIDs, string(make([]byte, msgSize)))
}
}
if ctl.Size() > limit {
return &pb.ControlMessage{}
}
ctl.Ihave = make([]*pb.ControlIHave, 0, numIHAVEMsgs)
for i := 0; i < numIHAVEMsgs; i++ {
msgSize := int(generateU16(&data)) % limit
msgCount := int(generateU16(&data)) % limit
topicSize := int(generateU16(&data)) % limit
topic := string(make([]byte, topicSize))
ctl.Ihave = append(ctl.Ihave, &pb.ControlIHave{TopicID: &topic})
ctl.Ihave[i].MessageIDs = make([]string, 0, msgCount)
for j := 0; j < msgCount; j++ {
ctl.Ihave[i].MessageIDs = append(ctl.Ihave[i].MessageIDs, string(make([]byte, msgSize)))
}
}
if ctl.Size() > limit {
return &pb.ControlMessage{}
}
numGraft := int(generateU16(&data)) % limit
ctl.Graft = make([]*pb.ControlGraft, 0, numGraft)
for i := 0; i < numGraft; i++ {
topicSize := int(generateU16(&data)) % limit
topic := string(make([]byte, topicSize))
ctl.Graft = append(ctl.Graft, &pb.ControlGraft{TopicID: &topic})
}
if ctl.Size() > limit {
return &pb.ControlMessage{}
}
numPrune := int(generateU16(&data)) % limit
ctl.Prune = make([]*pb.ControlPrune, 0, numPrune)
for i := 0; i < numPrune; i++ {
topicSize := int(generateU16(&data)) % limit
topic := string(make([]byte, topicSize))
ctl.Prune = append(ctl.Prune, &pb.ControlPrune{TopicID: &topic})
}
if ctl.Size() > limit {
return &pb.ControlMessage{}
}
return ctl
}
func generateRPC(data []byte, limit int) *RPC {
rpc := &RPC{RPC: pb.RPC{}}
sizeTester := RPC{RPC: pb.RPC{}}
msgCount := int(generateU16(&data)) % (limit / 2)
rpc.Publish = make([]*pb.Message, 0, msgCount)
for i := 0; i < msgCount; i++ {
msg := generateMessage(data, limit)
sizeTester.Publish = []*pb.Message{msg}
size := sizeTester.Size()
sizeTester.Publish = nil
if size > limit {
continue
}
rpc.Publish = append(rpc.Publish, msg)
}
subCount := int(generateU16(&data)) % (limit / 2)
rpc.Subscriptions = make([]*pb.RPC_SubOpts, 0, subCount)
for i := 0; i < subCount; i++ {
sub := generateSub(data, limit)
sizeTester.Subscriptions = []*pb.RPC_SubOpts{sub}
size := sizeTester.Size()
sizeTester.Subscriptions = nil
if size > limit {
continue
}
rpc.Subscriptions = append(rpc.Subscriptions, sub)
}
ctl := generateControl(data, limit)
sizeTester.Control = ctl
size := sizeTester.Size()
sizeTester.Control = nil
if size <= limit {
rpc.Control = ctl
}
return rpc
}