2020-08-25 13:05:58 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const PeerId = require('peer-id')
|
|
|
|
const uint8ArrayFromString = require('uint8arrays/from-string')
|
|
|
|
|
|
|
|
const { utils } = require('..')
|
|
|
|
const PeerStreams = require('../peer-streams')
|
2020-11-11 09:16:49 -07:00
|
|
|
const { SignaturePolicy } = require('../signature-policy')
|
2020-08-25 13:05:58 +02:00
|
|
|
|
|
|
|
const topic = 'foo'
|
|
|
|
const data = uint8ArrayFromString('bar')
|
|
|
|
|
|
|
|
module.exports = (common) => {
|
|
|
|
describe('messages', () => {
|
|
|
|
let pubsub
|
|
|
|
|
|
|
|
// Create pubsub router
|
|
|
|
beforeEach(async () => {
|
|
|
|
[pubsub] = await common.setup(1)
|
|
|
|
pubsub.start()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
sinon.restore()
|
|
|
|
pubsub && pubsub.stop()
|
|
|
|
await common.teardown()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should emit normalized signed messages on publish', async () => {
|
2020-11-11 09:16:49 -07:00
|
|
|
pubsub.globalSignaturePolicy = SignaturePolicy.StrictSign
|
2020-08-25 13:05:58 +02:00
|
|
|
sinon.spy(pubsub, '_emitMessage')
|
|
|
|
|
|
|
|
await pubsub.publish(topic, data)
|
|
|
|
expect(pubsub._emitMessage.callCount).to.eql(1)
|
|
|
|
|
|
|
|
const [messageToEmit] = pubsub._emitMessage.getCall(0).args
|
|
|
|
|
2020-11-11 09:16:49 -07:00
|
|
|
expect(messageToEmit.seqno).to.not.eql(undefined)
|
|
|
|
expect(messageToEmit.key).to.not.eql(undefined)
|
|
|
|
expect(messageToEmit.signature).to.not.eql(undefined)
|
2020-08-25 13:05:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should drop unsigned messages', async () => {
|
|
|
|
sinon.spy(pubsub, '_emitMessage')
|
|
|
|
sinon.spy(pubsub, '_publish')
|
|
|
|
sinon.spy(pubsub, 'validate')
|
|
|
|
|
2020-12-10 14:03:17 +01:00
|
|
|
const peerStream = new PeerStreams({
|
|
|
|
id: await PeerId.create(),
|
|
|
|
protocol: 'test'
|
|
|
|
})
|
2020-08-25 13:05:58 +02:00
|
|
|
const rpc = {
|
|
|
|
subscriptions: [],
|
|
|
|
msgs: [{
|
|
|
|
receivedFrom: peerStream.id.toB58String(),
|
|
|
|
from: peerStream.id.toBytes(),
|
|
|
|
data,
|
|
|
|
seqno: utils.randomSeqno(),
|
|
|
|
topicIDs: [topic]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
pubsub.subscribe(topic)
|
|
|
|
pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(pubsub.validate.callCount).to.eql(1)
|
|
|
|
expect(pubsub._emitMessage.called).to.eql(false)
|
|
|
|
expect(pubsub._publish.called).to.eql(false)
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
}, 50)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not drop unsigned messages if strict signing is disabled', async () => {
|
2020-11-11 09:16:49 -07:00
|
|
|
pubsub.globalSignaturePolicy = SignaturePolicy.StrictNoSign
|
2020-08-25 13:05:58 +02:00
|
|
|
sinon.spy(pubsub, '_emitMessage')
|
|
|
|
sinon.spy(pubsub, '_publish')
|
|
|
|
sinon.spy(pubsub, 'validate')
|
|
|
|
|
2020-12-10 14:03:17 +01:00
|
|
|
const peerStream = new PeerStreams({
|
|
|
|
id: await PeerId.create(),
|
|
|
|
protocol: 'test'
|
|
|
|
})
|
|
|
|
|
2020-08-25 13:05:58 +02:00
|
|
|
const rpc = {
|
|
|
|
subscriptions: [],
|
|
|
|
msgs: [{
|
|
|
|
data,
|
|
|
|
topicIDs: [topic]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
pubsub.subscribe(topic)
|
|
|
|
pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(pubsub.validate.callCount).to.eql(1)
|
|
|
|
expect(pubsub._emitMessage.called).to.eql(true)
|
|
|
|
expect(pubsub._publish.called).to.eql(true)
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
}, 50)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|