|
| 1 | +/* |
| 2 | +Copyright 2025 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { type Mocked } from "jest-mock"; |
| 18 | + |
| 19 | +import { makeMockEvent, membershipTemplate, mockCallMembership } from "./mocks"; |
| 20 | +import { ClientEvent, EventType, type MatrixClient } from "../../../src"; |
| 21 | +import { ToDeviceKeyTransport } from "../../../src/matrixrtc/ToDeviceKeyTransport.ts"; |
| 22 | +import { getMockClientWithEventEmitter } from "../../test-utils/client.ts"; |
| 23 | +import { type Statistics } from "../../../src/matrixrtc"; |
| 24 | +import { KeyTransportEvents } from "../../../src/matrixrtc/IKeyTransport.ts"; |
| 25 | +import { defer } from "../../../src/utils.ts"; |
| 26 | +import { type Logger } from "../../../src/logger.ts"; |
| 27 | + |
| 28 | +describe("ToDeviceKeyTransport", () => { |
| 29 | + const roomId = "!room:id"; |
| 30 | + |
| 31 | + let mockClient: Mocked<MatrixClient>; |
| 32 | + let statistics: Statistics; |
| 33 | + let mockLogger: Mocked<Logger>; |
| 34 | + let transport: ToDeviceKeyTransport; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + mockClient = getMockClientWithEventEmitter({ |
| 38 | + encryptAndSendToDevice: jest.fn(), |
| 39 | + }); |
| 40 | + mockLogger = { |
| 41 | + debug: jest.fn(), |
| 42 | + warn: jest.fn(), |
| 43 | + } as unknown as Mocked<Logger>; |
| 44 | + statistics = { |
| 45 | + counters: { |
| 46 | + roomEventEncryptionKeysSent: 0, |
| 47 | + roomEventEncryptionKeysReceived: 0, |
| 48 | + }, |
| 49 | + totals: { |
| 50 | + roomEventEncryptionKeysReceivedTotalAge: 0, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + transport = new ToDeviceKeyTransport("@alice:example.org", "MYDEVICE", roomId, mockClient, statistics, { |
| 55 | + getChild: jest.fn().mockReturnValue(mockLogger), |
| 56 | + } as unknown as Mocked<Logger>); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should send my keys on via to device", async () => { |
| 60 | + transport.start(); |
| 61 | + |
| 62 | + const keyBase64Encoded = "ABCDEDF"; |
| 63 | + const keyIndex = 2; |
| 64 | + await transport.sendKey(keyBase64Encoded, keyIndex, [ |
| 65 | + mockCallMembership( |
| 66 | + Object.assign({}, membershipTemplate, { device_id: "BOBDEVICE" }), |
| 67 | + roomId, |
| 68 | + "@bob:example.org", |
| 69 | + ), |
| 70 | + mockCallMembership( |
| 71 | + Object.assign({}, membershipTemplate, { device_id: "CARLDEVICE" }), |
| 72 | + roomId, |
| 73 | + "@carl:example.org", |
| 74 | + ), |
| 75 | + mockCallMembership( |
| 76 | + Object.assign({}, membershipTemplate, { device_id: "MATDEVICE" }), |
| 77 | + roomId, |
| 78 | + "@mat:example.org", |
| 79 | + ), |
| 80 | + ]); |
| 81 | + |
| 82 | + expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledTimes(1); |
| 83 | + expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledWith( |
| 84 | + "io.element.call.encryption_keys", |
| 85 | + [ |
| 86 | + { userId: "@bob:example.org", deviceId: "BOBDEVICE" }, |
| 87 | + { userId: "@carl:example.org", deviceId: "CARLDEVICE" }, |
| 88 | + { userId: "@mat:example.org", deviceId: "MATDEVICE" }, |
| 89 | + ], |
| 90 | + { |
| 91 | + keys: { |
| 92 | + index: keyIndex, |
| 93 | + key: keyBase64Encoded, |
| 94 | + }, |
| 95 | + member: { |
| 96 | + claimed_device_id: "MYDEVICE", |
| 97 | + }, |
| 98 | + room_id: roomId, |
| 99 | + session: { |
| 100 | + application: "m.call", |
| 101 | + call_id: "", |
| 102 | + scope: "m.room", |
| 103 | + }, |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + expect(statistics.counters.roomEventEncryptionKeysSent).toBe(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should emit when a key is received", async () => { |
| 111 | + const deferred = defer<{ userId: string; deviceId: string; keyBase64Encoded: string; index: number }>(); |
| 112 | + transport.on(KeyTransportEvents.ReceivedKeys, (userId, deviceId, keyBase64Encoded, index, timestamp) => { |
| 113 | + deferred.resolve({ userId, deviceId, keyBase64Encoded, index }); |
| 114 | + }); |
| 115 | + transport.start(); |
| 116 | + |
| 117 | + const testEncoded = "ABCDEDF"; |
| 118 | + const testKeyIndex = 2; |
| 119 | + |
| 120 | + mockClient.emit( |
| 121 | + ClientEvent.ToDeviceEvent, |
| 122 | + makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, { |
| 123 | + keys: { |
| 124 | + index: testKeyIndex, |
| 125 | + key: testEncoded, |
| 126 | + }, |
| 127 | + member: { |
| 128 | + claimed_device_id: "BOBDEVICE", |
| 129 | + }, |
| 130 | + room_id: roomId, |
| 131 | + session: { |
| 132 | + application: "m.call", |
| 133 | + call_id: "", |
| 134 | + scope: "m.room", |
| 135 | + }, |
| 136 | + }), |
| 137 | + ); |
| 138 | + |
| 139 | + const { userId, deviceId, keyBase64Encoded, index } = await deferred.promise; |
| 140 | + expect(userId).toBe("@bob:example.org"); |
| 141 | + expect(deviceId).toBe("BOBDEVICE"); |
| 142 | + expect(keyBase64Encoded).toBe(testEncoded); |
| 143 | + expect(index).toBe(testKeyIndex); |
| 144 | + |
| 145 | + expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(1); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should not sent to ourself", async () => { |
| 149 | + const keyBase64Encoded = "ABCDEDF"; |
| 150 | + const keyIndex = 2; |
| 151 | + await transport.sendKey(keyBase64Encoded, keyIndex, [ |
| 152 | + mockCallMembership( |
| 153 | + Object.assign({}, membershipTemplate, { device_id: "MYDEVICE" }), |
| 154 | + roomId, |
| 155 | + "@alice:example.org", |
| 156 | + ), |
| 157 | + ]); |
| 158 | + |
| 159 | + transport.start(); |
| 160 | + |
| 161 | + expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledTimes(0); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should warn when there is a room mismatch", () => { |
| 165 | + transport.start(); |
| 166 | + |
| 167 | + const testEncoded = "ABCDEDF"; |
| 168 | + const testKeyIndex = 2; |
| 169 | + |
| 170 | + mockClient.emit( |
| 171 | + ClientEvent.ToDeviceEvent, |
| 172 | + makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, { |
| 173 | + keys: { |
| 174 | + index: testKeyIndex, |
| 175 | + key: testEncoded, |
| 176 | + }, |
| 177 | + member: { |
| 178 | + claimed_device_id: "BOBDEVICE", |
| 179 | + }, |
| 180 | + room_id: "!anotherroom:id", |
| 181 | + session: { |
| 182 | + application: "m.call", |
| 183 | + call_id: "", |
| 184 | + scope: "m.room", |
| 185 | + }, |
| 186 | + }), |
| 187 | + ); |
| 188 | + |
| 189 | + expect(mockLogger.warn).toHaveBeenCalledWith("Malformed Event: Mismatch roomId"); |
| 190 | + expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(0); |
| 191 | + }); |
| 192 | + |
| 193 | + describe("malformed events", () => { |
| 194 | + const MALFORMED_EVENT = [ |
| 195 | + { |
| 196 | + keys: {}, |
| 197 | + member: { claimed_device_id: "MYDEVICE" }, |
| 198 | + room_id: "!room:id", |
| 199 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 200 | + }, |
| 201 | + { |
| 202 | + keys: { index: 0 }, |
| 203 | + member: { claimed_device_id: "MYDEVICE" }, |
| 204 | + room_id: "!room:id", |
| 205 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 206 | + }, |
| 207 | + { |
| 208 | + keys: { keys: "ABCDEF" }, |
| 209 | + member: { claimed_device_id: "MYDEVICE" }, |
| 210 | + room_id: "!room:id", |
| 211 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 212 | + }, |
| 213 | + { |
| 214 | + keys: { keys: "ABCDEF", index: 2 }, |
| 215 | + room_id: "!room:id", |
| 216 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 217 | + }, |
| 218 | + { |
| 219 | + keys: { keys: "ABCDEF", index: 2 }, |
| 220 | + member: {}, |
| 221 | + room_id: "!room:id", |
| 222 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 223 | + }, |
| 224 | + { |
| 225 | + keys: { keys: "ABCDEF", index: 2 }, |
| 226 | + member: { claimed_device_id: "MYDEVICE" }, |
| 227 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 228 | + }, |
| 229 | + { |
| 230 | + keys: { keys: "ABCDEF", index: 2 }, |
| 231 | + member: { claimed_device_id: "MYDEVICE" }, |
| 232 | + room_id: "!room:id", |
| 233 | + session: { application: "m.call", call_id: "", scope: "m.room" }, |
| 234 | + }, |
| 235 | + ]; |
| 236 | + |
| 237 | + test.each(MALFORMED_EVENT)("should warn on malformed event %j", (event) => { |
| 238 | + transport.start(); |
| 239 | + |
| 240 | + mockClient.emit( |
| 241 | + ClientEvent.ToDeviceEvent, |
| 242 | + makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, event), |
| 243 | + ); |
| 244 | + |
| 245 | + expect(mockLogger.warn).toHaveBeenCalled(); |
| 246 | + expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(0); |
| 247 | + }); |
| 248 | + }); |
| 249 | +}); |
0 commit comments