15 _ "github.com/anacrolix/envpprof"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
19 "github.com/anacrolix/torrent/util"
22 // Ensure net.IPs are stored big-endian, to match the way they're read from
24 func TestNetIPv4Bytes(t *testing.T) {
25 ip := net.IP([]byte{127, 0, 0, 1})
26 if ip.String() != "127.0.0.1" {
29 if string(ip) != "\x7f\x00\x00\x01" {
34 func TestMarshalAnnounceResponse(t *testing.T) {
35 peers := util.CompactIPv4Peers{
36 {[]byte{127, 0, 0, 1}, 2},
37 {[]byte{255, 0, 0, 3}, 4},
39 b, err := peers.MarshalBinary()
40 require.NoError(t, err)
41 require.EqualValues(t,
42 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
44 require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
47 // Failure to write an entire packet to UDP is expected to given an error.
48 func TestLongWriteUDP(t *testing.T) {
50 l, err := net.ListenUDP("udp", nil)
55 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
60 for msgLen := 1; ; msgLen *= 2 {
61 n, err := c.Write(make([]byte, msgLen))
63 require.Contains(t, err.Error(), "message too long")
72 func TestShortBinaryRead(t *testing.T) {
73 var data ResponseHeader
74 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
75 if err != io.ErrUnexpectedEOF {
80 func TestConvertInt16ToInt(t *testing.T) {
82 if int(uint16(int16(i))) != 50000 {
87 func TestAnnounceLocalhost(t *testing.T) {
90 t: map[[20]byte]torrent{
91 [20]byte{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
94 Peers: []util.CompactPeer{
95 {[]byte{1, 2, 3, 4}, 5},
96 {[]byte{6, 7, 8, 9}, 10},
102 srv.pc, err = net.ListenPacket("udp", ":0")
103 require.NoError(t, err)
106 require.NoError(t, srv.serveOne())
108 req := AnnounceRequest{
112 rand.Read(req.PeerId[:])
113 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
115 require.NoError(t, srv.serveOne())
117 ar, err := Announce(fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()), &req)
118 require.NoError(t, err)
119 assert.EqualValues(t, 1, ar.Seeders)
120 assert.EqualValues(t, 2, len(ar.Peers))
123 func TestUDPTracker(t *testing.T) {
128 req := AnnounceRequest{
131 rand.Read(req.PeerId[:])
132 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
133 ar, err := Announce("udp://tracker.openbittorrent.com:80/announce", &req)
134 // Skip temporary errors as we don't control the server.
135 if ne, ok := err.(net.Error); ok {
140 // Skip DNS errors because the network might not be available, and we
141 // don't control the domains we're testing.
142 if oe, ok := err.(*net.OpError); ok {
143 if _, ok := oe.Err.(*net.DNSError); ok {
147 require.NoError(t, err)
151 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
154 // This test involves contacting third party servers that may have
155 // unpreditable results.
158 req := AnnounceRequest{
161 rand.Read(req.PeerId[:])
162 rand.Read(req.InfoHash[:])
163 wg := sync.WaitGroup{}
164 success := make(chan bool)
165 fail := make(chan struct{})
166 for _, url := range []string{
167 "udp://tracker.openbittorrent.com:80/announce",
168 "udp://tracker.publicbt.com:80",
169 "udp://tracker.istole.it:6969",
170 "udp://tracker.ccc.de:80",
171 "udp://tracker.open.demonii.com:1337",
172 "udp://open.demonii.com:1337",
173 "udp://exodus.desync.com:6969",
176 go func(url string) {
178 resp, err := Announce(url, &req)
180 t.Logf("error announcing to %s: %s", url, err)
183 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
184 // The info hash we generated was random in 2^160 space. If we
185 // get a hit, something is weird.
188 t.Logf("announced to %s", url)
189 // TODO: Can probably get stuck here, but it's just a throwaway
200 // It doesn't matter if they all fail, the servers could just be down.
202 // Bail as quickly as we can. One success is enough.
206 // Check that URLPath option is done correctly.
207 func TestURLPathOption(t *testing.T) {
208 conn, err := net.ListenUDP("udp", nil)
214 _, err := Announce((&url.URL{
216 Host: conn.LocalAddr().String(),
218 }).String(), &AnnounceRequest{})
222 require.NoError(t, err)
225 _, addr, _ := conn.ReadFrom(b[:])
226 r := bytes.NewReader(b[:])
230 write(w, ResponseHeader{
231 TransactionId: h.TransactionId,
233 write(w, ConnectionResponse{42})
234 conn.WriteTo(w.Bytes(), addr)
235 n, _, _ := conn.ReadFrom(b[:])
236 r = bytes.NewReader(b[:n])
238 read(r, &AnnounceRequest{})
239 all, _ := ioutil.ReadAll(r)
240 if string(all) != "\x02\x09/announce" {
244 write(w, ResponseHeader{
245 TransactionId: h.TransactionId,
247 write(w, AnnounceResponseHeader{})
248 conn.WriteTo(w.Bytes(), addr)