Skip to content

Commit 65a32d4

Browse files
committed
Run some of the megolm integ tests against rust sdk
1 parent 4f1ef35 commit 65a32d4

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

spec/integ/megolm-integ.spec.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717

1818
import anotherjson from "another-json";
1919
import MockHttpBackend from "matrix-mock-request";
20+
import "fake-indexeddb/auto";
21+
import { IDBFactory } from "fake-indexeddb";
2022

2123
import * as testUtils from "../test-utils/test-utils";
2224
import { TestClient } from "../TestClient";
@@ -38,9 +40,17 @@ import {
3840
} from "../../src/matrix";
3941
import { IDeviceKeys } from "../../src/crypto/dehydration";
4042
import { DeviceInfo } from "../../src/crypto/deviceinfo";
43+
import { CRYPTO_BACKENDS, InitCrypto } from "../test-utils/test-utils";
4144

4245
const ROOM_ID = "!room:id";
4346

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

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

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

411427
beforeEach(async () => {
412428
aliceTestClient = new TestClient("@alice:localhost", "xzcvb", "akjgkrgjs");
413-
await aliceTestClient.client.initCrypto();
429+
await initCrypto(aliceTestClient.client);
414430

431+
// create a test olm device which we will use to communicate with alice. We use libolm to implement this.
432+
await Olm.init();
415433
testOlmAccount = new Olm.Account();
416434
testOlmAccount.create();
417435
const testE2eKeys = JSON.parse(testOlmAccount.identity_keys());
@@ -615,7 +633,7 @@ describe("megolm", () => {
615633
expect(event.getContent().body).toEqual("42");
616634
});
617635

618-
it("Alice sends a megolm message", async () => {
636+
oldBackendOnly("Alice sends a megolm message", async () => {
619637
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
620638
await aliceTestClient.start();
621639
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -658,7 +676,7 @@ describe("megolm", () => {
658676
]);
659677
});
660678

661-
it("We shouldn't attempt to send to blocked devices", async () => {
679+
oldBackendOnly("We shouldn't attempt to send to blocked devices", async () => {
662680
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
663681
await aliceTestClient.start();
664682
await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -702,7 +720,7 @@ describe("megolm", () => {
702720
expect(() => aliceTestClient.client.getGlobalErrorOnUnknownDevices()).toThrowError("encryption disabled");
703721
});
704722

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

708726
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
@@ -760,7 +778,7 @@ describe("megolm", () => {
760778
);
761779
});
762780

763-
it("should disable sending to unverified devices", async () => {
781+
oldBackendOnly("should disable sending to unverified devices", async () => {
764782
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
765783
await aliceTestClient.start();
766784
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -818,7 +836,7 @@ describe("megolm", () => {
818836
});
819837
});
820838

821-
it("We should start a new megolm session when a device is blocked", async () => {
839+
oldBackendOnly("We should start a new megolm session when a device is blocked", async () => {
822840
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
823841
await aliceTestClient.start();
824842
const p2pSession = await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -876,7 +894,7 @@ describe("megolm", () => {
876894
});
877895

878896
// https://github.com/vector-im/element-web/issues/2676
879-
it("Alice should send to her other devices", async () => {
897+
oldBackendOnly("Alice should send to her other devices", async () => {
880898
// for this test, we make the testOlmAccount be another of Alice's devices.
881899
// it ought to get included in messages Alice sends.
882900
await aliceTestClient.start();
@@ -957,7 +975,7 @@ describe("megolm", () => {
957975
expect(decrypted.content?.body).toEqual("test");
958976
});
959977

960-
it("Alice should wait for device list to complete when sending a megolm message", async () => {
978+
oldBackendOnly("Alice should wait for device list to complete when sending a megolm message", async () => {
961979
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
962980
await aliceTestClient.start();
963981
await establishOlmSession(aliceTestClient, testOlmAccount);
@@ -1047,7 +1065,7 @@ describe("megolm", () => {
10471065
aliceTestClient.stop();
10481066

10491067
aliceTestClient = new TestClient("@alice:localhost", "device2", "access_token2");
1050-
await aliceTestClient.client.initCrypto();
1068+
await initCrypto(aliceTestClient.client);
10511069
await aliceTestClient.client.importRoomKeys(exported);
10521070
await aliceTestClient.start();
10531071

@@ -1189,7 +1207,7 @@ describe("megolm", () => {
11891207
expect(decryptedEvent.getClearContent()).toBeUndefined();
11901208
});
11911209

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

@@ -1341,7 +1359,7 @@ describe("megolm", () => {
13411359
await beccaTestClient.stop();
13421360
});
13431361

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

@@ -1487,7 +1505,7 @@ describe("megolm", () => {
14871505
await beccaTestClient.stop();
14881506
});
14891507

1490-
it("allows sending an encrypted event as soon as room state arrives", async () => {
1508+
oldBackendOnly("allows sending an encrypted event as soon as room state arrives", async () => {
14911509
/* Empirically, clients expect to be able to send encrypted events as soon as the
14921510
* RoomStateEvent.NewMember notification is emitted, so test that works correctly.
14931511
*/
@@ -1612,7 +1630,7 @@ describe("megolm", () => {
16121630
await aliceTestClient.httpBackend.flush(membersPath, 1);
16131631
}
16141632

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

@@ -1644,7 +1662,7 @@ describe("megolm", () => {
16441662
]);
16451663
});
16461664

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

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

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

16821700
// 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)