2018-10-25 02:01:08 +02:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
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 expect from 'expect';
|
|
|
|
import peg from '../src/MatrixClientPeg';
|
2018-10-27 03:47:53 +02:00
|
|
|
import {
|
|
|
|
makeGroupPermalink,
|
|
|
|
makeRoomPermalink,
|
|
|
|
makeUserPermalink,
|
2019-02-21 17:06:06 +01:00
|
|
|
RoomPermaLinkCreator,
|
2018-10-27 03:47:53 +02:00
|
|
|
} from "../src/matrix-to";
|
2018-10-25 23:22:28 +02:00
|
|
|
import * as testUtils from "./test-utils";
|
2018-10-25 02:01:08 +02:00
|
|
|
|
2019-02-21 17:06:06 +01:00
|
|
|
function mockRoom(roomId, members, serverACL) {
|
|
|
|
members.forEach(m => m.membership = "join");
|
|
|
|
const powerLevelsUsers = members.reduce((pl, member) => {
|
|
|
|
if (Number.isFinite(member.powerLevel)) {
|
|
|
|
pl[member.userId] = member.powerLevel;
|
|
|
|
}
|
|
|
|
return pl;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return {
|
|
|
|
roomId,
|
|
|
|
getJoinedMembers: () => members,
|
|
|
|
getMember: (userId) => members.find(m => m.userId === userId),
|
|
|
|
currentState: {
|
|
|
|
getStateEvents: (type, key) => {
|
|
|
|
if (key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let content;
|
|
|
|
switch (type) {
|
|
|
|
case "m.room.server_acl":
|
|
|
|
content = serverACL;
|
|
|
|
break;
|
|
|
|
case "m.room.power_levels":
|
|
|
|
content = {users: powerLevelsUsers, users_default: 0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (content) {
|
|
|
|
return {
|
|
|
|
getContent: () => content,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2018-10-25 02:01:08 +02:00
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
describe('matrix-to', function() {
|
2018-10-25 23:22:28 +02:00
|
|
|
let sandbox;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
testUtils.beforeEach(this);
|
|
|
|
sandbox = testUtils.stubClient();
|
|
|
|
peg.get().credentials = { userId: "@test:example.com" };
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
it('should pick no candidate servers when the room has no members', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, []);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-10-25 02:01:08 +02:00
|
|
|
});
|
2018-10-25 23:22:28 +02:00
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
it('should pick a candidate server for the highest power level user in the room', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:pl_50",
|
|
|
|
powerLevel: 50,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:pl_75",
|
|
|
|
powerLevel: 75,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:pl_95",
|
|
|
|
powerLevel: 95,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(3);
|
|
|
|
expect(creator._serverCandidates[0]).toBe("pl_95");
|
2018-10-25 02:01:08 +02:00
|
|
|
// we don't check the 2nd and 3rd servers because that is done by the next test
|
|
|
|
});
|
2018-10-25 23:22:28 +02:00
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
it('should pick candidate servers based on user population', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:first",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:first",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@charlie:first",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@charlie:third",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(3);
|
|
|
|
expect(creator._serverCandidates[0]).toBe("first");
|
|
|
|
expect(creator._serverCandidates[1]).toBe("second");
|
|
|
|
expect(creator._serverCandidates[2]).toBe("third");
|
2018-10-25 02:01:08 +02:00
|
|
|
});
|
2018-10-25 23:22:28 +02:00
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
it('should pick prefer candidate servers with higher power levels', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:first",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@charlie:third",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates.length).toBe(3);
|
|
|
|
expect(creator._serverCandidates[0]).toBe("first");
|
|
|
|
expect(creator._serverCandidates[1]).toBe("second");
|
|
|
|
expect(creator._serverCandidates[2]).toBe("third");
|
2018-10-25 02:01:08 +02:00
|
|
|
});
|
2018-10-25 23:22:52 +02:00
|
|
|
|
2018-12-06 02:00:09 +01:00
|
|
|
it('should pick a maximum of 3 candidate servers', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:alpha",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:bravo",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:charlie",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:delta",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@alice:echo",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(3);
|
2018-12-06 02:00:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not consider IPv4 hosts', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:127.0.0.1",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-10-25 23:22:52 +02:00
|
|
|
});
|
|
|
|
|
2018-12-06 02:00:09 +01:00
|
|
|
it('should not consider IPv6 hosts', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:[::1]",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-10-25 23:22:52 +02:00
|
|
|
});
|
|
|
|
|
2018-12-06 02:00:09 +01:00
|
|
|
it('should not consider IPv4 hostnames with ports', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:127.0.0.1:8448",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-10-25 23:22:52 +02:00
|
|
|
});
|
|
|
|
|
2018-12-06 02:00:09 +01:00
|
|
|
it('should not consider IPv6 hostnames with ports', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:[::1]:8448",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-10-25 23:22:52 +02:00
|
|
|
});
|
|
|
|
|
2018-10-26 18:22:18 +02:00
|
|
|
it('should work with hostnames with ports', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:example.org:8448",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(1);
|
|
|
|
expect(creator._serverCandidates[0]).toBe("example.org:8448");
|
2018-10-25 23:22:52 +02:00
|
|
|
});
|
2018-10-27 03:47:53 +02:00
|
|
|
|
2018-12-06 02:00:09 +01:00
|
|
|
it('should not consider servers explicitly denied by ACLs', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:evilcorp.com",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:chat.evilcorp.com",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
], {
|
|
|
|
deny: ["evilcorp.com", "*.evilcorp.com"],
|
|
|
|
allow: ["*"],
|
|
|
|
});
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-12-06 02:00:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not consider servers not allowed by ACLs', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:evilcorp.com",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:chat.evilcorp.com",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
], {
|
|
|
|
deny: [],
|
|
|
|
allow: [], // implies "ban everyone"
|
|
|
|
});
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(0);
|
2018-12-06 02:00:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should consider servers not explicitly banned by ACLs', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:evilcorp.com",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:chat.evilcorp.com",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
], {
|
|
|
|
deny: ["*.evilcorp.com"], // evilcorp.com is still good though
|
|
|
|
allow: ["*"],
|
|
|
|
});
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(1);
|
|
|
|
expect(creator._serverCandidates[0]).toEqual("evilcorp.com");
|
2018-12-06 02:00:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should consider servers not disallowed by ACLs', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom(null, [
|
|
|
|
{
|
|
|
|
userId: "@alice:evilcorp.com",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:chat.evilcorp.com",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
], {
|
|
|
|
deny: [],
|
|
|
|
allow: ["evilcorp.com"], // implies "ban everyone else"
|
|
|
|
});
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator._serverCandidates).toBeTruthy();
|
|
|
|
expect(creator._serverCandidates.length).toBe(1);
|
|
|
|
expect(creator._serverCandidates[0]).toEqual("evilcorp.com");
|
2018-12-06 02:00:09 +01:00
|
|
|
});
|
|
|
|
|
2018-10-27 03:47:53 +02:00
|
|
|
it('should generate an event permalink for room IDs with no candidate servers', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom("!somewhere:example.org", []);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
const result = creator.forEvent("$something:example.com");
|
2018-10-27 03:47:53 +02:00
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate an event permalink for room IDs with some candidate servers', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
const room = mockRoom("!somewhere:example.org", [
|
|
|
|
{
|
|
|
|
userId: "@alice:first",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermaLinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
const result = creator.forEvent("$something:example.com");
|
2018-10-27 03:47:53 +02:00
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com?via=first&via=second");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a room permalink for room IDs with some candidate servers', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
peg.get().getRoom = (roomId) => {
|
|
|
|
return mockRoom(roomId, [
|
|
|
|
{
|
|
|
|
userId: "@alice:first",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
2018-10-27 03:47:53 +02:00
|
|
|
};
|
|
|
|
const result = makeRoomPermalink("!somewhere:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org?via=first&via=second");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a room permalink for room aliases with no candidate servers', function() {
|
|
|
|
peg.get().getRoom = () => null;
|
|
|
|
const result = makeRoomPermalink("#somewhere:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
|
|
|
|
});
|
|
|
|
|
2018-10-27 04:49:01 +02:00
|
|
|
it('should generate a room permalink for room aliases without candidate servers', function() {
|
2019-02-21 17:06:06 +01:00
|
|
|
peg.get().getRoom = (roomId) => {
|
|
|
|
return mockRoom(roomId, [
|
|
|
|
{
|
|
|
|
userId: "@alice:first",
|
|
|
|
powerLevel: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: "@bob:second",
|
|
|
|
powerLevel: 0,
|
|
|
|
},
|
|
|
|
]);
|
2018-10-27 03:47:53 +02:00
|
|
|
};
|
|
|
|
const result = makeRoomPermalink("#somewhere:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a user permalink', function() {
|
|
|
|
const result = makeUserPermalink("@someone:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/@someone:example.org");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a group permalink', function() {
|
|
|
|
const result = makeGroupPermalink("+community:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/+community:example.org");
|
|
|
|
});
|
2018-10-25 02:01:08 +02:00
|
|
|
});
|