16 "github.com/anacrolix/torrent/util"
19 // Ensure net.IPs are stored big-endian, to match the way they're read from
21 func TestNetIPv4Bytes(t *testing.T) {
22 ip := net.IP([]byte{127, 0, 0, 1})
23 if ip.String() != "127.0.0.1" {
26 if string(ip) != "\x7f\x00\x00\x01" {
31 func TestMarshalAnnounceResponse(t *testing.T) {
33 peers := util.CompactPeers{{[4]byte{127, 0, 0, 1}, 2}, {[4]byte{255, 0, 0, 3}, 4}}
34 err := peers.WriteBinary(&w)
36 t.Fatalf("error writing udp announce response addrs: %s", err)
38 if w.String() != "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04" {
41 if binary.Size(AnnounceResponseHeader{}) != 12 {
46 // Failure to write an entire packet to UDP is expected to given an error.
47 func TestLongWriteUDP(t *testing.T) {
48 l, err := net.ListenUDP("udp", nil)
53 c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
58 for msgLen := 1; ; msgLen *= 2 {
59 n, err := c.Write(make([]byte, msgLen))
61 err := err.(*net.OpError).Err
62 if err != syscall.EMSGSIZE {
63 t.Fatalf("write error isn't EMSGSIZE: %s", err)
73 func TestShortBinaryRead(t *testing.T) {
74 var data ResponseHeader
75 err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
76 if err != io.ErrUnexpectedEOF {
81 func TestConvertInt16ToInt(t *testing.T) {
83 if int(uint16(int16(i))) != 50000 {
88 func TestUDPTracker(t *testing.T) {
92 tr, err := New("udp://tracker.openbittorrent.com:80/announce")
96 if err := tr.Connect(); err != nil {
99 req := AnnounceRequest{
103 rand.Read(req.PeerId[:])
104 copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
105 _, err = tr.Announce(&req)
111 // TODO: Create a fake UDP tracker to make these requests to.
112 func TestAnnounceRandomInfoHash(t *testing.T) {
116 req := AnnounceRequest{
119 rand.Read(req.PeerId[:])
120 rand.Read(req.InfoHash[:])
121 wg := sync.WaitGroup{}
122 for _, url := range []string{
123 "udp://tracker.openbittorrent.com:80/announce",
124 "udp://tracker.publicbt.com:80",
125 "udp://tracker.istole.it:6969",
126 "udp://tracker.ccc.de:80",
127 "udp://tracker.open.demonii.com:1337",
129 go func(url string) {
135 if err := tr.Connect(); err != nil {
139 resp, err := tr.Announce(&req)
141 t.Logf("error announcing to %s: %s", url, err)
144 if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
153 // Check that URLPath option is done correctly.
154 func TestURLPathOption(t *testing.T) {
155 conn, err := net.ListenUDP("udp", nil)
160 cl := newClient(&url.URL{
161 Host: conn.LocalAddr().String(),
169 log.Print("connected")
170 _, err = cl.Announce(&AnnounceRequest{})
176 _, addr, _ := conn.ReadFrom(b[:])
177 r := bytes.NewReader(b[:])
181 write(w, ResponseHeader{
182 TransactionId: h.TransactionId,
184 write(w, ConnectionResponse{42})
185 conn.WriteTo(w.Bytes(), addr)
186 n, _, _ := conn.ReadFrom(b[:])
187 r = bytes.NewReader(b[:n])
189 read(r, &AnnounceRequest{})
190 all, _ := ioutil.ReadAll(r)
191 if string(all) != "\x02\x09/announce" {
195 write(w, ResponseHeader{
196 TransactionId: h.TransactionId,
198 write(w, AnnounceResponseHeader{})
199 conn.WriteTo(w.Bytes(), addr)