Test contexts

This commit is contained in:
2024-09-14 14:24:53 +02:00
parent 76cf4632e4
commit f2f39fd318
5 changed files with 80 additions and 1 deletions
+16
View File
@@ -36,6 +36,22 @@ test('Key generation', async () => {
expect(k2.publicKey.extractable).toBeTrue()
})
test('Encrypt with context', async () => {
const context = 'Super context !'
const box = await PrivateBox.encrypt(message, k1.publicKey, context)
expect(box).not.toBeNull()
const unboxed = await box!.decrypt(k1.privateKey, context)
expect(unboxed).toEqual(message)
})
test('Encrypt with different context', async () => {
const context1 = 'Super context !'
const context2 = 'Awesome context !'
const box = await PrivateBox.encrypt(message, k1.publicKey, context1)
expect(box).not.toBeNull()
const unboxed = await box!.decrypt(k1.privateKey, context2)
expect(unboxed).toBeNull()
})
test('serialization', async () => {
const box = await PrivateBox.encrypt(message, k1.publicKey)
expect(box).not.toBeNull()
+16
View File
@@ -36,6 +36,22 @@ test('Key generation', async () => {
expect(k2.publicKey.extractable).toBeTrue()
})
test("Encryption works with context", async () => {
const context = 'Super context !'
const box = await PrivateWrap.wrap(message, k1.publicKey, context)
expect(box).not.toBeNull()
const unboxed = await box!.unwrap(k1.privateKey, context)
expect(unboxed).not.toBeNull()
})
test("Encryption doesn't work with different context", async () => {
const context1 = 'Super context !'
const context2 = 'Awesome context !'
const box = await PrivateWrap.wrap(message, k1.publicKey, context1)
expect(box).not.toBeNull()
const unboxed = await box!.unwrap(k1.privateKey, context2)
expect(unboxed).toBeNull()
})
test('serialization', async () => {
const box = await PrivateWrap.wrap(message, k1.publicKey)
expect(box).not.toBeNull()
+15
View File
@@ -23,6 +23,21 @@ test("Different key can't decrypt", async () => {
const unboxed = await box!.decrypt(k2)
expect(unboxed).toBeNull()
})
test("Encryption works with context", async () => {
const c1 = 'context1'
const box = await PwdBox.encrypt(message, k1, c1)
expect(box).not.toBeNull()
const unboxed = await box!.decrypt(k1, c1)
expect(unboxed).not.toBeNull()
})
test("Encryption doesn't work with different context", async () => {
const c1 = 'context1'
const c2 = 'context2'
const box = await PwdBox.encrypt(message, k1, c1)
expect(box).not.toBeNull()
const unboxed = await box!.decrypt(k1, c2)
expect(unboxed).toBeNull()
})
test('serialization', async () => {
const box = await PwdBox.encrypt(message, k1)
+17
View File
@@ -24,6 +24,23 @@ test("Different key can't decrypt", async () => {
expect(unboxed).toBeNull()
})
test('Encryption works with context', async () => {
const context = 'awesome context !'
const box = await PwdWrap.wrap(message, k1, context)
expect(box).not.toBeNull()
const unboxed = await box!.unwrap(k1, context)
expect(unboxed).toEqual(message)
})
test("Encryption doesn't work with different context", async () => {
const c1 = 'super context !'
const c2 = 'awesome context !'
const box = await PwdWrap.wrap(message, k1, c1)
expect(box).not.toBeNull()
const unboxed = await box!.unwrap(k1,c2)
expect(unboxed).toBeNull()
expect(unboxed).not.toEqual(message)
})
test('serialization', async () => {
const box = await PwdWrap.wrap(message, k1)
expect(box).not.toBeNull()
+16 -1
View File
@@ -29,7 +29,7 @@ test('Key generation', async () => {
expect(k2.extractable).toBeTrue()
})
test('serialization', async () => {
test('String serialization', async () => {
const box = await SecretBox.encrypt(message, k1)
expect(box).not.toBeNull()
@@ -43,3 +43,18 @@ test('serialization', async () => {
expect(unboxed).not.toBeNull()
expect(unboxed).toEqual(message)
})
test('Byte serialization', async () => {
const box = await SecretBox.encrypt(message, k1)
expect(box).not.toBeNull()
const ser = box!.toBytes()
const de = SecretBox.fromBytes(ser)
expect(de).not.toBeNull()
expect(de).toEqual(box)
const unboxed = await de!.decrypt(k1)
expect(unboxed).not.toBeNull()
expect(unboxed).toEqual(message)
})