]> Sergey Matveev's repositories - btrtrc.git/blob - network_test.go
Use Option with request piece states cache
[btrtrc.git] / network_test.go
1 package torrent
2
3 import (
4         "net"
5         "testing"
6
7         "github.com/anacrolix/log"
8         "github.com/anacrolix/missinggo/v2"
9         "github.com/stretchr/testify/assert"
10         "github.com/stretchr/testify/require"
11 )
12
13 func testListenerNetwork(
14         t *testing.T,
15         listenFunc func(net, addr string) (net.Listener, error),
16         expectedNet, givenNet, addr string, validIp4 bool,
17 ) {
18         l, err := listenFunc(givenNet, addr)
19         if isUnsupportedNetworkError(err) {
20                 return
21         }
22         require.NoError(t, err)
23         defer l.Close()
24         assert.EqualValues(t, expectedNet, l.Addr().Network())
25         ip := missinggo.AddrIP(l.Addr())
26         assert.Equal(t, validIp4, ip.To4() != nil, ip)
27 }
28
29 func listenUtpListener(net, addr string) (l net.Listener, err error) {
30         l, err = NewUtpSocket(net, addr, nil, log.Default)
31         return
32 }
33
34 func testAcceptedConnAddr(
35         t *testing.T,
36         network string, valid4 bool,
37         dial func(addr string) (net.Conn, error),
38         listen func() (net.Listener, error),
39 ) {
40         l, err := listen()
41         require.NoError(t, err)
42         defer l.Close()
43         done := make(chan struct{})
44         defer close(done)
45         go func() {
46                 c, err := dial(l.Addr().String())
47                 require.NoError(t, err)
48                 <-done
49                 c.Close()
50         }()
51         c, err := l.Accept()
52         require.NoError(t, err)
53         defer c.Close()
54         assert.EqualValues(t, network, c.RemoteAddr().Network())
55         assert.Equal(t, valid4, missinggo.AddrIP(c.RemoteAddr()).To4() == nil)
56 }
57
58 func listenClosure(
59         rawListenFunc func(string, string) (net.Listener, error),
60         network, addr string,
61 ) func() (net.Listener, error) {
62         return func() (net.Listener, error) {
63                 return rawListenFunc(network, addr)
64         }
65 }
66
67 func dialClosure(f func(net, addr string) (net.Conn, error), network string) func(addr string) (net.Conn, error) {
68         return func(addr string) (net.Conn, error) {
69                 return f(network, addr)
70         }
71 }
72
73 func TestListenLocalhostNetwork(t *testing.T) {
74         testListenerNetwork(t, net.Listen, "tcp", "tcp", "0.0.0.0:0", false)
75         testListenerNetwork(t, net.Listen, "tcp", "tcp", "[::1]:0", false)
76         testListenerNetwork(t, listenUtpListener, "udp", "udp6", "[::1]:0", false)
77         testListenerNetwork(t, listenUtpListener, "udp", "udp6", "[::]:0", false)
78         testListenerNetwork(t, listenUtpListener, "udp", "udp4", "localhost:0", true)
79
80         testAcceptedConnAddr(
81                 t,
82                 "tcp",
83                 false,
84                 dialClosure(net.Dial, "tcp"),
85                 listenClosure(net.Listen, "tcp4", "localhost:0"),
86         )
87 }