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