From 62ab263d0ea98e3029b71021aced8f96228cce25 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 22 Sep 2022 11:27:12 +0200 Subject: [PATCH] test(crypto-js): Test `OlmMachine.(en|de)crypt_exported_room_keys`. --- .../tests/machine.test.js | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/tests/machine.test.js b/bindings/matrix-sdk-crypto-js/tests/machine.test.js index 6b68e681b..0d600c084 100644 --- a/bindings/matrix-sdk-crypto-js/tests/machine.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/machine.test.js @@ -502,15 +502,16 @@ describe(OlmMachine.name, () => { m = await machine(); await m.shareRoomKey(room, [new UserId('@bob:example.org')], new EncryptionSettings()); - exportedRoomKeys = JSON.parse(await m.exportRoomKeys(session => { + exportedRoomKeys = await m.exportRoomKeys(session => { expect(session).toBeInstanceOf(InboundGroupSession); expect(session.roomId.toString()).toStrictEqual(room.toString()); return true; - })); + }); - expect(exportedRoomKeys).toHaveLength(1); - expect(exportedRoomKeys[0]).toMatchObject({ + const roomKeys = JSON.parse(exportedRoomKeys); + expect(roomKeys).toHaveLength(1); + expect(roomKeys[0]).toMatchObject({ algorithm: expect.any(String), room_id: room.toString(), sender_key: expect.any(String), @@ -523,6 +524,28 @@ describe(OlmMachine.name, () => { }); }); + let encryptedExportedRoomKeys; + let encryptionPassphrase = 'Hello, Matrix!'; + + test('can encrypt the exported room keys', () => { + encryptedExportedRoomKeys = OlmMachine.encryptExportedRoomKeys( + exportedRoomKeys, + encryptionPassphrase, + 100_000, + ); + + expect(encryptedExportedRoomKeys).toMatch(/^-----BEGIN MEGOLM SESSION DATA-----/); + }); + + test('can decrypt the exported room keys', () => { + const decryptedExportedRoomKeys = OlmMachine.decryptExportedRoomKeys( + encryptedExportedRoomKeys, + encryptionPassphrase, + ); + + expect(decryptedExportedRoomKeys).toStrictEqual(exportedRoomKeys); + }); + test('can import room keys', async () => { const progressListener = (progress, total) => { expect(progress).toBeLessThan(total); @@ -532,7 +555,7 @@ describe(OlmMachine.name, () => { expect(total).toStrictEqual(1n); }; - const result = JSON.parse(await m.importRoomKeys(JSON.stringify(exportedRoomKeys), progressListener)); + const result = JSON.parse(await m.importRoomKeys(exportedRoomKeys, progressListener)); expect(result).toMatchObject({ imported_count: expect.any(Number),