Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valere/element r/bump bindings 2.1.0 wip #3816

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
"dependencies": {
"@babel/runtime": "^7.12.5",
"@matrix-org/matrix-sdk-crypto-wasm": "^2.0.0",
"@matrix-org/matrix-sdk-crypto-wasm": "^2.1.0",
"another-json": "^0.2.0",
"bs58": "^5.0.0",
"content-type": "^1.0.4",
Expand Down
4 changes: 3 additions & 1 deletion spec/unit/rust-crypto/KeyClaimManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { KeyClaimManager } from "../../../src/rust-crypto/KeyClaimManager";
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter";
import { HttpApiEvent, HttpApiEventHandlerMap, MatrixHttpApi } from "../../../src";
import { RequestSender } from "../../../lib/rust-crypto/RequestSender";

Check failure on line 26 in spec/unit/rust-crypto/KeyClaimManager.spec.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Cannot find module '../../../lib/rust-crypto/RequestSender' or its corresponding type declarations.

afterEach(() => {
fetchMock.mockReset();
Expand Down Expand Up @@ -52,7 +53,8 @@
markRequestAsSent: jest.fn(),
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>;

const outgoingRequestProcessor = new OutgoingRequestProcessor(olmMachine, httpApi);
const requestSender = new RequestSender(httpApi);
const outgoingRequestProcessor = new OutgoingRequestProcessor(olmMachine, requestSender);

keyClaimManager = new KeyClaimManager(olmMachine, outgoingRequestProcessor);
});
Expand Down
45 changes: 24 additions & 21 deletions spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ import {

import { TypedEventEmitter } from "../../../src/models/typed-event-emitter";
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, MatrixHttpApi, UIAuthCallback } from "../../../src";
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
import { OutgoingRequest, OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
import { defer } from "../../../src/utils";
import { RequestSender } from "../../../src/rust-crypto/RequestSender";

describe("OutgoingRequestProcessor", () => {
/** the OutgoingRequestProcessor implementation under test */
let processor: OutgoingRequestProcessor;
/** the RequestSender implementation under test */
let requestSender: RequestSender;

/** A mock http backend which processor is connected to */
let httpBackend: MockHttpBackend;
Expand Down Expand Up @@ -67,7 +70,8 @@ describe("OutgoingRequestProcessor", () => {
markRequestAsSent: jest.fn(),
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>;

processor = new OutgoingRequestProcessor(olmMachine, httpApi);
requestSender = new RequestSender(httpApi);
processor = new OutgoingRequestProcessor(olmMachine, requestSender);
});

/* simple requests that map directly to the request body */
Expand All @@ -82,12 +86,12 @@ describe("OutgoingRequestProcessor", () => {
"https://example.com/_matrix/client/v3/keys/signatures/upload",
],
["KeysBackupRequest", KeysBackupRequest, "PUT", "https://example.com/_matrix/client/v3/room_keys/keys"],
[
"SigningKeysUploadRequest",
SigningKeysUploadRequest,
"POST",
"https://example.com/_matrix/client/v3/keys/device_signing/upload",
],
// [
// "SigningKeysUploadRequest",
// SigningKeysUploadRequest,
// "POST",
// "https://example.com/_matrix/client/v3/keys/device_signing/upload",
// ],
];

test.each(tests)(`should handle %ss`, async (_, RequestClass, expectedMethod, expectedPath) => {
Expand All @@ -96,7 +100,7 @@ describe("OutgoingRequestProcessor", () => {
const outgoingRequest = new RequestClass("1234", testBody);

// ... then poke it into the OutgoingRequestProcessor under test.
const reqProm = processor.makeOutgoingRequest(outgoingRequest);
const reqProm = processor.sendOutgoingRequest(outgoingRequest);

// Now: check that it makes a matching HTTP request ...
const testResponse = '{ "result": 1 }';
Expand Down Expand Up @@ -125,7 +129,7 @@ describe("OutgoingRequestProcessor", () => {
const outgoingRequest = new ToDeviceRequest("1234", "test/type", "test/txnid", testBody);

// ... then poke it into the OutgoingRequestProcessor under test.
const reqProm = processor.makeOutgoingRequest(outgoingRequest);
const reqProm = processor.sendOutgoingRequest(outgoingRequest);

// Now: check that it makes a matching HTTP request ...
const testResponse = '{ "result": 1 }';
Expand Down Expand Up @@ -154,7 +158,7 @@ describe("OutgoingRequestProcessor", () => {
const outgoingRequest = new RoomMessageRequest("1234", "test/room", "test/txnid", "test/type", testBody);

// ... then poke it into the OutgoingRequestProcessor under test.
const reqProm = processor.makeOutgoingRequest(outgoingRequest);
const reqProm = processor.sendOutgoingRequest(outgoingRequest);

// Now: check that it makes a matching HTTP request ...
const testResponse = '{ "result": 1 }';
Expand Down Expand Up @@ -182,15 +186,15 @@ describe("OutgoingRequestProcessor", () => {
it("should handle SigningKeysUploadRequests with UIA", async () => {
// first, mock up a request as we might expect to receive it from the Rust layer ...
const testReq = { foo: "bar" };
const outgoingRequest = new SigningKeysUploadRequest("1234", JSON.stringify(testReq));
const outgoingRequest = new SigningKeysUploadRequest(JSON.stringify(testReq));

// also create a UIA callback
const authCallback: UIAuthCallback<Object> = async (makeRequest) => {
return await makeRequest({ type: "test" });
};

// ... then poke the request into the OutgoingRequestProcessor under test
const reqProm = processor.makeOutgoingRequest(outgoingRequest, authCallback);
const reqProm = processor.requestSender.createHttpRequest(outgoingRequest, authCallback);

// Now: check that it makes a matching HTTP request ...
const testResponse = '{"result":1}';
Expand All @@ -204,19 +208,18 @@ describe("OutgoingRequestProcessor", () => {
})
.respond(200, testResponse, true);

// ... and that it calls OlmMachine.markAsSent.
const markSentCallPromise = awaitCallToMarkAsSent();
await httpBackend.flushAllExpected();

await Promise.all([reqProm, markSentCallPromise]);
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse);
await reqProm;
httpBackend.verifyNoOutstandingRequests();
});

it("does not explode with unknown requests", async () => {
const outgoingRequest = { id: "5678", type: 987 };
const markSentCallPromise = awaitCallToMarkAsSent();
await Promise.all([processor.makeOutgoingRequest(outgoingRequest), markSentCallPromise]);
await Promise.all([
processor.sendOutgoingRequest(outgoingRequest as any as OutgoingRequest),
markSentCallPromise,
]);
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("5678", 987, "");
});

Expand All @@ -236,12 +239,12 @@ describe("OutgoingRequestProcessor", () => {
return await authRequestResultDefer.promise;
},
} as unknown as Mocked<MatrixHttpApi<IHttpOpts & { onlyData: true }>>;
processor = new OutgoingRequestProcessor(olmMachine, mockHttpApi);
processor = new OutgoingRequestProcessor(olmMachine, new RequestSender(mockHttpApi));
});

// build a request
const request = olmMachine.queryKeysForUsers([new RustSdkCryptoJs.UserId("@bob:example.com")]);
const result = processor.makeOutgoingRequest(request);
const result = processor.sendOutgoingRequest(request);

// wait for the HTTP request to be made
await authRequestCalledPromise;
Expand Down
12 changes: 7 additions & 5 deletions spec/unit/rust-crypto/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,13 @@
*/
function awaitCallToMakeOutgoingRequest(): Promise<() => void> {
return new Promise<() => void>((resolveCalledPromise, _reject) => {
outgoingRequestProcessor.makeOutgoingRequest.mockImplementationOnce(async () => {
outgoingRequestProcessor.processOutgoingRequests.mockImplementationOnce(async () => {
const completePromise = new Promise<void>((resolveCompletePromise, _reject) => {
resolveCalledPromise(resolveCompletePromise);
});
return completePromise;
});
outgoingRequestProcessor.sendOutgoingRequest.mockImplementationOnce(async () => "{}");
});
}

Expand All @@ -336,7 +337,8 @@
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>;

outgoingRequestProcessor = {
makeOutgoingRequest: jest.fn(),
processOutgoingRequests: jest.fn(),
sendOutgoingRequest: jest.fn(),
} as unknown as Mocked<OutgoingRequestProcessor>;

rustCrypto = new RustCrypto(
Expand All @@ -360,7 +362,7 @@

await makeRequestPromise;
expect(olmMachine.outgoingRequests).toHaveBeenCalled();
expect(outgoingRequestProcessor.makeOutgoingRequest).toHaveBeenCalledWith(testReq);
expect(outgoingRequestProcessor.processOutgoingRequests).toHaveBeenCalledWith([testReq]);
});

it("should go round the loop again if another sync completes while the first `outgoingRequests` is running", async () => {
Expand All @@ -385,7 +387,7 @@
// we should have a second call queued. It should trigger a call to `makeOutgoingRequest`.
firstOutgoingRequestsDefer.resolve([]);
await awaitCallToMakeOutgoingRequest();
expect(olmMachine.outgoingRequests).toHaveBeenCalledTimes(2);

Check failure on line 390 in spec/unit/rust-crypto/rust-crypto.spec.ts

View workflow job for this annotation

GitHub Actions / Jest [unit] (Node 18)

RustCrypto › outgoing requests › should go round the loop again if another sync completes while the first `outgoingRequests` is running

expect(jest.fn()).toHaveBeenCalledTimes(expected) Expected number of calls: 2 Received number of calls: 1 at Object.toHaveBeenCalledTimes (spec/unit/rust-crypto/rust-crypto.spec.ts:390:49)

Check failure on line 390 in spec/unit/rust-crypto/rust-crypto.spec.ts

View workflow job for this annotation

GitHub Actions / Jest [unit] (Node latest)

RustCrypto › outgoing requests › should go round the loop again if another sync completes while the first `outgoingRequests` is running

expect(jest.fn()).toHaveBeenCalledTimes(expected) Expected number of calls: 2 Received number of calls: 1 at Object.toHaveBeenCalledTimes (spec/unit/rust-crypto/rust-crypto.spec.ts:390:49)
});

it("stops looping when stop() is called", async () => {
Expand All @@ -412,7 +414,7 @@
rustCrypto.onSyncCompleted({});

resolveMakeRequest = await makeRequestPromise;
outgoingRequestProcessor.makeOutgoingRequest.mockReset();
outgoingRequestProcessor.processOutgoingRequests.mockReset();
resolveMakeRequest();

// now stop...
Expand All @@ -425,7 +427,7 @@
setTimeout(resolve, 100);
});
expect(rustCrypto["outgoingRequestLoopRunning"]).toBeFalsy();
expect(outgoingRequestProcessor.makeOutgoingRequest).not.toHaveBeenCalled();
expect(outgoingRequestProcessor.processOutgoingRequests).not.toHaveBeenCalled();
expect(olmMachine.outgoingRequests).not.toHaveBeenCalled();

// we sent three, so there should be 2 left
Expand Down
14 changes: 10 additions & 4 deletions src/rust-crypto/CrossSigningIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class CrossSigningIdentity {

// Sign the device with our cross-signing key and upload the signature
const request: RustSdkCryptoJs.SignatureUploadRequest = await device.verify();
await this.outgoingRequestProcessor.makeOutgoingRequest(request);
await this.outgoingRequestProcessor.sendOutgoingRequest(request);
} else {
logger.log(
"bootStrapCrossSigning: Cross-signing private keys not found locally or in secret storage, creating new keys",
Expand Down Expand Up @@ -128,9 +128,15 @@ export class CrossSigningIdentity {
await this.exportCrossSigningKeysToStorage();
}
logger.log("bootStrapCrossSigning: publishing keys to server");
for (const req of outgoingRequests) {
await this.outgoingRequestProcessor.makeOutgoingRequest(req, authUploadDeviceSigningKeys);
}

// TODO this do not need to be marked as sent as stated by the API doc
// https://github.com/matrix-org/matrix-rust-sdk/issues/2749
// Just send and bubble in case of error
await Promise.all(
outgoingRequests.map((req) => {
this.outgoingRequestProcessor.requestSender.createHttpRequest(req, authUploadDeviceSigningKeys);
}),
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/rust-crypto/KeyClaimManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class KeyClaimManager {
}
const claimRequest = await this.olmMachine.getMissingSessions(userList);
if (claimRequest) {
await this.outgoingRequestProcessor.makeOutgoingRequest(claimRequest);
await this.outgoingRequestProcessor.sendOutgoingRequest(claimRequest);
}
}
}
Loading
Loading