Skip to content

Commit d87f7d8

Browse files
committed
Add crypto test for to-device messages
1 parent 974771b commit d87f7d8

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

test/MatrixClientTest.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,8 +1400,7 @@ describe('MatrixClient', () => {
14001400
describe('processSync', () => {
14011401
interface ProcessSyncClient {
14021402
userId: string;
1403-
1404-
processSync(raw: any): Promise<any>;
1403+
processSync(raw: any): MatrixClient["processSync"];
14051404
}
14061405

14071406
it('should process non-room account data', async () => {

test/encryption/CryptoClientTest.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as simple from "simple-mock";
22
import HttpBackend from 'matrix-mock-request';
33

4-
import { EncryptedFile, MatrixClient, MembershipEvent, OTKAlgorithm, RoomEncryptionAlgorithm } from "../../src";
4+
import { EncryptedFile, EncryptionAlgorithm, IOlmEncrypted, IToDeviceMessage, MatrixClient, MembershipEvent, OTKAlgorithm, RoomEncryptionAlgorithm } from "../../src";
55
import { createTestClient, testCryptoStores, TEST_DEVICE_ID } from "../TestUtils";
66

77
export function bindNullEngine(http: HttpBackend) {
@@ -85,6 +85,71 @@ describe('CryptoClient', () => {
8585
}));
8686
});
8787

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+
88153
describe('isRoomEncrypted', () => {
89154
it('should fail when the crypto has not been prepared', () => testCryptoStores(async (cryptoStoreType) => {
90155
const userId = "@alice:example.org";

0 commit comments

Comments
 (0)