54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import {beforeAll, expect, test} from 'bun:test'
|
|
import {PwdWrap, SecretBox} from '..'
|
|
|
|
let k1!: string
|
|
let k2!: string
|
|
let message!: CryptoKey
|
|
|
|
beforeAll(async () => {
|
|
k1 = 'abc'
|
|
k2 = 'def'
|
|
message = await SecretBox.gen(true)
|
|
})
|
|
|
|
test('base case', async () => {
|
|
const box = await PwdWrap.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 PwdWrap.wrap(message, k1)
|
|
expect(box).not.toBeNull()
|
|
const unboxed = await box!.unwrap(k2)
|
|
expect(unboxed).toBeNull()
|
|
})
|
|
|
|
test('Encryption works with context', async () => {
|
|
const context = 'awesome context !'
|
|
const box = await PwdWrap.wrap(message, k1, context)
|
|
expect(box).not.toBeNull()
|
|
const unboxed = await box!.unwrap(k1, context)
|
|
expect(unboxed).toEqual(message)
|
|
})
|
|
test("Encryption doesn't work with different context", async () => {
|
|
const c1 = 'super context !'
|
|
const c2 = 'awesome context !'
|
|
const box = await PwdWrap.wrap(message, k1, c1)
|
|
expect(box).not.toBeNull()
|
|
const unboxed = await box!.unwrap(k1,c2)
|
|
expect(unboxed).toBeNull()
|
|
expect(unboxed).not.toEqual(message)
|
|
})
|
|
|
|
test('serialization', async () => {
|
|
const box = await PwdWrap.wrap(message, k1)
|
|
expect(box).not.toBeNull()
|
|
|
|
const ser = box!.toString()
|
|
const de = PwdWrap.fromString(ser)
|
|
expect(de).not.toBeNull()
|
|
|
|
expect(de).toEqual(box)
|
|
})
|