api-crash-reporter-spec.ts 26 KB

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