@libp2p/interface-compliance-tests 1.1.21 → 1.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/pubsub/api.d.ts +2 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +10 -8
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +2 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +41 -27
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +2 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +12 -7
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +2 -3
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts +2 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +8 -66
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +109 -53
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +2 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +40 -55
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +3 -2
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +2 -2
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +2 -4
- package/src/pubsub/api.ts +11 -10
- package/src/pubsub/connection-handlers.ts +53 -39
- package/src/pubsub/emit-self.ts +15 -10
- package/src/pubsub/index.ts +2 -3
- package/src/pubsub/messages.ts +11 -90
- package/src/pubsub/multiple-nodes.ts +120 -66
- package/src/pubsub/two-nodes.ts +47 -66
- package/src/pubsub/utils.ts +5 -5
package/src/pubsub/api.ts
CHANGED
|
@@ -6,18 +6,16 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
6
6
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
7
7
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
8
|
import delay from 'delay'
|
|
9
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
9
|
import type { TestSetup } from '../index.js'
|
|
11
10
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
12
11
|
import type { PubSubArgs } from './index.js'
|
|
13
12
|
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
14
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
15
13
|
import { Components } from '@libp2p/interfaces/components'
|
|
16
14
|
|
|
17
15
|
const topic = 'foo'
|
|
18
16
|
const data = uint8ArrayFromString('bar')
|
|
19
17
|
|
|
20
|
-
export default (common: TestSetup<
|
|
18
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
21
19
|
describe('pubsub api', () => {
|
|
22
20
|
let pubsub: PubSub
|
|
23
21
|
let registrar: Registrar
|
|
@@ -68,19 +66,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
await pubsub.start()
|
|
71
|
-
pubsub.
|
|
69
|
+
pubsub.subscribe(topic)
|
|
70
|
+
pubsub.addEventListener('message', handler)
|
|
72
71
|
|
|
73
72
|
await pWaitFor(() => {
|
|
74
73
|
const topics = pubsub.getTopics()
|
|
75
74
|
return topics.length === 1 && topics[0] === topic
|
|
76
75
|
})
|
|
77
76
|
|
|
78
|
-
pubsub.removeEventListener(
|
|
77
|
+
pubsub.removeEventListener('message', handler)
|
|
78
|
+
pubsub.unsubscribe(topic)
|
|
79
79
|
|
|
80
80
|
await pWaitFor(() => pubsub.getTopics().length === 0)
|
|
81
81
|
|
|
82
82
|
// Publish to guarantee the handler is not called
|
|
83
|
-
pubsub.
|
|
83
|
+
pubsub.publish(topic, data)
|
|
84
84
|
|
|
85
85
|
// handlers are called async
|
|
86
86
|
await delay(100)
|
|
@@ -93,12 +93,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
93
93
|
|
|
94
94
|
await pubsub.start()
|
|
95
95
|
|
|
96
|
-
pubsub.
|
|
97
|
-
|
|
98
|
-
expect(
|
|
96
|
+
pubsub.subscribe(topic)
|
|
97
|
+
pubsub.addEventListener('message', (evt) => {
|
|
98
|
+
expect(evt).to.have.nested.property('detail.topic', topic)
|
|
99
|
+
expect(evt).to.have.deep.nested.property('detail.data', data)
|
|
99
100
|
defer.resolve()
|
|
100
101
|
})
|
|
101
|
-
pubsub.
|
|
102
|
+
pubsub.publish(topic, data)
|
|
102
103
|
await defer.promise
|
|
103
104
|
|
|
104
105
|
await pubsub.stop()
|
|
@@ -6,20 +6,18 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
7
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
8
|
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
9
|
import type { TestSetup } from '../index.js'
|
|
11
|
-
import type { Message } from '@libp2p/interfaces/pubsub'
|
|
10
|
+
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
12
11
|
import type { PubSubArgs } from './index.js'
|
|
13
12
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
14
13
|
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
15
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
16
14
|
import { Components } from '@libp2p/interfaces/components'
|
|
17
15
|
import { start, stop } from '../index.js'
|
|
18
16
|
|
|
19
|
-
export default (common: TestSetup<
|
|
17
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
20
18
|
describe('pubsub connection handlers', () => {
|
|
21
|
-
let psA:
|
|
22
|
-
let psB:
|
|
19
|
+
let psA: PubSub
|
|
20
|
+
let psB: PubSub
|
|
23
21
|
let peerA: PeerId
|
|
24
22
|
let peerB: PeerId
|
|
25
23
|
let registrarA: Registrar
|
|
@@ -74,10 +72,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
74
72
|
|
|
75
73
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
76
74
|
const subscriptionsChanged = Promise.all([
|
|
77
|
-
new Promise((resolve) => psA.addEventListener('
|
|
75
|
+
new Promise((resolve) => psA.addEventListener('subscription-change', resolve, {
|
|
78
76
|
once: true
|
|
79
77
|
})),
|
|
80
|
-
new Promise((resolve) => psB.addEventListener('
|
|
78
|
+
new Promise((resolve) => psB.addEventListener('subscription-change', resolve, {
|
|
81
79
|
once: true
|
|
82
80
|
}))
|
|
83
81
|
])
|
|
@@ -104,8 +102,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
104
102
|
})
|
|
105
103
|
|
|
106
104
|
describe('pubsub started before connect', () => {
|
|
107
|
-
let psA:
|
|
108
|
-
let psB:
|
|
105
|
+
let psA: PubSub
|
|
106
|
+
let psB: PubSub
|
|
109
107
|
let peerA: PeerId
|
|
110
108
|
let peerB: PeerId
|
|
111
109
|
let registrarA: Registrar
|
|
@@ -174,10 +172,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
174
172
|
let subscribedTopics = psA.getTopics()
|
|
175
173
|
expect(subscribedTopics).to.not.include(topic)
|
|
176
174
|
|
|
177
|
-
psA.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
175
|
+
psA.subscribe(topic)
|
|
176
|
+
psA.addEventListener('message', (evt) => {
|
|
177
|
+
if (evt.detail.topic === topic) {
|
|
178
|
+
const msg = evt.detail
|
|
179
|
+
expect(msg.data).to.equalBytes(data)
|
|
180
|
+
defer.resolve()
|
|
181
|
+
}
|
|
181
182
|
})
|
|
182
183
|
psA.subscribe(topic)
|
|
183
184
|
|
|
@@ -189,15 +190,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
189
190
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
190
191
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
191
192
|
})
|
|
192
|
-
|
|
193
|
+
psB.publish(topic, data)
|
|
193
194
|
|
|
194
195
|
await defer.promise
|
|
195
196
|
})
|
|
196
197
|
})
|
|
197
198
|
|
|
198
199
|
describe('pubsub started after connect', () => {
|
|
199
|
-
let psA:
|
|
200
|
-
let psB:
|
|
200
|
+
let psA: PubSub
|
|
201
|
+
let psB: PubSub
|
|
201
202
|
let peerA: PeerId
|
|
202
203
|
let peerB: PeerId
|
|
203
204
|
let registrarA: Registrar
|
|
@@ -275,10 +276,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
275
276
|
let subscribedTopics = psA.getTopics()
|
|
276
277
|
expect(subscribedTopics).to.not.include(topic)
|
|
277
278
|
|
|
278
|
-
psA.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
279
|
+
psA.subscribe(topic)
|
|
280
|
+
psA.addEventListener('message', (evt) => {
|
|
281
|
+
if (evt.detail.topic === topic) {
|
|
282
|
+
const msg = evt.detail
|
|
283
|
+
expect(msg.data).to.equalBytes(data)
|
|
284
|
+
defer.resolve()
|
|
285
|
+
}
|
|
282
286
|
})
|
|
283
287
|
psA.subscribe(topic)
|
|
284
288
|
|
|
@@ -290,15 +294,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
290
294
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
291
295
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
292
296
|
})
|
|
293
|
-
|
|
297
|
+
psB.publish(topic, data)
|
|
294
298
|
|
|
295
299
|
await defer.promise
|
|
296
300
|
})
|
|
297
301
|
})
|
|
298
302
|
|
|
299
303
|
describe('pubsub with intermittent connections', () => {
|
|
300
|
-
let psA:
|
|
301
|
-
let psB:
|
|
304
|
+
let psA: PubSub
|
|
305
|
+
let psB: PubSub
|
|
302
306
|
let peerA: PeerId
|
|
303
307
|
let peerB: PeerId
|
|
304
308
|
let registrarA: Registrar
|
|
@@ -357,11 +361,14 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
357
361
|
let subscribedTopics = psA.getTopics()
|
|
358
362
|
expect(subscribedTopics).to.not.include(topic)
|
|
359
363
|
|
|
360
|
-
psA.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
364
|
+
psA.subscribe(topic)
|
|
365
|
+
psA.addEventListener('message', (evt) => {
|
|
366
|
+
if (evt.detail.topic === topic) {
|
|
367
|
+
const msg = evt.detail
|
|
368
|
+
expect(msg.data).to.equalBytes(data)
|
|
369
|
+
counter++
|
|
370
|
+
counter === 1 ? defer1.resolve() : defer2.resolve()
|
|
371
|
+
}
|
|
365
372
|
})
|
|
366
373
|
psA.subscribe(topic)
|
|
367
374
|
|
|
@@ -373,7 +380,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
373
380
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
374
381
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
375
382
|
})
|
|
376
|
-
|
|
383
|
+
psB.publish(topic, data)
|
|
377
384
|
|
|
378
385
|
await defer1.promise
|
|
379
386
|
|
|
@@ -406,7 +413,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
406
413
|
return subscribedPeers.toString().includes(peerA.toString())
|
|
407
414
|
})
|
|
408
415
|
|
|
409
|
-
|
|
416
|
+
psB.publish(topic, data)
|
|
410
417
|
|
|
411
418
|
await defer2.promise
|
|
412
419
|
})
|
|
@@ -417,8 +424,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
417
424
|
let aReceivedSecondMessageFromB = false
|
|
418
425
|
let bReceivedFirstMessageFromA = false
|
|
419
426
|
let bReceivedSecondMessageFromA = false
|
|
427
|
+
const topic = 'reconnect-channel'
|
|
420
428
|
|
|
421
429
|
const handlerSpyA = (evt: CustomEvent<Message>) => {
|
|
430
|
+
if (evt.detail.topic !== topic) {
|
|
431
|
+
return
|
|
432
|
+
}
|
|
433
|
+
|
|
422
434
|
const message = evt.detail
|
|
423
435
|
const data = uint8ArrayToString(message.data)
|
|
424
436
|
|
|
@@ -431,6 +443,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
431
443
|
}
|
|
432
444
|
}
|
|
433
445
|
const handlerSpyB = (evt: CustomEvent<Message>) => {
|
|
446
|
+
if (evt.detail.topic !== topic) {
|
|
447
|
+
return
|
|
448
|
+
}
|
|
449
|
+
|
|
434
450
|
const message = evt.detail
|
|
435
451
|
const data = uint8ArrayToString(message.data)
|
|
436
452
|
|
|
@@ -443,10 +459,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
443
459
|
}
|
|
444
460
|
}
|
|
445
461
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
psA.addEventListener(topic, handlerSpyA)
|
|
449
|
-
psB.addEventListener(topic, handlerSpyB)
|
|
462
|
+
psA.addEventListener('message', handlerSpyA)
|
|
463
|
+
psB.addEventListener('message', handlerSpyB)
|
|
450
464
|
psA.subscribe(topic)
|
|
451
465
|
psB.subscribe(topic)
|
|
452
466
|
|
|
@@ -470,8 +484,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
470
484
|
})
|
|
471
485
|
|
|
472
486
|
// Verify messages go both ways
|
|
473
|
-
|
|
474
|
-
|
|
487
|
+
psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
|
|
488
|
+
psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
|
|
475
489
|
await pWaitFor(() => {
|
|
476
490
|
return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
|
|
477
491
|
})
|
|
@@ -484,8 +498,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
484
498
|
await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
|
|
485
499
|
|
|
486
500
|
// Verify messages go both ways after the disconnect
|
|
487
|
-
|
|
488
|
-
|
|
501
|
+
psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
|
|
502
|
+
psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
|
|
489
503
|
await pWaitFor(() => {
|
|
490
504
|
return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
|
|
491
505
|
})
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -3,20 +3,19 @@ import sinon from 'sinon'
|
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
5
5
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
6
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
7
6
|
import type { TestSetup } from '../index.js'
|
|
8
7
|
import type { PubSubArgs } from './index.js'
|
|
9
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
10
8
|
import { Components } from '@libp2p/interfaces/components'
|
|
11
9
|
import { start, stop } from '../index.js'
|
|
10
|
+
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
12
11
|
|
|
13
12
|
const topic = 'foo'
|
|
14
13
|
const data = uint8ArrayFromString('bar')
|
|
15
14
|
const shouldNotHappen = () => expect.fail()
|
|
16
15
|
|
|
17
|
-
export default (common: TestSetup<
|
|
16
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
18
17
|
describe('emit self', () => {
|
|
19
|
-
let pubsub:
|
|
18
|
+
let pubsub: PubSub
|
|
20
19
|
|
|
21
20
|
describe('enabled', () => {
|
|
22
21
|
before(async () => {
|
|
@@ -43,11 +42,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
43
42
|
})
|
|
44
43
|
|
|
45
44
|
it('should emit to self on publish', async () => {
|
|
46
|
-
const promise = new Promise((resolve) =>
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
const promise = new Promise<void>((resolve) => {
|
|
46
|
+
pubsub.addEventListener('message', (evt) => {
|
|
47
|
+
if (evt.detail.topic === topic) {
|
|
48
|
+
resolve()
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
once: true
|
|
52
|
+
})
|
|
53
|
+
})
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
pubsub.publish(topic, data)
|
|
51
56
|
|
|
52
57
|
return await promise
|
|
53
58
|
})
|
|
@@ -78,11 +83,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
78
83
|
})
|
|
79
84
|
|
|
80
85
|
it('should not emit to self on publish', async () => {
|
|
81
|
-
pubsub.addEventListener(
|
|
86
|
+
pubsub.addEventListener('message', shouldNotHappen, {
|
|
82
87
|
once: true
|
|
83
88
|
})
|
|
84
89
|
|
|
85
|
-
|
|
90
|
+
pubsub.publish(topic, data)
|
|
86
91
|
|
|
87
92
|
// Wait 1 second to guarantee that self is not noticed
|
|
88
93
|
return await new Promise((resolve) => setTimeout(resolve, 1000))
|
package/src/pubsub/index.ts
CHANGED
|
@@ -5,8 +5,7 @@ import connectionHandlersTest from './connection-handlers.js'
|
|
|
5
5
|
import twoNodesTest from './two-nodes.js'
|
|
6
6
|
import multipleNodesTest from './multiple-nodes.js'
|
|
7
7
|
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type { PubSubInit } from '@libp2p/interfaces/pubsub'
|
|
9
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
8
|
+
import type { PubSub, PubSubInit } from '@libp2p/interfaces/pubsub'
|
|
10
9
|
import type { Components } from '@libp2p/interfaces/components'
|
|
11
10
|
|
|
12
11
|
export interface PubSubArgs {
|
|
@@ -14,7 +13,7 @@ export interface PubSubArgs {
|
|
|
14
13
|
init: PubSubInit
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export default (common: TestSetup<
|
|
16
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
18
17
|
describe('interface-pubsub compliance tests', () => {
|
|
19
18
|
apiTest(common)
|
|
20
19
|
emitSelfTest(common)
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -2,28 +2,22 @@ import { expect } from 'aegir/chai'
|
|
|
2
2
|
import sinon from 'sinon'
|
|
3
3
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
|
-
import { noSignMsgId } from '@libp2p/pubsub/utils'
|
|
6
|
-
import { PeerStreams } from '@libp2p/pubsub/peer-streams'
|
|
7
5
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
8
|
-
import pDefer from 'p-defer'
|
|
9
|
-
import delay from 'delay'
|
|
10
|
-
import pWaitFor from 'p-wait-for'
|
|
11
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
12
6
|
import type { TestSetup } from '../index.js'
|
|
13
|
-
import type {
|
|
7
|
+
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
14
8
|
import type { PubSubArgs } from './index.js'
|
|
15
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
16
9
|
import { Components } from '@libp2p/interfaces/components'
|
|
17
10
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
18
11
|
import { start, stop } from '../index.js'
|
|
12
|
+
import { pEvent } from 'p-event'
|
|
19
13
|
|
|
20
14
|
const topic = 'foo'
|
|
21
15
|
const data = uint8ArrayFromString('bar')
|
|
22
16
|
|
|
23
|
-
export default (common: TestSetup<
|
|
17
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
24
18
|
describe('messages', () => {
|
|
25
19
|
let peerId: PeerId
|
|
26
|
-
let pubsub:
|
|
20
|
+
let pubsub: PubSub
|
|
27
21
|
|
|
28
22
|
// Create pubsub router
|
|
29
23
|
beforeEach(async () => {
|
|
@@ -49,88 +43,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
49
43
|
|
|
50
44
|
it('should emit normalized signed messages on publish', async () => {
|
|
51
45
|
pubsub.globalSignaturePolicy = 'StrictSign'
|
|
46
|
+
pubsub.publish(topic, data)
|
|
52
47
|
|
|
53
|
-
const
|
|
48
|
+
const event = await pEvent<'message', CustomEvent<Message>>(pubsub, 'message')
|
|
49
|
+
const message = event.detail
|
|
54
50
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
expect(spy).to.have.property('callCount', 1)
|
|
62
|
-
|
|
63
|
-
const [from, messageToEmit] = spy.getCall(0).args
|
|
64
|
-
|
|
65
|
-
expect(from.toString()).to.equal(peerId.toString())
|
|
66
|
-
expect(messageToEmit.sequenceNumber).to.not.eql(undefined)
|
|
67
|
-
expect(messageToEmit.key).to.not.eql(undefined)
|
|
68
|
-
expect(messageToEmit.signature).to.not.eql(undefined)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it('should drop unsigned messages', async () => {
|
|
72
|
-
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
73
|
-
sinon.spy(pubsub, 'validate')
|
|
74
|
-
|
|
75
|
-
const peerStream = new PeerStreams({
|
|
76
|
-
id: await createEd25519PeerId(),
|
|
77
|
-
protocol: 'test'
|
|
78
|
-
})
|
|
79
|
-
const rpc: PubSubRPC = {
|
|
80
|
-
subscriptions: [],
|
|
81
|
-
messages: [{
|
|
82
|
-
from: peerStream.id.toBytes(),
|
|
83
|
-
data,
|
|
84
|
-
sequenceNumber: await noSignMsgId(data),
|
|
85
|
-
topic: topic
|
|
86
|
-
}]
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
pubsub.subscribe(topic)
|
|
90
|
-
|
|
91
|
-
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
92
|
-
|
|
93
|
-
// message should not be delivered
|
|
94
|
-
await delay(1000)
|
|
95
|
-
|
|
96
|
-
expect(publishSpy).to.have.property('called', false)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('should not drop unsigned messages if strict signing is disabled', async () => {
|
|
100
|
-
pubsub.globalSignaturePolicy = 'StrictNoSign'
|
|
101
|
-
|
|
102
|
-
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
103
|
-
sinon.spy(pubsub, 'validate')
|
|
104
|
-
|
|
105
|
-
const peerStream = new PeerStreams({
|
|
106
|
-
id: await createEd25519PeerId(),
|
|
107
|
-
protocol: 'test'
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
const rpc: PubSubRPC = {
|
|
111
|
-
subscriptions: [],
|
|
112
|
-
messages: [{
|
|
113
|
-
from: peerStream.id.toBytes(),
|
|
114
|
-
data,
|
|
115
|
-
topic
|
|
116
|
-
}]
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
pubsub.subscribe(topic)
|
|
120
|
-
|
|
121
|
-
const deferred = pDefer()
|
|
122
|
-
|
|
123
|
-
pubsub.addEventListener(topic, () => {
|
|
124
|
-
deferred.resolve()
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
128
|
-
|
|
129
|
-
// await message delivery
|
|
130
|
-
await deferred.promise
|
|
131
|
-
|
|
132
|
-
expect(pubsub.validate).to.have.property('callCount', 1)
|
|
133
|
-
expect(publishSpy).to.have.property('callCount', 1)
|
|
51
|
+
expect(message.from.toString()).to.equal(peerId.toString())
|
|
52
|
+
expect(message.sequenceNumber).to.not.eql(undefined)
|
|
53
|
+
expect(message.key).to.not.eql(undefined)
|
|
54
|
+
expect(message.signature).to.not.eql(undefined)
|
|
134
55
|
})
|
|
135
56
|
})
|
|
136
57
|
}
|