18 "github.com/anacrolix/dht/v2/krpc"
19 _ "github.com/anacrolix/envpprof"
20 "github.com/anacrolix/torrent/tracker/udp"
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
25 var trackers = []string{
26 "udp://tracker.coppersurfer.tk:6969",
27 "udp://tracker.leechers-paradise.org:6969",
30 // Ensure net.IPs are stored big-endian, to match the way they're read from
32 func TestNetIPv4Bytes(t *testing.T) {
33 ip := net.IP([]byte{127, 0, 0, 1})
34 if ip.String() != "127.0.0.1" {
37 if string(ip) != "\x7f\x00\x00\x01" {
42 func TestMarshalAnnounceResponse(t *testing.T) {
43 peers := krpc.CompactIPv4NodeAddrs{
44 {[]byte{127, 0, 0, 1}, 2},
45 {[]byte{255, 0, 0, 3}, 4},
47 b, err := peers.MarshalBinary()
48 require.NoError(t, err)
49 require.EqualValues(t,
50 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
52 require.EqualValues(t, 12, binary.Size(udp.AnnounceResponseHeader{}))
55 // Failure to write an entire packet to UDP is expected to given an error.
56 func TestLongWriteUDP(t *testing.T) {
58 l, err := net.ListenUDP("udp4", nil)
59 require.NoError(t, err)
61 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
66 for msgLen := 1; ; msgLen *= 2 {
67 n, err := c.Write(make([]byte, msgLen))
69 require.Contains(t, err.Error(), "message too long")
78 func TestShortBinaryRead(t *testing.T) {
79 var data udp.ResponseHeader
80 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
81 if err != io.ErrUnexpectedEOF {
86 func TestConvertInt16ToInt(t *testing.T) {
88 if int(uint16(int16(i))) != 50000 {
93 func TestAnnounceLocalhost(t *testing.T) {
96 t: map[[20]byte]torrent{
97 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
100 Peers: krpc.CompactIPv4NodeAddrs{
101 {[]byte{1, 2, 3, 4}, 5},
102 {[]byte{6, 7, 8, 9}, 10},
108 srv.pc, err = net.ListenPacket("udp", ":0")
109 require.NoError(t, err)
112 require.NoError(t, srv.serveOne())
114 req := AnnounceRequest{
118 rand.Read(req.PeerId[:])
119 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
121 require.NoError(t, srv.serveOne())
124 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
127 require.NoError(t, err)
128 assert.EqualValues(t, 1, ar.Seeders)
129 assert.EqualValues(t, 2, len(ar.Peers))
132 func TestUDPTracker(t *testing.T) {
137 req := AnnounceRequest{
140 rand.Read(req.PeerId[:])
141 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
142 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
144 if dl, ok := t.Deadline(); ok {
146 ctx, cancel = context.WithDeadline(context.Background(), dl.Add(-time.Second))
150 TrackerUrl: trackers[0],
154 // Skip any net errors as we don't control the server.
156 if errors.As(err, &ne) {
159 require.NoError(t, err)
163 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
166 // This test involves contacting third party servers that may have
167 // unpredictable results.
170 req := AnnounceRequest{
173 rand.Read(req.PeerId[:])
174 rand.Read(req.InfoHash[:])
175 wg := sync.WaitGroup{}
176 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
178 if dl, ok := t.Deadline(); ok {
180 ctx, cancel = context.WithDeadline(ctx, dl.Add(-time.Second))
183 for _, url := range trackers {
185 go func(url string) {
187 resp, err := Announce{
193 t.Logf("error announcing to %s: %s", url, err)
196 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
197 // The info hash we generated was random in 2^160 space. If we
198 // get a hit, something is weird.
201 t.Logf("announced to %s", url)
209 // Check that URLPath option is done correctly.
210 func TestURLPathOption(t *testing.T) {
211 conn, err := net.ListenUDP("udp", nil)
216 announceErr := make(chan error)
219 TrackerUrl: (&url.URL{
221 Host: conn.LocalAddr().String(),
229 _, addr, _ := conn.ReadFrom(b[:])
230 r := bytes.NewReader(b[:])
231 var h udp.RequestHeader
234 udp.Write(w, udp.ResponseHeader{
235 Action: udp.ActionConnect,
236 TransactionId: h.TransactionId,
238 udp.Write(w, udp.ConnectionResponse{42})
239 conn.WriteTo(w.Bytes(), addr)
240 n, _, _ := conn.ReadFrom(b[:])
241 r = bytes.NewReader(b[:n])
243 udp.Read(r, &AnnounceRequest{})
244 all, _ := ioutil.ReadAll(r)
245 if string(all) != "\x02\x09/announce" {
249 udp.Write(w, udp.ResponseHeader{
250 Action: udp.ActionAnnounce,
251 TransactionId: h.TransactionId,
253 udp.Write(w, udp.AnnounceResponseHeader{})
254 conn.WriteTo(w.Bytes(), addr)
255 require.NoError(t, <-announceErr)