|
| 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 parallel = require('async/parallel') |
| 9 | +const series = require('async/series') |
| 10 | +const Factory = require('../../utils/ipfs-factory-daemon') |
| 11 | + |
| 12 | +const crypto = require('crypto') |
| 13 | +const utils = require('./utils') |
| 14 | + |
| 15 | +const multiaddr = require('multiaddr') |
| 16 | + |
| 17 | +chai.use(dirtyChai) |
| 18 | + |
| 19 | +describe('circuit interop', function () { |
| 20 | + this.timeout(20 * 1000) |
| 21 | + |
| 22 | + let jsTCP |
| 23 | + let jsTCPAddrs |
| 24 | + let jsWS |
| 25 | + let jsWSAddrs |
| 26 | + let jsRelayAddrs |
| 27 | + let factory = new Factory() |
| 28 | + |
| 29 | + let goRelayAddrs |
| 30 | + let goRelayDaemon |
| 31 | + |
| 32 | + let goTCPAddrs |
| 33 | + let goTCPDaemon |
| 34 | + let goTCP |
| 35 | + |
| 36 | + let goWSAddrs |
| 37 | + let goWSDaemon |
| 38 | + let goWS |
| 39 | + |
| 40 | + beforeEach((done) => { |
| 41 | + parallel([ |
| 42 | + (pCb) => utils.setupJsNode([ |
| 43 | + '/ip4/127.0.0.1/tcp/61454/ws', |
| 44 | + '/ip4/127.0.0.1/tcp/61453' |
| 45 | + ], factory, true, pCb), |
| 46 | + (pCb) => utils.setupJsNode([ |
| 47 | + '/ip4/127.0.0.1/tcp/9002' |
| 48 | + ], factory, pCb), |
| 49 | + (pCb) => utils.setupJsNode([ |
| 50 | + '/ip4/127.0.0.1/tcp/9003/ws' |
| 51 | + ], factory, pCb), |
| 52 | + (pCb) => utils.setupGoNode([ |
| 53 | + '/ip4/0.0.0.0/tcp/0/ws', |
| 54 | + '/ip4/0.0.0.0/tcp/0' |
| 55 | + ], true, pCb), |
| 56 | + (pCb) => utils.setupGoNode([ |
| 57 | + '/ip4/0.0.0.0/tcp/0' |
| 58 | + ], pCb), |
| 59 | + (pCb) => utils.setupGoNode([ |
| 60 | + '/ip4/0.0.0.0/tcp/0/ws' |
| 61 | + ], pCb) |
| 62 | + ], (err, res) => { |
| 63 | + expect(err).to.not.exist() |
| 64 | + |
| 65 | + jsRelayPeer = res[0][0] |
| 66 | + jsRelayAddrs = res[0][1].map((a) => a.toString()).filter((a) => !a.includes('/p2p-circuit')) |
| 67 | + jsTCP = res[1][0] |
| 68 | + jsTCPAddrs = res[1][1].map((a) => a.toString()).filter((a) => a.includes('/p2p-circuit')) |
| 69 | + jsWS = res[2][0] |
| 70 | + jsWSAddrs = res[2][1].map((a) => a.toString()).filter((a) => a.includes('/p2p-circuit')) |
| 71 | + |
| 72 | + goRelay = res[3][0].api |
| 73 | + goRelayDaemon = res[3][0] |
| 74 | + goRelayAddrs = res[3][1] |
| 75 | + goTCP = res[4][0].api |
| 76 | + goTCPDaemon = res[4][0] |
| 77 | + goTCPAddrs = res[4][1] |
| 78 | + goWS = res[5][0].api |
| 79 | + goWSDaemon = res[5][0] |
| 80 | + goWSAddrs = res[5][1] |
| 81 | + done() |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + afterEach((done) => { |
| 86 | + parallel([ |
| 87 | + (cb) => factory.dismantle(cb), |
| 88 | + (cb) => goRelayDaemon.stop(cb), |
| 89 | + (cb) => goTCPDaemon.stop(cb), |
| 90 | + (cb) => goWSDaemon.stop(cb) |
| 91 | + ], done) |
| 92 | + }) |
| 93 | + |
| 94 | + it('jsWS <-> jsRelay <-> jsTCP', function (done) { |
| 95 | + const data = crypto.randomBytes(128) |
| 96 | + series([ |
| 97 | + (cb) => jsWS.swarm.connect(jsRelayAddrs[0], cb), |
| 98 | + (cb) => jsTCP.swarm.connect(jsRelayAddrs[1], cb), |
| 99 | + (cb) => setTimeout(cb, 1000), |
| 100 | + (cb) => jsTCP.swarm.connect(jsWSAddrs[0], cb) |
| 101 | + ], (err) => { |
| 102 | + expect(err).to.not.exist() |
| 103 | + utils.addAndCat(data, |
| 104 | + jsWS, |
| 105 | + jsTCP, |
| 106 | + (err, data) => { |
| 107 | + expect(err).to.not.exist() |
| 108 | + expect(data).to.be.equal(data) |
| 109 | + done() |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('goWS <-> jsRelay <-> goTCP', function (done) { |
| 115 | + const data = crypto.randomBytes(128) |
| 116 | + series([ |
| 117 | + (cb) => goWS.swarm.connect(jsRelayAddrs[0], cb), |
| 118 | + (cb) => goTCP.swarm.connect(jsRelayAddrs[1], cb), |
| 119 | + (cb) => setTimeout(cb, 1000), |
| 120 | + (cb) => goTCP.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(goWSAddrs[0]).getPeerId()}`, cb) |
| 121 | + ], (err) => { |
| 122 | + expect(err).to.not.exist() |
| 123 | + utils.addAndCat(data, |
| 124 | + goWS, |
| 125 | + goTCP, |
| 126 | + (err, data) => { |
| 127 | + expect(err).to.not.exist() |
| 128 | + expect(data).to.be.equal(data) |
| 129 | + done() |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + it('jsWS <-> jsRelay <-> goTCP', function (done) { |
| 135 | + const data = crypto.randomBytes(128) |
| 136 | + series([ |
| 137 | + (cb) => jsWS.swarm.connect(jsRelayAddrs[0], cb), |
| 138 | + (cb) => goTCP.swarm.connect(jsRelayAddrs[1], cb), |
| 139 | + (cb) => setTimeout(cb, 1000), |
| 140 | + (cb) => goTCP.swarm.connect(jsWSAddrs[0], cb) |
| 141 | + ], (err) => { |
| 142 | + expect(err).to.not.exist() |
| 143 | + utils.addAndCat(data, |
| 144 | + jsWS, |
| 145 | + goTCP, |
| 146 | + (err, data) => { |
| 147 | + expect(err).to.not.exist() |
| 148 | + expect(data).to.be.equal(data) |
| 149 | + done() |
| 150 | + }) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + it('jsTCP <-> goRelay <-> jsWS', function (done) { |
| 155 | + const data = crypto.randomBytes(128) |
| 156 | + series([ |
| 157 | + (cb) => jsTCP.swarm.connect(goRelayAddrs[2], cb), |
| 158 | + (cb) => jsWS.swarm.connect(goRelayAddrs[0], cb), |
| 159 | + (cb) => setTimeout(cb, 1000), |
| 160 | + (cb) => jsWS.swarm.connect(jsTCPAddrs[0], cb) |
| 161 | + ], (err) => { |
| 162 | + expect(err).to.not.exist() |
| 163 | + utils.addAndCat(data, |
| 164 | + jsWS, |
| 165 | + jsTCP, |
| 166 | + (err, data) => { |
| 167 | + expect(err).to.not.exist() |
| 168 | + expect(data).to.be.equal(data) |
| 169 | + done() |
| 170 | + }) |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + it('goTCP <-> goRelay <-> goWS', function (done) { |
| 175 | + const data = crypto.randomBytes(128) |
| 176 | + console.log(`goRelayAddrs: ${goRelayAddrs.map((a) => a.toString())}`) |
| 177 | + console.log(`goTCPAddrs: ${JSON.stringify(goTCPAddrs)})`) |
| 178 | + series([ |
| 179 | + (cb) => goWS.swarm.connect(goRelayAddrs[0], cb), |
| 180 | + (cb) => goTCP.swarm.connect(goRelayAddrs[2], cb), |
| 181 | + (cb) => setTimeout(cb, 1000), |
| 182 | + (cb) => goWS.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(goTCPAddrs[0]).getPeerId()}`, cb) |
| 183 | + ], (err) => { |
| 184 | + expect(err).to.not.exist() |
| 185 | + utils.addAndCat(data, |
| 186 | + goWS, |
| 187 | + goTCP, |
| 188 | + (err, data) => { |
| 189 | + expect(err).to.not.exist() |
| 190 | + expect(data).to.be.equal(data) |
| 191 | + done() |
| 192 | + }) |
| 193 | + }) |
| 194 | + }) |
| 195 | + |
| 196 | + it('jsWS <-> goRelay <-> goTCP', function (done) { |
| 197 | + const data = crypto.randomBytes(128) |
| 198 | + console.log(`goRelayAddrs: ${goRelayAddrs.map(a => a.toString())}`) |
| 199 | + series([ |
| 200 | + (cb) => jsWS.swarm.connect(goRelayAddrs[0], cb), |
| 201 | + (cb) => goTCP.swarm.connect(goRelayAddrs[2], cb), |
| 202 | + (cb) => setTimeout(cb, 1000), |
| 203 | + (cb) => goTCP.swarm.connect(`/p2p-circuit/ipfs/${multiaddr(jsWSAddrs[0]).getPeerId()}`, cb) |
| 204 | + ], (err) => { |
| 205 | + expect(err).to.not.exist() |
| 206 | + utils.addAndCat(data, |
| 207 | + jsWS, |
| 208 | + goTCP, |
| 209 | + (err, data) => { |
| 210 | + expect(err).to.not.exist() |
| 211 | + expect(data).to.be.equal(data) |
| 212 | + done() |
| 213 | + }) |
| 214 | + }) |
| 215 | + }) |
| 216 | +}) |
0 commit comments