From 0eb9ecbeab9c6ff16e02d13b891547ddaee5dc6f Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Fri, 21 Oct 2022 15:07:04 +0300 Subject: [PATCH] add more tests to encryption --- .../lib/encryption/__tests__/encrypt.test.ts | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/core/data-transfer/lib/encryption/__tests__/encrypt.test.ts b/packages/core/data-transfer/lib/encryption/__tests__/encrypt.test.ts index 5b3c5aa902..33b69e312c 100644 --- a/packages/core/data-transfer/lib/encryption/__tests__/encrypt.test.ts +++ b/packages/core/data-transfer/lib/encryption/__tests__/encrypt.test.ts @@ -7,7 +7,7 @@ describe('Encryption', () => { const encryptedData = cipher.update(textToEncrypt); expect(cipher).toBeDefined(); - expect(encryptedData).not.toBe(textToEncrypt); + expect(encryptedData.toString()).not.toBe(textToEncrypt); expect(encryptedData).toBeInstanceOf(Buffer); }); @@ -17,7 +17,7 @@ describe('Encryption', () => { const encryptedData = cipher.update(textToEncrypt); expect(cipher).toBeDefined(); - expect(encryptedData).not.toBe(textToEncrypt); + expect(encryptedData.toString()).not.toBe(textToEncrypt); expect(encryptedData).toBeInstanceOf(Buffer); }); @@ -27,7 +27,7 @@ describe('Encryption', () => { const encryptedData = cipher.update(textToEncrypt); expect(cipher).toBeDefined(); - expect(encryptedData).not.toBe(textToEncrypt); + expect(encryptedData.toString()).not.toBe(textToEncrypt); expect(encryptedData).toBeInstanceOf(Buffer); }); @@ -37,7 +37,31 @@ describe('Encryption', () => { const encryptedData = cipher.update(textToEncrypt); expect(cipher).toBeDefined(); - expect(encryptedData).not.toBe(textToEncrypt); + expect(encryptedData.toString()).not.toBe(textToEncrypt); expect(encryptedData).toBeInstanceOf(Buffer); }); + + test('data encrypted with different algorithms should have different results', () => { + const cipherAES256 = createCipher('password', 'aes256'); + const cipherAES192 = createCipher('password', 'aes192'); + const cipherDefault = createCipher('password'); + const textToEncrypt = 'something ate an apple'; + const encryptedDataAES256 = cipherAES256.update(textToEncrypt).toString(); + const encryptedDataAES192 = cipherAES192.update(textToEncrypt).toString(); + const encryptedDataDefault = cipherDefault.update(textToEncrypt).toString(); + + expect(encryptedDataAES256).not.toBe(encryptedDataDefault); + expect(encryptedDataAES256).not.toBe(encryptedDataAES192); + expect(encryptedDataDefault).not.toBe(encryptedDataAES192); + }); + + test('data encrypted with different key should be different', () => { + const cipher1 = createCipher('password'); + const cipher2 = createCipher('differentpassword'); + const textToEncrypt = 'something ate an apple'; + const encryptedData1 = cipher1.update(textToEncrypt).toString(); + const encryptedData2 = cipher2.update(textToEncrypt).toString(); + + expect(encryptedData1).not.toBe(encryptedData2); + }); });