16 "github.com/anacrolix/dht/krpc"
17 _ "github.com/anacrolix/envpprof"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
22 var trackers = []string{
23 "udp://tracker.coppersurfer.tk:6969",
24 "udp://tracker.leechers-paradise.org:6969",
27 // Ensure net.IPs are stored big-endian, to match the way they're read from
29 func TestNetIPv4Bytes(t *testing.T) {
30 ip := net.IP([]byte{127, 0, 0, 1})
31 if ip.String() != "127.0.0.1" {
34 if string(ip) != "\x7f\x00\x00\x01" {
39 func TestMarshalAnnounceResponse(t *testing.T) {
40 peers := krpc.CompactIPv4NodeAddrs{
41 {[]byte{127, 0, 0, 1}, 2},
42 {[]byte{255, 0, 0, 3}, 4},
44 b, err := peers.MarshalBinary()
45 require.NoError(t, err)
46 require.EqualValues(t,
47 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
49 require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
52 // Failure to write an entire packet to UDP is expected to given an error.
53 func TestLongWriteUDP(t *testing.T) {
55 l, err := net.ListenUDP("udp4", nil)
56 require.NoError(t, err)
58 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
63 for msgLen := 1; ; msgLen *= 2 {
64 n, err := c.Write(make([]byte, msgLen))
66 require.Contains(t, err.Error(), "message too long")
75 func TestShortBinaryRead(t *testing.T) {
76 var data ResponseHeader
77 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
78 if err != io.ErrUnexpectedEOF {
83 func TestConvertInt16ToInt(t *testing.T) {
85 if int(uint16(int16(i))) != 50000 {
90 func TestAnnounceLocalhost(t *testing.T) {
93 t: map[[20]byte]torrent{
94 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
97 Peers: krpc.CompactIPv4NodeAddrs{
98 {[]byte{1, 2, 3, 4}, 5},
99 {[]byte{6, 7, 8, 9}, 10},
105 srv.pc, err = net.ListenPacket("udp", ":0")
106 require.NoError(t, err)
109 require.NoError(t, srv.serveOne())
111 req := AnnounceRequest{
115 rand.Read(req.PeerId[:])
116 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
118 require.NoError(t, srv.serveOne())
121 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
124 require.NoError(t, err)
125 assert.EqualValues(t, 1, ar.Seeders)
126 assert.EqualValues(t, 2, len(ar.Peers))
129 func TestUDPTracker(t *testing.T) {
134 req := AnnounceRequest{
137 rand.Read(req.PeerId[:])
138 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
140 TrackerUrl: trackers[0],
143 // Skip any net errors as we don't control the server.
144 if _, ok := err.(net.Error); 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 ctx, cancel := context.WithCancel(context.Background())
165 for _, url := range trackers {
167 go func(url string) {
169 resp, err := Announce{
175 t.Logf("error announcing to %s: %s", url, err)
178 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
179 // The info hash we generated was random in 2^160 space. If we
180 // get a hit, something is weird.
183 t.Logf("announced to %s", url)
190 // Check that URLPath option is done correctly.
191 func TestURLPathOption(t *testing.T) {
192 conn, err := net.ListenUDP("udp", nil)
199 TrackerUrl: (&url.URL{
201 Host: conn.LocalAddr().String(),
208 require.NoError(t, err)
211 _, addr, _ := conn.ReadFrom(b[:])
212 r := bytes.NewReader(b[:])
216 write(w, ResponseHeader{
217 TransactionId: h.TransactionId,
219 write(w, ConnectionResponse{42})
220 conn.WriteTo(w.Bytes(), addr)
221 n, _, _ := conn.ReadFrom(b[:])
222 r = bytes.NewReader(b[:n])
224 read(r, &AnnounceRequest{})
225 all, _ := ioutil.ReadAll(r)
226 if string(all) != "\x02\x09/announce" {
230 write(w, ResponseHeader{
231 TransactionId: h.TransactionId,
233 write(w, AnnounceResponseHeader{})
234 conn.WriteTo(w.Bytes(), addr)