|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const expect = chai.expect |
| 7 | +chai.use(dirtyChai) |
| 8 | + |
| 9 | +const series = require('async/series') |
| 10 | +const parallel = require('async/parallel') |
| 11 | +const waterfall = require('async/waterfall') |
| 12 | +const crypto = require('crypto') |
| 13 | +const os = require('os') |
| 14 | +const path = require('path') |
| 15 | +const hat = require('hat') |
| 16 | +const fs = require('fs') |
| 17 | +const writeKey = require('libp2p-pnet').generate |
| 18 | + |
| 19 | +const DaemonFactory = require('ipfsd-ctl') |
| 20 | +const goDf = DaemonFactory.create() |
| 21 | +const jsDf = DaemonFactory.create({ type: 'js' }) |
| 22 | + |
| 23 | +// Create network keys |
| 24 | +const networkAKey = Buffer.alloc(95) |
| 25 | +const networkBKey = Buffer.alloc(95) |
| 26 | +writeKey(networkAKey) |
| 27 | +writeKey(networkBKey) |
| 28 | + |
| 29 | +const config = { |
| 30 | + Bootstrap: [], |
| 31 | + Discovery: { |
| 32 | + MDNS: { |
| 33 | + Enabled: false |
| 34 | + }, |
| 35 | + webRTCStar: { |
| 36 | + Enabled: false |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const goSameNetworkRepoPath = path.join(os.tmpdir(), hat()) |
| 42 | +const goDiffNetworkRepoPath = path.join(os.tmpdir(), hat()) |
| 43 | +const jsPrivateRepoPath = path.join(os.tmpdir(), hat()) |
| 44 | +const jsSameNetworkRepoPath = path.join(os.tmpdir(), hat()) |
| 45 | +const jsDiffNetworkRepoPath = path.join(os.tmpdir(), hat()) |
| 46 | + |
| 47 | +function startIpfsNode (daemonSpawner, repoPath, key, callback) { |
| 48 | + let daemon |
| 49 | + |
| 50 | + series([ |
| 51 | + (cb) => daemonSpawner.spawn({ |
| 52 | + disposable: false, |
| 53 | + repoPath: repoPath, |
| 54 | + config: config |
| 55 | + }, (err, node) => { |
| 56 | + daemon = node |
| 57 | + cb(err) |
| 58 | + }), |
| 59 | + (cb) => daemon.init(cb), |
| 60 | + (cb) => fs.writeFile(path.join(repoPath, 'swarm.key'), key, cb), |
| 61 | + (cb) => daemon.start(cb) |
| 62 | + ], (err) => { |
| 63 | + callback(err, daemon) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +describe('Private network', function () { |
| 68 | + this.timeout(30 * 1000) |
| 69 | + |
| 70 | + let goSameNetworkDaemon |
| 71 | + let goDiffNetworkDaemon |
| 72 | + let jsPrivateDaemon |
| 73 | + let jsSameNetworkDaemon |
| 74 | + let jsDiffNetworkDaemon |
| 75 | + // let goPublicNetworkDaemon |
| 76 | + // let jsPublicNetworkDaemon |
| 77 | + |
| 78 | + before('start the nodes', function (done) { |
| 79 | + this.timeout(45 * 1000) |
| 80 | + parallel([ |
| 81 | + // Create and start the 3 private nodes |
| 82 | + (cb) => startIpfsNode(goDf, goSameNetworkRepoPath, networkAKey, cb), |
| 83 | + (cb) => startIpfsNode(goDf, goDiffNetworkRepoPath, networkBKey, cb), |
| 84 | + (cb) => startIpfsNode(jsDf, jsPrivateRepoPath, networkAKey, cb), |
| 85 | + (cb) => startIpfsNode(jsDf, jsSameNetworkRepoPath, networkAKey, cb), |
| 86 | + (cb) => startIpfsNode(jsDf, jsDiffNetworkRepoPath, networkBKey, cb) |
| 87 | + // Create and start 1 public go node |
| 88 | + // (cb) => goDf.spawn((err, daemon) => { |
| 89 | + // if (err) { |
| 90 | + // return cb(err) |
| 91 | + // } |
| 92 | + |
| 93 | + // goPublicNetworkDaemon = daemon |
| 94 | + // goPublicNetworkDaemon.start(cb) |
| 95 | + // }) |
| 96 | + ], (err, nodes) => { |
| 97 | + goSameNetworkDaemon = nodes[0] |
| 98 | + goDiffNetworkDaemon = nodes[1] |
| 99 | + jsPrivateDaemon = nodes[2] |
| 100 | + jsSameNetworkDaemon = nodes[3] |
| 101 | + jsDiffNetworkDaemon = nodes[4] |
| 102 | + done(err) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + after((done) => { |
| 107 | + series([ |
| 108 | + (cb) => goSameNetworkDaemon.stop(cb), |
| 109 | + (cb) => goDiffNetworkDaemon.stop(cb), |
| 110 | + (cb) => jsPrivateDaemon.stop(cb), |
| 111 | + (cb) => jsSameNetworkDaemon.stop(cb), |
| 112 | + (cb) => jsDiffNetworkDaemon.stop(cb) |
| 113 | + // (cb) => jsD.stop(cb) |
| 114 | + ], done) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('js <-> js on the same private network', () => { |
| 118 | + let jsId |
| 119 | + let jsId2 |
| 120 | + |
| 121 | + before('should be able to connect js <-> js', function (done) { |
| 122 | + this.timeout(20 * 1000) |
| 123 | + |
| 124 | + series([ |
| 125 | + (cb) => parallel([ |
| 126 | + (cb) => jsPrivateDaemon.api.id(cb), |
| 127 | + (cb) => jsSameNetworkDaemon.api.id(cb) |
| 128 | + ], (err, ids) => { |
| 129 | + expect(err).to.not.exist() |
| 130 | + jsId = ids[0] |
| 131 | + jsId2 = ids[1] |
| 132 | + cb() |
| 133 | + }), |
| 134 | + (cb) => jsPrivateDaemon.api.swarm.connect(jsId2.addresses[0], cb), |
| 135 | + (cb) => jsSameNetworkDaemon.api.swarm.connect(jsId.addresses[0], cb), |
| 136 | + (cb) => parallel([ |
| 137 | + (cb) => jsPrivateDaemon.api.swarm.peers(cb), |
| 138 | + (cb) => jsSameNetworkDaemon.api.swarm.peers(cb) |
| 139 | + ], (err, peers) => { |
| 140 | + expect(err).to.not.exist() |
| 141 | + expect(peers[0].map((p) => p.peer.toB58String())).to.include(jsId2.id) |
| 142 | + expect(peers[1].map((p) => p.peer.toB58String())).to.include(jsId.id) |
| 143 | + cb() |
| 144 | + }) |
| 145 | + ], done) |
| 146 | + }) |
| 147 | + |
| 148 | + after('disconnect the nodes', (done) => { |
| 149 | + jsPrivateDaemon.api.swarm.disconnect(jsId2.addresses[0], done) |
| 150 | + }) |
| 151 | + |
| 152 | + it('should be able to fetch data from js via js', (done) => { |
| 153 | + const data = crypto.randomBytes(1024) |
| 154 | + waterfall([ |
| 155 | + (cb) => jsSameNetworkDaemon.api.add(data, cb), |
| 156 | + (res, cb) => jsPrivateDaemon.api.cat(res[0].hash, cb) |
| 157 | + ], (err, file) => { |
| 158 | + expect(err).to.not.exist() |
| 159 | + expect(file).to.be.eql(data) |
| 160 | + done() |
| 161 | + }) |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + describe('go <-> js on the same private network', () => { |
| 166 | + let jsId |
| 167 | + let goId |
| 168 | + |
| 169 | + before('should be able to connect go <-> js', function (done) { |
| 170 | + this.timeout(20 * 1000) |
| 171 | + |
| 172 | + series([ |
| 173 | + (cb) => parallel([ |
| 174 | + (cb) => jsPrivateDaemon.api.id(cb), |
| 175 | + (cb) => goSameNetworkDaemon.api.id(cb) |
| 176 | + ], (err, ids) => { |
| 177 | + expect(err).to.not.exist() |
| 178 | + jsId = ids[0] |
| 179 | + goId = ids[1] |
| 180 | + cb() |
| 181 | + }), |
| 182 | + (cb) => goSameNetworkDaemon.api.swarm.connect(jsId.addresses[0], cb), |
| 183 | + (cb) => jsPrivateDaemon.api.swarm.connect(goId.addresses[0], cb), |
| 184 | + (cb) => parallel([ |
| 185 | + (cb) => goSameNetworkDaemon.api.swarm.peers(cb), |
| 186 | + (cb) => jsPrivateDaemon.api.swarm.peers(cb) |
| 187 | + ], (err, peers) => { |
| 188 | + expect(err).to.not.exist() |
| 189 | + expect(peers[0].map((p) => p.peer.toB58String())).to.include(jsId.id) |
| 190 | + expect(peers[1].map((p) => p.peer.toB58String())).to.include(goId.id) |
| 191 | + cb() |
| 192 | + }) |
| 193 | + ], done) |
| 194 | + }) |
| 195 | + |
| 196 | + after('disconnect the nodes', (done) => { |
| 197 | + jsPrivateDaemon.api.swarm.disconnect(goId.addresses[0], done) |
| 198 | + }) |
| 199 | + |
| 200 | + it('should be able to fetch data from go via js', (done) => { |
| 201 | + const data = crypto.randomBytes(1024) |
| 202 | + waterfall([ |
| 203 | + (cb) => goSameNetworkDaemon.api.add(data, cb), |
| 204 | + (res, cb) => jsPrivateDaemon.api.cat(res[0].hash, cb) |
| 205 | + ], (err, file) => { |
| 206 | + expect(err).to.not.exist() |
| 207 | + expect(file).to.be.eql(data) |
| 208 | + done() |
| 209 | + }) |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + describe('go <-> js on different private networks', () => { |
| 214 | + it('should NOT be able to connect go <-> js', function (done) { |
| 215 | + this.timeout(20 * 1000) |
| 216 | + let goId |
| 217 | + |
| 218 | + series([ |
| 219 | + (cb) => parallel([ |
| 220 | + (cb) => jsPrivateDaemon.api.id(cb), |
| 221 | + (cb) => goDiffNetworkDaemon.api.id(cb) |
| 222 | + ], (err, ids) => { |
| 223 | + expect(err).to.not.exist() |
| 224 | + goId = ids[1] |
| 225 | + cb() |
| 226 | + }), |
| 227 | + (cb) => jsPrivateDaemon.api.swarm.connect(goId.addresses[0], cb) |
| 228 | + ], (err) => { |
| 229 | + console.log(err) |
| 230 | + expect(err).to.exist() |
| 231 | + done() |
| 232 | + }) |
| 233 | + }) |
| 234 | + }) |
| 235 | + |
| 236 | + describe('js <-> js on different private networks', () => { |
| 237 | + it('should NOT be able to connect js <-> js', function (done) { |
| 238 | + this.timeout(20 * 1000) |
| 239 | + let jsDiffId |
| 240 | + |
| 241 | + series([ |
| 242 | + (cb) => parallel([ |
| 243 | + (cb) => jsPrivateDaemon.api.id(cb), |
| 244 | + (cb) => jsDiffNetworkDaemon.api.id(cb) |
| 245 | + ], (err, ids) => { |
| 246 | + expect(err).to.not.exist() |
| 247 | + jsDiffId = ids[1] |
| 248 | + cb() |
| 249 | + }), |
| 250 | + (cb) => jsPrivateDaemon.api.swarm.connect(jsDiffId.addresses[0], cb) |
| 251 | + ], (err) => { |
| 252 | + console.log(err) |
| 253 | + expect(err).to.exist() |
| 254 | + done() |
| 255 | + }) |
| 256 | + }) |
| 257 | + }) |
| 258 | +}) |
0 commit comments