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

Commit

Permalink
test: add sample network circuit relay tests (#275)
Browse files Browse the repository at this point in the history
* test: add sample network circuit relay tests

* test: use ephemeral ports
  • Loading branch information
jacobheun committed Aug 15, 2018
1 parent aa7f2e8 commit 30310f8
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 6 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -37,7 +37,7 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^15.0.0",
"aegir": "^15.1.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"libp2p-mplex": "~0.7.0",
Expand All @@ -48,6 +48,7 @@
"libp2p-webrtc-star": "~0.14.0",
"libp2p-websockets": "~0.12.0",
"peer-book": "~0.7.0",
"portfinder": "^1.0.16",
"sinon": "^5.0.2",
"webrtcsupport": "^2.2.0"
},
Expand All @@ -58,7 +59,7 @@
"hashlru": "^2.2.1",
"interface-connection": "~0.3.2",
"ip-address": "^5.8.9",
"libp2p-circuit": "~0.2.0",
"libp2p-circuit": "~0.2.1",
"libp2p-identify": "~0.7.2",
"lodash.includes": "^4.3.0",
"moving-average": "^1.0.0",
Expand Down
209 changes: 205 additions & 4 deletions test/circuit-relay.node.js
Expand Up @@ -7,19 +7,21 @@ const expect = chai.expect
chai.use(dirtyChai)

const sinon = require('sinon')

const once = require('once')
const parallel = require('async/parallel')
const series = require('async/series')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const multiplex = require('libp2p-mplex')
const PeerBook = require('peer-book')
const getPorts = require('portfinder').getPorts

const utils = require('./utils')
const createInfos = utils.createInfos
// const tryEcho = utils.tryEcho
const Swarm = require('../src')

describe(`circuit`, function () {
let swarmA // TCP
let swarmA // TCP and WS
let swarmB // WS
let swarmC // no transports
let dialSpyA
Expand Down Expand Up @@ -76,7 +78,7 @@ describe(`circuit`, function () {
expect(swarmB.transports.Circuit).to.exist()
})

it('add /p2p-curcuit addrs on start', (done) => {
it('add /p2p-circuit addrs on start', (done) => {
parallel([
(cb) => swarmA.start(cb),
(cb) => swarmB.start(cb)
Expand Down Expand Up @@ -136,4 +138,203 @@ describe(`circuit`, function () {
done()
})
})

describe('in a basic network', () => {
// Create 5 nodes
// Make node 1 act as a Bootstrap node and relay (speak tcp and ws)
// Make nodes 2 & 3 speak tcp only
// Make nodes 4 & 5 speak WS only
// Have all nodes dial node 1
// Each node should get the peers of node 1
// Attempt to dial to each peer
let bootstrapSwitch
let tcpSwitch1
let tcpSwitch2
let wsSwitch1
let wsSwitch2
let bootstrapPeer
let tcpPeer1
let tcpPeer2
let wsPeer1
let wsPeer2

before((done) => createInfos(5, (err, infos) => {
expect(err).to.not.exist()

getPorts(6, (err, ports) => {
expect(err).to.not.exist()

bootstrapPeer = infos[0]
tcpPeer1 = infos[1]
tcpPeer2 = infos[2]
wsPeer1 = infos[3]
wsPeer2 = infos[4]

// Setup the addresses of our nodes
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
tcpPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
tcpPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
wsPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
wsPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)

// Setup the bootstrap node with the minimum needed for being a relay
bootstrapSwitch = new Swarm(bootstrapPeer, new PeerBook())
bootstrapSwitch.connection.addStreamMuxer(multiplex)
bootstrapSwitch.connection.reuse()
bootstrapSwitch.connection.enableCircuitRelay({
enabled: true,
// The relay needs to allow hopping
hop: {
enabled: true
}
})

// Setup the tcp1 node with the minimum needed for dialing via a relay
tcpSwitch1 = new Swarm(tcpPeer1, new PeerBook())
tcpSwitch1.connection.addStreamMuxer(multiplex)
tcpSwitch1.connection.reuse()
tcpSwitch1.connection.enableCircuitRelay({
enabled: true
})

// Setup tcp2 node to not be able to dial/listen over relay
tcpSwitch2 = new Swarm(tcpPeer2, new PeerBook())
tcpSwitch2.connection.reuse()
tcpSwitch2.connection.addStreamMuxer(multiplex)

// Setup the ws1 node with the minimum needed for dialing via a relay
wsSwitch1 = new Swarm(wsPeer1, new PeerBook())
wsSwitch1.connection.addStreamMuxer(multiplex)
wsSwitch1.connection.reuse()
wsSwitch1.connection.enableCircuitRelay({
enabled: true
})

// Setup the ws2 node with the minimum needed for dialing via a relay
wsSwitch2 = new Swarm(wsPeer2, new PeerBook())
wsSwitch2.connection.addStreamMuxer(multiplex)
wsSwitch2.connection.reuse()
wsSwitch2.connection.enableCircuitRelay({
enabled: true
})

bootstrapSwitch.transport.add('tcp', new TCP())
bootstrapSwitch.transport.add('ws', new WS())
tcpSwitch1.transport.add('tcp', new TCP())
tcpSwitch2.transport.add('tcp', new TCP())
wsSwitch1.transport.add('ws', new WS())
wsSwitch2.transport.add('ws', new WS())

series([
// start the nodes
(cb) => {
parallel([
(cb) => bootstrapSwitch.start(cb),
(cb) => tcpSwitch1.start(cb),
(cb) => tcpSwitch2.start(cb),
(cb) => wsSwitch1.start(cb),
(cb) => wsSwitch2.start(cb)
], cb)
},
// dial to the bootstrap node
(cb) => {
parallel([
(cb) => tcpSwitch1.dial(bootstrapPeer, cb),
(cb) => tcpSwitch2.dial(bootstrapPeer, cb),
(cb) => wsSwitch1.dial(bootstrapPeer, cb),
(cb) => wsSwitch2.dial(bootstrapPeer, cb)
], cb)
}
], (err) => {
if (err) return done(err)

done = once(done)
// Wait for everyone to connect, before we try relaying
bootstrapSwitch.on('peer-mux-established', () => {
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
done()
}
})
})
})
}))

after((done) => {
parallel([
(cb) => bootstrapSwitch.stop(cb),
(cb) => tcpSwitch1.stop(cb),
(cb) => tcpSwitch2.stop(cb),
(cb) => wsSwitch1.stop(cb),
(cb) => wsSwitch2.stop(cb)
], done)
})

it('should be able to dial tcp -> tcp', (done) => {
tcpSwitch2.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(tcpPeer1.id.toB58String())
done()
})
tcpSwitch1.dial(tcpPeer2, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})

it('should be able to dial tcp -> ws over relay', (done) => {
wsSwitch1.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(tcpPeer1.id.toB58String())
done()
})
tcpSwitch1.dial(wsPeer1, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})

it('should be able to dial ws -> ws', (done) => {
wsSwitch2.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(wsPeer1.id.toB58String())
done()
})
wsSwitch1.dial(wsPeer2, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})

it('should be able to dial ws -> tcp over relay', (done) => {
tcpSwitch1.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(wsPeer2.id.toB58String())
expect(Object.keys(tcpSwitch1._peerBook.getAll())).to.include(wsPeer2.id.toB58String())
done()
})
wsSwitch2.dial(tcpPeer1, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})

it('shouldnt be able to dial to a non relay node', (done) => {
// tcpPeer2 doesnt have relay enabled
wsSwitch1.dial(tcpPeer2, (err, connection) => {
expect(err).to.exist()
expect(connection).to.not.exist()
done()
})
})

it('shouldnt be able to dial from a non relay node', (done) => {
// tcpSwitch2 doesnt have relay enabled
tcpSwitch2.dial(wsPeer1, (err, connection) => {
expect(err).to.exist()
expect(connection).to.not.exist()
done()
})
})
})
})

0 comments on commit 30310f8

Please sign in to comment.