-
Notifications
You must be signed in to change notification settings - Fork 21
/
listener_fuzz_test.go
188 lines (151 loc) · 4.01 KB
/
listener_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package khatru
import (
"math/rand"
"testing"
"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/require"
)
func FuzzRandomListenerClientRemoving(f *testing.F) {
f.Add(uint(20), uint(20), uint(1))
f.Fuzz(func(t *testing.T, utw uint, ubs uint, ualf uint) {
totalWebsockets := int(utw)
baseSubs := int(ubs)
addListenerFreq := int(ualf) + 1
rl := NewRelay()
f := nostr.Filter{Kinds: []int{1}}
cancel := func(cause error) {}
websockets := make([]*WebSocket, 0, totalWebsockets*baseSubs)
l := 0
for i := 0; i < totalWebsockets; i++ {
ws := &WebSocket{}
websockets = append(websockets, ws)
rl.clients[ws] = nil
}
s := 0
for j := 0; j < baseSubs; j++ {
for i := 0; i < totalWebsockets; i++ {
ws := websockets[i]
w := idFromSeqUpper(i)
if s%addListenerFreq == 0 {
l++
rl.addListener(ws, w+":"+idFromSeqLower(j), rl, f, cancel)
}
s++
}
}
require.Len(t, rl.clients, totalWebsockets)
require.Len(t, rl.listeners, l)
for ws := range rl.clients {
rl.removeClientAndListeners(ws)
}
require.Len(t, rl.clients, 0)
require.Len(t, rl.listeners, 0)
})
}
func FuzzRandomListenerIdRemoving(f *testing.F) {
f.Add(uint(20), uint(20), uint(1), uint(4))
f.Fuzz(func(t *testing.T, utw uint, ubs uint, ualf uint, ualef uint) {
totalWebsockets := int(utw)
baseSubs := int(ubs)
addListenerFreq := int(ualf) + 1
addExtraListenerFreq := int(ualef) + 1
if totalWebsockets > 1024 || baseSubs > 1024 {
return
}
rl := NewRelay()
f := nostr.Filter{Kinds: []int{1}}
cancel := func(cause error) {}
websockets := make([]*WebSocket, 0, totalWebsockets)
type wsid struct {
ws *WebSocket
id string
}
subs := make([]wsid, 0, totalWebsockets*baseSubs)
extra := 0
for i := 0; i < totalWebsockets; i++ {
ws := &WebSocket{}
websockets = append(websockets, ws)
rl.clients[ws] = nil
}
s := 0
for j := 0; j < baseSubs; j++ {
for i := 0; i < totalWebsockets; i++ {
ws := websockets[i]
w := idFromSeqUpper(i)
if s%addListenerFreq == 0 {
id := w + ":" + idFromSeqLower(j)
rl.addListener(ws, id, rl, f, cancel)
subs = append(subs, wsid{ws, id})
if s%addExtraListenerFreq == 0 {
rl.addListener(ws, id, rl, f, cancel)
extra++
}
}
s++
}
}
require.Len(t, rl.clients, totalWebsockets)
require.Len(t, rl.listeners, len(subs)+extra)
rand.Shuffle(len(subs), func(i, j int) {
subs[i], subs[j] = subs[j], subs[i]
})
for _, wsidToRemove := range subs {
rl.removeListenerId(wsidToRemove.ws, wsidToRemove.id)
}
require.Len(t, rl.listeners, 0)
require.Len(t, rl.clients, totalWebsockets)
for _, specs := range rl.clients {
require.Len(t, specs, 0)
}
})
}
func FuzzRouterListenersPabloCrash(f *testing.F) {
f.Add(uint(3), uint(6), uint(2), uint(20))
f.Fuzz(func(t *testing.T, totalRelays uint, totalConns uint, subFreq uint, subIterations uint) {
totalRelays++
totalConns++
subFreq++
subIterations++
rl := NewRelay()
relays := make([]*Relay, int(totalRelays))
for i := 0; i < int(totalRelays); i++ {
relays[i] = NewRelay()
}
conns := make([]*WebSocket, int(totalConns))
for i := 0; i < int(totalConns); i++ {
ws := &WebSocket{}
conns[i] = ws
rl.clients[ws] = make([]listenerSpec, 0, subIterations)
}
f := nostr.Filter{Kinds: []int{1}}
cancel := func(cause error) {}
type wsid struct {
ws *WebSocket
id string
}
s := 0
subs := make([]wsid, 0, subIterations*totalConns*totalRelays)
for i, conn := range conns {
w := idFromSeqUpper(i)
for j := 0; j < int(subIterations); j++ {
id := w + ":" + idFromSeqLower(j)
for _, rlt := range relays {
if s%int(subFreq) == 0 {
rl.addListener(conn, id, rlt, f, cancel)
subs = append(subs, wsid{conn, id})
}
s++
}
}
}
for _, wsid := range subs {
rl.removeListenerId(wsid.ws, wsid.id)
}
for _, wsid := range subs {
require.Len(t, rl.clients[wsid.ws], 0)
}
for _, rlt := range relays {
require.Len(t, rlt.listeners, 0)
}
})
}