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}`)
|
console.log(`Storage directory: ${storageDirectory}`)
|
||||||
|
|
||||||
describe('generateKeyPair()', () => {
|
describe('ECC', () => {
|
||||||
it('generates a keypair', async () => {
|
describe('generateKeyPair()', () => {
|
||||||
expect.hasAssertions()
|
it('generates a keypair', async () => {
|
||||||
const pair = await generateKeyPair(uuid())
|
expect.hasAssertions()
|
||||||
|
const pair = await generateKeyPair(uuid())
|
||||||
|
|
||||||
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
||||||
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
||||||
expect(pair.publicKey).toBeInstanceOf(Buffer)
|
expect(pair.publicKey).toBeInstanceOf(Buffer)
|
||||||
expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy()
|
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
|
|
||||||
})
|
})
|
||||||
const deviceId = uuid()
|
it('returns the same pair for the same device', async () => {
|
||||||
const pair = await generateKeyPair(deviceId)
|
expect.hasAssertions()
|
||||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
const id = uuid()
|
||||||
expect(isAuthorizedDevice({ deviceId })).toBeTruthy()
|
const pair = await generateKeyPair(id)
|
||||||
})
|
const pairAgain = await generateKeyPair(id)
|
||||||
})
|
|
||||||
|
|
||||||
describe('encryptMessage()/decryptMessage()', () => {
|
expect(pairAgain).toStrictEqual(pair)
|
||||||
before(() =>
|
|
||||||
Storage.init({
|
|
||||||
dir: storageDirectory
|
|
||||||
})
|
})
|
||||||
)
|
})
|
||||||
it('throws if provided with an unauthorized device id when encrypting', async () => {
|
|
||||||
expect.hasAssertions()
|
|
||||||
const deviceId = uuid()
|
|
||||||
|
|
||||||
try {
|
describe('authorizeDevice()/isAuthorizedDevice()', () => {
|
||||||
await encryptMessage({
|
it('authorizes a device given its ID', async () => {
|
||||||
message: uuid(),
|
expect.hasAssertions()
|
||||||
deviceId
|
await Storage.init({
|
||||||
|
dir: storageDirectory
|
||||||
})
|
})
|
||||||
throw new Error('encryptMessage() did not throw')
|
const deviceId = uuid()
|
||||||
} catch (_) {
|
const pair = await generateKeyPair(deviceId)
|
||||||
expect(true).toBeTruthy()
|
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 {
|
describe('encryptMessage()/decryptMessage()', () => {
|
||||||
await 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,
|
deviceId,
|
||||||
encryptedMessage: {
|
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)
|
expect(decrypted).toEqual(message)
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -10,25 +10,27 @@ const {
|
||||||
convertBufferToBase64
|
convertBufferToBase64
|
||||||
} = require('./crypto')
|
} = require('./crypto')
|
||||||
|
|
||||||
describe('generateRandomString()', () => {
|
describe('crypto', () => {
|
||||||
it('creates a random string of the specified length', async () => {
|
describe('generateRandomString()', () => {
|
||||||
expect.hasAssertions()
|
it('creates a random string of the specified length', async () => {
|
||||||
const base = Math.ceil(Math.random() * 100)
|
expect.hasAssertions()
|
||||||
const len = base % 2 !== 0 ? base + 1 : base
|
const base = Math.ceil(Math.random() * 100)
|
||||||
const result = await generateRandomString(len)
|
const len = base % 2 !== 0 ? base + 1 : base
|
||||||
|
const result = await generateRandomString(len)
|
||||||
|
|
||||||
expect(result.length).toEqual(len)
|
expect(result.length).toEqual(len)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Buffer <> String <> Buffer', () => {
|
describe('Buffer <> String <> Buffer', () => {
|
||||||
it('preserves values', async () => {
|
it('preserves values', async () => {
|
||||||
const rnd = await generateRandomString(24)
|
const rnd = await generateRandomString(24)
|
||||||
|
|
||||||
const asBuffer = convertBase64ToBuffer(rnd)
|
const asBuffer = convertBase64ToBuffer(rnd)
|
||||||
|
|
||||||
const asStringAgain = convertBufferToBase64(asBuffer)
|
const asStringAgain = convertBufferToBase64(asBuffer)
|
||||||
|
|
||||||
expect(asStringAgain).toEqual(rnd)
|
expect(asStringAgain).toEqual(rnd)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue