toolkit/mozapps/update/tests/unit_aus_update/updateRootDirMigration_win.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/updateRootDirMigration_win.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     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 +const PREF_APP_UPDATE_MIGRATE_APP_DIR = "app.update.migrated.updateDir";
    1.10 +
    1.11 +function clearTaskbarIDHash(exePath, appInfoName) {
    1.12 +  let registry = AUS_Cc["@mozilla.org/windows-registry-key;1"].
    1.13 +                 createInstance(AUS_Ci.nsIWindowsRegKey);
    1.14 +  try {
    1.15 +    registry.open(AUS_Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    1.16 +                  "Software\\Mozilla\\" + appInfoName + "\\TaskBarIDs",
    1.17 +                  AUS_Ci.nsIWindowsRegKey.ACCESS_ALL);
    1.18 +    registry.removeValue(exePath);
    1.19 +  } catch (e) {
    1.20 +  }
    1.21 +  finally {
    1.22 +    registry.close();
    1.23 +  }
    1.24 +}
    1.25 +
    1.26 +function setTaskbarIDHash(exePath, hash, appInfoName) {
    1.27 +  let registry = AUS_Cc["@mozilla.org/windows-registry-key;1"].
    1.28 +                 createInstance(AUS_Ci.nsIWindowsRegKey);
    1.29 +  try {
    1.30 +    registry.create(AUS_Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    1.31 +                    "Software\\Mozilla\\" + appInfoName + "\\TaskBarIDs",
    1.32 +                    AUS_Ci.nsIWindowsRegKey.ACCESS_WRITE);
    1.33 +    registry.writeStringValue(exePath, hash);
    1.34 +  } catch (e) {
    1.35 +  }
    1.36 +  finally {
    1.37 +    registry.close();
    1.38 +  }
    1.39 +};
    1.40 +
    1.41 +function getMigrated() {
    1.42 +  var migrated = 0;
    1.43 +  try {
    1.44 +    migrated = Services.prefs.getBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR);
    1.45 +  } catch (e) {
    1.46 +  }
    1.47 +  return migrated;
    1.48 +}
    1.49 +
    1.50 +/* General Update Manager Tests */
    1.51 +
    1.52 +function run_test() {
    1.53 +  setupTestCommon();
    1.54 +
    1.55 +  standardInit();
    1.56 +
    1.57 +  var appinfo = AUS_Cc["@mozilla.org/xre/app-info;1"].
    1.58 +                getService(AUS_Ci.nsIXULAppInfo).
    1.59 +                QueryInterface(AUS_Ci.nsIXULRuntime);
    1.60 +
    1.61 +   // Obtain the old update root leaf
    1.62 +  var updateLeafName;
    1.63 +  var exeFile = FileUtils.getFile(XRE_EXECUTABLE_FILE, []);
    1.64 +  var programFiles = FileUtils.getFile("ProgF", []);
    1.65 +  if (exeFile.path.substring(0, programFiles.path.length).toLowerCase() ==
    1.66 +      programFiles.path.toLowerCase()) {
    1.67 +    updateLeafName = exeFile.parent.leafName;
    1.68 +  } else {
    1.69 +    updateLeafName = appinfo.name;
    1.70 +  }
    1.71 +
    1.72 +  // Obtain the old update root
    1.73 +  var oldUpdateRoot;
    1.74 +  if (appinfo.vendor) {
    1.75 +    oldUpdateRoot = FileUtils.getDir("LocalAppData", [appinfo.vendor,
    1.76 +                                                      appinfo.name,
    1.77 +                                                      updateLeafName], false);
    1.78 +  } else {
    1.79 +    oldUpdateRoot = FileUtils.getDir("LocalAppData", [appinfo.name,
    1.80 +                                                      updateLeafName], false);
    1.81 +  }
    1.82 +  // Obtain the new update root
    1.83 +  var newUpdateRoot = FileUtils.getDir("UpdRootD", [], false);
    1.84 +
    1.85 +  ///////////////////////////////////////////////////////////
    1.86 +  // Setting a taskbar ID without the old update dir existing should set the
    1.87 +  // pref so a migration isn't retried.
    1.88 +
    1.89 +  // Remove the old and new update root directories
    1.90 +  try {
    1.91 +    oldUpdateRoot.remove(true);
    1.92 +  } catch (e) {
    1.93 +  }
    1.94 +  try {
    1.95 +    newUpdateRoot.remove(true);
    1.96 +  } catch (e) {
    1.97 +  }
    1.98 +  Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false);
    1.99 +  setTaskbarIDHash(exeFile.parent.path, "AAAAAAAA", appinfo.name);
   1.100 +  initUpdateServiceStub();
   1.101 +  do_check_eq(getMigrated(), 1);
   1.102 +
   1.103 +  ///////////////////////////////////////////////////////////
   1.104 +  // An application without a taskbar ID should bail early without a pref
   1.105 +  // being set, so that if a taskbar is set for the application, the migration
   1.106 +  // will be retried.
   1.107 +
   1.108 +  Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false);
   1.109 +  clearTaskbarIDHash(exeFile.parent.path, appinfo.name);
   1.110 +  initUpdateServiceStub();
   1.111 +  do_check_eq(getMigrated(), 0);
   1.112 +
   1.113 +  ///////////////////////////////////////////////////////////
   1.114 +  // Migrating files should work
   1.115 +
   1.116 +  Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false);
   1.117 +  setTaskbarIDHash(exeFile.parent.path, "AAAAAAAA", appinfo.name);
   1.118 +  var oldUpdateDirs = oldUpdateRoot.clone();
   1.119 +  oldUpdateDirs.append("updates");
   1.120 +  oldUpdateDirs.append("0");
   1.121 +  oldUpdateDirs.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
   1.122 +
   1.123 +  // Get an array of all of the files we want to migrate.
   1.124 +  // We do this to create them in the old update directory.
   1.125 +  var filesToMigrate = [FILE_UPDATES_DB, FILE_UPDATE_ACTIVE,
   1.126 +                        ["updates", FILE_LAST_LOG], ["updates", FILE_BACKUP_LOG],
   1.127 +                        ["updates", "0", FILE_UPDATE_STATUS]];
   1.128 +  // Move each of those files to the new directory
   1.129 +  filesToMigrate.forEach(relPath => {
   1.130 +    let oldFile = oldUpdateRoot.clone();
   1.131 +    if (relPath instanceof Array) {
   1.132 +      relPath.forEach(relPathPart => {
   1.133 +        oldFile.append(relPathPart);
   1.134 +      });
   1.135 +    } else {
   1.136 +      oldFile.append(relPath);
   1.137 +    }
   1.138 +    oldFile.create(AUS_Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
   1.139 +  });
   1.140 +  // Do the migration
   1.141 +  initUpdateServiceStub();
   1.142 +  doTestFinish();
   1.143 +  return;
   1.144 +  // Now verify that each of the files exist in the new update directory
   1.145 +  filesToMigrate.forEach(relPath => {
   1.146 +    let newFile = newUpdateRoot.clone();
   1.147 +    let oldFile = oldUpdateRoot.clone();
   1.148 +    if (relPath instanceof Array) {
   1.149 +      relPath.forEach(relPathPart => {
   1.150 +        newFile.append(relPathPart);
   1.151 +        oldFile.append(relPathPart);
   1.152 +      });
   1.153 +    } else {
   1.154 +      newFile.append(relPath);
   1.155 +      oldFile.append(relPath);
   1.156 +    }
   1.157 +    // The file should be mimgrated, except for FILE_UPDATE_STATUS
   1.158 +    // which gets consumed by post update after it is migrated..
   1.159 +    if (newFile.leafName != FILE_UPDATE_STATUS) {
   1.160 +      do_check_true(newFile.exists());
   1.161 +    }
   1.162 +    do_check_false(oldFile.exists());
   1.163 +  });
   1.164 +
   1.165 +  doTestFinish();
   1.166 +}
   1.167 +
   1.168 +function end_test() {
   1.169 +  var appinfo = AUS_Cc["@mozilla.org/xre/app-info;1"].
   1.170 +                getService(AUS_Ci.nsIXULAppInfo).
   1.171 +                QueryInterface(AUS_Ci.nsIXULRuntime);
   1.172 +  var exeFile = FileUtils.getFile(XRE_EXECUTABLE_FILE, []);
   1.173 +  clearTaskbarIDHash(exeFile.parent.path, appinfo.name);
   1.174 +}

mercurial