Skip to content

Commit

Permalink
Merge branch 'develop' into florianduros/rip-out-legacy-crypto/remove…
Browse files Browse the repository at this point in the history
…-initLegacyCrypto

# Conflicts:
#	spec/integ/sliding-sync-sdk.spec.ts
  • Loading branch information
florianduros committed Jan 17, 2025
2 parents c01acbc + a6fd28b commit 1e86e77
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
58 changes: 57 additions & 1 deletion spec/integ/sliding-sync-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { logger } from "../../src/logger";
import { emitPromise } from "../test-utils/test-utils";
import { defer } from "../../src/utils";
import { KnownMembership } from "../../src/@types/membership";
import { SyncCryptoCallbacks } from "../../src/common-crypto/CryptoBackend";

declare module "../../src/@types/event" {
interface AccountDataEvents {
Expand All @@ -57,6 +58,7 @@ describe("SlidingSyncSdk", () => {
let httpBackend: MockHttpBackend | undefined;
let sdk: SlidingSyncSdk | undefined;
let mockSlidingSync: SlidingSync | undefined;
let syncCryptoCallback: SyncCryptoCallbacks | undefined;
const selfUserId = "@alice:localhost";
const selfAccessToken = "aseukfgwef";

Expand Down Expand Up @@ -117,13 +119,19 @@ describe("SlidingSyncSdk", () => {
};

// assign client/httpBackend globals
const setupClient = async (testOpts?: Partial<IStoredClientOpts>) => {
const setupClient = async (testOpts?: Partial<IStoredClientOpts & { withCrypto: boolean }>) => {
testOpts = testOpts || {};
const syncOpts: SyncApiOptions = {};
const testClient = new TestClient(selfUserId, "DEVICE", selfAccessToken);
httpBackend = testClient.httpBackend;
client = testClient.client;
mockSlidingSync = mockifySlidingSync(new SlidingSync("", new Map(), {}, client, 0));
if (testOpts.withCrypto) {
httpBackend!.when("GET", "/room_keys/version").respond(404, {});
await client!.initRustCrypto({ useIndexedDB: false });
syncCryptoCallback = client!.getCrypto() as unknown as SyncCryptoCallbacks;
syncOpts.cryptoCallbacks = syncCryptoCallback;
}
httpBackend!.when("GET", "/_matrix/client/v3/pushrules").respond(200, {});
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);
};
Expand Down Expand Up @@ -622,6 +630,54 @@ describe("SlidingSyncSdk", () => {
});
});

describe("ExtensionE2EE", () => {
let ext: Extension<any, any>;

beforeAll(async () => {
await setupClient({
withCrypto: true,
});
const hasSynced = sdk!.sync();
await httpBackend!.flushAllExpected();
await hasSynced;
ext = findExtension("e2ee");
});

it("gets enabled on the initial request only", () => {
expect(ext.onRequest(true)).toEqual({
enabled: true,
});
expect(ext.onRequest(false)).toEqual(undefined);
});

it("can update device lists", () => {
syncCryptoCallback!.processDeviceLists = jest.fn();
ext.onResponse({
device_lists: {
changed: ["@alice:localhost"],
left: ["@bob:localhost"],
},
});
expect(syncCryptoCallback!.processDeviceLists).toHaveBeenCalledWith({
changed: ["@alice:localhost"],
left: ["@bob:localhost"],
});
});

it("can update OTK counts and unused fallback keys", () => {
syncCryptoCallback!.processKeyCounts = jest.fn();
ext.onResponse({
device_one_time_keys_count: {
signed_curve25519: 42,
},
device_unused_fallback_key_types: ["signed_curve25519"],
});
expect(syncCryptoCallback!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
"signed_curve25519",
]);
});
});

describe("ExtensionAccountData", () => {
let ext: Extension<any, any>;

Expand Down
7 changes: 3 additions & 4 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
SetPresence,
} from "./sync.ts";
import { MatrixEvent } from "./models/event.ts";
import { Crypto } from "./crypto/index.ts";
import { IMinimalEvent, IRoomEvent, IStateEvent, IStrippedState, ISyncResponse } from "./sync-accumulator.ts";
import { MatrixError } from "./http-api/index.ts";
import {
Expand Down Expand Up @@ -66,7 +65,7 @@ type ExtensionE2EEResponse = Pick<
>;

class ExtensionE2EE implements Extension<ExtensionE2EERequest, ExtensionE2EEResponse> {
public constructor(private readonly crypto: Crypto) {}
public constructor(private readonly crypto: SyncCryptoCallbacks) {}

public name(): string {
return "e2ee";
Expand Down Expand Up @@ -373,8 +372,8 @@ export class SlidingSyncSdk {
new ExtensionTyping(this.client),
new ExtensionReceipts(this.client),
];
if (this.syncOpts.crypto) {
extensions.push(new ExtensionE2EE(this.syncOpts.crypto));
if (this.syncOpts.cryptoCallbacks) {
extensions.push(new ExtensionE2EE(this.syncOpts.cryptoCallbacks));
}
extensions.forEach((ext) => {
this.slidingSync.registerExtension(ext);
Expand Down

0 comments on commit 1e86e77

Please sign in to comment.