Skip to content

Commit

Permalink
feat: add support for specifying noise and secio
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Feb 9, 2020
1 parent dd8cdb5 commit c77458b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cross-env": "^7.0.0",
"dirty-chai": "^2.0.1",
"go-libp2p-dep": "~0.5.0",
"libp2p-daemon": "^0.3.0",
"libp2p-daemon": "libp2p/js-libp2p-daemon#feat/noise",
"libp2p-daemon-client": "^0.3.0",
"multiaddr": "^7.2.1",
"rimraf": "^3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@ class Daemon {
if (this._type === 'go') {
execOptions = ['-listen', addr]

options.secio && execOptions.push('-secio')
options.noise && execOptions.push('-noise')
options.dht && execOptions.push('-dht')
options.pubsub && execOptions.push('-pubsub')
options.pubsubRouter && execOptions.push('-pubsubRouter', options.pubsubRouter)
} else {
execOptions = ['--listen', addr]

options.secio && execOptions.push('--secio')
options.noise && execOptions.push('--noise')
options.dht && execOptions.push('--dht')
options.pubsub && execOptions.push('--pubsub')
options.pubsubRouter && execOptions.push('--pubsubRouter', options.pubsubRouter)
Expand Down
89 changes: 45 additions & 44 deletions test/connect/js2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,50 @@ const expect = chai.expect

const spawnDaemons = require('../utils/spawnDaemons')

describe('connect', () => {
let daemons

// Start Daemons
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, 'go')
})

// Stop daemons
after(async function () {
await Promise.all(
daemons.map((daemon) => daemon.stop())
)
})

it('js peer to js peer', async function () {
this.timeout(10 * 1000)

const identify1 = await daemons[0].client.identify()
const identify2 = await daemons[1].client.identify()

// verify connected peers
const knownPeersBeforeConnect1 = await daemons[0].client.listPeers()
expect(knownPeersBeforeConnect1).to.have.lengthOf(0)

const knownPeersBeforeConnect2 = await daemons[1].client.listPeers()
expect(knownPeersBeforeConnect2).to.have.lengthOf(0)

// connect peers
await daemons[1].client.connect(identify1.peerId, identify1.addrs)

// daemons[0] will take some time to get the peers
await new Promise(resolve => setTimeout(resolve, 1000))

// verify connected peers
const knownPeersAfterConnect1 = await daemons[0].client.listPeers()
expect(knownPeersAfterConnect1).to.have.lengthOf(1)
expect(knownPeersAfterConnect1[0].toB58String()).to.equal(identify2.peerId.toB58String())

const knownPeersAfterConnect2 = await daemons[1].client.listPeers()
expect(knownPeersAfterConnect2).to.have.lengthOf(1)
expect(knownPeersAfterConnect2[0].toB58String()).to.equal(identify1.peerId.toB58String())
;[{ secio: true, noise: false }, { secio: false, noise: true }].forEach(config => {
describe(`connect ${JSON.stringify(config)}`, () => {
let daemons

// Start Daemons
before(async function () {
this.timeout(20 * 1000)
daemons = await spawnDaemons(2, 'js', config)
})

// Stop daemons
after(async function () {
await Promise.all(
daemons.map((daemon) => daemon.stop())
)
})

it('js peer to js peer', async function () {
this.timeout(10 * 1000)

const identify1 = await daemons[0].client.identify()
const identify2 = await daemons[1].client.identify()

// verify connected peers
const knownPeersBeforeConnect1 = await daemons[0].client.listPeers()
expect(knownPeersBeforeConnect1).to.have.lengthOf(0)

const knownPeersBeforeConnect2 = await daemons[1].client.listPeers()
expect(knownPeersBeforeConnect2).to.have.lengthOf(0)

// connect peers
await daemons[1].client.connect(identify1.peerId, identify1.addrs)

// daemons[0] will take some time to get the peers
await new Promise(resolve => setTimeout(resolve, 1000))

// verify connected peers
const knownPeersAfterConnect1 = await daemons[0].client.listPeers()
expect(knownPeersAfterConnect1).to.have.lengthOf(1)
expect(knownPeersAfterConnect1[0].toB58String()).to.equal(identify2.peerId.toB58String())

const knownPeersAfterConnect2 = await daemons[1].client.listPeers()
expect(knownPeersAfterConnect2).to.have.lengthOf(1)
expect(knownPeersAfterConnect2[0].toB58String()).to.equal(identify1.peerId.toB58String())
})
})
})

0 comments on commit c77458b

Please sign in to comment.