toolkit/mozapps/update/tests/unit_aus_update/urlConstruction.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/update/tests/unit_aus_update/urlConstruction.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,408 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +
     1.9 +/* General URL Construction Tests */
    1.10 +
    1.11 +Components.utils.import("resource://gre/modules/ctypes.jsm")
    1.12 +
    1.13 +const URL_PREFIX = URL_HOST + "/";
    1.14 +
    1.15 +var gAppInfo;
    1.16 +
    1.17 +function run_test() {
    1.18 +  // This test needs access to omni.ja to read the update.locale file so don't
    1.19 +  // use a custom directory for the application directory.
    1.20 +  gUseTestAppDir = false;
    1.21 +  setupTestCommon();
    1.22 +
    1.23 +  // The mock XMLHttpRequest is MUCH faster
    1.24 +  overrideXHR(callHandleEvent);
    1.25 +  standardInit();
    1.26 +  gAppInfo = AUS_Cc["@mozilla.org/xre/app-info;1"].
    1.27 +             getService(AUS_Ci.nsIXULAppInfo).
    1.28 +             QueryInterface(AUS_Ci.nsIXULRuntime);
    1.29 +  do_execute_soon(run_test_pt1);
    1.30 +}
    1.31 +
    1.32 +// Callback function used by the custom XMLHttpRequest implementation to
    1.33 +// call the nsIDOMEventListener's handleEvent method for onload.
    1.34 +function callHandleEvent() {
    1.35 +  var e = { target: gXHR };
    1.36 +  gXHR.onload(e);
    1.37 +}
    1.38 +
    1.39 +// Helper function for parsing the result from the contructed url
    1.40 +function getResult(url) {
    1.41 +  return url.substr(URL_PREFIX.length).split("/")[0];
    1.42 +}
    1.43 +
    1.44 +// url constructed with %PRODUCT%
    1.45 +function run_test_pt1() {
    1.46 +  gCheckFunc = check_test_pt1;
    1.47 +  var url = URL_PREFIX + "%PRODUCT%/";
    1.48 +  logTestInfo("testing url constructed with %PRODUCT% - " + url);
    1.49 +  setUpdateURLOverride(url);
    1.50 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
    1.51 +}
    1.52 +
    1.53 +function check_test_pt1() {
    1.54 +  do_check_eq(getResult(gRequestURL), gAppInfo.name);
    1.55 +  run_test_pt2();
    1.56 +}
    1.57 +
    1.58 +// url constructed with %VERSION%
    1.59 +function run_test_pt2() {
    1.60 +  gCheckFunc = check_test_pt2;
    1.61 +  var url = URL_PREFIX + "%VERSION%/";
    1.62 +  logTestInfo("testing url constructed with %VERSION% - " + url);
    1.63 +  setUpdateURLOverride(url);
    1.64 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
    1.65 +}
    1.66 +
    1.67 +function check_test_pt2() {
    1.68 +  do_check_eq(getResult(gRequestURL), gAppInfo.version);
    1.69 +  run_test_pt3();
    1.70 +}
    1.71 +
    1.72 +// url constructed with %BUILD_ID%
    1.73 +function run_test_pt3() {
    1.74 +  gCheckFunc = check_test_pt3;
    1.75 +  var url = URL_PREFIX + "%BUILD_ID%/";
    1.76 +  logTestInfo("testing url constructed with %BUILD_ID% - " + url);
    1.77 +  setUpdateURLOverride(url);
    1.78 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
    1.79 +}
    1.80 +
    1.81 +function check_test_pt3() {
    1.82 +  do_check_eq(getResult(gRequestURL), gAppInfo.appBuildID);
    1.83 +  run_test_pt4();
    1.84 +}
    1.85 +
    1.86 +// url constructed with %BUILD_TARGET%
    1.87 +// XXX TODO - it might be nice if we tested the actual ABI
    1.88 +function run_test_pt4() {
    1.89 +  gCheckFunc = check_test_pt4;
    1.90 +  var url = URL_PREFIX + "%BUILD_TARGET%/";
    1.91 +  logTestInfo("testing url constructed with %BUILD_TARGET% - " + url);
    1.92 +  setUpdateURLOverride(url);
    1.93 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
    1.94 +}
    1.95 +
    1.96 +function check_test_pt4() {
    1.97 +  var abi;
    1.98 +  try {
    1.99 +    abi = gAppInfo.XPCOMABI;
   1.100 +  } catch (e) {
   1.101 +    do_throw("nsIXULAppInfo:XPCOMABI not defined\n");
   1.102 +  }
   1.103 +
   1.104 +  if (IS_MACOSX) {
   1.105 +    // Mac universal build should report a different ABI than either macppc
   1.106 +    // or mactel. This is necessary since nsUpdateService.js will set the ABI to
   1.107 +    // Universal-gcc3 for Mac universal builds.
   1.108 +    var macutils = AUS_Cc["@mozilla.org/xpcom/mac-utils;1"].
   1.109 +                   getService(AUS_Ci.nsIMacUtils);
   1.110 +
   1.111 +    if (macutils.isUniversalBinary)
   1.112 +      abi += "-u-" + macutils.architecturesInBinary;
   1.113 +    if (IS_SHARK) {
   1.114 +      // Disambiguate optimised and shark nightlies
   1.115 +      abi += "-shark"
   1.116 +    }
   1.117 +
   1.118 +  }
   1.119 +
   1.120 +  do_check_eq(getResult(gRequestURL), gAppInfo.OS + "_" + abi);
   1.121 +  run_test_pt5();
   1.122 +}
   1.123 +
   1.124 +// url constructed with %LOCALE%
   1.125 +// Bug 488936 added the update.locale file that stores the update locale
   1.126 +function run_test_pt5() {
   1.127 +  gCheckFunc = check_test_pt5;
   1.128 +  var url = URL_PREFIX + "%LOCALE%/";
   1.129 +  logTestInfo("testing url constructed with %LOCALE% - " + url);
   1.130 +  setUpdateURLOverride(url);
   1.131 +  try {
   1.132 +    gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.133 +  } catch (e) {
   1.134 +    logTestInfo("The following error is most likely due to a missing " +
   1.135 +                "update.locale file");
   1.136 +    do_throw(e);
   1.137 +  }
   1.138 +}
   1.139 +
   1.140 +function check_test_pt5() {
   1.141 +  do_check_eq(getResult(gRequestURL), INSTALL_LOCALE);
   1.142 +  run_test_pt6();
   1.143 +}
   1.144 +
   1.145 +// url constructed with %CHANNEL%
   1.146 +function run_test_pt6() {
   1.147 +  gCheckFunc = check_test_pt6;
   1.148 +  var url = URL_PREFIX + "%CHANNEL%/";
   1.149 +  logTestInfo("testing url constructed with %CHANNEL% - " + url);
   1.150 +  setUpdateURLOverride(url);
   1.151 +  setUpdateChannel("test_channel");
   1.152 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.153 +}
   1.154 +
   1.155 +function check_test_pt6() {
   1.156 +  do_check_eq(getResult(gRequestURL), "test_channel");
   1.157 +  run_test_pt7();
   1.158 +}
   1.159 +
   1.160 +// url constructed with %CHANNEL% with distribution partners
   1.161 +function run_test_pt7() {
   1.162 +  gCheckFunc = check_test_pt7;
   1.163 +  var url = URL_PREFIX + "%CHANNEL%/";
   1.164 +  logTestInfo("testing url constructed with %CHANNEL% - " + url);
   1.165 +  setUpdateURLOverride(url);
   1.166 +  gDefaultPrefBranch.setCharPref(PREF_APP_PARTNER_BRANCH + "test_partner1", "test_partner1");
   1.167 +  gDefaultPrefBranch.setCharPref(PREF_APP_PARTNER_BRANCH + "test_partner2", "test_partner2");
   1.168 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.169 +}
   1.170 +
   1.171 +function check_test_pt7() {
   1.172 +  do_check_eq(getResult(gRequestURL), "test_channel-cck-test_partner1-test_partner2");
   1.173 +  run_test_pt8();
   1.174 +}
   1.175 +
   1.176 +// url constructed with %PLATFORM_VERSION%
   1.177 +function run_test_pt8() {
   1.178 +  gCheckFunc = check_test_pt8;
   1.179 +  var url = URL_PREFIX + "%PLATFORM_VERSION%/";
   1.180 +  logTestInfo("testing url constructed with %PLATFORM_VERSION% - " + url);
   1.181 +  setUpdateURLOverride(url);
   1.182 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.183 +}
   1.184 +
   1.185 +function check_test_pt8() {
   1.186 +  do_check_eq(getResult(gRequestURL), gAppInfo.platformVersion);
   1.187 +  run_test_pt9();
   1.188 +}
   1.189 +
   1.190 +// url constructed with %OS_VERSION%
   1.191 +function run_test_pt9() {
   1.192 +  gCheckFunc = check_test_pt9;
   1.193 +  var url = URL_PREFIX + "%OS_VERSION%/";
   1.194 +  logTestInfo("testing url constructed with %OS_VERSION% - " + url);
   1.195 +  setUpdateURLOverride(url);
   1.196 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.197 +}
   1.198 +
   1.199 +function getServicePack() {
   1.200 +  // NOTE: This function is a helper function and not a test.  Thus,
   1.201 +  // it uses throw() instead of do_throw().  Any tests that use this function
   1.202 +  // should catch exceptions thrown in this function and deal with them
   1.203 +  // appropriately (usually by calling do_throw).
   1.204 +  const BYTE = ctypes.uint8_t;
   1.205 +  const WORD = ctypes.uint16_t;
   1.206 +  const DWORD = ctypes.uint32_t;
   1.207 +  const WCHAR = ctypes.jschar;
   1.208 +  const BOOL = ctypes.int;
   1.209 +
   1.210 +  // This structure is described at:
   1.211 +  // http://msdn.microsoft.com/en-us/library/ms724833%28v=vs.85%29.aspx
   1.212 +  const SZCSDVERSIONLENGTH = 128;
   1.213 +  const OSVERSIONINFOEXW = new ctypes.StructType('OSVERSIONINFOEXW',
   1.214 +      [
   1.215 +      {dwOSVersionInfoSize: DWORD},
   1.216 +      {dwMajorVersion: DWORD},
   1.217 +      {dwMinorVersion: DWORD},
   1.218 +      {dwBuildNumber: DWORD},
   1.219 +      {dwPlatformId: DWORD},
   1.220 +      {szCSDVersion: ctypes.ArrayType(WCHAR, SZCSDVERSIONLENGTH)},
   1.221 +      {wServicePackMajor: WORD},
   1.222 +      {wServicePackMinor: WORD},
   1.223 +      {wSuiteMask: WORD},
   1.224 +      {wProductType: BYTE},
   1.225 +      {wReserved: BYTE}
   1.226 +      ]);
   1.227 +
   1.228 +  let kernel32 = ctypes.open("kernel32");
   1.229 +  try {
   1.230 +    let GetVersionEx = kernel32.declare("GetVersionExW",
   1.231 +                                        ctypes.default_abi,
   1.232 +                                        BOOL,
   1.233 +                                        OSVERSIONINFOEXW.ptr);
   1.234 +    let winVer = OSVERSIONINFOEXW();
   1.235 +    winVer.dwOSVersionInfoSize = OSVERSIONINFOEXW.size;
   1.236 +
   1.237 +    if(0 === GetVersionEx(winVer.address())) {
   1.238 +      // Using "throw" instead of "do_throw" (see NOTE above)
   1.239 +      throw("Failure in GetVersionEx (returned 0)");
   1.240 +    }
   1.241 +
   1.242 +    return winVer.wServicePackMajor + "." + winVer.wServicePackMinor;
   1.243 +  } finally {
   1.244 +    kernel32.close();
   1.245 +  }
   1.246 +}
   1.247 +
   1.248 +function getProcArchitecture() {
   1.249 +  // NOTE: This function is a helper function and not a test.  Thus,
   1.250 +  // it uses throw() instead of do_throw().  Any tests that use this function
   1.251 +  // should catch exceptions thrown in this function and deal with them
   1.252 +  // appropriately (usually by calling do_throw).
   1.253 +  const WORD = ctypes.uint16_t;
   1.254 +  const DWORD = ctypes.uint32_t;
   1.255 +
   1.256 +  // This structure is described at:
   1.257 +  // http://msdn.microsoft.com/en-us/library/ms724958%28v=vs.85%29.aspx
   1.258 +  const SYSTEM_INFO = new ctypes.StructType('SYSTEM_INFO',
   1.259 +      [
   1.260 +      {wProcessorArchitecture: WORD},
   1.261 +      {wReserved: WORD},
   1.262 +      {dwPageSize: DWORD},
   1.263 +      {lpMinimumApplicationAddress: ctypes.voidptr_t},
   1.264 +      {lpMaximumApplicationAddress: ctypes.voidptr_t},
   1.265 +      {dwActiveProcessorMask: DWORD.ptr},
   1.266 +      {dwNumberOfProcessors: DWORD},
   1.267 +      {dwProcessorType: DWORD},
   1.268 +      {dwAllocationGranularity: DWORD},
   1.269 +      {wProcessorLevel: WORD},
   1.270 +      {wProcessorRevision: WORD}
   1.271 +      ]);
   1.272 +
   1.273 +  let kernel32 = ctypes.open("kernel32");
   1.274 +  try {
   1.275 +    let GetNativeSystemInfo = kernel32.declare("GetNativeSystemInfo",
   1.276 +                                               ctypes.default_abi,
   1.277 +                                               ctypes.void_t,
   1.278 +                                               SYSTEM_INFO.ptr);
   1.279 +    let sysInfo = SYSTEM_INFO();
   1.280 +    // Default to unknown
   1.281 +    sysInfo.wProcessorArchitecture = 0xffff;
   1.282 +
   1.283 +    GetNativeSystemInfo(sysInfo.address());
   1.284 +    switch(sysInfo.wProcessorArchitecture) {
   1.285 +      case 9:
   1.286 +        return "x64";
   1.287 +      case 6:
   1.288 +        return "IA64";
   1.289 +      case 0:
   1.290 +        return "x86";
   1.291 +      default:
   1.292 +        // Using "throw" instead of "do_throw" (see NOTE above)
   1.293 +        throw("Unknown architecture returned from GetNativeSystemInfo: " + sysInfo.wProcessorArchitecture);
   1.294 +    }
   1.295 +  } finally {
   1.296 +    kernel32.close();
   1.297 +  }
   1.298 +}
   1.299 +
   1.300 +function check_test_pt9() {
   1.301 +  var osVersion;
   1.302 +  var sysInfo = AUS_Cc["@mozilla.org/system-info;1"].
   1.303 +                getService(AUS_Ci.nsIPropertyBag2);
   1.304 +  osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
   1.305 +
   1.306 +  if(IS_WIN) {
   1.307 +    try {
   1.308 +      let servicePack = getServicePack();
   1.309 +      osVersion += "." + servicePack;
   1.310 +    } catch (e) {
   1.311 +      do_throw("Failure obtaining service pack: " + e);
   1.312 +    }
   1.313 +
   1.314 +    if("5.0" === sysInfo.getProperty("version")) { // Win2K
   1.315 +      osVersion += " (unknown)";
   1.316 +    } else {
   1.317 +      try {
   1.318 +        osVersion += " (" + getProcArchitecture() + ")";
   1.319 +      } catch (e) {
   1.320 +        do_throw("Failed to obtain processor architecture: " + e);
   1.321 +      }
   1.322 +    }
   1.323 +  }
   1.324 +
   1.325 +  if (osVersion) {
   1.326 +    try {
   1.327 +      osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
   1.328 +    } catch (e) {
   1.329 +      // Not all platforms have a secondary widget library, so an error is
   1.330 +      // nothing to worry about.
   1.331 +    }
   1.332 +    osVersion = encodeURIComponent(osVersion);
   1.333 +  }
   1.334 +
   1.335 +  do_check_eq(getResult(gRequestURL), osVersion);
   1.336 +  run_test_pt10();
   1.337 +}
   1.338 +
   1.339 +// url constructed with %DISTRIBUTION%
   1.340 +function run_test_pt10() {
   1.341 +  gCheckFunc = check_test_pt10;
   1.342 +  var url = URL_PREFIX + "%DISTRIBUTION%/";
   1.343 +  logTestInfo("testing url constructed with %DISTRIBUTION% - " + url);
   1.344 +  setUpdateURLOverride(url);
   1.345 +  gDefaultPrefBranch.setCharPref(PREF_DISTRIBUTION_ID, "test_distro");
   1.346 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.347 +}
   1.348 +
   1.349 +function check_test_pt10() {
   1.350 +  do_check_eq(getResult(gRequestURL), "test_distro");
   1.351 +  run_test_pt11();
   1.352 +}
   1.353 +
   1.354 +// url constructed with %DISTRIBUTION_VERSION%
   1.355 +function run_test_pt11() {
   1.356 +  gCheckFunc = check_test_pt11;
   1.357 +  var url = URL_PREFIX + "%DISTRIBUTION_VERSION%/";
   1.358 +  logTestInfo("testing url constructed with %DISTRIBUTION_VERSION% - " + url);
   1.359 +  setUpdateURLOverride(url);
   1.360 +  gDefaultPrefBranch.setCharPref(PREF_DISTRIBUTION_VERSION, "test_distro_version");
   1.361 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.362 +}
   1.363 +
   1.364 +function check_test_pt11() {
   1.365 +  do_check_eq(getResult(gRequestURL), "test_distro_version");
   1.366 +  run_test_pt12();
   1.367 +}
   1.368 +
   1.369 +// url with force param that doesn't already have a param - bug 454357
   1.370 +function run_test_pt12() {
   1.371 +  gCheckFunc = check_test_pt12;
   1.372 +  var url = URL_PREFIX;
   1.373 +  logTestInfo("testing url with force param that doesn't already have a " +
   1.374 +              "param - " + url);
   1.375 +  setUpdateURLOverride(url);
   1.376 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.377 +}
   1.378 +
   1.379 +function check_test_pt12() {
   1.380 +  do_check_eq(getResult(gRequestURL), "?force=1");
   1.381 +  run_test_pt13();
   1.382 +}
   1.383 +
   1.384 +// url with force param that already has a param - bug 454357
   1.385 +function run_test_pt13() {
   1.386 +  gCheckFunc = check_test_pt13;
   1.387 +  var url = URL_PREFIX + "?extra=param";
   1.388 +  logTestInfo("testing url with force param that already has a param - " + url);
   1.389 +  logTestInfo("testing url constructed that has a parameter - " + url);
   1.390 +  setUpdateURLOverride(url);
   1.391 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.392 +}
   1.393 +
   1.394 +function check_test_pt13() {
   1.395 +  do_check_eq(getResult(gRequestURL), "?extra=param&force=1");
   1.396 +  run_test_pt14();
   1.397 +}
   1.398 +
   1.399 +function run_test_pt14() {
   1.400 +  Services.prefs.setCharPref("app.update.custom", "custom");
   1.401 +  gCheckFunc = check_test_pt14;
   1.402 +  var url = URL_PREFIX + "?custom=%CUSTOM%";
   1.403 +  logTestInfo("testing url constructed with %CUSTOM% - " + url);
   1.404 +  setUpdateURLOverride(url);
   1.405 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
   1.406 +}
   1.407 +
   1.408 +function check_test_pt14() {
   1.409 +  do_check_eq(getResult(gRequestURL), "?custom=custom&force=1");
   1.410 +  doTestFinish();
   1.411 +}

mercurial