|
| 1 | +/* |
| 2 | +Copyright 2023 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 * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js"; |
| 18 | +import fetchMock from "fetch-mock-jest"; |
| 19 | +import { Mocked } from "jest-mock"; |
| 20 | +import { KeysClaimRequest, UserId } from "@matrix-org/matrix-sdk-crypto-js"; |
| 21 | + |
| 22 | +import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; |
| 23 | +import { KeyClaimManager } from "../../../src/rust-crypto/KeyClaimManager"; |
| 24 | +import { TypedEventEmitter } from "../../../src/models/typed-event-emitter"; |
| 25 | +import { HttpApiEvent, HttpApiEventHandlerMap, MatrixHttpApi } from "../../../src"; |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + fetchMock.mockReset(); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("KeyClaimManager", () => { |
| 32 | + /* for these tests, we connect a KeyClaimManager to a mock OlmMachine, and a real OutgoingRequestProcessor |
| 33 | + * (which is connected to a mock fetch implementation) |
| 34 | + */ |
| 35 | + |
| 36 | + /** the KeyClaimManager implementation under test */ |
| 37 | + let keyClaimManager: KeyClaimManager; |
| 38 | + |
| 39 | + /** a mocked-up OlmMachine which the OutgoingRequestProcessor and KeyClaimManager are connected to */ |
| 40 | + let olmMachine: Mocked<RustSdkCryptoJs.OlmMachine>; |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + const dummyEventEmitter = new TypedEventEmitter<HttpApiEvent, HttpApiEventHandlerMap>(); |
| 44 | + const httpApi = new MatrixHttpApi(dummyEventEmitter, { |
| 45 | + baseUrl: "https://example.com", |
| 46 | + prefix: "/_matrix", |
| 47 | + onlyData: true, |
| 48 | + }); |
| 49 | + |
| 50 | + olmMachine = { |
| 51 | + getMissingSessions: jest.fn(), |
| 52 | + markRequestAsSent: jest.fn(), |
| 53 | + } as unknown as Mocked<RustSdkCryptoJs.OlmMachine>; |
| 54 | + |
| 55 | + const outgoingRequestProcessor = new OutgoingRequestProcessor(olmMachine, httpApi); |
| 56 | + |
| 57 | + keyClaimManager = new KeyClaimManager(olmMachine, outgoingRequestProcessor); |
| 58 | + }); |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns a promise which resolve once olmMachine.markRequestAsSent is called. |
| 62 | + * |
| 63 | + * The call itself will block initially. |
| 64 | + * |
| 65 | + * The promise returned by this function yields a callback function, which should be called to unblock the |
| 66 | + * markRequestAsSent call. |
| 67 | + */ |
| 68 | + function awaitCallToMarkRequestAsSent(): Promise<() => void> { |
| 69 | + return new Promise<() => void>((resolveCalledPromise, _reject) => { |
| 70 | + olmMachine.markRequestAsSent.mockImplementationOnce(async () => { |
| 71 | + // the mock implementation returns a promise... |
| 72 | + const completePromise = new Promise<void>((resolveCompletePromise, _reject) => { |
| 73 | + // ... and we now resolve the original promise with the resolver for that second promise. |
| 74 | + resolveCalledPromise(resolveCompletePromise); |
| 75 | + }); |
| 76 | + return completePromise; |
| 77 | + }); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + it("should claim missing keys", async () => { |
| 82 | + const u1 = new UserId("@alice:example.com"); |
| 83 | + const u2 = new UserId("@bob:example.com"); |
| 84 | + |
| 85 | + // stub out olmMachine.getMissingSessions(), with a result indicating that it needs a keyclaim |
| 86 | + const keysClaimRequest = new KeysClaimRequest("1234", '{ "k1": "v1" }'); |
| 87 | + olmMachine.getMissingSessions.mockResolvedValueOnce(keysClaimRequest); |
| 88 | + |
| 89 | + // have the claim request return a 200 |
| 90 | + fetchMock.postOnce("https://example.com/_matrix/client/v3/keys/claim", '{ "k": "v" }'); |
| 91 | + |
| 92 | + // also stub out olmMachine.markRequestAsSent |
| 93 | + olmMachine.markRequestAsSent.mockResolvedValueOnce(undefined); |
| 94 | + |
| 95 | + // fire off the request |
| 96 | + await keyClaimManager.ensureSessionsForUsers([u1, u2]); |
| 97 | + |
| 98 | + // check that all the calls were made |
| 99 | + expect(olmMachine.getMissingSessions).toHaveBeenCalledWith([u1, u2]); |
| 100 | + expect(fetchMock).toHaveFetched("https://example.com/_matrix/client/v3/keys/claim", { |
| 101 | + method: "POST", |
| 102 | + body: { k1: "v1" }, |
| 103 | + }); |
| 104 | + expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", keysClaimRequest.type, '{ "k": "v" }'); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should wait for previous claims to complete before making another", async () => { |
| 108 | + const u1 = new UserId("@alice:example.com"); |
| 109 | + const u2 = new UserId("@bob:example.com"); |
| 110 | + |
| 111 | + // stub out olmMachine.getMissingSessions(), with a result indicating that it needs a keyclaim |
| 112 | + const keysClaimRequest = new KeysClaimRequest("1234", '{ "k1": "v1" }'); |
| 113 | + olmMachine.getMissingSessions.mockResolvedValue(keysClaimRequest); |
| 114 | + |
| 115 | + // have the claim request return a 200 |
| 116 | + fetchMock.post("https://example.com/_matrix/client/v3/keys/claim", '{ "k": "v" }'); |
| 117 | + |
| 118 | + // stub out olmMachine.markRequestAsSent, and have it block |
| 119 | + let markRequestAsSentPromise = awaitCallToMarkRequestAsSent(); |
| 120 | + |
| 121 | + // fire off two requests, and keep track of whether their promises resolve |
| 122 | + let req1Resolved = false; |
| 123 | + keyClaimManager.ensureSessionsForUsers([u1]).then(() => { |
| 124 | + req1Resolved = true; |
| 125 | + }); |
| 126 | + let req2Resolved = false; |
| 127 | + const req2 = keyClaimManager.ensureSessionsForUsers([u2]).then(() => { |
| 128 | + req2Resolved = true; |
| 129 | + }); |
| 130 | + |
| 131 | + // now: wait for the (first) call to OlmMachine.markRequestAsSent |
| 132 | + let resolveMarkRequestAsSentCallback = await markRequestAsSentPromise; |
| 133 | + |
| 134 | + // at this point, there should have been a single call to getMissingSessions, and a single fetch; and neither |
| 135 | + // call to ensureSessionsAsUsers should have completed |
| 136 | + expect(olmMachine.getMissingSessions).toHaveBeenCalledWith([u1]); |
| 137 | + expect(olmMachine.getMissingSessions).toHaveBeenCalledTimes(1); |
| 138 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 139 | + expect(req1Resolved).toBe(false); |
| 140 | + expect(req2Resolved).toBe(false); |
| 141 | + |
| 142 | + // await the next call to markRequestAsSent, and release the first one |
| 143 | + markRequestAsSentPromise = awaitCallToMarkRequestAsSent(); |
| 144 | + resolveMarkRequestAsSentCallback(); |
| 145 | + resolveMarkRequestAsSentCallback = await markRequestAsSentPromise; |
| 146 | + |
| 147 | + // the first request should now have completed, and we should have more calls and fetches |
| 148 | + expect(olmMachine.getMissingSessions).toHaveBeenCalledWith([u2]); |
| 149 | + expect(olmMachine.getMissingSessions).toHaveBeenCalledTimes(2); |
| 150 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 151 | + expect(req1Resolved).toBe(true); |
| 152 | + expect(req2Resolved).toBe(false); |
| 153 | + |
| 154 | + // finally, release the second call to markRequestAsSent and check that the second request completes |
| 155 | + resolveMarkRequestAsSentCallback(); |
| 156 | + await req2; |
| 157 | + }); |
| 158 | +}); |
0 commit comments