import {beforeAll, expect, test} from 'bun:test' import {PrivateWrap, type KeyPair} from 'boxes/private-wrap' import * as sym from 'boxes/symmetric' let k1!: KeyPair; let k2!: KeyPair; let kw_sym!: sym.Key; let kw_sym_non!: sym.Key; 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_sym_non = await sym.SecretBox.gen_key(false) expect(kw_sym_non.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) }) 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) }) 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") })