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

Commit

Permalink
test: adding relay tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Apr 9, 2017
1 parent 78e82e1 commit 70ef4a6
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 30 deletions.
8 changes: 4 additions & 4 deletions test/dialer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('dialer tests', function () {
dialer.negotiateRelay.reset()
})

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

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

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

it(`should fail negotiating relay`, function (done) {
it(`fail negotiating relay`, function (done) {
callback.callsFake((err, msg) => {
expect(err).to.not.be.null
expect(err).to.be.an.instanceOf(Error)
Expand Down
42 changes: 26 additions & 16 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ describe('test relay', function () {
],
isCrypto: true,
config: {
relay: {
hop: true
Relay: {
Circuit: {
Enabled: true
}
}
}
},
Expand Down Expand Up @@ -92,13 +94,13 @@ describe('test relay', function () {
utils.stopNodes(testNodes, () => done())
})

it('should dial to a node over a relay and write values', function (done) {
it('dial to a node over a relay and write values', function (done) {
utils.dialAndReverse(
testNodes.nodeA,
testNodes.nodeB,
['hello', 'hello1', 'hello2', 'hello3'],
(err, result) => {
if (err) return done(err)
expect(err).to.be.null
expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
expect(result[1]).to.equal('hello1'.split('').reverse('').join(''))
expect(result[2]).to.equal('hello2'.split('').reverse('').join(''))
Expand Down Expand Up @@ -131,8 +133,10 @@ describe('test relay', function () {
],
isCrypto: true,
config: {
relay: {
hop: true
Relay: {
Circuit: {
Enabled: true
}
}
}
},
Expand All @@ -146,8 +150,10 @@ describe('test relay', function () {
],
isCrypto: true,
config: {
relay: {
hop: true
Relay: {
Circuit: {
Enabled: true
}
}
}
},
Expand Down Expand Up @@ -214,7 +220,7 @@ describe('test relay', function () {
utils.stopNodes(testNodes, done)
})

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

it('should dial over the correct relay and transport', function (done) {
it('dial over the correct relay and transport', function (done) {
utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => {
expect(err).to.be.null
expect(relaySpy1.called).to.be.not.ok
Expand Down Expand Up @@ -261,8 +267,10 @@ describe('test relay', function () {
],
isCrypto: true,
config: {
relay: {
hop: true
Relay: {
Circuit: {
Enabled: true
}
}
}
},
Expand All @@ -276,8 +284,10 @@ describe('test relay', function () {
],
isCrypto: true,
config: {
relay: {
hop: true
Relay: {
Circuit: {
Enabled: true
}
}
}
},
Expand Down Expand Up @@ -346,7 +356,7 @@ describe('test relay', function () {
utils.stopNodes(testNodes, done)
})

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

it('should dial over the correct chained relay addr and transport', function (done) {
it('dial over the correct chained relay addr and transport', function (done) {
utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => {
expect(err).to.be.null
expect(relaySpy1.called).to.be.ok
Expand Down
17 changes: 7 additions & 10 deletions test/onion-dialer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ describe('onion dialer tests', function () {
dialer._onionDial.reset()
})

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

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

dialer.dial(chainedAddr + `/ipfs/${nodes.node3.id}`, (err, conn) => {
if (err) return done(err)

expect(err).to.be.null
expect(dialer._onionDial.calledOnce).to.be.ok
expect(dialer._onionDial.calledWith([
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`,
Expand All @@ -56,7 +55,7 @@ describe('onion dialer tests', function () {
})
})

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

Expand Down Expand Up @@ -97,7 +96,7 @@ describe('onion dialer tests', function () {
dialer.dialRelay.reset()
})

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

dialer._onionDial(chainedAddrs, (err, conn) => {
if (err) return done(err)

expect(err).to.be.null
expect(dialer.dialRelay.calledWith(util.peerInfoFromMa(addr1))).to.be.ok
expect(dialer.dialPeer.calledWith(addr2)).to.be.ok
expect(dialer._handshake.calledWith(util.peerInfoFromMa(addr2))).to.be.ok
Expand Down Expand Up @@ -145,10 +143,9 @@ describe('onion dialer tests', function () {
dialer.dialRelay.reset()
})

it(`should dial chained multiaddr correctly`, function (done) {
it(`dial chained multiaddr correctly`, function (done) {
dialer._onionDial([`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`], (err, conn) => {
if (err) return done(err)

expect(err).to.be.null
expect(dialer.dialPeer.calledWith(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodes.node2.id}`)).to.be.ok
done()
})
Expand Down
134 changes: 134 additions & 0 deletions test/relay.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* eslint-env mocha */
'use strict'

const Relay = require('../src/circuit/relay')
const Dialer = require('../src/circuit/dialer')
const nodes = require('./fixtures/nodes')
const Connection = require('interface-connection').Connection
const multiaddr = require('multiaddr')
const constants = require('../src/circuit/constants')
const handshake = require('pull-handshake')
const multicodec = require('../src/multicodec')
const waterfall = require('async/waterfall')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')

const sinon = require('sinon')
const expect = require('chai').expect

describe('relay', function () {
describe(`handle circuit requests`, function () {
const relay = sinon.createStubInstance(Relay)
const dialer = sinon.createStubInstance(Dialer)

let swarm
let fromConn
let toConn
let stream
let shake
let handlerSpy

beforeEach(function (done) {
stream = handshake({timeout: 1000 * 60})
shake = stream.handshake
fromConn = new Connection(stream)
toConn = new Connection(shake.rest())

waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
swarm = {
_peerInfo: peer,
handle: sinon.spy((proto, h) => {
handlerSpy = sinon.spy(h)
}),
conns: {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection()
}
}
cb()
}
], () => {
relay.mount.callThrough()
relay.emit.callThrough()
relay.on.callThrough()
relay.mount(swarm) // mount the swarm
relay.circuit.callsArg(2, null, toConn)

dialer.negotiateRelay.callThrough()
done()
})
})

afterEach(() => {
relay.mount.reset()
relay.emit.reset()
relay.on.reset()
relay.circuit.reset()
dialer.negotiateRelay.reset()
})

it(`handle a valid circuit request`, function (done) {
relay.on('circuit:success', () => {
expect(relay.circuit.calledWith(sinon.match.any, dstMa)).to.be.ok
done()
})

let dstMa = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/`)
dialer.negotiateRelay(fromConn, dstMa, () => {})

handlerSpy(multicodec.hop, toConn)
})

// it(`fail dialing to invalid multiaddr`, function () {
// // TODO: implement without relying on negotiateRelay
// })

it(`not dial to self`, function (done) {
let dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => {
expect(err).to.not.be.null
expect(err).to.be.an.instanceOf(Error)
expect(err.message)
.to.equal(`Got ${constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF} error code trying to dial over relay`)
expect(newConn).to.be.undefined
done()
})

handlerSpy(multicodec.hop, toConn)
})

it(`fail on address exeding 1024 bytes`, function (done) {
let dstMa = multiaddr(
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` +
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit`)

dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => {
expect(err).to.not.be.null
expect(err).to.be.an.instanceOf(Error)
expect(err.message)
.to.equal(`Got ${constants.RESPONSE.HOP.DST_ADDR_TOO_LONG} error code trying to dial over relay`)
expect(newConn).to.be.undefined
done()
})

handlerSpy(multicodec.hop, toConn)
})
})
})

0 comments on commit 70ef4a6

Please sign in to comment.