cleanup test infra

This commit is contained in:
2024-07-29 16:14:12 +02:00
parent 72a31ab3e2
commit c485d5c715
18 changed files with 2247 additions and 1771 deletions

View File

@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Random > Should throw error on empty array 1`] = `[Error: Invalid number of bytes]`;
exports[`Random > Should throw error on negative bytes 1`] = `[Error: Invalid number of bytes]`;

View File

@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`RSA > Generate keys > Should not be able to generate a key below 2048bit 1`] = `[Error: bit sizes below 2048 are considered insecure.]`;
exports[`RSA > Generate keys > Should not be able to generate a key below 2048bit 2`] = `[Error: bit sizes below 2048 are considered insecure.]`;

View File

@@ -1,3 +1,4 @@
import { describe, expect, it } from 'vitest'
import { AES, Bytes, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
@@ -15,10 +16,10 @@ describe('AES', () => {
length: keySize,
salt: Hex.decode(Precomputed.Crypto.Bytes[16]),
})
const ciphertext = await AES.encrypt(data, key, AES.Modes.GCM)
const ciphertext = await AES.encrypt(data, key, AES.Modes.AES_GCM)
const plaintext = await AES.decrypt(ciphertext, key)
chai.expect(data).to.be.deep.equal(plaintext)
chai.expect(message).to.be.equal(Bytes.decode(plaintext))
expect(data.buffer).toEqual(plaintext.buffer)
expect(message).toEqual(Bytes.decode(plaintext))
})
}
})
@@ -28,15 +29,15 @@ describe('AES', () => {
const data = Bytes.encode(message)
const ciphertext = await AES.encrypt(data, key)
const plaintext = await AES.decrypt(ciphertext, key)
chai.expect(data).to.be.deep.equal(plaintext)
chai.expect(message).to.be.equal(Bytes.decode(plaintext))
expect(data.buffer).toEqual(plaintext.buffer)
expect(message).toEqual(Bytes.decode(plaintext))
})
it('Easy API', async () => {
const password = 'foobar'
const encrypted = await AES.encryptEasy(message, password)
const decrypted = await AES.decryptEasy(encrypted, password)
chai.expect(message).to.be.equal(decrypted)
expect(message).toEqual(decrypted)
})
})
}

View File

@@ -1,15 +1,15 @@
import { describe, expect, it } from 'vitest'
import { Base64, Bytes, Hex } from '../dist/index.js'
import { compareArrayLike } from './utils.js'
import { Precomputed } from './values.js'
describe('Encoding', () => {
describe('Bytes', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Bytes)) {
it(`Should encode ${input} to ${output}`, async () => {
compareArrayLike(Bytes.encode(input), output)
expect(Bytes.encode(input).buffer).toEqual(output.buffer)
})
it(`Should decode ${output} to ${input}`, async () => {
compareArrayLike(await Bytes.decode(output), input)
expect(Bytes.decode(output)).toEqual(input)
})
}
})
@@ -17,10 +17,10 @@ describe('Encoding', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Hex)) {
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
chai.expect(await Hex.encode(buffer)).to.equal(output)
expect(Hex.encode(buffer)).toEqual(output)
})
it(`Should decode ${output} to ${input}`, async () => {
chai.expect(await Hex.decode(output)).to.deep.equal(buffer)
expect(Hex.decode(output).buffer).toEqual(buffer.buffer)
})
}
})
@@ -28,10 +28,10 @@ describe('Encoding', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
chai.expect(await Base64.encode(buffer)).to.equal(output)
expect(await Base64.encode(buffer)).toEqual(output)
})
it(`Should decode ${output} to ${input}`, async () => {
chai.expect(await Base64.decode(output)).to.deep.equal(buffer)
expect((await Base64.decode(output)).buffer).toEqual(buffer.buffer)
})
}
})

View File

@@ -1,3 +1,4 @@
import { describe, expect, it } from 'vitest'
import { Bytes, Hash, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
@@ -6,16 +7,18 @@ describe('Hash', () => {
describe(type, () => {
const values = Precomputed.Hash[type]
for (const [input, output] of Object.entries(values)) {
if (typeof output !== 'string') throw new Error('Bad test data')
it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => {
const hashed = await Hash.hash(input, Hashes[type])
chai.expect(hashed).to.equal(output)
expect(hashed).toEqual(output)
})
it(`Should hash "${input}" to "${output.slice(0, 8)}..." as buffer`, async () => {
const outputBuffer = Hex.decode(output)
const inputBuffer = Bytes.encode(input)
const hashed = await Hash.hash(inputBuffer, Hashes[type])
chai.expect(hashed).to.deep.equal(outputBuffer)
expect(hashed).toEqual(outputBuffer)
})
}
})

View File

@@ -1,12 +0,0 @@
/**
* Hook for mocha tests in node
* Initialises chai and chai-as-promised as global variables
*/
export const mochaHooks = {
async beforeEach() {
if (typeof chai === 'undefined') {
global.chai = await import('chai')
}
},
}

View File

@@ -1,18 +1,18 @@
import { describe, expect, it } from 'vitest'
import { getRandomBytes } from '../dist/index.js'
import { Promises } from './utils.js'
describe('Random', () => {
it('Should be able to create random values', async () => {
const buffer = await getRandomBytes(8)
chai.expect(buffer).to.be.instanceOf(Uint8Array)
chai.expect(buffer.byteLength).to.equal(8)
expect(buffer).instanceOf(Uint8Array)
expect(buffer.byteLength).toEqual(8)
})
it('Should throw error on empty array', async () => {
await Promises.reject(() => getRandomBytes(0))
await expect(() => getRandomBytes(0)).rejects.toThrowErrorMatchingSnapshot()
})
it('Should throw error on negative bytes', async () => {
await Promises.reject(() => getRandomBytes(-1))
await expect(() => getRandomBytes(-1)).rejects.toThrowErrorMatchingSnapshot()
})
})

View File

@@ -1,11 +1,11 @@
import { describe } from 'vitest'
import { Bytes, RSA } from '../dist/index.js'
import { Promises } from './utils.js'
import { Precomputed } from './values.js'
import { it } from 'vitest'
import { expect } from 'vitest'
describe('RSA', () => {
describe('Generate keys', function () {
this.timeout(10_000)
it('Should be able to generate a keypair', async () => {
await RSA.generateKeyPair()
})
@@ -16,10 +16,10 @@ describe('RSA', () => {
await RSA.generateKeyPair(4096)
})
it('Should not be able to generate a key below 2048bit', async () => {
await Promises.reject(() => RSA.generateKeyPair(1024))
await expect(() => RSA.generateKeyPair(1024)).rejects.toThrowErrorMatchingSnapshot()
})
it('Should not be able to generate a key below 2048bit', async () => {
await Promises.reject(() => RSA.generateKeyPair(-1))
await expect(() => RSA.generateKeyPair(-1)).rejects.toThrowErrorMatchingSnapshot()
})
})
@@ -30,10 +30,9 @@ describe('RSA', () => {
const bytes = Bytes.encode(message)
try {
const encrypted = await RSA.encrypt(bytes, pair.public)
chai.expect.fail('Should have thrown error')
const decrypted = await RSA.decrypt(encrypted, pair.private)
chai.expect(decrypted).to.be.deep.equal(bytes)
chai.expect(message).to.be.equal(Bytes.decode(decrypted))
expect(decrypted).toEqual(bytes)
expect(message).toEqual(Bytes.decode(decrypted))
} catch {}
})
}

View File

@@ -1,15 +0,0 @@
export class Promises {
static async reject(fn) {
try {
await fn()
chai.expect.fail('Should have thrown error')
} catch {}
}
}
export function compareArrayLike(a, b) {
chai.expect(a.length).to.equal(b.length)
for (let i = 0; i < a.length; i++) {
chai.expect(a[i]).to.equal(b[i])
}
}

View File

@@ -57,4 +57,4 @@ export const Precomputed = {
'Marvelous intentions joy deceptions overcome sexuality spirit against. Selfish of marvelous play dead war snare eternal-return ultimate. Reason aversion suicide.',
},
},
}
} as const