forked from nictuku/dht
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dht_test.go
338 lines (317 loc) · 8.43 KB
/
dht_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package dht
import (
"crypto/rand"
"dht/remoteNode"
"dht/util"
"expvar"
"flag"
"fmt"
"net"
"strconv"
"sync"
"testing"
"time"
)
// 16 bytes.
const ffff = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
func BenchmarkFindClosest(b *testing.B) {
b.StopTimer()
cfg := NewConfig()
cfg.SaveRoutingTable = false
node, err := New(cfg)
node.nodeId = "00bcdefghij01234567"
if err != nil {
b.Fatal(err)
}
// Add 100k nodes to the remote nodes slice.
for i := 0; i < 100000; i++ {
rId := make([]byte, 4)
if _, err := rand.Read(rId); err != nil {
b.Fatal("Couldnt produce random numbers for FindClosest:", err)
}
// Take the first four bytes of rId and use them to build a random IPv4 address.
ip := net.IPv4(rId[0], rId[1], rId[2], rId[3])
port := i % 65536
address := net.UDPAddr{IP: ip, Port: port}
r := &remoteNode.RemoteNode{
LastQueryID: 0,
ID: string(rId) + ffff,
Address: address,
}
if len(r.ID) != 20 {
b.Fatalf("remoteNode construction error, wrong len: want %d, got %d",
20, len(r.ID))
}
r.Reachable = true
node.routingTable.Insert(r, "udp")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
f := node.routingTable.LookupFiltered(util.InfoHash(fmt.Sprintf("x%10v", i) + "xxxxxxxxx"))
if len(f) != util.KNodes {
b.Fatalf("Missing results. Wanted %d, got %d", util.KNodes, len(f))
}
}
}
// ExampleDHT is a simple example that searches for a particular infohash and
// exits when it finds any peers. A stand-alone version can be found in the
// examples/ directory.
func ExampleDHT() {
if testing.Short() {
fmt.Println("Peer found for the requested infohash or the test was skipped")
return
}
d, err := New(nil)
if err != nil {
fmt.Println(err)
return
}
if err = d.Start(); err != nil {
fmt.Println(err)
return
}
infoHash, err := util.DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2")
if err != nil {
fmt.Printf("DecodeInfoHash faiure: %v", err)
return
}
tick := time.Tick(time.Second)
var infoHashPeers map[util.InfoHash][]string
timer := time.NewTimer(30 * time.Second)
defer timer.Stop()
M:
for {
select {
case <-tick:
// Repeat the request until a result appears, querying nodes that haven't been
// consulted before and finding close-by candidates for the infohash.
d.PeersRequest(string(infoHash), false)
case infoHashPeers = <-d.PeersRequestResults:
break M
case <-timer.C:
fmt.Printf("Could not find new peers: timed out")
return
}
}
for ih, peers := range infoHashPeers {
if len(peers) > 0 {
// Peers are encoded in binary format. Decoding example using github.com/nictuku/util:
//for _, peer := range peers {
// fmt.Println(DecodePeerAddress(peer))
//}
if fmt.Sprintf("%x", ih) == "d1c5676ae7ac98e8b19f63565905105e3c4c37a2" {
fmt.Println("Peer found for the requested infohash or the test was skipped")
return
}
}
}
// Output:
// Peer found for the requested infohash or the test was skipped
}
func startNode(routers string, ih string) (*DHT, error) {
c := NewConfig()
c.SaveRoutingTable = false
c.DHTRouters = routers
c.Port = 0
node, err := New(c)
if err != nil {
return nil, err
}
// Remove the buffer
node.peersRequest = make(chan ihReq)
if err = node.Start(); err != nil {
return nil, err
}
node.PeersRequest(ih, true)
return node, nil
}
// drainResults loops until the target number of peers are found, or a time limit is reached.
func drainResults(n *DHT, ih string, targetCount int, timeout time.Duration) error {
count := 0
for {
select {
case r := <-n.PeersRequestResults:
for _, peers := range r {
for _, x := range peers {
fmt.Printf("Found peer %d: %v\n", count, util.DecodePeerAddress(x))
count++
if count >= targetCount {
return nil
}
}
}
case <-time.Tick(timeout):
return fmt.Errorf("drainResult timed out")
case <-time.Tick(time.Second / 5):
n.PeersRequest(ih, true)
}
}
}
func TestDHTLocal(t *testing.T) {
if testing.Short() {
fmt.Println("Skipping TestDHTLocal")
return
}
//searchRetryPeriod := time.Second
infoHash, err := util.DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2")
if err != nil {
t.Fatalf(err.Error())
}
n1, err := startNode("", string(infoHash))
if err != nil {
t.Errorf("n1 startNode: %v", err)
return
}
router := fmt.Sprintf("localhost:%d", n1.Port())
n2, err := startNode(router, string(infoHash))
if err != nil {
t.Errorf("n2 startNode: %v", err)
return
}
n3, err := startNode(router, string(infoHash))
if err != nil {
t.Errorf("n3 startNode: %v", err)
return
}
// n2 and n3 should find each other.
wg := new(sync.WaitGroup)
wg.Add(2)
go func() {
if err := drainResults(n2, string(infoHash), 1, 10*time.Second); err != nil {
t.Errorf("drainResult n2: %v", err)
}
wg.Done()
}()
go func() {
if err := drainResults(n3, string(infoHash), 1, 10*time.Second); err != nil {
t.Errorf("drainResult n3: %v", err)
}
wg.Done()
}()
wg.Wait()
n1.Stop()
n2.Stop()
n3.Stop()
//searchRetryPeriod = time.Second * 15
}
// Requires Internet access and can be flaky if the server or the internet is
// slow.
func TestDHTLarge(t *testing.T) {
if testing.Short() {
t.Skip("TestDHTLarge requires internet access and can be flaky. Skipping in short mode.")
}
defer stats(t)
c := NewConfig()
c.SaveRoutingTable = false
node, err := New(c)
if err != nil {
t.Fatalf("dht New: %v", err)
}
if err = node.Start(); err != nil {
t.Fatalf("node.Run: %v", err)
}
realDHTNodes := []string{
"1.a.magnets.im",
"router.utorrent.com",
}
for _, addr := range realDHTNodes {
ip, err := net.LookupHost(addr)
if err != nil {
t.Error(err)
continue
}
node.AddNode(ip[0] + ":6881")
}
// Test that we can reach at least one node.
success := false
var (
reachable int
v expvar.Var
)
for i := 0; i < 10; i++ {
v = expvar.Get("totalNodesReached")
reachable, err = strconv.Atoi(v.String())
if err != nil {
t.Errorf("totalNodesReached conversion to int failed: %v", err)
continue
}
if reachable > 0 {
t.Logf("Contacted %d DHT nodes.", reachable)
success = true
break
}
time.Sleep(time.Second)
}
if !success {
t.Fatal("No external DHT node could be contacted.")
}
// Test that we can find peers for a known torrent in a timely fashion.
//
// Torrent from: http://www.clearbits.net/torrents/244-time-management-for-anarchists-1
infoHash := util.InfoHash("\xb4\x62\xc0\xa8\xbc\xef\x1c\xe5\xbb\x56\xb9\xfd\xb8\xcf\x37\xff\xd0\x2f\x5f\x59")
go node.PeersRequest(string(infoHash), true)
var infoHashPeers map[util.InfoHash][]string
select {
case infoHashPeers = <-node.PeersRequestResults:
t.Logf("Found %d peers.", len(infoHashPeers[infoHash]))
case <-time.Tick(10 * time.Second):
t.Fatal("Could not find new peers: timed out")
}
for ih, peers := range infoHashPeers {
if infoHash != ih {
t.Fatal("Unexpected infohash returned")
}
if len(peers) == 0 {
t.Fatal("Could not find new torrent peers.")
}
for _, peer := range peers {
t.Logf("peer found: %v", util.BinaryToDottedPort(peer))
}
}
}
func TestNewDHTConfig(t *testing.T) {
c := NewConfig()
c.Port = 6060
c.NumTargetPeers = 10
d, err := New(c)
if err != nil {
t.Fatalf("DHT failed to init with config: %v", err)
}
if d.config.Port != c.Port || d.config.NumTargetPeers != c.NumTargetPeers {
t.Fatal("DHT not initialized with config")
}
}
func TestRegisterFlags(t *testing.T) {
c := &Config{
DHTRouters: "example.router.com:6060",
MaxNodes: 2020,
CleanupPeriod: time.Second,
SavePeriod: time.Second * 2,
RateLimit: 999,
}
RegisterFlags(c)
if flag.Lookup("routers").DefValue != c.DHTRouters {
t.Fatal("Incorrect routers flag")
}
if flag.Lookup("maxNodes").DefValue != strconv.FormatInt(int64(c.MaxNodes), 10) {
t.Fatal("Incorrect maxNodes flag")
}
if flag.Lookup("cleanupPeriod").DefValue != c.CleanupPeriod.String() {
t.Fatal("Incorrect cleanupPeriod flag")
}
if flag.Lookup("savePeriod").DefValue != c.SavePeriod.String() {
t.Fatal("Incorrect routers flag")
}
if flag.Lookup("rateLimit").DefValue != strconv.FormatInt(c.RateLimit, 10) {
t.Fatal("Incorrect routers flag")
}
}
func stats(t *testing.T) {
t.Logf("=== Stats ===")
t.Logf("totalNodesReached: %v", totalNodesReached)
t.Logf("totalGetPeersDupes: %v", totalGetPeersDupes)
t.Logf("totalFindNodeDupes: %v", totalFindNodeDupes)
t.Logf("totalPeers: %v", totalPeers)
t.Logf("totalSentFindNode: %v", totalSentFindNode)
t.Logf("totalSentGetPeers: %v", totalSentGetPeers)
}