js-libp2p/test/peer-store/key-book.spec.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-04-28 15:03:16 +02:00
'use strict'
/* eslint-env mocha */
2021-04-27 17:13:34 +02:00
const { expect } = require('aegir/utils/chai')
2020-04-28 15:03:16 +02:00
const sinon = require('sinon')
const { MemoryDatastore } = require('datastore-core/memory')
2020-04-28 15:03:16 +02:00
const PeerStore = require('../../src/peer-store')
const pDefer = require('p-defer')
2020-04-28 15:03:16 +02:00
const peerUtils = require('../utils/creators/peer')
const {
codes: { ERR_INVALID_PARAMETERS }
} = require('../../src/errors')
/**
* @typedef {import('../../src/peer-store/types').PeerStore} PeerStore
* @typedef {import('../../src/peer-store/types').KeyBook} KeyBook
* @typedef {import('peer-id')} PeerId
*/
2020-04-28 15:03:16 +02:00
describe('keyBook', () => {
/** @type {PeerId} */
let peerId
/** @type {PeerStore} */
let peerStore
/** @type {KeyBook} */
let kb
/** @type {MemoryDatastore} */
let datastore
2020-04-28 15:03:16 +02:00
beforeEach(async () => {
[peerId] = await peerUtils.createPeerId()
datastore = new MemoryDatastore()
peerStore = new PeerStore({
peerId,
datastore
})
2020-04-28 15:03:16 +02:00
kb = peerStore.keyBook
})
it('throws invalid parameters error if invalid PeerId is provided in set', async () => {
2020-04-28 15:03:16 +02:00
try {
await kb.set('invalid peerId')
} catch (/** @type {any} */ err) {
2020-04-28 15:03:16 +02:00
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
return
}
throw new Error('invalid peerId should throw error')
})
it('throws invalid parameters error if invalid PeerId is provided in get', async () => {
try {
await kb.get('invalid peerId')
} catch (/** @type {any} */ err) {
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
return
}
throw new Error('invalid peerId should throw error')
})
it('stores the peerId in the book and returns the public key', async () => {
2020-04-28 15:03:16 +02:00
// Set PeerId
await kb.set(peerId, peerId.pubKey)
2020-04-28 15:03:16 +02:00
// Get public key
const pubKey = await kb.get(peerId)
2020-04-28 15:03:16 +02:00
expect(peerId.pubKey.bytes).to.equalBytes(pubKey.bytes)
})
it('should not store if already stored', async () => {
const spy = sinon.spy(datastore, 'put')
2020-04-28 15:03:16 +02:00
// Set PeerId
await kb.set(peerId, peerId.pubKey)
await kb.set(peerId, peerId.pubKey)
2020-04-28 15:03:16 +02:00
expect(spy).to.have.property('callCount', 1)
})
it('should emit an event when setting a key', async () => {
const defer = pDefer()
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
expect(id.toB58String()).to.equal(peerId.toB58String())
expect(pubKey.bytes).to.equalBytes(peerId.pubKey.bytes)
defer.resolve()
})
// Set PeerId
await kb.set(peerId, peerId.pubKey)
await defer.promise
})
it('should not set when key does not match', async () => {
const [edKey] = await peerUtils.createPeerId({ fixture: false, opts: { keyType: 'Ed25519' } })
// Set PeerId
await expect(kb.set(edKey, peerId.pubKey)).to.eventually.be.rejectedWith(/bytes do not match/)
})
it('should emit an event when deleting a key', async () => {
const defer = pDefer()
await kb.set(peerId, peerId.pubKey)
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
expect(id.toB58String()).to.equal(peerId.toB58String())
expect(pubKey).to.be.undefined()
defer.resolve()
})
await kb.delete(peerId)
await defer.promise
})
2020-04-28 15:03:16 +02:00
})