Skip to content

Commit

Permalink
feat: add check for protector and enforced pnet
Browse files Browse the repository at this point in the history
fix: update protector config and tests
  • Loading branch information
jacobheun committed Jun 28, 2018
1 parent 7baf9f4 commit dc55b08
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const schema = Joi.object({
Joi.object()
)
).allow(null),
connProtector: Joi.object().keys({
protect: Joi.func().required()
}),
peerDiscovery: Joi.array().items(
Joi.alternatives().try(
Joi.func(),
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class Node extends EventEmitter {
})
}

// Attach private network protector
if (this._modules.connProtector) {
this._switch.protector = this._modules.connProtector
} else if (process.env.LIBP2P_FORCE_PNET) {
throw new Error('Private network is enforced, but no protector was provided')
}

// dht provided components (peerRouting, contentRouting, dht)
if (this._config.EXPERIMENTAL.dht) {
const DHT = this._modules.dht
Expand Down
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

require('./pnet.node')
require('./transports.node')
require('./stream-muxing.node')
require('./peer-discovery.node')
Expand Down
87 changes: 87 additions & 0 deletions test/pnet.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const WS = require('libp2p-websockets')
const defaultsDeep = require('@nodeutils/defaults-deep')

const Libp2p = require('../src')

describe('private network', () => {
let config

before((done) => {
waterfall([
(cb) => PeerId.create({ bits: 512 }, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peerInfo, cb) => {
config = {
peerInfo,
modules: {
transport: [ WS ]
}
}
cb()
}
], () => done())
})

describe('enforced network protection', () => {
before(() => {
process.env.LIBP2P_FORCE_PNET = 1
})

after(() => {
delete process.env.LIBP2P_FORCE_PNET
})

it('should throw an error without a provided protector', () => {
expect(() => {
return new Libp2p(config)
}).to.throw('Private network is enforced, but no protector was provided')
})

it('should create a libp2p node with a provided protector', () => {
let node
let protector = {
protect: () => { }
}

expect(() => {
let options = defaultsDeep(config, {
modules: {
connProtector: protector
}
})

return node = new Libp2p(options)
}).to.not.throw()
expect(node._switch.protector).to.deep.equal(protector)
})

it('should throw an error if the protector does not have a protect method', () => {
expect(() => {
let options = defaultsDeep(config, {
modules: {
connProtector: { }
}
})

return new Libp2p(options)
}).to.throw()
})
})

describe('network protection not enforced', () => {
it('should not throw an error with no provided protector', () => {
expect(() => {
return new Libp2p(config)
}).to.not.throw()
})
})
})

0 comments on commit dc55b08

Please sign in to comment.