|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const Relay = require('../src/circuit/relay') |
| 5 | +const Dialer = require('../src/circuit/dialer') |
| 6 | +const nodes = require('./fixtures/nodes') |
| 7 | +const Connection = require('interface-connection').Connection |
| 8 | +const multiaddr = require('multiaddr') |
| 9 | +const constants = require('../src/circuit/constants') |
| 10 | +const handshake = require('pull-handshake') |
| 11 | +const multicodec = require('../src/multicodec') |
| 12 | +const waterfall = require('async/waterfall') |
| 13 | +const PeerInfo = require('peer-info') |
| 14 | +const PeerId = require('peer-id') |
| 15 | + |
| 16 | +const sinon = require('sinon') |
| 17 | +const expect = require('chai').expect |
| 18 | + |
| 19 | +describe('relay', function () { |
| 20 | + describe(`handle circuit requests`, function () { |
| 21 | + const relay = sinon.createStubInstance(Relay) |
| 22 | + const dialer = sinon.createStubInstance(Dialer) |
| 23 | + |
| 24 | + let swarm |
| 25 | + let fromConn |
| 26 | + let toConn |
| 27 | + let stream |
| 28 | + let shake |
| 29 | + let handlerSpy |
| 30 | + |
| 31 | + beforeEach(function (done) { |
| 32 | + stream = handshake({timeout: 1000 * 60}) |
| 33 | + shake = stream.handshake |
| 34 | + fromConn = new Connection(stream) |
| 35 | + toConn = new Connection(shake.rest()) |
| 36 | + |
| 37 | + waterfall([ |
| 38 | + (cb) => PeerId.createFromJSON(nodes.node4, cb), |
| 39 | + (peerId, cb) => PeerInfo.create(peerId, cb), |
| 40 | + (peer, cb) => { |
| 41 | + swarm = { |
| 42 | + _peerInfo: peer, |
| 43 | + handle: sinon.spy((proto, h) => { |
| 44 | + handlerSpy = sinon.spy(h) |
| 45 | + }), |
| 46 | + conns: { |
| 47 | + QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection() |
| 48 | + } |
| 49 | + } |
| 50 | + cb() |
| 51 | + } |
| 52 | + ], () => { |
| 53 | + relay.mount.callThrough() |
| 54 | + relay.emit.callThrough() |
| 55 | + relay.on.callThrough() |
| 56 | + relay.mount(swarm) // mount the swarm |
| 57 | + relay.circuit.callsArg(2, null, toConn) |
| 58 | + |
| 59 | + dialer.negotiateRelay.callThrough() |
| 60 | + done() |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + relay.mount.reset() |
| 66 | + relay.emit.reset() |
| 67 | + relay.on.reset() |
| 68 | + relay.circuit.reset() |
| 69 | + dialer.negotiateRelay.reset() |
| 70 | + }) |
| 71 | + |
| 72 | + it(`handle a valid circuit request`, function (done) { |
| 73 | + relay.on('circuit:success', () => { |
| 74 | + expect(relay.circuit.calledWith(sinon.match.any, dstMa)).to.be.ok |
| 75 | + done() |
| 76 | + }) |
| 77 | + |
| 78 | + let dstMa = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/`) |
| 79 | + dialer.negotiateRelay(fromConn, dstMa, () => {}) |
| 80 | + |
| 81 | + handlerSpy(multicodec.hop, toConn) |
| 82 | + }) |
| 83 | + |
| 84 | + // it(`fail dialing to invalid multiaddr`, function () { |
| 85 | + // // TODO: implement without relying on negotiateRelay |
| 86 | + // }) |
| 87 | + |
| 88 | + it(`not dial to self`, function (done) { |
| 89 | + let dstMa = multiaddr(`/ipfs/${nodes.node4.id}`) |
| 90 | + dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => { |
| 91 | + expect(err).to.not.be.null |
| 92 | + expect(err).to.be.an.instanceOf(Error) |
| 93 | + expect(err.message) |
| 94 | + .to.equal(`Got ${constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF} error code trying to dial over relay`) |
| 95 | + expect(newConn).to.be.undefined |
| 96 | + done() |
| 97 | + }) |
| 98 | + |
| 99 | + handlerSpy(multicodec.hop, toConn) |
| 100 | + }) |
| 101 | + |
| 102 | + it(`fail on address exeding 1024 bytes`, function (done) { |
| 103 | + let dstMa = multiaddr( |
| 104 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 105 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 106 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 107 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 108 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 109 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 110 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 111 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 112 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 113 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 114 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 115 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 116 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 117 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 118 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 119 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + |
| 120 | + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit`) |
| 121 | + |
| 122 | + dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => { |
| 123 | + expect(err).to.not.be.null |
| 124 | + expect(err).to.be.an.instanceOf(Error) |
| 125 | + expect(err.message) |
| 126 | + .to.equal(`Got ${constants.RESPONSE.HOP.DST_ADDR_TOO_LONG} error code trying to dial over relay`) |
| 127 | + expect(newConn).to.be.undefined |
| 128 | + done() |
| 129 | + }) |
| 130 | + |
| 131 | + handlerSpy(multicodec.hop, toConn) |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments