This commit is contained in:
Zoe 2020-03-27 15:37:59 +00:00
parent fda533ab48
commit 4f3d4426ea

View file

@ -1,20 +1,20 @@
import { shieldStatusForMembership } from '../../src/utils/ShieldUtils'; import { shieldStatusForMembership } from '../../src/utils/ShieldUtils';
import DMRoomMap from '../../src/utils/DMRoomMap'; import DMRoomMap from '../../src/utils/DMRoomMap';
function mkClient (selfTrust) { function mkClient(selfTrust) {
return { return {
getUserId: () => "@self:localhost", getUserId: () => "@self:localhost",
checkUserTrust: (userId) => ({ checkUserTrust: (userId) => ({
isCrossSigningVerified: () => userId[1] == "T" isCrossSigningVerified: () => userId[1] == "T",
}), }),
checkDeviceTrust: (userId, deviceId) => ({ checkDeviceTrust: (userId, deviceId) => ({
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T" isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
}), }),
getStoredDevicesForUser: async (userId) => ["DEVICE"], getStoredDevicesForUser: async (userId) => ["DEVICE"],
} };
} }
describe("mkClient self-test", function () { describe("mkClient self-test", function() {
test.each([true, false])("behaves well for self-trust=%s", (v) => { test.each([true, false])("behaves well for self-trust=%s", (v) => {
const client = mkClient(v); const client = mkClient(v);
expect(client.checkDeviceTrust("@self:localhost", "DEVICE").isVerified()).toBe(v); expect(client.checkDeviceTrust("@self:localhost", "DEVICE").isVerified()).toBe(v);
@ -24,8 +24,8 @@ describe("mkClient self-test", function () {
["@TT:h", true], ["@TT:h", true],
["@TF:h", true], ["@TF:h", true],
["@FT:h", false], ["@FT:h", false],
["@FF:h", false]]) ["@FF:h", false]],
("behaves well for user trust %s", (userId, trust) => { )("behaves well for user trust %s", (userId, trust) => {
expect(mkClient().checkUserTrust(userId).isCrossSigningVerified()).toBe(trust); expect(mkClient().checkUserTrust(userId).isCrossSigningVerified()).toBe(trust);
}); });
@ -33,13 +33,13 @@ describe("mkClient self-test", function () {
["@TT:h", true], ["@TT:h", true],
["@TF:h", false], ["@TF:h", false],
["@FT:h", true], ["@FT:h", true],
["@FF:h", false]]) ["@FF:h", false]],
("behaves well for device trust %s", (userId, trust) => { )("behaves well for device trust %s", (userId, trust) => {
expect(mkClient().checkDeviceTrust(userId, "device").isVerified()).toBe(trust); expect(mkClient().checkDeviceTrust(userId, "device").isVerified()).toBe(trust);
}); });
}); });
describe("shieldStatusForMembership self-trust behaviour", function () { describe("shieldStatusForMembership self-trust behaviour", function() {
beforeAll(() => { beforeAll(() => {
DMRoomMap._sharedInstance = { DMRoomMap._sharedInstance = {
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null, getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
@ -48,84 +48,84 @@ describe("shieldStatusForMembership self-trust behaviour", function () {
it.each( it.each(
[[true, true], [true, false], [[true, true], [true, false],
[false, true], [false, false]] [false, true], [false, false]],
)("2 unverified: returns 'normal', self-trust = %s, DM = %s", async (trusted, dm) => { )("2 unverified: returns 'normal', self-trust = %s, DM = %s", async (trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual("normal"); expect(status).toEqual("normal");
}); });
it.each( it.each(
[["verified", true, true], ["verified", true, false], [["verified", true, true], ["verified", true, false],
["verified", false, true], ["warning", false, false]] ["verified", false, true], ["warning", false, false]],
)("2 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => { )("2 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["normal", true, true], ["normal", true, false], [["normal", true, true], ["normal", true, false],
["normal", false, true], ["warning", false, false]] ["normal", false, true], ["warning", false, false]],
)("2 mixed: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => { )("2 mixed: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["verified", true, true], ["verified", true, false], [["verified", true, true], ["verified", true, false],
["warning", false, true], ["warning", false, false]] ["warning", false, true], ["warning", false, false]],
)("0 others: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => { )("0 others: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["verified", true, true], ["verified", true, false], [["verified", true, true], ["verified", true, false],
["verified", false, true], ["verified", false, false]] ["verified", false, true], ["verified", false, false]],
)("1 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => { )("1 verified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["normal", true, true], ["normal", true, false], [["normal", true, true], ["normal", true, false],
["normal", false, true], ["normal", false, false]] ["normal", false, true], ["normal", false, false]],
)("1 unverified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => { )("1 unverified: returns '%s', self-trust = %s, DM = %s", async (result, trusted, dm) => {
const client = mkClient(trusted); const client = mkClient(trusted);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
}); });
describe("shieldStatusForMembership other-trust behaviour", function () { describe("shieldStatusForMembership other-trust behaviour", function() {
beforeAll(() => { beforeAll(() => {
DMRoomMap._sharedInstance = { DMRoomMap._sharedInstance = {
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null, getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
@ -133,37 +133,37 @@ describe("shieldStatusForMembership other-trust behaviour", function () {
}); });
it.each( it.each(
[["warning", true], ["warning", false]] [["warning", true], ["warning", false]],
)("1 verified/untrusted: returns '%s', DM = %s", async (result, dm) => { )("1 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
const client = mkClient(true); const client = mkClient(true);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["warning", true], ["warning", false]] [["warning", true], ["warning", false]],
)("2 verified/untrusted: returns '%s', DM = %s", async (result, dm) => { )("2 verified/untrusted: returns '%s', DM = %s", async (result, dm) => {
const client = mkClient(true); const client = mkClient(true);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT: h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT: h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });
it.each( it.each(
[["normal", true], ["normal", false]] [["normal", true], ["normal", false]],
)("2 unverified/untrusted: returns '%s', DM = %s", async (result, dm) => { )("2 unverified/untrusted: returns '%s', DM = %s", async (result, dm) => {
const client = mkClient(true); const client = mkClient(true);
const room = { const room = {
roomId: dm ? "DM" : "other", roomId: dm ? "DM" : "other",
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT: h"].map((userId) => ({userId})), getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT: h"].map((userId) => ({userId})),
} };
const status = await shieldStatusForMembership(client, room); const status = await shieldStatusForMembership(client, room);
expect(status).toEqual(result); expect(status).toEqual(result);
}); });