|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const expect = chai.expect |
| 7 | +chai.use(dirtyChai) |
| 8 | +const parallel = require('async/parallel') |
| 9 | +const TCP = require('libp2p-tcp') |
| 10 | +const multiplex = require('libp2p-mplex') |
| 11 | +const pull = require('pull-stream') |
| 12 | +const PeerBook = require('peer-book') |
| 13 | +const secio = require('libp2p-secio') |
| 14 | +const Protector = require('libp2p-pnet') |
| 15 | + |
| 16 | +const utils = require('./utils') |
| 17 | +const createInfos = utils.createInfos |
| 18 | +const tryEcho = utils.tryEcho |
| 19 | +const Switch = require('../src') |
| 20 | + |
| 21 | +const generatePSK = Protector.generate |
| 22 | + |
| 23 | +const psk = Buffer.alloc(95) |
| 24 | +const psk2 = Buffer.alloc(95) |
| 25 | +generatePSK(psk) |
| 26 | +generatePSK(psk2) |
| 27 | + |
| 28 | +describe('Private Network', function () { |
| 29 | + this.timeout(20 * 1000) |
| 30 | + |
| 31 | + let switchA |
| 32 | + let switchB |
| 33 | + let switchC |
| 34 | + let switchD |
| 35 | + |
| 36 | + before((done) => createInfos(4, (err, infos) => { |
| 37 | + expect(err).to.not.exist() |
| 38 | + |
| 39 | + const peerA = infos[0] |
| 40 | + const peerB = infos[1] |
| 41 | + const peerC = infos[2] |
| 42 | + const peerD = infos[3] |
| 43 | + |
| 44 | + peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') |
| 45 | + peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002') |
| 46 | + peerC.multiaddrs.add('/ip4/127.0.0.1/tcp/9003') |
| 47 | + peerD.multiaddrs.add('/ip4/127.0.0.1/tcp/9004') |
| 48 | + |
| 49 | + switchA = new Switch(peerA, new PeerBook(), { |
| 50 | + protector: new Protector(psk) |
| 51 | + }) |
| 52 | + switchB = new Switch(peerB, new PeerBook(), { |
| 53 | + protector: new Protector(psk) |
| 54 | + }) |
| 55 | + // alternative way to add the protector |
| 56 | + switchC = new Switch(peerC, new PeerBook()) |
| 57 | + switchC.protector = new Protector(psk) |
| 58 | + // Create a switch on a different private network |
| 59 | + switchD = new Switch(peerD, new PeerBook(), { |
| 60 | + protector: new Protector(psk2) |
| 61 | + }) |
| 62 | + |
| 63 | + switchA.transport.add('tcp', new TCP()) |
| 64 | + switchB.transport.add('tcp', new TCP()) |
| 65 | + switchC.transport.add('tcp', new TCP()) |
| 66 | + switchD.transport.add('tcp', new TCP()) |
| 67 | + |
| 68 | + switchA.connection.crypto(secio.tag, secio.encrypt) |
| 69 | + switchB.connection.crypto(secio.tag, secio.encrypt) |
| 70 | + switchC.connection.crypto(secio.tag, secio.encrypt) |
| 71 | + switchD.connection.crypto(secio.tag, secio.encrypt) |
| 72 | + |
| 73 | + switchA.connection.addStreamMuxer(multiplex) |
| 74 | + switchB.connection.addStreamMuxer(multiplex) |
| 75 | + switchC.connection.addStreamMuxer(multiplex) |
| 76 | + switchD.connection.addStreamMuxer(multiplex) |
| 77 | + |
| 78 | + parallel([ |
| 79 | + (cb) => switchA.transport.listen('tcp', {}, null, cb), |
| 80 | + (cb) => switchB.transport.listen('tcp', {}, null, cb), |
| 81 | + (cb) => switchC.transport.listen('tcp', {}, null, cb), |
| 82 | + (cb) => switchD.transport.listen('tcp', {}, null, cb) |
| 83 | + ], done) |
| 84 | + })) |
| 85 | + |
| 86 | + after(function (done) { |
| 87 | + this.timeout(3 * 1000) |
| 88 | + parallel([ |
| 89 | + (cb) => switchA.stop(cb), |
| 90 | + (cb) => switchB.stop(cb), |
| 91 | + (cb) => switchC.stop(cb), |
| 92 | + (cb) => switchD.stop(cb) |
| 93 | + ], done) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should handle + dial on protocol', (done) => { |
| 97 | + switchB.handle('/abacaxi/1.0.0', (protocol, conn) => pull(conn, conn)) |
| 98 | + |
| 99 | + switchA.dial(switchB._peerInfo, '/abacaxi/1.0.0', (err, conn) => { |
| 100 | + expect(err).to.not.exist() |
| 101 | + expect(Object.keys(switchA.muxedConns).length).to.equal(1) |
| 102 | + tryEcho(conn, done) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should dial to warm conn', (done) => { |
| 107 | + switchB.dial(switchA._peerInfo, (err) => { |
| 108 | + expect(err).to.not.exist() |
| 109 | + expect(Object.keys(switchB.conns).length).to.equal(0) |
| 110 | + expect(Object.keys(switchB.muxedConns).length).to.equal(1) |
| 111 | + done() |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should dial on protocol, reuseing warmed conn', (done) => { |
| 116 | + switchA.handle('/papaia/1.0.0', (protocol, conn) => pull(conn, conn)) |
| 117 | + |
| 118 | + switchB.dial(switchA._peerInfo, '/papaia/1.0.0', (err, conn) => { |
| 119 | + expect(err).to.not.exist() |
| 120 | + expect(Object.keys(switchB.conns).length).to.equal(0) |
| 121 | + expect(Object.keys(switchB.muxedConns).length).to.equal(1) |
| 122 | + tryEcho(conn, done) |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should enable identify to reuse incomming muxed conn', (done) => { |
| 127 | + switchA.connection.reuse() |
| 128 | + switchC.connection.reuse() |
| 129 | + |
| 130 | + switchC.dial(switchA._peerInfo, (err) => { |
| 131 | + expect(err).to.not.exist() |
| 132 | + setTimeout(() => { |
| 133 | + expect(Object.keys(switchC.muxedConns).length).to.equal(1) |
| 134 | + expect(Object.keys(switchA.muxedConns).length).to.equal(2) |
| 135 | + done() |
| 136 | + }, 500) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + /** |
| 141 | + * This test is being skipped until a related issue with pull-reader overreading can be resolved |
| 142 | + * Currently this test will time out instead of returning an error properly. This is the same issue |
| 143 | + * in ipfs/interop, https://github.com/ipfs/interop/pull/24/commits/179978996ecaef39e78384091aa9669dcdb94cc0 |
| 144 | + */ |
| 145 | + it.skip('should fail to talk to a switch on a different private network', function (done) { |
| 146 | + this.timeout(30e3) |
| 147 | + switchD.dial(switchA._peerInfo, (err, conn) => { |
| 148 | + expect(err).to.exist() |
| 149 | + expect(conn).to.not.exist() |
| 150 | + expect(Object.keys(switchD.muxedConns).length).to.equal(0) |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments