Use top level describe()
This commit is contained in:
parent
0166636ccc
commit
5542d7b8db
2 changed files with 102 additions and 98 deletions
|
|
@ -24,96 +24,98 @@ const storageDirectory = Path.resolve(__dirname, `./.test-storage`)
|
|||
|
||||
console.log(`Storage directory: ${storageDirectory}`)
|
||||
|
||||
describe('generateKeyPair()', () => {
|
||||
it('generates a keypair', async () => {
|
||||
expect.hasAssertions()
|
||||
const pair = await generateKeyPair(uuid())
|
||||
describe('ECC', () => {
|
||||
describe('generateKeyPair()', () => {
|
||||
it('generates a keypair', async () => {
|
||||
expect.hasAssertions()
|
||||
const pair = await generateKeyPair(uuid())
|
||||
|
||||
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
||||
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
||||
expect(pair.publicKey).toBeInstanceOf(Buffer)
|
||||
expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy()
|
||||
})
|
||||
it('returns the same pair for the same device', async () => {
|
||||
expect.hasAssertions()
|
||||
const id = uuid()
|
||||
const pair = await generateKeyPair(id)
|
||||
const pairAgain = await generateKeyPair(id)
|
||||
|
||||
expect(pairAgain).toStrictEqual(pair)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authorizeDevice()/isAuthorizedDevice()', () => {
|
||||
it('authorizes a device given its ID', async () => {
|
||||
expect.hasAssertions()
|
||||
await Storage.init({
|
||||
dir: storageDirectory
|
||||
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
||||
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
||||
expect(pair.publicKey).toBeInstanceOf(Buffer)
|
||||
expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy()
|
||||
})
|
||||
const deviceId = uuid()
|
||||
const pair = await generateKeyPair(deviceId)
|
||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
||||
expect(isAuthorizedDevice({ deviceId })).toBeTruthy()
|
||||
})
|
||||
})
|
||||
it('returns the same pair for the same device', async () => {
|
||||
expect.hasAssertions()
|
||||
const id = uuid()
|
||||
const pair = await generateKeyPair(id)
|
||||
const pairAgain = await generateKeyPair(id)
|
||||
|
||||
describe('encryptMessage()/decryptMessage()', () => {
|
||||
before(() =>
|
||||
Storage.init({
|
||||
dir: storageDirectory
|
||||
expect(pairAgain).toStrictEqual(pair)
|
||||
})
|
||||
)
|
||||
it('throws if provided with an unauthorized device id when encrypting', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
})
|
||||
|
||||
try {
|
||||
await encryptMessage({
|
||||
message: uuid(),
|
||||
deviceId
|
||||
describe('authorizeDevice()/isAuthorizedDevice()', () => {
|
||||
it('authorizes a device given its ID', async () => {
|
||||
expect.hasAssertions()
|
||||
await Storage.init({
|
||||
dir: storageDirectory
|
||||
})
|
||||
throw new Error('encryptMessage() did not throw')
|
||||
} catch (_) {
|
||||
expect(true).toBeTruthy()
|
||||
}
|
||||
const deviceId = uuid()
|
||||
const pair = await generateKeyPair(deviceId)
|
||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
||||
expect(isAuthorizedDevice({ deviceId })).toBeTruthy()
|
||||
})
|
||||
})
|
||||
it('throws if provided with an unknown device id when decrypting', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
|
||||
try {
|
||||
await decryptMessage({
|
||||
describe('encryptMessage()/decryptMessage()', () => {
|
||||
before(() =>
|
||||
Storage.init({
|
||||
dir: storageDirectory
|
||||
})
|
||||
)
|
||||
it('throws if provided with an unauthorized device id when encrypting', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
|
||||
try {
|
||||
await encryptMessage({
|
||||
message: uuid(),
|
||||
deviceId
|
||||
})
|
||||
throw new Error('encryptMessage() did not throw')
|
||||
} catch (_) {
|
||||
expect(true).toBeTruthy()
|
||||
}
|
||||
})
|
||||
it('throws if provided with an unknown device id when decrypting', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
|
||||
try {
|
||||
await decryptMessage({
|
||||
deviceId,
|
||||
encryptedMessage: {
|
||||
ciphertext: uuid(),
|
||||
ephemPublicKey: uuid(),
|
||||
iv: uuid(),
|
||||
mac: uuid(),
|
||||
metadata: uuid()
|
||||
}
|
||||
})
|
||||
throw new Error('decryptMessage() did not throw')
|
||||
} catch (_) {
|
||||
expect(true).toBeTruthy()
|
||||
}
|
||||
})
|
||||
it('encrypts and decrypts messages when given a known device id', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
|
||||
const pair = await generateKeyPair(deviceId)
|
||||
|
||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
||||
|
||||
const message = 'Bitcoin fixes this'
|
||||
|
||||
const encryptedMessage = await encryptMessage({ deviceId, message })
|
||||
|
||||
const decrypted = await decryptMessage({
|
||||
deviceId,
|
||||
encryptedMessage: {
|
||||
ciphertext: uuid(),
|
||||
ephemPublicKey: uuid(),
|
||||
iv: uuid(),
|
||||
mac: uuid(),
|
||||
metadata: uuid()
|
||||
}
|
||||
encryptedMessage
|
||||
})
|
||||
throw new Error('decryptMessage() did not throw')
|
||||
} catch (_) {
|
||||
expect(true).toBeTruthy()
|
||||
}
|
||||
})
|
||||
it('encrypts and decrypts messages when given a known device id', async () => {
|
||||
expect.hasAssertions()
|
||||
const deviceId = uuid()
|
||||
|
||||
const pair = await generateKeyPair(deviceId)
|
||||
|
||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
||||
|
||||
const message = 'Bitcoin fixes this'
|
||||
|
||||
const encryptedMessage = await encryptMessage({ deviceId, message })
|
||||
|
||||
const decrypted = await decryptMessage({
|
||||
deviceId,
|
||||
encryptedMessage
|
||||
expect(decrypted).toEqual(message)
|
||||
})
|
||||
|
||||
expect(decrypted).toEqual(message)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -10,25 +10,27 @@ const {
|
|||
convertBufferToBase64
|
||||
} = require('./crypto')
|
||||
|
||||
describe('generateRandomString()', () => {
|
||||
it('creates a random string of the specified length', async () => {
|
||||
expect.hasAssertions()
|
||||
const base = Math.ceil(Math.random() * 100)
|
||||
const len = base % 2 !== 0 ? base + 1 : base
|
||||
const result = await generateRandomString(len)
|
||||
describe('crypto', () => {
|
||||
describe('generateRandomString()', () => {
|
||||
it('creates a random string of the specified length', async () => {
|
||||
expect.hasAssertions()
|
||||
const base = Math.ceil(Math.random() * 100)
|
||||
const len = base % 2 !== 0 ? base + 1 : base
|
||||
const result = await generateRandomString(len)
|
||||
|
||||
expect(result.length).toEqual(len)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Buffer <> String <> Buffer', () => {
|
||||
it('preserves values', async () => {
|
||||
const rnd = await generateRandomString(24)
|
||||
|
||||
const asBuffer = convertBase64ToBuffer(rnd)
|
||||
|
||||
const asStringAgain = convertBufferToBase64(asBuffer)
|
||||
|
||||
expect(asStringAgain).toEqual(rnd)
|
||||
expect(result.length).toEqual(len)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Buffer <> String <> Buffer', () => {
|
||||
it('preserves values', async () => {
|
||||
const rnd = await generateRandomString(24)
|
||||
|
||||
const asBuffer = convertBase64ToBuffer(rnd)
|
||||
|
||||
const asStringAgain = convertBufferToBase64(asBuffer)
|
||||
|
||||
expect(asStringAgain).toEqual(rnd)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue