Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 074b1fd

Browse files
committed
test: adding circuit tests
1 parent ce504cd commit 074b1fd

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

src/core/components/libp2p.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = function libp2p (self) {
1919
webRTCStar: get(config, 'Discovery.webRTCStar.Enabled'),
2020
bootstrap: get(config, 'Bootstrap'),
2121
dht: get(self._options, 'EXPERIMENTAL.dht'),
22-
modules: self._libp2pModules
22+
modules: self._libp2pModules,
23+
relay: get(config, 'Relay')
2324
}
2425

2526
self._libp2pNode = new Node(self._peerInfo, self._peerInfoBook, options)

test/core/circuit.spec.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
const waterfall = require('async/waterfall')
10+
const parallel = require('async/parallel')
11+
const isNode = require('detect-node')
12+
13+
const IPFS = require('../../src/core')
14+
const createTempRepo = require('../utils/create-repo-node.js')
15+
16+
describe('circuit', () => {
17+
if (!isNode) {
18+
return
19+
}
20+
21+
describe('transfer over circuit', function () {
22+
this.timeout(5000)
23+
24+
let ipfsRelay
25+
let ipfsSrc
26+
let ipfsDst
27+
28+
before((done) => {
29+
ipfsRelay = new IPFS({
30+
repo: createTempRepo(),
31+
config: {
32+
Addresses: {
33+
Swarm: [
34+
'/ip4/0.0.0.0/tcp/9000',
35+
'/ip4/0.0.0.0/tcp/9000/ws'
36+
]
37+
},
38+
Bootstrap: [],
39+
Relay: {
40+
Circuit: {
41+
Enabled: true,
42+
Active: true
43+
}
44+
},
45+
Identity: {
46+
PeerID: 'QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81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1wQkbIgxo9a1NfTSQKBgH6Q0o8tL6i1Epdr3CDD1QKpKWdpL+zNZYPaG3hg4C2XSP7GSKAaT3/OkwjmC/rohTKF8e/i6yujFMQW/Skzfz2aep7VxNXk966gRltXPO5NYBSN8Q+Jg2NAYzdG5YkdFTdgDoT92iGMTbG9BL/c4MRho2ftQd1jZYN7gZbO7kEBAoGARh5eaWCcrfZuyj7mVqN6RbEwjycmMOM21IZn0cDAchvS607XNaIloL+1zJuMXj6iWtQNiMzSa7e/EtY4UL4sRRcGpZdtpxRiGDKVzkTmbjcz1yZlaX3vYYTZOpL2KrRHDcUX0AazUS9SqJSLvFxvmouwY6wBvBov6tStAZjRJvECgYA5XjWN7tHRwEr+NoAGsSmtsqGCrHrCPdj8SOyKTXRARQKRdvJ0KBJkMmzLdmLVErYjuy9OmwdigWcTZjZ2HhrhXNR95Xepg3I5ylzuouMPZepAacjtGl4oi91rdv6z9hD4/cSB+wIfXG9o0ghgJ/vexzvxFzDnvY+8MqEa2PP6NA=='
48+
}
49+
}
50+
})
51+
52+
ipfsSrc = new IPFS({
53+
repo: createTempRepo(),
54+
config: {
55+
Addresses: {
56+
Swarm: [
57+
'/ip4/0.0.0.0/tcp/9001'
58+
]
59+
},
60+
Bootstrap: []
61+
}
62+
})
63+
64+
ipfsDst = new IPFS({
65+
repo: createTempRepo(),
66+
config: {
67+
Addresses: {
68+
Swarm: [
69+
'/ip4/0.0.0.0/tcp/9002/ws'
70+
]
71+
},
72+
Bootstrap: []
73+
}
74+
})
75+
76+
parallel([
77+
(pCb) => ipfsRelay.once('start', pCb),
78+
(pCb) => ipfsSrc.once('start', pCb),
79+
(pCb) => ipfsDst.once('start', pCb)
80+
], () => {
81+
// get everything inter-connected
82+
waterfall([
83+
(cb) => ipfsSrc
84+
.swarm
85+
.connect(`/ip4/0.0.0.0/tcp/9000/ipfs/QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81SoTHcMu1dXBrV`,
86+
() => cb()),
87+
(cb) => ipfsDst
88+
.swarm
89+
.connect(`/ip4/0.0.0.0/tcp/9000/ws/ipfs/QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81SoTHcMu1dXBrV`,
90+
() => cb()),
91+
(cb) => ipfsSrc.id(cb),
92+
(id, cb) => ipfsRelay.swarm.connect(`/ip4/0.0.0.0/tcp/9001/ipfs/${id.id}`, () => cb()),
93+
(cb) => ipfsDst.id(cb),
94+
(id, cb) => ipfsRelay.swarm.connect(`/ip4/0.0.0.0/tcp/9002/ws/ipfs/${id.id}`, () => cb())
95+
], done)
96+
})
97+
})
98+
99+
after((done) => {
100+
waterfall([
101+
(cb) => ipfsRelay.stop(() => cb()),
102+
(cb) => ipfsSrc.stop(() => cb()),
103+
(cb) => ipfsDst.stop(() => cb())
104+
], done)
105+
})
106+
107+
it('should be able to connect over circuit', (done) => {
108+
ipfsDst.id((err, id) => {
109+
expect(err).to.be.null()
110+
ipfsSrc.swarm.connect(id.addresses[0], (err, conn) => {
111+
expect(err).to.be.null()
112+
expect(conn).to.not.be.null()
113+
done()
114+
})
115+
})
116+
})
117+
118+
it('should be able to transfer data over circuit', (done) => {
119+
waterfall([
120+
(cb) => ipfsDst.id(cb),
121+
// dial destination over WS /ip4/0.0.0.0/tcp/9002/ws
122+
(id, cb) => ipfsSrc.swarm.connect(id.addresses[0], cb),
123+
(conn, cb) => ipfsSrc.files.add(new ipfsSrc.types.Buffer('Hello world over circuit!'), (err, res) => {
124+
expect(err).to.be.null()
125+
expect(res[0]).to.not.be.null()
126+
cb(null, res[0].hash)
127+
}),
128+
(fileHash, cb) => ipfsDst.files.cat(fileHash, function (err, stream) {
129+
expect(err).to.be.null()
130+
131+
var res = ''
132+
133+
stream.on('data', function (chunk) {
134+
res += chunk.toString()
135+
})
136+
137+
stream.on('error', function (err) {
138+
cb(err)
139+
})
140+
141+
stream.on('end', function () {
142+
expect(res).to.be.equal('Hello world over circuit!')
143+
cb(null, res)
144+
})
145+
})
146+
], done)
147+
})
148+
})
149+
})

0 commit comments

Comments
 (0)