16 "github.com/anacrolix/dht/v2/krpc"
17 _ "github.com/anacrolix/envpprof"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
21 "github.com/anacrolix/torrent/tracker/udp"
24 var trackers = []string{
25 "udp://tracker.opentrackr.org:1337/announce",
26 "udp://tracker.openbittorrent.com:6969/announce",
27 "udp://localhost:42069",
30 func TestAnnounceLocalhost(t *testing.T) {
33 t: map[[20]byte]torrent{
34 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
37 Peers: krpc.CompactIPv4NodeAddrs{
38 {[]byte{1, 2, 3, 4}, 5},
39 {[]byte{6, 7, 8, 9}, 10},
45 srv.pc, err = net.ListenPacket("udp", "localhost:0")
46 require.NoError(t, err)
49 require.NoError(t, srv.serveOne())
51 req := AnnounceRequest{
55 rand.Read(req.PeerId[:])
56 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
58 require.NoError(t, srv.serveOne())
61 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
64 require.NoError(t, err)
65 assert.EqualValues(t, 1, ar.Seeders)
66 assert.EqualValues(t, 2, len(ar.Peers))
69 func TestUDPTracker(t *testing.T) {
74 req := AnnounceRequest{
77 rand.Read(req.PeerId[:])
78 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
79 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
81 if dl, ok := t.Deadline(); ok {
83 ctx, cancel = context.WithDeadline(context.Background(), dl.Add(-time.Second))
87 TrackerUrl: trackers[0],
91 // Skip any net errors as we don't control the server.
93 if errors.As(err, &ne) {
96 require.NoError(t, err)
100 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
103 // This test involves contacting third party servers that may have
104 // unpredictable results.
107 req := AnnounceRequest{
110 rand.Read(req.PeerId[:])
111 rand.Read(req.InfoHash[:])
112 wg := sync.WaitGroup{}
113 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
115 if dl, ok := t.Deadline(); ok {
117 ctx, cancel = context.WithDeadline(ctx, dl.Add(-time.Second))
120 for _, url := range trackers {
122 go func(url string) {
124 resp, err := Announce{
130 t.Logf("error announcing to %s: %s", url, err)
133 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
134 // The info hash we generated was random in 2^160 space. If we
135 // get a hit, something is weird.
138 t.Logf("announced to %s", url)
146 // Check that URLPath option is done correctly.
147 func TestURLPathOption(t *testing.T) {
148 conn, err := net.ListenPacket("udp", "localhost:0")
153 announceErr := make(chan error)
156 TrackerUrl: (&url.URL{
158 Host: conn.LocalAddr().String(),
166 // conn.SetReadDeadline(time.Now().Add(time.Second))
167 _, addr, _ := conn.ReadFrom(b[:])
168 r := bytes.NewReader(b[:])
169 var h udp.RequestHeader
172 udp.Write(w, udp.ResponseHeader{
173 Action: udp.ActionConnect,
174 TransactionId: h.TransactionId,
176 udp.Write(w, udp.ConnectionResponse{42})
177 conn.WriteTo(w.Bytes(), addr)
178 n, _, _ := conn.ReadFrom(b[:])
179 r = bytes.NewReader(b[:n])
181 udp.Read(r, &AnnounceRequest{})
182 all, _ := ioutil.ReadAll(r)
183 if string(all) != "\x02\x09/announce" {
187 udp.Write(w, udp.ResponseHeader{
188 Action: udp.ActionAnnounce,
189 TransactionId: h.TransactionId,
191 udp.Write(w, udp.AnnounceResponseHeader{})
192 conn.WriteTo(w.Bytes(), addr)
193 require.NoError(t, <-announceErr)