53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import {expect, test} from 'bun:test'
|
|
|
|
import * as pwd from 'boxes/pwd'
|
|
|
|
test('base case', async () => {
|
|
const password = "AwesomePassword123!"
|
|
const data = new Uint8Array([1, 2, 3, 4, 5])
|
|
|
|
const box = await pwd.PwdBox.encrypt<Uint8Array>(password, data)
|
|
const result = (await box.decrypt(password)).expect("Should decrypt the data")
|
|
|
|
expect(result).toEqual(data)
|
|
})
|
|
|
|
test('wrong password', async () => {
|
|
const password1 = "AwesomePassword123!"
|
|
const password2 = "AwesomePassword321!"
|
|
expect(password1).not.toEqual(password2)
|
|
|
|
const data = new Uint8Array([1, 2, 3, 4, 5])
|
|
|
|
const box = await pwd.PwdBox.encrypt<Uint8Array>(password1, data)
|
|
|
|
;(await box.decrypt(password2)).expect_err("Should not decrypt the data with the wrong password")
|
|
})
|
|
|
|
test('toString and fromString are inverses', async () => {
|
|
const password = "AwesomePassword123!"
|
|
const data = new Uint8Array([1, 2, 3, 4, 5])
|
|
|
|
const box = await pwd.PwdBox.encrypt<Uint8Array>(password, data)
|
|
|
|
const str = box.toString()
|
|
const box2 = pwd.PwdBox.fromString<Uint8Array>(str).expect("Should be able to parse the string")
|
|
expect(box2).toEqual(box)
|
|
|
|
const result = (await box2.decrypt(password)).expect("Should decrypt the data")
|
|
|
|
expect(result).toEqual(data)
|
|
})
|
|
|
|
test('tampered salt should fail', async () => {
|
|
const password = "AwesomePassword123!"
|
|
const data = new Uint8Array([1, 2, 3, 4, 5])
|
|
|
|
const box = await pwd.PwdBox.encrypt<Uint8Array>(password, data)
|
|
|
|
// @ts-expect-error : I know salt is private, but I want to test it
|
|
box.salt[0] += 1
|
|
|
|
;(await box.decrypt(password)).expect_err("Should not decrypt the data with a tampered salt")
|
|
})
|