2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as childProcess from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as multiparty from 'multiparty';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { ifdescribe, ifit } from './spec-helpers';
|
|
|
|
import * as temp from 'temp';
|
|
|
|
import * as url from 'url';
|
|
|
|
import { ipcMain, app, BrowserWindow, crashReporter, BrowserWindowConstructorOptions } from 'electron';
|
|
|
|
import { AddressInfo } from 'net';
|
|
|
|
import { closeWindow, closeAllWindows } from './window-helpers';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
|
|
|
|
temp.track();
|
|
|
|
|
|
|
|
const afterTest: ((() => void) | (() => Promise<void>))[] = [];
|
2019-11-01 20:37:02 +00:00
|
|
|
async function cleanup () {
|
2019-10-14 21:38:54 +00:00
|
|
|
for (const cleanup of afterTest) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = cleanup();
|
|
|
|
if (r instanceof Promise) { await r; }
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
afterTest.length = 0;
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(alexeykuzmin): [Ch66] This test fails on Linux. Fix it and enable back.
|
|
|
|
ifdescribe(!process.mas && !process.env.DISABLE_CRASH_REPORTER_TESTS && process.platform !== 'linux')('crashReporter module', function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let originalTempDirectory: string;
|
|
|
|
let tempDirectory = null;
|
|
|
|
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
before(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tempDirectory = temp.mkdirSync('electronCrashReporterSpec-');
|
|
|
|
originalTempDirectory = app.getPath('temp');
|
|
|
|
app.setPath('temp', tempDirectory);
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
after(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
app.setPath('temp', originalTempDirectory);
|
2019-10-14 21:38:54 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
temp.cleanupSync();
|
2019-10-14 21:38:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
// ignore.
|
2020-03-20 20:28:31 +00:00
|
|
|
console.warn(e.stack);
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(cleanup);
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
it('should send minidump when node processes crash', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port, waitForCrash } = await startServer();
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const crashesDir = path.join(app.getPath('temp'), `${app.name} Crashes`);
|
|
|
|
const version = app.getVersion();
|
|
|
|
const crashPath = path.join(fixtures, 'module', 'crash.js');
|
|
|
|
childProcess.fork(crashPath, [port.toString(), version, crashesDir], { silent: true });
|
|
|
|
const crash = await waitForCrash();
|
|
|
|
checkCrash('node', crash);
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
const generateSpecs = (description: string, browserWindowOpts: BrowserWindowConstructorOptions) => {
|
|
|
|
describe(description, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow(Object.assign({ show: false }, browserWindowOpts));
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
afterEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
it('should send minidump when renderer crashes', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port, waitForCrash } = await startServer();
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'crash.html'), { query: { port: port.toString() } });
|
|
|
|
const crash = await waitForCrash();
|
|
|
|
checkCrash('renderer', crash);
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
ifit(!browserWindowOpts.webPreferences!.sandbox)('should send minidump when node processes crash', async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port, waitForCrash } = await startServer();
|
|
|
|
const crashesDir = path.join(app.getPath('temp'), `${app.name} Crashes`);
|
|
|
|
const version = app.getVersion();
|
|
|
|
const crashPath = path.join(fixtures, 'module', 'crash.js');
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'crash_child.html'), { query: { port: port.toString(), crashesDir, crashPath, version } });
|
|
|
|
const crash = await waitForCrash();
|
|
|
|
expect(String((crash as any).newExtra)).to.equal('newExtra');
|
|
|
|
expect((crash as any).removeExtra).to.be.undefined();
|
|
|
|
checkCrash('node', crash);
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('when uploadToServer is false', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
after(() => { crashReporter.setUploadToServer(true); });
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
it('should not send minidump', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port, getCrashes } = await startServer();
|
|
|
|
crashReporter.setUploadToServer(false);
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let crashesDir = crashReporter.getCrashesDirectory();
|
|
|
|
const existingDumpFiles = new Set();
|
2019-10-14 21:38:54 +00:00
|
|
|
// crashpad puts the dump files in the "completed" subdirectory
|
|
|
|
if (process.platform === 'darwin') {
|
2020-03-20 20:28:31 +00:00
|
|
|
crashesDir = path.join(crashesDir, 'completed');
|
2019-10-14 21:38:54 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
crashesDir = path.join(crashesDir, 'reports');
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const crashUrl = url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
pathname: path.join(fixtures, 'api', 'crash.html'),
|
|
|
|
search: `?port=${port}&skipUpload=1`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadURL(crashUrl);
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
ipcMain.once('list-existing-dumps', (event) => {
|
|
|
|
fs.readdir(crashesDir, (err, files) => {
|
|
|
|
if (!err) {
|
|
|
|
for (const file of files) {
|
|
|
|
if (/\.dmp$/.test(file)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
existingDumpFiles.add(file);
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = null; // allow the renderer to crash
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
const dumpFileCreated = async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
async function getDumps () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const files = await fs.promises.readdir(crashesDir);
|
|
|
|
return files.filter((file) => /\.dmp$/.test(file) && !existingDumpFiles.has(file));
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
for (let i = 0; i < 30; i++) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const dumps = await getDumps();
|
2019-10-14 21:38:54 +00:00
|
|
|
if (dumps.length) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return path.join(crashesDir, dumps[0]);
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const dumpFile = await dumpFileCreated();
|
|
|
|
expect(dumpFile).to.be.a('string');
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
// dump file should not be deleted when not uploading, so we wait
|
|
|
|
// 1s and assert it still exists
|
2020-03-20 20:28:31 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
expect(fs.existsSync(dumpFile!)).to.be.true();
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
// the server should not have received any crashes.
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(getCrashes()).to.be.empty();
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
it('should send minidump with updated extra parameters', async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port, waitForCrash } = await startServer();
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
const crashUrl = url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
pathname: path.join(fixtures, 'api', 'crash-restart.html'),
|
|
|
|
search: `?port=${port}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadURL(crashUrl);
|
|
|
|
const crash = await waitForCrash();
|
|
|
|
checkCrash('renderer', crash);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
generateSpecs('without sandbox', {
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
generateSpecs('with sandbox', {
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-sandbox.js')
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('start(options)', () => {
|
|
|
|
it('requires that the companyName and submitURL options be specified', () => {
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
crashReporter.start({ companyName: 'Missing submitURL' } as any);
|
|
|
|
}).to.throw('submitURL is a required option to crashReporter.start');
|
2019-10-14 21:38:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
crashReporter.start({ submitURL: 'Missing companyName' } as any);
|
|
|
|
}).to.throw('companyName is a required option to crashReporter.start');
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('can be called multiple times', () => {
|
|
|
|
expect(() => {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation 2',
|
|
|
|
submitURL: 'http://127.0.0.1/more-crashes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('getCrashesDirectory', () => {
|
|
|
|
it('correctly returns the directory', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const crashesDir = crashReporter.getCrashesDirectory();
|
|
|
|
const dir = path.join(app.getPath('temp'), 'Electron Test Main Crashes');
|
|
|
|
expect(crashesDir).to.equal(dir);
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('getUploadedReports', () => {
|
|
|
|
it('returns an array of reports', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const reports = crashReporter.getUploadedReports();
|
|
|
|
expect(reports).to.be.an('array');
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
// TODO(alexeykuzmin): This suite should explicitly
|
|
|
|
// generate several crash reports instead of hoping
|
|
|
|
// that there will be enough of them already.
|
|
|
|
describe('getLastCrashReport', () => {
|
|
|
|
it('correctly returns the most recent report', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const reports = crashReporter.getUploadedReports();
|
|
|
|
expect(reports).to.be.an('array');
|
2019-10-14 21:38:54 +00:00
|
|
|
expect(reports).to.have.lengthOf.at.least(2,
|
2020-03-20 20:28:31 +00:00
|
|
|
'There are not enough reports for this test');
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const lastReport = crashReporter.getLastCrashReport();
|
|
|
|
expect(lastReport).to.be.an('object');
|
|
|
|
expect(lastReport.date).to.be.an.instanceOf(Date);
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
// Let's find the newest report.
|
|
|
|
const { report: newestReport } = reports.reduce((acc, cur) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const timestamp = new Date(cur.date).getTime();
|
2019-10-14 21:38:54 +00:00
|
|
|
return (timestamp > acc.timestamp)
|
|
|
|
? { report: cur, timestamp: timestamp }
|
2020-03-20 20:28:31 +00:00
|
|
|
: acc;
|
|
|
|
}, { timestamp: -Infinity } as { timestamp: number, report?: any });
|
|
|
|
expect(newestReport).to.be.an('object');
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
expect(lastReport.date.getTime()).to.be.equal(
|
|
|
|
newestReport.date.getTime(),
|
2020-03-20 20:28:31 +00:00
|
|
|
'Last report is not the newest.');
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('getUploadToServer()', () => {
|
|
|
|
it('returns true when uploadToServer is set to true', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes',
|
|
|
|
uploadToServer: true
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
expect(crashReporter.getUploadToServer()).to.be.true();
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('returns false when uploadToServer is set to false', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes',
|
|
|
|
uploadToServer: true
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
crashReporter.setUploadToServer(false);
|
|
|
|
expect(crashReporter.getUploadToServer()).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('setUploadToServer(uploadToServer)', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-10-14 21:38:54 +00:00
|
|
|
it('throws an error when called from the renderer process', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2019-10-14 21:38:54 +00:00
|
|
|
await expect(
|
2020-03-20 15:12:18 +00:00
|
|
|
w.webContents.executeJavaScript('require(\'electron\').crashReporter.setUploadToServer(true)')
|
2020-03-20 20:28:31 +00:00
|
|
|
).to.eventually.be.rejected();
|
2019-10-14 21:38:54 +00:00
|
|
|
await expect(
|
2020-03-20 15:12:18 +00:00
|
|
|
w.webContents.executeJavaScript('require(\'electron\').crashReporter.getUploadToServer()')
|
2020-03-20 20:28:31 +00:00
|
|
|
).to.eventually.be.rejected();
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('sets uploadToServer false when called with false', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes',
|
|
|
|
uploadToServer: true
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
crashReporter.setUploadToServer(false);
|
|
|
|
expect(crashReporter.getUploadToServer()).to.be.false();
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('sets uploadToServer true when called with true', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes',
|
|
|
|
uploadToServer: false
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
crashReporter.setUploadToServer(true);
|
|
|
|
expect(crashReporter.getUploadToServer()).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('Parameters', () => {
|
|
|
|
it('returns all of the current parameters', () => {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const parameters = crashReporter.getParameters();
|
|
|
|
expect(parameters).to.be.an('object');
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('adds a parameter to current parameters', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
crashReporter.addExtraParameter('hello', 'world');
|
|
|
|
expect(crashReporter.getParameters()).to.have.property('hello');
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
it('removes a parameter from current parameters', function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
crashReporter.addExtraParameter('hello', 'world');
|
|
|
|
expect(crashReporter.getParameters()).to.have.property('hello');
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
crashReporter.removeExtraParameter('hello');
|
|
|
|
expect(crashReporter.getParameters()).to.not.have.property('hello');
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
describe('when not started', () => {
|
|
|
|
it('does not prevent process from crashing', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const appPath = path.join(fixtures, 'api', 'cookie-app');
|
|
|
|
const appProcess = childProcess.spawn(process.execPath, [appPath]);
|
2020-01-27 01:29:50 +00:00
|
|
|
appProcess.once('exit', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
type CrashInfo = {
|
|
|
|
prod: string
|
|
|
|
ver: string
|
2019-11-01 20:37:02 +00:00
|
|
|
process_type: string // eslint-disable-line camelcase
|
2019-10-14 21:38:54 +00:00
|
|
|
platform: string
|
|
|
|
extra1: string
|
|
|
|
extra2: string
|
|
|
|
extra3: undefined
|
|
|
|
_productName: string
|
|
|
|
_companyName: string
|
|
|
|
_version: string
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
async function waitForCrashReport () {
|
2019-10-14 21:38:54 +00:00
|
|
|
for (let times = 0; times < 10; times++) {
|
|
|
|
if (crashReporter.getLastCrashReport() != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('No crash report available');
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
async function checkReport (reportId: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
await waitForCrashReport();
|
|
|
|
expect(crashReporter.getLastCrashReport().id).to.equal(reportId);
|
|
|
|
expect(crashReporter.getUploadedReports()).to.be.an('array').that.is.not.empty();
|
|
|
|
expect(crashReporter.getUploadedReports()[0].id).to.equal(reportId);
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
function checkCrash (expectedProcessType: string, fields: CrashInfo) {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(String(fields.prod)).to.equal('Electron');
|
|
|
|
expect(String(fields.ver)).to.equal(process.versions.electron);
|
|
|
|
expect(String(fields.process_type)).to.equal(expectedProcessType);
|
|
|
|
expect(String(fields.platform)).to.equal(process.platform);
|
|
|
|
expect(String(fields.extra1)).to.equal('extra1');
|
|
|
|
expect(String(fields.extra2)).to.equal('extra2');
|
|
|
|
expect(fields.extra3).to.be.undefined();
|
|
|
|
expect(String(fields._productName)).to.equal('Zombies');
|
|
|
|
expect(String(fields._companyName)).to.equal('Umbrella Corporation');
|
|
|
|
expect(String(fields._version)).to.equal(app.getVersion());
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let crashReporterPort = 0;
|
2019-10-14 21:38:54 +00:00
|
|
|
const startServer = async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const crashes: CrashInfo[] = [];
|
|
|
|
function getCrashes () { return crashes; }
|
|
|
|
const emitter = new EventEmitter();
|
2019-11-01 20:37:02 +00:00
|
|
|
function waitForCrash (): Promise<CrashInfo> {
|
2019-10-14 21:38:54 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
emitter.once('crash', (crash) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve(crash);
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const form = new multiparty.Form();
|
2019-10-14 21:38:54 +00:00
|
|
|
form.parse(req, (error, fields) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
crashes.push(fields);
|
|
|
|
if (error) throw error;
|
|
|
|
const reportId = 'abc-123-def-456-abc-789-abc-123-abcd';
|
2019-10-14 21:38:54 +00:00
|
|
|
res.end(reportId, async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await checkReport(reportId);
|
|
|
|
req.socket.destroy();
|
|
|
|
emitter.emit('crash', fields);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
server.listen(crashReporterPort, '127.0.0.1', () => { resolve(); });
|
|
|
|
});
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const port = (server.address() as AddressInfo).port;
|
2019-10-14 21:38:54 +00:00
|
|
|
|
|
|
|
if (crashReporterPort === 0) {
|
|
|
|
// We can only start the crash reporter once, and after that these
|
|
|
|
// parameters are fixed.
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1:' + port
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
crashReporterPort = port;
|
2019-10-14 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
afterTest.push(() => { server.close(); });
|
2019-10-14 21:38:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return { getCrashes, port, waitForCrash };
|
|
|
|
};
|