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 {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 any net errors as we don't control the server.
135 if _, ok := err.(net.Error); ok {
138 require.NoError(t, err)
142 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
145 // This test involves contacting third party servers that may have
146 // unpreditable results.
149 req := AnnounceRequest{
152 rand.Read(req.PeerId[:])
153 rand.Read(req.InfoHash[:])
154 wg := sync.WaitGroup{}
155 success := make(chan bool)
156 fail := make(chan struct{})
157 for _, url := range []string{
158 "udp://tracker.openbittorrent.com:80/announce",
159 "udp://tracker.publicbt.com:80",
160 "udp://tracker.istole.it:6969",
161 "udp://tracker.ccc.de:80",
162 "udp://tracker.open.demonii.com:1337",
163 "udp://open.demonii.com:1337",
164 "udp://exodus.desync.com:6969",
167 go func(url string) {
169 resp, err := Announce(url, &req)
171 t.Logf("error announcing to %s: %s", url, err)
174 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
175 // The info hash we generated was random in 2^160 space. If we
176 // get a hit, something is weird.
179 t.Logf("announced to %s", url)
180 // TODO: Can probably get stuck here, but it's just a throwaway
191 // It doesn't matter if they all fail, the servers could just be down.
193 // Bail as quickly as we can. One success is enough.
197 // Check that URLPath option is done correctly.
198 func TestURLPathOption(t *testing.T) {
199 conn, err := net.ListenUDP("udp", nil)
205 _, err := Announce((&url.URL{
207 Host: conn.LocalAddr().String(),
209 }).String(), &AnnounceRequest{})
213 require.NoError(t, err)
216 _, addr, _ := conn.ReadFrom(b[:])
217 r := bytes.NewReader(b[:])
221 write(w, ResponseHeader{
222 TransactionId: h.TransactionId,
224 write(w, ConnectionResponse{42})
225 conn.WriteTo(w.Bytes(), addr)
226 n, _, _ := conn.ReadFrom(b[:])
227 r = bytes.NewReader(b[:n])
229 read(r, &AnnounceRequest{})
230 all, _ := ioutil.ReadAll(r)
231 if string(all) != "\x02\x09/announce" {
235 write(w, ResponseHeader{
236 TransactionId: h.TransactionId,
238 write(w, AnnounceResponseHeader{})
239 conn.WriteTo(w.Bytes(), addr)