]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/http/http_test.go
gorond test files
[btrtrc.git] / tracker / http / http_test.go
1 package httpTracker
2
3 import (
4         "net/url"
5         "testing"
6
7         qt "github.com/frankban/quicktest"
8         "github.com/stretchr/testify/assert"
9         "github.com/stretchr/testify/require"
10
11         "github.com/anacrolix/torrent/bencode"
12         "github.com/anacrolix/torrent/tracker/udp"
13 )
14
15 func TestUnmarshalHTTPResponsePeerDicts(t *testing.T) {
16         var hr HttpResponse
17         require.NoError(t, bencode.Unmarshal(
18                 []byte("d5:peersl"+
19                         "d2:ip7:1.2.3.47:peer id20:thisisthe20bytepeeri4:porti9999ee"+
20                         "d2:ip39:2001:0db8:85a3:0000:0000:8a2e:0370:73347:peer id20:thisisthe20bytepeeri4:porti9998ee"+
21                         "e"+
22                         "6:peers618:123412341234123456"+
23                         "e"),
24                 &hr))
25
26         require.Len(t, hr.Peers.List, 2)
27         assert.Equal(t, []byte("thisisthe20bytepeeri"), hr.Peers.List[0].ID)
28         assert.EqualValues(t, 9999, hr.Peers.List[0].Port)
29         assert.EqualValues(t, 9998, hr.Peers.List[1].Port)
30         assert.NotNil(t, hr.Peers.List[0].IP)
31         assert.NotNil(t, hr.Peers.List[1].IP)
32
33         assert.Len(t, hr.Peers6, 1)
34         assert.EqualValues(t, "1234123412341234", hr.Peers6[0].IP)
35         assert.EqualValues(t, 0x3536, hr.Peers6[0].Port)
36 }
37
38 func TestUnmarshalHttpResponseNoPeers(t *testing.T) {
39         var hr HttpResponse
40         require.NoError(t, bencode.Unmarshal(
41                 []byte("d6:peers618:123412341234123456e"),
42                 &hr,
43         ))
44         require.Len(t, hr.Peers.List, 0)
45         assert.Len(t, hr.Peers6, 1)
46 }
47
48 func TestUnmarshalHttpResponsePeers6NotCompact(t *testing.T) {
49         var hr HttpResponse
50         require.Error(t, bencode.Unmarshal(
51                 []byte("d6:peers6lee"),
52                 &hr,
53         ))
54 }
55
56 // Checks that infohash bytes that correspond to spaces are escaped with %20 instead of +. See
57 // https://github.com/anacrolix/torrent/issues/534
58 func TestSetAnnounceInfohashParamWithSpaces(t *testing.T) {
59         someUrl := &url.URL{}
60         ihBytes := [20]uint8{
61                 0x2b, 0x76, 0xa, 0xa1, 0x78, 0x93, 0x20, 0x30, 0xc8, 0x47,
62                 0xdc, 0xdf, 0x8e, 0xae, 0xbf, 0x56, 0xa, 0x1b, 0xd1, 0x6c,
63         }
64         setAnnounceParams(
65                 someUrl,
66                 &udp.AnnounceRequest{
67                         InfoHash: ihBytes,
68                 },
69                 AnnounceOpt{})
70         t.Logf("%q", someUrl)
71         qt.Assert(t, someUrl.Query().Get("info_hash"), qt.Equals, string(ihBytes[:]))
72         qt.Check(t,
73                 someUrl.String(),
74                 qt.Contains,
75                 "info_hash=%2Bv%0A%A1x%93%200%C8G%DC%DF%8E%AE%BFV%0A%1B%D1l")
76 }