Skip to content

Commit c3b4572

Browse files
authored
Add a new test for event encryption, which works with rust (#3203)
* crypto.spec.ts: factor out `expactAliceKeyClaim` utility * Add a new test for event encryption ... one that actually works on the rust SDK. * Bump matrix-sdk-crypto-js version ... to pick up recent fixes to race conditions
1 parent 40fe159 commit c3b4572

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
],
5656
"dependencies": {
5757
"@babel/runtime": "^7.12.5",
58-
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.3",
58+
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.5",
5959
"another-json": "^0.2.0",
6060
"bs58": "^5.0.0",
6161
"content-type": "^1.0.4",

spec/integ/crypto.spec.ts

+51-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import anotherjson from "another-json";
1919
import fetchMock from "fetch-mock-jest";
2020
import "fake-indexeddb/auto";
2121
import { IDBFactory } from "fake-indexeddb";
22-
import { MockResponse } from "fetch-mock";
22+
import { MockResponse, MockResponseFunction } from "fetch-mock";
2323

2424
import type { IDeviceKeys } from "../../src/@types/crypto";
2525
import * as testUtils from "../test-utils/test-utils";
@@ -47,6 +47,7 @@ import {
4747
import { DeviceInfo } from "../../src/crypto/deviceinfo";
4848
import { E2EKeyReceiver, IE2EKeyReceiver } from "../test-utils/E2EKeyReceiver";
4949
import { ISyncResponder, SyncResponder } from "../test-utils/SyncResponder";
50+
import { escapeRegExp } from "../../src/utils";
5051

5152
const ROOM_ID = "!room:id";
5253

@@ -438,8 +439,9 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
438439
});
439440
return response;
440441
}
442+
const rootRegexp = escapeRegExp(new URL("/_matrix/client/", aliceClient.getHomeserverUrl()).toString());
441443
fetchMock.postOnce(
442-
new URL("/_matrix/client/r0/keys/query", aliceClient.getHomeserverUrl()).toString(),
444+
new RegExp(rootRegexp + "(r0|v3)/keys/query"),
443445
(url: string, opts: RequestInit) => onQueryRequest(JSON.parse(opts.body as string)),
444446
{
445447
// append to the list of intercepts on this path
@@ -448,6 +450,17 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
448450
);
449451
}
450452

453+
/**
454+
* Add an expectation for a /keys/claim request for the MatrixClient under test
455+
*
456+
* @param response - the response to return from the request. Normally an {@link IClaimOTKsResult}
457+
* (or a function that returns one).
458+
*/
459+
function expectAliceKeyClaim(response: MockResponse | MockResponseFunction) {
460+
const rootRegexp = escapeRegExp(new URL("/_matrix/client/", aliceClient.getHomeserverUrl()).toString());
461+
fetchMock.postOnce(new RegExp(rootRegexp + "(r0|v3)/keys/claim"), response);
462+
}
463+
451464
/**
452465
* Get the device keys for testOlmAccount in a format suitable for a
453466
* response to /keys/query
@@ -755,10 +768,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
755768
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));
756769

757770
// ... and then claim one of his OTKs
758-
fetchMock.postOnce(
759-
new URL("/_matrix/client/r0/keys/claim", aliceClient.getHomeserverUrl()).toString(),
760-
getTestKeysClaimResponse("@bob:xyz"),
761-
);
771+
expectAliceKeyClaim(getTestKeysClaimResponse("@bob:xyz"));
762772

763773
// fire off the prepare request
764774
const room = aliceClient.getRoom(ROOM_ID);
@@ -772,7 +782,37 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
772782
await p;
773783
});
774784

785+
it("Alice sends a megolm message with GlobalErrorOnUnknownDevices=false", async () => {
786+
aliceClient.setGlobalErrorOnUnknownDevices(false);
787+
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
788+
await startClientAndAwaitFirstSync();
789+
790+
// Alice shares a room with Bob
791+
syncResponder.sendOrQueueSyncResponse(getSyncResponse(["@bob:xyz"]));
792+
await syncPromise(aliceClient);
793+
794+
// Once we send the message, Alice will check Bob's device list (twice, because reasons) ...
795+
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));
796+
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));
797+
798+
// ... and claim one of his OTKs ...
799+
expectAliceKeyClaim(getTestKeysClaimResponse("@bob:xyz"));
800+
801+
// ... and send an m.room_key message
802+
const inboundGroupSessionPromise = expectSendRoomKey("@bob:xyz", testOlmAccount);
803+
804+
// Finally, send the message, and expect to get an `m.room.encrypted` event that we can decrypt.
805+
await Promise.all([
806+
aliceClient.sendTextMessage(ROOM_ID, "test"),
807+
expectSendMegolmMessage(inboundGroupSessionPromise),
808+
]);
809+
});
810+
775811
oldBackendOnly("Alice sends a megolm message", async () => {
812+
// TODO: do something about this for the rust backend.
813+
// Currently it fails because we don't respect the default GlobalErrorOnUnknownDevices and
814+
// send messages to unknown devices.
815+
776816
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
777817
await startClientAndAwaitFirstSync();
778818
const p2pSession = await establishOlmSession(aliceClient, keyReceiver, syncResponder, testOlmAccount);
@@ -1037,14 +1077,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
10371077

10381078
// mark the device as known, and resend.
10391079
aliceClient.setDeviceKnown(aliceClient.getUserId()!, "DEVICE_ID");
1040-
fetchMock.postOnce(
1041-
new URL("/_matrix/client/r0/keys/claim", aliceClient.getHomeserverUrl()).toString(),
1042-
(url: string, opts: RequestInit): MockResponse => {
1043-
const content = JSON.parse(opts.body as string);
1044-
expect(content.one_time_keys[aliceClient.getUserId()!].DEVICE_ID).toEqual("signed_curve25519");
1045-
return getTestKeysClaimResponse(aliceClient.getUserId()!);
1046-
},
1047-
);
1080+
expectAliceKeyClaim((url: string, opts: RequestInit): MockResponse => {
1081+
const content = JSON.parse(opts.body as string);
1082+
expect(content.one_time_keys[aliceClient.getUserId()!].DEVICE_ID).toEqual("signed_curve25519");
1083+
return getTestKeysClaimResponse(aliceClient.getUserId()!);
1084+
});
10481085

10491086
const inboundGroupSessionPromise = expectSendRoomKey(aliceClient.getUserId()!, testOlmAccount);
10501087

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1494,10 +1494,10 @@
14941494
dependencies:
14951495
lodash "^4.17.21"
14961496

1497-
"@matrix-org/matrix-sdk-crypto-js@^0.1.0-alpha.3":
1498-
version "0.1.0-alpha.4"
1499-
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-js/-/matrix-sdk-crypto-js-0.1.0-alpha.4.tgz#1b20294e0354c3dcc9c7dc810d883198a4042f04"
1500-
integrity sha512-mdaDKrw3P5ZVCpq0ioW0pV6ihviDEbS8ZH36kpt9stLKHwwDSopPogE6CkQhi0B1jn1yBUtOYi32mBV/zcOR7g==
1497+
"@matrix-org/matrix-sdk-crypto-js@^0.1.0-alpha.5":
1498+
version "0.1.0-alpha.5"
1499+
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-js/-/matrix-sdk-crypto-js-0.1.0-alpha.5.tgz#60ede2c43b9d808ba8cf46085a3b347b290d9658"
1500+
integrity sha512-2KjAgWNGfuGLNjJwsrs6gGX157vmcTfNrA4u249utgnMPbJl7QwuUqh1bGxQ0PpK06yvZjgPlkna0lTbuwtuQw==
15011501

15021502
"@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.14.tgz":
15031503
version "3.2.14"

0 commit comments

Comments
 (0)