|
1 | 1 | import * as simple from "simple-mock";
|
2 | 2 | import HttpBackend from 'matrix-mock-request';
|
3 | 3 |
|
4 |
| -import { EncryptedFile, MatrixClient, MembershipEvent, OTKAlgorithm, RoomEncryptionAlgorithm } from "../../src"; |
| 4 | +import { EncryptedFile, EncryptionAlgorithm, IOlmEncrypted, IToDeviceMessage, MatrixClient, MembershipEvent, OTKAlgorithm, RoomEncryptionAlgorithm } from "../../src"; |
5 | 5 | import { createTestClient, testCryptoStores, TEST_DEVICE_ID } from "../TestUtils";
|
6 | 6 |
|
7 | 7 | export function bindNullEngine(http: HttpBackend) {
|
@@ -85,6 +85,71 @@ describe('CryptoClient', () => {
|
85 | 85 | }));
|
86 | 86 | });
|
87 | 87 |
|
| 88 | + describe('processSync', () => { |
| 89 | + /** |
| 90 | + * Helper class to be able to call {@link MatrixClient#processSync}, which is otherwise private. |
| 91 | + */ |
| 92 | + interface ProcessSyncClient { |
| 93 | + processSync: MatrixClient["processSync"]; |
| 94 | + } |
| 95 | + |
| 96 | + it('should process encrypted to-device messages', () => testCryptoStores(async (cryptoStoreType) => { |
| 97 | + const userId = "@alice:example.org"; |
| 98 | + const { client, http } = createTestClient(null, userId, cryptoStoreType); |
| 99 | + const psClient = <ProcessSyncClient>(<any>client); |
| 100 | + |
| 101 | + await client.cryptoStore.setDeviceId(TEST_DEVICE_ID); |
| 102 | + |
| 103 | + const toDeviceMessage: IToDeviceMessage<IOlmEncrypted> = { |
| 104 | + type: "m.room.encrypted", |
| 105 | + sender: userId, |
| 106 | + content: { |
| 107 | + algorithm: EncryptionAlgorithm.OlmV1Curve25519AesSha2, |
| 108 | + sender_key: "sender_curve25519_key", |
| 109 | + ciphertext: { |
| 110 | + ["device_curve25519_key"]: { |
| 111 | + type: 0, |
| 112 | + body: "encrypted_payload_base_64", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }; |
| 117 | + const sync = { |
| 118 | + to_device: { events: [toDeviceMessage] }, |
| 119 | + device_unused_fallback_key_types: [OTKAlgorithm.Signed], |
| 120 | + device_one_time_keys_count: { |
| 121 | + [OTKAlgorithm.Signed]: 12, |
| 122 | + [OTKAlgorithm.Unsigned]: 14, |
| 123 | + }, |
| 124 | + device_lists: { |
| 125 | + changed: ["@bob:example.org"], |
| 126 | + left: ["@charlie:example.org"], |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + const toDeviceSpy = simple.stub().callFn((ev) => { |
| 131 | + for (const prop in toDeviceMessage) { |
| 132 | + expect(ev).toHaveProperty(prop); |
| 133 | + } |
| 134 | + }); |
| 135 | + client.on("to_device.decrypted", toDeviceSpy); |
| 136 | + |
| 137 | + bindNullEngine(http); |
| 138 | + await Promise.all([ |
| 139 | + client.crypto.prepare([]), |
| 140 | + http.flushAllExpected(), |
| 141 | + ]); |
| 142 | + |
| 143 | + bindNullEngine(http); |
| 144 | + await Promise.all([ |
| 145 | + psClient.processSync(sync), |
| 146 | + http.flushAllExpected(), |
| 147 | + ]); |
| 148 | + |
| 149 | + expect(toDeviceSpy.callCount).toBe(1); |
| 150 | + })); |
| 151 | + }); |
| 152 | + |
88 | 153 | describe('isRoomEncrypted', () => {
|
89 | 154 | it('should fail when the crypto has not been prepared', () => testCryptoStores(async (cryptoStoreType) => {
|
90 | 155 | const userId = "@alice:example.org";
|
|
0 commit comments