|
| 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 | +chai.use(dirtyChai) |
| 9 | + |
| 10 | +const parallel = require('async/parallel') |
| 11 | +const series = require('async/series') |
| 12 | +const waterfall = require('async/waterfall') |
| 13 | +const multiaddr = require('multiaddr') |
| 14 | +const crypto = require('crypto') |
| 15 | +const IPFS = require('../../src') |
| 16 | + |
| 17 | +const DaemonFactory = require('ipfsd-ctl') |
| 18 | +const procDf = DaemonFactory.create({ type: 'proc', exec: IPFS }) |
| 19 | + |
| 20 | +const baseConf = { |
| 21 | + Bootstrap: [], |
| 22 | + Addresses: { |
| 23 | + API: '/ip4/0.0.0.0/tcp/0', |
| 24 | + Gateway: '/ip4/0.0.0.0/tcp/0' |
| 25 | + }, |
| 26 | + Discovery: { |
| 27 | + MDNS: { |
| 28 | + Enabled: |
| 29 | + false |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function setupInProcNode (addrs, hop, callback) { |
| 35 | + if (typeof hop === 'function') { |
| 36 | + callback = hop |
| 37 | + hop = false |
| 38 | + } |
| 39 | + |
| 40 | + procDf.spawn({ |
| 41 | + config: Object.assign({}, baseConf, { |
| 42 | + Addresses: { |
| 43 | + Swarm: addrs |
| 44 | + }, |
| 45 | + EXPERIMENTAL: { |
| 46 | + relay: { |
| 47 | + enabled: true, |
| 48 | + hop: { |
| 49 | + enabled: hop |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + }, (err, ipfsd) => { |
| 55 | + expect(err).to.not.exist() |
| 56 | + ipfsd.api.id((err, id) => { |
| 57 | + callback(err, { ipfsd, addrs: id.addresses }) |
| 58 | + }) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +const wsAddr = (addrs) => addrs.map((a) => a.toString()).find((a) => a.includes('/ws')) |
| 63 | +const tcpAddr = (addrs) => addrs.map((a) => a.toString()).find((a) => !a.includes('/ws')) |
| 64 | + |
| 65 | +describe(`A <-> R <-> B`, function () { |
| 66 | + this.timeout(80 * 1000) |
| 67 | + |
| 68 | + let nodeA |
| 69 | + let nodeAAddr |
| 70 | + let nodeB |
| 71 | + let nodeBAddr |
| 72 | + let nodeBCircuitAddr |
| 73 | + |
| 74 | + let relayNode |
| 75 | + |
| 76 | + let nodes |
| 77 | + before(function (done) { |
| 78 | + parallel([ |
| 79 | + (cb) => setupInProcNode([ |
| 80 | + '/ip4/0.0.0.0/tcp/0', |
| 81 | + '/ip4/0.0.0.0/tcp/0/ws' |
| 82 | + ], true, cb), |
| 83 | + (cb) => setupInProcNode(['/ip4/0.0.0.0/tcp/0'], cb), |
| 84 | + (cb) => setupInProcNode(['/ip4/0.0.0.0/tcp/0/ws'], cb) |
| 85 | + ], function (err, res) { |
| 86 | + expect(err).to.not.exist() |
| 87 | + nodes = res.map((node) => node.ipfsd) |
| 88 | + |
| 89 | + relayNode = res[0].ipfsd |
| 90 | + |
| 91 | + nodeAAddr = tcpAddr(res[1].addrs) |
| 92 | + nodeA = res[0].ipfsd.api |
| 93 | + |
| 94 | + nodeBAddr = wsAddr(res[2].addrs) |
| 95 | + |
| 96 | + nodeB = res[1].ipfsd.api |
| 97 | + nodeBCircuitAddr = `/p2p-circuit/ipfs/${multiaddr(nodeBAddr).getPeerId()}` |
| 98 | + |
| 99 | + done() |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done)) |
| 104 | + |
| 105 | + it('should connect', function (done) { |
| 106 | + series([ |
| 107 | + (cb) => relayNode.api.swarm.connect(nodeAAddr, cb), |
| 108 | + (cb) => setTimeout(cb, 1000), |
| 109 | + (cb) => relayNode.api.swarm.connect(nodeBAddr, cb), |
| 110 | + (cb) => setTimeout(cb, 1000), |
| 111 | + (cb) => nodeA.swarm.connect(nodeBCircuitAddr, cb) |
| 112 | + ], done) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should transfer', function (done) { |
| 116 | + const data = crypto.randomBytes(128) |
| 117 | + waterfall([ |
| 118 | + (cb) => nodeA.files.add(data, cb), |
| 119 | + (res, cb) => nodeB.files.cat(res[0].hash, cb), |
| 120 | + (buffer, cb) => { |
| 121 | + expect(buffer).to.deep.equal(data) |
| 122 | + cb() |
| 123 | + } |
| 124 | + ], done) |
| 125 | + }) |
| 126 | +}) |
0 commit comments