Files
libcrypto/test/boxes/pwd-wrap.test.ts
T
2024-05-15 12:14:15 +02:00

54 lines
1.7 KiB
TypeScript

import {beforeAll, expect, test} from 'bun:test'
import {PwdWrap} from '../../src/boxes'
import * as sym from '../../src/boxes/symmetric'
import * as asym from '../../src/boxes/asymmetric'
let kw_sym!: sym.Key;
let kw_asym!: asym.KeyPair;
beforeAll(async () => {
kw_sym = await sym.SecretBox.gen_key(true)
expect(kw_sym.extractable).toBe(true)
kw_asym = await asym.PrivateBox.gen_keypair(true)
expect(kw_asym[0].extractable).toBe(true)
})
test('base case', async () => {
const pwd = "password"
const testit = async (key: CryptoKey) => {
console.log(`Testing ${key.type} key with usage ${key.usages}`)
const wrapped = (await PwdWrap.wrap(pwd, key)).expect("Should wrap the key")
const unwrapped = (await wrapped.unwrap(pwd)).expect("Should unwrap the key")
expect(unwrapped).toEqual(key)
}
await testit(kw_sym)
await testit(kw_asym[0])
})
test('Fails with wrong password', async () => {
const pwd1 = "AwesomePassword123!"
const pwd2 = "AwesomePassword321!"
expect(pwd1).not.toEqual(pwd2)
const wrapped = (await PwdWrap.wrap(pwd1, kw_sym)).expect("Should wrap the key")
;(await wrapped.unwrap(pwd2)).expect_err("Shouldn't unwrap the key with wrong password")
})
test('toString and fromString are inverses', async () => {
const pwd = "password"
const wrapped = (await PwdWrap.wrap(pwd, kw_sym)).expect("Should wrap the key")
const str = wrapped.toString()
const wrapped2 = PwdWrap.fromString(str).expect("Should unwrap the key")
expect(wrapped2).toEqual(wrapped)
const unwrapped = (await wrapped.unwrap(pwd)).expect("Should unwrap the key")
expect(unwrapped).toEqual(kw_sym)
})