|
| 1 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const chai = require('chai') |
| 6 | +const dirtyChai = require('dirty-chai') |
| 7 | +const expect = chai.expect |
| 8 | +const waterfall = require('async/waterfall') |
| 9 | +const parallel = require('async/parallel') |
| 10 | +const Factory = require('../../utils/ipfs-factory-daemon/index') |
| 11 | +const GoDaemon = require('../daemons/go') |
| 12 | + |
| 13 | +const utils = require('./utils') |
| 14 | + |
| 15 | +chai.use(dirtyChai) |
| 16 | + |
| 17 | +describe('circuit interop', function () { |
| 18 | + this.timeout(20 * 1000) |
| 19 | + |
| 20 | + let jsTCP |
| 21 | + let jsWS |
| 22 | + let jsRelayPeer |
| 23 | + let jsRelayAddrs |
| 24 | + let goTCP = new GoDaemon() |
| 25 | + let goRelayAddrs |
| 26 | + let factory = new Factory() |
| 27 | + |
| 28 | + beforeEach((done) => { |
| 29 | + jsTCP = utils.setupNode([ |
| 30 | + '/ip4/0.0.0.0/tcp/9002' |
| 31 | + ]) |
| 32 | + |
| 33 | + jsWS = utils.setupNode([ |
| 34 | + '/ip4/0.0.0.0/tcp/9003/ws' |
| 35 | + ]) |
| 36 | + |
| 37 | + waterfall([ |
| 38 | + (pCb) => utils.setupRelay(['/ip4/127.0.0.1/tcp/61452/ws', '/ip4/127.0.0.1/tcp/61453'], factory, pCb), |
| 39 | + (peer, addr, pCb) => { |
| 40 | + jsRelayAddrs = addr |
| 41 | + jsRelayPeer = peer |
| 42 | + pCb() |
| 43 | + }, |
| 44 | + (pCb) => jsWS.start(pCb), |
| 45 | + (pCb) => jsTCP.start(pCb), |
| 46 | + (pCb) => goTCP.start(pCb), |
| 47 | + (pCb) => { |
| 48 | + goTCP.api.id((err, peer) => { |
| 49 | + expect(err).to.not.exist() |
| 50 | + console.log(JSON.stringify(peer.addresses)) |
| 51 | + |
| 52 | + goRelayAddrs = peer.addresses |
| 53 | + pCb() |
| 54 | + }) |
| 55 | + } |
| 56 | + ], done) |
| 57 | + }) |
| 58 | + |
| 59 | + afterEach((done) => { |
| 60 | + waterfall([ |
| 61 | + (cb) => jsWS.stop(cb), |
| 62 | + (cb) => jsTCP.stop(cb), |
| 63 | + (cb) => goTCP.stop(cb), |
| 64 | + (cb) => factory.dismantle((err) => done(err)) |
| 65 | + ], done) |
| 66 | + }) |
| 67 | + |
| 68 | + it('jsES <-> js-relay <-> jsTCP', function (done) { |
| 69 | + let addr = goRelayAddrs.filter((a) => !a.toString().includes('/p2p-circuit')) |
| 70 | + parallel([ |
| 71 | + (cb) => jsWS.swarm.connect(addr[0], cb), |
| 72 | + (cb) => jsTCP.swarm.connect(addr[1], cb) |
| 73 | + ], (err) => { |
| 74 | + expect(err).to.not.exist() |
| 75 | + waterfall([ |
| 76 | + (cb) => jsTCP.swarm.connect(jsWS._peerInfo, cb), |
| 77 | + (conn, cb) => utils.addAndCat(new jsTCP.types.Buffer('Hello world over circuit!'), |
| 78 | + jsWS, |
| 79 | + jsTCP, |
| 80 | + (err, data) => { |
| 81 | + expect(err).to.not.exist() |
| 82 | + expect(data).to.be.equal('Hello world over circuit!') |
| 83 | + cb() |
| 84 | + }) |
| 85 | + ], done) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('goTCP <-> js-relay <-> jsWS', function (done) { |
| 90 | + let addr = jsRelayAddrs.filter((a) => !a.toString().includes('/p2p-circuit')) |
| 91 | + parallel([ |
| 92 | + (cb) => jsWS.swarm.connect(addr[0], cb), |
| 93 | + (cb) => goTCP.api.swarm.connect(addr[1], cb) |
| 94 | + ], (err) => { |
| 95 | + expect(err).to.not.exist() |
| 96 | + waterfall([ |
| 97 | + (cb) => goTCP.api.swarm.connect(jsWS._peerInfo, cb), |
| 98 | + (conn, cb) => utils.addAndCat(new jsTCP.types.Buffer('Hello world over circuit!'), |
| 99 | + jsWS, |
| 100 | + goTCP.api, |
| 101 | + (err, data) => { |
| 102 | + expect(err).to.not.exist() |
| 103 | + expect(data).to.be.equal('Hello world over circuit!') |
| 104 | + }) |
| 105 | + ], done) |
| 106 | + }) |
| 107 | + }) |
| 108 | +}) |
0 commit comments