api-crash-reporter-spec.ts 27 KB

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