Implement pwd box
ci/woodpecker/push/test Pipeline failed

This commit is contained in:
2024-05-14 15:58:35 +02:00
parent 925517ac2c
commit f30516bd2c
2 changed files with 106 additions and 7 deletions
+52
View File
@@ -0,0 +1,52 @@
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")
})