15 "github.com/anacrolix/dht/krpc"
16 _ "github.com/anacrolix/envpprof"
17 "github.com/stretchr/testify/assert"
18 "github.com/stretchr/testify/require"
21 // Ensure net.IPs are stored big-endian, to match the way they're read from
23 func TestNetIPv4Bytes(t *testing.T) {
24 ip := net.IP([]byte{127, 0, 0, 1})
25 if ip.String() != "127.0.0.1" {
28 if string(ip) != "\x7f\x00\x00\x01" {
33 func TestMarshalAnnounceResponse(t *testing.T) {
34 peers := krpc.CompactIPv4NodeAddrs{
35 {[]byte{127, 0, 0, 1}, 2},
36 {[]byte{255, 0, 0, 3}, 4},
38 b, err := peers.MarshalBinary()
39 require.NoError(t, err)
40 require.EqualValues(t,
41 "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
43 require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
46 // Failure to write an entire packet to UDP is expected to given an error.
47 func TestLongWriteUDP(t *testing.T) {
49 l, err := net.ListenUDP("udp4", nil)
50 require.NoError(t, err)
52 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
57 for msgLen := 1; ; msgLen *= 2 {
58 n, err := c.Write(make([]byte, msgLen))
60 require.Contains(t, err.Error(), "message too long")
69 func TestShortBinaryRead(t *testing.T) {
70 var data ResponseHeader
71 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
72 if err != io.ErrUnexpectedEOF {
77 func TestConvertInt16ToInt(t *testing.T) {
79 if int(uint16(int16(i))) != 50000 {
84 func TestAnnounceLocalhost(t *testing.T) {
87 t: map[[20]byte]torrent{
88 {0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
91 Peers: krpc.CompactIPv4NodeAddrs{
92 {[]byte{1, 2, 3, 4}, 5},
93 {[]byte{6, 7, 8, 9}, 10},
99 srv.pc, err = net.ListenPacket("udp", ":0")
100 require.NoError(t, err)
103 require.NoError(t, srv.serveOne())
105 req := AnnounceRequest{
109 rand.Read(req.PeerId[:])
110 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
112 require.NoError(t, srv.serveOne())
114 ar, err := Announce(defaultClient, defaultHTTPUserAgent, fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()), &req)
115 require.NoError(t, err)
116 assert.EqualValues(t, 1, ar.Seeders)
117 assert.EqualValues(t, 2, len(ar.Peers))
120 func TestUDPTracker(t *testing.T) {
125 req := AnnounceRequest{
128 rand.Read(req.PeerId[:])
129 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
130 ar, err := Announce(defaultClient, defaultHTTPUserAgent, "udp://tracker.openbittorrent.com:80/announce", &req)
131 // Skip any net errors as we don't control the server.
132 if _, ok := err.(net.Error); ok {
135 require.NoError(t, err)
139 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
142 // This test involves contacting third party servers that may have
143 // unpreditable results.
146 req := AnnounceRequest{
149 rand.Read(req.PeerId[:])
150 rand.Read(req.InfoHash[:])
151 wg := sync.WaitGroup{}
152 success := make(chan bool)
153 fail := make(chan struct{})
154 for _, url := range []string{
155 "udp://tracker.openbittorrent.com:80/announce",
156 "udp://tracker.publicbt.com:80",
157 "udp://tracker.istole.it:6969",
158 "udp://tracker.ccc.de:80",
159 "udp://tracker.open.demonii.com:1337",
160 "udp://open.demonii.com:1337",
161 "udp://exodus.desync.com:6969",
164 go func(url string) {
166 resp, err := Announce(defaultClient, defaultHTTPUserAgent, url, &req)
168 t.Logf("error announcing to %s: %s", url, err)
171 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
172 // The info hash we generated was random in 2^160 space. If we
173 // get a hit, something is weird.
176 t.Logf("announced to %s", url)
177 // TODO: Can probably get stuck here, but it's just a throwaway
188 // It doesn't matter if they all fail, the servers could just be down.
190 // Bail as quickly as we can. One success is enough.
194 // Check that URLPath option is done correctly.
195 func TestURLPathOption(t *testing.T) {
196 conn, err := net.ListenUDP("udp", nil)
202 _, err := Announce(defaultClient, defaultHTTPUserAgent, (&url.URL{
204 Host: conn.LocalAddr().String(),
206 }).String(), &AnnounceRequest{})
210 require.NoError(t, err)
213 _, addr, _ := conn.ReadFrom(b[:])
214 r := bytes.NewReader(b[:])
218 write(w, ResponseHeader{
219 TransactionId: h.TransactionId,
221 write(w, ConnectionResponse{42})
222 conn.WriteTo(w.Bytes(), addr)
223 n, _, _ := conn.ReadFrom(b[:])
224 r = bytes.NewReader(b[:n])
226 read(r, &AnnounceRequest{})
227 all, _ := ioutil.ReadAll(r)
228 if string(all) != "\x02\x09/announce" {
232 write(w, ResponseHeader{
233 TransactionId: h.TransactionId,
235 write(w, AnnounceResponseHeader{})
236 conn.WriteTo(w.Bytes(), addr)