toolkit/components/crashes/CrashManagerTest.jsm

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * This file provides common and shared functionality to facilitate
michael@0 7 * testing of the Crashes component (CrashManager.jsm).
michael@0 8 */
michael@0 9
michael@0 10 "use strict";
michael@0 11
michael@0 12 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 13
michael@0 14 this.EXPORTED_SYMBOLS = [
michael@0 15 "configureLogging",
michael@0 16 "getManager",
michael@0 17 "sleep",
michael@0 18 "TestingCrashManager",
michael@0 19 ];
michael@0 20
michael@0 21 Cu.import("resource://gre/modules/CrashManager.jsm", this);
michael@0 22 Cu.import("resource://gre/modules/Log.jsm", this);
michael@0 23 Cu.import("resource://gre/modules/osfile.jsm", this);
michael@0 24 Cu.import("resource://gre/modules/Promise.jsm", this);
michael@0 25 Cu.import("resource://gre/modules/Task.jsm", this);
michael@0 26 Cu.import("resource://gre/modules/Timer.jsm", this);
michael@0 27
michael@0 28 let loggingConfigured = false;
michael@0 29
michael@0 30 this.configureLogging = function () {
michael@0 31 if (loggingConfigured) {
michael@0 32 return;
michael@0 33 }
michael@0 34
michael@0 35 let log = Log.repository.getLogger("Crashes.CrashManager");
michael@0 36 log.level = Log.Level.All;
michael@0 37 let appender = new Log.DumpAppender();
michael@0 38 appender.level = Log.Level.All;
michael@0 39 log.addAppender(appender);
michael@0 40 loggingConfigured = true;
michael@0 41 };
michael@0 42
michael@0 43 this.sleep = function (wait) {
michael@0 44 let deferred = Promise.defer();
michael@0 45
michael@0 46 setTimeout(() => {
michael@0 47 deferred.resolve();
michael@0 48 }, wait);
michael@0 49
michael@0 50 return deferred.promise;
michael@0 51 };
michael@0 52
michael@0 53 this.TestingCrashManager = function (options) {
michael@0 54 CrashManager.call(this, options);
michael@0 55 }
michael@0 56
michael@0 57 this.TestingCrashManager.prototype = {
michael@0 58 __proto__: CrashManager.prototype,
michael@0 59
michael@0 60 createDummyDump: function (submitted=false, date=new Date(), hr=false) {
michael@0 61 let uuid = Cc["@mozilla.org/uuid-generator;1"]
michael@0 62 .getService(Ci.nsIUUIDGenerator)
michael@0 63 .generateUUID()
michael@0 64 .toString();
michael@0 65 uuid = uuid.substring(1, uuid.length - 1);
michael@0 66
michael@0 67 let path;
michael@0 68 let mode;
michael@0 69 if (submitted) {
michael@0 70 if (hr) {
michael@0 71 path = OS.Path.join(this._submittedDumpsDir, "bp-hr-" + uuid + ".txt");
michael@0 72 } else {
michael@0 73 path = OS.Path.join(this._submittedDumpsDir, "bp-" + uuid + ".txt");
michael@0 74 }
michael@0 75 mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR |
michael@0 76 OS.Constants.libc.S_IRGRP | OS.Constants.libc.S_IROTH;
michael@0 77 } else {
michael@0 78 path = OS.Path.join(this._pendingDumpsDir, uuid + ".dmp");
michael@0 79 mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR;
michael@0 80 }
michael@0 81
michael@0 82 return Task.spawn(function* () {
michael@0 83 let f = yield OS.File.open(path, {create: true}, {unixMode: mode});
michael@0 84 yield f.setDates(date, date);
michael@0 85 yield f.close();
michael@0 86 dump("Created fake crash: " + path + "\n");
michael@0 87
michael@0 88 return uuid;
michael@0 89 });
michael@0 90 },
michael@0 91
michael@0 92 createIgnoredDumpFile: function (filename, submitted=false) {
michael@0 93 let path;
michael@0 94 if (submitted) {
michael@0 95 path = OS.Path.join(this._submittedDumpsDir, filename);
michael@0 96 } else {
michael@0 97 path = OS.Path.join(this._pendingDumpsDir, filename);
michael@0 98 }
michael@0 99
michael@0 100 return Task.spawn(function* () {
michael@0 101 let mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR;
michael@0 102 yield OS.File.open(path, {create: true}, {unixMode: mode});
michael@0 103 dump ("Create ignored dump file: " + path + "\n");
michael@0 104 });
michael@0 105 },
michael@0 106
michael@0 107 createEventsFile: function (filename, type, date, content, index=0) {
michael@0 108 let path = OS.Path.join(this._eventsDirs[index], filename);
michael@0 109
michael@0 110 let data = type + "\n" +
michael@0 111 Math.floor(date.getTime() / 1000) + "\n" +
michael@0 112 content;
michael@0 113 let encoder = new TextEncoder();
michael@0 114 let array = encoder.encode(data);
michael@0 115
michael@0 116 return Task.spawn(function* () {
michael@0 117 yield OS.File.writeAtomic(path, array);
michael@0 118 yield OS.File.setDates(path, date, date);
michael@0 119 });
michael@0 120 },
michael@0 121
michael@0 122 /**
michael@0 123 * Overwrite event file handling to process our test file type.
michael@0 124 *
michael@0 125 * We can probably delete this once we have actual events defined.
michael@0 126 */
michael@0 127 _handleEventFilePayload: function (store, entry, type, date, payload) {
michael@0 128 if (type == "test.1") {
michael@0 129 if (payload == "malformed") {
michael@0 130 return this.EVENT_FILE_ERROR_MALFORMED;
michael@0 131 } else if (payload == "success") {
michael@0 132 return this.EVENT_FILE_SUCCESS;
michael@0 133 } else {
michael@0 134 return this.EVENT_FILE_ERROR_UNKNOWN_EVENT;
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 return CrashManager.prototype._handleEventFilePayload.call(this,
michael@0 139 store,
michael@0 140 entry,
michael@0 141 type,
michael@0 142 date,
michael@0 143 payload);
michael@0 144 },
michael@0 145 };
michael@0 146
michael@0 147 let DUMMY_DIR_COUNT = 0;
michael@0 148
michael@0 149 this.getManager = function () {
michael@0 150 return Task.spawn(function* () {
michael@0 151 const dirMode = OS.Constants.libc.S_IRWXU;
michael@0 152 let baseFile = OS.Constants.Path.profileDir;
michael@0 153
michael@0 154 function makeDir(create=true) {
michael@0 155 return Task.spawn(function* () {
michael@0 156 let path = OS.Path.join(baseFile, "dummy-dir-" + DUMMY_DIR_COUNT++);
michael@0 157
michael@0 158 if (!create) {
michael@0 159 return path;
michael@0 160 }
michael@0 161
michael@0 162 dump("Creating directory: " + path + "\n");
michael@0 163 yield OS.File.makeDir(path, {unixMode: dirMode});
michael@0 164
michael@0 165 return path;
michael@0 166 });
michael@0 167 }
michael@0 168
michael@0 169 let pendingD = yield makeDir();
michael@0 170 let submittedD = yield makeDir();
michael@0 171 let eventsD1 = yield makeDir();
michael@0 172 let eventsD2 = yield makeDir();
michael@0 173
michael@0 174 // Store directory is created at run-time if needed. Ensure those code
michael@0 175 // paths are triggered.
michael@0 176 let storeD = yield makeDir(false);
michael@0 177
michael@0 178 let m = new TestingCrashManager({
michael@0 179 pendingDumpsDir: pendingD,
michael@0 180 submittedDumpsDir: submittedD,
michael@0 181 eventsDirs: [eventsD1, eventsD2],
michael@0 182 storeDir: storeD,
michael@0 183 });
michael@0 184
michael@0 185 return m;
michael@0 186 });
michael@0 187 };

mercurial