Skip to content

Commit 3f74785

Browse files
authored
Merge pull request #225 from matrix-org/valere/receive_to_device_tests
test: Add tests to assert what receiveSyncChanges returns
2 parents 82f0d5e + 6ab2111 commit 3f74785

1 file changed

Lines changed: 180 additions & 0 deletions

File tree

tests/machine.test.ts

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,4 +1607,184 @@ describe(OlmMachine.name, () => {
16071607
await m.setRoomSettings(new RoomId("!test:room"), settings2);
16081608
});
16091609
});
1610+
1611+
describe("Receiving to device messages", () => {
1612+
test("Should return plain text to device objects as they are received", async () => {
1613+
const m = await machine();
1614+
1615+
const someClearEvent = {
1616+
sender: "@alice:example.com",
1617+
type: "custom.type",
1618+
content: {
1619+
value: "foo",
1620+
},
1621+
};
1622+
1623+
let toDeviceEvents = [someClearEvent];
1624+
1625+
const received = await m.receiveSyncChanges(
1626+
JSON.stringify(toDeviceEvents),
1627+
new DeviceLists(),
1628+
new Map<string, number>(),
1629+
undefined,
1630+
);
1631+
1632+
const receivedToDevices = JSON.parse(received);
1633+
expect(receivedToDevices).toBeInstanceOf(Array);
1634+
const receivedToDeviceArray: Array<any> = receivedToDevices;
1635+
expect(receivedToDeviceArray.length).toBe(1);
1636+
const toDeviceEvent = receivedToDeviceArray[0];
1637+
1638+
expect(toDeviceEvent.sender).toEqual("@alice:example.com");
1639+
expect(toDeviceEvent.type).toEqual("custom.type");
1640+
expect(toDeviceEvent.content.value).toEqual("foo");
1641+
});
1642+
1643+
test("Should return unable to decrypt to device untouched", async () => {
1644+
const m = await machine();
1645+
1646+
const aliceCurve = m.identityKeys.curve25519.toBase64();
1647+
1648+
const utdEvent = {
1649+
content: {
1650+
"algorithm": "m.olm.v1.curve25519-aes-sha2",
1651+
"ciphertext": {
1652+
aliceCurve: {
1653+
// this payload is just captured from a sync of some other element web with other users
1654+
body: "Awogjvpx458CGhuo77HX/+tp1sxgRoCi7iAlzMvfrpbWoREQAiKACysX/p+ojr5QitCi9WRXNyamW2v2LT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0tZGtH07RXfUyhp1vtTH49OBZHGkb/r+L8OjYJTST1dDCGqeGXO3uwYjoWHXtezLVHYgL+UOwcLJfMF5s9DQiqcfYXzp2kEWGsaetBFXcUWqq4RMHqlr6QfbxyuYLlQzc/AYA/MrT3J6nDpNLcvozH3RcIs8NcKcjdtjvgL0QGThy3RcecJQEDx3STrkkePL3dlyFCtVsmtQ0vjBBCxUgdySfxiobGGnpezZYi7q+Xz61GOZ9QqYmkcZOPzfNWeqtmzB7gqlH1gkFsK2yMAzKq2XCDFHvA7YAT3yMGiY06FcQ+2jyg7Bk2Q+AvjTG8hlPlmt6BZfW5cz1qx1apQn1qHXHrgfWcI52rApYQlNPOU1Uc8kZ8Ee6XUhhXBGY1rvZiKjKFG0PPuS8xo4/P7/u+gH5gItmEVDFL6giYPFsPpqAQkUN7hFoGiVZEjO4PwrLOmydsEcNOfACqrnUs08FQtvPg0sjHnxh6nh6FUQv93ukKl6+c9d+pCsN2xukrQ7Dog3nrjFZ6PrS5J0k9rDAOwTB55sfGXPZ2rATOK1WS4XcpsCtqwnYm4sGNc8ALMQkQ97zCnw8TcQwLvdUMlfbqQ5ykDQpQD68fITEDDHmBAeTCjpC713E6AhvOMwTJvjhd7hSkeOTRTmn9zXIVGNo1jSr8u0xO9uLGeWsV0+UlRLgp7/nsgfermjwNN8wj6MW3DHGS8UzzYfe9TGCeywqqIUTqgfXY48leGgB7twh4cl4jcOQniLATTvigIAQIvq/Uv8L45BGnkpKTdQ5F73gehXdVA",
1655+
type: 1,
1656+
},
1657+
},
1658+
"org.matrix.msgid": "93ee0170aa7740d0ac9ee137e820302d",
1659+
"sender_key": "WJ6Ce7U67a6jqkHYHd8o0+5H4bqdi9hInZdk0+swuXs",
1660+
},
1661+
type: "m.room.encrypted",
1662+
sender: "@bob:example.org",
1663+
};
1664+
1665+
let toDeviceEvents = [utdEvent];
1666+
1667+
const received = await m.receiveSyncChanges(
1668+
JSON.stringify(toDeviceEvents),
1669+
new DeviceLists(),
1670+
new Map<string, number>(),
1671+
undefined,
1672+
);
1673+
1674+
const receivedToDevices = JSON.parse(received);
1675+
expect(receivedToDevices).toBeInstanceOf(Array);
1676+
const receivedToDeviceArray: Array<any> = receivedToDevices;
1677+
expect(receivedToDeviceArray.length).toBe(1);
1678+
const toDeviceEvent = receivedToDeviceArray[0];
1679+
1680+
expect(toDeviceEvent.sender).toEqual("@bob:example.org");
1681+
expect(toDeviceEvent.type).toEqual("m.room.encrypted");
1682+
expect(toDeviceEvent.content.ciphertext).toBeDefined();
1683+
});
1684+
1685+
test("Should return the clear text version of decrypted events", async () => {
1686+
const aliceUserId = new UserId("@alice:example.org");
1687+
const aliceDeviceId = new DeviceId("ALICE_DEV");
1688+
1689+
const bobUserId = new UserId("@bob:example.org");
1690+
const bobDeviceId = new DeviceId("BOB_DEV");
1691+
1692+
const alice = await machine(aliceUserId, aliceDeviceId);
1693+
const bob = await machine(bobUserId, bobDeviceId);
1694+
1695+
const [keysUploadRequest, keysQueryRequest] = await bob.outgoingRequests();
1696+
expect(keysUploadRequest).toBeInstanceOf(KeysUploadRequest);
1697+
expect(keysQueryRequest).toBeInstanceOf(KeysQueryRequest);
1698+
const keysUploadBody = JSON.parse(keysUploadRequest.body);
1699+
const otks = Object.values(keysUploadBody.one_time_keys);
1700+
await bob.markRequestAsSent(
1701+
keysUploadRequest.id!,
1702+
keysUploadRequest.type,
1703+
JSON.stringify({
1704+
one_time_key_counts: {
1705+
signed_curve25519: otks.length,
1706+
},
1707+
}),
1708+
);
1709+
1710+
// Let Alice know about bob keys
1711+
await alice.markRequestAsSent(
1712+
"SomeUniqueId",
1713+
RequestType.KeysQuery,
1714+
JSON.stringify({
1715+
device_keys: {
1716+
"@bob:example.org": {
1717+
BOB_DEV: keysUploadBody.device_keys,
1718+
},
1719+
},
1720+
failures: {},
1721+
}),
1722+
);
1723+
1724+
// Let Alice claim one otk for bob to establish the olm session
1725+
const [otkId, otk] = Object.entries(keysUploadBody.one_time_keys)[0];
1726+
await alice.markRequestAsSent(
1727+
"foo",
1728+
RequestType.KeysClaim,
1729+
JSON.stringify({
1730+
one_time_keys: {
1731+
"@bob:example.org": {
1732+
BOB_DEV: {
1733+
[otkId]: otk,
1734+
},
1735+
},
1736+
},
1737+
}),
1738+
);
1739+
1740+
{
1741+
// Let bob be aware of alice device
1742+
// If not olm decryption will fail because of MissingSigningKeys
1743+
const [keysUploadRequest, keysQueryRequest] = await alice.outgoingRequests();
1744+
const keysUploadBody = JSON.parse(keysUploadRequest.body);
1745+
await bob.markRequestAsSent(
1746+
"SomeUniqueId",
1747+
RequestType.KeysQuery,
1748+
JSON.stringify({
1749+
device_keys: {
1750+
"@alice:example.org": {
1751+
ALICE_DEV: keysUploadBody.device_keys,
1752+
},
1753+
},
1754+
failures: {},
1755+
}),
1756+
);
1757+
}
1758+
1759+
const aliceBobDevice = (await alice.getDevice(bobUserId, bobDeviceId))!;
1760+
const encryptedContent = await aliceBobDevice.encryptToDeviceEvent("custom.type", {
1761+
foo: "bar",
1762+
});
1763+
1764+
const encryptedToDevice = {
1765+
type: "m.room.encrypted",
1766+
content: JSON.parse(encryptedContent),
1767+
sender: "@alice:example.org",
1768+
};
1769+
1770+
// Bob should be able to decrypt this
1771+
1772+
const received = await bob.receiveSyncChanges(
1773+
JSON.stringify([encryptedToDevice]),
1774+
new DeviceLists(),
1775+
new Map<string, number>(),
1776+
undefined,
1777+
);
1778+
1779+
const receivedToDevices = JSON.parse(received);
1780+
expect(receivedToDevices).toBeInstanceOf(Array);
1781+
const receivedToDeviceArray: Array<any> = receivedToDevices;
1782+
expect(receivedToDeviceArray.length).toBe(1);
1783+
const toDeviceEvent = receivedToDeviceArray[0];
1784+
1785+
expect(toDeviceEvent.sender).toEqual("@alice:example.org");
1786+
expect(toDeviceEvent.type).toEqual("custom.type");
1787+
expect(toDeviceEvent.content.foo).toEqual("bar");
1788+
});
1789+
});
16101790
});

0 commit comments

Comments
 (0)