api-crash-reporter-spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. import { expect } from 'chai';
  2. import * as childProcess from 'child_process';
  3. import * as http from 'http';
  4. import * as Busboy from 'busboy';
  5. import * as path from 'path';
  6. import { ifdescribe, ifit, defer, startRemoteControlApp, delay, repeatedly } from './spec-helpers';
  7. import { app } from 'electron/main';
  8. import { crashReporter } from 'electron/common';
  9. import { AddressInfo } from 'net';
  10. import { EventEmitter } from 'events';
  11. import * as fs from 'fs';
  12. import * as uuid from 'uuid';
  13. const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
  14. const isLinuxOnArm = process.platform === 'linux' && process.arch.includes('arm');
  15. type CrashInfo = {
  16. prod: string
  17. ver: string
  18. process_type: string // eslint-disable-line camelcase
  19. ptype: string
  20. platform: string
  21. _productName: string
  22. _version: string
  23. upload_file_minidump: Buffer // eslint-disable-line camelcase
  24. guid: string
  25. mainProcessSpecific: 'mps' | undefined
  26. rendererSpecific: 'rs' | undefined
  27. globalParam: 'globalValue' | undefined
  28. addedThenRemoved: 'to-be-removed' | undefined
  29. longParam: string | undefined
  30. 'electron.v8-fatal.location': string | undefined
  31. 'electron.v8-fatal.message': string | undefined
  32. }
  33. function checkCrash (expectedProcessType: string, fields: CrashInfo) {
  34. expect(String(fields.prod)).to.equal('Electron', 'prod');
  35. expect(String(fields.ver)).to.equal(process.versions.electron, 'ver');
  36. expect(String(fields.ptype)).to.equal(expectedProcessType, 'ptype');
  37. expect(String(fields.process_type)).to.equal(expectedProcessType, 'process_type');
  38. expect(String(fields.platform)).to.equal(process.platform, 'platform');
  39. expect(String(fields._productName)).to.equal('Zombies', '_productName');
  40. expect(String(fields._version)).to.equal(app.getVersion(), '_version');
  41. expect(fields.upload_file_minidump).to.be.an.instanceOf(Buffer);
  42. // TODO(nornagon): minidumps are sometimes (not always) turning up empty on
  43. // 32-bit Linux. Figure out why.
  44. if (!(process.platform === 'linux' && process.arch === 'ia32')) {
  45. expect(fields.upload_file_minidump.length).to.be.greaterThan(0);
  46. }
  47. }
  48. const startServer = async () => {
  49. const crashes: CrashInfo[] = [];
  50. function getCrashes () { return crashes; }
  51. const emitter = new EventEmitter();
  52. function waitForCrash (): Promise<CrashInfo> {
  53. return new Promise(resolve => {
  54. emitter.once('crash', (crash) => {
  55. resolve(crash);
  56. });
  57. });
  58. }
  59. const server = http.createServer((req, res) => {
  60. const busboy = new Busboy({ headers: req.headers });
  61. const fields = {} as Record<string, any>;
  62. const files = {} as Record<string, Buffer>;
  63. busboy.on('file', (fieldname, file) => {
  64. const chunks = [] as Array<Buffer>;
  65. file.on('data', (chunk) => {
  66. chunks.push(chunk);
  67. });
  68. file.on('end', () => {
  69. files[fieldname] = Buffer.concat(chunks);
  70. });
  71. });
  72. busboy.on('field', (fieldname, val) => {
  73. fields[fieldname] = val;
  74. });
  75. busboy.on('finish', () => {
  76. // breakpad id must be 16 hex digits.
  77. const reportId = Math.random().toString(16).split('.')[1].padStart(16, '0');
  78. res.end(reportId, async () => {
  79. req.socket.destroy();
  80. emitter.emit('crash', { ...fields, ...files });
  81. });
  82. });
  83. req.pipe(busboy);
  84. });
  85. await new Promise<void>(resolve => {
  86. server.listen(0, '127.0.0.1', () => { resolve(); });
  87. });
  88. const port = (server.address() as AddressInfo).port;
  89. defer(() => { server.close(); });
  90. return { getCrashes, port, waitForCrash };
  91. };
  92. function runApp (appPath: string, args: Array<string> = []) {
  93. const appProcess = childProcess.spawn(process.execPath, [appPath, ...args]);
  94. return new Promise(resolve => {
  95. appProcess.once('exit', resolve);
  96. });
  97. }
  98. function runCrashApp (crashType: string, port: number, extraArgs: Array<string> = []) {
  99. const appPath = path.join(__dirname, 'fixtures', 'apps', 'crash');
  100. return runApp(appPath, [
  101. `--crash-type=${crashType}`,
  102. `--crash-reporter-url=http://127.0.0.1:${port}`,
  103. ...extraArgs
  104. ]);
  105. }
  106. function waitForNewFileInDir (dir: string): Promise<string[]> {
  107. function readdirIfPresent (dir: string): string[] {
  108. try {
  109. return fs.readdirSync(dir);
  110. } catch (e) {
  111. return [];
  112. }
  113. }
  114. const initialFiles = readdirIfPresent(dir);
  115. return new Promise(resolve => {
  116. const ivl = setInterval(() => {
  117. const newCrashFiles = readdirIfPresent(dir).filter(f => !initialFiles.includes(f));
  118. if (newCrashFiles.length) {
  119. clearInterval(ivl);
  120. resolve(newCrashFiles);
  121. }
  122. }, 1000);
  123. });
  124. }
  125. // TODO(nornagon): Fix tests on linux/arm.
  126. ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_TESTS)('crashReporter module', function () {
  127. describe('should send minidump', () => {
  128. it('when renderer crashes', async () => {
  129. const { port, waitForCrash } = await startServer();
  130. runCrashApp('renderer', port);
  131. const crash = await waitForCrash();
  132. checkCrash('renderer', crash);
  133. expect(crash.mainProcessSpecific).to.be.undefined();
  134. });
  135. it('when sandboxed renderer crashes', async () => {
  136. const { port, waitForCrash } = await startServer();
  137. runCrashApp('sandboxed-renderer', port);
  138. const crash = await waitForCrash();
  139. checkCrash('renderer', crash);
  140. expect(crash.mainProcessSpecific).to.be.undefined();
  141. });
  142. // TODO(nornagon): Minidump generation in main/node process on Linux/Arm is
  143. // broken (//components/crash prints "Failed to generate minidump"). Figure
  144. // out why.
  145. ifit(!isLinuxOnArm)('when main process crashes', async () => {
  146. const { port, waitForCrash } = await startServer();
  147. runCrashApp('main', port);
  148. const crash = await waitForCrash();
  149. checkCrash('browser', crash);
  150. expect(crash.mainProcessSpecific).to.equal('mps');
  151. });
  152. ifit(!isLinuxOnArm)('when a node process crashes', async () => {
  153. const { port, waitForCrash } = await startServer();
  154. runCrashApp('node', port);
  155. const crash = await waitForCrash();
  156. checkCrash('node', crash);
  157. expect(crash.mainProcessSpecific).to.be.undefined();
  158. expect(crash.rendererSpecific).to.be.undefined();
  159. });
  160. describe('with guid', () => {
  161. for (const processType of ['main', 'renderer', 'sandboxed-renderer']) {
  162. it(`when ${processType} crashes`, async () => {
  163. const { port, waitForCrash } = await startServer();
  164. runCrashApp(processType, port);
  165. const crash = await waitForCrash();
  166. expect(crash.guid).to.be.a('string');
  167. });
  168. }
  169. it('is a consistent id', async () => {
  170. let crash1Guid;
  171. let crash2Guid;
  172. {
  173. const { port, waitForCrash } = await startServer();
  174. runCrashApp('main', port);
  175. const crash = await waitForCrash();
  176. crash1Guid = crash.guid;
  177. }
  178. {
  179. const { port, waitForCrash } = await startServer();
  180. runCrashApp('main', port);
  181. const crash = await waitForCrash();
  182. crash2Guid = crash.guid;
  183. }
  184. expect(crash2Guid).to.equal(crash1Guid);
  185. });
  186. });
  187. describe('with extra parameters', () => {
  188. it('when renderer crashes', async () => {
  189. const { port, waitForCrash } = await startServer();
  190. runCrashApp('renderer', port, ['--set-extra-parameters-in-renderer']);
  191. const crash = await waitForCrash();
  192. checkCrash('renderer', crash);
  193. expect(crash.mainProcessSpecific).to.be.undefined();
  194. expect(crash.rendererSpecific).to.equal('rs');
  195. expect(crash.addedThenRemoved).to.be.undefined();
  196. });
  197. it('when sandboxed renderer crashes', async () => {
  198. const { port, waitForCrash } = await startServer();
  199. runCrashApp('sandboxed-renderer', port, ['--set-extra-parameters-in-renderer']);
  200. const crash = await waitForCrash();
  201. checkCrash('renderer', crash);
  202. expect(crash.mainProcessSpecific).to.be.undefined();
  203. expect(crash.rendererSpecific).to.equal('rs');
  204. expect(crash.addedThenRemoved).to.be.undefined();
  205. });
  206. it('contains v8 crash keys when a v8 crash occurs', async () => {
  207. const { remotely } = await startRemoteControlApp();
  208. const { port, waitForCrash } = await startServer();
  209. await remotely((port: number) => {
  210. require('electron').crashReporter.start({
  211. submitURL: `http://127.0.0.1:${port}`,
  212. compress: false,
  213. ignoreSystemCrashHandler: true
  214. });
  215. }, [port]);
  216. remotely(() => {
  217. const { BrowserWindow } = require('electron');
  218. const bw = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  219. bw.loadURL('about:blank');
  220. bw.webContents.executeJavaScript('process._linkedBinding(\'electron_common_v8_util\').triggerFatalErrorForTesting()');
  221. });
  222. const crash = await waitForCrash();
  223. expect(crash.prod).to.equal('Electron');
  224. expect(crash._productName).to.equal('electron-test-remote-control');
  225. expect(crash.process_type).to.equal('renderer');
  226. expect(crash['electron.v8-fatal.location']).to.equal('v8::Context::New()');
  227. expect(crash['electron.v8-fatal.message']).to.equal('Circular extension dependency');
  228. });
  229. });
  230. });
  231. ifdescribe(!isLinuxOnArm)('extra parameter limits', () => {
  232. function stitchLongCrashParam (crash: any, paramKey: string) {
  233. if (crash[paramKey]) return crash[paramKey];
  234. let chunk = 1;
  235. let stitched = '';
  236. while (crash[`${paramKey}__${chunk}`]) {
  237. stitched += crash[`${paramKey}__${chunk}`];
  238. chunk++;
  239. }
  240. return stitched;
  241. }
  242. it('should truncate extra values longer than 5 * 4096 characters', async () => {
  243. const { port, waitForCrash } = await startServer();
  244. const { remotely } = await startRemoteControlApp();
  245. remotely((port: number) => {
  246. require('electron').crashReporter.start({
  247. submitURL: `http://127.0.0.1:${port}`,
  248. compress: false,
  249. ignoreSystemCrashHandler: true,
  250. extra: { longParam: 'a'.repeat(100000) }
  251. });
  252. setTimeout(() => process.crash());
  253. }, port);
  254. const crash = await waitForCrash();
  255. expect(stitchLongCrashParam(crash, 'longParam')).to.have.lengthOf(160 * 127 + (process.platform === 'linux' ? 159 : 0), 'crash should have truncated longParam');
  256. });
  257. it('should omit extra keys with names longer than the maximum', async () => {
  258. const kKeyLengthMax = 39;
  259. const { port, waitForCrash } = await startServer();
  260. const { remotely } = await startRemoteControlApp();
  261. remotely((port: number, kKeyLengthMax: number) => {
  262. require('electron').crashReporter.start({
  263. submitURL: `http://127.0.0.1:${port}`,
  264. compress: false,
  265. ignoreSystemCrashHandler: true,
  266. extra: {
  267. ['a'.repeat(kKeyLengthMax + 10)]: 'value',
  268. ['b'.repeat(kKeyLengthMax)]: 'value',
  269. 'not-long': 'not-long-value'
  270. }
  271. });
  272. require('electron').crashReporter.addExtraParameter('c'.repeat(kKeyLengthMax + 10), 'value');
  273. setTimeout(() => process.crash());
  274. }, port, kKeyLengthMax);
  275. const crash = await waitForCrash();
  276. expect(crash).not.to.have.property('a'.repeat(kKeyLengthMax + 10));
  277. expect(crash).not.to.have.property('a'.repeat(kKeyLengthMax));
  278. expect(crash).to.have.property('b'.repeat(kKeyLengthMax), 'value');
  279. expect(crash).to.have.property('not-long', 'not-long-value');
  280. expect(crash).not.to.have.property('c'.repeat(kKeyLengthMax + 10));
  281. expect(crash).not.to.have.property('c'.repeat(kKeyLengthMax));
  282. });
  283. });
  284. describe('globalExtra', () => {
  285. ifit(!isLinuxOnArm)('should be sent with main process dumps', async () => {
  286. const { port, waitForCrash } = await startServer();
  287. runCrashApp('main', port, ['--add-global-param=globalParam:globalValue']);
  288. const crash = await waitForCrash();
  289. expect(crash.globalParam).to.equal('globalValue');
  290. });
  291. it('should be sent with renderer process dumps', async () => {
  292. const { port, waitForCrash } = await startServer();
  293. runCrashApp('renderer', port, ['--add-global-param=globalParam:globalValue']);
  294. const crash = await waitForCrash();
  295. expect(crash.globalParam).to.equal('globalValue');
  296. });
  297. it('should be sent with sandboxed renderer process dumps', async () => {
  298. const { port, waitForCrash } = await startServer();
  299. runCrashApp('sandboxed-renderer', port, ['--add-global-param=globalParam:globalValue']);
  300. const crash = await waitForCrash();
  301. expect(crash.globalParam).to.equal('globalValue');
  302. });
  303. ifit(!isLinuxOnArm)('should not be overridden by extra in main process', async () => {
  304. const { port, waitForCrash } = await startServer();
  305. runCrashApp('main', port, ['--add-global-param=mainProcessSpecific:global']);
  306. const crash = await waitForCrash();
  307. expect(crash.mainProcessSpecific).to.equal('global');
  308. });
  309. ifit(!isLinuxOnArm)('should not be overridden by extra in renderer process', async () => {
  310. const { port, waitForCrash } = await startServer();
  311. runCrashApp('main', port, ['--add-global-param=rendererSpecific:global']);
  312. const crash = await waitForCrash();
  313. expect(crash.rendererSpecific).to.equal('global');
  314. });
  315. });
  316. // TODO(nornagon): also test crashing main / sandboxed renderers.
  317. ifit(!isWindowsOnArm)('should not send a minidump when uploadToServer is false', async () => {
  318. const { port, waitForCrash, getCrashes } = await startServer();
  319. waitForCrash().then(() => expect.fail('expected not to receive a dump'));
  320. await runCrashApp('renderer', port, ['--no-upload']);
  321. // wait a sec in case the crash reporter is about to upload a crash
  322. await delay(1000);
  323. expect(getCrashes()).to.have.length(0);
  324. });
  325. describe('getUploadedReports', () => {
  326. it('returns an array of reports', async () => {
  327. const { remotely } = await startRemoteControlApp();
  328. await remotely(() => {
  329. require('electron').crashReporter.start({ submitURL: 'http://127.0.0.1' });
  330. });
  331. const reports = await remotely(() => require('electron').crashReporter.getUploadedReports());
  332. expect(reports).to.be.an('array');
  333. });
  334. });
  335. // TODO(nornagon): re-enable on woa
  336. ifdescribe(!isWindowsOnArm)('getLastCrashReport', () => {
  337. it('returns the last uploaded report', async () => {
  338. const { remotely } = await startRemoteControlApp();
  339. const { port, waitForCrash } = await startServer();
  340. // 0. clear the crash reports directory.
  341. const dir = await remotely(() => require('electron').app.getPath('crashDumps'));
  342. try {
  343. fs.rmdirSync(dir, { recursive: true });
  344. fs.mkdirSync(dir);
  345. } catch (e) { /* ignore */ }
  346. // 1. start the crash reporter.
  347. await remotely((port: number) => {
  348. require('electron').crashReporter.start({
  349. submitURL: `http://127.0.0.1:${port}`,
  350. compress: false,
  351. ignoreSystemCrashHandler: true
  352. });
  353. }, [port]);
  354. // 2. generate a crash in the renderer.
  355. remotely(() => {
  356. const { BrowserWindow } = require('electron');
  357. const bw = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  358. bw.loadURL('about:blank');
  359. bw.webContents.executeJavaScript('process.crash()');
  360. });
  361. await waitForCrash();
  362. // 3. get the crash from getLastCrashReport.
  363. const firstReport = await repeatedly(
  364. () => remotely(() => require('electron').crashReporter.getLastCrashReport())
  365. );
  366. expect(firstReport).to.not.be.null();
  367. expect(firstReport.date).to.be.an.instanceOf(Date);
  368. expect((+new Date()) - (+firstReport.date)).to.be.lessThan(30000);
  369. });
  370. });
  371. describe('getParameters', () => {
  372. it('returns all of the current parameters', async () => {
  373. const { remotely } = await startRemoteControlApp();
  374. await remotely(() => {
  375. require('electron').crashReporter.start({
  376. submitURL: 'http://127.0.0.1',
  377. extra: { extra1: 'hi' }
  378. });
  379. });
  380. const parameters = await remotely(() => require('electron').crashReporter.getParameters());
  381. expect(parameters).to.have.property('extra1', 'hi');
  382. });
  383. it('reflects added and removed parameters', async () => {
  384. const { remotely } = await startRemoteControlApp();
  385. await remotely(() => {
  386. require('electron').crashReporter.start({ submitURL: 'http://127.0.0.1' });
  387. require('electron').crashReporter.addExtraParameter('hello', 'world');
  388. });
  389. {
  390. const parameters = await remotely(() => require('electron').crashReporter.getParameters());
  391. expect(parameters).to.have.property('hello', 'world');
  392. }
  393. await remotely(() => { require('electron').crashReporter.removeExtraParameter('hello'); });
  394. {
  395. const parameters = await remotely(() => require('electron').crashReporter.getParameters());
  396. expect(parameters).not.to.have.property('hello');
  397. }
  398. });
  399. it('can be called in the renderer', async () => {
  400. const { remotely } = await startRemoteControlApp();
  401. const rendererParameters = await remotely(async () => {
  402. const { crashReporter, BrowserWindow } = require('electron');
  403. crashReporter.start({ submitURL: 'http://' });
  404. const bw = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  405. bw.loadURL('about:blank');
  406. await bw.webContents.executeJavaScript('require(\'electron\').crashReporter.addExtraParameter(\'hello\', \'world\')');
  407. return bw.webContents.executeJavaScript('require(\'electron\').crashReporter.getParameters()');
  408. });
  409. expect(rendererParameters).to.deep.equal({ hello: 'world' });
  410. });
  411. it('can be called in a node child process', async () => {
  412. function slurp (stream: NodeJS.ReadableStream): Promise<string> {
  413. return new Promise((resolve, reject) => {
  414. const chunks: Buffer[] = [];
  415. stream.on('data', chunk => { chunks.push(chunk); });
  416. stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
  417. stream.on('error', e => reject(e));
  418. });
  419. }
  420. // TODO(nornagon): how to enable crashpad in a node child process...?
  421. const child = childProcess.fork(path.join(__dirname, 'fixtures', 'module', 'print-crash-parameters.js'), [], { silent: true });
  422. const output = await slurp(child.stdout!);
  423. expect(JSON.parse(output)).to.deep.equal({ hello: 'world' });
  424. });
  425. });
  426. describe('crash dumps directory', () => {
  427. it('is set by default', () => {
  428. expect(app.getPath('crashDumps')).to.be.a('string');
  429. });
  430. it('is inside the user data dir', () => {
  431. expect(app.getPath('crashDumps')).to.include(app.getPath('userData'));
  432. });
  433. function crash (processType: string, remotely: Function) {
  434. if (processType === 'main') {
  435. return remotely(() => {
  436. setTimeout(() => { process.crash(); });
  437. });
  438. } else if (processType === 'renderer') {
  439. return remotely(() => {
  440. const { BrowserWindow } = require('electron');
  441. const bw = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  442. bw.loadURL('about:blank');
  443. bw.webContents.executeJavaScript('process.crash()');
  444. });
  445. } else if (processType === 'sandboxed-renderer') {
  446. const preloadPath = path.join(__dirname, 'fixtures', 'apps', 'crash', 'sandbox-preload.js');
  447. return remotely((preload: string) => {
  448. const { BrowserWindow } = require('electron');
  449. const bw = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload, contextIsolation: false } });
  450. bw.loadURL('about:blank');
  451. }, preloadPath);
  452. } else if (processType === 'node') {
  453. const crashScriptPath = path.join(__dirname, 'fixtures', 'apps', 'crash', 'node-crash.js');
  454. return remotely((crashScriptPath: string) => {
  455. const { app } = require('electron');
  456. const childProcess = require('child_process');
  457. const version = app.getVersion();
  458. const url = 'http://127.0.0.1';
  459. childProcess.fork(crashScriptPath, [url, version], { silent: true });
  460. }, crashScriptPath);
  461. }
  462. }
  463. const processList = process.platform === 'linux' ? ['main', 'renderer', 'sandboxed-renderer']
  464. : ['main', 'renderer', 'sandboxed-renderer', 'node'];
  465. for (const crashingProcess of processList) {
  466. describe(`when ${crashingProcess} crashes`, () => {
  467. it('stores crashes in the crash dump directory when uploadToServer: false', async () => {
  468. const { remotely } = await startRemoteControlApp();
  469. const crashesDir = await remotely(() => {
  470. const { crashReporter, app } = require('electron');
  471. crashReporter.start({ submitURL: 'http://127.0.0.1', uploadToServer: false, ignoreSystemCrashHandler: true });
  472. return app.getPath('crashDumps');
  473. });
  474. let reportsDir = crashesDir;
  475. if (process.platform === 'darwin' || process.platform === 'linux') {
  476. reportsDir = path.join(crashesDir, 'completed');
  477. } else if (process.platform === 'win32') {
  478. reportsDir = path.join(crashesDir, 'reports');
  479. }
  480. const newFileAppeared = waitForNewFileInDir(reportsDir);
  481. crash(crashingProcess, remotely);
  482. const newFiles = await newFileAppeared;
  483. expect(newFiles.length).to.be.greaterThan(0);
  484. expect(newFiles[0]).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.dmp$/);
  485. });
  486. it('respects an overridden crash dump directory', async () => {
  487. const { remotely } = await startRemoteControlApp();
  488. const crashesDir = path.join(app.getPath('temp'), uuid.v4());
  489. const remoteCrashesDir = await remotely((crashesDir: string) => {
  490. const { crashReporter, app } = require('electron');
  491. app.setPath('crashDumps', crashesDir);
  492. crashReporter.start({ submitURL: 'http://127.0.0.1', uploadToServer: false, ignoreSystemCrashHandler: true });
  493. return app.getPath('crashDumps');
  494. }, crashesDir);
  495. expect(remoteCrashesDir).to.equal(crashesDir);
  496. let reportsDir = crashesDir;
  497. if (process.platform === 'darwin' || process.platform === 'linux') {
  498. reportsDir = path.join(crashesDir, 'completed');
  499. } else if (process.platform === 'win32') {
  500. reportsDir = path.join(crashesDir, 'reports');
  501. }
  502. const newFileAppeared = waitForNewFileInDir(reportsDir);
  503. crash(crashingProcess, remotely);
  504. const newFiles = await newFileAppeared;
  505. expect(newFiles.length).to.be.greaterThan(0);
  506. expect(newFiles[0]).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.dmp$/);
  507. });
  508. });
  509. }
  510. });
  511. describe('start() option validation', () => {
  512. it('requires that the submitURL option be specified', () => {
  513. expect(() => {
  514. crashReporter.start({} as any);
  515. }).to.throw('submitURL must be specified when uploadToServer is true');
  516. });
  517. it('allows the submitURL option to be omitted when uploadToServer is false', () => {
  518. expect(() => {
  519. crashReporter.start({ uploadToServer: false } as any);
  520. }).not.to.throw();
  521. });
  522. it('can be called twice', async () => {
  523. const { remotely } = await startRemoteControlApp();
  524. await expect(remotely(() => {
  525. const { crashReporter } = require('electron');
  526. crashReporter.start({ submitURL: 'http://127.0.0.1' });
  527. crashReporter.start({ submitURL: 'http://127.0.0.1' });
  528. })).to.be.fulfilled();
  529. });
  530. });
  531. describe('getUploadToServer()', () => {
  532. it('returns true when uploadToServer is set to true (by default)', async () => {
  533. const { remotely } = await startRemoteControlApp();
  534. await remotely(() => { require('electron').crashReporter.start({ submitURL: 'http://127.0.0.1' }); });
  535. const uploadToServer = await remotely(() => require('electron').crashReporter.getUploadToServer());
  536. expect(uploadToServer).to.be.true();
  537. });
  538. it('returns false when uploadToServer is set to false in init', async () => {
  539. const { remotely } = await startRemoteControlApp();
  540. await remotely(() => { require('electron').crashReporter.start({ submitURL: 'http://127.0.0.1', uploadToServer: false }); });
  541. const uploadToServer = await remotely(() => require('electron').crashReporter.getUploadToServer());
  542. expect(uploadToServer).to.be.false();
  543. });
  544. it('is updated by setUploadToServer', async () => {
  545. const { remotely } = await startRemoteControlApp();
  546. await remotely(() => { require('electron').crashReporter.start({ submitURL: 'http://127.0.0.1' }); });
  547. await remotely(() => { require('electron').crashReporter.setUploadToServer(false); });
  548. expect(await remotely(() => require('electron').crashReporter.getUploadToServer())).to.be.false();
  549. await remotely(() => { require('electron').crashReporter.setUploadToServer(true); });
  550. expect(await remotely(() => require('electron').crashReporter.getUploadToServer())).to.be.true();
  551. });
  552. });
  553. describe('when not started', () => {
  554. it('does not prevent process from crashing', async () => {
  555. const appPath = path.join(__dirname, '..', 'spec', 'fixtures', 'api', 'cookie-app');
  556. await runApp(appPath);
  557. });
  558. });
  559. });