forked from chr15m/bugout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
348 lines (288 loc) · 10.8 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
var test = require("tape");
var WebTorrent = require("webtorrent");
var Bugout = require("./index.js");
var wtest = new WebTorrent({dht: false, tracker: false});
var wtest2 = new WebTorrent({dht: false, tracker: false});
var wtest3 = new WebTorrent({dht: false, tracker: false});
// PRS WELCOME
//
// TODO: test RPC with each type of argument combination
// TODO: test bad RPC with unknown nonce
// TODO: more bad parameters & calls
// TODO: check mutated keys yield cryptographic errors
// TODO: test malformed JSON packets
test.onFinish(function() {
wtest.destroy();
wtest2.destroy();
wtest3.destroy();
});
test('Instantiation', function (t) {
t.plan(10);
var b1 = new Bugout({seed: "BohNtZ24TrgMwZTLx9VDKtcZARNVuCt5tnecAAxYtTBC8pC61uGN", wt: wtest});
t.equal(b1.identifier, "bYSkTy24xXJj6dWe79ZAQXKJZrn2n983SQ", "server identifier");
t.equal(b1.pk, "CXENBY9X3x5TN1yjRyu1U1WkGuujuVBNiqxA16oAYbFo", "server public key");
t.equal(b1.identifier, Bugout.address("CXENBY9X3x5TN1yjRyu1U1WkGuujuVBNiqxA16oAYbFo"), "server address from pk");
t.equal(b1.identifier, Bugout.address(b1.keyPair.publicKey), "server address from pk array");
b1.torrent.on("infoHash", function() {
t.equal(b1.torrent.infoHash, "28d878040b7d2f5215409373b415fb99bc0e6d88", "server infoHash");
});
t.throws(function() {
var b2 = new Bugout({seed: "BohNtZ24TrgMwZTLx9VDLtcZARNVuCt5tnecAAxYtTBC8pC61uGN", wt: wtest});
}, "Error: Invalid checksum", "invalid seed checksum");
var b3 = new Bugout("bMuHpwCxcD5vhC5u7VKuajYu5RU7FUnaGJ", {wt: wtest});
t.equal(b3.identifier, "bMuHpwCxcD5vhC5u7VKuajYu5RU7FUnaGJ", "client identifier");
b3.torrent.on("infoHash", function() {
t.equal(b3.torrent.infoHash, "d96fe55834a62d86e48573c132345c01a38f5ffd", "client infoHash");
});
var b4 = new Bugout({seed: Bugout.encodeseed(Array(32).fill(0x23)), wt: wtest});
console.log(b4.address());
t.equal(b4.identifier, "bYwqSagZb5n42M9qXSw2uu3Cpxg9JhZcnd", "encode seed client identifier");
b4.torrent.on("infoHash", function() {
console.log(b4.torrent.infoHash);
t.equal(b4.torrent.infoHash, "5486696a87e91c6c7fcfc6279c9b08709c7aa61f", "client infoHash");
});
});
test("Connectivity events", function(t) {
t.plan(7);
var bs = new Bugout({wt: wtest});
var bc = new Bugout(bs.address(), {wt: wtest2});
var clast = null;
var times = 2;
function connectioncounter(c) {
t.notEqual(clast, c, "connection count");
times -= 1;
clast = c;
if (!times) {
bs.removeListener("connections", connectioncounter);
}
}
bs.on("connections", connectioncounter);
bs.connections();
bs.connections();
bc.on("wireseen", function(c) {
t.equal(c, 1, "client wire count");
});
bs.on("wireseen", function(c) {
t.equal(c, 1, "server wire count");
});
bc.on("seen", function(address) {
t.equal(address, bs.address(), "client remote address");
});
bs.on("seen", function(address) {
t.equal(address, bc.address(), "server remote address");
});
bc.on("server", function(address) {
t.equal(address, bs.address(), "server seen correct address");
});
// connect the two clients together
bs.torrent.on("infoHash", function() {
bs.torrent.addPeer("127.0.0.1:" + bc.wt.address().port);
});
});
test("RPC and message passing", function(t) {
t.plan(7);
var bs = new Bugout({wt: wtest});
var bc = new Bugout(bs.address(), {wt: wtest2});
var msg = {"Hello": "world"};
bs.register("ping", function(address, args, cb) {
t.equal(address, bc.address(), "client rpc address");
args["pong"] = true;
cb(args);
});
bs.register("rpc", console.log.bind(null, "rpc"));
bs.on("seen", function(address) {
t.equal(address, bc.address(), "server seen client address");
bs.send(address, {"Hello": "world"});
});
bc.on("server", function(address) {
t.equal(address, bs.address(), "client seen server address");
bc.rpc("ping", msg, function(response) {
t.equal(response.Hello, "world", "RPC server response check value");
t.ok(response.pong, "RPC server response check pong");
});
});
bc.on("message", function(address, message) {
t.equal(address, bs.address(), "server message remote address");
t.deepEqual(message, msg, "server message content check");
});
// connect the two clients together
bs.torrent.on("infoHash", function() {
bs.torrent.addPeer("127.0.0.1:" + bc.wt.address().port);
});
});
test("3 party incomplete graph gossip test", function(t) {
t.plan(10);
var bs = new Bugout({wt: wtest});
var bc1 = new Bugout(bs.address(), {wt: wtest2});
var bc2 = new Bugout(bs.address(), {wt: wtest3});
var msg = {"Foo": "bar", "meaning": 42};
bs.register("ping", function(address, args, cb) {
t.equal(address, bc2.address(), "client rpc address");
args["pong"] = true;
cb(args);
});
bs.on("rpc", function(address, call, args) {
// check rpc was from client2
t.equal(bc2.address(), address, "server check client2 was rpc sender");
});
// this should never fire
bc1.on("rpc", console.log.bind(null, "client1 rpc"));
bc2.on("server", function(address) {
t.equal(address, bs.address(), "client2 seen server address");
// verify we're only acutally connected to other client
// (getting messages by gossip)
t.equal(bc2.torrent.wires.length, 1, "client2 only one wire");
t.equal(bc2.address(bc2.torrent.wires[0].peerExtendedHandshake.pk.toString()), bc1.address(), "client2 is connected to client1");
bc2.rpc("ping", msg, function(response) {
t.equal(response.Foo, "bar", "RPC server response check value 1");
t.equal(response.meaning, 42, "RPC server response check value 2");
t.ok(response.pong, "RPC server response check pong");
});
});
// connect first client to server
bs.torrent.on("infoHash", function() {
bs.torrent.addPeer("127.0.0.1:" + bc1.wt.address().port);
bs.once("seen", function(address) {
t.equal(address, bc1.address(), "server seen client1 address");
// bs.send(address, msg);
// check the second client's connection
bs.once("seen", function(address) {
t.equal(address, bc2.address(), "server seen client2 address");
});
// connect second client to first
setTimeout(function() {
bc1.torrent.addPeer("127.0.0.1:" + bc2.wt.address().port);
}, 100);
});
});
});
test("heartbeat seen and timeout", function(t) {
t.plan(20);
var interval = 100;
var timeout = 1000;
var bs = new Bugout({wt: wtest, heartbeat: interval, timeout: timeout});
var bc1 = new Bugout(bs.address(), {wt: wtest2, heartbeat: interval, timeout: timeout});
var bc2 = new Bugout(bs.address(), {wt: wtest3, heartbeat: interval, timeout: timeout});
/*console.log(" ->>> bs:", bs.address());
console.log(" ->>> bc1:", bc1.address());
console.log(" ->>> bc2:", bc2.address());*/
/*bs.on("timeout", console.log.bind(null, "-> bs timeout"));
bc2.on("timeout", console.log.bind(null, "-> bc2 timeout"));
bs.on("left", console.log.bind(null, "-> bs left"));
bc2.on("left", console.log.bind(null, "-> bc2 left"));
bs.on("ping", console.log.bind(null, "bs ping"));
bc1.on("ping", console.log.bind(null, "bc1 ping"));
bc2.on("ping", console.log.bind(null, "bc2 ping"));*/
var pingers = [
[bc2.address(), bc1.address(), bs],
[bs.address(), bc2.address(), bc1],
[bs.address(), bc1.address(), bc2],
];
// ensure each client receives at least one ping from each other client
pingers.map(function(pingtest) {
var src = pingtest.pop();
var expected = {};
for (var p=0; p<pingtest.length; p++) {
expected[pingtest[p]] = true;
}
src.on("ping", function(address) {
if (expected[address]) {
t.pass("ping from " + address);
delete expected[address];
}
});
});
// ensure server sees client 2 timeout
bs.on("timeout", function(address) {
// ignore bc1 timeout
if (address == bc2.address()) {
t.pass("server saw client2 timeout");
}
});
// ensure client2 sees server timeout
bc2.on("timeout", function(address) {
// ignore bc1 timeout
if (address == bs.address()) {
t.pass("client2 saw server timeout");
}
});
var leavers = [
[bc2.address(), bc1.address(), bs],
[bs.address(), bc1.address(), bc2],
];
// bs and bc2 should see the other two leave each once
leavers.map(function(leavetest) {
var src = leavetest.pop();
var expected = {};
for (var e=0; e<leavetest.length; e++) {
expected[leavetest[e]] = true;
}
src.on("left", function(address) {
if (expected[address]) {
t.pass(address + " left");
delete expected[address];
} else {
t.fail(address + " left unexpectedly");
}
// clean up once this test is done
if (Object.keys(expected).length == 0) {
src.destroy();
}
});
});
var msg = {"Goober": "dougal", "question": 42};
bc2.on("server", function(address) {
t.equal(address, bs.address(), "client2 seen server address");
// verify we're only acutally connected to other client
// (getting messages by gossip)
t.equal(bc2.torrent.wires.length, 1, "client2 only one wire");
t.equal(bc2.address(bc2.torrent.wires[0].peerExtendedHandshake.pk.toString()), bc1.address(), "client2 is connected to client1");
bs.on("wireleft", function() {
t.equal(bs.torrent.wires.length, 0, "server wires to zero");
});
bc2.on("wireleft", function() {
t.equal(bc2.torrent.wires.length, 0, "client2 wires to zero");
});
// disconnect bc2
setTimeout(function() {
bc1.destroy(function() {
t.pass("bc1 destroyed");
});
}, 500);
});
// connect first client to server
bs.torrent.on("infoHash", function() {
bs.torrent.addPeer("127.0.0.1:" + bc1.wt.address().port);
bs.once("seen", function(address) {
t.equal(address, bc1.address(), "server seen client1 address");
// bs.send(address, msg);
// check the second client's connection
bs.once("seen", function(address) {
t.equal(address, bc2.address(), "server seen client2 address");
});
// connect second client to first
setTimeout(function() {
bc1.torrent.addPeer("127.0.0.1:" + bc2.wt.address().port);
}, 100);
});
});
});
test("RPC response handles async callback", {"timeout": 3000}, function(t) {
t.plan(1);
var bs = new Bugout({wt: wtest});
var bc = new Bugout(bs.address(), {wt: wtest2});
bs.register("ping", function(address, args, cb) {
args["pong"] = true;
setTimeout(function() {
cb(args);
}, 1000);
});
bc.on("server", function(address) {
bc.rpc("ping", {}, function(response) {
t.ok(response.pong, "RPC server response check async pong");
});
});
bs.torrent.on("infoHash", function() {
bs.torrent.addPeer("127.0.0.1:" + bc.wt.address().port);
});
});