diff --git a/spec/unit/crypto/secrets.spec.ts b/spec/unit/crypto/secrets.spec.ts index 7939cdae2d1..8a8326867f0 100644 --- a/spec/unit/crypto/secrets.spec.ts +++ b/spec/unit/crypto/secrets.spec.ts @@ -250,8 +250,8 @@ describe("Secrets", function() { osborne2.client.crypto.deviceList.storeDevicesForUser("@alice:example.com", { "VAX": { - user_id: "@alice:example.com", - device_id: "VAX", + verified: 0, + known: false, algorithms: [olmlib.OLM_ALGORITHM, olmlib.MEGOLM_ALGORITHM], keys: { "ed25519:VAX": vaxDevice.deviceEd25519Key, @@ -261,9 +261,9 @@ describe("Secrets", function() { }); vax.client.crypto.deviceList.storeDevicesForUser("@alice:example.com", { "Osborne2": { - user_id: "@alice:example.com", - device_id: "Osborne2", algorithms: [olmlib.OLM_ALGORITHM, olmlib.MEGOLM_ALGORITHM], + verified: 0, + known: false, keys: { "ed25519:Osborne2": osborne2Device.deviceEd25519Key, "curve25519:Osborne2": osborne2Device.deviceCurve25519Key, diff --git a/spec/unit/crypto/verification/InRoomChannel.spec.js b/spec/unit/crypto/verification/InRoomChannel.spec.ts similarity index 97% rename from spec/unit/crypto/verification/InRoomChannel.spec.js rename to spec/unit/crypto/verification/InRoomChannel.spec.ts index 90fd05b475d..c6f9db33997 100644 --- a/spec/unit/crypto/verification/InRoomChannel.spec.js +++ b/spec/unit/crypto/verification/InRoomChannel.spec.ts @@ -13,9 +13,9 @@ 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 { MatrixClient } from "../../../../src/client"; import { InRoomChannel } from "../../../../src/crypto/verification/request/InRoomChannel"; import { MatrixEvent } from "../../../../src/models/event"; - "../../../../src/crypto/verification/request/ToDeviceChannel"; describe("InRoomChannel tests", function() { const ALICE = "@alice:hs.tld"; @@ -23,7 +23,7 @@ describe("InRoomChannel tests", function() { const MALORY = "@malory:hs.tld"; const client = { getUserId() { return ALICE; }, - }; + } as unknown as MatrixClient; it("getEventType only returns .request for a message with a msgtype", function() { const invalidEvent = new MatrixEvent({ diff --git a/spec/unit/crypto/verification/qr_code.spec.js b/spec/unit/crypto/verification/qr_code.spec.ts similarity index 100% rename from spec/unit/crypto/verification/qr_code.spec.js rename to spec/unit/crypto/verification/qr_code.spec.ts diff --git a/spec/unit/crypto/verification/request.spec.js b/spec/unit/crypto/verification/request.spec.ts similarity index 82% rename from spec/unit/crypto/verification/request.spec.js rename to spec/unit/crypto/verification/request.spec.ts index e530344e2eb..10b29bf9fee 100644 --- a/spec/unit/crypto/verification/request.spec.js +++ b/spec/unit/crypto/verification/request.spec.ts @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import "../../../olm-loader"; -import { verificationMethods } from "../../../../src/crypto"; +import { CryptoEvent, verificationMethods } from "../../../../src/crypto"; import { logger } from "../../../../src/logger"; import { SAS } from "../../../../src/crypto/verification/SAS"; import { makeTestClients, setupWebcrypto, teardownWebcrypto } from './util'; @@ -52,23 +52,22 @@ describe("verification request integration tests with crypto layer", function() alice.client.crypto.deviceList.getRawStoredDevicesForUser = function() { return { Dynabook: { + algorithms: [], + verified: 0, + known: false, keys: { "ed25519:Dynabook": "bob+base64+ed25519+key", }, }, }; }; - alice.client.downloadKeys = () => { - return Promise.resolve(); - }; - bob.client.downloadKeys = () => { - return Promise.resolve(); - }; - bob.client.on("crypto.verification.request", (request) => { + alice.client.downloadKeys = jest.fn().mockResolvedValue({}); + bob.client.downloadKeys = jest.fn().mockResolvedValue({}); + bob.client.on(CryptoEvent.VerificationRequest, (request) => { const bobVerifier = request.beginKeyVerification(verificationMethods.SAS); bobVerifier.verify(); - // XXX: Private function access (but it's a test, so we're okay) + // @ts-ignore Private function access (but it's a test, so we're okay) bobVerifier.endTimer(); }); const aliceRequest = await alice.client.requestVerification("@bob:example.com"); @@ -76,7 +75,7 @@ describe("verification request integration tests with crypto layer", function() const aliceVerifier = aliceRequest.verifier; expect(aliceVerifier).toBeInstanceOf(SAS); - // XXX: Private function access (but it's a test, so we're okay) + // @ts-ignore Private function access (but it's a test, so we're okay) aliceVerifier.endTimer(); alice.stop(); diff --git a/spec/unit/crypto/verification/util.js b/spec/unit/crypto/verification/util.ts similarity index 66% rename from spec/unit/crypto/verification/util.js rename to spec/unit/crypto/verification/util.ts index 572a4b270d2..d7c519e74ae 100644 --- a/spec/unit/crypto/verification/util.js +++ b/spec/unit/crypto/verification/util.ts @@ -19,20 +19,23 @@ import nodeCrypto from "crypto"; import { TestClient } from '../../../TestClient'; import { MatrixEvent } from "../../../../src/models/event"; +import { IRoomTimelineData } from "../../../../src/models/event-timeline-set"; +import { Room, RoomEvent } from "../../../../src/models/room"; import { logger } from '../../../../src/logger'; +import { MatrixClient, ClientEvent } from '../../../../src/client'; -export async function makeTestClients(userInfos, options) { - const clients = []; - const timeouts = []; - const clientMap = {}; - const sendToDevice = function(type, map) { +export async function makeTestClients(userInfos, options): Promise<[TestClient[], () => void]> { + const clients: TestClient[] = []; + const timeouts: ReturnType[] = []; + const clientMap: Record> = {}; + const makeSendToDevice = (matrixClient: MatrixClient): MatrixClient['sendToDevice'] => async (type, map) => { // logger.log(this.getUserId(), "sends", type, map); for (const [userId, devMap] of Object.entries(map)) { if (userId in clientMap) { for (const [deviceId, msg] of Object.entries(devMap)) { if (deviceId in clientMap[userId]) { const event = new MatrixEvent({ - sender: this.getUserId(), // eslint-disable-line @babel/no-invalid-this + sender: matrixClient.getUserId()!, type: type, content: msg, }); @@ -42,18 +45,19 @@ export async function makeTestClients(userInfos, options) { Promise.resolve(); decryptionPromise.then( - () => client.emit("toDeviceEvent", event), + () => client.emit(ClientEvent.ToDeviceEvent, event), ); } } } } + return {}; }; - const sendEvent = function(room, type, content) { + const makeSendEvent = (matrixClient: MatrixClient) => (room, type, content) => { // make up a unique ID as the event ID - const eventId = "$" + this.makeTxnId(); // eslint-disable-line @babel/no-invalid-this + const eventId = "$" + matrixClient.makeTxnId(); const rawEvent = { - sender: this.getUserId(), // eslint-disable-line @babel/no-invalid-this + sender: matrixClient.getUserId()!, type: type, content: content, room_id: room, @@ -63,22 +67,24 @@ export async function makeTestClients(userInfos, options) { const event = new MatrixEvent(rawEvent); const remoteEcho = new MatrixEvent(Object.assign({}, rawEvent, { unsigned: { - transaction_id: this.makeTxnId(), // eslint-disable-line @babel/no-invalid-this + transaction_id: matrixClient.makeTxnId(), }, })); const timeout = setTimeout(() => { for (const tc of clients) { - if (tc.client === this) { // eslint-disable-line @babel/no-invalid-this + const room = new Room('test', tc.client, tc.client.getUserId()!); + const roomTimelineData = {} as unknown as IRoomTimelineData; + if (tc.client === matrixClient) { logger.log("sending remote echo!!"); - tc.client.emit("Room.timeline", remoteEcho); + tc.client.emit(RoomEvent.Timeline, remoteEcho, room, false, false, roomTimelineData); } else { - tc.client.emit("Room.timeline", event); + tc.client.emit(RoomEvent.Timeline, event, room, false, false, roomTimelineData); } } }); - timeouts.push(timeout); + timeouts.push(timeout as unknown as ReturnType); return Promise.resolve({ event_id: eventId }); }; @@ -99,8 +105,8 @@ export async function makeTestClients(userInfos, options) { clientMap[userInfo.userId] = {}; } clientMap[userInfo.userId][userInfo.deviceId] = testClient.client; - testClient.client.sendToDevice = sendToDevice; - testClient.client.sendEvent = sendEvent; + testClient.client.sendToDevice = makeSendToDevice(testClient.client); + testClient.client.sendEvent = makeSendEvent(testClient.client); clients.push(testClient); } @@ -116,11 +122,12 @@ export async function makeTestClients(userInfos, options) { export function setupWebcrypto() { global.crypto = { getRandomValues: (buf) => { - return nodeCrypto.randomFillSync(buf); + return nodeCrypto.randomFillSync(buf as any); }, - }; + } as unknown as Crypto; } export function teardownWebcrypto() { + // @ts-ignore undefined != Crypto global.crypto = undefined; }