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

33 lines
910 B
TypeScript
Raw 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-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)
2020-06-19 13:06:31 +02:00
assert(result?.equals(key), 'Stored and loaded key are not the same')
2020-01-13 15:38:14 +01:00
} catch (e) {
assert(false, `Test failed - ${e.message}`)
}
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)
assert(!result)
2020-01-13 16:18:10 +01:00
} catch (e) {
assert(false, `Test failed - ${e.message}`)
}
2020-06-19 12:49:40 +02:00
})
})