summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/test
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/crashreporter/test
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/crashreporter/test')
-rw-r--r--toolkit/crashreporter/test/CrashTestUtils.jsm72
-rw-r--r--toolkit/crashreporter/test/browser/.eslintrc.js7
-rw-r--r--toolkit/crashreporter/test/browser/browser.ini8
-rw-r--r--toolkit/crashreporter/test/browser/browser_aboutCrashes.js27
-rw-r--r--toolkit/crashreporter/test/browser/browser_aboutCrashesResubmit.js152
-rw-r--r--toolkit/crashreporter/test/browser/browser_bug471404.js41
-rw-r--r--toolkit/crashreporter/test/browser/browser_clearReports.js124
-rw-r--r--toolkit/crashreporter/test/browser/crashreport.sjs180
-rw-r--r--toolkit/crashreporter/test/browser/head.js139
-rw-r--r--toolkit/crashreporter/test/dumputils.cpp99
-rw-r--r--toolkit/crashreporter/test/moz.build40
-rw-r--r--toolkit/crashreporter/test/nsTestCrasher.cpp126
-rw-r--r--toolkit/crashreporter/test/unit/.eslintrc.js7
-rw-r--r--toolkit/crashreporter/test/unit/crasher_subprocess_head.js33
-rw-r--r--toolkit/crashreporter/test/unit/crasher_subprocess_tail.js15
-rw-r--r--toolkit/crashreporter/test/unit/head_crashreporter.js173
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js102
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_abort.js16
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure.js21
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure_reporting.js27
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_after_js_oom_recovered.js20
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported.js34
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported_2.js26
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_moz_crash.js16
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_oom.js19
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_purevirtual.js24
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_runtimeabort.js21
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_terminator.js40
-rw-r--r--toolkit/crashreporter/test/unit/test_crash_with_memory_report.js55
-rw-r--r--toolkit/crashreporter/test/unit/test_crashreporter.js85
-rw-r--r--toolkit/crashreporter/test/unit/test_crashreporter_appmem.js12
-rw-r--r--toolkit/crashreporter/test/unit/test_crashreporter_crash.js51
-rw-r--r--toolkit/crashreporter/test/unit/test_crashreporter_crash_profile_lock.js26
-rw-r--r--toolkit/crashreporter/test/unit/test_event_files.js56
-rw-r--r--toolkit/crashreporter/test/unit/test_oom_annotation_windows.js27
-rw-r--r--toolkit/crashreporter/test/unit/test_override_exception_handler.js12
-rw-r--r--toolkit/crashreporter/test/unit/xpcshell.ini37
-rw-r--r--toolkit/crashreporter/test/unit_ipc/.eslintrc.js7
-rw-r--r--toolkit/crashreporter/test/unit_ipc/test_content_annotation.js22
-rw-r--r--toolkit/crashreporter/test/unit_ipc/test_content_exception_time_annotation.js17
-rw-r--r--toolkit/crashreporter/test/unit_ipc/test_content_memory_list.js23
-rw-r--r--toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js23
-rw-r--r--toolkit/crashreporter/test/unit_ipc/xpcshell.ini15
43 files changed, 2077 insertions, 0 deletions
diff --git a/toolkit/crashreporter/test/CrashTestUtils.jsm b/toolkit/crashreporter/test/CrashTestUtils.jsm
new file mode 100644
index 000000000..70162ab63
--- /dev/null
+++ b/toolkit/crashreporter/test/CrashTestUtils.jsm
@@ -0,0 +1,72 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+this.EXPORTED_SYMBOLS = ["CrashTestUtils"];
+
+this.CrashTestUtils = {
+ // These will be defined using ctypes APIs below.
+ crash: null,
+ lockDir: null,
+ dumpHasStream: null,
+ dumpHasInstructionPointerMemory: null,
+
+ // Constants for crash()
+ // Keep these in sync with nsTestCrasher.cpp!
+ CRASH_INVALID_POINTER_DEREF: 0,
+ CRASH_PURE_VIRTUAL_CALL: 1,
+ CRASH_RUNTIMEABORT: 2,
+ CRASH_OOM: 3,
+ CRASH_MOZ_CRASH: 4,
+ CRASH_ABORT: 5,
+
+ // Constants for dumpHasStream()
+ // From google_breakpad/common/minidump_format.h
+ MD_THREAD_LIST_STREAM: 3,
+ MD_MEMORY_INFO_LIST_STREAM: 16
+};
+
+// Grab APIs from the testcrasher shared library
+Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource://gre/modules/ctypes.jsm");
+var dir = Services.dirsvc.get("CurWorkD", Components.interfaces.nsILocalFile);
+var file = dir.clone();
+file = file.parent;
+file.append(ctypes.libraryName("testcrasher"));
+var lib = ctypes.open(file.path);
+CrashTestUtils.crash = lib.declare("Crash",
+ ctypes.default_abi,
+ ctypes.void_t,
+ ctypes.int16_t);
+CrashTestUtils.saveAppMemory = lib.declare("SaveAppMemory",
+ ctypes.default_abi,
+ ctypes.uint64_t);
+
+CrashTestUtils.lockDir = lib.declare("LockDir",
+ ctypes.default_abi,
+ ctypes.voidptr_t, // nsILocalFile*
+ ctypes.voidptr_t); // nsISupports*
+
+
+try {
+ CrashTestUtils.TryOverrideExceptionHandler = lib.declare("TryOverrideExceptionHandler",
+ ctypes.default_abi,
+ ctypes.void_t);
+}
+catch (ex) {}
+
+CrashTestUtils.dumpHasStream = lib.declare("DumpHasStream",
+ ctypes.default_abi,
+ ctypes.bool,
+ ctypes.char.ptr,
+ ctypes.uint32_t);
+
+CrashTestUtils.dumpHasInstructionPointerMemory =
+ lib.declare("DumpHasInstructionPointerMemory",
+ ctypes.default_abi,
+ ctypes.bool,
+ ctypes.char.ptr);
+
+CrashTestUtils.dumpCheckMemory = lib.declare("DumpCheckMemory",
+ ctypes.default_abi,
+ ctypes.bool,
+ ctypes.char.ptr);
diff --git a/toolkit/crashreporter/test/browser/.eslintrc.js b/toolkit/crashreporter/test/browser/.eslintrc.js
new file mode 100644
index 000000000..c764b133d
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../testing/mochitest/browser.eslintrc.js"
+ ]
+};
diff --git a/toolkit/crashreporter/test/browser/browser.ini b/toolkit/crashreporter/test/browser/browser.ini
new file mode 100644
index 000000000..b58176571
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/browser.ini
@@ -0,0 +1,8 @@
+[DEFAULT]
+support-files =
+ head.js
+
+[browser_aboutCrashes.js]
+[browser_aboutCrashesResubmit.js]
+[browser_bug471404.js]
+[browser_clearReports.js]
diff --git a/toolkit/crashreporter/test/browser/browser_aboutCrashes.js b/toolkit/crashreporter/test/browser/browser_aboutCrashes.js
new file mode 100644
index 000000000..1293df030
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/browser_aboutCrashes.js
@@ -0,0 +1,27 @@
+add_task(function* test() {
+ let appD = make_fake_appdir();
+ let crD = appD.clone();
+ crD.append("Crash Reports");
+ let crashes = add_fake_crashes(crD, 5);
+ // sanity check
+ let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
+ .getService(Components.interfaces.nsIProperties);
+ let appDtest = dirSvc.get("UAppData", Components.interfaces.nsILocalFile);
+ ok(appD.equals(appDtest), "directory service provider registered ok");
+
+ yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:crashes" }, function (browser) {
+ info("about:crashes loaded");
+ return ContentTask.spawn(browser, crashes, function (crashes) {
+ let doc = content.document;
+ let crashlinks = doc.getElementById("submitted").querySelectorAll(".crashReport");
+ Assert.equal(crashlinks.length, crashes.length,
+ "about:crashes lists correct number of crash reports");
+ for (let i = 0; i < crashes.length; i++) {
+ Assert.equal(crashlinks[i].firstChild.textContent, crashes[i].id,
+ i + ": crash ID is correct");
+ }
+ });
+ });
+
+ cleanup_fake_appdir();
+});
diff --git a/toolkit/crashreporter/test/browser/browser_aboutCrashesResubmit.js b/toolkit/crashreporter/test/browser/browser_aboutCrashesResubmit.js
new file mode 100644
index 000000000..a911c67e8
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/browser_aboutCrashesResubmit.js
@@ -0,0 +1,152 @@
+function cleanup_and_finish() {
+ try {
+ cleanup_fake_appdir();
+ } catch (ex) {}
+ Services.prefs.clearUserPref("breakpad.reportURL");
+ BrowserTestUtils.removeTab(gBrowser.selectedTab).then(finish);
+}
+
+/*
+ * check_crash_list
+ *
+ * Check that the list of crashes displayed by about:crashes matches
+ * the list of crashes that we placed in the pending+submitted directories.
+ *
+ * NB: This function is run in the child process via ContentTask.spawn.
+ */
+function check_crash_list(crashes) {
+ let doc = content.document;
+ let crashlinks = doc.getElementsByClassName("crashReport");
+ Assert.equal(crashlinks.length, crashes.length,
+ "about:crashes lists correct number of crash reports");
+ // no point in checking this if the lists aren't the same length
+ if (crashlinks.length == crashes.length) {
+ for (let i=0; i<crashes.length; i++) {
+ Assert.equal(crashlinks[i].id, crashes[i].id, i + ": crash ID is correct");
+ if (crashes[i].pending) {
+ // we set the breakpad.reportURL pref in test()
+ Assert.equal(crashlinks[i].getAttribute("href"),
+ "http://example.com/browser/toolkit/crashreporter/about/throttling",
+ "pending URL links to the correct static page");
+ }
+ }
+ }
+}
+
+/*
+ * check_submit_pending
+ *
+ * Click on a pending crash in about:crashes, wait for it to be submitted (which
+ * should redirect us to the crash report page). Verify that the data provided
+ * by our test crash report server matches the data we submitted.
+ * Additionally, click "back" and verify that the link now points to our new
+ */
+function check_submit_pending(tab, crashes) {
+ let browser = gBrowser.getBrowserForTab(tab);
+ let SubmittedCrash = null;
+ let CrashID = null;
+ let CrashURL = null;
+ function csp_onload() {
+ // loaded the crash report page
+ ok(true, 'got submission onload');
+
+ ContentTask.spawn(browser, null, function() {
+ // grab the Crash ID here to verify later
+ let CrashID = content.location.search.split("=")[1];
+ let CrashURL = content.location.toString();
+
+ // check the JSON content vs. what we submitted
+ let result = JSON.parse(content.document.documentElement.textContent);
+ Assert.equal(result.upload_file_minidump, "MDMP", "minidump file sent properly");
+ Assert.equal(result.memory_report, "Let's pretend this is a memory report",
+ "memory report sent properly");
+ Assert.equal(+result.Throttleable, 0, "correctly sent as non-throttleable");
+ // we checked these, they're set by the submission process,
+ // so they won't be in the "extra" data.
+ delete result.upload_file_minidump;
+ delete result.memory_report;
+ delete result.Throttleable;
+
+ return { id: CrashID, url: CrashURL, result };
+ }).then(({ id, url, result }) => {
+ // Likewise, this is discarded before it gets to the server
+ delete SubmittedCrash.extra.ServerURL;
+
+ CrashID = id;
+ CrashURL = url;
+ for (let x in result) {
+ if (x in SubmittedCrash.extra)
+ is(result[x], SubmittedCrash.extra[x],
+ "submitted value for " + x + " matches expected");
+ else
+ ok(false, "property " + x + " missing from submitted data!");
+ }
+ for (let y in SubmittedCrash.extra) {
+ if (!(y in result))
+ ok(false, "property " + y + " missing from result data!");
+ }
+
+ // NB: Despite appearances, this doesn't use a CPOW.
+ BrowserTestUtils.waitForEvent(browser, "pageshow", true).then(csp_pageshow);
+
+ // now navigate back
+ browser.goBack();
+ });
+ }
+ function csp_fail() {
+ browser.removeEventListener("CrashSubmitFailed", csp_fail, true);
+ ok(false, "failed to submit crash report!");
+ cleanup_and_finish();
+ }
+ browser.addEventListener("CrashSubmitFailed", csp_fail, true);
+ BrowserTestUtils.browserLoaded(browser, false, (url) => url !== "about:crashes").then(csp_onload);
+ function csp_pageshow() {
+ ContentTask.spawn(browser, { CrashID, CrashURL }, function({ CrashID, CrashURL }) {
+ Assert.equal(content.location.href, "about:crashes", "navigated back successfully");
+ let link = content.document.getElementById(CrashID);
+ Assert.notEqual(link, null, "crash report link changed correctly");
+ if (link)
+ Assert.equal(link.href, CrashURL, "crash report link points to correct href");
+ }).then(cleanup_and_finish);
+ }
+
+ // try submitting the pending report
+ for (let crash of crashes) {
+ if (crash.pending) {
+ SubmittedCrash = crash;
+ break;
+ }
+ }
+
+ ContentTask.spawn(browser, SubmittedCrash.id, function(id) {
+ let link = content.document.getElementById(id);
+ link.click();
+ });
+}
+
+function test() {
+ waitForExplicitFinish();
+ let appD = make_fake_appdir();
+ let crD = appD.clone();
+ crD.append("Crash Reports");
+ let crashes = add_fake_crashes(crD, 1);
+ // we don't need much data here, it's not going to a real Socorro
+ crashes.push(addPendingCrashreport(crD,
+ crashes[crashes.length - 1].date + 60000,
+ {'ServerURL': 'http://example.com/browser/toolkit/crashreporter/test/browser/crashreport.sjs',
+ 'ProductName': 'Test App',
+ // test that we don't truncate
+ // at = (bug 512853)
+ 'Foo': 'ABC=XYZ'
+ }));
+ crashes.sort((a, b) => b.date - a.date);
+
+ // set this pref so we can link to our test server
+ Services.prefs.setCharPref("breakpad.reportURL",
+ "http://example.com/browser/toolkit/crashreporter/test/browser/crashreport.sjs?id=");
+
+ BrowserTestUtils.openNewForegroundTab(gBrowser, "about:crashes").then((tab) => {
+ ContentTask.spawn(tab.linkedBrowser, crashes, check_crash_list)
+ .then(() => check_submit_pending(tab, crashes));
+ });
+}
diff --git a/toolkit/crashreporter/test/browser/browser_bug471404.js b/toolkit/crashreporter/test/browser/browser_bug471404.js
new file mode 100644
index 000000000..f0eb00b71
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/browser_bug471404.js
@@ -0,0 +1,41 @@
+function check_clear_visible(browser, aVisible) {
+ return ContentTask.spawn(browser, aVisible, function (aVisible) {
+ let doc = content.document;
+ let visible = false;
+ let button = doc.getElementById("clear-reports");
+ if (button) {
+ let style = doc.defaultView.getComputedStyle(button, "");
+ if (style.display != "none" &&
+ style.visibility == "visible")
+ visible = true;
+ }
+ Assert.equal(visible, aVisible,
+ "clear reports button is " + (aVisible ? "visible" : "hidden"));
+ });
+}
+
+// each test here has a setup (run before loading about:crashes) and onload (run after about:crashes loads)
+var _tests = [{setup: null, onload: function(browser) { return check_clear_visible(browser, false); }},
+ {setup: function(crD) { return add_fake_crashes(crD, 1); },
+ onload: function(browser) { return check_clear_visible(browser, true); }}
+ ];
+
+add_task(function* test() {
+ let appD = make_fake_appdir();
+ let crD = appD.clone();
+ crD.append("Crash Reports");
+
+ yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" }, function* (browser) {
+ for (let test of _tests) {
+ // Run setup before loading about:crashes.
+ if (test.setup) {
+ yield test.setup(crD);
+ }
+
+ BrowserTestUtils.loadURI(browser, "about:crashes");
+ yield BrowserTestUtils.browserLoaded(browser).then(() => test.onload(browser));
+ }
+ });
+
+ cleanup_fake_appdir();
+});
diff --git a/toolkit/crashreporter/test/browser/browser_clearReports.js b/toolkit/crashreporter/test/browser/browser_clearReports.js
new file mode 100644
index 000000000..a7a1780a9
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/browser_clearReports.js
@@ -0,0 +1,124 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+* License, v. 2.0. If a copy of the MPL was not distributed with this file,
+* You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function clickClearReports(browser) {
+ let doc = content.document;
+
+ let button = doc.getElementById("clear-reports");
+
+ if (!button) {
+ Assert.ok(false, "Button not found");
+ return Promise.resolve();
+ }
+
+ let style = doc.defaultView.getComputedStyle(button, "");
+
+ Assert.notEqual(style.display, "none", "Clear reports button visible");
+
+ let deferred = {};
+ deferred.promise = new Promise(resolve => deferred.resolve = resolve);
+ var observer = new content.MutationObserver(function(mutations) {
+ for (let mutation of mutations) {
+ if (mutation.type == "attributes" &&
+ mutation.attributeName == "style") {
+ observer.disconnect();
+ Assert.equal(style.display, "none", "Clear reports button hidden");
+ deferred.resolve();
+ }
+ }
+ });
+ observer.observe(button, {
+ attributes: true,
+ childList: true,
+ characterData: true,
+ attributeFilter: ["style"],
+ });
+
+ button.click();
+ return deferred.promise;
+}
+
+var promptShown = false;
+
+var oldPrompt = Services.prompt;
+Services.prompt = {
+ confirm: function() {
+ promptShown = true;
+ return true;
+ },
+};
+
+registerCleanupFunction(function () {
+ Services.prompt = oldPrompt;
+});
+
+add_task(function* test() {
+ let appD = make_fake_appdir();
+ let crD = appD.clone();
+ crD.append("Crash Reports");
+
+ // Add crashes to submitted dir
+ let submitdir = crD.clone();
+ submitdir.append("submitted");
+
+ let file1 = submitdir.clone();
+ file1.append("bp-nontxt");
+ file1.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ let file2 = submitdir.clone();
+ file2.append("nonbp-file.txt");
+ file2.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ add_fake_crashes(crD, 5);
+
+ // Add crashes to pending dir
+ let pendingdir = crD.clone();
+ pendingdir.append("pending");
+
+ let crashes = add_fake_crashes(crD, 2);
+ addPendingCrashreport(crD, crashes[0].date);
+ addPendingCrashreport(crD, crashes[1].date);
+
+ // Add crashes to reports dir
+ let report1 = crD.clone();
+ report1.append("NotInstallTime777");
+ report1.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ let report2 = crD.clone();
+ report2.append("InstallTime" + Services.appinfo.appBuildID);
+ report2.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ let report3 = crD.clone();
+ report3.append("InstallTimeNew");
+ report3.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ let report4 = crD.clone();
+ report4.append("InstallTimeOld");
+ report4.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ report4.lastModifiedTime = Date.now() - 63172000000;
+
+ registerCleanupFunction(function () {
+ cleanup_fake_appdir();
+ });
+
+ yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:crashes" },
+ function* (browser) {
+ let dirs = [ submitdir, pendingdir, crD ];
+ let existing = [ file1.path, file2.path, report1.path, report2.path,
+ report3.path, submitdir.path, pendingdir.path ];
+
+ yield ContentTask.spawn(browser, null, clickClearReports);
+
+ for (let dir of dirs) {
+ let entries = dir.directoryEntries;
+ while (entries.hasMoreElements()) {
+ let file = entries.getNext().QueryInterface(Ci.nsIFile);
+ let index = existing.indexOf(file.path);
+ isnot(index, -1, file.leafName + " exists");
+
+ if (index != -1) {
+ existing.splice(index, 1);
+ }
+ }
+ }
+
+ is(existing.length, 0, "All the files that should still exist exist");
+ ok(promptShown, "Prompt shown");
+ });
+});
diff --git a/toolkit/crashreporter/test/browser/crashreport.sjs b/toolkit/crashreporter/test/browser/crashreport.sjs
new file mode 100644
index 000000000..f3bd858eb
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/crashreport.sjs
@@ -0,0 +1,180 @@
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const CC = Components.Constructor;
+
+const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+
+function parseHeaders(data, start)
+{
+ let headers = {};
+
+ while (true) {
+ let done = false;
+ let end = data.indexOf("\r\n", start);
+ if (end == -1) {
+ done = true;
+ end = data.length;
+ }
+ let line = data.substring(start, end);
+ start = end + 2;
+ if (line == "")
+ // empty line, we're done
+ break;
+
+ //XXX: this doesn't handle multi-line headers. do we care?
+ let [name, value] = line.split(':');
+ //XXX: not normalized, should probably use nsHttpHeaders or something
+ headers[name] = value.trimLeft();
+ }
+ return [headers, start];
+}
+
+function parseMultipartForm(request)
+{
+ let boundary = null;
+ // See if this is a multipart/form-data request, and if so, find the
+ // boundary string
+ if (request.hasHeader("Content-Type")) {
+ var contenttype = request.getHeader("Content-Type");
+ var bits = contenttype.split(";");
+ if (bits[0] == "multipart/form-data") {
+ for (var i = 1; i < bits.length; i++) {
+ var b = bits[i].trimLeft();
+ if (b.indexOf("boundary=") == 0) {
+ // grab everything after boundary=
+ boundary = "--" + b.substring(9);
+ break;
+ }
+ }
+ }
+ }
+ if (boundary == null)
+ return null;
+
+ let body = new BinaryInputStream(request.bodyInputStream);
+ let avail;
+ let bytes = [];
+ while ((avail = body.available()) > 0) {
+ let readBytes = body.readByteArray(avail);
+ for (let b of readBytes) {
+ bytes.push(b);
+ }
+ }
+ let data = "";
+ for (let b of bytes) {
+ data += String.fromCharCode(b);
+ }
+ let formData = {};
+ let done = false;
+ let start = 0;
+ while (true) {
+ // read first line
+ let end = data.indexOf("\r\n", start);
+ if (end == -1) {
+ done = true;
+ end = data.length;
+ }
+
+ let line = data.substring(start, end);
+ // look for closing boundary delimiter line
+ if (line == boundary + "--") {
+ break;
+ }
+
+ if (line != boundary) {
+ dump("expected boundary line but didn't find it!");
+ break;
+ }
+
+ // parse headers
+ start = end + 2;
+ let headers = null;
+ [headers, start] = parseHeaders(data, start);
+
+ // find next boundary string
+ end = data.indexOf("\r\n" + boundary, start);
+ if (end == -1) {
+ dump("couldn't find next boundary string\n");
+ break;
+ }
+
+ // read part data, stick in formData using Content-Disposition header
+ let part = data.substring(start, end);
+ start = end + 2;
+
+ if ("Content-Disposition" in headers) {
+ let bits = headers["Content-Disposition"].split(';');
+ if (bits[0] == 'form-data') {
+ for (let i = 0; i < bits.length; i++) {
+ let b = bits[i].trimLeft();
+ if (b.indexOf('name=') == 0) {
+ //TODO: handle non-ascii here?
+ let name = b.substring(6, b.length - 1);
+ //TODO: handle multiple-value properties?
+ formData[name] = part;
+ }
+ //TODO: handle filename= ?
+ //TODO: handle multipart/mixed for multi-file uploads?
+ }
+ }
+ }
+ }
+ return formData;
+}
+
+function handleRequest(request, response)
+{
+ if (request.method == "GET") {
+ let id = null;
+ for (let p of request.queryString.split('&')) {
+ let [key, value] = p.split('=');
+ if (key == 'id')
+ id = value;
+ }
+ if (id == null) {
+ response.setStatusLine(request.httpVersion, 400, "Bad Request");
+ response.write("Missing id parameter");
+ }
+ else {
+ let data = getState(id);
+ if (data == "") {
+ response.setStatusLine(request.httpVersion, 404, "Not Found");
+ response.write("Not Found");
+ }
+ else {
+ response.setHeader("Content-Type", "text/plain", false);
+ response.write(data);
+ }
+ }
+ }
+ else if (request.method == "POST") {
+ let formData = parseMultipartForm(request);
+
+ if (formData && 'upload_file_minidump' in formData) {
+ response.setHeader("Content-Type", "text/plain", false);
+
+ let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
+ .getService(Ci.nsIUUIDGenerator);
+ let uuid = uuidGenerator.generateUUID().toString();
+ // ditch the {}, add bp- prefix
+ uuid = 'bp-' + uuid.substring(1,uuid.length-2);
+
+ let d = JSON.stringify(formData);
+ //dump('saving crash report ' + uuid + ': ' + d + '\n');
+ setState(uuid, d);
+
+ response.write("CrashID=" + uuid + "\n");
+ }
+ else {
+ dump('*** crashreport.sjs: Malformed request?\n');
+ response.setStatusLine(request.httpVersion, 400, "Bad Request");
+ response.write("Missing minidump file");
+ }
+ }
+ else {
+ response.setStatusLine(request.httpVersion, 405, "Method not allowed");
+ response.write("Can't handle HTTP method " + request.method);
+ }
+}
diff --git a/toolkit/crashreporter/test/browser/head.js b/toolkit/crashreporter/test/browser/head.js
new file mode 100644
index 000000000..f35edfe38
--- /dev/null
+++ b/toolkit/crashreporter/test/browser/head.js
@@ -0,0 +1,139 @@
+function create_subdir(dir, subdirname) {
+ let subdir = dir.clone();
+ subdir.append(subdirname);
+ if (subdir.exists()) {
+ subdir.remove(true);
+ }
+ subdir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
+ return subdir;
+}
+
+// need to hold on to this to unregister for cleanup
+var _provider = null;
+
+function make_fake_appdir() {
+ // Create a directory inside the profile and register it as UAppData, so
+ // we can stick fake crash reports inside there. We put it inside the profile
+ // just because we know that will get cleaned up after the mochitest run.
+ let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
+ .getService(Ci.nsIProperties);
+ let profD = dirSvc.get("ProfD", Ci.nsILocalFile);
+ // create a subdir just to keep our files out of the way
+ let appD = create_subdir(profD, "UAppData");
+
+ let crashesDir = create_subdir(appD, "Crash Reports");
+ create_subdir(crashesDir, "pending");
+ create_subdir(crashesDir, "submitted");
+
+ _provider = {
+ getFile: function(prop, persistent) {
+ persistent.value = true;
+ if (prop == "UAppData") {
+ return appD.clone();
+ }
+ // Depending on timing we can get requests for other files.
+ // When we threw an exception here, in the world before bug 997440, this got lost
+ // because of the arbitrary JSContext being used in XPCWrappedJSClass::CallMethod.
+ // After bug 997440 this gets reported to our window and causes the tests to fail.
+ // So, we'll just dump out a message to the logs.
+ dump("WARNING: make_fake_appdir - fake nsIDirectoryServiceProvider - Unexpected getFile for: '" + prop + "'\n");
+ return null;
+ },
+ QueryInterface: function(iid) {
+ if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
+ iid.equals(Ci.nsISupports)) {
+ return this;
+ }
+ throw Components.results.NS_ERROR_NO_INTERFACE;
+ }
+ };
+ // register our new provider
+ dirSvc.QueryInterface(Ci.nsIDirectoryService)
+ .registerProvider(_provider);
+ // and undefine the old value
+ try {
+ dirSvc.undefine("UAppData");
+ } catch (ex) {} // it's ok if this fails, the value might not be cached yet
+ return appD.clone();
+}
+
+function cleanup_fake_appdir() {
+ let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
+ .getService(Ci.nsIProperties);
+ dirSvc.QueryInterface(Ci.nsIDirectoryService)
+ .unregisterProvider(_provider);
+ // undefine our value so future calls get the real value
+ try {
+ dirSvc.undefine("UAppData");
+ } catch (ex) {
+ dump("cleanup_fake_appdir: dirSvc.undefine failed: " + ex.message +"\n");
+ }
+}
+
+function add_fake_crashes(crD, count) {
+ let results = [];
+ let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
+ .getService(Ci.nsIUUIDGenerator);
+ let submitdir = crD.clone();
+ submitdir.append("submitted");
+ // create them from oldest to newest, to ensure that about:crashes
+ // displays them in the correct order
+ let date = Date.now() - count * 60000;
+ for (let i = 0; i < count; i++) {
+ let uuid = uuidGenerator.generateUUID().toString();
+ // ditch the {}
+ uuid = "bp-" + uuid.substring(1, uuid.length - 2);
+ let fn = uuid + ".txt";
+ let file = submitdir.clone();
+ file.append(fn);
+ file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+ file.lastModifiedTime = date;
+ results.push({'id': uuid, 'date': date, 'pending': false});
+
+ date += 60000;
+ }
+ // we want them sorted newest to oldest, since that's the order
+ // that about:crashes lists them in
+ results.sort((a, b) => b.date - a.date);
+ return results;
+}
+
+function writeDataToFile(file, data) {
+ var fstream = Cc["@mozilla.org/network/file-output-stream;1"]
+ .createInstance(Ci.nsIFileOutputStream);
+ // open, write, truncate
+ fstream.init(file, -1, -1, 0);
+ var os = Cc["@mozilla.org/intl/converter-output-stream;1"]
+ .createInstance(Ci.nsIConverterOutputStream);
+ os.init(fstream, "UTF-8", 0, 0x0000);
+ os.writeString(data);
+ os.close();
+ fstream.close();
+}
+
+function addPendingCrashreport(crD, date, extra) {
+ let pendingdir = crD.clone();
+ pendingdir.append("pending");
+ let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
+ .getService(Ci.nsIUUIDGenerator);
+ let uuid = uuidGenerator.generateUUID().toString();
+ // ditch the {}
+ uuid = uuid.substring(1, uuid.length - 1);
+ let dumpfile = pendingdir.clone();
+ dumpfile.append(uuid + ".dmp");
+ writeDataToFile(dumpfile, "MDMP"); // that's the start of a valid minidump, anyway
+ let extrafile = pendingdir.clone();
+ extrafile.append(uuid + ".extra");
+ let extradata = "";
+ for (let x in extra) {
+ extradata += x + "=" + extra[x] + "\n";
+ }
+ writeDataToFile(extrafile, extradata);
+ let memoryfile = pendingdir.clone();
+ memoryfile.append(uuid + ".memory.json.gz");
+ writeDataToFile(memoryfile, "Let's pretend this is a memory report");
+ dumpfile.lastModifiedTime = date;
+ extrafile.lastModifiedTime = date;
+ memoryfile.lastModifiedTime = date;
+ return {'id': uuid, 'date': date, 'pending': true, 'extra': extra};
+}
diff --git a/toolkit/crashreporter/test/dumputils.cpp b/toolkit/crashreporter/test/dumputils.cpp
new file mode 100644
index 000000000..36ee3eedf
--- /dev/null
+++ b/toolkit/crashreporter/test/dumputils.cpp
@@ -0,0 +1,99 @@
+#include <stdio.h>
+
+#include "google_breakpad/processor/minidump.h"
+#include "nscore.h"
+
+using namespace google_breakpad;
+
+// Return true if the specified minidump contains a stream of |stream_type|.
+extern "C"
+NS_EXPORT bool
+DumpHasStream(const char* dump_file, uint32_t stream_type)
+{
+ Minidump dump(dump_file);
+ if (!dump.Read())
+ return false;
+
+ uint32_t length;
+ if (!dump.SeekToStreamType(stream_type, &length) || length == 0)
+ return false;
+
+ return true;
+}
+
+// Return true if the specified minidump contains a memory region
+// that contains the instruction pointer from the exception record.
+extern "C"
+NS_EXPORT bool
+DumpHasInstructionPointerMemory(const char* dump_file)
+{
+ Minidump minidump(dump_file);
+ if (!minidump.Read())
+ return false;
+
+ MinidumpException* exception = minidump.GetException();
+ MinidumpMemoryList* memory_list = minidump.GetMemoryList();
+ if (!exception || !memory_list) {
+ return false;
+ }
+
+ MinidumpContext* context = exception->GetContext();
+ if (!context)
+ return false;
+
+ uint64_t instruction_pointer;
+ if (!context->GetInstructionPointer(&instruction_pointer)) {
+ return false;
+ }
+
+ MinidumpMemoryRegion* region =
+ memory_list->GetMemoryRegionForAddress(instruction_pointer);
+ return region != nullptr;
+}
+
+// This function tests for a very specific condition. It finds
+// an address in a file, "crash-addr", in the CWD. It checks
+// that the minidump has a memory region starting at that
+// address. The region must be 32 bytes long and contain the
+// values 0 to 31 as bytes, in ascending order.
+extern "C"
+NS_EXPORT bool
+DumpCheckMemory(const char* dump_file)
+{
+ Minidump dump(dump_file);
+ if (!dump.Read())
+ return false;
+
+ MinidumpMemoryList* memory_list = dump.GetMemoryList();
+ if (!memory_list) {
+ return false;
+ }
+
+ void *addr;
+ FILE *fp = fopen("crash-addr", "r");
+ if (!fp)
+ return false;
+ if (fscanf(fp, "%p", &addr) != 1) {
+ fclose(fp);
+ return false;
+ }
+ fclose(fp);
+
+ remove("crash-addr");
+
+ MinidumpMemoryRegion* region =
+ memory_list->GetMemoryRegionForAddress(uint64_t(addr));
+ if(!region)
+ return false;
+
+ const uint8_t* chars = region->GetMemory();
+ if (region->GetSize() != 32)
+ return false;
+
+ for (int i=0; i<32; i++) {
+ if (chars[i] != i)
+ return false;
+ }
+
+ return true;
+}
diff --git a/toolkit/crashreporter/test/moz.build b/toolkit/crashreporter/test/moz.build
new file mode 100644
index 000000000..058e6a71e
--- /dev/null
+++ b/toolkit/crashreporter/test/moz.build
@@ -0,0 +1,40 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+FINAL_TARGET = '_tests/xpcshell/toolkit/crashreporter/test'
+
+XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini', 'unit_ipc/xpcshell.ini']
+BROWSER_CHROME_MANIFESTS += ['browser/browser.ini']
+
+UNIFIED_SOURCES += [
+ '../google-breakpad/src/processor/basic_code_modules.cc',
+ '../google-breakpad/src/processor/dump_context.cc',
+ '../google-breakpad/src/processor/dump_object.cc',
+ '../google-breakpad/src/processor/logging.cc',
+ '../google-breakpad/src/processor/minidump.cc',
+ '../google-breakpad/src/processor/pathname_stripper.cc',
+ '../google-breakpad/src/processor/proc_maps_linux.cc',
+ 'dumputils.cpp',
+ 'nsTestCrasher.cpp',
+]
+
+GeckoSharedLibrary('testcrasher')
+
+DEFINES['SHARED_LIBRARY'] = '%s%s%s' % (
+ CONFIG['DLL_PREFIX'],
+ LIBRARY_NAME,
+ CONFIG['DLL_SUFFIX']
+)
+
+TEST_HARNESS_FILES.xpcshell.toolkit.crashreporter.test.unit += ['CrashTestUtils.jsm']
+TEST_HARNESS_FILES.xpcshell.toolkit.crashreporter.test.unit_ipc += ['CrashTestUtils.jsm']
+
+include('/toolkit/crashreporter/crashreporter.mozbuild')
+
+NO_PGO = True
+
+# Temporary workaround for an issue in upstream breakpad
+if CONFIG['_MSC_VER']:
+ CXXFLAGS += ['-wd4334']
diff --git a/toolkit/crashreporter/test/nsTestCrasher.cpp b/toolkit/crashreporter/test/nsTestCrasher.cpp
new file mode 100644
index 000000000..1be001160
--- /dev/null
+++ b/toolkit/crashreporter/test/nsTestCrasher.cpp
@@ -0,0 +1,126 @@
+#include "mozilla/Assertions.h"
+
+#include <stdio.h>
+
+#include "nscore.h"
+#include "nsXULAppAPI.h"
+#include "nsExceptionHandler.h"
+#include "mozilla/Unused.h"
+
+/*
+ * This pure virtual call example is from MSDN
+ */
+class A;
+
+void fcn( A* );
+
+class A
+{
+public:
+ virtual void f() = 0;
+ A() { fcn( this ); }
+};
+
+class B : A
+{
+ void f() { }
+public:
+ void use() { }
+};
+
+void fcn( A* p )
+{
+ p->f();
+}
+
+void PureVirtualCall()
+{
+ // generates a pure virtual function call
+ B b;
+ b.use(); // make sure b's actually used
+}
+
+// Keep these in sync with CrashTestUtils.jsm!
+const int16_t CRASH_INVALID_POINTER_DEREF = 0;
+const int16_t CRASH_PURE_VIRTUAL_CALL = 1;
+const int16_t CRASH_RUNTIMEABORT = 2;
+const int16_t CRASH_OOM = 3;
+const int16_t CRASH_MOZ_CRASH = 4;
+const int16_t CRASH_ABORT = 5;
+
+extern "C" NS_EXPORT
+void Crash(int16_t how)
+{
+ switch (how) {
+ case CRASH_INVALID_POINTER_DEREF: {
+ volatile int* foo = (int*)0x42;
+ *foo = 0;
+ // not reached
+ break;
+ }
+ case CRASH_PURE_VIRTUAL_CALL: {
+ PureVirtualCall();
+ // not reached
+ break;
+ }
+ case CRASH_RUNTIMEABORT: {
+ NS_RUNTIMEABORT("Intentional crash");
+ break;
+ }
+ case CRASH_OOM: {
+ mozilla::Unused << moz_xmalloc((size_t) -1);
+ mozilla::Unused << moz_xmalloc((size_t) -1);
+ mozilla::Unused << moz_xmalloc((size_t) -1);
+ break;
+ }
+ case CRASH_MOZ_CRASH: {
+ MOZ_CRASH();
+ break;
+ }
+ case CRASH_ABORT: {
+ abort();
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+extern "C" NS_EXPORT
+nsISupports* LockDir(nsIFile *directory)
+{
+ nsISupports* lockfile = nullptr;
+ XRE_LockProfileDirectory(directory, &lockfile);
+ return lockfile;
+}
+
+char testData[32];
+
+extern "C" NS_EXPORT
+uint64_t SaveAppMemory()
+{
+ for (size_t i=0; i<sizeof(testData); i++)
+ testData[i] = i;
+
+ FILE *fp = fopen("crash-addr", "w");
+ if (!fp)
+ return 0;
+ fprintf(fp, "%p\n", (void *)testData);
+ fclose(fp);
+
+ return (int64_t)testData;
+}
+
+#ifdef XP_WIN32
+static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo)
+{
+ TerminateProcess(GetCurrentProcess(), 0);
+ return 0;
+}
+
+extern "C" NS_EXPORT
+void TryOverrideExceptionHandler()
+{
+ SetUnhandledExceptionFilter(HandleException);
+}
+#endif
diff --git a/toolkit/crashreporter/test/unit/.eslintrc.js b/toolkit/crashreporter/test/unit/.eslintrc.js
new file mode 100644
index 000000000..fee088c17
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../testing/xpcshell/xpcshell.eslintrc.js"
+ ]
+};
diff --git a/toolkit/crashreporter/test/unit/crasher_subprocess_head.js b/toolkit/crashreporter/test/unit/crasher_subprocess_head.js
new file mode 100644
index 000000000..ed8e16ae4
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/crasher_subprocess_head.js
@@ -0,0 +1,33 @@
+// enable crash reporting first
+var cwd = Components.classes["@mozilla.org/file/directory_service;1"]
+ .getService(Components.interfaces.nsIProperties)
+ .get("CurWorkD", Components.interfaces.nsILocalFile);
+
+// get the temp dir
+var env = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment);
+var _tmpd = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
+_tmpd.initWithPath(env.get("XPCSHELL_TEST_TEMP_DIR"));
+
+var crashReporter =
+ Components.classes["@mozilla.org/toolkit/crash-reporter;1"]
+ .getService(Components.interfaces.nsICrashReporter);
+
+// We need to call this or crash events go in an undefined location.
+crashReporter.UpdateCrashEventsDir();
+
+// Setting the minidump path is not allowed in content processes
+var processType = Components.classes["@mozilla.org/xre/runtime;1"].
+ getService(Components.interfaces.nsIXULRuntime).processType;
+if (processType == Components.interfaces.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
+ crashReporter.minidumpPath = _tmpd;
+}
+
+var ios = Components.classes["@mozilla.org/network/io-service;1"]
+ .getService(Components.interfaces.nsIIOService);
+var protocolHandler = ios.getProtocolHandler("resource")
+ .QueryInterface(Components.interfaces.nsIResProtocolHandler);
+var curDirURI = ios.newFileURI(cwd);
+protocolHandler.setSubstitution("test", curDirURI);
+Components.utils.import("resource://test/CrashTestUtils.jsm");
+var crashType = CrashTestUtils.CRASH_INVALID_POINTER_DEREF;
+var shouldDelay = false;
diff --git a/toolkit/crashreporter/test/unit/crasher_subprocess_tail.js b/toolkit/crashreporter/test/unit/crasher_subprocess_tail.js
new file mode 100644
index 000000000..8b4dd2b79
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/crasher_subprocess_tail.js
@@ -0,0 +1,15 @@
+// Let the event loop process a bit before crashing.
+if (shouldDelay) {
+ let shouldCrashNow = false;
+ let thr = Components.classes["@mozilla.org/thread-manager;1"]
+ .getService().currentThread;
+ thr.dispatch({ run: () => { shouldCrashNow = true; } },
+ Components.interfaces.nsIThread.DISPATCH_NORMAL);
+
+ while (!shouldCrashNow) {
+ thr.processNextEvent(true);
+ }
+}
+
+// now actually crash
+CrashTestUtils.crash(crashType);
diff --git a/toolkit/crashreporter/test/unit/head_crashreporter.js b/toolkit/crashreporter/test/unit/head_crashreporter.js
new file mode 100644
index 000000000..45c491ad2
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/head_crashreporter.js
@@ -0,0 +1,173 @@
+Components.utils.import("resource://gre/modules/osfile.jsm");
+
+function getEventDir() {
+ return OS.Path.join(do_get_tempdir().path, "crash-events");
+}
+
+/*
+ * Run an xpcshell subprocess and crash it.
+ *
+ * @param setup
+ * A string of JavaScript code to execute in the subprocess
+ * before crashing. If this is a function and not a string,
+ * it will have .toSource() called on it, and turned into
+ * a call to itself. (for programmer convenience)
+ * This code will be evaluted between crasher_subprocess_head.js
+ * and crasher_subprocess_tail.js, so it will have access
+ * to everything defined in crasher_subprocess_head.js,
+ * which includes "crashReporter", a variable holding
+ * the crash reporter service.
+ *
+ * @param callback
+ * A JavaScript function to be called after the subprocess
+ * crashes. It will be passed (minidump, extra), where
+ * minidump is an nsILocalFile of the minidump file produced,
+ * and extra is an object containing the key,value pairs from
+ * the .extra file.
+ *
+ * @param canReturnZero
+ * If true, the subprocess may return with a zero exit code.
+ * Certain types of crashes may not cause the process to
+ * exit with an error.
+ */
+function do_crash(setup, callback, canReturnZero)
+{
+ // get current process filename (xpcshell)
+ let ds = Components.classes["@mozilla.org/file/directory_service;1"]
+ .getService(Components.interfaces.nsIProperties);
+ let bin = ds.get("XREExeF", Components.interfaces.nsILocalFile);
+ if (!bin.exists()) {
+ // weird, can't find xpcshell binary?
+ do_throw("Can't find xpcshell binary!");
+ }
+ // get Gre dir (GreD)
+ let greD = ds.get("GreD", Components.interfaces.nsILocalFile);
+ let headfile = do_get_file("crasher_subprocess_head.js");
+ let tailfile = do_get_file("crasher_subprocess_tail.js");
+ // run xpcshell -g GreD -f head -e "some setup code" -f tail
+ let process = Components.classes["@mozilla.org/process/util;1"]
+ .createInstance(Components.interfaces.nsIProcess);
+ process.init(bin);
+ let args = ['-g', greD.path,
+ '-f', headfile.path];
+ if (setup) {
+ if (typeof(setup) == "function")
+ // funky, but convenient
+ setup = "("+setup.toSource()+")();";
+ args.push('-e', setup);
+ }
+ args.push('-f', tailfile.path);
+
+ let env = Components.classes["@mozilla.org/process/environment;1"]
+ .getService(Components.interfaces.nsIEnvironment);
+
+ let crashD = do_get_tempdir();
+ crashD.append("crash-events");
+ if (!crashD.exists()) {
+ crashD.create(crashD.DIRECTORY_TYPE, 0o700);
+ }
+
+ env.set("CRASHES_EVENTS_DIR", crashD.path);
+
+ try {
+ process.run(true, args, args.length);
+ }
+ catch (ex) {} // on Windows we exit with a -1 status when crashing.
+ finally {
+ env.set("CRASHES_EVENTS_DIR", "");
+ }
+
+ if (!canReturnZero) {
+ // should exit with an error (should have crashed)
+ do_check_neq(process.exitValue, 0);
+ }
+
+ handleMinidump(callback);
+}
+
+function handleMinidump(callback)
+{
+ // find minidump
+ let minidump = null;
+ let en = do_get_tempdir().directoryEntries;
+ while (en.hasMoreElements()) {
+ let f = en.getNext().QueryInterface(Components.interfaces.nsILocalFile);
+ if (f.leafName.substr(-4) == ".dmp") {
+ minidump = f;
+ break;
+ }
+ }
+
+ if (minidump == null)
+ do_throw("No minidump found!");
+
+ let extrafile = minidump.clone();
+ extrafile.leafName = extrafile.leafName.slice(0, -4) + ".extra";
+
+ let memoryfile = minidump.clone();
+ memoryfile.leafName = memoryfile.leafName.slice(0, -4) + ".memory.json.gz";
+
+ // Just in case, don't let these files linger.
+ do_register_cleanup(function() {
+ if (minidump.exists())
+ minidump.remove(false);
+ if (extrafile.exists())
+ extrafile.remove(false);
+ if (memoryfile.exists())
+ memoryfile.remove(false);
+ });
+ do_check_true(extrafile.exists());
+ let extra = parseKeyValuePairsFromFile(extrafile);
+
+ if (callback)
+ callback(minidump, extra);
+
+ if (minidump.exists())
+ minidump.remove(false);
+ if (extrafile.exists())
+ extrafile.remove(false);
+ if (memoryfile.exists())
+ memoryfile.remove(false);
+}
+
+function do_content_crash(setup, callback)
+{
+ do_load_child_test_harness();
+ do_test_pending();
+
+ // Setting the minidump path won't work in the child, so we need to do
+ // that here.
+ let crashReporter =
+ Components.classes["@mozilla.org/toolkit/crash-reporter;1"]
+ .getService(Components.interfaces.nsICrashReporter);
+ crashReporter.minidumpPath = do_get_tempdir();
+
+ let headfile = do_get_file("../unit/crasher_subprocess_head.js");
+ let tailfile = do_get_file("../unit/crasher_subprocess_tail.js");
+ if (setup) {
+ if (typeof(setup) == "function")
+ // funky, but convenient
+ setup = "("+setup.toSource()+")();";
+ }
+
+ let handleCrash = function() {
+ try {
+ handleMinidump(callback);
+ } catch (x) {
+ do_report_unexpected_exception(x);
+ }
+ do_test_finished();
+ };
+
+ sendCommand("load(\"" + headfile.path.replace(/\\/g, "/") + "\");", () =>
+ sendCommand(setup, () =>
+ sendCommand("load(\"" + tailfile.path.replace(/\\/g, "/") + "\");", () =>
+ do_execute_soon(handleCrash)
+ )
+ )
+ );
+}
+
+// Import binary APIs via js-ctypes.
+Components.utils.import("resource://test/CrashTestUtils.jsm");
+Components.utils.import("resource://gre/modules/KeyValueParser.jsm");
diff --git a/toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js b/toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js
new file mode 100644
index 000000000..bff1c5700
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js
@@ -0,0 +1,102 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test that AsyncShutdown report errors correctly
+
+function setup_crash() {
+ Components.utils.import("resource://gre/modules/AsyncShutdown.jsm", this);
+ Components.utils.import("resource://gre/modules/Services.jsm", this);
+ Components.utils.import("resource://gre/modules/Promise.jsm", this);
+
+ Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
+ Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 10);
+
+ let TOPIC = "testing-async-shutdown-crash";
+ let phase = AsyncShutdown._getPhase(TOPIC);
+ phase.addBlocker("A blocker that is never satisfied", function() {
+ dump("Installing blocker\n");
+ let deferred = Promise.defer();
+ return deferred.promise;
+ });
+
+ Services.obs.notifyObservers(null, TOPIC, null);
+ dump(new Error().stack + "\n");
+ dump("Waiting for crash\n");
+}
+
+function after_crash(mdump, extra) {
+ do_print("after crash: " + extra.AsyncShutdownTimeout);
+ let info = JSON.parse(extra.AsyncShutdownTimeout);
+ Assert.equal(info.phase, "testing-async-shutdown-crash");
+ Assert.equal(info.conditions[0].name, "A blocker that is never satisfied");
+ // This test spawns subprocesses by using argument "-e" of xpcshell, so
+ // this is the filename known to xpcshell.
+ Assert.equal(info.conditions[0].filename, "-e");
+}
+
+// Test that AsyncShutdown + OS.File reports errors correctly, in a case in which
+// the latest operation succeeded
+
+function setup_osfile_crash_noerror() {
+ Components.utils.import("resource://gre/modules/Services.jsm", this);
+ Components.utils.import("resource://gre/modules/osfile.jsm", this);
+ Components.utils.import("resource://gre/modules/Promise.jsm", this);
+
+ Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
+ Services.prefs.setBoolPref("toolkit.osfile.native", false);
+
+ OS.File.profileBeforeChange.addBlocker("Adding a blocker that will never be resolved", () => Promise.defer().promise);
+ OS.File.getCurrentDirectory();
+
+ Services.obs.notifyObservers(null, "profile-before-change", null);
+ dump("Waiting for crash\n");
+}
+
+function after_osfile_crash_noerror(mdump, extra) {
+ do_print("after OS.File crash: " + extra.AsyncShutdownTimeout);
+ let info = JSON.parse(extra.AsyncShutdownTimeout);
+ let state = info.conditions[0].state;
+ do_print("Keys: " + Object.keys(state).join(", "));
+ do_check_eq(info.phase, "profile-before-change");
+ do_check_true(state.launched);
+ do_check_false(state.shutdown);
+ do_check_true(state.worker);
+ do_check_true(!!state.latestSent);
+ do_check_eq(state.latestSent[1], "getCurrentDirectory");
+}
+
+// Test that AsyncShutdown + OS.File reports errors correctly, in a case in which
+// the latest operation failed
+
+function setup_osfile_crash_exn() {
+ Components.utils.import("resource://gre/modules/Services.jsm", this);
+ Components.utils.import("resource://gre/modules/osfile.jsm", this);
+ Components.utils.import("resource://gre/modules/Promise.jsm", this);
+
+ Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
+ Services.prefs.setBoolPref("toolkit.osfile.native", false);
+
+ OS.File.profileBeforeChange.addBlocker("Adding a blocker that will never be resolved", () => Promise.defer().promise);
+ OS.File.read("I do not exist");
+
+ Services.obs.notifyObservers(null, "profile-before-change", null);
+ dump("Waiting for crash\n");
+}
+
+function after_osfile_crash_exn(mdump, extra) {
+ do_print("after OS.File crash: " + extra.AsyncShutdownTimeout);
+ let info = JSON.parse(extra.AsyncShutdownTimeout);
+ let state = info.conditions[0].state;
+ do_print("Keys: " + Object.keys(state).join(", "));
+ do_check_eq(info.phase, "profile-before-change");
+ do_check_false(state.shutdown);
+ do_check_true(state.worker);
+ do_check_true(!!state.latestSent);
+ do_check_eq(state.latestSent[1], "read");
+}
+
+function run_test() {
+ do_crash(setup_crash, after_crash);
+ do_crash(setup_osfile_crash_noerror, after_osfile_crash_noerror);
+ do_crash(setup_osfile_crash_exn, after_osfile_crash_exn);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_abort.js b/toolkit/crashreporter/test/unit/test_crash_abort.js
new file mode 100644
index 000000000..67008346f
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_abort.js
@@ -0,0 +1,16 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test()
+{
+ // Try crashing with an abort().
+ do_crash(function() {
+ crashType = CrashTestUtils.CRASH_ABORT;
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ },
+ // process will exit with a zero exit status
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure.js b/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure.js
new file mode 100644
index 000000000..084730cba
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure.js
@@ -0,0 +1,21 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_after_js_large_allocation_failure.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+ Components.utils.getJSTestingFunctions().reportLargeAllocationFailure();
+ Components.utils.forceGC();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+ do_check_false("JSOutOfMemory" in extra);
+ do_check_eq(extra.JSLargeAllocationFailure, "Recovered");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure_reporting.js b/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure_reporting.js
new file mode 100644
index 000000000..42996ced1
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_after_js_large_allocation_failure_reporting.js
@@ -0,0 +1,27 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_after_js_oom_reporting.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+
+ function crashWhileReporting() {
+ CrashTestUtils.crash(crashType);
+ }
+
+ var observerService = Components.classes["@mozilla.org/observer-service;1"]
+ .getService(Components.interfaces.nsIObserverService);
+ observerService.addObserver(crashWhileReporting, "memory-pressure", false);
+ Components.utils.getJSTestingFunctions().reportLargeAllocationFailure();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+ do_check_eq(extra.JSLargeAllocationFailure, "Reporting");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_after_js_oom_recovered.js b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_recovered.js
new file mode 100644
index 000000000..fd1ca1259
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_recovered.js
@@ -0,0 +1,20 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_after_js_oom_recovered.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+ Components.utils.getJSTestingFunctions().reportOutOfMemory();
+ Components.utils.forceGC();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+ do_check_eq(extra.JSOutOfMemory, "Recovered");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported.js b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported.js
new file mode 100644
index 000000000..4bed8e2fd
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported.js
@@ -0,0 +1,34 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_after_js_oom_reported.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+
+ // GC now to avoid having it happen randomly later, which would make the
+ // test bogusly fail. See comment below.
+ Components.utils.forceGC();
+
+ Components.utils.getJSTestingFunctions().reportOutOfMemory();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+
+ // The JSOutOfMemory field is absent if the JS engine never reported OOM,
+ // "Reported" if it did, and "Recovered" if it reported OOM but
+ // subsequently completed a full GC cycle. Since this test calls
+ // reportOutOfMemory() and then crashes, we expect "Reported".
+ //
+ // Theoretically, GC can happen any time, so it is just possible that
+ // this property could be "Recovered" even if the implementation is
+ // correct. More likely, though, that indicates a bug, so only accept
+ // "Reported".
+ do_check_eq(extra.JSOutOfMemory, "Reported");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported_2.js b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported_2.js
new file mode 100644
index 000000000..5c038c822
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_after_js_oom_reported_2.js
@@ -0,0 +1,26 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_after_js_oom_reported_2.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+ Components.utils.getJSTestingFunctions().reportOutOfMemory();
+ Components.utils.forceGC(); // recover from first OOM
+ Components.utils.getJSTestingFunctions().reportOutOfMemory();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+
+ // Technically, GC can happen at any time, but it would be really
+ // peculiar for it to happen again heuristically right after a GC was
+ // forced. If extra.JSOutOfMemory is "Recovered" here, that's most
+ // likely a bug in the error reporting machinery.
+ do_check_eq(extra.JSOutOfMemory, "Reported");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_moz_crash.js b/toolkit/crashreporter/test/unit/test_crash_moz_crash.js
new file mode 100644
index 000000000..4e2b043ad
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_moz_crash.js
@@ -0,0 +1,16 @@
+function run_test()
+{
+ // Try crashing with a runtime abort
+ do_crash(function() {
+ crashType = CrashTestUtils.CRASH_MOZ_CRASH;
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ do_check_false("OOMAllocationSize" in extra);
+ do_check_false("JSOutOfMemory" in extra);
+ do_check_false("JSLargeAllocationFailure" in extra);
+ },
+ // process will exit with a zero exit status
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_oom.js b/toolkit/crashreporter/test/unit/test_crash_oom.js
new file mode 100644
index 000000000..559688841
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_oom.js
@@ -0,0 +1,19 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_oom.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_OOM;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+ do_check_true("OOMAllocationSize" in extra);
+ do_check_true(Number(extra.OOMAllocationSize) > 0);
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_purevirtual.js b/toolkit/crashreporter/test/unit/test_crash_purevirtual.js
new file mode 100644
index 000000000..6746aca29
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_purevirtual.js
@@ -0,0 +1,24 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_purevirtual.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ var isOSX = ("nsILocalFileMac" in Components.interfaces);
+ if (isOSX) {
+ dump("INFO | test_crash_purevirtual.js | TODO: purecalls not caught on OS X\n");
+ return;
+ }
+
+ // Try crashing with a pure virtual call
+ do_crash(function() {
+ crashType = CrashTestUtils.CRASH_PURE_VIRTUAL_CALL;
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ },
+ // process will exit with a zero exit status
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_runtimeabort.js b/toolkit/crashreporter/test/unit/test_crash_runtimeabort.js
new file mode 100644
index 000000000..ffb076cec
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_runtimeabort.js
@@ -0,0 +1,21 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_runtimeabort.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // Try crashing with a runtime abort
+ do_crash(function() {
+ crashType = CrashTestUtils.CRASH_RUNTIMEABORT;
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ do_check_true(/xpcom_runtime_abort/.test(extra.Notes));
+ do_check_false("OOMAllocationSize" in extra);
+ do_check_true(/Intentional crash/.test(extra.AbortMessage));
+ },
+ // process will exit with a zero exit status
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_terminator.js b/toolkit/crashreporter/test/unit/test_crash_terminator.js
new file mode 100644
index 000000000..291bbc92f
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_terminator.js
@@ -0,0 +1,40 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// Test that the Shutdown Terminator report errors correctly
+
+function setup_crash() {
+ Components.utils.import("resource://gre/modules/Services.jsm");
+
+ Services.prefs.setBoolPref("toolkit.terminator.testing", true);
+ Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 10);
+
+ // Initialize the terminator
+ // (normally, this is done through the manifest file, but xpcshell
+ // doesn't take them into account).
+ let terminator = Components.classes["@mozilla.org/toolkit/shutdown-terminator;1"].
+ createInstance(Components.interfaces.nsIObserver);
+ terminator.observe(null, "profile-after-change", null);
+
+ // Inform the terminator that shutdown has started
+ // Pick an arbitrary notification
+ terminator.observe(null, "xpcom-will-shutdown", null);
+ terminator.observe(null, "profile-before-change", null);
+
+ dump("Waiting (actively) for the crash\n");
+ while (true) {
+ Services.tm.currentThread.processNextEvent(true);
+ }
+}
+
+
+function after_crash(mdump, extra) {
+ do_print("Crash signature: " + JSON.stringify(extra, null, "\t"));
+ Assert.equal(extra.ShutdownProgress, "profile-before-change");
+}
+
+function run_test() {
+ do_crash(setup_crash, after_crash);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crash_with_memory_report.js b/toolkit/crashreporter/test/unit/test_crash_with_memory_report.js
new file mode 100644
index 000000000..d08b52206
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crash_with_memory_report.js
@@ -0,0 +1,55 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_oom.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // This was shamelessly copied and stripped down from do_get_profile() in
+ // head.js so that nsICrashReporter::saveMemoryReport can use a profile
+ // within the crasher subprocess.
+
+ do_crash(
+ function() {
+ // Delay crashing so that the memory report has time to complete.
+ shouldDelay = true;
+
+ let Cc = Components.classes;
+ let Ci = Components.interfaces;
+
+ let env = Cc["@mozilla.org/process/environment;1"]
+ .getService(Ci.nsIEnvironment);
+ let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
+ let file = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ file.initWithPath(profd);
+
+ let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
+ .getService(Ci.nsIProperties);
+ let provider = {
+ getFile: function(prop, persistent) {
+ persistent.value = true;
+ if (prop == "ProfD" || prop == "ProfLD" || prop == "ProfDS" ||
+ prop == "ProfLDS" || prop == "TmpD") {
+ return file.clone();
+ }
+ throw Components.results.NS_ERROR_FAILURE;
+ },
+ QueryInterface: function(iid) {
+ if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
+ iid.equals(Ci.nsISupports)) {
+ return this;
+ }
+ throw Components.results.NS_ERROR_NO_INTERFACE;
+ }
+ };
+ dirSvc.QueryInterface(Ci.nsIDirectoryService)
+ .registerProvider(provider);
+
+ crashReporter.saveMemoryReport();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.ContainsMemoryReport, "1");
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crashreporter.js b/toolkit/crashreporter/test/unit/test_crashreporter.js
new file mode 100644
index 000000000..f86152234
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crashreporter.js
@@ -0,0 +1,85 @@
+function run_test()
+{
+ dump("INFO | test_crashreporter.js | Get crashreporter service.\n");
+ var cr = Components.classes["@mozilla.org/toolkit/crash-reporter;1"]
+ .getService(Components.interfaces.nsICrashReporter);
+ do_check_neq(cr, null);
+
+ do_check_true(cr.enabled);
+
+ try {
+ let su = cr.serverURL;
+ do_throw("Getting serverURL when not set should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_FAILURE);
+ }
+
+ // check setting/getting serverURL
+ var ios = Components.classes["@mozilla.org/network/io-service;1"]
+ .getService(Components.interfaces.nsIIOService);
+
+ // try it with two different URLs, just for kicks
+ var testspecs = ["http://example.com/submit",
+ "https://example.org/anothersubmit"];
+ for (var i = 0; i < testspecs.length; ++i) {
+ cr.serverURL = ios.newURI(testspecs[i], null, null);
+ do_check_eq(cr.serverURL.spec, testspecs[i]);
+ }
+
+ // should not allow setting non-http/https URLs
+ try {
+ cr.serverURL = ios.newURI("ftp://example.com/submit", null, null);
+ do_throw("Setting serverURL to a non-http(s) URL should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
+ }
+
+ // check getting/setting minidumpPath
+ // it should be $TEMP by default, but I'm not sure if we can exactly test that
+ // this will at least test that it doesn't throw
+ do_check_neq(cr.minidumpPath.path, "");
+ var cwd = do_get_cwd();
+ cr.minidumpPath = cwd;
+ do_check_eq(cr.minidumpPath.path, cwd.path);
+
+ try {
+ cr.annotateCrashReport("equal=equal", "");
+ do_throw("Calling annotateCrashReport() with an '=' in key should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
+ }
+ try {
+ cr.annotateCrashReport("new\nline", "");
+ do_throw("Calling annotateCrashReport() with a '\\n' in key should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
+ }
+ try {
+ cr.annotateCrashReport("", "da\0ta");
+ do_throw("Calling annotateCrashReport() with a '\\0' in data should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
+ }
+ cr.annotateCrashReport("testKey", "testData1");
+ // Replace previous data.
+ cr.annotateCrashReport("testKey", "testData2");
+
+ try {
+ cr.appendAppNotesToCrashReport("da\0ta");
+ do_throw("Calling appendAppNotesToCrashReport() with a '\\0' in data should have thrown!");
+ }
+ catch (ex) {
+ do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
+ }
+ cr.appendAppNotesToCrashReport("additional testData3");
+ // Add more data.
+ cr.appendAppNotesToCrashReport("additional testData4");
+
+ cr.minidumpPath = cwd;
+ do_check_eq(cr.minidumpPath.path, cwd.path);
+}
diff --git a/toolkit/crashreporter/test/unit/test_crashreporter_appmem.js b/toolkit/crashreporter/test/unit/test_crashreporter_appmem.js
new file mode 100644
index 000000000..29d96fe87
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crashreporter_appmem.js
@@ -0,0 +1,12 @@
+function run_test()
+{
+ do_crash(function() {
+ appAddr = CrashTestUtils.saveAppMemory();
+ crashReporter.registerAppMemory(appAddr, 32);
+ },
+ function(mdump, extra) {
+ do_check_true(mdump.exists());
+ do_check_true(mdump.fileSize > 0);
+ do_check_true(CrashTestUtils.dumpCheckMemory(mdump.path));
+ });
+}
diff --git a/toolkit/crashreporter/test/unit/test_crashreporter_crash.js b/toolkit/crashreporter/test/unit/test_crashreporter_crash.js
new file mode 100644
index 000000000..bfaaef90c
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crashreporter_crash.js
@@ -0,0 +1,51 @@
+function run_test()
+{
+ var is_win7_or_newer = false;
+ var is_windows = false;
+ var ph = Components.classes["@mozilla.org/network/protocol;1?name=http"]
+ .getService(Components.interfaces.nsIHttpProtocolHandler);
+ var match = ph.userAgent.match(/Windows NT (\d+).(\d+)/);
+ if (match) {
+ is_windows = true;
+ }
+ if (match && (parseInt(match[1]) > 6 ||
+ parseInt(match[1]) == 6 && parseInt(match[2]) >= 1)) {
+ is_win7_or_newer = true;
+ }
+
+ // try a basic crash
+ do_crash(null, function(mdump, extra) {
+ do_check_true(mdump.exists());
+ do_check_true(mdump.fileSize > 0);
+ do_check_true('StartupTime' in extra);
+ do_check_true('CrashTime' in extra);
+ do_check_true(CrashTestUtils.dumpHasStream(mdump.path, CrashTestUtils.MD_THREAD_LIST_STREAM));
+ do_check_true(CrashTestUtils.dumpHasInstructionPointerMemory(mdump.path));
+ if (is_windows) {
+ ['SystemMemoryUsePercentage', 'TotalVirtualMemory', 'AvailableVirtualMemory',
+ 'AvailablePageFile', 'AvailablePhysicalMemory'].forEach(function(prop) {
+ do_check_true(/^\d+$/.test(extra[prop].toString()));
+ });
+ }
+ if (is_win7_or_newer)
+ do_check_true(CrashTestUtils.dumpHasStream(mdump.path, CrashTestUtils.MD_MEMORY_INFO_LIST_STREAM));
+ });
+
+ // check setting some basic data
+ do_crash(function() {
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ crashReporter.annotateCrashReport("\u2665", "\u{1F4A9}");
+ crashReporter.appendAppNotesToCrashReport("Junk");
+ crashReporter.appendAppNotesToCrashReport("MoreJunk");
+ // TelemetrySession setup will trigger the session annotation
+ let scope = {};
+ Components.utils.import("resource://gre/modules/TelemetryController.jsm", scope);
+ scope.TelemetryController.testSetup();
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ do_check_eq(extra["\u2665"], "\u{1F4A9}");
+ do_check_eq(extra.Notes, "JunkMoreJunk");
+ do_check_true(!("TelemetrySessionId" in extra));
+ });
+}
diff --git a/toolkit/crashreporter/test/unit/test_crashreporter_crash_profile_lock.js b/toolkit/crashreporter/test/unit/test_crashreporter_crash_profile_lock.js
new file mode 100644
index 000000000..78492ea5d
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_crashreporter_crash_profile_lock.js
@@ -0,0 +1,26 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crashreporter.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // lock a profile directory, crash, and ensure that
+ // the profile lock signal handler doesn't interfere with
+ // writing a minidump
+ do_crash(function() {
+ let env = Components.classes["@mozilla.org/process/environment;1"]
+ .getService(Components.interfaces.nsIEnvironment);
+ // the python harness sets this in the environment for us
+ let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
+ let dir = Components.classes["@mozilla.org/file/local;1"]
+ .createInstance(Components.interfaces.nsILocalFile);
+ dir.initWithPath(profd);
+ let lock = CrashTestUtils.lockDir(dir);
+ // when we crash, the lock file should be cleaned up
+ },
+ function(mdump, extra) {
+ // if we got here, we have a minidump, so that's all we wanted
+ do_check_true(true);
+ });
+}
diff --git a/toolkit/crashreporter/test/unit/test_event_files.js b/toolkit/crashreporter/test/unit/test_event_files.js
new file mode 100644
index 000000000..d57978e23
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_event_files.js
@@ -0,0 +1,56 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+var {utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/Promise.jsm", this);
+Cu.import("resource://gre/modules/Services.jsm", this);
+Cu.import("resource://testing-common/AppData.jsm", this);
+
+function run_test() {
+ run_next_test();
+}
+
+add_task(function* test_setup() {
+ do_get_profile();
+ yield makeFakeAppDir();
+});
+
+add_task(function* test_main_process_crash() {
+ let cm = Services.crashmanager;
+ Assert.ok(cm, "CrashManager available.");
+
+ let basename;
+ let deferred = Promise.defer();
+ do_crash(
+ function() {
+ // TelemetrySession setup will trigger the session annotation
+ let scope = {};
+ Components.utils.import("resource://gre/modules/TelemetryController.jsm", scope);
+ scope.TelemetryController.testSetup();
+ crashType = CrashTestUtils.CRASH_RUNTIMEABORT;
+ crashReporter.annotateCrashReport("ShutdownProgress", "event-test");
+ },
+ (minidump, extra) => {
+ basename = minidump.leafName;
+ cm._eventsDirs = [getEventDir()];
+ cm.aggregateEventsFiles().then(deferred.resolve, deferred.reject);
+ },
+ true);
+
+ let count = yield deferred.promise;
+ Assert.equal(count, 1, "A single crash event file was seen.");
+ let crashes = yield cm.getCrashes();
+ Assert.equal(crashes.length, 1);
+ let crash = crashes[0];
+ Assert.ok(crash.isOfType(cm.PROCESS_TYPE_MAIN, cm.CRASH_TYPE_CRASH));
+ Assert.equal(crash.id + ".dmp", basename, "ID recorded properly");
+ Assert.equal(crash.metadata.ShutdownProgress, "event-test");
+ Assert.ok("TelemetrySessionId" in crash.metadata);
+ Assert.ok("UptimeTS" in crash.metadata);
+ Assert.ok(/^[0-9a-f]{8}\-([0-9a-f]{4}\-){3}[0-9a-f]{12}$/.test(crash.metadata.TelemetrySessionId));
+ Assert.ok("CrashTime" in crash.metadata);
+ Assert.ok(/^\d+$/.test(crash.metadata.CrashTime));
+});
diff --git a/toolkit/crashreporter/test/unit/test_oom_annotation_windows.js b/toolkit/crashreporter/test/unit/test_oom_annotation_windows.js
new file mode 100644
index 000000000..4e103dee5
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_oom_annotation_windows.js
@@ -0,0 +1,27 @@
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_crash_oom.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ do_crash(
+ function() {
+ crashType = CrashTestUtils.CRASH_OOM;
+ crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestingOOMCrash, "Yes");
+ do_check_true("OOMAllocationSize" in extra);
+ do_check_true(Number(extra.OOMAllocationSize) > 0);
+ do_check_true("SystemMemoryUsePercentage" in extra);
+ do_check_true("TotalVirtualMemory" in extra);
+ do_check_true("AvailableVirtualMemory" in extra);
+ do_check_true("TotalPageFile" in extra);
+ do_check_true("AvailablePageFile" in extra);
+ do_check_true("TotalPhysicalMemory" in extra);
+ do_check_true("AvailablePhysicalMemory" in extra);
+
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/test_override_exception_handler.js b/toolkit/crashreporter/test/unit/test_override_exception_handler.js
new file mode 100644
index 000000000..7e7787d80
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_override_exception_handler.js
@@ -0,0 +1,12 @@
+function run_test()
+{
+ // Ensure that attempting to override the exception handler doesn't cause
+ // us to lose our exception handler.
+ do_crash(
+ function() {
+ CrashTestUtils.TryOverrideExceptionHandler();
+ },
+ function(mdump, extra) {
+ },
+ true);
+}
diff --git a/toolkit/crashreporter/test/unit/xpcshell.ini b/toolkit/crashreporter/test/unit/xpcshell.ini
new file mode 100644
index 000000000..ca9778086
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/xpcshell.ini
@@ -0,0 +1,37 @@
+[DEFAULT]
+head = head_crashreporter.js
+tail =
+skip-if = toolkit == 'android'
+support-files =
+ crasher_subprocess_head.js
+ crasher_subprocess_tail.js
+
+[test_crash_moz_crash.js]
+[test_crash_purevirtual.js]
+[test_crash_runtimeabort.js]
+[test_crash_after_js_oom_reported.js]
+[test_crash_after_js_oom_recovered.js]
+[test_crash_after_js_oom_reported_2.js]
+[test_crash_after_js_large_allocation_failure.js]
+[test_crash_after_js_large_allocation_failure_reporting.js]
+[test_crash_oom.js]
+[test_oom_annotation_windows.js]
+skip-if = os != 'win'
+
+[test_crash_abort.js]
+skip-if = os == 'win'
+
+[test_crash_with_memory_report.js]
+[test_crashreporter.js]
+[test_crashreporter_crash.js]
+[test_crashreporter_crash_profile_lock.js]
+[test_override_exception_handler.js]
+skip-if = os != 'win'
+
+[test_crashreporter_appmem.js]
+# we need to skip this due to bug 838613
+skip-if = (os != 'win' && os != 'linux') || (os=='linux' && bits==32)
+
+[test_crash_AsyncShutdown.js]
+[test_event_files.js]
+[test_crash_terminator.js]
diff --git a/toolkit/crashreporter/test/unit_ipc/.eslintrc.js b/toolkit/crashreporter/test/unit_ipc/.eslintrc.js
new file mode 100644
index 000000000..fee088c17
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../testing/xpcshell/xpcshell.eslintrc.js"
+ ]
+};
diff --git a/toolkit/crashreporter/test/unit_ipc/test_content_annotation.js b/toolkit/crashreporter/test/unit_ipc/test_content_annotation.js
new file mode 100644
index 000000000..8a5c962e3
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/test_content_annotation.js
@@ -0,0 +1,22 @@
+load("../unit/head_crashreporter.js");
+
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_content_annotation.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // Try crashing with a runtime abort
+ do_content_crash(function() {
+ crashType = CrashTestUtils.CRASH_RUNTIMEABORT;
+ crashReporter.annotateCrashReport("TestKey", "TestValue");
+ crashReporter.appendAppNotesToCrashReport("!!!foo!!!");
+ },
+ function(mdump, extra) {
+ do_check_eq(extra.TestKey, "TestValue");
+ do_check_true('StartupTime' in extra);
+ do_check_true('ProcessType' in extra);
+ do_check_neq(extra.Notes.indexOf("!!!foo!!!"), -1);
+ });
+}
diff --git a/toolkit/crashreporter/test/unit_ipc/test_content_exception_time_annotation.js b/toolkit/crashreporter/test/unit_ipc/test_content_exception_time_annotation.js
new file mode 100644
index 000000000..810f683b2
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/test_content_exception_time_annotation.js
@@ -0,0 +1,17 @@
+load("../unit/head_crashreporter.js");
+
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_content_annotation.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // Try crashing with an OOM
+ do_content_crash(function() {
+ crashType = CrashTestUtils.CRASH_OOM;
+ },
+ function(mdump, extra) {
+ do_check_true('OOMAllocationSize' in extra);
+ });
+}
diff --git a/toolkit/crashreporter/test/unit_ipc/test_content_memory_list.js b/toolkit/crashreporter/test/unit_ipc/test_content_memory_list.js
new file mode 100644
index 000000000..906f57e83
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/test_content_memory_list.js
@@ -0,0 +1,23 @@
+// Any copyright is dedicated to the Public Domain.
+// http://creativecommons.org/publicdomain/zero/1.0/
+
+load("../unit/head_crashreporter.js");
+
+function run_test()
+{
+ var is_win7_or_newer = false;
+ var ph = Components.classes["@mozilla.org/network/protocol;1?name=http"]
+ .getService(Components.interfaces.nsIHttpProtocolHandler);
+ var match = ph.userAgent.match(/Windows NT (\d+).(\d+)/);
+ if (match && (parseInt(match[1]) > 6 ||
+ parseInt(match[1]) == 6 && parseInt(match[2]) >= 1)) {
+ is_win7_or_newer = true;
+ }
+
+ do_content_crash(null, function(mdump, extra) {
+ do_check_true(mdump.exists());
+ do_check_true(mdump.fileSize > 0);
+ if (is_win7_or_newer)
+ do_check_true(CrashTestUtils.dumpHasStream(mdump.path, CrashTestUtils.MD_MEMORY_INFO_LIST_STREAM));
+ });
+}
diff --git a/toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js b/toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js
new file mode 100644
index 000000000..9853696da
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js
@@ -0,0 +1,23 @@
+load("../unit/head_crashreporter.js");
+
+function run_test()
+{
+ if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+ dump("INFO | test_content_annotation.js | Can't test crashreporter in a non-libxul build.\n");
+ return;
+ }
+
+ // Try crashing with an OOM
+ do_content_crash(function() {
+ crashType = CrashTestUtils.CRASH_OOM;
+ },
+ function(mdump, extra) {
+ do_check_true("SystemMemoryUsePercentage" in extra);
+ do_check_true("TotalVirtualMemory" in extra);
+ do_check_true("AvailableVirtualMemory" in extra);
+ do_check_true("TotalPageFile" in extra);
+ do_check_true("AvailablePageFile" in extra);
+ do_check_true("TotalPhysicalMemory" in extra);
+ do_check_true("AvailablePhysicalMemory" in extra);
+ });
+}
diff --git a/toolkit/crashreporter/test/unit_ipc/xpcshell.ini b/toolkit/crashreporter/test/unit_ipc/xpcshell.ini
new file mode 100644
index 000000000..33fcc1a4c
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/xpcshell.ini
@@ -0,0 +1,15 @@
+[DEFAULT]
+head =
+tail =
+skip-if = toolkit == 'android'
+support-files =
+ !/toolkit/crashreporter/test/unit/crasher_subprocess_head.js
+ !/toolkit/crashreporter/test/unit/crasher_subprocess_tail.js
+ !/toolkit/crashreporter/test/unit/head_crashreporter.js
+
+[test_content_annotation.js]
+[test_content_exception_time_annotation.js]
+[test_content_oom_annotation_windows.js]
+skip-if = os != 'win'
+[test_content_memory_list.js]
+skip-if = os != 'win'