Adapt tests
This commit is contained in:
+7
-3
@@ -11,7 +11,8 @@ test('Private box', async () => {
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
const box = await PrivateBox.encrypt(message, de!)
|
||||
const unbox = await box.decrypt(k.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unbox = await box!.decrypt(k.privateKey)
|
||||
expect(unbox).toEqual(message)
|
||||
})
|
||||
test('Private wrap', async () => {
|
||||
@@ -24,11 +25,14 @@ test('Private wrap', async () => {
|
||||
const message = crypto.getRandomValues(new Uint8Array(8))
|
||||
const k_wrapped = await SecretBox.gen(true)
|
||||
const boxed_message = await SecretBox.encrypt(message, k_wrapped)
|
||||
expect(boxed_message).not.toBeNull()
|
||||
|
||||
const box = await PrivateWrap.wrap(k_wrapped, de!)
|
||||
const unbox = await box.unwrap(k.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unbox = await box!.unwrap(k.privateKey)
|
||||
expect(unbox).not.toBeNull()
|
||||
|
||||
const unboxed_message = await boxed_message.decrypt(unbox!)
|
||||
const unboxed_message = await boxed_message!.decrypt(unbox!)
|
||||
expect(unboxed_message).toEqual(message)
|
||||
})
|
||||
test('Signature', async () => {
|
||||
|
||||
@@ -14,12 +14,14 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PrivateBox.encrypt(message, k1.publicKey)
|
||||
const unboxed = await box.decrypt(k1.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k1.privateKey)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PrivateBox.encrypt(message, k1.publicKey)
|
||||
const unboxed = await box.decrypt(k2.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k2.privateKey)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -36,8 +38,9 @@ test('Key generation', async () => {
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PrivateBox.encrypt(message, k1.publicKey)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = await box.toString()
|
||||
const ser = await box!.toString()
|
||||
const de = await PrivateBox.fromString(ser)
|
||||
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
@@ -14,12 +14,14 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
const unboxed = await box.unwrap(k1.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k1.privateKey)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
const unboxed = await box.unwrap(k2.privateKey)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k2.privateKey)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -36,8 +38,9 @@ test('Key generation', async () => {
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = await box.toString()
|
||||
const ser = await box!.toString()
|
||||
const de = await PrivateWrap.fromString(ser)
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
|
||||
@@ -13,19 +13,22 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PwdBox.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k1)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PwdBox.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k2)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PwdBox.encrypt(message, k1)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = box.toString()
|
||||
const ser = box!.toString()
|
||||
const de = PwdBox.fromString(ser)
|
||||
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
@@ -13,19 +13,22 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k1)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k2)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = box.toString()
|
||||
const ser = box!.toString()
|
||||
const de = PwdWrap.fromString(ser)
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
|
||||
@@ -13,12 +13,14 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await SecretBox.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k1)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await SecretBox.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k2)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.decrypt(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -29,8 +31,9 @@ test('Key generation', async () => {
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await SecretBox.encrypt(message, k1)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = box.toString()
|
||||
const ser = box!.toString()
|
||||
const de = SecretBox.fromString(ser)
|
||||
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
@@ -13,12 +13,14 @@ beforeAll(async () => {
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k1)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k2)
|
||||
expect(box).not.toBeNull()
|
||||
const unboxed = await box!.unwrap(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -29,8 +31,9 @@ test('Key generation', async () => {
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
expect(box).not.toBeNull()
|
||||
|
||||
const ser = box.toString()
|
||||
const ser = box!.toString()
|
||||
const de = SecretWrap.fromString(ser)
|
||||
expect(de).not.toBeNull()
|
||||
|
||||
|
||||
+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