forked from holepunchto/dht-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
627 lines (466 loc) · 14.2 KB
/
test.js
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
const test = require('brittle')
const UDX = require('udx-native')
const DHT = require('./')
test('bootstrapper', async function (t) {
const port = await freePort()
const node = DHT.bootstrapper(port, '127.0.0.1')
await node.ready()
if (node.address().family === 4) {
t.is(node.address().host, '0.0.0.0')
t.is(node.address().family, 4)
} else {
t.is(node.address().host, '::')
t.is(node.address().family, 6)
}
t.is(node.address().port, port)
await node.destroy()
})
test('bootstrapper - bind host', async function (t) {
const port = await freePort()
const node = DHT.bootstrapper(port, '127.0.0.1', { host: '127.0.0.1' })
await node.ready()
t.is(node.address().host, '127.0.0.1')
t.is(node.address().family, 4)
t.is(node.address().port, port)
await node.destroy()
})
test('bootstrapper - opts.bootstrap', async function (t) {
const port1 = await freePort()
const port2 = await freePort()
const node1 = DHT.bootstrapper(port1, '127.0.0.1')
await node1.ready()
const bootstrap = [{ host: '127.0.0.1', port: node1.address().port }]
const node2 = DHT.bootstrapper(port2, '127.0.0.1', { bootstrap })
await node2.ready()
t.is(node1.bootstrapNodes.length, 0)
t.alike(node2.bootstrapNodes, bootstrap)
await node1.destroy()
await node2.destroy()
})
test('bootstrapper - port and host are required', function (t) {
t.plan(3)
try {
DHT.bootstrapper()
} catch (error) {
t.is(error.message, 'Port is required')
}
try {
DHT.bootstrapper(0)
} catch (error) {
t.is(error.message, 'Port is required')
}
try {
DHT.bootstrapper(49737)
} catch (error) {
t.is(error.message, 'Host is required')
}
})
test('make tiny swarm', async function (t) {
await makeSwarm(2, t)
t.pass('could make swarm')
})
test('make bigger swarm', { timeout: 120000 }, async function (t) {
const swarm = await makeSwarm(500, t)
const targetNode = swarm[25]
const target = targetNode.id
let q = swarm[499].findNode(target)
let messages = 0
let found = false
for await (const data of q) {
messages++
if (data.from.id && data.from.id.equals(target)) {
found = true
break
}
}
const replies = q.closestReplies
t.ok(found, 'found target in ' + messages + ' message(s)')
q = swarm[490].findNode(target, { nodes: q.closestNodes })
messages = 0
found = false
for await (const data of q) {
messages++
if (data.from.id && data.from.id.equals(target)) {
found = true
break
}
}
t.ok(found, 'found target again in ' + messages + ' message(s)')
q = swarm[470].findNode(target, { replies })
messages = 0
found = false
for await (const data of q) {
messages++
if (data.from.id && data.from.id.equals(target)) {
found = true
break
}
}
t.ok(found, 'found target again in ' + messages + ' message(s) with original replies')
const { firewalled, host, port } = swarm[490]
t.is(firewalled, false)
t.is(port, swarm[490].address().port)
t.ok(host)
})
test('commit after query', async function (t) {
const swarm = await makeSwarm(100, t)
const BEFORE = 0
const AFTER = 1
let commits = 0
for (const node of swarm) {
node.on('request', function (req) {
if (req.command === BEFORE) {
return req.reply(null)
}
if (req.command === AFTER && req.token) {
commits++
return req.reply(null)
}
})
}
const q = swarm[42].query({ command: BEFORE, target: swarm[0].table.id }, {
commit (m, dht, query) {
return dht.request({ command: AFTER, target: query.target, token: m.token }, m.from)
}
})
await q.finished()
t.is(commits, swarm[42].table.k)
})
test('map query stream', async function (t) {
const swarm = await makeSwarm(10, t)
const expected = []
const q = swarm[0].findNode(swarm[0].table.id, {
map (data) {
if (expected.length > 3) return null
expected.push(data.from.id)
return data.from.id
}
})
const buf = []
q.on('data', (data) => buf.push(data))
await q.finished()
t.ok(expected.length > 0)
t.alike(buf, expected)
})
test('timeouts', async function (t) {
const [, a, b] = await makeSwarm(3, t)
let tries = 0
const NOPE = 52
t.plan(4)
b.on('request', function (req) {
if (req.command === NOPE) {
tries++
t.pass('ignoring request')
}
})
const q = a.query({ command: NOPE, target: Buffer.alloc(32) })
await q.finished()
t.is(tries, 3)
})
test('request with/without retries', async function (t) {
const [, a, b] = await makeSwarm(3, t)
let tries = 0
const NOPE = 442
b.on('request', function (req) {
if (req.command === NOPE) {
tries++
t.pass('ignoring request')
}
})
try {
await a.request({ command: NOPE }, { host: '127.0.0.1', port: b.address().port })
} catch {
// do nothing
}
t.is(tries, 3)
try {
await a.request({ command: NOPE }, { host: '127.0.0.1', port: b.address().port }, { retry: false })
} catch {
// do nothing
}
t.is(tries, 4)
})
test('shorthand commit', async function (t) {
const swarm = await makeSwarm(40, t)
let tokens = 0
let notTokens = 0
for (const node of swarm) {
node.on('request', function (req) {
if (req.token) tokens++
else notTokens++
req.reply(null)
})
}
const q = swarm[0].query({ command: 42, target: Buffer.alloc(32) }, { commit: true })
await q.finished()
t.is(tokens, 20)
t.ok(notTokens >= tokens)
})
test('after ready it is always bound', async function (t) {
t.plan(2)
const node = new DHT()
node.on('listening', function () {
t.pass('is listening')
})
await node.ready()
const addr = node.address()
t.ok(typeof addr.port, 'is number')
await node.destroy()
})
test('timeouts when commiting', async function (t) {
const [, a, b] = await makeSwarm(3, t)
let tries = 0
const NOPE = 41
b.on('request', function (req) {
if (req.command === NOPE) {
tries++
t.pass('ignoring request')
}
})
const q = a.query({ command: NOPE, target: Buffer.alloc(32) }, { commit: true })
let error = null
try {
await q.finished()
} catch (err) {
error = err
}
t.ok(error, 'commit should fail')
t.is(tries, 3)
})
test('toArray', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3, t)
t.alike(a.toArray(), [{ host: '127.0.0.1', port: b.address().port }])
t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }])
t.alike(bootstrap.toArray().sort(), [{ host: '127.0.0.1', port: a.address().port }, { host: '127.0.0.1', port: b.address().port }].sort())
})
test('addNode / nodes option', async function (t) {
const [bootstrap, a] = await makeSwarm(2, t)
a.on('request', function (req) {
t.is(req.value, null, 'expected data')
req.reply(Buffer.from('world'))
})
await bootstrap.ready()
await a.ready()
const b = new DHT({ ephemeral: false, nodes: [{ host: '127.0.0.1', port: a.address().port }] })
await b.ready()
const bNodes = b.toArray()
t.alike(bNodes, [{ host: '127.0.0.1', port: a.address().port }])
const responses = []
for await (const data of b.query({ command: 52, target: a.id })) {
responses.push(data)
}
t.is(responses.length, 1, 'one response')
t.alike(responses[0].value, Buffer.from('world'), 'responded')
const aNodes = a.toArray()
t.alike(aNodes, [{ host: '127.0.0.1', port: b.address().port }])
await b.destroy()
})
test('set bind', async function (t) {
const port = await freePort()
const a = new DHT({ port, firewalled: false })
await a.ready()
t.alike(a.address().port, port, 'bound to explicit port')
const b = new DHT({ port })
await b.ready()
t.not(b.address().port, port, 'bound to different port as explicit one is taken')
await a.destroy()
await b.destroy()
})
test('relay', async function (t) {
const [, a, b, c] = await makeSwarm(4, t)
const ROUTE = 1
b.on('request', function (req) {
t.is(req.command, ROUTE, 'b got request')
t.is(req.from.port, a.address().port, 'from a')
const value = Buffer.concat([req.value, Buffer.from('b')])
req.relay(value, { host: '127.0.0.1', port: c.address().port })
})
c.on('request', function (req) {
t.is(req.command, ROUTE, 'c got request')
t.is(req.from.port, b.address().port, 'from b')
const value = Buffer.concat([req.value, Buffer.from('c')])
req.reply(value, { to: { host: '127.0.0.1', port: a.address().port } })
})
const res = await a.request({ command: ROUTE, value: Buffer.from('a') }, { host: '127.0.0.1', port: b.address().port })
t.alike(res.value, Buffer.from('abc'))
t.is(res.from.port, c.address().port)
t.is(res.to.port, a.address().port)
})
test('filter nodes from routing table', async function (t) {
const [a, b, c] = await makeSwarm(3, t)
const node = new DHT({
ephemeral: false,
bootstrap: [a],
addNode (from) {
return from.port !== b.port
}
})
t.teardown(() => node.destroy())
const q = node.findNode(c.id)
await q.finished()
t.absent(node.table.has(b.id), 'should not have b')
})
test('request session, destroy all', async function (t) {
const [, a, b] = await makeSwarm(3, t)
a.on('request', () => t.fail())
const s = b.session()
const p = [
s.request({ command: 42 }, a),
s.request({ command: 42 }, a)
]
const err = new Error('destroyed')
s.destroy(err)
for (const { status, reason } of await Promise.allSettled(p)) {
t.is(status, 'rejected')
t.is(reason, err)
}
})
test('close event', async function (t) {
t.plan(1)
const node = new DHT()
node.once('close', function () {
t.pass('node closed')
})
await node.destroy()
})
test('close event is only emitted once', async function (t) {
t.plan(2)
const node = new DHT()
let count = 0
node.on('close', function () {
if (++count >= 2) t.fail('close was emitted more than once')
else t.pass('node closed')
})
await Promise.all([node.destroy(), node.destroy()])
await node.destroy()
await node.destroy()
t.pass()
})
test('local address', async function (t) {
t.plan(4)
const node = new DHT()
t.is(node.localAddress(), null)
await node.ready()
t.comment('Local address (host): ' + node.localAddress().host)
t.ok(node.localAddress().host.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/))
t.is(node.localAddress().port, node.io.serverSocket.address().port)
await node.destroy()
t.pass()
})
test('remote address without peers', async function (t) {
t.plan(3)
const node = new DHT()
t.is(node.remoteAddress(), null)
await node.ready()
t.is(node.remoteAddress(), null)
await node.destroy()
t.pass()
})
test('remote address with peers', async function (t) {
t.plan(6)
const a = new DHT({ ephemeral: false, firewalled: false })
t.is(a.remoteAddress(), null)
await a.ready()
t.is(a.remoteAddress(), null)
const bootstrap = ['localhost:' + a.address().port]
const b = new DHT({ ephemeral: false, bootstrap })
t.is(b.remoteAddress(), null)
await b.ready()
t.is(b.remoteAddress().host, '127.0.0.1')
t.is(a.remoteAddress().host, '127.0.0.1')
await a.destroy()
await b.destroy()
t.pass()
})
test('nat update event', async function (t) {
t.plan(4)
const a = new DHT({ ephemeral: false, firewalled: false })
await a.ready()
const bootstrap = ['localhost:' + a.address().port]
const b = new DHT({ ephemeral: false, bootstrap })
b.once('nat-update', function (host, port) {
t.is(host, '127.0.0.1')
t.is(port, b.io.clientSocket.address().port)
b.once('nat-update', function (host, port) {
t.is(host, '127.0.0.1')
t.is(port, b.io.serverSocket.address().port)
})
})
await b.ready()
await a.destroy()
await b.destroy()
})
test('suspend - random port', async function (t) {
const a = new DHT({ ephemeral: false, firewalled: false })
await a.ready()
const bootstrap = ['localhost:' + a.address().port]
const serverPortBefore = a.io.serverSocket.address().port
const clientPortBefore = a.io.clientSocket.address().port
await a.suspend()
await a.resume()
const serverPortAfter = a.io.serverSocket.address().port
const clientPortAfter = a.io.clientSocket.address().port
t.is(serverPortBefore, serverPortAfter)
t.is(clientPortBefore, clientPortAfter)
// Very basic check that the rebinded node still works
const c = new DHT({ ephemeral: false, bootstrap })
t.is(c.remoteAddress(), null)
await c.ready()
t.is(c.remoteAddress().host, '127.0.0.1')
await c.destroy()
await a.destroy()
})
test('suspend - custom port', async function (t) {
const port = await freePort()
const a = new DHT({ ephemeral: false, firewalled: false, port, anyPort: false })
await a.ready()
const b = new DHT({ ephemeral: false, firewalled: false, port })
await b.ready()
const bootstrap = ['localhost:' + b.address().port]
await a.destroy()
const serverPortBefore = b.io.serverSocket.address().port
const clientPortBefore = b.io.clientSocket.address().port
await a.suspend()
await a.resume()
const serverPortAfter = b.io.serverSocket.address().port
const clientPortAfter = b.io.clientSocket.address().port
t.is(serverPortBefore, serverPortAfter)
t.is(clientPortBefore, clientPortAfter)
t.not(serverPortBefore, port)
// Very basic check that the rebinded node still works
const c = new DHT({ ephemeral: false, bootstrap })
t.is(c.remoteAddress(), null)
await c.ready()
t.is(c.remoteAddress().host, '127.0.0.1')
await c.destroy()
await b.destroy()
})
test('response includes roundtrip time', async function (t) {
const [, a, b] = await makeSwarm(3, t)
const NOPE = 442
const response = await a.request({ command: NOPE }, { host: '127.0.0.1', port: b.address().port })
t.ok(response.rtt !== undefined)
})
async function freePort () {
const udx = new UDX()
const sock = udx.createSocket()
sock.bind(0)
const port = sock.address().port
await sock.close()
return port
}
async function makeSwarm (n, t) {
const node = new DHT({ ephemeral: false, firewalled: false })
await node.ready()
const all = [node]
const bootstrap = ['localhost:' + node.address().port]
while (all.length < n) {
const node = new DHT({ ephemeral: false, bootstrap })
await node.ready()
all.push(node)
}
t.teardown(async function () {
for (const node of all) await node.destroy()
})
return all
}