toolkit/mozapps/extensions/test/xpcshell/test_updatecheck.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_updatecheck.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,312 @@
     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 AddonUpdateChecker works correctly
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/addons/AddonUpdateChecker.jsm");
    1.11 +
    1.12 +Components.utils.import("resource://testing-common/httpd.js");
    1.13 +var testserver;
    1.14 +
    1.15 +function run_test() {
    1.16 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
    1.17 +
    1.18 +  // Create and configure the HTTP server.
    1.19 +  testserver = new HttpServer();
    1.20 +  testserver.registerDirectory("/data/", do_get_file("data"));
    1.21 +  testserver.start(4444);
    1.22 +
    1.23 +  do_test_pending();
    1.24 +  run_test_1();
    1.25 +}
    1.26 +
    1.27 +function end_test() {
    1.28 +  testserver.stop(do_test_finished);
    1.29 +}
    1.30 +
    1.31 +// Test that a basic update check returns the expected available updates
    1.32 +function run_test_1() {
    1.33 +  AddonUpdateChecker.checkForUpdates("updatecheck1@tests.mozilla.org", null,
    1.34 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
    1.35 +    onUpdateCheckComplete: function(updates) {
    1.36 +      check_test_1(updates);
    1.37 +    },
    1.38 +
    1.39 +    onUpdateCheckError: function(status) {
    1.40 +      do_throw("Update check failed with status " + status);
    1.41 +    }
    1.42 +  });
    1.43 +}
    1.44 +
    1.45 +function check_test_1(updates) {
    1.46 +  do_check_eq(updates.length, 5);
    1.47 +  let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates);
    1.48 +  do_check_neq(update, null);
    1.49 +  do_check_eq(update.version, 3);
    1.50 +  update = AddonUpdateChecker.getCompatibilityUpdate(updates, "2");
    1.51 +  do_check_neq(update, null);
    1.52 +  do_check_eq(update.version, 2);
    1.53 +  do_check_eq(update.targetApplications[0].minVersion, 1);
    1.54 +  do_check_eq(update.targetApplications[0].maxVersion, 2);
    1.55 +
    1.56 +  run_test_2();
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * Tests that the security checks are applied correctly
    1.61 + *
    1.62 + * Test     signature   updateHash  updateLink   expected
    1.63 + *--------------------------------------------------------
    1.64 + * 2        absent      absent      http         fail
    1.65 + * 3        broken      absent      http         fail
    1.66 + * 4        correct     absent      http         no update
    1.67 + * 5        correct     sha1        http         update
    1.68 + * 6        corrent     absent      https        update
    1.69 + * 7        corrent     sha1        https        update
    1.70 + * 8        corrent     md2         http         no update
    1.71 + * 9        corrent     md2         https        update
    1.72 + */
    1.73 +
    1.74 +let updateKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK426erD/H3XtsjvaB5+PJqbhj" +
    1.75 +                "Zc9EDI5OCJS8R3FIObJ9ZHJK1TXeaE7JWqt9WUmBWTEFvwS+FI9vWu8058N9CHhD" +
    1.76 +                "NyeP6i4LuUYjTURnn7Yw/IgzyIJ2oKsYa32RuxAyteqAWqPT/J63wBixIeCxmysf" +
    1.77 +                "awB/zH4KaPiY3vnrzQIDAQAB";
    1.78 +
    1.79 +function run_test_2() {
    1.80 +  AddonUpdateChecker.checkForUpdates("test_bug378216_5@tests.mozilla.org",
    1.81 +                                     updateKey,
    1.82 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
    1.83 +    onUpdateCheckComplete: function(updates) {
    1.84 +      do_throw("Expected the update check to fail");
    1.85 +    },
    1.86 +
    1.87 +    onUpdateCheckError: function(status) {
    1.88 +      run_test_3();
    1.89 +    }
    1.90 +  });
    1.91 +}
    1.92 +
    1.93 +function run_test_3() {
    1.94 +  AddonUpdateChecker.checkForUpdates("test_bug378216_7@tests.mozilla.org",
    1.95 +                                     updateKey,
    1.96 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
    1.97 +    onUpdateCheckComplete: function(updates) {
    1.98 +      do_throw("Expected the update check to fail");
    1.99 +    },
   1.100 +
   1.101 +    onUpdateCheckError: function(status) {
   1.102 +      run_test_4();
   1.103 +    }
   1.104 +  });
   1.105 +}
   1.106 +
   1.107 +function run_test_4() {
   1.108 +  AddonUpdateChecker.checkForUpdates("test_bug378216_8@tests.mozilla.org",
   1.109 +                                     updateKey,
   1.110 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.111 +    onUpdateCheckComplete: function(updates) {
   1.112 +      do_check_eq(updates.length, 1);
   1.113 +      do_check_false("updateURL" in updates[0]);
   1.114 +      run_test_5();
   1.115 +    },
   1.116 +
   1.117 +    onUpdateCheckError: function(status) {
   1.118 +      do_throw("Update check failed with status " + status);
   1.119 +    }
   1.120 +  });
   1.121 +}
   1.122 +
   1.123 +function run_test_5() {
   1.124 +  AddonUpdateChecker.checkForUpdates("test_bug378216_9@tests.mozilla.org",
   1.125 +                                     updateKey,
   1.126 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.127 +    onUpdateCheckComplete: function(updates) {
   1.128 +      do_check_eq(updates.length, 1);
   1.129 +      do_check_eq(updates[0].version, "2.0");
   1.130 +      do_check_true("updateURL" in updates[0]);
   1.131 +      run_test_6();
   1.132 +    },
   1.133 +
   1.134 +    onUpdateCheckError: function(status) {
   1.135 +      do_throw("Update check failed with status " + status);
   1.136 +    }
   1.137 +  });
   1.138 +}
   1.139 +
   1.140 +function run_test_6() {
   1.141 +  AddonUpdateChecker.checkForUpdates("test_bug378216_10@tests.mozilla.org",
   1.142 +                                     updateKey,
   1.143 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.144 +    onUpdateCheckComplete: function(updates) {
   1.145 +      do_check_eq(updates.length, 1);
   1.146 +      do_check_eq(updates[0].version, "2.0");
   1.147 +      do_check_true("updateURL" in updates[0]);
   1.148 +      run_test_7();
   1.149 +    },
   1.150 +
   1.151 +    onUpdateCheckError: function(status) {
   1.152 +      do_throw("Update check failed with status " + status);
   1.153 +    }
   1.154 +  });
   1.155 +}
   1.156 +
   1.157 +function run_test_7() {
   1.158 +  AddonUpdateChecker.checkForUpdates("test_bug378216_11@tests.mozilla.org",
   1.159 +                                     updateKey,
   1.160 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.161 +    onUpdateCheckComplete: function(updates) {
   1.162 +      do_check_eq(updates.length, 1);
   1.163 +      do_check_eq(updates[0].version, "2.0");
   1.164 +      do_check_true("updateURL" in updates[0]);
   1.165 +      run_test_8();
   1.166 +    },
   1.167 +
   1.168 +    onUpdateCheckError: function(status) {
   1.169 +      do_throw("Update check failed with status " + status);
   1.170 +    }
   1.171 +  });
   1.172 +}
   1.173 +
   1.174 +function run_test_8() {
   1.175 +  AddonUpdateChecker.checkForUpdates("test_bug378216_12@tests.mozilla.org",
   1.176 +                                     updateKey,
   1.177 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.178 +    onUpdateCheckComplete: function(updates) {
   1.179 +      do_check_eq(updates.length, 1);
   1.180 +      do_check_false("updateURL" in updates[0]);
   1.181 +      run_test_9();
   1.182 +    },
   1.183 +
   1.184 +    onUpdateCheckError: function(status) {
   1.185 +      do_throw("Update check failed with status " + status);
   1.186 +    }
   1.187 +  });
   1.188 +}
   1.189 +
   1.190 +function run_test_9() {
   1.191 +  AddonUpdateChecker.checkForUpdates("test_bug378216_13@tests.mozilla.org",
   1.192 +                                     updateKey,
   1.193 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.194 +    onUpdateCheckComplete: function(updates) {
   1.195 +      do_check_eq(updates.length, 1);
   1.196 +      do_check_eq(updates[0].version, "2.0");
   1.197 +      do_check_true("updateURL" in updates[0]);
   1.198 +      run_test_10();
   1.199 +    },
   1.200 +
   1.201 +    onUpdateCheckError: function(status) {
   1.202 +      do_throw("Update check failed with status " + status);
   1.203 +    }
   1.204 +  });
   1.205 +}
   1.206 +
   1.207 +function run_test_10() {
   1.208 +  AddonUpdateChecker.checkForUpdates("test_bug378216_14@tests.mozilla.org",
   1.209 +                                     null,
   1.210 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.211 +    onUpdateCheckComplete: function(updates) {
   1.212 +      do_check_eq(updates.length, 0);
   1.213 +      run_test_11();
   1.214 +    },
   1.215 +
   1.216 +    onUpdateCheckError: function(status) {
   1.217 +      do_throw("Update check failed with status " + status);
   1.218 +    }
   1.219 +  });
   1.220 +}
   1.221 +
   1.222 +function run_test_11() {
   1.223 +  AddonUpdateChecker.checkForUpdates("test_bug378216_15@tests.mozilla.org",
   1.224 +                                     null,
   1.225 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.226 +    onUpdateCheckComplete: function(updates) {
   1.227 +      do_throw("Update check should have failed");
   1.228 +    },
   1.229 +
   1.230 +    onUpdateCheckError: function(status) {
   1.231 +      do_check_eq(status, AddonUpdateChecker.ERROR_PARSE_ERROR);
   1.232 +      run_test_12();
   1.233 +    }
   1.234 +  });
   1.235 +}
   1.236 +
   1.237 +function run_test_12() {
   1.238 +  AddonUpdateChecker.checkForUpdates("ignore-compat@tests.mozilla.org",
   1.239 +                                     null,
   1.240 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.241 +    onUpdateCheckComplete: function(updates) {
   1.242 +      do_check_eq(updates.length, 3);
   1.243 +      let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates,
   1.244 +                                                                null,
   1.245 +                                                                null,
   1.246 +                                                                true);
   1.247 +      do_check_neq(update, null);
   1.248 +      do_check_eq(update.version, 2);
   1.249 +      run_test_13();
   1.250 +    },
   1.251 +
   1.252 +    onUpdateCheckError: function(status) {
   1.253 +      do_throw("Update check failed with status " + status);
   1.254 +    }
   1.255 +  });
   1.256 +}
   1.257 +
   1.258 +function run_test_13() {
   1.259 +  AddonUpdateChecker.checkForUpdates("compat-override@tests.mozilla.org",
   1.260 +                                     null,
   1.261 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.262 +    onUpdateCheckComplete: function(updates) {
   1.263 +      do_check_eq(updates.length, 3);
   1.264 +      let overrides = [{
   1.265 +        type: "incompatible",
   1.266 +        minVersion: 1,
   1.267 +        maxVersion: 2,
   1.268 +        appID: "xpcshell@tests.mozilla.org",
   1.269 +        appMinVersion: 0.1,
   1.270 +        appMaxVersion: 0.2
   1.271 +      }, {
   1.272 +        type: "incompatible",
   1.273 +        minVersion: 2,
   1.274 +        maxVersion: 2,
   1.275 +        appID: "xpcshell@tests.mozilla.org",
   1.276 +        appMinVersion: 1,
   1.277 +        appMaxVersion: 2
   1.278 +      }];
   1.279 +      let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates,
   1.280 +                                                                null,
   1.281 +                                                                null,
   1.282 +                                                                true,
   1.283 +                                                                false,
   1.284 +                                                                overrides);
   1.285 +      do_check_neq(update, null);
   1.286 +      do_check_eq(update.version, 1);
   1.287 +      run_test_14();
   1.288 +    },
   1.289 +
   1.290 +    onUpdateCheckError: function(status) {
   1.291 +      do_throw("Update check failed with status " + status);
   1.292 +    }
   1.293 +  });
   1.294 +}
   1.295 +
   1.296 +function run_test_14() {
   1.297 +  AddonUpdateChecker.checkForUpdates("compat-strict-optin@tests.mozilla.org",
   1.298 +                                     null,
   1.299 +                                     "http://localhost:4444/data/test_updatecheck.rdf", {
   1.300 +    onUpdateCheckComplete: function(updates) {
   1.301 +      do_check_eq(updates.length, 1);
   1.302 +      let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates,
   1.303 +                                                                null,
   1.304 +                                                                null,
   1.305 +                                                                true,
   1.306 +                                                                false);
   1.307 +      do_check_eq(update, null);
   1.308 +      end_test();
   1.309 +    },
   1.310 +
   1.311 +    onUpdateCheckError: function(status) {
   1.312 +      do_throw("Update check failed with status " + status);
   1.313 +    }
   1.314 +  });
   1.315 +}

mercurial