api-crash-reporter-spec.ts 26 KB

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