Skip to content

Commit 5c01a2d

Browse files
committed
update test to fail without the fixed encryption key index
1 parent 96382ca commit 5c01a2d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ describe("MatrixRTCSession", () => {
835835
it("rotates key if a member leaves", async () => {
836836
jest.useFakeTimers();
837837
try {
838+
const KEY_DALAY = 3000;
838839
const member2 = Object.assign({}, membershipTemplate, {
839840
device_id: "BBBBBBB",
840841
});
@@ -855,7 +856,8 @@ describe("MatrixRTCSession", () => {
855856
sendEventMock.mockImplementation((_roomId, _evType, payload) => resolve(payload));
856857
});
857858

858-
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
859+
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true, makeKeyDelay: KEY_DALAY });
860+
const sendKeySpy = jest.spyOn((sess as unknown as any).encryptionManager.transport, "sendKey");
859861
const firstKeysPayload = await keysSentPromise1;
860862
expect(firstKeysPayload.keys).toHaveLength(1);
861863
expect(firstKeysPayload.keys[0].index).toEqual(0);
@@ -872,14 +874,24 @@ describe("MatrixRTCSession", () => {
872874
.mockReturnValue(makeMockRoomState([membershipTemplate], mockRoom.roomId));
873875
sess.onRTCSessionMemberUpdate();
874876

875-
jest.advanceTimersByTime(10000);
877+
jest.advanceTimersByTime(3000);
878+
expect(sendKeySpy).toHaveBeenCalledTimes(1);
879+
// check that we send the key with index 1 even though the send gets delayed when leaving.
880+
// this makes sure we do not use an index that is one too old.
881+
expect(sendKeySpy).toHaveBeenLastCalledWith(expect.any(String), 1, sess.memberships);
882+
// fake a condition in which we send another encryption key event.
883+
// this could happen do to someone joining the call.
884+
(sess as unknown as any).encryptionManager.sendEncryptionKeysEvent();
885+
expect(sendKeySpy).toHaveBeenLastCalledWith(expect.any(String), 1, sess.memberships);
886+
jest.advanceTimersByTime(7000);
876887

877888
const secondKeysPayload = await keysSentPromise2;
878889

879890
expect(secondKeysPayload.keys).toHaveLength(1);
880891
expect(secondKeysPayload.keys[0].index).toEqual(1);
881892
expect(onMyEncryptionKeyChanged).toHaveBeenCalledTimes(2);
882-
expect(sess!.statistics.counters.roomEventEncryptionKeysSent).toEqual(2);
893+
// initial, on leave and the fake one we do with: `(sess as unknown as any).encryptionManager.sendEncryptionKeysEvent();`
894+
expect(sess!.statistics.counters.roomEventEncryptionKeysSent).toEqual(3);
883895
} finally {
884896
jest.useRealTimers();
885897
}

0 commit comments

Comments
 (0)