Skip to content

Commit 4847d78

Browse files
authored
Improvements to megolm integration tests (#3060)
The megolm tests were making a few assumptions which they really shouldn't; in particular: * They were creating mock events with event_ids not starting `$`, and lacking `sender`, `origin_server_ts` and `unsigned` properties * They were not including the (now) required `keys.ed25519` property inside the ciphertext of an olm message. These work ok currently, but they aren't really correct, and they cause problems when testing the new rust implementation.
1 parent 789aec7 commit 4847d78

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

spec/integ/megolm-integ.spec.ts

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright 2016 OpenMarket Ltd
3-
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
3+
Copyright 2019-2023 The Matrix.org Foundation C.I.C.
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -59,13 +59,21 @@ interface ToDeviceEvent {
5959
type: string;
6060
}
6161

62-
// encrypt an event with olm
62+
/** encrypt an event with an existing olm session */
6363
function encryptOlmEvent(opts: {
64+
/** the sender's user id */
6465
sender?: string;
66+
/** the sender's curve25519 key */
6567
senderKey: string;
68+
/** the sender's ed25519 key */
69+
senderSigningKey: string;
70+
/** the olm session to use for encryption */
6671
p2pSession: Olm.Session;
72+
/** the recipient client */
6773
recipient: TestClient;
74+
/** the payload of the message */
6875
plaincontent?: object;
76+
/** the event type of the payload */
6977
plaintype?: string;
7078
}): ToDeviceEvent {
7179
expect(opts.senderKey).toBeTruthy();
@@ -78,6 +86,9 @@ function encryptOlmEvent(opts: {
7886
recipient_keys: {
7987
ed25519: opts.recipient.getSigningKey(),
8088
},
89+
keys: {
90+
ed25519: opts.senderSigningKey,
91+
},
8192
sender: opts.sender || "@bob:xyz",
8293
type: opts.plaintype || "m.test",
8394
};
@@ -101,7 +112,7 @@ function encryptMegolmEvent(opts: {
101112
groupSession: Olm.OutboundGroupSession;
102113
plaintext?: Partial<IEvent>;
103114
room_id?: string;
104-
}): Pick<IEvent, "event_id" | "content" | "type"> {
115+
}): IEvent {
105116
expect(opts.senderKey).toBeTruthy();
106117
expect(opts.groupSession).toBeTruthy();
107118

@@ -119,30 +130,44 @@ function encryptMegolmEvent(opts: {
119130
expect(opts.room_id).toBeTruthy();
120131
plaintext.room_id = opts.room_id;
121132
}
133+
return encryptMegolmEventRawPlainText({ senderKey: opts.senderKey, groupSession: opts.groupSession, plaintext });
134+
}
122135

136+
function encryptMegolmEventRawPlainText(opts: {
137+
senderKey: string;
138+
groupSession: Olm.OutboundGroupSession;
139+
plaintext: Partial<IEvent>;
140+
}): IEvent {
123141
return {
124-
event_id: "test_megolm_event_" + Math.random(),
142+
event_id: "$test_megolm_event_" + Math.random(),
143+
sender: "@not_the_real_sender:example.com",
144+
origin_server_ts: 1672944778000,
125145
content: {
126146
algorithm: "m.megolm.v1.aes-sha2",
127-
ciphertext: opts.groupSession.encrypt(JSON.stringify(plaintext)),
147+
ciphertext: opts.groupSession.encrypt(JSON.stringify(opts.plaintext)),
128148
device_id: "testDevice",
129149
sender_key: opts.senderKey,
130150
session_id: opts.groupSession.session_id(),
131151
},
132152
type: "m.room.encrypted",
153+
unsigned: {},
133154
};
134155
}
135156

136-
// build an encrypted room_key event to share a group session
157+
/** build an encrypted room_key event to share a group session, using an existing olm session */
137158
function encryptGroupSessionKey(opts: {
138-
senderKey: string;
139159
recipient: TestClient;
160+
/** sender's olm account */
161+
olmAccount: Olm.Account;
162+
/** sender's olm session with the recipient */
140163
p2pSession: Olm.Session;
141164
groupSession: Olm.OutboundGroupSession;
142165
room_id?: string;
143166
}): Partial<IEvent> {
167+
const senderKeys = JSON.parse(opts.olmAccount.identity_keys());
144168
return encryptOlmEvent({
145-
senderKey: opts.senderKey,
169+
senderKey: senderKeys.curve25519,
170+
senderSigningKey: senderKeys.ed25519,
146171
recipient: opts.recipient,
147172
p2pSession: opts.p2pSession,
148173
plaincontent: {
@@ -219,6 +244,7 @@ async function establishOlmSession(testClient: TestClient, peerOlmAccount: Olm.A
219244
const p2pSession = await createOlmSession(peerOlmAccount, testClient);
220245
const olmEvent = encryptOlmEvent({
221246
senderKey: peerE2EKeys.curve25519,
247+
senderSigningKey: peerE2EKeys.ed25519,
222248
recipient: testClient,
223249
p2pSession: p2pSession,
224250
});
@@ -392,7 +418,9 @@ describe("megolm", () => {
392418
testSenderKey = testE2eKeys.curve25519;
393419
});
394420

395-
afterEach(() => aliceTestClient.stop());
421+
afterEach(async () => {
422+
await aliceTestClient.stop();
423+
});
396424

397425
it("Alice receives a megolm message", async () => {
398426
await aliceTestClient.start();
@@ -405,8 +433,8 @@ describe("megolm", () => {
405433

406434
// make the room_key event
407435
const roomKeyEncrypted = encryptGroupSessionKey({
408-
senderKey: testSenderKey,
409436
recipient: aliceTestClient,
437+
olmAccount: testOlmAccount,
410438
p2pSession: p2pSession,
411439
groupSession: groupSession,
412440
room_id: ROOM_ID,
@@ -456,8 +484,8 @@ describe("megolm", () => {
456484

457485
// make the room_key event, but don't send it yet
458486
const roomKeyEncrypted = encryptGroupSessionKey({
459-
senderKey: testSenderKey,
460487
recipient: aliceTestClient,
488+
olmAccount: testOlmAccount,
461489
p2pSession: p2pSession,
462490
groupSession: groupSession,
463491
room_id: ROOM_ID,
@@ -516,8 +544,8 @@ describe("megolm", () => {
516544

517545
// make the room_key event
518546
const roomKeyEncrypted1 = encryptGroupSessionKey({
519-
senderKey: testSenderKey,
520547
recipient: aliceTestClient,
548+
olmAccount: testOlmAccount,
521549
p2pSession: p2pSession,
522550
groupSession: groupSession,
523551
room_id: ROOM_ID,
@@ -533,8 +561,8 @@ describe("megolm", () => {
533561
// make a second room_key event now that we have advanced the group
534562
// session.
535563
const roomKeyEncrypted2 = encryptGroupSessionKey({
536-
senderKey: testSenderKey,
537564
recipient: aliceTestClient,
565+
olmAccount: testOlmAccount,
538566
p2pSession: p2pSession,
539567
groupSession: groupSession,
540568
room_id: ROOM_ID,
@@ -958,8 +986,8 @@ describe("megolm", () => {
958986

959987
// make the room_key event
960988
const roomKeyEncrypted = encryptGroupSessionKey({
961-
senderKey: testSenderKey,
962989
recipient: aliceTestClient,
990+
olmAccount: testOlmAccount,
963991
p2pSession: p2pSession,
964992
groupSession: groupSession,
965993
room_id: ROOM_ID,
@@ -1088,8 +1116,8 @@ describe("megolm", () => {
10881116

10891117
// make the room_key event
10901118
const roomKeyEncrypted = encryptGroupSessionKey({
1091-
senderKey: testSenderKey,
10921119
recipient: aliceTestClient,
1120+
olmAccount: testOlmAccount,
10931121
p2pSession: p2pSession,
10941122
groupSession: groupSession,
10951123
room_id: ROOM_ID,
@@ -1101,17 +1129,11 @@ describe("megolm", () => {
11011129
room_id: ROOM_ID,
11021130
};
11031131

1104-
const messageEncrypted = {
1105-
event_id: "test_megolm_event",
1106-
content: {
1107-
algorithm: "m.megolm.v1.aes-sha2",
1108-
ciphertext: groupSession.encrypt(JSON.stringify(plaintext)),
1109-
device_id: "testDevice",
1110-
sender_key: testSenderKey,
1111-
session_id: groupSession.session_id(),
1112-
},
1113-
type: "m.room.encrypted",
1114-
};
1132+
const messageEncrypted = encryptMegolmEventRawPlainText({
1133+
senderKey: testSenderKey,
1134+
groupSession: groupSession,
1135+
plaintext: plaintext,
1136+
});
11151137

11161138
// Alice gets both the events in a single sync
11171139
const syncResponse = {
@@ -1149,8 +1171,8 @@ describe("megolm", () => {
11491171

11501172
// make the room_key event
11511173
const roomKeyEncrypted = encryptGroupSessionKey({
1152-
senderKey: testSenderKey,
11531174
recipient: aliceTestClient,
1175+
olmAccount: testOlmAccount,
11541176
p2pSession: p2pSession,
11551177
groupSession: groupSession,
11561178
room_id: ROOM_ID,
@@ -1268,6 +1290,7 @@ describe("megolm", () => {
12681290
);
12691291
const encryptedForwardedKey = encryptOlmEvent({
12701292
sender: "@becca:localhost",
1293+
senderSigningKey: beccaTestClient.getSigningKey(),
12711294
senderKey: beccaTestClient.getDeviceKey(),
12721295
recipient: aliceTestClient,
12731296
p2pSession: p2pSession,
@@ -1413,6 +1436,7 @@ describe("megolm", () => {
14131436
const encryptedForwardedKey = encryptOlmEvent({
14141437
sender: "@becca:localhost",
14151438
senderKey: beccaTestClient.getDeviceKey(),
1439+
senderSigningKey: beccaTestClient.getSigningKey(),
14161440
recipient: aliceTestClient,
14171441
p2pSession: p2pSession,
14181442
plaincontent: {

0 commit comments

Comments
 (0)