This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
6 changed files
with
336 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/* eslint max-nested-callbacks: ["error", 8] */ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
const parallel = require('async/parallel') | ||
const series = require('async/series') | ||
const Factory = require('../../utils/ipfs-factory-daemon') | ||
|
||
const crypto = require('crypto') | ||
const utils = require('./utils') | ||
|
||
const multiaddr = require('multiaddr') | ||
|
||
chai.use(dirtyChai) | ||
|
||
describe('circuit interop', function () { | ||
this.timeout(20 * 1000) | ||
|
||
let jsTCP | ||
let jsTCPAddrs | ||
let jsWS | ||
let jsWSAddrs | ||
let jsRelayAddrs | ||
let factory = new Factory() | ||
|
||
let goRelayAddrs | ||
let goRelayDaemon | ||
|
||
let goTCPAddrs | ||
let goTCPDaemon | ||
let goTCP | ||
|
||
let goWSAddrs | ||
let goWSDaemon | ||
let goWS | ||
|
||
beforeEach((done) => { | ||
parallel([ | ||
(pCb) => utils.setupJsNode([ | ||
'/ip4/127.0.0.1/tcp/61454/ws', | ||
'/ip4/127.0.0.1/tcp/61453' | ||
], factory, true, pCb), | ||
(pCb) => utils.setupJsNode([ | ||
'/ip4/127.0.0.1/tcp/9002' | ||
], factory, pCb), | ||
(pCb) => utils.setupJsNode([ | ||
'/ip4/127.0.0.1/tcp/9003/ws' | ||
], factory, pCb), | ||
(pCb) => utils.setupGoNode([ | ||
'/ip4/0.0.0.0/tcp/0/ws', | ||
'/ip4/0.0.0.0/tcp/0' | ||
], true, pCb), | ||
(pCb) => utils.setupGoNode([ | ||
'/ip4/0.0.0.0/tcp/0' | ||
], pCb), | ||
(pCb) => utils.setupGoNode([ | ||
'/ip4/0.0.0.0/tcp/0/ws' | ||
], pCb) | ||
], (err, res) => { | ||
expect(err).to.not.exist() | ||
|
||
jsRelayPeer = res[0][0] | ||
jsRelayAddrs = res[0][1].map((a) => a.toString()).filter((a) => !a.includes('/p2p-circuit')) | ||
jsTCP = res[1][0] | ||
jsTCPAddrs = res[1][1].map((a) => a.toString()).filter((a) => a.includes('/p2p-circuit')) | ||
jsWS = res[2][0] | ||
jsWSAddrs = res[2][1].map((a) => a.toString()).filter((a) => a.includes('/p2p-circuit')) | ||
|
||
goRelay = res[3][0].api | ||
goRelayDaemon = res[3][0] | ||
goRelayAddrs = res[3][1] | ||
goTCP = res[4][0].api | ||
goTCPDaemon = res[4][0] | ||
goTCPAddrs = res[4][1] | ||
goWS = res[5][0].api | ||
goWSDaemon = res[5][0] | ||
goWSAddrs = res[5][1] | ||
done() | ||
}) | ||
}) | ||
|
||
afterEach((done) => { | ||
parallel([ | ||
(cb) => factory.dismantle(cb), | ||
(cb) => goRelayDaemon.stop(cb), | ||
(cb) => goTCPDaemon.stop(cb), | ||
(cb) => goWSDaemon.stop(cb) | ||
], done) | ||
}) | ||
|
||
it('jsWS <-> jsRelay <-> jsTCP', function (done) { | ||
const data = crypto.randomBytes(128) | ||
series([ | ||
(cb) => jsWS.swarm.connect(jsRelayAddrs[0], cb), | ||
(cb) => jsTCP.swarm.connect(jsRelayAddrs[1], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => jsTCP.swarm.connect(jsWSAddrs[0], cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
jsWS, | ||
jsTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('goWS <-> jsRelay <-> goTCP', function (done) { | ||
const data = crypto.randomBytes(128) | ||
series([ | ||
(cb) => goWS.swarm.connect(jsRelayAddrs[0], cb), | ||
(cb) => goTCP.swarm.connect(jsRelayAddrs[1], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => goTCP.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(goWSAddrs[0]).getPeerId()}`, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
goWS, | ||
goTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('jsWS <-> jsRelay <-> goTCP', function (done) { | ||
const data = crypto.randomBytes(128) | ||
series([ | ||
(cb) => jsWS.swarm.connect(jsRelayAddrs[0], cb), | ||
(cb) => goTCP.swarm.connect(jsRelayAddrs[1], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => goTCP.swarm.connect(jsWSAddrs[0], cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
jsWS, | ||
goTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('jsTCP <-> goRelay <-> jsWS', function (done) { | ||
const data = crypto.randomBytes(128) | ||
series([ | ||
(cb) => jsTCP.swarm.connect(goRelayAddrs[2], cb), | ||
(cb) => jsWS.swarm.connect(goRelayAddrs[0], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => jsWS.swarm.connect(jsTCPAddrs[0], cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
jsWS, | ||
jsTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('goTCP <-> goRelay <-> goWS', function (done) { | ||
const data = crypto.randomBytes(128) | ||
console.log(`goRelayAddrs: ${goRelayAddrs.map((a) => a.toString())}`) | ||
console.log(`goTCPAddrs: ${JSON.stringify(goTCPAddrs)})`) | ||
series([ | ||
(cb) => goWS.swarm.connect(goRelayAddrs[0], cb), | ||
(cb) => goTCP.swarm.connect(goRelayAddrs[2], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => goWS.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(goTCPAddrs[0]).getPeerId()}`, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
goWS, | ||
goTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('jsWS <-> goRelay <-> goTCP', function (done) { | ||
const data = crypto.randomBytes(128) | ||
console.log(`goRelayAddrs: ${goRelayAddrs.map(a => a.toString())}`) | ||
series([ | ||
(cb) => jsWS.swarm.connect(goRelayAddrs[0], cb), | ||
(cb) => goTCP.swarm.connect(goRelayAddrs[2], cb), | ||
(cb) => setTimeout(cb, 1000), | ||
(cb) => goTCP.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(jsWSAddrs[0]).getPeerId()}`, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
utils.addAndCat(data, | ||
jsWS, | ||
goTCP, | ||
(err, data) => { | ||
expect(err).to.not.exist() | ||
expect(data).to.be.equal(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
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,114 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
const waterfall = require('async/waterfall') | ||
|
||
const createTempRepo = require('../../utils/create-repo-nodejs') | ||
const relayConfig = require('../../utils/ipfs-factory-daemon/default-config.json') | ||
|
||
const GoDaemon = require('../daemons/go') | ||
|
||
exports.setupGoNode = function setupGoNode (addrs, hop, cb) { | ||
if (typeof hop === 'function') { | ||
cb = hop | ||
hop = false | ||
} | ||
|
||
const daemon = new GoDaemon({ | ||
disposable: true, | ||
init: true, | ||
config: { | ||
Addresses: { | ||
Swarm: addrs, | ||
API: `/ip4/0.0.0.0/tcp/0`, | ||
Gateway: `/ip4/0.0.0.0/tcp/0` | ||
}, | ||
Swarm: { | ||
AddrFilters: null, | ||
DisableBandwidthMetrics: false, | ||
DisableNatPortMap: false, | ||
DisableRelay: false, | ||
EnableRelayHop: hop | ||
} | ||
} | ||
}) | ||
|
||
daemon.start((err) => { | ||
expect(err).to.not.exist() | ||
daemon.api.id((err, id) => { | ||
expect(err).to.not.exist() | ||
cb(null, daemon, id.addresses) | ||
}) | ||
}) | ||
} | ||
|
||
exports.setupJsNode = function setupJsNode (addrs, factory, hop, cb) { | ||
let relayPeer | ||
let relayAddrs | ||
|
||
if (typeof hop === 'function') { | ||
cb = hop | ||
hop = false | ||
} | ||
|
||
cb = cb || (() => {}) | ||
|
||
waterfall([ | ||
(pCb) => { | ||
factory.spawnNode(createTempRepo(), Object.assign(relayConfig, { | ||
Addresses: { | ||
Swarm: addrs, | ||
API: `/ip4/0.0.0.0/tcp/0`, | ||
Gateway: `/ip4/0.0.0.0/tcp/0` | ||
}, | ||
EXPERIMENTAL: { | ||
Relay: { | ||
Enabled: true, | ||
HOP: { | ||
Enabled: hop, | ||
Active: false | ||
} | ||
} | ||
} | ||
}), (err, node) => { | ||
expect(err).to.not.exist() | ||
relayPeer = node | ||
pCb() | ||
}) | ||
}, | ||
(pCb) => { | ||
relayPeer.swarm.localAddrs((err, addrs) => { | ||
expect(err).to.not.exist() | ||
relayAddrs = addrs | ||
pCb() | ||
}) | ||
}], (err) => { | ||
expect(err).to.not.exist() | ||
cb(null, relayPeer, relayAddrs) | ||
}) | ||
} | ||
|
||
exports.addAndCat = function addAndCat (data, ipfsSrc, ipfsDst, callback) { | ||
waterfall([ | ||
(cb) => ipfsDst.files.add(data, cb), | ||
(res, cb) => ipfsSrc.files.cat(res[0].hash, function (err, stream) { | ||
expect(err).to.be.null() | ||
var res = '' | ||
|
||
stream.on('data', function (chunk) { | ||
res += chunk.toString() | ||
}) | ||
|
||
stream.on('error', function (err) { | ||
cb(err) | ||
}) | ||
|
||
stream.on('end', function () { | ||
cb(null, res) | ||
}) | ||
}) | ||
], callback) | ||
} |
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