2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2017-01-24 21:49:45 +00:00
|
|
|
const childProcess = require('child_process')
|
2017-03-30 14:11:49 +00:00
|
|
|
const fs = require('fs')
|
2016-03-25 20:03:49 +00:00
|
|
|
const http = require('http')
|
|
|
|
const multiparty = require('multiparty')
|
|
|
|
const path = require('path')
|
2016-10-05 20:53:13 +00:00
|
|
|
const temp = require('temp').track()
|
2016-03-25 20:03:49 +00:00
|
|
|
const url = require('url')
|
2016-08-03 19:47:53 +00:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-10-05 20:40:39 +00:00
|
|
|
const {remote} = require('electron')
|
2017-05-15 17:57:45 +00:00
|
|
|
const isCI = remote.getGlobal('isCi')
|
2016-10-05 20:40:39 +00:00
|
|
|
const {app, BrowserWindow, crashReporter} = remote.require('electron')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-08-09 22:41:46 +00:00
|
|
|
describe('crashReporter module', function () {
|
2017-03-21 13:48:36 +00:00
|
|
|
if (process.mas) {
|
|
|
|
return
|
|
|
|
}
|
2017-04-05 20:42:24 +00:00
|
|
|
|
2017-05-15 17:57:45 +00:00
|
|
|
// FIXME internal Linux CI is failing when it detects a process crashes
|
|
|
|
// which is a false positive here since crashes are explicitly triggered
|
|
|
|
if (isCI && process.platform === 'linux') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:42:24 +00:00
|
|
|
var originalTempDirectory = null
|
|
|
|
var tempDirectory = null
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
tempDirectory = temp.mkdirSync('electronCrashReporterSpec-')
|
|
|
|
originalTempDirectory = app.getPath('temp')
|
|
|
|
app.setPath('temp', tempDirectory)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
app.setPath('temp', originalTempDirectory)
|
|
|
|
})
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
var fixtures = path.resolve(__dirname, 'fixtures')
|
2017-03-21 13:48:36 +00:00
|
|
|
const generateSpecs = (description, browserWindowOpts) => {
|
2017-03-21 13:53:17 +00:00
|
|
|
describe(description, function () {
|
|
|
|
var w = null
|
2017-04-19 16:48:32 +00:00
|
|
|
var stopServer = null
|
2017-03-21 13:53:17 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-04-19 16:48:32 +00:00
|
|
|
stopServer = null
|
2017-03-21 13:53:17 +00:00
|
|
|
w = new BrowserWindow(Object.assign({
|
|
|
|
show: false
|
|
|
|
}, browserWindowOpts))
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
return closeWindow(w).then(function () { w = null })
|
|
|
|
})
|
|
|
|
|
2017-04-19 23:32:43 +00:00
|
|
|
afterEach(function () {
|
|
|
|
stopCrashService()
|
|
|
|
})
|
|
|
|
|
2017-04-19 16:48:32 +00:00
|
|
|
afterEach(function (done) {
|
|
|
|
if (stopServer != null) {
|
|
|
|
stopServer(done)
|
2017-04-19 16:52:28 +00:00
|
|
|
} else {
|
|
|
|
done()
|
2017-04-19 16:48:32 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-03-21 13:53:17 +00:00
|
|
|
it('should send minidump when renderer crashes', function (done) {
|
|
|
|
if (process.env.APPVEYOR === 'True') return done()
|
|
|
|
if (process.env.TRAVIS === 'true') return done()
|
|
|
|
|
|
|
|
this.timeout(120000)
|
|
|
|
|
2017-04-19 16:48:32 +00:00
|
|
|
stopServer = startServer({
|
2017-03-21 13:53:17 +00:00
|
|
|
callback (port) {
|
|
|
|
const crashUrl = url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
pathname: path.join(fixtures, 'api', 'crash.html'),
|
|
|
|
search: '?port=' + port
|
|
|
|
})
|
|
|
|
w.loadURL(crashUrl)
|
|
|
|
},
|
|
|
|
processType: 'renderer',
|
|
|
|
done: done
|
2016-08-09 22:41:46 +00:00
|
|
|
})
|
2017-03-21 13:53:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should send minidump when node processes crash', function (done) {
|
|
|
|
if (process.env.APPVEYOR === 'True') return done()
|
|
|
|
if (process.env.TRAVIS === 'true') return done()
|
|
|
|
|
|
|
|
this.timeout(120000)
|
|
|
|
|
2017-04-19 16:48:32 +00:00
|
|
|
stopServer = startServer({
|
2017-03-21 13:53:17 +00:00
|
|
|
callback (port) {
|
2017-04-19 23:53:57 +00:00
|
|
|
const crashesDir = path.join(app.getPath('temp'), `${process.platform === 'win32' ? 'Zombies' : app.getName()} Crashes`)
|
2017-03-21 13:53:17 +00:00
|
|
|
const version = app.getVersion()
|
|
|
|
const crashPath = path.join(fixtures, 'module', 'crash.js')
|
2017-04-19 23:32:43 +00:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
const crashServiceProcess = childProcess.spawn(process.execPath, [
|
|
|
|
`--reporter-url=http://127.0.0.1:${port}`,
|
|
|
|
'--application-name=Zombies',
|
|
|
|
`--crashes-directory=${crashesDir}`
|
|
|
|
], {
|
2017-04-19 23:44:31 +00:00
|
|
|
env: {
|
|
|
|
ELECTRON_INTERNAL_CRASH_SERVICE: 1
|
|
|
|
},
|
|
|
|
detached: true
|
2017-04-19 23:32:43 +00:00
|
|
|
})
|
|
|
|
remote.process.crashServicePid = crashServiceProcess.pid
|
|
|
|
}
|
|
|
|
|
2017-03-21 13:53:17 +00:00
|
|
|
childProcess.fork(crashPath, [port, version, crashesDir], {silent: true})
|
|
|
|
},
|
|
|
|
processType: 'browser',
|
|
|
|
done: done
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-03-30 14:11:49 +00:00
|
|
|
it('should not send minidump if uploadToServer is false', function (done) {
|
|
|
|
this.timeout(120000)
|
|
|
|
|
2017-04-05 20:42:24 +00:00
|
|
|
let dumpFile
|
|
|
|
let crashesDir = crashReporter.getCrashesDirectory()
|
|
|
|
const existingDumpFiles = new Set()
|
2017-03-30 14:11:49 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2017-04-05 20:42:24 +00:00
|
|
|
// crashpad puts the dump files in the "completed" subdirectory
|
|
|
|
crashesDir = path.join(crashesDir, 'completed')
|
2017-03-30 14:11:49 +00:00
|
|
|
crashReporter.setUploadToServer(false)
|
|
|
|
}
|
|
|
|
const testDone = (uploaded) => {
|
|
|
|
if (uploaded) {
|
2017-04-19 23:32:43 +00:00
|
|
|
return done(new Error('Uploaded crash report'))
|
2017-03-30 14:11:49 +00:00
|
|
|
}
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
crashReporter.setUploadToServer(true)
|
|
|
|
}
|
|
|
|
assert(fs.existsSync(dumpFile))
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
let pollInterval
|
|
|
|
const pollDumpFile = () => {
|
|
|
|
fs.readdir(crashesDir, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
return
|
|
|
|
}
|
2017-04-05 20:42:24 +00:00
|
|
|
const dumps = files.filter((file) => /\.dmp$/.test(file) && !existingDumpFiles.has(file))
|
2017-03-30 14:11:49 +00:00
|
|
|
if (!dumps.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.equal(1, dumps.length)
|
|
|
|
dumpFile = path.join(crashesDir, dumps[0])
|
|
|
|
clearInterval(pollInterval)
|
|
|
|
// dump file should not be deleted when not uploading, so we wait
|
2017-04-05 20:42:24 +00:00
|
|
|
// 1s and assert it still exists in `testDone`
|
|
|
|
setTimeout(testDone, 1000)
|
2017-03-30 14:11:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:42:24 +00:00
|
|
|
remote.ipcMain.once('list-existing-dumps', (event) => {
|
2017-03-30 14:11:49 +00:00
|
|
|
fs.readdir(crashesDir, (err, files) => {
|
|
|
|
if (!err) {
|
2017-03-31 17:42:43 +00:00
|
|
|
for (const file of files) {
|
2017-03-30 14:11:49 +00:00
|
|
|
if (/\.dmp$/.test(file)) {
|
2017-04-05 20:42:24 +00:00
|
|
|
existingDumpFiles.add(file)
|
2017-03-30 14:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.returnValue = null // allow the renderer to crash
|
|
|
|
pollInterval = setInterval(pollDumpFile, 100)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-19 16:48:32 +00:00
|
|
|
stopServer = startServer({
|
2017-03-30 14:11:49 +00:00
|
|
|
callback (port) {
|
|
|
|
const crashUrl = url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
pathname: path.join(fixtures, 'api', 'crash.html'),
|
|
|
|
search: `?port=${port}&skipUpload=1`
|
|
|
|
})
|
|
|
|
w.loadURL(crashUrl)
|
|
|
|
},
|
|
|
|
processType: 'renderer',
|
|
|
|
done: testDone.bind(null, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-03-21 13:53:17 +00:00
|
|
|
it('should send minidump with updated extra parameters', function (done) {
|
|
|
|
if (process.env.APPVEYOR === 'True') return done()
|
|
|
|
if (process.env.TRAVIS === 'true') return done()
|
|
|
|
|
2017-04-19 16:30:03 +00:00
|
|
|
this.timeout(120000)
|
2017-03-21 13:53:17 +00:00
|
|
|
|
2017-04-19 16:48:32 +00:00
|
|
|
stopServer = startServer({
|
2017-03-21 13:53:17 +00:00
|
|
|
callback (port) {
|
|
|
|
const crashUrl = url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
pathname: path.join(fixtures, 'api', 'crash-restart.html'),
|
|
|
|
search: '?port=' + port
|
|
|
|
})
|
|
|
|
w.loadURL(crashUrl)
|
|
|
|
},
|
|
|
|
processType: 'renderer',
|
|
|
|
done: done
|
2017-02-13 18:46:19 +00:00
|
|
|
})
|
2017-03-21 13:53:17 +00:00
|
|
|
})
|
2017-02-13 18:46:19 +00:00
|
|
|
})
|
2017-03-21 13:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generateSpecs('without sandbox', {})
|
2017-04-19 16:28:52 +00:00
|
|
|
generateSpecs('with sandbox', {
|
2017-03-21 13:48:36 +00:00
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-sandbox.js')
|
|
|
|
}
|
|
|
|
})
|
2017-02-13 18:46:19 +00:00
|
|
|
|
2016-04-28 16:58:04 +00:00
|
|
|
describe('.start(options)', function () {
|
2016-03-25 20:03:49 +00:00
|
|
|
it('requires that the companyName and submitURL options be specified', function () {
|
|
|
|
assert.throws(function () {
|
2016-02-17 01:39:11 +00:00
|
|
|
crashReporter.start({
|
2016-01-12 02:40:23 +00:00
|
|
|
companyName: 'Missing submitURL'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-28 16:48:13 +00:00
|
|
|
}, /submitURL is a required option to crashReporter\.start/)
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.throws(function () {
|
2016-02-17 01:39:11 +00:00
|
|
|
crashReporter.start({
|
2016-01-12 02:40:23 +00:00
|
|
|
submitURL: 'Missing companyName'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-28 16:48:13 +00:00
|
|
|
}, /companyName is a required option to crashReporter\.start/)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-07-26 17:43:57 +00:00
|
|
|
|
|
|
|
it('can be called multiple times', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes'
|
|
|
|
})
|
|
|
|
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation 2',
|
|
|
|
submitURL: 'http://127.0.0.1/more-crashes'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-11-28 23:01:38 +00:00
|
|
|
|
|
|
|
describe('.get/setUploadToServer', function () {
|
|
|
|
it('throws an error when called from the renderer process', function () {
|
|
|
|
assert.throws(() => require('electron').crashReporter.getUploadToServer())
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can be read/set from the main process', function () {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1/crashes',
|
2017-02-13 18:08:43 +00:00
|
|
|
uploadToServer: true
|
2016-11-28 23:01:38 +00:00
|
|
|
})
|
|
|
|
assert.equal(crashReporter.getUploadToServer(), true)
|
|
|
|
crashReporter.setUploadToServer(false)
|
|
|
|
assert.equal(crashReporter.getUploadToServer(), false)
|
|
|
|
} else {
|
2016-11-28 23:07:26 +00:00
|
|
|
assert.equal(crashReporter.getUploadToServer(), true)
|
2016-11-28 23:01:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-10-06 16:45:58 +00:00
|
|
|
|
|
|
|
const waitForCrashReport = () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let times = 0
|
|
|
|
const checkForReport = () => {
|
|
|
|
if (crashReporter.getLastCrashReport() != null) {
|
|
|
|
resolve()
|
|
|
|
} else if (times >= 10) {
|
|
|
|
reject(new Error('No crash report available'))
|
|
|
|
} else {
|
|
|
|
times++
|
|
|
|
setTimeout(checkForReport, 100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkForReport()
|
|
|
|
})
|
|
|
|
}
|
2017-01-24 21:49:45 +00:00
|
|
|
|
|
|
|
const startServer = ({callback, processType, done}) => {
|
|
|
|
var called = false
|
|
|
|
var server = http.createServer((req, res) => {
|
|
|
|
var form = new multiparty.Form()
|
|
|
|
form.parse(req, (error, fields) => {
|
|
|
|
if (error) throw error
|
|
|
|
if (called) return
|
|
|
|
called = true
|
|
|
|
assert.equal(fields.prod, 'Electron')
|
|
|
|
assert.equal(fields.ver, process.versions.electron)
|
|
|
|
assert.equal(fields.process_type, processType)
|
|
|
|
assert.equal(fields.platform, process.platform)
|
|
|
|
assert.equal(fields.extra1, 'extra1')
|
|
|
|
assert.equal(fields.extra2, 'extra2')
|
2017-02-13 18:46:19 +00:00
|
|
|
assert.equal(fields.extra3, undefined)
|
2017-01-24 21:49:45 +00:00
|
|
|
assert.equal(fields._productName, 'Zombies')
|
|
|
|
assert.equal(fields._companyName, 'Umbrella Corporation')
|
|
|
|
assert.equal(fields._version, app.getVersion())
|
|
|
|
|
|
|
|
const reportId = 'abc-123-def-456-abc-789-abc-123-abcd'
|
|
|
|
res.end(reportId, () => {
|
|
|
|
waitForCrashReport().then(() => {
|
|
|
|
assert.equal(crashReporter.getLastCrashReport().id, reportId)
|
|
|
|
assert.notEqual(crashReporter.getUploadedReports().length, 0)
|
|
|
|
assert.equal(crashReporter.getUploadedReports()[0].id, reportId)
|
2017-02-13 18:46:19 +00:00
|
|
|
req.socket.destroy()
|
2017-01-24 21:49:45 +00:00
|
|
|
done()
|
|
|
|
}, done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-04-19 16:48:32 +00:00
|
|
|
|
|
|
|
const activeConnections = new Set()
|
|
|
|
server.on('connection', (connection) => {
|
|
|
|
activeConnections.add(connection)
|
|
|
|
connection.once('close', () => {
|
|
|
|
activeConnections.delete(connection)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-24 21:49:45 +00:00
|
|
|
let {port} = remote.process
|
|
|
|
server.listen(port, '127.0.0.1', () => {
|
|
|
|
port = server.address().port
|
|
|
|
remote.process.port = port
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
crashReporter.start({
|
|
|
|
companyName: 'Umbrella Corporation',
|
|
|
|
submitURL: 'http://127.0.0.1:' + port
|
|
|
|
})
|
|
|
|
}
|
|
|
|
callback(port)
|
|
|
|
})
|
2017-04-19 16:48:32 +00:00
|
|
|
|
|
|
|
return function stopServer (done) {
|
|
|
|
for (const connection of activeConnections) {
|
|
|
|
connection.destroy()
|
|
|
|
}
|
|
|
|
server.close(function () {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
2017-01-24 21:49:45 +00:00
|
|
|
}
|
2017-04-19 23:32:43 +00:00
|
|
|
|
|
|
|
const stopCrashService = () => {
|
|
|
|
const {crashServicePid} = remote.process
|
|
|
|
if (crashServicePid) {
|
|
|
|
remote.process.crashServicePid = 0
|
|
|
|
try {
|
|
|
|
process.kill(crashServicePid)
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ESRCH') {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|