42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import {beforeAll, expect, test} from 'bun:test'
|
|
import {SecretWrap} from '..'
|
|
|
|
let k1!: CryptoKey
|
|
let k2!: CryptoKey
|
|
let message!: CryptoKey
|
|
|
|
beforeAll(async () => {
|
|
k1 = await SecretWrap.gen(false)
|
|
k2 = await SecretWrap.gen(true)
|
|
message = await SecretWrap.gen(true)
|
|
})
|
|
|
|
test('base case', async () => {
|
|
const box = await SecretWrap.wrap(message, k1)
|
|
expect(box).not.toBeNull()
|
|
const unboxed = await box!.unwrap(k1)
|
|
expect(unboxed).toEqual(message)
|
|
})
|
|
test("Different key can't decrypt", async () => {
|
|
const box = await SecretWrap.wrap(message, k1)
|
|
expect(box).not.toBeNull()
|
|
const unboxed = await box!.unwrap(k2)
|
|
expect(unboxed).toBeNull()
|
|
})
|
|
|
|
test('Key generation', async () => {
|
|
expect(k1.extractable).toBeFalse()
|
|
expect(k2.extractable).toBeTrue()
|
|
})
|
|
|
|
test('serialization', async () => {
|
|
const box = await SecretWrap.wrap(message, k1)
|
|
expect(box).not.toBeNull()
|
|
|
|
const ser = box!.toString()
|
|
const de = SecretWrap.fromString(ser)
|
|
expect(de).not.toBeNull()
|
|
|
|
expect(de).toEqual(box)
|
|
})
|