2022-05-02 10:57:35 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-02 10:57:35 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-02 10:57:35 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2023-09-18 19:34:00 +03:00
|
|
|
import { UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";
|
2022-05-02 10:57:35 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import { shieldStatusForRoom } from "../../src/utils/ShieldUtils";
|
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
2020-03-27 18:25:44 +03:00
|
|
|
|
2022-05-02 10:57:35 +03:00
|
|
|
function mkClient(selfTrust = false) {
|
2020-03-27 18:25:44 +03:00
|
|
|
return {
|
|
|
|
getUserId: () => "@self:localhost",
|
2023-04-24 16:19:46 +03:00
|
|
|
getCrypto: () => ({
|
|
|
|
getDeviceVerificationStatus: (userId: string, deviceId: string) =>
|
|
|
|
Promise.resolve({
|
|
|
|
isVerified: () => (userId === "@self:localhost" ? selfTrust : userId[2] == "T"),
|
|
|
|
}),
|
2023-05-19 13:57:45 +03:00
|
|
|
getUserDeviceInfo: async (userIds: string[]) => {
|
|
|
|
return new Map(userIds.map((u) => [u, new Map([["DEVICE", {}]])]));
|
|
|
|
},
|
2023-09-18 19:34:00 +03:00
|
|
|
getUserVerificationStatus: async (userId: string): Promise<UserVerificationStatus> =>
|
|
|
|
new UserVerificationStatus(userId[1] == "T", userId[1] == "T" || userId[1] == "W", false),
|
2020-03-27 18:25:44 +03:00
|
|
|
}),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as MatrixClient;
|
2020-03-27 18:25:44 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("mkClient self-test", function () {
|
2023-04-24 16:19:46 +03:00
|
|
|
test.each([true, false])("behaves well for self-trust=%s", async (v) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(v);
|
2023-04-24 16:19:46 +03:00
|
|
|
const status = await client.getCrypto()!.getDeviceVerificationStatus("@self:localhost", "DEVICE");
|
|
|
|
expect(status?.isVerified()).toBe(v);
|
2020-03-27 18:25:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
["@TT:h", true],
|
|
|
|
["@TF:h", true],
|
|
|
|
["@FT:h", false],
|
2022-12-12 14:24:14 +03:00
|
|
|
["@FF:h", false],
|
2023-09-18 19:34:00 +03:00
|
|
|
])("behaves well for user trust %s", async (userId, trust) => {
|
|
|
|
const status = await mkClient().getCrypto()?.getUserVerificationStatus(userId);
|
|
|
|
expect(status!.isCrossSigningVerified()).toBe(trust);
|
2020-03-27 18:25:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
["@TT:h", true],
|
|
|
|
["@TF:h", false],
|
|
|
|
["@FT:h", true],
|
2022-12-12 14:24:14 +03:00
|
|
|
["@FF:h", false],
|
2023-04-24 16:19:46 +03:00
|
|
|
])("behaves well for device trust %s", async (userId, trust) => {
|
|
|
|
const status = await mkClient().getCrypto()!.getDeviceVerificationStatus(userId, "device");
|
|
|
|
expect(status?.isVerified()).toBe(trust);
|
2020-03-27 18:25:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("shieldStatusForMembership self-trust behaviour", function () {
|
2020-03-27 18:25:44 +03:00
|
|
|
beforeAll(() => {
|
2022-05-02 10:57:35 +03:00
|
|
|
const mockInstance = {
|
2023-02-13 14:39:16 +03:00
|
|
|
getUserIdForRoomId: (roomId: string) => (roomId === "DM" ? "@any:h" : null),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as DMRoomMap;
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance);
|
2022-05-02 10:57:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(DMRoomMap, "shared").mockRestore();
|
2020-03-27 18:25:44 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
[true, true],
|
|
|
|
[true, false],
|
|
|
|
[false, true],
|
|
|
|
[false, false],
|
|
|
|
])("2 unverified: returns 'normal', self-trust = %s, DM = %s", async (trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual("normal");
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["verified", true, true],
|
|
|
|
["verified", true, false],
|
|
|
|
["verified", false, true],
|
|
|
|
["warning", false, false],
|
|
|
|
])("2 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["normal", true, true],
|
|
|
|
["normal", true, false],
|
|
|
|
["normal", false, true],
|
|
|
|
["warning", false, false],
|
|
|
|
])("2 mixed: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["verified", true, true],
|
|
|
|
["verified", true, false],
|
|
|
|
["warning", false, true],
|
|
|
|
["warning", false, false],
|
|
|
|
])("0 others: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["verified", true, true],
|
|
|
|
["verified", true, false],
|
|
|
|
["verified", false, true],
|
|
|
|
["verified", false, false],
|
|
|
|
])("1 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["normal", true, true],
|
|
|
|
["normal", true, false],
|
|
|
|
["normal", false, true],
|
|
|
|
["normal", false, false],
|
|
|
|
])("1 unverified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("shieldStatusForMembership other-trust behaviour", function () {
|
2020-03-27 18:25:44 +03:00
|
|
|
beforeAll(() => {
|
2022-05-02 10:57:35 +03:00
|
|
|
const mockInstance = {
|
2023-02-13 14:39:16 +03:00
|
|
|
getUserIdForRoomId: (roomId: string) => (roomId === "DM" ? "@any:h" : null),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as DMRoomMap;
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance);
|
2020-03-27 18:25:44 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["warning", true],
|
|
|
|
["warning", false],
|
|
|
|
])("1 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["warning", true],
|
|
|
|
["warning", false],
|
|
|
|
])("2 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["normal", true],
|
|
|
|
["normal", false],
|
|
|
|
])("2 unverified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
2020-03-27 18:25:44 +03:00
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 18:33:16 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each([
|
|
|
|
["warning", true],
|
|
|
|
["warning", false],
|
|
|
|
])("2 was verified: returns '%s', DM = %s", async (result, dm) => {
|
2020-03-30 18:33:16 +03:00
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2021-06-29 15:11:58 +03:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })),
|
2022-05-02 10:57:35 +03:00
|
|
|
} as unknown as Room;
|
2020-03-30 12:18:47 +03:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 18:25:44 +03:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|