15 "github.com/anacrolix/dht/krpc"
16 _ "github.com/anacrolix/envpprof"
17 "github.com/stretchr/testify/assert"
18 "github.com/stretchr/testify/require"
21 var trackers = []string{
22 "udp://tracker.coppersurfer.tk:6969",
23 "udp://tracker.leechers-paradise.org:6969",
26 // Ensure net.IPs are stored big-endian, to match the way they're read from
28 func TestNetIPv4Bytes(t *testing.T) {
29 ip := net.IP([]byte{127, 0, 0, 1})
30 if ip.String() != "127.0.0.1" {
33 if string(ip) != "\x7f\x00\x00\x01" {
38 func TestMarshalAnnounceResponse(t *testing.T) {
39 peers := krpc.CompactIPv4NodeAddrs{
40 {[]byte{127, 0, 0, 1}, 2},
41 {[]byte{255, 0, 0, 3}, 4},
43 b, err := peers.MarshalBinary()
44 require.NoError(t, err)
45 require.EqualValues(t,
46 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
48 require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
51 // Failure to write an entire packet to UDP is expected to given an error.
52 func TestLongWriteUDP(t *testing.T) {
54 l, err := net.ListenUDP("udp4", nil)
55 require.NoError(t, err)
57 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
62 for msgLen := 1; ; msgLen *= 2 {
63 n, err := c.Write(make([]byte, msgLen))
65 require.Contains(t, err.Error(), "message too long")
74 func TestShortBinaryRead(t *testing.T) {
75 var data ResponseHeader
76 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
77 if err != io.ErrUnexpectedEOF {
82 func TestConvertInt16ToInt(t *testing.T) {
84 if int(uint16(int16(i))) != 50000 {
89 func TestAnnounceLocalhost(t *testing.T) {
92 t: map[[20]byte]torrent{
93 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
96 Peers: krpc.CompactIPv4NodeAddrs{
97 {[]byte{1, 2, 3, 4}, 5},
98 {[]byte{6, 7, 8, 9}, 10},
104 srv.pc, err = net.ListenPacket("udp", ":0")
105 require.NoError(t, err)
108 require.NoError(t, srv.serveOne())
110 req := AnnounceRequest{
114 rand.Read(req.PeerId[:])
115 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
117 require.NoError(t, srv.serveOne())
120 TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
123 require.NoError(t, err)
124 assert.EqualValues(t, 1, ar.Seeders)
125 assert.EqualValues(t, 2, len(ar.Peers))
128 func TestUDPTracker(t *testing.T) {
133 req := AnnounceRequest{
136 rand.Read(req.PeerId[:])
137 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
139 TrackerUrl: trackers[0],
142 // Skip any net errors as we don't control the server.
143 if _, ok := err.(net.Error); ok {
146 require.NoError(t, err)
150 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
153 // This test involves contacting third party servers that may have
154 // unpreditable results.
157 req := AnnounceRequest{
160 rand.Read(req.PeerId[:])
161 rand.Read(req.InfoHash[:])
162 wg := sync.WaitGroup{}
163 success := make(chan bool)
164 fail := make(chan struct{})
165 for _, url := range trackers {
167 go func(url string) {
169 resp, err := Announce{
174 t.Logf("error announcing to %s: %s", url, err)
177 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
178 // The info hash we generated was random in 2^160 space. If we
179 // get a hit, something is weird.
182 t.Logf("announced to %s", url)
183 // TODO: Can probably get stuck here, but it's just a throwaway
194 // It doesn't matter if they all fail, the servers could just be down.
196 // Bail as quickly as we can. One success is enough.
200 // Check that URLPath option is done correctly.
201 func TestURLPathOption(t *testing.T) {
202 conn, err := net.ListenUDP("udp", nil)
209 TrackerUrl: (&url.URL{
211 Host: conn.LocalAddr().String(),
218 require.NoError(t, err)
221 _, addr, _ := conn.ReadFrom(b[:])
222 r := bytes.NewReader(b[:])
226 write(w, ResponseHeader{
227 TransactionId: h.TransactionId,
229 write(w, ConnectionResponse{42})
230 conn.WriteTo(w.Bytes(), addr)
231 n, _, _ := conn.ReadFrom(b[:])
232 r = bytes.NewReader(b[:n])
234 read(r, &AnnounceRequest{})
235 all, _ := ioutil.ReadAll(r)
236 if string(all) != "\x02\x09/announce" {
240 write(w, ResponseHeader{
241 TransactionId: h.TransactionId,
243 write(w, AnnounceResponseHeader{})
244 conn.WriteTo(w.Bytes(), addr)