api-crash-reporter-spec.ts 27 KB

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