Skip to content

Commit f06ccc1

Browse files
committed
Run some of the megolm integ tests against rust sdk
1 parent 33ab56c commit f06ccc1

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

spec/integ/megolm-integ.spec.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ limitations under the License.
1818
import anotherjson from "another-json";
1919
import MockHttpBackend from "matrix-mock-request";
2020

21+
import "fake-indexeddb/auto";
22+
import { IDBFactory } from "fake-indexeddb";
23+
2124
import * as testUtils from "../test-utils/test-utils";
2225
import { TestClient } from "../TestClient";
2326
import { logger } from "../../src/logger";
@@ -38,9 +41,17 @@ import {
3841
} from "../../src/matrix";
3942
import { IDeviceKeys } from "../../src/crypto/dehydration";
4043
import { DeviceInfo } from "../../src/crypto/deviceinfo";
44+
import { CRYPTO_BACKENDS, InitCrypto } from "../test-utils/test-utils";
4145

4246
const ROOM_ID = "!room:id";
4347

48+
afterEach(() => {
49+
// reset fake-indexeddb after each test, to make sure we don't leak connections
50+
// cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state
51+
// eslint-disable-next-line no-global-assign
52+
indexedDB = new IDBFactory();
53+
});
54+
4455
// start an Olm session with a given recipient
4556
async function createOlmSession(olmAccount: Olm.Account, recipientTestClient: TestClient): Promise<Olm.Session> {
4657
const keys = await recipientTestClient.awaitOneTimeKeyUpload();
@@ -341,11 +352,17 @@ async function expectSendMegolmMessage(
341352
return JSON.parse(r.plaintext);
342353
}
343354

344-
describe("megolm", () => {
355+
describe.each(Object.entries(CRYPTO_BACKENDS))("megolm (%s)", (backend: string, initCrypto: InitCrypto) => {
345356
if (!global.Olm) {
357+
// currently we use libolm to implement the crypto in the tests, so need it to be present.
346358
logger.warn("not running megolm tests: Olm not present");
347359
return;
348360
}
361+
362+
// oldBackendOnly is an alternative to `it` or `test` which will skip the test if we are running against the
363+
// Rust backend. Once we have full support in the rust sdk, it will go away.
364+
const oldBackendOnly = backend === "rust-sdk" ? test.skip : test;
365+
349366
const Olm = global.Olm;
350367

351368
let testOlmAccount = {} as unknown as Olm.Account;
@@ -410,8 +427,10 @@ describe("megolm", () => {
410427

411428
beforeEach(async () => {
412429
aliceTestClient = new TestClient("@alice:localhost", "xzcvb", "akjgkrgjs");
413-
await aliceTestClient.client.initCrypto();
430+
await initCrypto(aliceTestClient.client);
414431

432+
// create a test olm device which we will use to communicate with alice. We use libolm to implement this.
433+
await Olm.init();
415434
testOlmAccount = new Olm.Account();
416435
testOlmAccount.create();
417436
const testE2eKeys = JSON.parse(testOlmAccount.identity_keys());
@@ -615,7 +634,7 @@ describe("megolm", () => {
615634
expect(event.getContent().body).toEqual("42");
616635
});
617636

618-
it("Alice sends a megolm message", async () => {
637+
oldBackendOnly("Alice sends a megolm message", async () => {
619638
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
620639
await aliceTestClient.start();
621640
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -658,7 +677,7 @@ describe("megolm", () => {
658677
]);
659678
});
660679

661-
it("We shouldn't attempt to send to blocked devices", async () => {
680+
oldBackendOnly("We shouldn't attempt to send to blocked devices", async () => {
662681
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
663682
await aliceTestClient.start();
664683
await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -702,7 +721,7 @@ describe("megolm", () => {
702721
expect(() => aliceTestClient.client.getGlobalErrorOnUnknownDevices()).toThrowError("encryption disabled");
703722
});
704723

705-
it("should permit sending to unknown devices", async () => {
724+
oldBackendOnly("should permit sending to unknown devices", async () => {
706725
expect(aliceTestClient.client.getGlobalErrorOnUnknownDevices()).toBeTruthy();
707726

708727
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
@@ -760,7 +779,7 @@ describe("megolm", () => {
760779
);
761780
});
762781

763-
it("should disable sending to unverified devices", async () => {
782+
oldBackendOnly("should disable sending to unverified devices", async () => {
764783
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
765784
await aliceTestClient.start();
766785
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -818,7 +837,7 @@ describe("megolm", () => {
818837
});
819838
});
820839

821-
it("We should start a new megolm session when a device is blocked", async () => {
840+
oldBackendOnly("We should start a new megolm session when a device is blocked", async () => {
822841
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
823842
await aliceTestClient.start();
824843
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -876,7 +895,7 @@ describe("megolm", () => {
876895
});
877896

878897
// https://github.com/vector-im/element-web/issues/2676
879-
it("Alice should send to her other devices", async () => {
898+
oldBackendOnly("Alice should send to her other devices", async () => {
880899
// for this test, we make the testOlmAccount be another of Alice's devices.
881900
// it ought to get included in messages Alice sends.
882901
await aliceTestClient.start();
@@ -957,7 +976,7 @@ describe("megolm", () => {
957976
expect(decrypted.content?.body).toEqual("test");
958977
});
959978

960-
it("Alice should wait for device list to complete when sending a megolm message", async () => {
979+
oldBackendOnly("Alice should wait for device list to complete when sending a megolm message", async () => {
961980
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
962981
await aliceTestClient.start();
963982
await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -1047,7 +1066,7 @@ describe("megolm", () => {
10471066
aliceTestClient.stop();
10481067

10491068
aliceTestClient = new TestClient("@alice:localhost", "device2", "access_token2");
1050-
await aliceTestClient.client.initCrypto();
1069+
await initCrypto(aliceTestClient.client);
10511070
await aliceTestClient.client.importRoomKeys(exported);
10521071
await aliceTestClient.start();
10531072

@@ -1189,7 +1208,7 @@ describe("megolm", () => {
11891208
expect(decryptedEvent.getClearContent()).toBeUndefined();
11901209
});
11911210

1192-
it("Alice receives shared history before being invited to a room by the sharer", async () => {
1211+
oldBackendOnly("Alice receives shared history before being invited to a room by the sharer", async () => {
11931212
const beccaTestClient = new TestClient("@becca:localhost", "foobar", "bazquux");
11941213
await beccaTestClient.client.initCrypto();
11951214

@@ -1341,7 +1360,7 @@ describe("megolm", () => {
13411360
await beccaTestClient.stop();
13421361
});
13431362

1344-
it("Alice receives shared history before being invited to a room by someone else", async () => {
1363+
oldBackendOnly("Alice receives shared history before being invited to a room by someone else", async () => {
13451364
const beccaTestClient = new TestClient("@becca:localhost", "foobar", "bazquux");
13461365
await beccaTestClient.client.initCrypto();
13471366

@@ -1487,7 +1506,7 @@ describe("megolm", () => {
14871506
await beccaTestClient.stop();
14881507
});
14891508

1490-
it("allows sending an encrypted event as soon as room state arrives", async () => {
1509+
oldBackendOnly("allows sending an encrypted event as soon as room state arrives", async () => {
14911510
/* Empirically, clients expect to be able to send encrypted events as soon as the
14921511
* RoomStateEvent.NewMember notification is emitted, so test that works correctly.
14931512
*/
@@ -1612,7 +1631,7 @@ describe("megolm", () => {
16121631
await aliceTestClient.httpBackend.flush(membersPath, 1);
16131632
}
16141633

1615-
it("Sending an event initiates a member list sync", async () => {
1634+
oldBackendOnly("Sending an event initiates a member list sync", async () => {
16161635
// we expect a call to the /members list...
16171636
const memberListPromise = expectMembershipRequest(ROOM_ID, ["@bob:xyz"]);
16181637

@@ -1644,7 +1663,7 @@ describe("megolm", () => {
16441663
]);
16451664
});
16461665

1647-
it("loading the membership list inhibits a later load", async () => {
1666+
oldBackendOnly("loading the membership list inhibits a later load", async () => {
16481667
const room = aliceTestClient.client.getRoom(ROOM_ID)!;
16491668
await Promise.all([room.loadMembersIfNeeded(), expectMembershipRequest(ROOM_ID, ["@bob:xyz"])]);
16501669

@@ -1676,7 +1695,7 @@ describe("megolm", () => {
16761695
// TODO: there are a bunch more tests for this sort of thing in spec/unit/crypto/algorithms/megolm.spec.ts.
16771696
// They should be converted to integ tests and moved.
16781697

1679-
it("does not block decryption on an 'm.unavailable' report", async function () {
1698+
oldBackendOnly("does not block decryption on an 'm.unavailable' report", async function () {
16801699
await aliceTestClient.start();
16811700

16821701
// there may be a key downloads for alice

spec/test-utils/test-utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,15 @@ export const mkPusher = (extra: Partial<IPusher> = {}): IPusher => ({
403403
pushkey: "pushpush",
404404
...extra,
405405
});
406+
407+
/**
408+
* a list of the supported crypto implementations, each with a callback to initialise that implementation
409+
* for the given client
410+
*/
411+
export const CRYPTO_BACKENDS: Record<string, InitCrypto> = {};
412+
export type InitCrypto = (_: MatrixClient) => Promise<void>;
413+
414+
CRYPTO_BACKENDS["rust-sdk"] = (client: MatrixClient) => client.initRustCrypto();
415+
if (global.Olm) {
416+
CRYPTO_BACKENDS["libolm"] = (client: MatrixClient) => client.initCrypto();
417+
}

0 commit comments

Comments
 (0)