This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
162 lines (139 loc) · 3.63 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
var Hub = require('signalhub')
var Server = require('signalhub/server')
var Swarm = require('.')
var test = require('flip-tape') // eslint-disable-line
var wrtc = require('electron-webrtc')()
var server = new Server()
test.onFinish(function () {
server.close()
wrtc.close()
process.exit(0)
})
process.on('SIGINT', function () {
server.close()
wrtc.close()
process.exit(1)
})
server.listen(9000, function () {
'connect using key from after instantiation'.test(function (t) {
t.plan(8)
var hub1 = new Hub('test', 'localhost:9000')
var hub2 = new Hub('test', 'localhost:9000')
var key = Swarm.createKey()
var swarm1 = new Swarm(hub1, {
keys: [key],
wrtc
})
var swarm2 = new Swarm(hub2, {
wrtc
})
greetAndClose(swarm1, swarm2)
setTimeout(x => {
t.comment('Attaching shared key')
swarm2.keys.push(swarm1.keys[0])
}, 1000) // allow for a couple of failed tries
})
'connect using key discovery'.test(function (t) {
t.plan(8)
var hub1 = new Hub('test', 'localhost:9000')
var hub2 = new Hub('test', 'localhost:9000')
var key1 = Swarm.createKey()
var key2 = Swarm.createKey()
var swarm1 = new Swarm(hub1, {
keys: [key1, key2],
wrtc
})
var swarm2 = new Swarm(hub2, {
keys: [key1],
wrtc
})
greetAndClose(swarm1, swarm2)
})
'connect using a automatically created key'.test(function (t) {
t.plan(8)
var hub1 = new Hub('test', 'localhost:9000')
var hub2 = new Hub('test', 'localhost:9000')
var swarm1 = new Swarm(hub1, {
wrtc
})
var swarm2 = new Swarm(hub2, {
keys: [swarm1.keys[0]],
wrtc
})
greetAndClose(swarm1, swarm2)
})
'connect using a manually created key'.test(function (t) {
t.plan(8)
var hub1 = new Hub('test', 'localhost:9000')
var hub2 = new Hub('test', 'localhost:9000')
var key = Swarm.createKey()
var swarm1 = new Swarm(hub1, {
keys: [key],
wrtc
})
var swarm2 = new Swarm(hub2, {
keys: [key],
wrtc
})
greetAndClose(swarm1, swarm2)
})
'retrieve shared key from `simple-peer` instance'.test(function (t) {
t.plan(1)
var hub1 = new Hub('test', 'localhost:9000')
var hub2 = new Hub('test', 'localhost:9000')
var keys = [Swarm.createKey()]
var swarm1 = new Swarm(hub1, {
keys,
wrtc
})
var swarm2 = new Swarm(hub2, {
keys,
wrtc
})
swarm1.on('peer', peer => {
t.equal(peer.sharedKey, keys[0])
peer.destroy() // otherwise `TypeError`
swarm1.close()
swarm2.close()
console.log('done')
})
})
})
function greetAndClose (swarm1, swarm2) {
// includes 8 tests
var hello = 'hello'
var goodbye = 'goodbye'
var peerIds = {}
swarm1.on('peer', function (peer, id) {
'connected to peer from swarm2'.pass()
peerIds.swarm2 = id
peer.send(hello)
peer.on('data', function (data) {
'goodbye received'.equal(data.toString(), goodbye)
swarm1.close(function () {
'swarm1 closed'.pass()
})
})
})
swarm2.on('peer', function (peer, id) {
'connected to peer from swarm1'.pass()
peerIds.swarm1 = id
peer.on('data', function (data) {
'hello received'.equal(data.toString(), hello)
peer.send(goodbye)
swarm2.close(function () {
'swarm2 closed'.pass()
})
})
})
swarm1.on('disconnect', function (peer, id) {
if (id === peerIds.swarm2) {
'connection to peer from swarm2 lost'.pass()
}
})
swarm2.on('disconnect', function (peer, id) {
if (id === peerIds.swarm1) {
'connection to peer from swarm1 lost'.pass()
}
})
}