Implement PrivateWrap
ci/woodpecker/push/test Pipeline failed

Closes #7
This commit is contained in:
2024-05-14 22:24:33 +02:00
parent 3d2fe5f517
commit 528ccbdad7
3 changed files with 197 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
import {beforeAll, expect, test} from 'bun:test'
import {PrivateWrap, type KeyPair} from 'boxes/private-wrap'
import * as sym from 'boxes/symmetric'
import * as signature from 'signature'
let k1!: KeyPair;
let k2!: KeyPair;
let kw_sym!: sym.Key;
let kw_sig!: signature.KeyPair;
let kw_sym_non!: sym.Key;
let kw_sig_non!: signature.KeyPair;
beforeAll(async () => {
k1 = await PrivateWrap.gen_key(false)
k2 = await PrivateWrap.gen_key(true)
expect(k1[0].extractable).toBe(false)
expect(k1[1].extractable).toBe(true)
expect(k2[0].extractable).toBe(true)
expect(k2[1].extractable).toBe(true)
kw_sym = await sym.SecretBox.gen_key(true)
expect(kw_sym.extractable).toBe(true)
kw_sig = await signature.gen_keypair(true)
expect(kw_sig[0].extractable).toBe(true)
kw_sym_non = await sym.SecretBox.gen_key(false)
expect(kw_sym_non.extractable).toBe(false)
kw_sig_non = await signature.gen_keypair(false)
expect(kw_sig_non[0].extractable).toBe(false)
})
test('base case', async () => {
const [priv, pub] = k1
expect(pub.type).toBe("public")
const sym = (await PrivateWrap.wrap(pub, kw_sym)).expect("Should wrap the sym key")
const rsym = (await sym.unwrap(priv)).expect("Should unwrap the sym key")
expect(rsym).toEqual(kw_sym)
const sig = (await PrivateWrap.wrap(pub, kw_sig[0])).expect("Should wrap the signature key")
const rsig = (await sig.unwrap(priv)).expect("Should unwrap the signature key")
expect(rsig).toEqual(kw_sig[0])
})
test('toString and fromString and inverses', async () => {
const [_priv, pub] = k1
const sym = (await PrivateWrap.wrap(pub, kw_sym)).expect("Should wrap the sym key")
const sym_str = sym.toString()
const rsym = (PrivateWrap.fromString(sym_str)).expect("Should parse the sym key")
expect(rsym).toEqual(sym)
const sig = (await PrivateWrap.wrap(pub, kw_sig[0])).expect("Should wrap the signature key")
const sig_str = sig.toString()
const rsig = (PrivateWrap.fromString(sig_str)).expect("Should parse the signature key")
expect(rsig).toEqual(sig)
})
test("Can't wrap with private key", async () => {
const [priv, _pub] = k1
;(await PrivateWrap.wrap(priv, kw_sym)).expect_err("Shouldn't wrap with private key")
})
test("Can't unwrap with public key", async () => {
const [_priv, pub] = k1
const sym = (await PrivateWrap.wrap(pub, kw_sym)).expect("Should wrap the sym key")
;(await sym.unwrap(pub)).expect_err("Shouldn't unwrap with public key")
})
test("Can't unwrap with wrong private key", async () => {
const [_priv, pub] = k1
const sym = (await PrivateWrap.wrap(pub, kw_sym)).expect("Should wrap the sym key")
const [priv, _pub] = k2
;(await sym.unwrap(priv)).expect_err("Shouldn't unwrap with wrong private key")
})
test("Can't wrap if not extractable", async () => {
const [_priv, pub] = k1
;(await PrivateWrap.wrap(pub, kw_sym_non)).expect_err("Shouldn't wrap if not extractable")
;(await PrivateWrap.wrap(pub, kw_sig_non[0])).expect_err("Shouldn't wrap if not extractable")
})