-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathudp_test.go
194 lines (186 loc) · 4.66 KB
/
udp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tracker
import (
"bytes"
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"net"
"net/url"
"sync"
"testing"
"time"
"github.com/anacrolix/dht/v2/krpc"
_ "github.com/anacrolix/envpprof"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anacrolix/torrent/tracker/udp"
)
var trackers = []string{
"udp://tracker.opentrackr.org:1337/announce",
"udp://tracker.openbittorrent.com:6969/announce",
"udp://localhost:42069",
}
func TestAnnounceLocalhost(t *testing.T) {
t.Parallel()
srv := server{
t: map[[20]byte]torrent{
{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
Seeders: 1,
Leechers: 2,
Peers: krpc.CompactIPv4NodeAddrs{
{[]byte{1, 2, 3, 4}, 5},
{[]byte{6, 7, 8, 9}, 10},
},
},
},
}
var err error
srv.pc, err = net.ListenPacket("udp", "localhost:0")
require.NoError(t, err)
defer srv.pc.Close()
go func() {
require.NoError(t, srv.serveOne())
}()
req := AnnounceRequest{
NumWant: -1,
Event: Started,
}
rand.Read(req.PeerId[:])
copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
go func() {
require.NoError(t, srv.serveOne())
}()
ar, err := Announce{
TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
Request: req,
}.Do()
require.NoError(t, err)
assert.EqualValues(t, 1, ar.Seeders)
assert.EqualValues(t, 2, len(ar.Peers))
}
func TestUDPTracker(t *testing.T) {
t.Parallel()
if testing.Short() {
t.SkipNow()
}
req := AnnounceRequest{
NumWant: -1,
}
rand.Read(req.PeerId[:])
copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
defer cancel()
if dl, ok := t.Deadline(); ok {
var cancel func()
ctx, cancel = context.WithDeadline(context.Background(), dl.Add(-time.Second))
defer cancel()
}
ar, err := Announce{
TrackerUrl: trackers[0],
Request: req,
Context: ctx,
}.Do()
// Skip any net errors as we don't control the server.
var ne net.Error
if errors.As(err, &ne) {
t.Skip(err)
}
require.NoError(t, err)
t.Logf("%+v", ar)
}
func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
t.Parallel()
if testing.Short() {
// This test involves contacting third party servers that may have
// unpredictable results.
t.SkipNow()
}
req := AnnounceRequest{
Event: Stopped,
}
rand.Read(req.PeerId[:])
rand.Read(req.InfoHash[:])
wg := sync.WaitGroup{}
ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
defer cancel()
if dl, ok := t.Deadline(); ok {
var cancel func()
ctx, cancel = context.WithDeadline(ctx, dl.Add(-time.Second))
defer cancel()
}
for _, url := range trackers {
wg.Add(1)
go func(url string) {
defer wg.Done()
resp, err := Announce{
TrackerUrl: url,
Request: req,
Context: ctx,
}.Do()
if err != nil {
t.Logf("error announcing to %s: %s", url, err)
return
}
if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
// The info hash we generated was random in 2^160 space. If we
// get a hit, something is weird.
t.Fatal(resp)
}
t.Logf("announced to %s", url)
cancel()
}(url)
}
wg.Wait()
cancel()
}
// Check that URLPath option is done correctly.
func TestURLPathOption(t *testing.T) {
conn, err := net.ListenPacket("udp", "localhost:0")
if err != nil {
panic(err)
}
defer conn.Close()
announceErr := make(chan error)
go func() {
_, err := Announce{
TrackerUrl: (&url.URL{
Scheme: "udp",
Host: conn.LocalAddr().String(),
Path: "/announce",
}).String(),
}.Do()
defer conn.Close()
announceErr <- err
}()
var b [512]byte
// conn.SetReadDeadline(time.Now().Add(time.Second))
_, addr, _ := conn.ReadFrom(b[:])
r := bytes.NewReader(b[:])
var h udp.RequestHeader
udp.Read(r, &h)
w := &bytes.Buffer{}
udp.Write(w, udp.ResponseHeader{
Action: udp.ActionConnect,
TransactionId: h.TransactionId,
})
udp.Write(w, udp.ConnectionResponse{42})
conn.WriteTo(w.Bytes(), addr)
n, _, _ := conn.ReadFrom(b[:])
r = bytes.NewReader(b[:n])
udp.Read(r, &h)
udp.Read(r, &AnnounceRequest{})
all, _ := io.ReadAll(r)
if string(all) != "\x02\x09/announce" {
t.FailNow()
}
w = &bytes.Buffer{}
udp.Write(w, udp.ResponseHeader{
Action: udp.ActionAnnounce,
TransactionId: h.TransactionId,
})
udp.Write(w, udp.AnnounceResponseHeader{})
conn.WriteTo(w.Bytes(), addr)
require.NoError(t, <-announceErr)
}