diff options
Diffstat (limited to 'toolkit/crashreporter/test/unit')
25 files changed, 955 insertions, 0 deletions
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] |