2020-03-30 11:18:47 +02:00
|
|
|
import { shieldStatusForRoom } from '../../src/utils/ShieldUtils';
|
2020-03-27 16:25:44 +01:00
|
|
|
import DMRoomMap from '../../src/utils/DMRoomMap';
|
|
|
|
|
2020-03-27 16:37:59 +01:00
|
|
|
function mkClient(selfTrust) {
|
2020-03-27 16:25:44 +01:00
|
|
|
return {
|
|
|
|
getUserId: () => "@self:localhost",
|
|
|
|
checkUserTrust: (userId) => ({
|
2020-03-27 16:37:59 +01:00
|
|
|
isCrossSigningVerified: () => userId[1] == "T",
|
2020-03-30 17:33:16 +02:00
|
|
|
wasCrossSigningVerified: () => userId[1] == "T" || userId[1] == "W",
|
2020-03-27 16:25:44 +01:00
|
|
|
}),
|
|
|
|
checkDeviceTrust: (userId, deviceId) => ({
|
2020-03-27 16:37:59 +01:00
|
|
|
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
|
2020-03-27 16:25:44 +01:00
|
|
|
}),
|
2020-04-28 19:35:16 +02:00
|
|
|
getStoredDevicesForUser: (userId) => ["DEVICE"],
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-27 16:25:44 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 16:37:59 +01:00
|
|
|
describe("mkClient self-test", function() {
|
2020-03-27 16:25:44 +01:00
|
|
|
test.each([true, false])("behaves well for self-trust=%s", (v) => {
|
|
|
|
const client = mkClient(v);
|
|
|
|
expect(client.checkDeviceTrust("@self:localhost", "DEVICE").isVerified()).toBe(v);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
["@TT:h", true],
|
|
|
|
["@TF:h", true],
|
|
|
|
["@FT:h", false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["@FF:h", false]],
|
2021-04-27 17:23:27 +02:00
|
|
|
)("behaves well for user trust %s", (userId, trust) => {
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(mkClient().checkUserTrust(userId).isCrossSigningVerified()).toBe(trust);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
["@TT:h", true],
|
|
|
|
["@TF:h", false],
|
|
|
|
["@FT:h", true],
|
2020-03-27 16:37:59 +01:00
|
|
|
["@FF:h", false]],
|
2021-04-27 17:23:27 +02:00
|
|
|
)("behaves well for device trust %s", (userId, trust) => {
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(mkClient().checkDeviceTrust(userId, "device").isVerified()).toBe(trust);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-27 16:37:59 +01:00
|
|
|
describe("shieldStatusForMembership self-trust behaviour", function() {
|
2020-03-27 16:25:44 +01:00
|
|
|
beforeAll(() => {
|
2021-01-13 17:02:35 +01:00
|
|
|
DMRoomMap.sharedInstance = {
|
2020-03-27 16:25:44 +01:00
|
|
|
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[[true, true], [true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
[false, true], [false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("2 unverified: returns 'normal', self-trust = %s, DM = %s", async (trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual("normal");
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["verified", true, true], ["verified", true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["verified", false, true], ["warning", false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("2 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["normal", true, true], ["normal", true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["normal", false, true], ["warning", false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("2 mixed: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["verified", true, true], ["verified", true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["warning", false, true], ["warning", false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("0 others: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["verified", true, true], ["verified", true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["verified", false, true], ["verified", false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("1 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["normal", true, true], ["normal", true, false],
|
2020-03-27 16:37:59 +01:00
|
|
|
["normal", false, true], ["normal", false, false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("1 unverified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
|
|
|
|
const client = mkClient(trusted);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-27 16:37:59 +01:00
|
|
|
describe("shieldStatusForMembership other-trust behaviour", function() {
|
2020-03-27 16:25:44 +01:00
|
|
|
beforeAll(() => {
|
2021-04-23 13:19:08 +02:00
|
|
|
DMRoomMap.sharedInstance = {
|
2020-03-27 16:25:44 +01:00
|
|
|
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
2020-03-27 16:37:59 +01:00
|
|
|
[["warning", true], ["warning", false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("1 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
2020-03-27 16:37:59 +01:00
|
|
|
[["warning", true], ["warning", false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("2 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2020-03-30 17:33:16 +02:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
2020-03-27 16:37:59 +01:00
|
|
|
[["normal", true], ["normal", false]],
|
2020-03-27 16:25:44 +01:00
|
|
|
)("2 unverified/untrusted: returns '%s', DM = %s", async (result, dm) => {
|
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
2020-03-30 17:33:16 +02:00
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({userId})),
|
|
|
|
};
|
|
|
|
const status = await shieldStatusForRoom(client, room);
|
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
|
|
|
[["warning", true], ["warning", false]],
|
|
|
|
)("2 was verified: returns '%s', DM = %s", async (result, dm) => {
|
|
|
|
const client = mkClient(true);
|
|
|
|
const room = {
|
|
|
|
roomId: dm ? "DM" : "other",
|
|
|
|
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({userId})),
|
2020-03-27 16:37:59 +01:00
|
|
|
};
|
2020-03-30 11:18:47 +02:00
|
|
|
const status = await shieldStatusForRoom(client, room);
|
2020-03-27 16:25:44 +01:00
|
|
|
expect(status).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|