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