16 "github.com/anacrolix/dht/krpc"
17 _ "github.com/anacrolix/envpprof"
18 "github.com/pkg/errors"
19 "github.com/stretchr/testify/assert"
20 "github.com/stretchr/testify/require"
23 var trackers = []string{
24 "udp://tracker.coppersurfer.tk:6969",
25 "udp://tracker.leechers-paradise.org:6969",
28 // Ensure net.IPs are stored big-endian, to match the way they're read from
30 func TestNetIPv4Bytes(t *testing.T) {
31 ip := net.IP([]byte{127, 0, 0, 1})
32 if ip.String() != "127.0.0.1" {
35 if string(ip) != "\x7f\x00\x00\x01" {
40 func TestMarshalAnnounceResponse(t *testing.T) {
41 peers := krpc.CompactIPv4NodeAddrs{
42 {[]byte{127, 0, 0, 1}, 2},
43 {[]byte{255, 0, 0, 3}, 4},
45 b, err := peers.MarshalBinary()
46 require.NoError(t, err)
47 require.EqualValues(t,
48 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
50 require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
53 // Failure to write an entire packet to UDP is expected to given an error.
54 func TestLongWriteUDP(t *testing.T) {
56 l, err := net.ListenUDP("udp4", nil)
57 require.NoError(t, err)
59 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
64 for msgLen := 1; ; msgLen *= 2 {
65 n, err := c.Write(make([]byte, msgLen))
67 require.Contains(t, err.Error(), "message too long")
76 func TestShortBinaryRead(t *testing.T) {
77 var data ResponseHeader
78 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
79 if err != io.ErrUnexpectedEOF {
84 func TestConvertInt16ToInt(t *testing.T) {
86 if int(uint16(int16(i))) != 50000 {
91 func TestAnnounceLocalhost(t *testing.T) {
94 t: map[[20]byte]torrent{
95 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
98 Peers: krpc.CompactIPv4NodeAddrs{
99 {[]byte{1, 2, 3, 4}, 5},
100 {[]byte{6, 7, 8, 9}, 10},
106 srv.pc, err = net.ListenPacket("udp", ":0")
107 require.NoError(t, err)
110 require.NoError(t, srv.serveOne())
112 req := AnnounceRequest{
116 rand.Read(req.PeerId[:])
117 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
119 require.NoError(t, srv.serveOne())
122 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
125 require.NoError(t, err)
126 assert.EqualValues(t, 1, ar.Seeders)
127 assert.EqualValues(t, 2, len(ar.Peers))
130 func TestUDPTracker(t *testing.T) {
135 req := AnnounceRequest{
138 rand.Read(req.PeerId[:])
139 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
141 TrackerUrl: trackers[0],
144 // Skip any net errors as we don't control the server.
145 if _, ok := errors.Cause(err).(net.Error); ok {
148 require.NoError(t, err)
152 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
155 // This test involves contacting third party servers that may have
156 // unpredictable results.
159 req := AnnounceRequest{
162 rand.Read(req.PeerId[:])
163 rand.Read(req.InfoHash[:])
164 wg := sync.WaitGroup{}
165 ctx, cancel := context.WithCancel(context.Background())
166 for _, url := range trackers {
168 go func(url string) {
170 resp, err := Announce{
176 t.Logf("error announcing to %s: %s", url, err)
179 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
180 // The info hash we generated was random in 2^160 space. If we
181 // get a hit, something is weird.
184 t.Logf("announced to %s", url)
191 // Check that URLPath option is done correctly.
192 func TestURLPathOption(t *testing.T) {
193 conn, err := net.ListenUDP("udp", nil)
200 TrackerUrl: (&url.URL{
202 Host: conn.LocalAddr().String(),
209 require.NoError(t, err)
212 _, addr, _ := conn.ReadFrom(b[:])
213 r := bytes.NewReader(b[:])
217 write(w, ResponseHeader{
218 TransactionId: h.TransactionId,
220 write(w, ConnectionResponse{42})
221 conn.WriteTo(w.Bytes(), addr)
222 n, _, _ := conn.ReadFrom(b[:])
223 r = bytes.NewReader(b[:n])
225 read(r, &AnnounceRequest{})
226 all, _ := ioutil.ReadAll(r)
227 if string(all) != "\x02\x09/announce" {
231 write(w, ResponseHeader{
232 TransactionId: h.TransactionId,
234 write(w, AnnounceResponseHeader{})
235 conn.WriteTo(w.Bytes(), addr)