|
| 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 series = require('async/series') |
| 8 | +const DaemonFactory = require('ipfsd-ctl') |
| 9 | +const ipfsExec = require('../utils/ipfs-exec') |
| 10 | + |
| 11 | +const df = DaemonFactory.create({ type: 'js' }) |
| 12 | +const expect = chai.expect |
| 13 | +chai.use(dirtyChai) |
| 14 | + |
| 15 | +const config = { |
| 16 | + Bootstrap: [], |
| 17 | + Discovery: { |
| 18 | + MDNS: { |
| 19 | + Enabled: |
| 20 | + false |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('ping', function () { |
| 26 | + this.timeout(60 * 1000) |
| 27 | + let ipfsdA |
| 28 | + let ipfsdB |
| 29 | + let bMultiaddr |
| 30 | + let ipfsdBId |
| 31 | + let cli |
| 32 | + |
| 33 | + before((done) => { |
| 34 | + this.timeout(60 * 1000) |
| 35 | + series([ |
| 36 | + (cb) => { |
| 37 | + df.spawn({ |
| 38 | + exec: `./src/cli/bin.js`, |
| 39 | + config, |
| 40 | + initOptions: { bits: 512 } |
| 41 | + }, (err, _ipfsd) => { |
| 42 | + expect(err).to.not.exist() |
| 43 | + ipfsdB = _ipfsd |
| 44 | + cb() |
| 45 | + }) |
| 46 | + }, |
| 47 | + (cb) => { |
| 48 | + ipfsdB.api.id((err, peerInfo) => { |
| 49 | + expect(err).to.not.exist() |
| 50 | + ipfsdBId = peerInfo.id |
| 51 | + bMultiaddr = peerInfo.addresses[0] |
| 52 | + cb() |
| 53 | + }) |
| 54 | + } |
| 55 | + ], done) |
| 56 | + }) |
| 57 | + |
| 58 | + before(function (done) { |
| 59 | + this.timeout(60 * 1000) |
| 60 | + |
| 61 | + df.spawn({ |
| 62 | + exec: './src/cli/bin.js', |
| 63 | + config, |
| 64 | + initoptions: { bits: 512 } |
| 65 | + }, (err, _ipfsd) => { |
| 66 | + expect(err).to.not.exist() |
| 67 | + ipfsdA = _ipfsd |
| 68 | + // Without DHT we need to have an already established connection |
| 69 | + ipfsdA.api.swarm.connect(bMultiaddr, done) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + before((done) => { |
| 74 | + this.timeout(60 * 1000) |
| 75 | + cli = ipfsExec(ipfsdA.repoPath) |
| 76 | + done() |
| 77 | + }) |
| 78 | + |
| 79 | + after((done) => ipfsdA.stop(done)) |
| 80 | + after((done) => ipfsdB.stop(done)) |
| 81 | + |
| 82 | + it('ping host', (done) => { |
| 83 | + this.timeout(60 * 1000) |
| 84 | + const ping = cli(`ping ${ipfsdBId}`) |
| 85 | + const result = [] |
| 86 | + ping.stdout.on('data', (output) => { |
| 87 | + const packets = output.toString().split('\n').slice(0, -1) |
| 88 | + result.push(...packets) |
| 89 | + }) |
| 90 | + |
| 91 | + ping.stdout.on('end', () => { |
| 92 | + expect(result).to.have.lengthOf(12) |
| 93 | + expect(result[0]).to.equal(`PING ${ipfsdBId}`) |
| 94 | + for (let i = 1; i < 11; i++) { |
| 95 | + expect(result[i]).to.match(/^Pong received: time=\d+ ms$/) |
| 96 | + } |
| 97 | + expect(result[11]).to.match(/^Average latency: \d+(.\d+)?ms$/) |
| 98 | + done() |
| 99 | + }) |
| 100 | + |
| 101 | + ping.catch((err) => { |
| 102 | + expect(err).to.not.exist() |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('ping host with --n option', (done) => { |
| 107 | + this.timeout(60 * 1000) |
| 108 | + const ping = cli(`ping --n 1 ${ipfsdBId}`) |
| 109 | + const result = [] |
| 110 | + ping.stdout.on('data', (output) => { |
| 111 | + const packets = output.toString().split('\n').slice(0, -1) |
| 112 | + result.push(...packets) |
| 113 | + }) |
| 114 | + |
| 115 | + ping.stdout.on('end', () => { |
| 116 | + expect(result).to.have.lengthOf(3) |
| 117 | + expect(result[0]).to.equal(`PING ${ipfsdBId}`) |
| 118 | + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) |
| 119 | + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) |
| 120 | + done() |
| 121 | + }) |
| 122 | + |
| 123 | + ping.catch((err) => { |
| 124 | + expect(err).to.not.exist() |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + it('ping host with --count option', (done) => { |
| 129 | + this.timeout(60 * 1000) |
| 130 | + const ping = cli(`ping --count 1 ${ipfsdBId}`) |
| 131 | + const result = [] |
| 132 | + ping.stdout.on('data', (output) => { |
| 133 | + const packets = output.toString().split('\n').slice(0, -1) |
| 134 | + result.push(...packets) |
| 135 | + }) |
| 136 | + |
| 137 | + ping.stdout.on('end', () => { |
| 138 | + expect(result).to.have.lengthOf(3) |
| 139 | + expect(result[0]).to.equal(`PING ${ipfsdBId}`) |
| 140 | + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) |
| 141 | + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) |
| 142 | + done() |
| 143 | + }) |
| 144 | + |
| 145 | + ping.catch((err) => { |
| 146 | + expect(err).to.not.exist() |
| 147 | + }) |
| 148 | + }) |
| 149 | +}) |
0 commit comments