-
Notifications
You must be signed in to change notification settings - Fork 9
/
net_test.go
326 lines (257 loc) · 6.16 KB
/
net_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-FileCopyrightText: 2021 The Secretstream Authors
//
// SPDX-License-Identifier: MIT
package secretstream
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net"
"strings"
"sync"
"testing"
"github.com/ssbc/go-netwrap"
"github.com/ssbc/go-secretstream/secrethandshake"
"github.com/stretchr/testify/require"
)
var (
clientKeys, serverKeys *secrethandshake.EdKeyPair
appKey []byte
)
func init() {
var err error
clientKeys, err = secrethandshake.GenEdKeyPair(nil)
check(err)
serverKeys, err = secrethandshake.GenEdKeyPair(nil)
check(err)
appKey, err = base64.StdEncoding.DecodeString("UjFLJ+aDSwKlaxxLBA3aWfL0pJDbrERwF1MWzQbeD0A=")
check(err)
}
func check(err error) {
if err != nil {
panic(err)
}
}
func mkCheck(errc chan<- error) func(err error) {
return func(err error) {
if err != nil {
errc <- err
}
}
}
func mergedErrors(cs ...<-chan error) <-chan error {
var wg sync.WaitGroup
out := make(chan error, 1)
output := func(c <-chan error) {
for a := range c {
out <- a
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func TestNet(t *testing.T) {
r := require.New(t)
s, err := NewServer(*serverKeys, appKey)
r.NoError(err)
l, err := netwrap.Listen(&net.TCPAddr{IP: net.IP{127, 0, 0, 1}}, s.ListenerWrapper())
r.NoError(err)
testData := strings.Repeat("Hello, World!", 50)
srvErrc := make(chan error)
check := mkCheck(srvErrc)
go func() {
var (
conn net.Conn
err error
)
conn, err = l.Accept()
check(err)
_, err = conn.Write(appKey)
check(err)
buf := make([]byte, len(testData))
_, err = io.ReadFull(conn, buf)
check(err)
if string(buf) != testData {
fmt.Errorf("server read wrong bytes: %x", buf)
return
}
check(conn.Close())
check(l.Close())
close(srvErrc)
}()
c, err := NewClient(*clientKeys, appKey)
r.NoError(err)
tcpAddr := netwrap.GetAddr(l.Addr(), "tcp")
connWrap := c.ConnWrapper(serverKeys.Public)
conn, err := netwrap.Dial(tcpAddr, connWrap)
r.NoError(err)
buf := make([]byte, len(appKey))
_, err = io.ReadFull(conn, buf)
r.NoError(err)
if !bytes.Equal(buf, appKey) {
t.Fatalf("client read wrong bytes - expected %q, got %q", appKey, buf)
}
_, err = conn.Write([]byte(testData))
r.NoError(err)
r.NoError(conn.Close(), "failed to close conn")
i := 0
for e := range srvErrc {
r.NoError(e, "err %d from chan", i)
i++
}
}
func TestNetClose(t *testing.T) {
r := require.New(t)
s, err := NewServer(*serverKeys, appKey)
r.NoError(err)
l, err := netwrap.Listen(&net.TCPAddr{IP: net.IP{127, 0, 0, 1}}, s.ListenerWrapper())
r.NoError(err)
// 1 MiB
testData := make([]byte, 1024*1024)
for i, _ := range testData {
testData[i] = byte(rand.Int() % 255)
}
srvErrc := make(chan error)
check := mkCheck(srvErrc)
go func() {
var (
c net.Conn
err error
)
c, err = l.Accept()
check(err)
_, err = c.Write(testData)
check(err)
// Immediately close conn after Write()
check(c.Close())
check(l.Close())
close(srvErrc)
}()
c, err := NewClient(*clientKeys, appKey)
r.NoError(err)
client, err := netwrap.Dial(netwrap.GetAddr(l.Addr(), "tcp"), c.ConnWrapper(serverKeys.Public))
r.NoError(err)
recData := make([]byte, 1024*1024)
_, err = io.ReadFull(client, recData)
r.NoError(err)
r.Equal(recData, testData, "client read wrong bytes")
r.NoError(client.Close(), "failed to close client")
i := 0
for e := range srvErrc {
r.NoError(e, "err %d from chan", i)
i++
}
}
// a concurrent write might produce a race on the nonce
func TestRaceClose(t *testing.T) {
r := require.New(t)
s, err := NewServer(*serverKeys, appKey)
r.NoError(err)
l, err := netwrap.Listen(&net.TCPAddr{IP: net.IP{127, 0, 0, 1}}, s.ListenerWrapper())
r.NoError(err)
// 1 MiB
testData := make([]byte, 1024*1024)
for i, _ := range testData {
testData[i] = byte(rand.Int() % 255)
}
srvErrc := make(chan error, 1)
srving := make(chan net.Conn)
go func() {
check := mkCheck(srvErrc)
c, err := l.Accept()
check(err)
// check(l.Close()) // one connection is enough
srving <- c // give the conn to a _connection pool_
// write one byte at a time, just to make this case more likely
for b := bytes.NewBuffer(testData); b.Len() > 0; {
_, err = c.Write(b.Next(1))
if oerr, ok := err.(*net.OpError); ok {
if oerr.Err.Error() == "use of closed network connection" {
close(srvErrc)
return
}
}
check(err)
}
check(c.Close())
close(srvErrc)
}()
// another part of the stack might decide to close it as it's being used
closeErrc := make(chan error, 1)
go func() {
check := mkCheck(closeErrc)
c := <-srving
check(c.Close())
close(closeErrc)
}()
c, err := NewClient(*clientKeys, appKey)
r.NoError(err)
client, err := netwrap.Dial(netwrap.GetAddr(l.Addr(), "tcp"), c.ConnWrapper(serverKeys.Public))
r.NoError(err)
recData := make([]byte, 1024*1024)
_, err = io.ReadFull(client, recData)
r.Error(err)
err = client.Close()
if err != nil {
t.Error("failed to close client", err)
}
i := 0
for e := range mergedErrors(srvErrc, closeErrc) {
r.NoError(e, "err %d from chan", i)
i++
}
}
func TestNetCloseEarly(t *testing.T) {
r := require.New(t)
s, err := NewServer(*serverKeys, appKey)
r.NoError(err)
l, err := netwrap.Listen(&net.TCPAddr{IP: net.IP{127, 0, 0, 1}}, s.ListenerWrapper())
r.NoError(err)
// 1 MiB
testData := make([]byte, 1024*1024)
for i, _ := range testData {
testData[i] = byte(rand.Int() % 255)
}
srvErrc := make(chan error)
check := mkCheck(srvErrc)
go func() {
var (
c net.Conn
err error
)
c, err = l.Accept()
check(err)
check(l.Close()) // one connection is enough
// short write
_, err = c.Write(testData[:10])
check(err)
check(c.Close())
close(srvErrc)
}()
c, err := NewClient(*clientKeys, appKey)
r.NoError(err)
client, err := netwrap.Dial(netwrap.GetAddr(l.Addr(), "tcp"), c.ConnWrapper(serverKeys.Public))
r.NoError(err)
recData := make([]byte, 1024*1024)
_, err = io.ReadFull(client, recData)
r.Error(err)
err = client.Close()
if err != nil {
t.Error("failed to close client", err)
}
i := 0
for e := range srvErrc {
r.NoError(e, "err %d from chan", i)
i++
}
}