-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
175 lines (147 loc) · 4.13 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
const crypto = require('crypto')
const test = require('tape')
const dht = require('dht-rpc')
const discovery = require('./')
const bootstrap = dht({ ephemeral: true })
var bootstrapPort
test('setup bootstrap', t => {
bootstrap.listen(null, err => {
t.error(err)
bootstrapPort = bootstrap.socket.address().port
t.pass(`bootstrap listening at 127.0.0.1:${bootstrapPort}`)
t.end()
})
})
test('ping', t => {
const d1 = inst()
d1.ping((err, res) => {
t.error(err)
t.equal(res.length, 1, 'one bootstrap pinged')
t.equal(res[0].bootstrap.port, bootstrapPort, 'pinged bootstrap port is correct')
t.equal(res[0].bootstrap.host, '127.0.0.1', 'pinged bootstrap host is correct')
t.ok(typeof res[0].rtt === 'number', 'ping rtt number is included')
t.equal(res[0].pong.host, '127.0.0.1', 'pinged pong host is correct')
t.ok(typeof res[0].pong.port === 'number', 'pinged pong port number is included')
d1.destroy()
t.end()
})
})
test('announce & lookup', t => {
const key = crypto.randomBytes(32)
const d1 = inst()
const d2 = inst()
const to = setTimeout(() => {
t.fail('Timed out waiting for lookup')
d1.destroy()
d2.destroy()
t.end()
}, 5e3)
const port = allocPort()
d1.announce(key, { port })
const lookup = d2.lookup(key)
lookup.on('peer', (peer) => {
clearTimeout(to)
t.equal(peer.port, port, 'peer port is as expected')
t.ok(typeof peer.host === 'string', 'peer host string is included')
t.equal(peer.local, true, 'peer was local')
t.equal(peer.referrer, null, 'peer referrer is null')
d1.destroy()
d2.destroy()
t.end()
})
})
test('announce & lookupOne', t => {
const key = crypto.randomBytes(32)
const d1 = inst()
const d2 = inst()
const to = setTimeout(() => {
t.fail('Timed out waiting for lookup')
d1.destroy()
d2.destroy()
t.end()
}, 5e3)
const port = allocPort()
d1.announce(key, { port })
d2.lookupOne(key, (err, peer) => {
clearTimeout(to)
t.error(err)
t.equal(peer.port, port, 'peer port is as expected')
t.ok(typeof peer.host === 'string', 'peer host string is included')
t.equal(peer.local, true, 'peer was local')
t.equal(peer.referrer, null, 'peer referrer is null')
d1.destroy()
d2.destroy()
t.end()
})
})
test('announce & announce with lookup = true', t => {
const key = crypto.randomBytes(32)
const d1 = inst()
const d2 = inst()
const to = setTimeout(() => {
t.fail('Timed out waiting for peers')
d1.destroy()
d2.destroy()
t.end()
}, 5e3)
const port1 = allocPort()
const ann1 = d1.announce(key, { port: port1, lookup: true })
const port2 = allocPort()
const ann2 = d2.announce(key, { port: port2, lookup: true })
var hits = 2
function onPeer (port) {
return (peer) => {
t.equal(peer.port, port, 'peer port is as expected')
t.ok(typeof peer.host === 'string', 'peer host string is included')
t.equal(peer.local, true, 'peer was local')
t.equal(peer.referrer, null, 'peer referrer is null')
if (!(--hits)) {
clearTimeout(to)
d1.destroy()
d2.destroy()
t.end()
}
}
}
ann1.on('peer', onPeer(port2))
ann2.on('peer', onPeer(port1))
})
test('flush discovery', t => {
const key = crypto.randomBytes(32)
const d1 = inst({ ephemeral: false })
const d2 = inst({ ephemeral: false })
d2.dht.bootstrap(() => {
d1.flush(function () {
d1.announce(key, { port: 1001 })
d1.announce(key, { port: 1002 })
d1.flush(function () {
const peers = []
d1.lookup(key).on('peer', function (peer) {
peers.push(peer.port)
if (peers.length === 4) {
peers.sort()
t.same(peers, [
1001,
1001,
1002,
1002
])
d1.destroy()
d2.destroy()
t.end()
}
})
})
})
})
})
test.onFinish(() => {
bootstrap.destroy()
})
function inst (opts = {}) {
return discovery(Object.assign({ bootstrap: [`127.0.0.1:${bootstrapPort}`] }, opts))
}
var nextPort = 10000
function allocPort () {
return nextPort++
}