This repository was 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
/
Copy pathname.spec.js
305 lines (254 loc) · 10.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
/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const delay = require('delay')
const { Key } = require('interface-datastore')
const PeerId = require('peer-id')
const errCode = require('err-code')
const ipns = require('ipns')
const getIpnsRoutingConfig = require('../src/ipns/routing/config')
const IpnsPublisher = require('../src/ipns/publisher')
const IpnsRepublisher = require('../src/ipns/republisher')
const IpnsResolver = require('../src/ipns/resolver')
const OfflineDatastore = require('../src/ipns/routing/offline-datastore')
const PubsubDatastore = require('../src/ipns/routing/pubsub-datastore')
const uint8ArrayFromString = require('uint8arrays/from-string')
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
describe('name', function () {
describe('republisher', function () {
this.timeout(120 * 1000)
let republisher
afterEach(async () => {
if (republisher) {
await republisher.stop()
republisher = null
}
})
it('should republish entries', async function () {
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), sinon.stub(), {
initialBroadcastInterval: 200,
broadcastInterval: 500
})
republisher._republishEntries = sinon.stub()
await republisher.start()
expect(republisher._republishEntries.calledOnce).to.equal(false)
// Initial republish should happen after ~200ms
await delay(300)
expect(republisher._republishEntries.calledOnce).to.equal(true)
// Subsequent republishes should happen after ~700
await delay(600)
expect(republisher._republishEntries.calledTwice).to.equal(true)
})
it('should not republish self key twice', async function () {
const mockKeychain = {
listKeys: () => Promise.resolve([{ name: 'self' }])
}
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), mockKeychain, {
initialBroadcastInterval: 100,
broadcastInterval: 1000,
pass: 'pass'
})
republisher._republishEntry = sinon.stub()
await republisher.start()
expect(republisher._republishEntry.calledOnce).to.equal(false)
// Initial republish should happen after ~100ms
await delay(200)
expect(republisher._republishEntry.calledOnce).to.equal(true)
})
it('should error if run republish again', async () => {
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), sinon.stub(), {
initialBroadcastInterval: 50,
broadcastInterval: 100
})
republisher._republishEntries = sinon.stub()
await republisher.start()
await expect(republisher.start())
.to.eventually.be.rejected()
.with.a.property('code').that.equals('ERR_REPUBLISH_ALREADY_RUNNING')
})
})
describe('publisher', () => {
it('should fail to publish if does not receive private key', () => {
const publisher = new IpnsPublisher()
return expect(publisher.publish(null, ipfsRef))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_PRIVATE_KEY')
})
it('should fail to publish if an invalid private key is received', () => {
const publisher = new IpnsPublisher()
return expect(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 fail to publish if _updateOrCreateRecord fails', async () => {
const publisher = new IpnsPublisher()
const err = new Error('error')
const peerId = await PeerId.create()
sinon.stub(publisher, '_updateOrCreateRecord').rejects(err)
return expect(publisher.publish(peerId.privKey, ipfsRef))
.to.eventually.be.rejectedWith(err)
})
it('should fail to publish if _putRecordToRouting receives an invalid peer id', () => {
const publisher = new IpnsPublisher()
return expect(publisher._putRecordToRouting(undefined, undefined))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_PEER_ID')
})
it('should fail to publish if receives an invalid datastore key', async () => {
const routing = {
get: sinon.stub().rejects(errCode(new Error('not found'), 'ERR_NOT_FOUND'))
}
const datastore = {
get: sinon.stub().rejects(errCode(new Error('not found'), 'ERR_NOT_FOUND')),
put: sinon.stub().resolves()
}
const publisher = new IpnsPublisher(routing, datastore)
const peerId = await PeerId.create()
const stub = sinon.stub(Key, 'isKey').returns(false)
await expect(publisher.publish(peerId.privKey, ipfsRef))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_DATASTORE_KEY')
stub.restore()
})
it('should fail to publish if we receive a unexpected error getting from datastore', async () => {
const routing = {}
const datastore = {
get: sinon.stub().rejects(new Error('boom'))
}
const publisher = new IpnsPublisher(routing, datastore)
const peerId = await PeerId.create()
await expect(publisher.publish(peerId.privKey, ipfsRef))
.to.eventually.be.rejected()
.with.property('code', 'ERR_DETERMINING_PUBLISHED_RECORD')
})
it('should fail to publish if we receive a unexpected error putting to datastore', async () => {
const routing = {
get: sinon.stub().rejects(errCode(new Error('not found'), 'ERR_NOT_FOUND'))
}
const datastore = {
get: sinon.stub().rejects(errCode(new Error('not found'), 'ERR_NOT_FOUND')),
put: sinon.stub().rejects(new Error('error-unexpected'))
}
const publisher = new IpnsPublisher(routing, datastore)
const peerId = await PeerId.create()
await expect(publisher.publish(peerId.privKey, ipfsRef))
.to.eventually.be.rejected()
.with.property('code', 'ERR_STORING_IN_DATASTORE')
})
})
describe('resolver', () => {
it('should resolve an inlined public key', async () => {
const peerId = await PeerId.create({ keyType: 'ed25519' })
const value = `/ipfs/${peerId.toB58String()}`
const record = await ipns.create(peerId.privKey, uint8ArrayFromString(value), 1, 10e3)
const routing = {
get: sinon.stub().returns(ipns.marshal(record))
}
const resolver = new IpnsResolver(routing)
const resolved = await resolver.resolve(`/ipns/${peerId.toB58String()}`)
expect(resolved).to.equal(value)
})
it('should fail to resolve if the received name is not a string', () => {
const resolver = new IpnsResolver()
return expect(resolver.resolve(false))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_NAME')
})
it('should fail to resolve if receives an invalid ipns path', () => {
const resolver = new IpnsResolver()
return expect(resolver.resolve('ipns/<cid>'))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_NAME')
})
it('should fail to resolve if receive error getting from datastore', async () => {
const routing = {
get: sinon.stub().rejects(new Error('boom'))
}
const resolver = new IpnsResolver(routing)
const peerId = await PeerId.create()
await expect(resolver.resolve(`/ipns/${peerId.toB58String()}`))
.to.eventually.be.rejected()
.with.property('code', 'ERR_UNEXPECTED_ERROR_GETTING_RECORD')
})
it('should fail to resolve if does not find the record', async () => {
const routing = {
get: sinon.stub().rejects(errCode(new Error('not found'), 'ERR_NOT_FOUND'))
}
const resolver = new IpnsResolver(routing)
const peerId = await PeerId.create()
await expect(resolver.resolve(`/ipns/${peerId.toB58String()}`))
.to.eventually.be.rejected()
.with.property('code', 'ERR_NO_RECORD_FOUND')
})
it('should fail to resolve if does not receive a buffer', async () => {
const routing = {
get: sinon.stub().resolves('not-a-buffer')
}
const resolver = new IpnsResolver(routing)
const peerId = await PeerId.create()
await expect(resolver.resolve(`/ipns/${peerId.toB58String()}`))
.to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_RECORD_RECEIVED')
})
})
describe('routing config', function () {
it('should use only the offline datastore by default', () => {
const config = getIpnsRoutingConfig({
libp2p: sinon.stub(),
repo: sinon.stub(),
peerId: sinon.stub(),
options: {}
})
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0] instanceof OfflineDatastore).to.eql(true)
})
it('should use only the offline datastore if offline', () => {
const config = getIpnsRoutingConfig({
libp2p: sinon.stub(),
repo: sinon.stub(),
peerId: sinon.stub(),
options: {
offline: true
}
})
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0] instanceof OfflineDatastore).to.eql(true)
})
it('should use the pubsub datastore if enabled', async () => {
const peerId = await PeerId.create()
const config = getIpnsRoutingConfig({
libp2p: { pubsub: sinon.stub() },
repo: { datastore: sinon.stub() },
peerId,
options: {
EXPERIMENTAL: {
ipnsPubsub: true
}
}
})
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)
})
it('should use the dht if enabled', () => {
const dht = sinon.stub()
const config = getIpnsRoutingConfig({
libp2p: { _dht: dht },
repo: sinon.stub(),
peerId: sinon.stub(),
options: {
libp2p: {
config: {
dht: {
enabled: true
}
}
}
}
})
expect(config.stores).to.have.lengthOf(1)
expect(config.stores[0]).to.eql(dht)
})
})
})