2022-02-11 22:32:51 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import assert from 'assert';
|
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
|
|
|
import os from 'os';
|
|
|
|
import createDebug from 'debug';
|
2023-03-13 23:41:47 +00:00
|
|
|
import pTimeout from 'p-timeout';
|
2023-07-14 16:53:20 +00:00
|
|
|
import normalizePath from 'normalize-path';
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
import type { Device, PrimaryDevice } from '@signalapp/mock-server';
|
2022-07-08 20:46:25 +00:00
|
|
|
import { Server, UUIDKind, loadCertificates } from '@signalapp/mock-server';
|
2022-04-13 00:50:17 +00:00
|
|
|
import { MAX_READ_KEYS as MAX_STORAGE_READ_KEYS } from '../services/storageConstants';
|
2022-02-11 22:32:51 +00:00
|
|
|
import * as durations from '../util/durations';
|
2023-03-13 23:41:47 +00:00
|
|
|
import { drop } from '../util/drop';
|
2022-04-13 00:50:17 +00:00
|
|
|
import { App } from './playwright';
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-04-20 19:35:53 +00:00
|
|
|
export { App };
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
const debug = createDebug('mock:bootstrap');
|
|
|
|
|
|
|
|
const ELECTRON = path.join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'.bin',
|
|
|
|
'electron'
|
|
|
|
);
|
|
|
|
const CI_SCRIPT = path.join(__dirname, '..', '..', 'ci.js');
|
|
|
|
|
|
|
|
const CLOSE_TIMEOUT = 10 * 1000;
|
|
|
|
|
|
|
|
const CONTACT_FIRST_NAMES = [
|
|
|
|
'Alice',
|
|
|
|
'Bob',
|
|
|
|
'Charlie',
|
|
|
|
'Paul',
|
|
|
|
'Steve',
|
|
|
|
'William',
|
|
|
|
];
|
|
|
|
const CONTACT_LAST_NAMES = [
|
|
|
|
'Smith',
|
|
|
|
'Brown',
|
|
|
|
'Jones',
|
|
|
|
'Miller',
|
|
|
|
'Davis',
|
|
|
|
'Lopez',
|
|
|
|
'Gonazales',
|
|
|
|
];
|
|
|
|
|
|
|
|
const CONTACT_NAMES = new Array<string>();
|
|
|
|
for (const firstName of CONTACT_FIRST_NAMES) {
|
|
|
|
for (const lastName of CONTACT_LAST_NAMES) {
|
|
|
|
CONTACT_NAMES.push(`${firstName} ${lastName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_CONTACTS = CONTACT_NAMES.length;
|
|
|
|
|
|
|
|
export type BootstrapOptions = Readonly<{
|
|
|
|
extraConfig?: Record<string, unknown>;
|
|
|
|
benchmark?: boolean;
|
|
|
|
|
|
|
|
linkedDevices?: number;
|
|
|
|
contactCount?: number;
|
2022-09-19 22:08:55 +00:00
|
|
|
contactsWithoutProfileKey?: number;
|
2022-03-09 18:22:34 +00:00
|
|
|
contactNames?: ReadonlyArray<string>;
|
|
|
|
contactPreKeyCount?: number;
|
2022-02-11 22:32:51 +00:00
|
|
|
}>;
|
|
|
|
|
|
|
|
type BootstrapInternalOptions = Pick<BootstrapOptions, 'extraConfig'> &
|
|
|
|
Readonly<{
|
|
|
|
benchmark: boolean;
|
|
|
|
linkedDevices: number;
|
|
|
|
contactCount: number;
|
2022-09-19 22:08:55 +00:00
|
|
|
contactsWithoutProfileKey: number;
|
2022-03-09 18:22:34 +00:00
|
|
|
contactNames: ReadonlyArray<string>;
|
2022-02-11 22:32:51 +00:00
|
|
|
}>;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Bootstrap is a class that prepares mock server and desktop for running
|
|
|
|
// tests/benchmarks.
|
|
|
|
//
|
|
|
|
// In general, the usage pattern is:
|
|
|
|
//
|
|
|
|
// const bootstrap = new Bootstrap();
|
|
|
|
// await bootstrap.init();
|
|
|
|
// const app = await bootstrap.link();
|
|
|
|
// await bootstrap.teardown();
|
|
|
|
//
|
|
|
|
// Once initialized `bootstrap` variable will have following useful properties:
|
|
|
|
//
|
|
|
|
// - `server` - a mock server instance
|
|
|
|
// - `desktop` - a linked device representing currently running desktop instance
|
|
|
|
// - `phone` - a primary device representing desktop's primary
|
|
|
|
// - `contacts` - a list of primary devices for contacts that are synced over
|
|
|
|
// through contact sync
|
|
|
|
//
|
|
|
|
// `bootstrap.getTimestamp()` could be used to generate consecutive timestamp
|
|
|
|
// for sending messages.
|
|
|
|
//
|
|
|
|
// All phone numbers and uuids for all contacts and ourselves are random and not
|
|
|
|
// the same between different test runs.
|
|
|
|
//
|
|
|
|
export class Bootstrap {
|
2022-04-13 00:50:17 +00:00
|
|
|
public readonly server: Server;
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
private readonly options: BootstrapInternalOptions;
|
|
|
|
private privContacts?: ReadonlyArray<PrimaryDevice>;
|
2022-09-19 22:08:55 +00:00
|
|
|
private privContactsWithoutProfileKey?: ReadonlyArray<PrimaryDevice>;
|
2022-02-11 22:32:51 +00:00
|
|
|
private privPhone?: PrimaryDevice;
|
|
|
|
private privDesktop?: Device;
|
|
|
|
private storagePath?: string;
|
2023-02-02 02:11:33 +00:00
|
|
|
private timestamp: number = Date.now() - durations.WEEK;
|
2023-03-13 23:41:47 +00:00
|
|
|
private lastApp?: App;
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
constructor(options: BootstrapOptions = {}) {
|
2022-04-13 00:50:17 +00:00
|
|
|
this.server = new Server({
|
|
|
|
// Limit number of storage read keys for easier testing
|
|
|
|
maxStorageReadKeys: MAX_STORAGE_READ_KEYS,
|
|
|
|
});
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
this.options = {
|
|
|
|
linkedDevices: 5,
|
|
|
|
contactCount: MAX_CONTACTS,
|
2022-09-19 22:08:55 +00:00
|
|
|
contactsWithoutProfileKey: 0,
|
2022-03-09 18:22:34 +00:00
|
|
|
contactNames: CONTACT_NAMES,
|
2022-02-11 22:32:51 +00:00
|
|
|
benchmark: false,
|
|
|
|
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
assert(
|
|
|
|
this.options.contactCount + this.options.contactsWithoutProfileKey <=
|
|
|
|
this.options.contactNames.length
|
|
|
|
);
|
2022-02-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async init(): Promise<void> {
|
|
|
|
debug('initializing');
|
|
|
|
|
|
|
|
await this.server.listen(0);
|
|
|
|
|
|
|
|
const { port } = this.server.address();
|
|
|
|
debug('started server on port=%d', port);
|
|
|
|
|
2022-03-09 18:22:34 +00:00
|
|
|
const contactNames = this.options.contactNames.slice(
|
|
|
|
0,
|
2022-09-19 22:08:55 +00:00
|
|
|
this.options.contactCount + this.options.contactsWithoutProfileKey
|
2022-03-09 18:22:34 +00:00
|
|
|
);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
const allContacts = await Promise.all(
|
2022-02-11 22:32:51 +00:00
|
|
|
contactNames.map(async profileName => {
|
2022-03-09 18:22:34 +00:00
|
|
|
const primary = await this.server.createPrimaryDevice({
|
|
|
|
profileName,
|
|
|
|
});
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < this.options.linkedDevices; i += 1) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await this.server.createSecondaryDevice(primary);
|
|
|
|
}
|
|
|
|
|
|
|
|
return primary;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
this.privContacts = allContacts.slice(0, this.options.contactCount);
|
|
|
|
this.privContactsWithoutProfileKey = allContacts.slice(
|
|
|
|
this.contacts.length
|
|
|
|
);
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
this.privPhone = await this.server.createPrimaryDevice({
|
2022-07-08 20:46:25 +00:00
|
|
|
profileName: 'Myself',
|
2022-02-11 22:32:51 +00:00
|
|
|
contacts: this.contacts,
|
2022-09-19 22:08:55 +00:00
|
|
|
contactsWithoutProfileKey: this.contactsWithoutProfileKey,
|
2022-02-11 22:32:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.storagePath = await fs.mkdtemp(path.join(os.tmpdir(), 'mock-signal-'));
|
|
|
|
|
|
|
|
debug('setting storage path=%j', this.storagePath);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:41:47 +00:00
|
|
|
public static benchmark(
|
|
|
|
fn: (bootstrap: Bootstrap) => Promise<void>,
|
|
|
|
timeout = 5 * durations.MINUTE
|
|
|
|
): void {
|
|
|
|
drop(Bootstrap.runBenchmark(fn, timeout));
|
|
|
|
}
|
|
|
|
|
2022-02-25 01:21:56 +00:00
|
|
|
public get logsDir(): string {
|
|
|
|
assert(
|
|
|
|
this.storagePath !== undefined,
|
|
|
|
'Bootstrap has to be initialized first, see: bootstrap.init()'
|
|
|
|
);
|
|
|
|
|
|
|
|
return path.join(this.storagePath, 'logs');
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
public async teardown(): Promise<void> {
|
|
|
|
debug('tearing down');
|
|
|
|
|
|
|
|
await Promise.race([
|
2023-03-13 23:41:47 +00:00
|
|
|
Promise.all([
|
|
|
|
this.storagePath
|
|
|
|
? fs.rm(this.storagePath, { recursive: true })
|
|
|
|
: Promise.resolve(),
|
|
|
|
this.server.close(),
|
|
|
|
this.lastApp?.close(),
|
|
|
|
]),
|
2022-02-11 22:32:51 +00:00
|
|
|
new Promise(resolve => setTimeout(resolve, CLOSE_TIMEOUT).unref()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async link(): Promise<App> {
|
|
|
|
debug('linking');
|
|
|
|
|
|
|
|
const app = await this.startApp();
|
|
|
|
|
|
|
|
const provision = await this.server.waitForProvision();
|
|
|
|
|
|
|
|
const provisionURL = await app.waitForProvisionURL();
|
|
|
|
|
|
|
|
this.privDesktop = await provision.complete({
|
|
|
|
provisionURL,
|
|
|
|
primaryDevice: this.phone,
|
|
|
|
});
|
|
|
|
|
|
|
|
debug('new desktop device %j', this.desktop.debugId);
|
|
|
|
|
|
|
|
const desktopKey = await this.desktop.popSingleUseKey();
|
|
|
|
await this.phone.addSingleUseKey(this.desktop, desktopKey);
|
|
|
|
|
2023-05-08 14:11:02 +00:00
|
|
|
for (const contact of this.allContacts) {
|
2022-07-08 20:46:25 +00:00
|
|
|
for (const uuidKind of [UUIDKind.ACI, UUIDKind.PNI]) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const contactKey = await this.desktop.popSingleUseKey(uuidKind);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await contact.addSingleUseKey(this.desktop, contactKey, uuidKind);
|
|
|
|
}
|
2022-02-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.phone.waitForSync(this.desktop);
|
|
|
|
this.phone.resetSyncState(this.desktop);
|
|
|
|
|
|
|
|
debug('synced with %j', this.desktop.debugId);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async linkAndClose(): Promise<void> {
|
|
|
|
const app = await this.link();
|
|
|
|
|
|
|
|
debug('closing the app after link');
|
|
|
|
await app.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async startApp(): Promise<App> {
|
|
|
|
assert(
|
|
|
|
this.storagePath !== undefined,
|
|
|
|
'Bootstrap has to be initialized first, see: bootstrap.init()'
|
|
|
|
);
|
|
|
|
|
|
|
|
debug('starting the app');
|
|
|
|
|
|
|
|
const { port } = this.server.address();
|
|
|
|
|
|
|
|
const app = new App({
|
|
|
|
main: ELECTRON,
|
|
|
|
args: [CI_SCRIPT],
|
|
|
|
config: await this.generateConfig(port),
|
|
|
|
});
|
|
|
|
|
|
|
|
await app.start();
|
|
|
|
|
2023-03-13 23:41:47 +00:00
|
|
|
this.lastApp = app;
|
|
|
|
app.on('close', () => {
|
|
|
|
if (this.lastApp === app) {
|
|
|
|
this.lastApp = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTimestamp(): number {
|
|
|
|
const result = this.timestamp;
|
|
|
|
this.timestamp += 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
public async maybeSaveLogs(
|
|
|
|
test?: Mocha.Test,
|
|
|
|
app: App | undefined = this.lastApp
|
|
|
|
): Promise<void> {
|
|
|
|
const { FORCE_ARTIFACT_SAVE } = process.env;
|
|
|
|
if (test?.state !== 'passed' || FORCE_ARTIFACT_SAVE) {
|
|
|
|
await this.saveLogs(app, test?.fullTitle());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async saveLogs(
|
|
|
|
app: App | undefined = this.lastApp,
|
|
|
|
pathPrefix?: string
|
|
|
|
): Promise<void> {
|
2022-07-08 20:46:25 +00:00
|
|
|
const { ARTIFACTS_DIR } = process.env;
|
|
|
|
if (!ARTIFACTS_DIR) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('Not saving logs. Please set ARTIFACTS_DIR env variable');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await fs.mkdir(ARTIFACTS_DIR, { recursive: true });
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
const normalizedPrefix = pathPrefix
|
2023-08-01 16:39:07 +00:00
|
|
|
? `-${normalizePath(pathPrefix.replace(/[^a-z]+/gi, '-'))}-`
|
2023-07-14 16:53:20 +00:00
|
|
|
: '';
|
|
|
|
const outDir = await fs.mkdtemp(
|
|
|
|
path.join(ARTIFACTS_DIR, `logs-${normalizedPrefix}`)
|
|
|
|
);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(`Saving logs to ${outDir}`);
|
|
|
|
|
|
|
|
const { logsDir } = this;
|
2022-09-01 16:35:44 +00:00
|
|
|
await fs.rename(logsDir, outDir);
|
|
|
|
|
|
|
|
if (app) {
|
|
|
|
const window = await app.getWindow();
|
|
|
|
const screenshot = await window.screenshot();
|
|
|
|
await fs.writeFile(path.join(outDir, 'screenshot.png'), screenshot);
|
|
|
|
}
|
2022-07-08 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
//
|
|
|
|
// Getters
|
|
|
|
//
|
|
|
|
|
|
|
|
public get phone(): PrimaryDevice {
|
|
|
|
assert(
|
|
|
|
this.privPhone,
|
|
|
|
'Bootstrap has to be initialized first, see: bootstrap.init()'
|
|
|
|
);
|
|
|
|
return this.privPhone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get desktop(): Device {
|
|
|
|
assert(
|
|
|
|
this.privDesktop,
|
|
|
|
'Bootstrap has to be linked first, see: bootstrap.link()'
|
|
|
|
);
|
|
|
|
return this.privDesktop;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get contacts(): ReadonlyArray<PrimaryDevice> {
|
|
|
|
assert(
|
|
|
|
this.privContacts,
|
|
|
|
'Bootstrap has to be initialized first, see: bootstrap.init()'
|
|
|
|
);
|
|
|
|
return this.privContacts;
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
public get contactsWithoutProfileKey(): ReadonlyArray<PrimaryDevice> {
|
|
|
|
assert(
|
|
|
|
this.privContactsWithoutProfileKey,
|
|
|
|
'Bootstrap has to be initialized first, see: bootstrap.init()'
|
|
|
|
);
|
|
|
|
return this.privContactsWithoutProfileKey;
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:11:02 +00:00
|
|
|
public get allContacts(): ReadonlyArray<PrimaryDevice> {
|
|
|
|
return [...this.contacts, ...this.contactsWithoutProfileKey];
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
2023-03-13 23:41:47 +00:00
|
|
|
private static async runBenchmark(
|
|
|
|
fn: (bootstrap: Bootstrap) => Promise<void>,
|
|
|
|
timeout: number
|
|
|
|
): Promise<void> {
|
|
|
|
const bootstrap = new Bootstrap({
|
|
|
|
benchmark: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await bootstrap.init();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await pTimeout(fn(bootstrap), timeout);
|
|
|
|
} catch (error) {
|
|
|
|
await bootstrap.saveLogs();
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
await bootstrap.teardown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
private async generateConfig(port: number): Promise<string> {
|
2022-02-28 23:46:32 +00:00
|
|
|
const url = `https://127.0.0.1:${port}`;
|
2022-02-11 22:32:51 +00:00
|
|
|
return JSON.stringify({
|
|
|
|
...(await loadCertificates()),
|
|
|
|
|
|
|
|
forcePreloadBundle: this.options.benchmark,
|
2023-07-20 22:37:56 +00:00
|
|
|
ciMode: 'full',
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
buildExpiration: Date.now() + durations.MONTH,
|
|
|
|
storagePath: this.storagePath,
|
|
|
|
storageProfile: 'mock',
|
|
|
|
serverUrl: url,
|
|
|
|
storageUrl: url,
|
|
|
|
cdn: {
|
|
|
|
'0': url,
|
|
|
|
'2': url,
|
|
|
|
},
|
|
|
|
updatesEnabled: false,
|
|
|
|
|
2022-08-19 00:31:12 +00:00
|
|
|
directoreType: 'cdsi',
|
|
|
|
directoryCDSIUrl: url,
|
|
|
|
directoryCDSIMRENCLAVE:
|
2022-07-08 20:46:25 +00:00
|
|
|
'51133fecb3fa18aaf0c8f64cb763656d3272d9faaacdb26ae7df082e414fb142',
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
...this.options.extraConfig,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|