js-libp2p-noise/test/keycache.spec.ts

36 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2020-06-19 13:06:31 +02:00
import { assert } from 'chai'
2020-06-19 12:49:40 +02:00
import { KeyCache } from '../src/keycache'
import { createPeerIds, createPeerIdsFromFixtures } from './fixtures/peer'
2020-08-11 15:14:11 +01:00
import uint8ArrayEquals from 'uint8arrays/equals'
2020-01-13 15:38:14 +01:00
2020-06-19 12:49:40 +02:00
describe('KeyCache', () => {
2020-06-19 13:06:31 +02:00
let peerA
2020-01-13 15:38:14 +01:00
before(async () => {
2020-06-19 13:06:31 +02:00
[peerA] = await createPeerIdsFromFixtures(2)
2020-06-19 12:49:40 +02:00
})
2020-01-13 15:38:14 +01:00
2020-06-19 12:49:40 +02:00
it('should store and load same key successfully', async () => {
2020-01-13 15:38:14 +01:00
try {
2020-06-19 12:49:40 +02:00
const key = Buffer.from('this is id 007')
await KeyCache.store(peerA, key)
const result = await KeyCache.load(peerA)
2021-01-26 21:39:37 +00:00
assert(result !== null && uint8ArrayEquals(result, key), 'Stored and loaded key are not the same')
2020-01-13 15:38:14 +01:00
} catch (e) {
2021-01-26 21:39:37 +00:00
const err = e as Error
assert(false, `Test failed - ${err.message}`)
2020-01-13 15:38:14 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-13 16:18:10 +01:00
2020-06-19 12:49:40 +02:00
it('should return undefined if key not found', async () => {
2020-01-13 16:18:10 +01:00
try {
2020-06-19 12:49:40 +02:00
const [newPeer] = await createPeerIds(1)
const result = await KeyCache.load(newPeer)
2021-01-26 21:39:37 +00:00
assert(result === null)
2020-01-13 16:18:10 +01:00
} catch (e) {
2021-01-26 21:39:37 +00:00
const err = e as Error
assert(false, `Test failed - ${err.message}`)
2020-01-13 16:18:10 +01:00
}
2020-06-19 12:49:40 +02:00
})
})