]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/udp_test.go
tracker: Fix network error handling in unit test
[btrtrc.git] / tracker / udp_test.go
1 package tracker
2
3 import (
4         "bytes"
5         "context"
6         "crypto/rand"
7         "encoding/binary"
8         "fmt"
9         "io"
10         "io/ioutil"
11         "net"
12         "net/url"
13         "sync"
14         "testing"
15
16         "github.com/anacrolix/dht/krpc"
17         _ "github.com/anacrolix/envpprof"
18         "github.com/pkg/errors"
19         "github.com/stretchr/testify/assert"
20         "github.com/stretchr/testify/require"
21 )
22
23 var trackers = []string{
24         "udp://tracker.coppersurfer.tk:6969",
25         "udp://tracker.leechers-paradise.org:6969",
26 }
27
28 // Ensure net.IPs are stored big-endian, to match the way they're read from
29 // the wire.
30 func TestNetIPv4Bytes(t *testing.T) {
31         ip := net.IP([]byte{127, 0, 0, 1})
32         if ip.String() != "127.0.0.1" {
33                 t.FailNow()
34         }
35         if string(ip) != "\x7f\x00\x00\x01" {
36                 t.Fatal([]byte(ip))
37         }
38 }
39
40 func TestMarshalAnnounceResponse(t *testing.T) {
41         peers := krpc.CompactIPv4NodeAddrs{
42                 {[]byte{127, 0, 0, 1}, 2},
43                 {[]byte{255, 0, 0, 3}, 4},
44         }
45         b, err := peers.MarshalBinary()
46         require.NoError(t, err)
47         require.EqualValues(t,
48                 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
49                 b)
50         require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
51 }
52
53 // Failure to write an entire packet to UDP is expected to given an error.
54 func TestLongWriteUDP(t *testing.T) {
55         t.Parallel()
56         l, err := net.ListenUDP("udp4", nil)
57         require.NoError(t, err)
58         defer l.Close()
59         c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
60         if err != nil {
61                 t.Fatal(err)
62         }
63         defer c.Close()
64         for msgLen := 1; ; msgLen *= 2 {
65                 n, err := c.Write(make([]byte, msgLen))
66                 if err != nil {
67                         require.Contains(t, err.Error(), "message too long")
68                         return
69                 }
70                 if n < msgLen {
71                         t.FailNow()
72                 }
73         }
74 }
75
76 func TestShortBinaryRead(t *testing.T) {
77         var data ResponseHeader
78         err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
79         if err != io.ErrUnexpectedEOF {
80                 t.FailNow()
81         }
82 }
83
84 func TestConvertInt16ToInt(t *testing.T) {
85         i := 50000
86         if int(uint16(int16(i))) != 50000 {
87                 t.FailNow()
88         }
89 }
90
91 func TestAnnounceLocalhost(t *testing.T) {
92         t.Parallel()
93         srv := server{
94                 t: map[[20]byte]torrent{
95                         {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
96                                 Seeders:  1,
97                                 Leechers: 2,
98                                 Peers: krpc.CompactIPv4NodeAddrs{
99                                         {[]byte{1, 2, 3, 4}, 5},
100                                         {[]byte{6, 7, 8, 9}, 10},
101                                 },
102                         },
103                 },
104         }
105         var err error
106         srv.pc, err = net.ListenPacket("udp", ":0")
107         require.NoError(t, err)
108         defer srv.pc.Close()
109         go func() {
110                 require.NoError(t, srv.serveOne())
111         }()
112         req := AnnounceRequest{
113                 NumWant: -1,
114                 Event:   Started,
115         }
116         rand.Read(req.PeerId[:])
117         copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
118         go func() {
119                 require.NoError(t, srv.serveOne())
120         }()
121         ar, err := Announce{
122                 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
123                 Request:    req,
124         }.Do()
125         require.NoError(t, err)
126         assert.EqualValues(t, 1, ar.Seeders)
127         assert.EqualValues(t, 2, len(ar.Peers))
128 }
129
130 func TestUDPTracker(t *testing.T) {
131         t.Parallel()
132         if testing.Short() {
133                 t.SkipNow()
134         }
135         req := AnnounceRequest{
136                 NumWant: -1,
137         }
138         rand.Read(req.PeerId[:])
139         copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
140         ar, err := Announce{
141                 TrackerUrl: trackers[0],
142                 Request:    req,
143         }.Do()
144         // Skip any net errors as we don't control the server.
145         if _, ok := errors.Cause(err).(net.Error); ok {
146                 t.Skip(err)
147         }
148         require.NoError(t, err)
149         t.Log(ar)
150 }
151
152 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
153         t.Parallel()
154         if testing.Short() {
155                 // This test involves contacting third party servers that may have
156                 // unpredictable results.
157                 t.SkipNow()
158         }
159         req := AnnounceRequest{
160                 Event: Stopped,
161         }
162         rand.Read(req.PeerId[:])
163         rand.Read(req.InfoHash[:])
164         wg := sync.WaitGroup{}
165         ctx, cancel := context.WithCancel(context.Background())
166         for _, url := range trackers {
167                 wg.Add(1)
168                 go func(url string) {
169                         defer wg.Done()
170                         resp, err := Announce{
171                                 TrackerUrl: url,
172                                 Request:    req,
173                                 Context:    ctx,
174                         }.Do()
175                         if err != nil {
176                                 t.Logf("error announcing to %s: %s", url, err)
177                                 return
178                         }
179                         if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
180                                 // The info hash we generated was random in 2^160 space. If we
181                                 // get a hit, something is weird.
182                                 t.Fatal(resp)
183                         }
184                         t.Logf("announced to %s", url)
185                         cancel()
186                 }(url)
187         }
188         wg.Wait()
189 }
190
191 // Check that URLPath option is done correctly.
192 func TestURLPathOption(t *testing.T) {
193         conn, err := net.ListenUDP("udp", nil)
194         if err != nil {
195                 panic(err)
196         }
197         defer conn.Close()
198         go func() {
199                 _, err := Announce{
200                         TrackerUrl: (&url.URL{
201                                 Scheme: "udp",
202                                 Host:   conn.LocalAddr().String(),
203                                 Path:   "/announce",
204                         }).String(),
205                 }.Do()
206                 if err != nil {
207                         defer conn.Close()
208                 }
209                 require.NoError(t, err)
210         }()
211         var b [512]byte
212         _, addr, _ := conn.ReadFrom(b[:])
213         r := bytes.NewReader(b[:])
214         var h RequestHeader
215         read(r, &h)
216         w := &bytes.Buffer{}
217         write(w, ResponseHeader{
218                 TransactionId: h.TransactionId,
219         })
220         write(w, ConnectionResponse{42})
221         conn.WriteTo(w.Bytes(), addr)
222         n, _, _ := conn.ReadFrom(b[:])
223         r = bytes.NewReader(b[:n])
224         read(r, &h)
225         read(r, &AnnounceRequest{})
226         all, _ := ioutil.ReadAll(r)
227         if string(all) != "\x02\x09/announce" {
228                 t.FailNow()
229         }
230         w = &bytes.Buffer{}
231         write(w, ResponseHeader{
232                 TransactionId: h.TransactionId,
233         })
234         write(w, AnnounceResponseHeader{})
235         conn.WriteTo(w.Bytes(), addr)
236 }