-
Notifications
You must be signed in to change notification settings - Fork 3
/
core-wire_test.go
244 lines (198 loc) · 5.35 KB
/
core-wire_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package jess
import (
"fmt"
"testing"
"time"
"github.com/safing/structures/container"
)
func TestWire(t *testing.T) {
t.Parallel()
wireReKeyAfterMsgs = 100
// current suites recommendation
testWireCorrespondence(t, getSuite(t, SuiteWire), testData1)
testWireCorrespondence(t, getSuite(t, SuiteWire), testData2)
// older suites
// testWireCorrespondence(t, getSuite(t, SuiteWireV1), testData1)
// testWireCorrespondence(t, getSuite(t, SuiteWireV1), testData2)
}
func testWireCorrespondence(t *testing.T, suite *Suite, testData string) {
t.Helper()
wtr := &wireTestRange{t: t}
wtr.init(suite, testData)
fmt.Printf("\n\nsimulating %v\n", suite.ID)
fmt.Println("two dots are one packet send+recv:")
fmt.Println("\nclient ->")
wtr.clientSend()
fmt.Println("\n-> server")
wtr.serverRecv()
wtr.clientSend()
wtr.serverRecv()
fmt.Println("\n<- server")
wtr.serverSend()
fmt.Println("\nclient <-")
wtr.clientRecv()
for i := 0; i < 100; i++ {
// up
wtr.clientSend()
wtr.serverRecv()
wtr.clientSend()
wtr.serverRecv()
// down
wtr.serverSend()
wtr.clientRecv()
wtr.serverSend()
wtr.clientRecv()
}
for i := 0; i < 100; i++ {
// up
wtr.clientSend()
wtr.serverRecv()
// down
wtr.serverSend()
wtr.clientRecv()
wtr.serverSend()
wtr.clientRecv()
wtr.serverSend()
wtr.clientRecv()
}
wtr.endTime = time.Now()
overhead := ((float64(wtr.bytesOnWire) / float64(wtr.bytesTransferred)) - 1) * 100
duration := wtr.endTime.Sub(wtr.startTime)
t.Logf(
"%v tested: msgsize=%d, rekey every %d msgs, %d msgs, %d bytes, +%f%% overhead, %s, %s per msg, %f Mbit/s",
wtr.suite.ID,
len(testData),
wireReKeyAfterMsgs,
wtr.msgsTransferred,
wtr.bytesTransferred,
overhead,
duration,
time.Duration(duration.Nanoseconds()/int64(wtr.msgsTransferred)),
float64(wtr.bytesTransferred*8/1000000)/duration.Seconds(),
)
}
// TODO
// func benchmarkWireCorrespondence(t *testing.T, toolIDs []string) {
// }
type wireTestRange struct {
t *testing.T
suite *Suite
testData string
client *Session
server *Session
clientToServer chan *container.Container
serverToClient chan *container.Container
msgsTransferred int
bytesTransferred int
bytesOnWire int
startTime time.Time
endTime time.Time
}
func (wtr *wireTestRange) init(suite *Suite, testData string) (detectedInvalid bool) {
wtr.suite = suite
e, err := setupEnvelopeAndTrustStore(wtr.t, wtr.suite)
if err != nil {
wtr.t.Fatalf("%s failed to setup envelope: %s", wtr.suite.ID, err)
return false
}
if e == nil {
return true
}
wtr.client, err = e.WireCorrespondence(testTrustStore)
if err != nil {
wtr.t.Fatalf("%s failed to init client session: %s", wtr.suite.ID, err)
}
// setup and reset
wtr.testData = testData
wtr.server = nil
wtr.clientToServer = make(chan *container.Container, 1000)
wtr.serverToClient = make(chan *container.Container, 1000)
wtr.msgsTransferred = 0
wtr.bytesTransferred = 0
wtr.bytesOnWire = 0
wtr.startTime = time.Now()
wtr.endTime = time.Time{}
return false
}
func (wtr *wireTestRange) clientSend() {
letter, err := wtr.client.Close([]byte(wtr.testData))
if err != nil {
wtr.t.Fatalf("%s failed to close: %s", wtr.suite.ID, err)
}
wireData, err := letter.ToWire()
if err != nil {
wtr.t.Fatalf("%s failed to serialize to wire: %s", wtr.suite.ID, err)
}
select {
case wtr.clientToServer <- wireData:
default:
wtr.t.Fatalf("%s could not send to server", wtr.suite.ID)
}
fmt.Print(".")
}
func (wtr *wireTestRange) serverRecv() {
select {
case wireData := <-wtr.clientToServer:
wtr.msgsTransferred++
wtr.bytesOnWire += wireData.Length()
letter, err := LetterFromWire(wireData)
if err != nil {
wtr.t.Fatalf("%s failed to parse initial wired letter: %s", wtr.suite.ID, err)
}
if wtr.server == nil {
wtr.server, err = letter.WireCorrespondence(testTrustStore)
if err != nil {
wtr.t.Fatalf("%s failed to init server session: %s", wtr.suite.ID, err)
}
}
origData, err := wtr.server.Open(letter)
if err != nil {
wtr.t.Fatalf("%s failed to open: %s", wtr.suite.ID, err)
}
wtr.bytesTransferred += len(origData)
if string(origData) != wtr.testData {
wtr.t.Fatalf("%s testdata mismatch", wtr.suite.ID)
}
default:
wtr.t.Fatalf("%s could not recv from client", wtr.suite.ID)
}
fmt.Print(".")
}
func (wtr *wireTestRange) serverSend() {
letter, err := wtr.server.Close([]byte(wtr.testData))
if err != nil {
wtr.t.Fatalf("%s failed to close: %s", wtr.suite.ID, err)
}
wireData, err := letter.ToWire()
if err != nil {
wtr.t.Fatalf("%s failed to serialize to wire: %s", wtr.suite.ID, err)
}
select {
case wtr.serverToClient <- wireData:
default:
wtr.t.Fatalf("%s could not send to client", wtr.suite.ID)
}
fmt.Print(".")
}
func (wtr *wireTestRange) clientRecv() {
select {
case wireData := <-wtr.serverToClient:
wtr.msgsTransferred++
wtr.bytesOnWire += wireData.Length()
letter, err := LetterFromWire(wireData)
if err != nil {
wtr.t.Fatalf("%s failed to parse initial wired letter: %s", wtr.suite.ID, err)
}
origData, err := wtr.client.Open(letter)
if err != nil {
wtr.t.Fatalf("%s failed to open: %s", wtr.suite.ID, err)
}
wtr.bytesTransferred += len(origData)
if string(origData) != wtr.testData {
wtr.t.Fatalf("%s testdata mismatch", wtr.suite.ID)
}
default:
wtr.t.Fatalf("%s could not recv from server", wtr.suite.ID)
}
fmt.Print(".")
}