Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { Crypto } from "@peculiar/webcrypto";

const webCrypto = new Crypto();

function getRandomValues(buf) {
function getRandomValues<T extends ArrayBufferView>(buf: T): T {
// @ts-ignore fussy generics
return nodeCrypto.randomFillSync(buf);
}

Expand Down Expand Up @@ -64,15 +65,18 @@ const TEST_VECTORS=[
],
];

function stringToArray(s) {
function stringToArray(s: string): ArrayBufferLike {
return new TextEncoder().encode(s).buffer;
}

describe('MegolmExportEncryption', function() {
let MegolmExportEncryption;

beforeAll(() => {
window.crypto = { subtle: webCrypto.subtle, getRandomValues };
window.crypto = {
subtle: webCrypto.subtle,
getRandomValues,
};
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
});

Expand Down
59 changes: 43 additions & 16 deletions test/utils/ShieldUtils-test.js → test/utils/ShieldUtils-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
/*
Comment thread
turt2live marked this conversation as resolved.
Copyright 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import {
MatrixClient,
Room,
} from 'matrix-js-sdk/src/matrix';

import { shieldStatusForRoom } from '../../src/utils/ShieldUtils';
import DMRoomMap from '../../src/utils/DMRoomMap';

function mkClient(selfTrust) {
function mkClient(selfTrust = false) {
return {
getUserId: () => "@self:localhost",
checkUserTrust: (userId) => ({
Expand All @@ -12,7 +33,7 @@ function mkClient(selfTrust) {
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
}),
getStoredDevicesForUser: (userId) => ["DEVICE"],
};
} as unknown as MatrixClient;
}

describe("mkClient self-test", function() {
Expand Down Expand Up @@ -42,9 +63,14 @@ describe("mkClient self-test", function() {

describe("shieldStatusForMembership self-trust behaviour", function() {
beforeAll(() => {
DMRoomMap.sharedInstance = {
const mockInstance = {
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
};
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance);
});

afterAll(() => {
jest.spyOn(DMRoomMap, 'shared').mockRestore();
});

it.each(
Expand All @@ -55,7 +81,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual("normal");
});
Expand All @@ -68,7 +94,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -81,7 +107,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -94,7 +120,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -107,7 +133,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -120,17 +146,18 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
});

describe("shieldStatusForMembership other-trust behaviour", function() {
beforeAll(() => {
DMRoomMap.sharedInstance = {
const mockInstance = {
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
};
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance);
});

it.each(
Expand All @@ -140,7 +167,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -152,7 +179,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -164,7 +191,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand All @@ -176,7 +203,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
const room = {
roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })),
};
} as unknown as Room;
const status = await shieldStatusForRoom(client, room);
expect(status).toEqual(result);
});
Expand Down