|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +chai.use(require('dirty-chai')) |
| 6 | +const expect = chai.expect |
| 7 | +const PeerInfo = require('peer-info') |
| 8 | +const PeerId = require('peer-id') |
| 9 | +const waterfall = require('async/waterfall') |
| 10 | +const WS = require('libp2p-websockets') |
| 11 | +const defaultsDeep = require('@nodeutils/defaults-deep') |
| 12 | + |
| 13 | +const Libp2p = require('../src') |
| 14 | + |
| 15 | +describe('private network', () => { |
| 16 | + let config |
| 17 | + |
| 18 | + before((done) => { |
| 19 | + waterfall([ |
| 20 | + (cb) => PeerId.create({ bits: 512 }, cb), |
| 21 | + (peerId, cb) => PeerInfo.create(peerId, cb), |
| 22 | + (peerInfo, cb) => { |
| 23 | + config = { |
| 24 | + peerInfo, |
| 25 | + modules: { |
| 26 | + transport: [ WS ] |
| 27 | + } |
| 28 | + } |
| 29 | + cb() |
| 30 | + } |
| 31 | + ], () => done()) |
| 32 | + }) |
| 33 | + |
| 34 | + describe('enforced network protection', () => { |
| 35 | + before(() => { |
| 36 | + process.env.LIBP2P_FORCE_PNET = 1 |
| 37 | + }) |
| 38 | + |
| 39 | + after(() => { |
| 40 | + delete process.env.LIBP2P_FORCE_PNET |
| 41 | + }) |
| 42 | + |
| 43 | + it('should throw an error without a provided protector', () => { |
| 44 | + expect(() => { |
| 45 | + return new Libp2p(config) |
| 46 | + }).to.throw('Private network is enforced, but no protector was provided') |
| 47 | + }) |
| 48 | + |
| 49 | + it('should create a libp2p node with a provided protector', () => { |
| 50 | + let node |
| 51 | + let protector = { |
| 52 | + psk: '123', |
| 53 | + tag: '/psk/1.0.0', |
| 54 | + protect: () => { } |
| 55 | + } |
| 56 | + |
| 57 | + expect(() => { |
| 58 | + let options = defaultsDeep(config, { |
| 59 | + modules: { |
| 60 | + connProtector: protector |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + node = new Libp2p(options) |
| 65 | + return node |
| 66 | + }).to.not.throw() |
| 67 | + expect(node._switch.protector).to.deep.equal(protector) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should throw an error if the protector does not have a protect method', () => { |
| 71 | + expect(() => { |
| 72 | + let options = defaultsDeep(config, { |
| 73 | + modules: { |
| 74 | + connProtector: { } |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + return new Libp2p(options) |
| 79 | + }).to.throw() |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('network protection not enforced', () => { |
| 84 | + it('should not throw an error with no provided protector', () => { |
| 85 | + expect(() => { |
| 86 | + return new Libp2p(config) |
| 87 | + }).to.not.throw() |
| 88 | + }) |
| 89 | + }) |
| 90 | +}) |
0 commit comments