Adapt tests
This commit is contained in:
+15
-8
@@ -9,13 +9,15 @@ beforeAll(async () => {
|
||||
|
||||
async function seal(key: CryptoKey): Promise<CryptoKey> {
|
||||
const box = await SecretWrap.wrap(key, wrap_key)
|
||||
const unboxed = await box.unwrap(wrap_key)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(wrap_key)
|
||||
expect(unboxed).not.toBeNull()
|
||||
return unboxed!
|
||||
}
|
||||
async function cant_seal(key: CryptoKey): Promise<void> {
|
||||
expect(key.extractable).toBeFalse()
|
||||
expect(SecretWrap.wrap(key, wrap_key)).rejects.toThrow()
|
||||
const box = await SecretWrap.wrap(key, wrap_key)
|
||||
expect(box).toBeNull()
|
||||
}
|
||||
|
||||
test('Signature', async () => {
|
||||
@@ -30,9 +32,10 @@ test('Signature', async () => {
|
||||
test('SecretWrap', async () => {
|
||||
let k = await SecretWrap.gen(true)
|
||||
const box = await SecretWrap.wrap(k, k) // Please kids, never do this for real !
|
||||
expect(box).not.toBeNull()
|
||||
k = await seal(k)
|
||||
|
||||
const unbox = await box.unwrap(k)
|
||||
const unbox = await box!.unwrap(k)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(k)
|
||||
})
|
||||
@@ -40,18 +43,20 @@ test('SecretBox', async () => {
|
||||
const message = crypto.getRandomValues(new Uint8Array(8))
|
||||
let k = await SecretBox.gen(true)
|
||||
const box = await SecretBox.encrypt(message, k)
|
||||
expect(box).not.toBeNull()
|
||||
k = await seal(k)
|
||||
|
||||
const unbox = await box.decrypt(k)
|
||||
const unbox = await box!.decrypt(k)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(message)
|
||||
})
|
||||
test('PrivateWrap', async () => {
|
||||
const k = await PrivateWrap.gen(true)
|
||||
const box = await PrivateWrap.wrap(wrap_key, k.publicKey)
|
||||
expect(box).not.toBeNull()
|
||||
const privk = await seal(k.privateKey)
|
||||
|
||||
const unbox = await box.unwrap(privk)
|
||||
const unbox = await box!.unwrap(privk)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(wrap_key)
|
||||
})
|
||||
@@ -59,19 +64,21 @@ test('PrivateBox', async () => {
|
||||
const message = crypto.getRandomValues(new Uint8Array(8))
|
||||
let k = await PrivateBox.gen(true)
|
||||
const box = await PrivateBox.encrypt(message, k.publicKey)
|
||||
expect(box).not.toBeNull()
|
||||
const privk = await seal(k.privateKey)
|
||||
|
||||
const unbox = await box.decrypt(privk)
|
||||
const unbox = await box!.decrypt(privk)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(message)
|
||||
})
|
||||
test("Can't unwrap with a different key", async () => {
|
||||
const wrap_k2 = await SecretWrap.gen(false)
|
||||
const box = await SecretWrap.wrap(wrap_key, wrap_key) // Never do this kids !
|
||||
const unbox = await box.unwrap(wrap_k2)
|
||||
expect(box).not.toBeNull()
|
||||
const unbox = await box!.unwrap(wrap_k2)
|
||||
expect(unbox).toBeNull()
|
||||
})
|
||||
test('Cant wrap non-extractable', async () => {
|
||||
test("Can't wrap non-extractable", async () => {
|
||||
const k_sb = await SecretBox.gen(false)
|
||||
const k_sw = await SecretWrap.gen(false)
|
||||
const k_pb = await PrivateBox.gen(false)
|
||||
|
||||
Reference in New Issue
Block a user