Skip to content

Commit

Permalink
feat: add support for specifying noise and secio
Browse files Browse the repository at this point in the history
feat: add ability to specify local daemons

test: run connect tests with multiple configs
  • Loading branch information
jacobheun committed Feb 12, 2020
1 parent dd8cdb5 commit 83ae5d4
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 199 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ This repository will be used for interop tests.
> npm test
```

### Test with a non yet released version of js-ipfs
#### Testing local daemons

TODO
It is possible to test local versions of the go and js daemons exporting the respective path before running the tests.

### Test with a non yet released version of go-ipfs
**Specifying the go-libp2p daemon**
See the go-libp2p-daemon [install instructions](https://github.com/libp2p/go-libp2p-daemon#install) for building the local binary.

TODO
```sh
$ LIBP2P_GO_BIN=$GOPATH/bin/p2pd npm run test
```

**Specifying the js-libp2p daemon**
From the js-libp2p-daemon local repo checkout you can perform an `npm link` to create a binary, `jsp2pd` in the global npm space.

```sh
$ LIBP2P_JS_BIN=$(which jsp2pd) npm run test
```

## Contribute

Expand Down
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
15 changes: 12 additions & 3 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ const path = require('path')
const rimraf = require('rimraf')

const Client = require('libp2p-daemon-client')
const { getMultiaddr, isWindows } = require('./utils')
const { getMultiaddr, isWindows, DEFAULT_CONFIG } = require('./utils')

// process path
const processPath = process.cwd()

// go-libp2p defaults
const goDaemon = {
defaultAddr: getMultiaddr('/tmp/p2pd-go.sock'),
bin: path.join('go-libp2p-dep', 'go-libp2p', isWindows ? 'p2pd.exe' : 'p2pd')
bin: process.env.LIBP2P_GO_BIN || path.join('go-libp2p-dep', 'go-libp2p', isWindows ? 'p2pd.exe' : 'p2pd')
}

// js-libp2p defaults
const jsDaemon = {
defaultAddr: getMultiaddr('/tmp/p2pd-js.sock'),
bin: path.join('libp2p-daemon', 'src', 'cli', 'bin.js')
bin: process.env.LIBP2P_JS_BIN || path.join('libp2p-daemon', 'src', 'cli', 'bin.js')
}

class Daemon {
Expand Down Expand Up @@ -55,6 +55,10 @@ class Daemon {
*/
_getBinPath (type) {
const depPath = type === 'go' ? goDaemon.bin : jsDaemon.bin
if (fs.existsSync(depPath)) {
return depPath
}

let npmPath = path.join(processPath, '../', depPath)

if (fs.existsSync(npmPath)) {
Expand Down Expand Up @@ -99,6 +103,7 @@ class Daemon {
* @returns {Promise}
*/
_startDaemon (options) {
options = { ...DEFAULT_CONFIG, ...options }
return new Promise((resolve, reject) => {
let execOptions
const addr = this._addr.toString()
Expand All @@ -107,12 +112,16 @@ class Daemon {
if (this._type === 'go') {
execOptions = ['-listen', addr]

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

execOptions.push(`--secio=${options.secio}`)
execOptions.push(`--noise=${options.noise}`)
options.dht && execOptions.push('--dht')
options.pubsub && execOptions.push('--pubsub')
options.pubsubRouter && execOptions.push('--pubsubRouter', options.pubsubRouter)
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ exports.getSockPath = (sockPath) => isWindows
exports.getMultiaddr = (sockPath, port) => isWindows
? ma(`/ip4/0.0.0.0/tcp/${port || 8080}`)
: ma(`/unix${path.resolve(os.tmpdir(), sockPath)}`)

exports.DEFAULT_CONFIG = {
secio: true,
noise: false,
dht: false
}
66 changes: 34 additions & 32 deletions test/connect/go2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,48 @@ const expect = chai.expect

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

describe('connect', () => {
let daemons
module.exports = (name, config) => {
describe(`connect ${name}`, () => {
let daemons

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

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

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

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

const identify1 = await daemons[0].client.identify()
const identify2 = await daemons[1].client.identify()
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)
// 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)
const knownPeersBeforeConnect2 = await daemons[1].client.listPeers()
expect(knownPeersBeforeConnect2).to.have.lengthOf(0)

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

// 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())
// 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())
const knownPeersAfterConnect2 = await daemons[1].client.listPeers()
expect(knownPeersAfterConnect2).to.have.lengthOf(1)
expect(knownPeersAfterConnect2[0].toB58String()).to.equal(identify1.peerId.toB58String())
})
})
})
}
52 changes: 27 additions & 25 deletions test/connect/go2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const expect = chai.expect

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

const beforeConnect = (ctx, keyType) => {
const beforeConnect = (ctx, keyType, config) => {
ctx.timeout(20 * 1000)

return spawnDaemons(2, [{ type: 'go', keyType }, { type: 'js', keyType }])
return spawnDaemons(2, [{ type: 'go', keyType }, { type: 'js', keyType }], config)
}

const afterConnect = async (daemons) => {
Expand Down Expand Up @@ -58,36 +58,38 @@ const performTest = async (ctx, daemons) => {
expect(knownPeersAfterConnectJs[0].toB58String()).to.equal(goId)
}

describe('connecting go peer to js peer', () => {
describe('with RSA keys', () => {
let daemons
module.exports = (name, config) => {
describe(`connecting go peer to js peer ${name}`, () => {
describe('with RSA keys', () => {
let daemons

before(async function () {
daemons = await beforeConnect(this, 'rsa')
})
before(async function () {
daemons = await beforeConnect(this, 'rsa', config)
})

after(async () => {
await afterConnect(daemons)
})
after(async () => {
await afterConnect(daemons)
})

it('should work', async function () {
await performTest(this, daemons)
it('should work', async function () {
await performTest(this, daemons)
})
})
})

describe('with SECP256k1 keys', () => {
let daemons
describe('with SECP256k1 keys', () => {
let daemons

before(async function () {
daemons = await beforeConnect(this, 'secp256k1')
})
before(async function () {
daemons = await beforeConnect(this, 'secp256k1', config)
})

after(async () => {
await afterConnect(daemons)
})
after(async () => {
await afterConnect(daemons)
})

it('should work', async function () {
await performTest(this, daemons)
it('should work', async function () {
await performTest(this, daemons)
})
})
})
})
}
15 changes: 11 additions & 4 deletions test/connect/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict'

require('./go2go')
require('./go2js')
require('./js2go')
require('./js2js')
// Perform tests against secio
require('./go2go')('secio', { secio: true, noise: false })
require('./go2js')('secio', { secio: true, noise: false })
require('./js2go')('secio', { secio: true, noise: false })
require('./js2js')('secio', { secio: true, noise: false })

// Perform tests against noise
require('./go2go')('noise', { secio: false, noise: true })
require('./go2js')('noise', { secio: false, noise: true })
require('./js2go')('noise', { secio: false, noise: true })
require('./js2js')('noise', { secio: false, noise: true })
52 changes: 27 additions & 25 deletions test/connect/js2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const expect = chai.expect

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

const beforeConnect = (ctx, keyType) => {
const beforeConnect = (ctx, keyType, config) => {
ctx.timeout(20 * 1000)

return spawnDaemons(2, [{ type: 'js', keyType }, { type: 'go', keyType }])
return spawnDaemons(2, [{ type: 'js', keyType }, { type: 'go', keyType }], config)
}

const afterConnect = async (daemons) => {
Expand Down Expand Up @@ -58,36 +58,38 @@ const performTest = async (ctx, daemons) => {
expect(knownPeersAfterConnectGo[0].toB58String()).to.equal(jsId)
}

describe('connecting js peer to go peer', () => {
describe('with RSA keys', () => {
let daemons
module.exports = (name, config) => {
describe(`connecting js peer to go peer ${name}`, () => {
describe('with RSA keys', () => {
let daemons

before(async function () {
daemons = await beforeConnect(this, 'rsa')
})
before(async function () {
daemons = await beforeConnect(this, 'rsa', config)
})

after(async () => {
await afterConnect(daemons)
})
after(async () => {
await afterConnect(daemons)
})

it('should work', async function () {
await performTest(this, daemons)
it('should work', async function () {
await performTest(this, daemons)
})
})
})

describe('with SECP256k1 keys', () => {
let daemons
describe('with SECP256k1 keys', () => {
let daemons

before(async function () {
daemons = await beforeConnect(this, 'secp256k1')
})
before(async function () {
daemons = await beforeConnect(this, 'secp256k1', config)
})

after(async () => {
await afterConnect(daemons)
})
after(async () => {
await afterConnect(daemons)
})

it('should work', async function () {
await performTest(this, daemons)
it('should work', async function () {
await performTest(this, daemons)
})
})
})
})
}
Loading

0 comments on commit 83ae5d4

Please sign in to comment.