This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
name.spec.js
471 lines (387 loc) · 13.2 KB
/
name.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* eslint max-nested-callbacks: ["error", 7] */
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const { expect } = require('interface-ipfs-core/src/utils/mocha')
const sinon = require('sinon')
const parallel = require('async/parallel')
const series = require('async/series')
const IPFS = require('../../src')
const ipnsPath = require('../../src/core/ipns/path')
const ipnsRouting = require('../../src/core/ipns/routing/config')
const OfflineDatastore = require('../../src/core/ipns/routing/offline-datastore')
const PubsubDatastore = require('../../src/core/ipns/routing/pubsub-datastore')
const { Key, Errors } = require('interface-datastore')
const CID = require('cids')
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({
type: 'proc',
IpfsClient: require('ipfs-http-client')
})
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
const publishAndResolve = (publisher, resolver, ipfsRef, publishOpts, nodeId, resolveOpts, callback) => {
series([
(cb) => publisher.name.publish(ipfsRef, publishOpts, cb),
(cb) => resolver.name.resolve(nodeId, resolveOpts, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res[0]).to.exist()
expect(res[1]).to.exist()
expect(res[1]).to.equal(ipfsRef)
callback()
})
}
describe('name', function () {
describe('republisher', function () {
this.timeout(40 * 1000)
let node
let ipfsd
before(async function () {
ipfsd = await df.spawn({
exec: IPFS,
args: [`--pass ${hat()}`, '--offline'],
config: { Bootstrap: [] },
preload: { enabled: false }
})
node = ipfsd.api
})
afterEach(() => {
sinon.restore()
})
after(() => {
if (ipfsd) {
return ipfsd.stop()
}
})
it('should republish entries after 60 seconds', function (done) {
this.timeout(120 * 1000)
sinon.spy(node._ipns.republisher, '_republishEntries')
setTimeout(function () {
expect(node._ipns.republisher._republishEntries.calledOnce).to.equal(true)
done()
}, 60 * 1000)
})
it('should error if run republish again', function (done) {
this.timeout(120 * 1000)
sinon.spy(node._ipns.republisher, '_republishEntries')
try {
node._ipns.republisher.start()
} catch (err) {
expect(err).to.exist()
expect(err.code).to.equal('ERR_REPUBLISH_ALREADY_RUNNING') // already runs when starting
done()
}
})
})
// TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994
describe.skip('work with dht', () => {
let nodes
let nodeA
let nodeB
let nodeC
let idA
const createNode = (callback) => {
df.spawn({
exec: IPFS,
args: [`--pass ${hat()}`],
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
}
}, callback)
}
before(function (done) {
this.timeout(70 * 1000)
parallel([
(cb) => createNode(cb),
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, _nodes) => {
expect(err).to.not.exist()
nodes = _nodes
nodeA = _nodes[0].api
nodeB = _nodes[1].api
nodeC = _nodes[2].api
parallel([
(cb) => nodeA.id(cb),
(cb) => nodeB.id(cb)
], (err, ids) => {
expect(err).to.not.exist()
idA = ids[0]
parallel([
(cb) => nodeC.swarm.connect(ids[0].addresses[0], cb), // C => A
(cb) => nodeC.swarm.connect(ids[1].addresses[0], cb), // C => B
(cb) => nodeA.swarm.connect(ids[1].addresses[0], cb) // A => B
], done)
})
})
})
after(function (done) {
this.timeout(80 * 1000)
parallel(nodes.map((node) => (cb) => node.stop(cb)), done)
})
it('should publish and then resolve correctly with the default options', function (done) {
this.timeout(380 * 1000)
publishAndResolve(nodeA, nodeB, ipfsRef, { resolve: false }, idA.id, {}, done)
})
it('should recursively resolve to an IPFS hash', function (done) {
this.timeout(360 * 1000)
const keyName = hat()
nodeA.key.gen(keyName, { type: 'rsa', size: 2048 }, function (err, key) {
expect(err).to.not.exist()
series([
(cb) => nodeA.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => nodeA.name.publish(`/ipns/${idA.id}`, { resolve: false, key: keyName }, cb),
(cb) => nodeB.name.resolve(key.id, { recursive: true }, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res[2]).to.exist()
expect(res[2]).to.equal(ipfsRef)
done()
})
})
})
})
describe('errors', function () {
let node
let nodeId
let ipfsd
before(async function () {
this.timeout(40 * 1000)
ipfsd = await df.spawn({
exec: IPFS,
args: [`--pass ${hat()}`],
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
},
preload: { enabled: false }
})
node = ipfsd.api
const res = await node.id()
nodeId = res.id
})
after(() => {
if (ipfsd) {
return ipfsd.stop()
}
})
it('should error to publish if does not receive private key', function () {
return expect(node._ipns.publisher.publish(null, ipfsRef))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_PRIVATE_KEY')
})
it('should error to publish if an invalid private key is received', function () {
return expect(node._ipns.publisher.publish({ bytes: 'not that valid' }, ipfsRef))
.to.eventually.be.rejected()
// .that.eventually.has.property('code', 'ERR_INVALID_PRIVATE_KEY') TODO: libp2p-crypto needs to throw err-code
})
it('should error to publish if _updateOrCreateRecord fails', async function () {
const err = new Error('error')
const stub = sinon.stub(node._ipns.publisher, '_updateOrCreateRecord').rejects(err)
await expect(node.name.publish(ipfsRef, { resolve: false }))
.to.eventually.be.rejectedWith(err)
stub.restore()
})
it('should error to publish if _putRecordToRouting receives an invalid peer id', function () {
return expect(node._ipns.publisher._putRecordToRouting(undefined, undefined))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_PEER_ID')
})
it('should error to publish if receives an invalid datastore key', async function () {
const stub = sinon.stub(Key, 'isKey').returns(false)
await expect(node.name.publish(ipfsRef, { resolve: false }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_DATASTORE_KEY')
stub.restore()
})
it('should error to publish if we receive a unexpected error getting from datastore', async function () {
const stub = sinon.stub(node._ipns.publisher._datastore, 'get').throws(new Error('error-unexpected'))
await expect(node.name.publish(ipfsRef, { resolve: false }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_DETERMINING_PUBLISHED_RECORD')
stub.restore()
})
it('should error to publish if we receive a unexpected error putting to datastore', async function () {
const stub = sinon.stub(node._ipns.publisher._datastore, 'put').throws(new Error('error-unexpected'))
await expect(node.name.publish(ipfsRef, { resolve: false }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_STORING_IN_DATASTORE')
stub.restore()
})
it('should error to resolve if the received name is not a string', function () {
return expect(node._ipns.resolver.resolve(false))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_NAME')
})
it('should error to resolve if receives an invalid ipns path', function () {
return expect(node._ipns.resolver.resolve('ipns/<cid>'))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_NAME')
})
it('should publish and then fail to resolve if receive error getting from datastore', async function () {
const stub = sinon.stub(node._ipns.resolver._routing, 'get').throws(new Error('error-unexpected'))
await node.name.publish(ipfsRef, { resolve: false })
await expect(node.name.resolve(nodeId, { nocache: true }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_UNEXPECTED_ERROR_GETTING_RECORD')
stub.restore()
})
it('should publish and then fail to resolve if does not find the record', async function () {
const stub = sinon.stub(node._ipns.resolver._routing, 'get').throws(Errors.notFoundError())
await node.name.publish(ipfsRef, { resolve: false })
await expect(node.name.resolve(nodeId, { nocache: true }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_NO_RECORD_FOUND')
stub.restore()
})
it('should publish and then fail to resolve if does not receive a buffer', async function () {
const stub = sinon.stub(node._ipns.resolver._routing, 'get').resolves('not-a-buffer')
await node.name.publish(ipfsRef, { resolve: false })
await expect(node.name.resolve(nodeId, { nocache: true }))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_RECORD_RECEIVED')
stub.restore()
})
})
describe('ipns.path', function () {
const fixture = {
path: 'test/fixtures/planets/solar-system.md',
content: Buffer.from('ipns.path')
}
let node
let ipfsd
let nodeId
before(async function () {
this.timeout(40 * 1000)
ipfsd = await df.spawn({
exec: IPFS,
args: [`--pass ${hat()}`, '--offline'],
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
},
preload: { enabled: false }
})
node = ipfsd.api
const res = await node.id()
nodeId = res.id
})
after(() => {
if (ipfsd) {
return ipfsd.stop()
}
})
it('should resolve an ipfs path correctly', async function () {
const res = await node.add(fixture)
await node.name.publish(`/ipfs/${res[0].hash}`)
const value = await ipnsPath.resolvePath(node, `/ipfs/${res[0].hash}`)
expect(value).to.exist()
})
it('should resolve an ipns path correctly', async function () {
const res = await node.add(fixture)
await node.name.publish(`/ipfs/${res[0].hash}`)
const value = await ipnsPath.resolvePath(node, `/ipns/${nodeId}`)
expect(value).to.exist()
})
it('should resolve an ipns path with PeerID as CIDv1 in Base32 correctly', async function () {
const res = await node.add(fixture)
await node.name.publish(`/ipfs/${res[0].hash}`)
let peerCid = new CID(nodeId)
if (peerCid.version === 0) peerCid = peerCid.toV1() // future-proofing
const value = await ipnsPath.resolvePath(node, `/ipns/${peerCid.toString('base32')}`)
expect(value).to.exist()
})
})
describe('ipns.routing', function () {
it('should use only the offline datastore by default', function (done) {
const ipfs = {}
const config = ipnsRouting(ipfs)
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0] instanceof OfflineDatastore).to.eql(true)
done()
})
it('should use only the offline datastore if offline', function (done) {
const ipfs = {
_options: {
offline: true
}
}
const config = ipnsRouting(ipfs)
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0] instanceof OfflineDatastore).to.eql(true)
done()
})
it('should use the pubsub datastore if enabled', function (done) {
const ipfs = {
libp2p: {
pubsub: {}
},
_peerInfo: {
id: {}
},
_repo: {
datastore: {}
},
_options: {
EXPERIMENTAL: {
ipnsPubsub: true
}
}
}
const config = ipnsRouting(ipfs)
expect(config.stores).to.have.lengthOf(2)
expect(config.stores[0] instanceof PubsubDatastore).to.eql(true)
expect(config.stores[1] instanceof OfflineDatastore).to.eql(true)
done()
})
it('should use the dht if enabled', function (done) {
const dht = {}
const ipfs = {
libp2p: {
dht
},
_peerInfo: {
id: {}
},
_repo: {
datastore: {}
},
_options: {
libp2p: {
config: {
dht: {
enabled: true
}
}
}
}
}
const config = ipnsRouting(ipfs)
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0]).to.eql(dht)
done()
})
})
})