forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_startup_crash.js
300 lines (259 loc) · 13.7 KB
/
test_startup_crash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Components.utils.import("resource://gre/modules/Services.jsm");
const Cc = Components.classes;
const Ci = Components.interfaces;
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "10.0");
let prefService = Services.prefs;
let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].
getService(Ci.nsIAppStartup);
const pref_last_success = "toolkit.startup.last_success";
const pref_recent_crashes = "toolkit.startup.recent_crashes";
const pref_max_resumed_crashes = "toolkit.startup.max_resumed_crashes";
const pref_always_use_safe_mode = "toolkit.startup.always_use_safe_mode";
function run_test() {
prefService.setBoolPref(pref_always_use_safe_mode, true);
resetTestEnv(0);
test_trackStartupCrashBegin();
test_trackStartupCrashEnd();
test_trackStartupCrashBegin_safeMode();
test_trackStartupCrashEnd_safeMode();
test_maxResumed();
resetTestEnv(0);
prefService.clearUserPref(pref_always_use_safe_mode);
}
// reset prefs to default
function resetTestEnv(replacedLockTime) {
try {
// call begin to reset mStartupCrashTrackingEnded
appStartup.trackStartupCrashBegin();
} catch (x) { }
prefService.setIntPref(pref_max_resumed_crashes, 2);
prefService.clearUserPref(pref_recent_crashes);
gAppInfo.replacedLockTime = replacedLockTime;
prefService.clearUserPref(pref_last_success);
}
function now_seconds() {
return ms_to_s(Date.now());
}
function ms_to_s(ms) {
return Math.floor(ms / 1000);
}
function test_trackStartupCrashBegin() {
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
do_check_false(gAppInfo.inSafeMode);
// first run with startup crash tracking - existing profile lock
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(prefService.prefHasUserValue(pref_last_success));
do_check_eq(replacedLockTime, gAppInfo.replacedLockTime);
try {
do_check_false(appStartup.trackStartupCrashBegin());
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
do_check_false(prefService.prefHasUserValue(pref_last_success));
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// first run with startup crash tracking - no existing profile lock
replacedLockTime = 0;
resetTestEnv(replacedLockTime);
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(prefService.prefHasUserValue(pref_last_success));
do_check_eq(replacedLockTime, gAppInfo.replacedLockTime);
try {
do_check_false(appStartup.trackStartupCrashBegin());
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
do_check_false(prefService.prefHasUserValue(pref_last_success));
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup - last startup was success
replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_eq(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
do_check_false(appStartup.trackStartupCrashBegin());
do_check_eq(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup with 1 recent crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_false(appStartup.trackStartupCrashBegin());
do_check_eq(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
do_check_eq(1, prefService.getIntPref(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup with max_resumed_crashes crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, max_resumed);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_false(appStartup.trackStartupCrashBegin());
do_check_eq(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
do_check_eq(max_resumed, prefService.getIntPref(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup with too many recent crashes
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_true(appStartup.trackStartupCrashBegin());
// should remain the same since the last startup was not a crash
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
do_check_true(appStartup.automaticSafeModeNecessary);
// normal startup with too many recent crashes and startup crash tracking disabled
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_max_resumed_crashes, -1);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_false(appStartup.trackStartupCrashBegin());
// should remain the same since the last startup was not a crash
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
// returns false when disabled
do_check_false(appStartup.automaticSafeModeNecessary);
do_check_eq(-1, prefService.getIntPref(pref_max_resumed_crashes));
// normal startup after 1 non-recent crash (1 year ago), no other recent
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 365 * 24 * 60 * 60);
do_check_false(appStartup.trackStartupCrashBegin());
// recent crash count pref should be unset since the last crash was not recent
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup after 1 crash (1 minute ago), no other recent
replacedLockTime = Date.now() - 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
do_check_false(appStartup.trackStartupCrashBegin());
// recent crash count pref should be created with value 1
do_check_eq(1, prefService.getIntPref(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup after another crash (1 minute ago), 1 already
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
replacedLockTime = Date.now() - 60 * 1000;
gAppInfo.replacedLockTime = replacedLockTime;
do_check_false(appStartup.trackStartupCrashBegin());
// recent crash count pref should be incremented by 1
do_check_eq(2, prefService.getIntPref(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
// normal startup after another crash (1 minute ago), 2 already
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
do_check_true(appStartup.trackStartupCrashBegin());
// recent crash count pref should be incremented by 1
do_check_eq(3, prefService.getIntPref(pref_recent_crashes));
do_check_true(appStartup.automaticSafeModeNecessary);
// normal startup after 1 non-recent crash (1 year ago), 3 crashes already
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
do_check_false(appStartup.trackStartupCrashBegin());
// recent crash count should be unset since the last crash was not recent
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
}
function test_trackStartupCrashEnd() {
// successful startup with no last_success (upgrade test)
let replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
try {
appStartup.trackStartupCrashBegin(); // required to be called before end
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
appStartup.trackStartupCrashEnd();
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_false(prefService.prefHasUserValue(pref_last_success));
// successful startup - should set last_success
replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
// ensure last_success was set since we have declared a succesful startup
// main timestamp doesn't get set in XPCShell so approximate with now
do_check_true(prefService.getIntPref(pref_last_success) <= now_seconds());
do_check_true(prefService.getIntPref(pref_last_success) >= now_seconds() - 4 * 60 * 60);
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
// successful startup with 1 recent crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
prefService.setIntPref(pref_recent_crashes, 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
// ensure recent_crashes was cleared since we have declared a succesful startup
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
}
function test_trackStartupCrashBegin_safeMode() {
gAppInfo.inSafeMode = true;
resetTestEnv(0);
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
// check manual safe mode doesn't change prefs without crash
let replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_true(prefService.prefHasUserValue(pref_last_success));
do_check_false(appStartup.automaticSafeModeNecessary);
do_check_false(appStartup.trackStartupCrashBegin());
do_check_false(prefService.prefHasUserValue(pref_recent_crashes));
do_check_true(prefService.prefHasUserValue(pref_last_success));
do_check_false(appStartup.automaticSafeModeNecessary);
// check forced safe mode doesn't change prefs without crash
replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
do_check_true(prefService.prefHasUserValue(pref_last_success));
do_check_false(appStartup.automaticSafeModeNecessary);
do_check_true(appStartup.trackStartupCrashBegin());
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
do_check_true(prefService.prefHasUserValue(pref_last_success));
do_check_true(appStartup.automaticSafeModeNecessary);
// check forced safe mode after old crash
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
// one year ago
let last_success = ms_to_s(replacedLockTime) - 365 * 24 * 60 * 60;
prefService.setIntPref(pref_last_success, last_success);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
do_check_true(prefService.prefHasUserValue(pref_last_success));
do_check_true(appStartup.automaticSafeModeNecessary);
do_check_true(appStartup.trackStartupCrashBegin());
do_check_eq(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
do_check_eq(last_success, prefService.getIntPref(pref_last_success));
do_check_true(appStartup.automaticSafeModeNecessary);
}
function test_trackStartupCrashEnd_safeMode() {
gAppInfo.inSafeMode = true;
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 24 * 60 * 60);
// ensure recent_crashes were not cleared in manual safe mode
prefService.setIntPref(pref_recent_crashes, 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
do_check_eq(1, prefService.getIntPref(pref_recent_crashes));
// recent_crashes should be set to max_resumed in forced safe mode to allow the user
// to try and start in regular mode after making changes.
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
do_check_eq(max_resumed, prefService.getIntPref(pref_recent_crashes));
}
function test_maxResumed() {
resetTestEnv(0);
gAppInfo.inSafeMode = false;
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_max_resumed_crashes, -1);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 24 * 60 * 60);
appStartup.trackStartupCrashBegin();
// should remain the same since the last startup was not a crash
do_check_eq(max_resumed + 2, prefService.getIntPref(pref_recent_crashes));
do_check_false(appStartup.automaticSafeModeNecessary);
}