Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 225a0c0

Browse files
committed
test: add some interop tests for private neworking
1 parent 4bfac3c commit 225a0c0

File tree

4 files changed

+269
-0
lines changed

4 files changed

+269
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ test/test-data/go-ipfs-repo/LOG.old
4040

4141
# while testing npm5
4242
package-lock.json
43+
yarn.lock

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"ipfs-api": "^22.0.0",
5959
"ipfsd-ctl": "~0.37.0",
6060
"left-pad": "^1.3.0",
61+
"libp2p-pnet": "github:libp2p/js-libp2p-pnet#feat/initial",
6162
"libp2p-websocket-star-rendezvous": "~0.2.3",
6263
"lodash": "^4.17.10",
6364
"mocha": "^5.2.0",
@@ -68,6 +69,14 @@
6869
"stream-to-promise": "^2.2.0",
6970
"transform-loader": "^0.2.4"
7071
},
72+
"resolutions": {
73+
"ipfs": "github:ipfs/js-ipfs#feat/priv-net",
74+
"ipfs-repo": "github:ipfs/js-ipfs-repo#feat/pnet",
75+
"ipfsd-ctl": "github:ipfs/js-ipfsd-ctl#master",
76+
"libp2p-pnet": "github:libp2p/js-libp2p-pnet#feat/initial",
77+
"libp2p": "github:libp2p/js-libp2p#feat/priv-net",
78+
"libp2p-switch": "github:libp2p/js-libp2p-switch#feat/priv-net"
79+
},
7180
"dependencies": {},
7281
"contributors": []
7382
}

test/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ require('./repo')
66
require('./exchange-files')
77
require('./kad-dht')
88
require('./pubsub')
9+
require('./pnet')

test/pnet.js

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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

Comments
 (0)