|
| 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 secio = require('libp2p-secio') |
| 13 | +const PeerBook = require('peer-book') |
| 14 | +const identify = require('libp2p-identify') |
| 15 | +const lp = require('pull-length-prefixed') |
| 16 | + |
| 17 | +const utils = require('./utils') |
| 18 | +const createInfos = utils.createInfos |
| 19 | +const Switch = require('../src') |
| 20 | + |
| 21 | +describe('Identify', () => { |
| 22 | + let switchA |
| 23 | + let switchB |
| 24 | + let switchC |
| 25 | + |
| 26 | + before((done) => createInfos(3, (err, infos) => { |
| 27 | + expect(err).to.not.exist() |
| 28 | + |
| 29 | + const peerA = infos[0] |
| 30 | + const peerB = infos[1] |
| 31 | + const peerC = infos[2] |
| 32 | + |
| 33 | + peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') |
| 34 | + peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002') |
| 35 | + peerC.multiaddrs.add('/ip4/127.0.0.1/tcp/9003') |
| 36 | + |
| 37 | + switchA = new Switch(peerA, new PeerBook()) |
| 38 | + switchB = new Switch(peerB, new PeerBook()) |
| 39 | + switchC = new Switch(peerC, new PeerBook()) |
| 40 | + |
| 41 | + switchA.transport.add('tcp', new TCP()) |
| 42 | + switchB.transport.add('tcp', new TCP()) |
| 43 | + switchC.transport.add('tcp', new TCP()) |
| 44 | + |
| 45 | + switchA.connection.crypto(secio.tag, secio.encrypt) |
| 46 | + switchB.connection.crypto(secio.tag, secio.encrypt) |
| 47 | + switchC.connection.crypto(secio.tag, secio.encrypt) |
| 48 | + |
| 49 | + switchA.connection.addStreamMuxer(multiplex) |
| 50 | + switchB.connection.addStreamMuxer(multiplex) |
| 51 | + switchC.connection.addStreamMuxer(multiplex) |
| 52 | + |
| 53 | + switchA.connection.reuse() |
| 54 | + switchB.connection.reuse() |
| 55 | + switchC.connection.reuse() |
| 56 | + |
| 57 | + parallel([ |
| 58 | + (cb) => switchA.transport.listen('tcp', {}, null, cb), |
| 59 | + (cb) => switchB.transport.listen('tcp', {}, null, cb), |
| 60 | + (cb) => switchC.transport.listen('tcp', {}, null, cb) |
| 61 | + ], done) |
| 62 | + })) |
| 63 | + |
| 64 | + after(function (done) { |
| 65 | + this.timeout(3 * 1000) |
| 66 | + parallel([ |
| 67 | + (cb) => switchA.stop(cb), |
| 68 | + (cb) => switchB.stop(cb), |
| 69 | + (cb) => switchC.stop(cb) |
| 70 | + ], done) |
| 71 | + }) |
| 72 | + |
| 73 | + afterEach(function (done) { |
| 74 | + // Hangup everything |
| 75 | + parallel([ |
| 76 | + (cb) => switchA.hangUp(switchB._peerInfo, cb), |
| 77 | + (cb) => switchA.hangUp(switchC._peerInfo, cb), |
| 78 | + (cb) => switchB.hangUp(switchA._peerInfo, cb), |
| 79 | + (cb) => switchB.hangUp(switchC._peerInfo, cb), |
| 80 | + (cb) => switchC.hangUp(switchA._peerInfo, cb), |
| 81 | + (cb) => switchC.hangUp(switchB._peerInfo, cb) |
| 82 | + ], done) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should identify a good peer', (done) => { |
| 86 | + switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn)) |
| 87 | + switchB.dial(switchA._peerInfo, '/id-test/1.0.0', (err, conn) => { |
| 88 | + expect(err).to.not.exist() |
| 89 | + let data = Buffer.from('data that cant be had') |
| 90 | + pull( |
| 91 | + pull.values([data]), |
| 92 | + conn, |
| 93 | + pull.collect((err, values) => { |
| 94 | + expect(err).to.not.exist() |
| 95 | + expect(values).to.deep.equal([data]) |
| 96 | + done() |
| 97 | + }) |
| 98 | + ) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should require crypto and identify to have the same peerId', (done) => { |
| 103 | + identify.listener = (conn) => { |
| 104 | + conn.getObservedAddrs((err, observedAddrs) => { |
| 105 | + if (err) { return } |
| 106 | + observedAddrs = observedAddrs[0] |
| 107 | + |
| 108 | + // pretend to be another peer |
| 109 | + let publicKey = switchC._peerInfo.id.pubKey.bytes |
| 110 | + |
| 111 | + const msgSend = identify.message.encode({ |
| 112 | + protocolVersion: 'ipfs/0.1.0', |
| 113 | + agentVersion: 'na', |
| 114 | + publicKey: publicKey, |
| 115 | + listenAddrs: switchC._peerInfo.multiaddrs.toArray().map((ma) => ma.buffer), |
| 116 | + observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from('') |
| 117 | + }) |
| 118 | + |
| 119 | + pull( |
| 120 | + pull.values([msgSend]), |
| 121 | + lp.encode(), |
| 122 | + conn |
| 123 | + ) |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn)) |
| 128 | + switchB.dial(switchA._peerInfo, '/id-test/1.0.0', (err, conn) => { |
| 129 | + expect(err).to.not.exist() |
| 130 | + pull( |
| 131 | + pull.values([Buffer.from('data that cant be had')]), |
| 132 | + conn, |
| 133 | + pull.collect((err, values) => { |
| 134 | + expect(err).to.exist() |
| 135 | + expect(values).to.have.length(0) |
| 136 | + done() |
| 137 | + }) |
| 138 | + ) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments