1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_corrupt.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,403 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +// Checks that we rebuild something sensible from a corrupt database 1.9 + 1.10 + 1.11 +Components.utils.import("resource://testing-common/httpd.js"); 1.12 +// Create and configure the HTTP server. 1.13 +var testserver = new HttpServer(); 1.14 +testserver.start(-1); 1.15 +gPort = testserver.identity.primaryPort; 1.16 + 1.17 +// register files with server 1.18 +testserver.registerDirectory("/addons/", do_get_file("addons")); 1.19 +mapFile("/data/test_corrupt.rdf", testserver); 1.20 + 1.21 +// The test extension uses an insecure update url. 1.22 +Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false); 1.23 +Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false); 1.24 + 1.25 +// Will be enabled 1.26 +var addon1 = { 1.27 + id: "addon1@tests.mozilla.org", 1.28 + version: "1.0", 1.29 + name: "Test 1", 1.30 + targetApplications: [{ 1.31 + id: "xpcshell@tests.mozilla.org", 1.32 + minVersion: "2", 1.33 + maxVersion: "2" 1.34 + }] 1.35 +}; 1.36 + 1.37 +// Will be disabled 1.38 +var addon2 = { 1.39 + id: "addon2@tests.mozilla.org", 1.40 + version: "1.0", 1.41 + name: "Test 2", 1.42 + targetApplications: [{ 1.43 + id: "xpcshell@tests.mozilla.org", 1.44 + minVersion: "2", 1.45 + maxVersion: "2" 1.46 + }] 1.47 +}; 1.48 + 1.49 +// Will get a compatibility update and stay enabled 1.50 +var addon3 = { 1.51 + id: "addon3@tests.mozilla.org", 1.52 + version: "1.0", 1.53 + name: "Test 3", 1.54 + updateURL: "http://localhost:" + gPort + "/data/test_corrupt.rdf", 1.55 + targetApplications: [{ 1.56 + id: "xpcshell@tests.mozilla.org", 1.57 + minVersion: "1", 1.58 + maxVersion: "1" 1.59 + }] 1.60 +}; 1.61 + 1.62 +// Will get a compatibility update and be enabled 1.63 +var addon4 = { 1.64 + id: "addon4@tests.mozilla.org", 1.65 + version: "1.0", 1.66 + name: "Test 4", 1.67 + updateURL: "http://localhost:" + gPort + "/data/test_corrupt.rdf", 1.68 + targetApplications: [{ 1.69 + id: "xpcshell@tests.mozilla.org", 1.70 + minVersion: "1", 1.71 + maxVersion: "1" 1.72 + }] 1.73 +}; 1.74 + 1.75 +// Would stay incompatible with strict compat 1.76 +var addon5 = { 1.77 + id: "addon5@tests.mozilla.org", 1.78 + version: "1.0", 1.79 + name: "Test 5", 1.80 + targetApplications: [{ 1.81 + id: "xpcshell@tests.mozilla.org", 1.82 + minVersion: "1", 1.83 + maxVersion: "1" 1.84 + }] 1.85 +}; 1.86 + 1.87 +// Enabled bootstrapped 1.88 +var addon6 = { 1.89 + id: "addon6@tests.mozilla.org", 1.90 + version: "1.0", 1.91 + name: "Test 6", 1.92 + bootstrap: "true", 1.93 + targetApplications: [{ 1.94 + id: "xpcshell@tests.mozilla.org", 1.95 + minVersion: "2", 1.96 + maxVersion: "2" 1.97 + }] 1.98 +}; 1.99 + 1.100 +// Disabled bootstrapped 1.101 +var addon7 = { 1.102 + id: "addon7@tests.mozilla.org", 1.103 + version: "1.0", 1.104 + name: "Test 7", 1.105 + bootstrap: "true", 1.106 + targetApplications: [{ 1.107 + id: "xpcshell@tests.mozilla.org", 1.108 + minVersion: "2", 1.109 + maxVersion: "2" 1.110 + }] 1.111 +}; 1.112 + 1.113 +// The default theme 1.114 +var theme1 = { 1.115 + id: "theme1@tests.mozilla.org", 1.116 + version: "1.0", 1.117 + name: "Theme 1", 1.118 + internalName: "classic/1.0", 1.119 + targetApplications: [{ 1.120 + id: "xpcshell@tests.mozilla.org", 1.121 + minVersion: "2", 1.122 + maxVersion: "2" 1.123 + }] 1.124 +}; 1.125 + 1.126 +// The selected theme 1.127 +var theme2 = { 1.128 + id: "theme2@tests.mozilla.org", 1.129 + version: "1.0", 1.130 + name: "Theme 2", 1.131 + internalName: "test/1.0", 1.132 + targetApplications: [{ 1.133 + id: "xpcshell@tests.mozilla.org", 1.134 + minVersion: "2", 1.135 + maxVersion: "2" 1.136 + }] 1.137 +}; 1.138 + 1.139 +const profileDir = gProfD.clone(); 1.140 +profileDir.append("extensions"); 1.141 + 1.142 +function run_test() { 1.143 + do_test_pending(); 1.144 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2", "2"); 1.145 + 1.146 + writeInstallRDFForExtension(addon1, profileDir); 1.147 + writeInstallRDFForExtension(addon2, profileDir); 1.148 + writeInstallRDFForExtension(addon3, profileDir); 1.149 + writeInstallRDFForExtension(addon4, profileDir); 1.150 + writeInstallRDFForExtension(addon5, profileDir); 1.151 + writeInstallRDFForExtension(addon6, profileDir); 1.152 + writeInstallRDFForExtension(addon7, profileDir); 1.153 + writeInstallRDFForExtension(theme1, profileDir); 1.154 + writeInstallRDFForExtension(theme2, profileDir); 1.155 + 1.156 + // Startup the profile and setup the initial state 1.157 + startupManager(); 1.158 + 1.159 + AddonManager.getAddonsByIDs(["addon2@tests.mozilla.org", 1.160 + "addon3@tests.mozilla.org", 1.161 + "addon4@tests.mozilla.org", 1.162 + "addon7@tests.mozilla.org", 1.163 + "theme2@tests.mozilla.org"], function([a2, a3, a4, 1.164 + a7, t2]) { 1.165 + // Set up the initial state 1.166 + a2.userDisabled = true; 1.167 + a4.userDisabled = true; 1.168 + a7.userDisabled = true; 1.169 + t2.userDisabled = false; 1.170 + a3.findUpdates({ 1.171 + onUpdateFinished: function() { 1.172 + a4.findUpdates({ 1.173 + onUpdateFinished: function() { 1.174 + do_execute_soon(run_test_1); 1.175 + } 1.176 + }, AddonManager.UPDATE_WHEN_PERIODIC_UPDATE); 1.177 + } 1.178 + }, AddonManager.UPDATE_WHEN_PERIODIC_UPDATE); 1.179 + }); 1.180 +} 1.181 + 1.182 +function end_test() { 1.183 + testserver.stop(do_test_finished); 1.184 +} 1.185 + 1.186 +function run_test_1() { 1.187 + restartManager(); 1.188 + 1.189 + AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org", 1.190 + "addon2@tests.mozilla.org", 1.191 + "addon3@tests.mozilla.org", 1.192 + "addon4@tests.mozilla.org", 1.193 + "addon5@tests.mozilla.org", 1.194 + "addon6@tests.mozilla.org", 1.195 + "addon7@tests.mozilla.org", 1.196 + "theme1@tests.mozilla.org", 1.197 + "theme2@tests.mozilla.org"], 1.198 + callback_soon(function([a1, a2, a3, a4, a5, a6, a7, t1, t2]) { 1.199 + do_check_neq(a1, null); 1.200 + do_check_true(a1.isActive); 1.201 + do_check_false(a1.userDisabled); 1.202 + do_check_false(a1.appDisabled); 1.203 + do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE); 1.204 + 1.205 + do_check_neq(a2, null); 1.206 + do_check_false(a2.isActive); 1.207 + do_check_true(a2.userDisabled); 1.208 + do_check_false(a2.appDisabled); 1.209 + do_check_eq(a2.pendingOperations, AddonManager.PENDING_NONE); 1.210 + 1.211 + do_check_neq(a3, null); 1.212 + do_check_true(a3.isActive); 1.213 + do_check_false(a3.userDisabled); 1.214 + do_check_false(a3.appDisabled); 1.215 + do_check_eq(a3.pendingOperations, AddonManager.PENDING_NONE); 1.216 + 1.217 + do_check_neq(a4, null); 1.218 + do_check_false(a4.isActive); 1.219 + do_check_true(a4.userDisabled); 1.220 + do_check_false(a4.appDisabled); 1.221 + do_check_eq(a4.pendingOperations, AddonManager.PENDING_NONE); 1.222 + 1.223 + do_check_neq(a5, null); 1.224 + do_check_true(a5.isActive); 1.225 + do_check_false(a5.userDisabled); 1.226 + do_check_false(a5.appDisabled); 1.227 + do_check_eq(a5.pendingOperations, AddonManager.PENDING_NONE); 1.228 + 1.229 + do_check_neq(a6, null); 1.230 + do_check_true(a6.isActive); 1.231 + do_check_false(a6.userDisabled); 1.232 + do_check_false(a6.appDisabled); 1.233 + do_check_eq(a6.pendingOperations, AddonManager.PENDING_NONE); 1.234 + 1.235 + do_check_neq(a7, null); 1.236 + do_check_false(a7.isActive); 1.237 + do_check_true(a7.userDisabled); 1.238 + do_check_false(a7.appDisabled); 1.239 + do_check_eq(a7.pendingOperations, AddonManager.PENDING_NONE); 1.240 + 1.241 + do_check_neq(t1, null); 1.242 + do_check_false(t1.isActive); 1.243 + do_check_true(t1.userDisabled); 1.244 + do_check_false(t1.appDisabled); 1.245 + do_check_eq(t1.pendingOperations, AddonManager.PENDING_NONE); 1.246 + 1.247 + do_check_neq(t2, null); 1.248 + do_check_true(t2.isActive); 1.249 + do_check_false(t2.userDisabled); 1.250 + do_check_false(t2.appDisabled); 1.251 + do_check_eq(t2.pendingOperations, AddonManager.PENDING_NONE); 1.252 + 1.253 + // Shutdown and replace the database with a corrupt file (a directory 1.254 + // serves this purpose). On startup the add-ons manager won't rebuild 1.255 + // because there is a file there still. 1.256 + shutdownManager(); 1.257 + gExtensionsJSON.remove(true); 1.258 + gExtensionsJSON.create(AM_Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); 1.259 + startupManager(false); 1.260 + 1.261 + // Accessing the add-ons should open and recover the database 1.262 + AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org", 1.263 + "addon2@tests.mozilla.org", 1.264 + "addon3@tests.mozilla.org", 1.265 + "addon4@tests.mozilla.org", 1.266 + "addon5@tests.mozilla.org", 1.267 + "addon6@tests.mozilla.org", 1.268 + "addon7@tests.mozilla.org", 1.269 + "theme1@tests.mozilla.org", 1.270 + "theme2@tests.mozilla.org"], 1.271 + callback_soon(function([a1, a2, a3, a4, a5, a6, a7, t1, t2]) { 1.272 + // Should be correctly recovered 1.273 + do_check_neq(a1, null); 1.274 + do_check_true(a1.isActive); 1.275 + do_check_false(a1.userDisabled); 1.276 + do_check_false(a1.appDisabled); 1.277 + do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE); 1.278 + 1.279 + // Should be correctly recovered 1.280 + do_check_neq(a2, null); 1.281 + do_check_false(a2.isActive); 1.282 + do_check_true(a2.userDisabled); 1.283 + do_check_false(a2.appDisabled); 1.284 + do_check_eq(a2.pendingOperations, AddonManager.PENDING_NONE); 1.285 + 1.286 + // The compatibility update won't be recovered but it should still be 1.287 + // active for this session 1.288 + do_check_neq(a3, null); 1.289 + do_check_true(a3.isActive); 1.290 + do_check_false(a3.userDisabled); 1.291 + do_check_false(a3.appDisabled); 1.292 + do_check_eq(a3.pendingOperations, AddonManager.PENDING_NONE); 1.293 + 1.294 + // The compatibility update won't be recovered and with strict 1.295 + // compatibility it would not have been able to tell that it was 1.296 + // previously userDisabled. However, without strict compat, it wasn't 1.297 + // appDisabled, so it knows it must have been userDisabled. 1.298 + do_check_neq(a4, null); 1.299 + do_check_false(a4.isActive); 1.300 + do_check_true(a4.userDisabled); 1.301 + do_check_false(a4.appDisabled); 1.302 + do_check_eq(a4.pendingOperations, AddonManager.PENDING_NONE); 1.303 + 1.304 + do_check_neq(a5, null); 1.305 + do_check_true(a5.isActive); 1.306 + do_check_false(a5.userDisabled); 1.307 + do_check_false(a5.appDisabled); 1.308 + do_check_eq(a5.pendingOperations, AddonManager.PENDING_NONE); 1.309 + 1.310 + do_check_neq(a6, null); 1.311 + do_check_true(a6.isActive); 1.312 + do_check_false(a6.userDisabled); 1.313 + do_check_false(a6.appDisabled); 1.314 + do_check_eq(a6.pendingOperations, AddonManager.PENDING_NONE); 1.315 + 1.316 + do_check_neq(a7, null); 1.317 + do_check_false(a7.isActive); 1.318 + do_check_true(a7.userDisabled); 1.319 + do_check_false(a7.appDisabled); 1.320 + do_check_eq(a7.pendingOperations, AddonManager.PENDING_NONE); 1.321 + 1.322 + // Should be correctly recovered 1.323 + do_check_neq(t1, null); 1.324 + do_check_false(t1.isActive); 1.325 + do_check_true(t1.userDisabled); 1.326 + do_check_false(t1.appDisabled); 1.327 + do_check_eq(t1.pendingOperations, AddonManager.PENDING_NONE); 1.328 + 1.329 + // Should be correctly recovered 1.330 + do_check_neq(t2, null); 1.331 + do_check_true(t2.isActive); 1.332 + do_check_false(t2.userDisabled); 1.333 + do_check_false(t2.appDisabled); 1.334 + do_check_eq(t2.pendingOperations, AddonManager.PENDING_NONE); 1.335 + 1.336 + restartManager(); 1.337 + 1.338 + AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org", 1.339 + "addon2@tests.mozilla.org", 1.340 + "addon3@tests.mozilla.org", 1.341 + "addon4@tests.mozilla.org", 1.342 + "addon5@tests.mozilla.org", 1.343 + "addon6@tests.mozilla.org", 1.344 + "addon7@tests.mozilla.org", 1.345 + "theme1@tests.mozilla.org", 1.346 + "theme2@tests.mozilla.org"], 1.347 + callback_soon(function([a1, a2, a3, a4, a5, a6, a7, t1, t2]) { 1.348 + do_check_neq(a1, null); 1.349 + do_check_true(a1.isActive); 1.350 + do_check_false(a1.userDisabled); 1.351 + do_check_false(a1.appDisabled); 1.352 + do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE); 1.353 + 1.354 + do_check_neq(a2, null); 1.355 + do_check_false(a2.isActive); 1.356 + do_check_true(a2.userDisabled); 1.357 + do_check_false(a2.appDisabled); 1.358 + do_check_eq(a2.pendingOperations, AddonManager.PENDING_NONE); 1.359 + 1.360 + do_check_neq(a3, null); 1.361 + do_check_true(a3.isActive); 1.362 + do_check_false(a3.userDisabled); 1.363 + do_check_false(a3.appDisabled); 1.364 + do_check_eq(a3.pendingOperations, AddonManager.PENDING_NONE); 1.365 + 1.366 + do_check_neq(a4, null); 1.367 + do_check_false(a4.isActive); 1.368 + do_check_true(a4.userDisabled); 1.369 + do_check_false(a4.appDisabled); 1.370 + do_check_eq(a4.pendingOperations, AddonManager.PENDING_NONE); 1.371 + 1.372 + do_check_neq(a5, null); 1.373 + do_check_true(a5.isActive); 1.374 + do_check_false(a5.userDisabled); 1.375 + do_check_false(a5.appDisabled); 1.376 + do_check_eq(a5.pendingOperations, AddonManager.PENDING_NONE); 1.377 + 1.378 + do_check_neq(a6, null); 1.379 + do_check_true(a6.isActive); 1.380 + do_check_false(a6.userDisabled); 1.381 + do_check_false(a6.appDisabled); 1.382 + do_check_eq(a6.pendingOperations, AddonManager.PENDING_NONE); 1.383 + 1.384 + do_check_neq(a7, null); 1.385 + do_check_false(a7.isActive); 1.386 + do_check_true(a7.userDisabled); 1.387 + do_check_false(a7.appDisabled); 1.388 + do_check_eq(a7.pendingOperations, AddonManager.PENDING_NONE); 1.389 + 1.390 + do_check_neq(t1, null); 1.391 + do_check_false(t1.isActive); 1.392 + do_check_true(t1.userDisabled); 1.393 + do_check_false(t1.appDisabled); 1.394 + do_check_eq(t1.pendingOperations, AddonManager.PENDING_NONE); 1.395 + 1.396 + do_check_neq(t2, null); 1.397 + do_check_true(t2.isActive); 1.398 + do_check_false(t2.userDisabled); 1.399 + do_check_false(t2.appDisabled); 1.400 + do_check_eq(t2.pendingOperations, AddonManager.PENDING_NONE); 1.401 + 1.402 + end_test(); 1.403 + })); 1.404 + })); 1.405 + })); 1.406 +}