Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 5cde2aa

Browse files
committed
test: adding relay tests
1 parent 46de6e7 commit 5cde2aa

File tree

4 files changed

+171
-30
lines changed

4 files changed

+171
-30
lines changed

test/dialer.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('dialer tests', function () {
2828
dialer.negotiateRelay.reset()
2929
})
3030

31-
it(`should negotiate a relay on all available relays`, function (done) {
31+
it(`negotiate a relay on all available relays`, function (done) {
3232
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
3333
dialer.negotiateRelay.callsFake(function (conn, dstMa, callback) {
3434
if (conn === dialer.relayPeers.get(nodes.node3.id)) {
@@ -46,7 +46,7 @@ describe('dialer tests', function () {
4646
})
4747
})
4848

49-
it(`should try all available relays and fail with error if none succeed`, function (done) {
49+
it(`try all available relays and fail with error if none succeed`, function (done) {
5050
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
5151
dialer.negotiateRelay.callsFake(function (conn, dstMa, callback) {
5252
callback('error')
@@ -83,7 +83,7 @@ describe('dialer tests', function () {
8383
callback.reset()
8484
})
8585

86-
it(`should write the correct dst addr`, function (done) {
86+
it(`write the correct dst addr`, function (done) {
8787
lp.decodeFromReader(shake, (err, msg) => {
8888
shake.write(new Buffer(String(constants.RESPONSE.SUCCESS)))
8989
expect(err).to.be.null
@@ -92,7 +92,7 @@ describe('dialer tests', function () {
9292
})
9393
})
9494

95-
it(`should fail negotiating relay`, function (done) {
95+
it(`fail negotiating relay`, function (done) {
9696
callback.callsFake((err, msg) => {
9797
expect(err).to.not.be.null
9898
expect(err).to.be.an.instanceOf(Error)

test/integration.spec.js

+26-16
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ describe('test relay', function () {
3333
],
3434
isCrypto: true,
3535
config: {
36-
relay: {
37-
hop: true
36+
Relay: {
37+
Circuit: {
38+
Enabled: true
39+
}
3840
}
3941
}
4042
},
@@ -92,13 +94,13 @@ describe('test relay', function () {
9294
utils.stopNodes(testNodes, () => done())
9395
})
9496

95-
it('should dial to a node over a relay and write values', function (done) {
97+
it('dial to a node over a relay and write values', function (done) {
9698
utils.dialAndReverse(
9799
testNodes.nodeA,
98100
testNodes.nodeB,
99101
['hello', 'hello1', 'hello2', 'hello3'],
100102
(err, result) => {
101-
if (err) return done(err)
103+
expect(err).to.be.null
102104
expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
103105
expect(result[1]).to.equal('hello1'.split('').reverse('').join(''))
104106
expect(result[2]).to.equal('hello2'.split('').reverse('').join(''))
@@ -131,8 +133,10 @@ describe('test relay', function () {
131133
],
132134
isCrypto: true,
133135
config: {
134-
relay: {
135-
hop: true
136+
Relay: {
137+
Circuit: {
138+
Enabled: true
139+
}
136140
}
137141
}
138142
},
@@ -146,8 +150,10 @@ describe('test relay', function () {
146150
],
147151
isCrypto: true,
148152
config: {
149-
relay: {
150-
hop: true
153+
Relay: {
154+
Circuit: {
155+
Enabled: true
156+
}
151157
}
152158
}
153159
},
@@ -214,7 +220,7 @@ describe('test relay', function () {
214220
utils.stopNodes(testNodes, done)
215221
})
216222

217-
it('should dial over the correct relay', function (done) {
223+
it('dial over the correct relay', function (done) {
218224
utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => {
219225
expect(err).to.be.null
220226
expect(relaySpy2.called).to.be.not.ok
@@ -227,7 +233,7 @@ describe('test relay', function () {
227233
})
228234
})
229235

230-
it('should dial over the correct relay and transport', function (done) {
236+
it('dial over the correct relay and transport', function (done) {
231237
utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => {
232238
expect(err).to.be.null
233239
expect(relaySpy1.called).to.be.not.ok
@@ -261,8 +267,10 @@ describe('test relay', function () {
261267
],
262268
isCrypto: true,
263269
config: {
264-
relay: {
265-
hop: true
270+
Relay: {
271+
Circuit: {
272+
Enabled: true
273+
}
266274
}
267275
}
268276
},
@@ -276,8 +284,10 @@ describe('test relay', function () {
276284
],
277285
isCrypto: true,
278286
config: {
279-
relay: {
280-
hop: true
287+
Relay: {
288+
Circuit: {
289+
Enabled: true
290+
}
281291
}
282292
}
283293
},
@@ -346,7 +356,7 @@ describe('test relay', function () {
346356
utils.stopNodes(testNodes, done)
347357
})
348358

349-
it('should dial over the correct chained relay addr', function (done) {
359+
it('dial over the correct chained relay addr', function (done) {
350360
utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => {
351361
expect(err).to.be.null
352362
expect(relaySpy1.called).to.be.ok
@@ -362,7 +372,7 @@ describe('test relay', function () {
362372
})
363373
})
364374

365-
it('should dial over the correct chained relay addr and transport', function (done) {
375+
it('dial over the correct chained relay addr and transport', function (done) {
366376
utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => {
367377
expect(err).to.be.null
368378
expect(relaySpy1.called).to.be.ok

test/onion-dialer.spec.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ describe('onion dialer tests', function () {
3737
dialer._onionDial.reset()
3838
})
3939

40-
it(`should split the multiaddr correctly`, function (done) {
40+
it(`split the multiaddr correctly`, function (done) {
4141
dialer._onionDial.callsArgWith(1, null, new Connection())
4242

4343
const chainedAddr = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}/p2p-circuit` +
4444
`/ip4/0.0.0.0/tcp/9031/ipfs/${nodes.node1.id}/p2p-circuit/`)
4545

4646
dialer.dial(chainedAddr + `/ipfs/${nodes.node3.id}`, (err, conn) => {
47-
if (err) return done(err)
48-
47+
expect(err).to.be.null
4948
expect(dialer._onionDial.calledOnce).to.be.ok
5049
expect(dialer._onionDial.calledWith([
5150
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`,
@@ -56,7 +55,7 @@ describe('onion dialer tests', function () {
5655
})
5756
})
5857

59-
it(`should not dial to itself`, function (done) {
58+
it(`not dial to itself`, function (done) {
6059
const chainedAddr = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}/p2p-circuit` +
6160
`/ip4/0.0.0.0/tcp/9031/ipfs/${nodes.node1.id}/p2p-circuit/`)
6261

@@ -97,7 +96,7 @@ describe('onion dialer tests', function () {
9796
dialer.dialRelay.reset()
9897
})
9998

100-
it(`should dial chained multiaddr correctly`, function (done) {
99+
it(`dial chained multiaddr correctly`, function (done) {
101100
const chainedAddrs = [
102101
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`,
103102
`/ip4/0.0.0.0/tcp/9031/ipfs/${nodes.node1.id}`,
@@ -109,8 +108,7 @@ describe('onion dialer tests', function () {
109108
const addr3 = chainedAddrs[2]
110109

111110
dialer._onionDial(chainedAddrs, (err, conn) => {
112-
if (err) return done(err)
113-
111+
expect(err).to.be.null
114112
expect(dialer.dialRelay.calledWith(util.peerInfoFromMa(addr1))).to.be.ok
115113
expect(dialer.dialPeer.calledWith(addr2)).to.be.ok
116114
expect(dialer._handshake.calledWith(util.peerInfoFromMa(addr2))).to.be.ok
@@ -145,10 +143,9 @@ describe('onion dialer tests', function () {
145143
dialer.dialRelay.reset()
146144
})
147145

148-
it(`should dial chained multiaddr correctly`, function (done) {
146+
it(`dial chained multiaddr correctly`, function (done) {
149147
dialer._onionDial([`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`], (err, conn) => {
150-
if (err) return done(err)
151-
148+
expect(err).to.be.null
152149
expect(dialer.dialPeer.calledWith(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`)).to.be.ok
153150
done()
154151
})

test/relay.spec.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const Relay = require('../src/circuit/relay')
5+
const Dialer = require('../src/circuit/dialer')
6+
const nodes = require('./fixtures/nodes')
7+
const Connection = require('interface-connection').Connection
8+
const multiaddr = require('multiaddr')
9+
const constants = require('../src/circuit/constants')
10+
const handshake = require('pull-handshake')
11+
const multicodec = require('../src/multicodec')
12+
const waterfall = require('async/waterfall')
13+
const PeerInfo = require('peer-info')
14+
const PeerId = require('peer-id')
15+
16+
const sinon = require('sinon')
17+
const expect = require('chai').expect
18+
19+
describe('relay', function () {
20+
describe(`handle circuit requests`, function () {
21+
const relay = sinon.createStubInstance(Relay)
22+
const dialer = sinon.createStubInstance(Dialer)
23+
24+
let swarm
25+
let fromConn
26+
let toConn
27+
let stream
28+
let shake
29+
let handlerSpy
30+
31+
beforeEach(function (done) {
32+
stream = handshake({timeout: 1000 * 60})
33+
shake = stream.handshake
34+
fromConn = new Connection(stream)
35+
toConn = new Connection(shake.rest())
36+
37+
waterfall([
38+
(cb) => PeerId.createFromJSON(nodes.node4, cb),
39+
(peerId, cb) => PeerInfo.create(peerId, cb),
40+
(peer, cb) => {
41+
swarm = {
42+
_peerInfo: peer,
43+
handle: sinon.spy((proto, h) => {
44+
handlerSpy = sinon.spy(h)
45+
}),
46+
conns: {
47+
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection()
48+
}
49+
}
50+
cb()
51+
}
52+
], () => {
53+
relay.mount.callThrough()
54+
relay.emit.callThrough()
55+
relay.on.callThrough()
56+
relay.mount(swarm) // mount the swarm
57+
relay.circuit.callsArg(2, null, toConn)
58+
59+
dialer.negotiateRelay.callThrough()
60+
done()
61+
})
62+
})
63+
64+
afterEach(() => {
65+
relay.mount.reset()
66+
relay.emit.reset()
67+
relay.on.reset()
68+
relay.circuit.reset()
69+
dialer.negotiateRelay.reset()
70+
})
71+
72+
it(`handle a valid circuit request`, function (done) {
73+
relay.on('circuit:success', () => {
74+
expect(relay.circuit.calledWith(sinon.match.any, dstMa)).to.be.ok
75+
done()
76+
})
77+
78+
let dstMa = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/`)
79+
dialer.negotiateRelay(fromConn, dstMa, () => {})
80+
81+
handlerSpy(multicodec.hop, toConn)
82+
})
83+
84+
// it(`fail dialing to invalid multiaddr`, function () {
85+
// // TODO: implement without relying on negotiateRelay
86+
// })
87+
88+
it(`not dial to self`, function (done) {
89+
let dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
90+
dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => {
91+
expect(err).to.not.be.null
92+
expect(err).to.be.an.instanceOf(Error)
93+
expect(err.message)
94+
.to.equal(`Got ${constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF} error code trying to dial over relay`)
95+
expect(newConn).to.be.undefined
96+
done()
97+
})
98+
99+
handlerSpy(multicodec.hop, toConn)
100+
})
101+
102+
it(`fail on address exeding 1024 bytes`, function (done) {
103+
let dstMa = multiaddr(
104+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
105+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
106+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
107+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
108+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
109+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
110+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
111+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
112+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
113+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
114+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
115+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
116+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
117+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
118+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
119+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
120+
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit`)
121+
122+
dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => {
123+
expect(err).to.not.be.null
124+
expect(err).to.be.an.instanceOf(Error)
125+
expect(err.message)
126+
.to.equal(`Got ${constants.RESPONSE.HOP.DST_ADDR_TOO_LONG} error code trying to dial over relay`)
127+
expect(newConn).to.be.undefined
128+
done()
129+
})
130+
131+
handlerSpy(multicodec.hop, toConn)
132+
})
133+
})
134+
})

0 commit comments

Comments
 (0)