2017-02-02 19:36:41 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2019-01-23 13:34:25 -05:00
|
|
|
Copyright 2018-2019 New Vector Ltd
|
2017-02-02 19:36:41 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-20 10:44:03 +01:00
|
|
|
// load olm before the sdk if possible
|
|
|
|
|
import './olm-loader';
|
|
|
|
|
|
2017-07-04 13:00:14 +01:00
|
|
|
import MockHttpBackend from 'matrix-mock-request';
|
2021-05-11 11:25:43 +01:00
|
|
|
import { LocalStorageCryptoStore } from '../src/crypto/store/localStorage-crypto-store';
|
|
|
|
|
import { logger } from '../src/logger';
|
|
|
|
|
import { WebStorageSessionStore } from "../src/store/session/webstorage";
|
|
|
|
|
import { syncPromise } from "./test-utils";
|
|
|
|
|
import { createClient } from "../src/matrix";
|
|
|
|
|
import { MockStorageApi } from "./MockStorageApi";
|
2017-02-02 19:36:41 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for a MockStorageApi, MockHttpBackend and MatrixClient
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {string} userId
|
|
|
|
|
* @param {string} deviceId
|
|
|
|
|
* @param {string} accessToken
|
2017-09-08 15:35:47 +01:00
|
|
|
*
|
|
|
|
|
* @param {WebStorage=} sessionStoreBackend a web storage object to use for the
|
|
|
|
|
* session store. If undefined, we will create a MockStorageApi.
|
2019-01-23 13:34:25 -05:00
|
|
|
* @param {object} options additional options to pass to the client
|
2017-02-02 19:36:41 +00:00
|
|
|
*/
|
2019-12-17 14:53:56 -07:00
|
|
|
export function TestClient(
|
2019-01-23 13:34:25 -05:00
|
|
|
userId, deviceId, accessToken, sessionStoreBackend, options,
|
2017-09-08 15:35:47 +01:00
|
|
|
) {
|
2017-02-02 19:36:41 +00:00
|
|
|
this.userId = userId;
|
|
|
|
|
this.deviceId = deviceId;
|
|
|
|
|
|
2017-09-08 15:35:47 +01:00
|
|
|
if (sessionStoreBackend === undefined) {
|
2019-12-17 14:53:56 -07:00
|
|
|
sessionStoreBackend = new MockStorageApi();
|
2017-09-08 15:35:47 +01:00
|
|
|
}
|
2019-12-17 14:53:56 -07:00
|
|
|
const sessionStore = new WebStorageSessionStore(sessionStoreBackend);
|
2018-01-15 16:27:28 +00:00
|
|
|
|
2017-02-02 19:36:41 +00:00
|
|
|
this.httpBackend = new MockHttpBackend();
|
2019-01-23 13:34:25 -05:00
|
|
|
|
|
|
|
|
options = Object.assign({
|
2017-02-02 19:36:41 +00:00
|
|
|
baseUrl: "http://" + userId + ".test.server",
|
|
|
|
|
userId: userId,
|
|
|
|
|
accessToken: accessToken,
|
|
|
|
|
deviceId: deviceId,
|
2018-01-16 17:57:49 +00:00
|
|
|
sessionStore: sessionStore,
|
2017-02-02 19:36:41 +00:00
|
|
|
request: this.httpBackend.requestFn,
|
2019-01-23 13:34:25 -05:00
|
|
|
}, options);
|
|
|
|
|
if (!options.cryptoStore) {
|
|
|
|
|
// expose this so the tests can get to it
|
|
|
|
|
this.cryptoStore = new LocalStorageCryptoStore(sessionStoreBackend);
|
|
|
|
|
options.cryptoStore = this.cryptoStore;
|
|
|
|
|
}
|
2019-12-17 14:53:56 -07:00
|
|
|
this.client = createClient(options);
|
2017-02-02 19:36:41 +00:00
|
|
|
|
|
|
|
|
this.deviceKeys = null;
|
|
|
|
|
this.oneTimeKeys = {};
|
2020-10-16 12:53:08 +01:00
|
|
|
this._callEventHandler = {
|
|
|
|
|
calls: new Map(),
|
|
|
|
|
};
|
2017-02-02 19:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-20 16:26:24 +00:00
|
|
|
TestClient.prototype.toString = function() {
|
|
|
|
|
return 'TestClient[' + this.userId + ']';
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-02 19:36:41 +00:00
|
|
|
/**
|
|
|
|
|
* start the client, and wait for it to initialise.
|
|
|
|
|
*
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2017-02-08 22:52:27 +00:00
|
|
|
TestClient.prototype.start = function() {
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log(this + ': starting');
|
2017-02-02 19:36:41 +00:00
|
|
|
this.httpBackend.when("GET", "/pushrules").respond(200, {});
|
|
|
|
|
this.httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
2017-02-20 16:26:24 +00:00
|
|
|
this.expectDeviceKeyUpload();
|
|
|
|
|
|
|
|
|
|
// we let the client do a very basic initial sync, which it needs before
|
|
|
|
|
// it will upload one-time keys.
|
|
|
|
|
this.httpBackend.when("GET", "/sync").respond(200, { next_batch: 1 });
|
2017-02-08 18:17:43 +00:00
|
|
|
|
2017-02-02 19:36:41 +00:00
|
|
|
this.client.startClient({
|
|
|
|
|
// set this so that we can get hold of failed events
|
|
|
|
|
pendingEventOrdering: 'detached',
|
|
|
|
|
});
|
|
|
|
|
|
2017-08-08 10:58:19 +01:00
|
|
|
return Promise.all([
|
|
|
|
|
this.httpBackend.flushAllExpected(),
|
2019-12-17 14:53:56 -07:00
|
|
|
syncPromise(this.client),
|
2017-08-08 10:58:19 +01:00
|
|
|
]).then(() => {
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log(this + ': started');
|
2017-02-08 18:17:43 +00:00
|
|
|
});
|
2017-02-02 19:36:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* stop the client
|
2018-10-25 14:43:17 +01:00
|
|
|
* @return {Promise} Resolves once the mock http backend has finished all pending flushes
|
2017-02-02 19:36:41 +00:00
|
|
|
*/
|
|
|
|
|
TestClient.prototype.stop = function() {
|
|
|
|
|
this.client.stopClient();
|
2018-10-25 14:29:25 +01:00
|
|
|
return this.httpBackend.stop();
|
2017-02-02 19:36:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2017-02-20 16:26:24 +00:00
|
|
|
* Set up expectations that the client will upload device keys.
|
2017-02-02 19:36:41 +00:00
|
|
|
*/
|
2017-02-20 16:26:24 +00:00
|
|
|
TestClient.prototype.expectDeviceKeyUpload = function() {
|
2017-02-02 19:36:41 +00:00
|
|
|
const self = this;
|
|
|
|
|
this.httpBackend.when("POST", "/keys/upload").respond(200, function(path, content) {
|
2017-02-08 07:29:01 +00:00
|
|
|
expect(content.one_time_keys).toBe(undefined);
|
|
|
|
|
expect(content.device_keys).toBeTruthy();
|
2017-02-20 16:26:24 +00:00
|
|
|
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log(self + ': received device keys');
|
2017-02-20 16:26:24 +00:00
|
|
|
// we expect this to happen before any one-time keys are uploaded.
|
|
|
|
|
expect(Object.keys(self.oneTimeKeys).length).toEqual(0);
|
|
|
|
|
|
2017-02-02 19:36:41 +00:00
|
|
|
self.deviceKeys = content.device_keys;
|
2021-05-11 11:25:43 +01:00
|
|
|
return { one_time_key_counts: { signed_curve25519: 0 } };
|
2017-02-02 19:36:41 +00:00
|
|
|
});
|
2017-02-20 16:26:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If one-time keys have already been uploaded, return them. Otherwise,
|
|
|
|
|
* set up an expectation that the keys will be uploaded, and wait for
|
|
|
|
|
* that to happen.
|
|
|
|
|
*
|
|
|
|
|
* @returns {Promise} for the one-time keys
|
|
|
|
|
*/
|
|
|
|
|
TestClient.prototype.awaitOneTimeKeyUpload = function() {
|
|
|
|
|
if (Object.keys(this.oneTimeKeys).length != 0) {
|
|
|
|
|
// already got one-time keys
|
2017-07-10 16:04:25 +01:00
|
|
|
return Promise.resolve(this.oneTimeKeys);
|
2017-02-20 16:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.httpBackend.when("POST", "/keys/upload")
|
|
|
|
|
.respond(200, (path, content) => {
|
|
|
|
|
expect(content.device_keys).toBe(undefined);
|
|
|
|
|
expect(content.one_time_keys).toBe(undefined);
|
2021-05-11 11:25:43 +01:00
|
|
|
return { one_time_key_counts: {
|
2017-02-20 16:26:24 +00:00
|
|
|
signed_curve25519: Object.keys(this.oneTimeKeys).length,
|
2021-05-11 11:25:43 +01:00
|
|
|
} };
|
2017-02-20 16:26:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.httpBackend.when("POST", "/keys/upload")
|
|
|
|
|
.respond(200, (path, content) => {
|
|
|
|
|
expect(content.device_keys).toBe(undefined);
|
|
|
|
|
expect(content.one_time_keys).toBeTruthy();
|
2019-11-20 19:52:50 +00:00
|
|
|
expect(content.one_time_keys).not.toEqual({});
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log('%s: received %i one-time keys', this,
|
2017-02-20 16:26:24 +00:00
|
|
|
Object.keys(content.one_time_keys).length);
|
|
|
|
|
this.oneTimeKeys = content.one_time_keys;
|
2021-05-11 11:25:43 +01:00
|
|
|
return { one_time_key_counts: {
|
2017-02-20 16:26:24 +00:00
|
|
|
signed_curve25519: Object.keys(this.oneTimeKeys).length,
|
2021-05-11 11:25:43 +01:00
|
|
|
} };
|
2017-02-20 16:26:24 +00:00
|
|
|
});
|
|
|
|
|
|
2017-07-18 18:46:33 +01:00
|
|
|
// this can take ages
|
|
|
|
|
return this.httpBackend.flush('/keys/upload', 2, 1000).then((flushed) => {
|
2017-02-20 16:26:24 +00:00
|
|
|
expect(flushed).toEqual(2);
|
|
|
|
|
return this.oneTimeKeys;
|
2017-02-02 19:36:41 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-08 22:52:27 +00:00
|
|
|
/**
|
|
|
|
|
* Set up expectations that the client will query device keys.
|
|
|
|
|
*
|
|
|
|
|
* We check that the query contains each of the users in `response`.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} response response to the query.
|
|
|
|
|
*/
|
|
|
|
|
TestClient.prototype.expectKeyQuery = function(response) {
|
|
|
|
|
this.httpBackend.when('POST', '/keys/query').respond(
|
|
|
|
|
200, (path, content) => {
|
|
|
|
|
Object.keys(response.device_keys).forEach((userId) => {
|
2019-02-06 16:48:57 +00:00
|
|
|
expect(content.device_keys[userId]).toEqual(
|
2020-04-07 16:19:46 +02:00
|
|
|
[],
|
2019-02-06 16:48:57 +00:00
|
|
|
"Expected key query for " + userId + ", got " +
|
|
|
|
|
Object.keys(content.device_keys),
|
|
|
|
|
);
|
2017-02-08 22:52:27 +00:00
|
|
|
});
|
|
|
|
|
return response;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-02 19:36:41 +00:00
|
|
|
/**
|
|
|
|
|
* get the uploaded curve25519 device key
|
|
|
|
|
*
|
|
|
|
|
* @return {string} base64 device key
|
|
|
|
|
*/
|
|
|
|
|
TestClient.prototype.getDeviceKey = function() {
|
|
|
|
|
const keyId = 'curve25519:' + this.deviceId;
|
|
|
|
|
return this.deviceKeys.keys[keyId];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get the uploaded ed25519 device key
|
|
|
|
|
*
|
|
|
|
|
* @return {string} base64 device key
|
|
|
|
|
*/
|
|
|
|
|
TestClient.prototype.getSigningKey = function() {
|
|
|
|
|
const keyId = 'ed25519:' + this.deviceId;
|
|
|
|
|
return this.deviceKeys.keys[keyId];
|
|
|
|
|
};
|
2017-07-04 12:47:27 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* flush a single /sync request, and wait for the syncing event
|
|
|
|
|
*
|
|
|
|
|
* @returns {Promise} promise which completes once the sync has been flushed
|
|
|
|
|
*/
|
|
|
|
|
TestClient.prototype.flushSync = function() {
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log(`${this}: flushSync`);
|
2017-07-10 16:55:29 +01:00
|
|
|
return Promise.all([
|
2017-07-04 12:47:27 +01:00
|
|
|
this.httpBackend.flush('/sync', 1),
|
2019-12-17 14:53:56 -07:00
|
|
|
syncPromise(this.client),
|
2017-08-08 10:58:19 +01:00
|
|
|
]).then(() => {
|
2019-05-19 09:29:40 +05:30
|
|
|
logger.log(`${this}: flushSync completed`);
|
2017-08-08 10:58:19 +01:00
|
|
|
});
|
2017-07-04 12:47:27 +01:00
|
|
|
};
|
2020-10-16 12:53:08 +01:00
|
|
|
|
|
|
|
|
TestClient.prototype.isFallbackICEServerAllowed = function() {
|
|
|
|
|
return true;
|
|
|
|
|
};
|