This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const Relay = require('../src/circuit/relay') | ||
const Dialer = require('../src/circuit/dialer') | ||
const nodes = require('./fixtures/nodes') | ||
const Connection = require('interface-connection').Connection | ||
const multiaddr = require('multiaddr') | ||
const constants = require('../src/circuit/constants') | ||
const handshake = require('pull-handshake') | ||
const multicodec = require('../src/multicodec') | ||
const waterfall = require('async/waterfall') | ||
const PeerInfo = require('peer-info') | ||
const PeerId = require('peer-id') | ||
|
||
const sinon = require('sinon') | ||
const expect = require('chai').expect | ||
|
||
describe('relay', function () { | ||
describe(`handle circuit requests`, function () { | ||
const relay = sinon.createStubInstance(Relay) | ||
const dialer = sinon.createStubInstance(Dialer) | ||
|
||
let swarm | ||
let fromConn | ||
let toConn | ||
let stream | ||
let shake | ||
let handlerSpy | ||
|
||
beforeEach(function (done) { | ||
stream = handshake({timeout: 1000 * 60}) | ||
shake = stream.handshake | ||
fromConn = new Connection(stream) | ||
toConn = new Connection(shake.rest()) | ||
|
||
waterfall([ | ||
(cb) => PeerId.createFromJSON(nodes.node4, cb), | ||
(peerId, cb) => PeerInfo.create(peerId, cb), | ||
(peer, cb) => { | ||
swarm = { | ||
_peerInfo: peer, | ||
handle: sinon.spy((proto, h) => { | ||
handlerSpy = sinon.spy(h) | ||
}), | ||
conns: { | ||
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection() | ||
} | ||
} | ||
cb() | ||
} | ||
], () => { | ||
relay.mount.callThrough() | ||
relay.emit.callThrough() | ||
relay.on.callThrough() | ||
relay.mount(swarm) // mount the swarm | ||
relay.circuit.callsArg(2, null, toConn) | ||
|
||
dialer.negotiateRelay.callThrough() | ||
done() | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
relay.mount.reset() | ||
relay.emit.reset() | ||
relay.on.reset() | ||
relay.circuit.reset() | ||
dialer.negotiateRelay.reset() | ||
}) | ||
|
||
it(`handle a valid circuit request`, function (done) { | ||
relay.on('circuit:success', () => { | ||
expect(relay.circuit.calledWith(sinon.match.any, dstMa)).to.be.ok | ||
done() | ||
}) | ||
|
||
let dstMa = multiaddr(`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/`) | ||
dialer.negotiateRelay(fromConn, dstMa, () => {}) | ||
|
||
handlerSpy(multicodec.hop, toConn) | ||
}) | ||
|
||
// it(`fail dialing to invalid multiaddr`, function () { | ||
// // TODO: implement without relying on negotiateRelay | ||
// }) | ||
|
||
it(`not dial to self`, function (done) { | ||
let dstMa = multiaddr(`/ipfs/${nodes.node4.id}`) | ||
dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => { | ||
expect(err).to.not.be.null | ||
expect(err).to.be.an.instanceOf(Error) | ||
expect(err.message) | ||
.to.equal(`Got ${constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF} error code trying to dial over relay`) | ||
expect(newConn).to.be.undefined | ||
done() | ||
}) | ||
|
||
handlerSpy(multicodec.hop, toConn) | ||
}) | ||
|
||
it(`fail on address exeding 1024 bytes`, function (done) { | ||
let dstMa = multiaddr( | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit` + | ||
`/ip4/0.0.0.0/tcp/9033/ws/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE/p2p-circuit`) | ||
|
||
dialer.negotiateRelay(fromConn, dstMa, (err, newConn) => { | ||
expect(err).to.not.be.null | ||
expect(err).to.be.an.instanceOf(Error) | ||
expect(err.message) | ||
.to.equal(`Got ${constants.RESPONSE.HOP.DST_ADDR_TOO_LONG} error code trying to dial over relay`) | ||
expect(newConn).to.be.undefined | ||
done() | ||
}) | ||
|
||
handlerSpy(multicodec.hop, toConn) | ||
}) | ||
}) | ||
}) |