1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_hotfix.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,345 @@ 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 +// This verifies that hotfix installation works 1.9 + 1.10 +// The test extension uses an insecure update url. 1.11 +Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false); 1.12 +// Ignore any certificate requirements the app has set 1.13 +Services.prefs.setBoolPref("extensions.hotfix.cert.checkAttributes", false); 1.14 + 1.15 +Components.utils.import("resource://testing-common/httpd.js"); 1.16 +var testserver = new HttpServer(); 1.17 +testserver.start(-1); 1.18 +gPort = testserver.identity.primaryPort; 1.19 +testserver.registerDirectory("/addons/", do_get_file("addons")); 1.20 +mapFile("/data/test_hotfix_1.rdf", testserver); 1.21 +mapFile("/data/test_hotfix_2.rdf", testserver); 1.22 +mapFile("/data/test_hotfix_3.rdf", testserver); 1.23 + 1.24 +const profileDir = gProfD.clone(); 1.25 +profileDir.append("extensions"); 1.26 + 1.27 +function run_test() { 1.28 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); 1.29 + 1.30 + startupManager(); 1.31 + 1.32 + do_test_pending(); 1.33 + run_test_1(); 1.34 +} 1.35 + 1.36 +function end_test() { 1.37 + testserver.stop(do_test_finished); 1.38 +} 1.39 + 1.40 +// Test that background updates find and install any available hotfix 1.41 +function run_test_1() { 1.42 + Services.prefs.setCharPref("extensions.hotfix.id", "hotfix@tests.mozilla.org"); 1.43 + Services.prefs.setCharPref("extensions.update.background.url", "http://localhost:" + 1.44 + gPort + "/data/test_hotfix_1.rdf"); 1.45 + 1.46 + prepare_test({ 1.47 + "hotfix@tests.mozilla.org": [ 1.48 + "onInstalling" 1.49 + ] 1.50 + }, [ 1.51 + "onNewInstall", 1.52 + "onDownloadStarted", 1.53 + "onDownloadEnded", 1.54 + "onInstallStarted", 1.55 + "onInstallEnded", 1.56 + ], callback_soon(check_test_1)); 1.57 + 1.58 + // Fake a timer event 1.59 + gInternalManager.notify(null); 1.60 +} 1.61 + 1.62 +function check_test_1() { 1.63 + restartManager(); 1.64 + 1.65 + AddonManager.getAddonByID("hotfix@tests.mozilla.org", function(aAddon) { 1.66 + do_check_neq(aAddon, null); 1.67 + do_check_eq(aAddon.version, "1.0"); 1.68 + 1.69 + aAddon.uninstall(); 1.70 + do_execute_soon(run_test_2); 1.71 + }); 1.72 +} 1.73 + 1.74 +// Don't install an already used hotfix 1.75 +function run_test_2() { 1.76 + restartManager(); 1.77 + 1.78 + AddonManager.addInstallListener({ 1.79 + onNewInstall: function() { 1.80 + do_throw("Should not have seen a new install created"); 1.81 + } 1.82 + }); 1.83 + 1.84 + function observer() { 1.85 + Services.obs.removeObserver(arguments.callee, "addons-background-update-complete"); 1.86 + do_execute_soon(run_test_3); 1.87 + } 1.88 + 1.89 + Services.obs.addObserver(observer, "addons-background-update-complete", false); 1.90 + 1.91 + // Fake a timer event 1.92 + gInternalManager.notify(null); 1.93 +} 1.94 + 1.95 +// Install a newer hotfix 1.96 +function run_test_3() { 1.97 + restartManager(); 1.98 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.99 + gPort + "/data/test_hotfix_2.rdf"); 1.100 + 1.101 + prepare_test({ 1.102 + "hotfix@tests.mozilla.org": [ 1.103 + "onInstalling" 1.104 + ] 1.105 + }, [ 1.106 + "onNewInstall", 1.107 + "onDownloadStarted", 1.108 + "onDownloadEnded", 1.109 + "onInstallStarted", 1.110 + "onInstallEnded", 1.111 + ], callback_soon(check_test_3)); 1.112 + 1.113 + // Fake a timer event 1.114 + gInternalManager.notify(null); 1.115 +} 1.116 + 1.117 +function check_test_3() { 1.118 + restartManager(); 1.119 + 1.120 + AddonManager.getAddonByID("hotfix@tests.mozilla.org", function(aAddon) { 1.121 + do_check_neq(aAddon, null); 1.122 + do_check_eq(aAddon.version, "2.0"); 1.123 + 1.124 + aAddon.uninstall(); 1.125 + do_execute_soon(run_test_4); 1.126 + }); 1.127 +} 1.128 + 1.129 +// Don't install an incompatible hotfix 1.130 +function run_test_4() { 1.131 + restartManager(); 1.132 + 1.133 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.134 + gPort + "/data/test_hotfix_3.rdf"); 1.135 + 1.136 + AddonManager.addInstallListener({ 1.137 + onNewInstall: function() { 1.138 + do_throw("Should not have seen a new install created"); 1.139 + } 1.140 + }); 1.141 + 1.142 + function observer() { 1.143 + Services.obs.removeObserver(arguments.callee, "addons-background-update-complete"); 1.144 + do_execute_soon(run_test_5); 1.145 + } 1.146 + 1.147 + Services.obs.addObserver(observer, "addons-background-update-complete", false); 1.148 + 1.149 + // Fake a timer event 1.150 + gInternalManager.notify(null); 1.151 +} 1.152 + 1.153 +// Don't install an older hotfix 1.154 +function run_test_5() { 1.155 + restartManager(); 1.156 + 1.157 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.158 + gPort + "/data/test_hotfix_1.rdf"); 1.159 + 1.160 + AddonManager.addInstallListener({ 1.161 + onNewInstall: function() { 1.162 + do_throw("Should not have seen a new install created"); 1.163 + } 1.164 + }); 1.165 + 1.166 + function observer() { 1.167 + Services.obs.removeObserver(arguments.callee, "addons-background-update-complete"); 1.168 + do_execute_soon(run_test_6); 1.169 + } 1.170 + 1.171 + Services.obs.addObserver(observer, "addons-background-update-complete", false); 1.172 + 1.173 + // Fake a timer event 1.174 + gInternalManager.notify(null); 1.175 +} 1.176 + 1.177 +// Don't re-download an already pending install 1.178 +function run_test_6() { 1.179 + restartManager(); 1.180 + 1.181 + Services.prefs.setCharPref("extensions.hotfix.lastVersion", "0"); 1.182 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.183 + gPort + "/data/test_hotfix_1.rdf"); 1.184 + 1.185 + prepare_test({ 1.186 + "hotfix@tests.mozilla.org": [ 1.187 + "onInstalling" 1.188 + ] 1.189 + }, [ 1.190 + "onNewInstall", 1.191 + "onDownloadStarted", 1.192 + "onDownloadEnded", 1.193 + "onInstallStarted", 1.194 + "onInstallEnded", 1.195 + ], callback_soon(check_test_6)); 1.196 + 1.197 + // Fake a timer event 1.198 + gInternalManager.notify(null); 1.199 +} 1.200 + 1.201 +function check_test_6() { 1.202 + AddonManager.addInstallListener({ 1.203 + onNewInstall: function() { 1.204 + do_throw("Should not have seen a new install created"); 1.205 + } 1.206 + }); 1.207 + 1.208 + function observer() { 1.209 + Services.obs.removeObserver(arguments.callee, "addons-background-update-complete"); 1.210 + restartManager(); 1.211 + 1.212 + AddonManager.getAddonByID("hotfix@tests.mozilla.org", function(aAddon) { 1.213 + aAddon.uninstall(); 1.214 + do_execute_soon(run_test_7); 1.215 + }); 1.216 + } 1.217 + 1.218 + Services.obs.addObserver(observer, "addons-background-update-complete", false); 1.219 + 1.220 + // Fake a timer event 1.221 + gInternalManager.notify(null); 1.222 +} 1.223 + 1.224 +// Start downloading again if something cancels the install 1.225 +function run_test_7() { 1.226 + restartManager(); 1.227 + 1.228 + Services.prefs.setCharPref("extensions.hotfix.lastVersion", "0"); 1.229 + 1.230 + prepare_test({ 1.231 + "hotfix@tests.mozilla.org": [ 1.232 + "onInstalling" 1.233 + ] 1.234 + }, [ 1.235 + "onNewInstall", 1.236 + "onDownloadStarted", 1.237 + "onDownloadEnded", 1.238 + "onInstallStarted", 1.239 + "onInstallEnded", 1.240 + ], check_test_7); 1.241 + 1.242 + // Fake a timer event 1.243 + gInternalManager.notify(null); 1.244 +} 1.245 + 1.246 +function check_test_7(aInstall) { 1.247 + prepare_test({ 1.248 + "hotfix@tests.mozilla.org": [ 1.249 + "onOperationCancelled" 1.250 + ] 1.251 + }, [ 1.252 + "onInstallCancelled", 1.253 + ]); 1.254 + 1.255 + aInstall.cancel(); 1.256 + 1.257 + prepare_test({ 1.258 + "hotfix@tests.mozilla.org": [ 1.259 + "onInstalling" 1.260 + ] 1.261 + }, [ 1.262 + "onNewInstall", 1.263 + "onDownloadStarted", 1.264 + "onDownloadEnded", 1.265 + "onInstallStarted", 1.266 + "onInstallEnded", 1.267 + ], callback_soon(finish_test_7)); 1.268 + 1.269 + // Fake a timer event 1.270 + gInternalManager.notify(null); 1.271 +} 1.272 + 1.273 +function finish_test_7() { 1.274 + restartManager(); 1.275 + 1.276 + AddonManager.getAddonByID("hotfix@tests.mozilla.org", function(aAddon) { 1.277 + do_check_neq(aAddon, null); 1.278 + do_check_eq(aAddon.version, "1.0"); 1.279 + 1.280 + aAddon.uninstall(); 1.281 + do_execute_soon(run_test_8); 1.282 + }); 1.283 +} 1.284 + 1.285 +// Cancel a pending install when a newer version is already available 1.286 +function run_test_8() { 1.287 + restartManager(); 1.288 + 1.289 + Services.prefs.setCharPref("extensions.hotfix.lastVersion", "0"); 1.290 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.291 + gPort + "/data/test_hotfix_1.rdf"); 1.292 + 1.293 + prepare_test({ 1.294 + "hotfix@tests.mozilla.org": [ 1.295 + "onInstalling" 1.296 + ] 1.297 + }, [ 1.298 + "onNewInstall", 1.299 + "onDownloadStarted", 1.300 + "onDownloadEnded", 1.301 + "onInstallStarted", 1.302 + "onInstallEnded", 1.303 + ], check_test_8); 1.304 + 1.305 + // Fake a timer event 1.306 + gInternalManager.notify(null); 1.307 +} 1.308 + 1.309 +function check_test_8() { 1.310 + Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + 1.311 + gPort + "/data/test_hotfix_2.rdf"); 1.312 + 1.313 + prepare_test({ 1.314 + "hotfix@tests.mozilla.org": [ 1.315 + "onOperationCancelled", 1.316 + "onInstalling" 1.317 + ] 1.318 + }, [ 1.319 + "onNewInstall", 1.320 + "onDownloadStarted", 1.321 + "onDownloadEnded", 1.322 + "onInstallStarted", 1.323 + "onInstallCancelled", 1.324 + "onInstallEnded", 1.325 + ], finish_test_8); 1.326 + 1.327 + // Fake a timer event 1.328 + gInternalManager.notify(null); 1.329 +} 1.330 + 1.331 +function finish_test_8() { 1.332 + AddonManager.getAllInstalls(callback_soon(function(aInstalls) { 1.333 + do_check_eq(aInstalls.length, 1); 1.334 + do_check_eq(aInstalls[0].version, "2.0"); 1.335 + 1.336 + restartManager(); 1.337 + 1.338 + AddonManager.getAddonByID("hotfix@tests.mozilla.org", callback_soon(function(aAddon) { 1.339 + do_check_neq(aAddon, null); 1.340 + do_check_eq(aAddon.version, "2.0"); 1.341 + 1.342 + aAddon.uninstall(); 1.343 + restartManager(); 1.344 + 1.345 + end_test(); 1.346 + })); 1.347 + })); 1.348 +}