-
Notifications
You must be signed in to change notification settings - Fork 6
/
testutils_linux_test.go
48 lines (40 loc) · 1.19 KB
/
testutils_linux_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
// +build linux
package secureio_test
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func testUDPPair(t *testing.T) (*net.UDPConn, *net.UDPConn) {
testUDPPairStart:
localhost := net.ParseIP("127.120.97.105") // just a free address, I hope
conn0, err := net.DialUDP(`udp`, &net.UDPAddr{IP: localhost}, &net.UDPAddr{IP: localhost})
assert.NoError(t, err)
conn1, err := net.DialUDP(`udp`,
&net.UDPAddr{IP: localhost, Port: 0},
&net.UDPAddr{IP: localhost, Port: conn0.LocalAddr().(*net.UDPAddr).Port},
)
assert.NoError(t, err)
err = conn0.Close()
assert.NoError(t, err)
conn0, err = net.DialUDP(`udp`,
&net.UDPAddr{IP: localhost, Port: conn1.RemoteAddr().(*net.UDPAddr).Port},
&net.UDPAddr{IP: localhost, Port: conn1.LocalAddr().(*net.UDPAddr).Port},
)
if err != nil {
goto testUDPPairStart // somebody already took the address
}
if t != nil {
assert.NoError(t, err)
writeBuf := []byte("abc123")
_, err = conn1.Write(writeBuf)
assert.NoError(t, err)
_, err = conn0.Write(writeBuf)
assert.NoError(t, err)
readBuf := make([]byte, 65536)
n, err := conn0.Read(readBuf)
assert.NoError(t, err)
assert.Equal(t, writeBuf, readBuf[:n])
}
return conn0, conn1
}