-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetstack_test.go
363 lines (334 loc) · 9.37 KB
/
netstack_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package wg
import (
"context"
"errors"
"github.com/point-c/simplewg"
"github.com/point-c/wg/pkg/ipcheck"
"github.com/stretchr/testify/require"
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"math"
"net"
"sync"
"testing"
"time"
)
var (
testLocalIP = net.IPv4(192, 168, 0, 1)
testRemoteIP = net.IPv4(192, 168, 0, 1)
testLoopbackIP = net.IPv4(127, 0, 0, 1)
)
func TestStackOptions(t *testing.T) {
t.Run("error on sack enable", func(t *testing.T) {
err := SetStackOptions(stack.New(stack.Options{}), nil, nil)
require.ErrorContains(t, err, "unknown protocol")
})
t.Run("error on nic add", func(t *testing.T) {
s := stack.New(stack.Options{
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
})
id := tcpip.NICID(int32(1))
s.CreateNIC(id, channel.New(1, uint32(1500), ""))
err := SetStackOptions(s, channel.New(1, uint32(1500), ""), &id)
require.ErrorContains(t, err, "duplicate nic id")
})
}
func TestTCPIPError(t *testing.T) {
e := &tcpip.ErrAborted{}
err := TCPIPError{Err: e}
require.ErrorContains(t, &err, e.String())
}
func TestWriteNotify(t *testing.T) {
t.Run("nil packet", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
wn := (*writeNotify)(netstack)
wn.WriteNotify()
})
t.Run("read", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
closeOnce := sync.OnceFunc(func() { require.NoError(t, netstack.Close()) })
defer closeOnce()
var testPackets stack.PacketBufferList
testPackets.PushBack(stack.NewPacketBuffer(stack.PacketBufferOptions{Payload: buffer.MakeWithData([]byte{4 << 4})}))
errs := make(chan error)
go func() {
defer close(errs)
i, err := netstack.ep.WritePackets(testPackets)
if i != 1 {
errs <- errors.New("packet not written")
} else if err != nil {
errs <- &TCPIPError{Err: err}
}
}()
_, err = netstack.Read([][]byte{{}}, []int{0}, 0)
require.NoError(t, err)
require.NoError(t, <-errs)
})
t.Run("read closed", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
errs := make(chan error)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
go func(ctx context.Context) {
defer cancel()
defer close(errs)
var testPackets stack.PacketBufferList
testPackets.PushBack(stack.NewPacketBuffer(stack.PacketBufferOptions{Payload: buffer.MakeWithData([]byte{4 << 4})}))
i, e := netstack.ep.WritePackets(testPackets)
var err error
if i != 1 {
err = errors.New("packet not written")
} else if err != nil {
err = &TCPIPError{Err: e}
}
select {
case <-ctx.Done():
case errs <- err:
}
}(ctx)
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
defer cancel()
select {
case <-ctx.Done():
require.NoError(t, netstack.Close())
require.NoError(t, <-errs)
}
})
}
func TestNewDefaultNetstack(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
require.NotNil(t, netstack)
require.Equal(t, DefaultMTU, netstack.mtu)
require.Equal(t, DefaultBatchSize, netstack.batchSize)
}
func TestNewNetstack(t *testing.T) {
mtu := 1400
batchSize := 10
channelSize := 50
netstack, err := NewNetstack(mtu, batchSize, channelSize)
require.NoError(t, err)
defer netstack.Close()
require.NotNil(t, netstack)
require.Equal(t, mtu, netstack.mtu)
require.Equal(t, batchSize, netstack.batchSize)
}
func TestNetstackClose(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
require.NoError(t, netstack.Close())
}
func TestNetstackFile(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
require.Nil(t, netstack.File())
}
func TestNetstackName(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
name, err := netstack.Name()
require.NoError(t, err)
require.Equal(t, "point-c", name)
}
func TestNetstackReadAndWrite(t *testing.T) {
t.Run("write ipv4", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
_, err = netstack.Write([][]byte{{4 << 4}}, 0)
require.NoError(t, err)
})
t.Run("write empty bufs", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
_, err = netstack.Write([][]byte{nil, nil}, 0)
require.NoError(t, err)
})
t.Run("write ipv6", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
_, err = netstack.Write([][]byte{{6 << 4}}, 0)
require.NoError(t, err)
})
t.Run("read packet", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
go func() { netstack.read <- []byte{} }()
_, err = netstack.Read([][]byte{{}}, []int{0}, 0)
require.NoError(t, err)
})
t.Run("read closed", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
require.NoError(t, netstack.Close())
_, err = netstack.Read([][]byte{{}}, []int{0}, 0)
require.Error(t, err)
})
}
func TestNetstackListen(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
n := netstack.Net()
listener, err := n.Listen(&net.TCPAddr{IP: testLocalIP, Port: 8080})
require.NoError(t, err)
require.NotNil(t, listener)
_, err = n.Listen(&net.TCPAddr{IP: testLoopbackIP})
require.ErrorIs(t, err, ipcheck.ErrInvalidLocalIP)
}
func TestNetstackListenPacket(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
n := netstack.Net()
packetConn, err := n.ListenPacket(&net.UDPAddr{IP: testLocalIP, Port: 8080})
require.NoError(t, err)
require.NotNil(t, packetConn)
_, err = n.ListenPacket(&net.UDPAddr{IP: testLoopbackIP})
require.ErrorIs(t, err, ipcheck.ErrInvalidLocalIP)
}
func TestDialerDialTCP(t *testing.T) {
t.Run("context cancelled", func(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
n := netstack.Net()
dialer := n.Dialer(testLocalIP, 8081)
ctx, cancel := context.WithCancel(context.TODO())
cancel()
conn, err := dialer.DialTCP(ctx, &net.TCPAddr{IP: testLocalIP, Port: 8080})
require.Error(t, err)
require.Nil(t, conn)
})
tests := []struct {
name string
listen net.IP
local net.IP
remote net.IP
err error
}{
{
name: "basic",
listen: testRemoteIP,
remote: testRemoteIP,
local: testLocalIP,
},
{
name: "listen 0.0.0.0",
listen: net.IPv4zero,
remote: testRemoteIP,
local: testLocalIP,
},
{
name: "invalid local",
listen: testRemoteIP,
remote: testRemoteIP,
local: testLoopbackIP,
err: ipcheck.ErrInvalidLocalIP,
},
{
name: "invalid remote",
listen: testRemoteIP,
remote: testLoopbackIP,
local: testLocalIP,
err: ipcheck.ErrInvalidRemoteIP,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var w simplewg.Wg
defer w.Wait()
netstack1, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack1.Close()
n1 := netstack1.Net()
netstack2, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack2.Close()
n2 := netstack2.Net()
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
if dl, ok := t.Deadline(); ok {
ctx, cancel = context.WithDeadline(ctx, dl)
} else {
if err != nil {
ctx, cancel = context.WithTimeout(ctx, time.Second*2)
} else {
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
}
}
defer cancel()
done := make(chan struct{})
if tt.err != nil {
close(done)
} else {
copyNetstack := func(net1, net2 *Netstack) {
for {
b := [][]byte{make([]byte, math.MaxUint16)}
s := []int{0}
n, err := net1.Read(b, s, 0)
if err != nil {
return
} else if n == 0 {
continue
}
_, err = net2.Write(b, 0)
if err != nil {
t.Error(err.Error())
}
}
}
w.Go(func() { copyNetstack(netstack1, netstack2) })
w.Go(func() { copyNetstack(netstack2, netstack1) })
go func() {
defer close(done)
defer cancel()
ln, err := n1.Listen(&net.TCPAddr{IP: tt.listen, Port: 123})
require.NoError(t, err)
cn, err := ln.Accept()
require.NoError(t, err)
require.NotNil(t, cn)
}()
}
cn, err := n2.Dialer(tt.local, 0).DialTCP(ctx, &net.TCPAddr{IP: tt.remote, Port: 123})
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
require.Nil(t, cn)
} else {
require.NoError(t, err)
require.NotNil(t, cn)
require.NoError(t, cn.Close())
}
<-done
})
}
}
func TestDialerDialUDP(t *testing.T) {
netstack, err := NewDefaultNetstack()
require.NoError(t, err)
defer netstack.Close()
n := netstack.Net()
dialer := n.Dialer(testLocalIP, 8080)
packetConn, err := dialer.DialUDP(&net.UDPAddr{IP: testLocalIP, Port: 8081})
require.NoError(t, err)
require.NotNil(t, packetConn)
_, err = n.Dialer(testLocalIP, 0).DialUDP(&net.UDPAddr{IP: testLoopbackIP})
require.ErrorIs(t, err, ipcheck.ErrInvalidRemoteIP)
_, err = n.Dialer(testLoopbackIP, 0).DialUDP(&net.UDPAddr{IP: testLocalIP})
require.ErrorIs(t, err, ipcheck.ErrInvalidLocalIP)
}