Skip to content

Commit

Permalink
Add crypto test for to-device messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Aug 2, 2023
1 parent 974771b commit d87f7d8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
3 changes: 1 addition & 2 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,8 +1400,7 @@ describe('MatrixClient', () => {
describe('processSync', () => {
interface ProcessSyncClient {
userId: string;

processSync(raw: any): Promise<any>;
processSync(raw: any): MatrixClient["processSync"];
}

it('should process non-room account data', async () => {
Expand Down
67 changes: 66 additions & 1 deletion test/encryption/CryptoClientTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as simple from "simple-mock";
import HttpBackend from 'matrix-mock-request';

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

export function bindNullEngine(http: HttpBackend) {
Expand Down Expand Up @@ -85,6 +85,71 @@ describe('CryptoClient', () => {
}));
});

describe('processSync', () => {
/**
* Helper class to be able to call {@link MatrixClient#processSync}, which is otherwise private.
*/
interface ProcessSyncClient {
processSync: MatrixClient["processSync"];
}

it('should process encrypted to-device messages', () => testCryptoStores(async (cryptoStoreType) => {
const userId = "@alice:example.org";
const { client, http } = createTestClient(null, userId, cryptoStoreType);
const psClient = <ProcessSyncClient>(<any>client);

await client.cryptoStore.setDeviceId(TEST_DEVICE_ID);

const toDeviceMessage: IToDeviceMessage<IOlmEncrypted> = {
type: "m.room.encrypted",
sender: userId,
content: {
algorithm: EncryptionAlgorithm.OlmV1Curve25519AesSha2,
sender_key: "sender_curve25519_key",
ciphertext: {
["device_curve25519_key"]: {
type: 0,
body: "encrypted_payload_base_64",
},
},
},
};
const sync = {
to_device: { events: [toDeviceMessage] },
device_unused_fallback_key_types: [OTKAlgorithm.Signed],
device_one_time_keys_count: {
[OTKAlgorithm.Signed]: 12,
[OTKAlgorithm.Unsigned]: 14,
},
device_lists: {
changed: ["@bob:example.org"],
left: ["@charlie:example.org"],
},
};

const toDeviceSpy = simple.stub().callFn((ev) => {
for (const prop in toDeviceMessage) {
expect(ev).toHaveProperty(prop);
}
});
client.on("to_device.decrypted", toDeviceSpy);

bindNullEngine(http);
await Promise.all([
client.crypto.prepare([]),
http.flushAllExpected(),
]);

bindNullEngine(http);
await Promise.all([
psClient.processSync(sync),
http.flushAllExpected(),
]);

expect(toDeviceSpy.callCount).toBe(1);
}));
});

describe('isRoomEncrypted', () => {
it('should fail when the crypto has not been prepared', () => testCryptoStores(async (cryptoStoreType) => {
const userId = "@alice:example.org";
Expand Down

0 comments on commit d87f7d8

Please sign in to comment.