You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-06 12:02:40 +03:00
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.
This commit is contained in:
committed by
GitHub
parent
789aec732a
commit
4847d78b42
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2016 OpenMarket Ltd
|
Copyright 2016 OpenMarket Ltd
|
||||||
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
|
Copyright 2019-2023 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@@ -59,13 +59,21 @@ interface ToDeviceEvent {
|
|||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// encrypt an event with olm
|
/** encrypt an event with an existing olm session */
|
||||||
function encryptOlmEvent(opts: {
|
function encryptOlmEvent(opts: {
|
||||||
|
/** the sender's user id */
|
||||||
sender?: string;
|
sender?: string;
|
||||||
|
/** the sender's curve25519 key */
|
||||||
senderKey: string;
|
senderKey: string;
|
||||||
|
/** the sender's ed25519 key */
|
||||||
|
senderSigningKey: string;
|
||||||
|
/** the olm session to use for encryption */
|
||||||
p2pSession: Olm.Session;
|
p2pSession: Olm.Session;
|
||||||
|
/** the recipient client */
|
||||||
recipient: TestClient;
|
recipient: TestClient;
|
||||||
|
/** the payload of the message */
|
||||||
plaincontent?: object;
|
plaincontent?: object;
|
||||||
|
/** the event type of the payload */
|
||||||
plaintype?: string;
|
plaintype?: string;
|
||||||
}): ToDeviceEvent {
|
}): ToDeviceEvent {
|
||||||
expect(opts.senderKey).toBeTruthy();
|
expect(opts.senderKey).toBeTruthy();
|
||||||
@@ -78,6 +86,9 @@ function encryptOlmEvent(opts: {
|
|||||||
recipient_keys: {
|
recipient_keys: {
|
||||||
ed25519: opts.recipient.getSigningKey(),
|
ed25519: opts.recipient.getSigningKey(),
|
||||||
},
|
},
|
||||||
|
keys: {
|
||||||
|
ed25519: opts.senderSigningKey,
|
||||||
|
},
|
||||||
sender: opts.sender || "@bob:xyz",
|
sender: opts.sender || "@bob:xyz",
|
||||||
type: opts.plaintype || "m.test",
|
type: opts.plaintype || "m.test",
|
||||||
};
|
};
|
||||||
@@ -101,7 +112,7 @@ function encryptMegolmEvent(opts: {
|
|||||||
groupSession: Olm.OutboundGroupSession;
|
groupSession: Olm.OutboundGroupSession;
|
||||||
plaintext?: Partial<IEvent>;
|
plaintext?: Partial<IEvent>;
|
||||||
room_id?: string;
|
room_id?: string;
|
||||||
}): Pick<IEvent, "event_id" | "content" | "type"> {
|
}): IEvent {
|
||||||
expect(opts.senderKey).toBeTruthy();
|
expect(opts.senderKey).toBeTruthy();
|
||||||
expect(opts.groupSession).toBeTruthy();
|
expect(opts.groupSession).toBeTruthy();
|
||||||
|
|
||||||
@@ -119,30 +130,44 @@ function encryptMegolmEvent(opts: {
|
|||||||
expect(opts.room_id).toBeTruthy();
|
expect(opts.room_id).toBeTruthy();
|
||||||
plaintext.room_id = opts.room_id;
|
plaintext.room_id = opts.room_id;
|
||||||
}
|
}
|
||||||
|
return encryptMegolmEventRawPlainText({ senderKey: opts.senderKey, groupSession: opts.groupSession, plaintext });
|
||||||
|
}
|
||||||
|
|
||||||
|
function encryptMegolmEventRawPlainText(opts: {
|
||||||
|
senderKey: string;
|
||||||
|
groupSession: Olm.OutboundGroupSession;
|
||||||
|
plaintext: Partial<IEvent>;
|
||||||
|
}): IEvent {
|
||||||
return {
|
return {
|
||||||
event_id: "test_megolm_event_" + Math.random(),
|
event_id: "$test_megolm_event_" + Math.random(),
|
||||||
|
sender: "@not_the_real_sender:example.com",
|
||||||
|
origin_server_ts: 1672944778000,
|
||||||
content: {
|
content: {
|
||||||
algorithm: "m.megolm.v1.aes-sha2",
|
algorithm: "m.megolm.v1.aes-sha2",
|
||||||
ciphertext: opts.groupSession.encrypt(JSON.stringify(plaintext)),
|
ciphertext: opts.groupSession.encrypt(JSON.stringify(opts.plaintext)),
|
||||||
device_id: "testDevice",
|
device_id: "testDevice",
|
||||||
sender_key: opts.senderKey,
|
sender_key: opts.senderKey,
|
||||||
session_id: opts.groupSession.session_id(),
|
session_id: opts.groupSession.session_id(),
|
||||||
},
|
},
|
||||||
type: "m.room.encrypted",
|
type: "m.room.encrypted",
|
||||||
|
unsigned: {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// build an encrypted room_key event to share a group session
|
/** build an encrypted room_key event to share a group session, using an existing olm session */
|
||||||
function encryptGroupSessionKey(opts: {
|
function encryptGroupSessionKey(opts: {
|
||||||
senderKey: string;
|
|
||||||
recipient: TestClient;
|
recipient: TestClient;
|
||||||
|
/** sender's olm account */
|
||||||
|
olmAccount: Olm.Account;
|
||||||
|
/** sender's olm session with the recipient */
|
||||||
p2pSession: Olm.Session;
|
p2pSession: Olm.Session;
|
||||||
groupSession: Olm.OutboundGroupSession;
|
groupSession: Olm.OutboundGroupSession;
|
||||||
room_id?: string;
|
room_id?: string;
|
||||||
}): Partial<IEvent> {
|
}): Partial<IEvent> {
|
||||||
|
const senderKeys = JSON.parse(opts.olmAccount.identity_keys());
|
||||||
return encryptOlmEvent({
|
return encryptOlmEvent({
|
||||||
senderKey: opts.senderKey,
|
senderKey: senderKeys.curve25519,
|
||||||
|
senderSigningKey: senderKeys.ed25519,
|
||||||
recipient: opts.recipient,
|
recipient: opts.recipient,
|
||||||
p2pSession: opts.p2pSession,
|
p2pSession: opts.p2pSession,
|
||||||
plaincontent: {
|
plaincontent: {
|
||||||
@@ -219,6 +244,7 @@ async function establishOlmSession(testClient: TestClient, peerOlmAccount: Olm.A
|
|||||||
const p2pSession = await createOlmSession(peerOlmAccount, testClient);
|
const p2pSession = await createOlmSession(peerOlmAccount, testClient);
|
||||||
const olmEvent = encryptOlmEvent({
|
const olmEvent = encryptOlmEvent({
|
||||||
senderKey: peerE2EKeys.curve25519,
|
senderKey: peerE2EKeys.curve25519,
|
||||||
|
senderSigningKey: peerE2EKeys.ed25519,
|
||||||
recipient: testClient,
|
recipient: testClient,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
});
|
});
|
||||||
@@ -392,7 +418,9 @@ describe("megolm", () => {
|
|||||||
testSenderKey = testE2eKeys.curve25519;
|
testSenderKey = testE2eKeys.curve25519;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => aliceTestClient.stop());
|
afterEach(async () => {
|
||||||
|
await aliceTestClient.stop();
|
||||||
|
});
|
||||||
|
|
||||||
it("Alice receives a megolm message", async () => {
|
it("Alice receives a megolm message", async () => {
|
||||||
await aliceTestClient.start();
|
await aliceTestClient.start();
|
||||||
@@ -405,8 +433,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event
|
// make the room_key event
|
||||||
const roomKeyEncrypted = encryptGroupSessionKey({
|
const roomKeyEncrypted = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -456,8 +484,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event, but don't send it yet
|
// make the room_key event, but don't send it yet
|
||||||
const roomKeyEncrypted = encryptGroupSessionKey({
|
const roomKeyEncrypted = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -516,8 +544,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event
|
// make the room_key event
|
||||||
const roomKeyEncrypted1 = encryptGroupSessionKey({
|
const roomKeyEncrypted1 = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -533,8 +561,8 @@ describe("megolm", () => {
|
|||||||
// make a second room_key event now that we have advanced the group
|
// make a second room_key event now that we have advanced the group
|
||||||
// session.
|
// session.
|
||||||
const roomKeyEncrypted2 = encryptGroupSessionKey({
|
const roomKeyEncrypted2 = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -958,8 +986,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event
|
// make the room_key event
|
||||||
const roomKeyEncrypted = encryptGroupSessionKey({
|
const roomKeyEncrypted = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -1088,8 +1116,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event
|
// make the room_key event
|
||||||
const roomKeyEncrypted = encryptGroupSessionKey({
|
const roomKeyEncrypted = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -1101,17 +1129,11 @@ describe("megolm", () => {
|
|||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
};
|
};
|
||||||
|
|
||||||
const messageEncrypted = {
|
const messageEncrypted = encryptMegolmEventRawPlainText({
|
||||||
event_id: "test_megolm_event",
|
senderKey: testSenderKey,
|
||||||
content: {
|
groupSession: groupSession,
|
||||||
algorithm: "m.megolm.v1.aes-sha2",
|
plaintext: plaintext,
|
||||||
ciphertext: groupSession.encrypt(JSON.stringify(plaintext)),
|
});
|
||||||
device_id: "testDevice",
|
|
||||||
sender_key: testSenderKey,
|
|
||||||
session_id: groupSession.session_id(),
|
|
||||||
},
|
|
||||||
type: "m.room.encrypted",
|
|
||||||
};
|
|
||||||
|
|
||||||
// Alice gets both the events in a single sync
|
// Alice gets both the events in a single sync
|
||||||
const syncResponse = {
|
const syncResponse = {
|
||||||
@@ -1149,8 +1171,8 @@ describe("megolm", () => {
|
|||||||
|
|
||||||
// make the room_key event
|
// make the room_key event
|
||||||
const roomKeyEncrypted = encryptGroupSessionKey({
|
const roomKeyEncrypted = encryptGroupSessionKey({
|
||||||
senderKey: testSenderKey,
|
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
|
olmAccount: testOlmAccount,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
groupSession: groupSession,
|
groupSession: groupSession,
|
||||||
room_id: ROOM_ID,
|
room_id: ROOM_ID,
|
||||||
@@ -1268,6 +1290,7 @@ describe("megolm", () => {
|
|||||||
);
|
);
|
||||||
const encryptedForwardedKey = encryptOlmEvent({
|
const encryptedForwardedKey = encryptOlmEvent({
|
||||||
sender: "@becca:localhost",
|
sender: "@becca:localhost",
|
||||||
|
senderSigningKey: beccaTestClient.getSigningKey(),
|
||||||
senderKey: beccaTestClient.getDeviceKey(),
|
senderKey: beccaTestClient.getDeviceKey(),
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
@@ -1413,6 +1436,7 @@ describe("megolm", () => {
|
|||||||
const encryptedForwardedKey = encryptOlmEvent({
|
const encryptedForwardedKey = encryptOlmEvent({
|
||||||
sender: "@becca:localhost",
|
sender: "@becca:localhost",
|
||||||
senderKey: beccaTestClient.getDeviceKey(),
|
senderKey: beccaTestClient.getDeviceKey(),
|
||||||
|
senderSigningKey: beccaTestClient.getSigningKey(),
|
||||||
recipient: aliceTestClient,
|
recipient: aliceTestClient,
|
||||||
p2pSession: p2pSession,
|
p2pSession: p2pSession,
|
||||||
plaincontent: {
|
plaincontent: {
|
||||||
|
Reference in New Issue
Block a user