1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import tempfile
from pathlib import Path
import mozfile
sys.path.append(os.fspath(Path(__file__).parents[0]))
from backup_test_base import BackupTestBase
class BackupReplaceCurrentProfileTest(BackupTestBase):
"""
Tests for recovering backups with replaceCurrentProfile=true.
This includes:
1. Verifying the old profile is deleted from the database
2. Verifying metadata (name, avatar, theme) is copied when recovering
a legacy backup into a selectable profile environment
"""
def test_replace_current_profile(self):
"""
Tests that recovering with replaceCurrentProfile=true deletes the
current profile from the database.
"""
self.logger.info("=== Test: Replace Current Profile ===")
self.logger.info("Step 1: Setting up source selectable profile")
profile_name = self.register_profile_and_restart()
self._cleanups.append({"profile_name": profile_name})
self.logger.info(f"Registered toolkit profile: {profile_name}")
selectable_info = self.setup_selectable_profile()
original_profile_id = selectable_info["id"]
original_profile_path = selectable_info["path"]
self.logger.info(
f"Source profile: id={original_profile_id}, path={original_profile_path}"
)
self.marionette.restart(clean=False, in_app=True)
self.logger.info("Step 2: Creating backup")
self._archive_path = self.create_backup()
self._cleanups.append({"path": self._archive_path})
self.assertTrue(
os.path.exists(self._archive_path), "Backup archive should exist"
)
self.logger.info(f"Backup created at: {self._archive_path}")
self.logger.info("Step 3: Getting profiles before recovery")
profiles_before = self.get_all_profiles()
self.logger.info(f"Profiles before recovery: {profiles_before}")
self.logger.info("Step 4: Recovering backup with replaceCurrentProfile=true")
self._recovery_path = os.path.join(
tempfile.gettempdir(), "replace-profile-recovery"
)
mozfile.remove(self._recovery_path)
self._cleanups.append({"path": self._recovery_path})
new_profile_root = self.run_async(
"""
let newProfileRoot = await IOUtils.createUniqueDirectory(
PathUtils.tempDir, "replaceProfileTest"
);
return newProfileRoot;
"""
)
self._cleanups.append({"path": new_profile_root})
self.logger.info(f"Created profile root for recovery: {new_profile_root}")
self.run_async(
"""
const { BackupService } = ChromeUtils.importESModule(
"resource:///modules/backup/BackupService.sys.mjs"
);
let [archivePath, recoveryPath, profileRoot] = arguments;
let bs = BackupService.get();
bs.deleteAndQuitCurrentSelectableProfile = async () => null;
await bs.recoverFromBackupArchive(
archivePath, null, false, recoveryPath, profileRoot, true
);
""",
script_args=[self._archive_path, self._recovery_path, new_profile_root],
)
self.logger.info("Step 5: Launching recovered profile to verify it works")
recovered_profile_dirs = os.listdir(new_profile_root)
self.assertEqual(
len(recovered_profile_dirs), 1, "Should have exactly one recovered profile"
)
recovered_profile_path = os.path.join(
new_profile_root, recovered_profile_dirs[0]
)
self.logger.info(f"Recovered profile path: {recovered_profile_path}")
self._new_profile_path = recovered_profile_path
self.marionette.quit()
self.marionette.instance.profile = recovered_profile_path
self.marionette.start_session()
self.marionette.set_context("chrome")
self.wait_for_post_recovery()
self.logger.info("Post-recovery complete")
self.init_selectable_profile_service()
self.logger.info("Step 6: Cleaning up")
self.cleanup_selectable_profiles()
self.logger.info("=== Test: Replace Current Profile PASSED ===")
def test_replace_current_profile_legacy_backup_copies_metadata(self):
"""
Tests that recovering a legacy backup with replaceCurrentProfile=true
copies the current profile's metadata (name, avatar, theme) to the
new profile.
"""
self.logger.info(
"=== Test: Replace Current Profile - Legacy Backup Copies Metadata ==="
)
self.logger.info("Step 1: Creating legacy backup (no selectable profiles)")
profile_name = self.register_profile_and_restart()
self._cleanups.append({"profile_name": profile_name})
self.set_prefs({
"browser.profiles.enabled": False,
"browser.profiles.created": False,
})
has_selectable = self.has_selectable_profiles()
self.assertFalse(has_selectable, "Should be a legacy profile")
self.logger.info("Verified profile is legacy")
self._archive_path = self.create_backup()
self._cleanups.append({"path": self._archive_path})
self.assertTrue(
os.path.exists(self._archive_path), "Backup archive should exist"
)
self.logger.info(f"Legacy backup created at: {self._archive_path}")
self.logger.info(
"Step 2: Setting up destination selectable profile with custom metadata"
)
self.marionette.quit()
self.marionette.instance.switch_profile()
self.marionette.start_session()
self.marionette.set_context("chrome")
recovery_profile_name = self.register_profile_and_restart()
self._cleanups.append({"profile_name": recovery_profile_name})
self.setup_selectable_profile()
custom_name = "My Custom Profile"
custom_avatar = "book"
custom_theme = {"themeBg": "#ff5500", "themeFg": "#ffffff"}
self.run_async(
"""
const { SelectableProfileService } = ChromeUtils.importESModule(
"resource:///modules/profiles/SelectableProfileService.sys.mjs"
);
let profile = SelectableProfileService.currentProfile;
profile.name = arguments[0];
profile.setAvatar(arguments[1]);
profile.theme = arguments[2];
""",
script_args=[custom_name, custom_avatar, custom_theme],
)
self.logger.info(
f"Set custom metadata: name={custom_name}, avatar={custom_avatar}"
)
self.logger.info(
"Step 3: Recovering legacy backup with replaceCurrentProfile=true"
)
self._recovery_path = os.path.join(
tempfile.gettempdir(), "legacy-replace-profile-recovery"
)
mozfile.remove(self._recovery_path)
self._cleanups.append({"path": self._recovery_path})
new_profile_root = self.run_async(
"""
let newProfileRoot = await IOUtils.createUniqueDirectory(
PathUtils.tempDir, "legacyReplaceTest"
);
return newProfileRoot;
"""
)
self._cleanups.append({"path": new_profile_root})
self.logger.info(f"Created profile root for recovery: {new_profile_root}")
self.run_async(
"""
const { BackupService } = ChromeUtils.importESModule(
"resource:///modules/backup/BackupService.sys.mjs"
);
let [archivePath, recoveryPath, profileRoot] = arguments;
let bs = BackupService.get();
bs.deleteAndQuitCurrentSelectableProfile = async () => null;
await bs.recoverFromBackupArchive(
archivePath, null, false, recoveryPath, profileRoot, true
);
""",
script_args=[self._archive_path, self._recovery_path, new_profile_root],
)
self.logger.info("Step 4: Launching recovered profile to verify metadata")
recovered_profile_dirs = os.listdir(new_profile_root)
self.assertEqual(
len(recovered_profile_dirs), 1, "Should have exactly one recovered profile"
)
recovered_profile_path = os.path.join(
new_profile_root, recovered_profile_dirs[0]
)
self.logger.info(f"Recovered profile path: {recovered_profile_path}")
self._new_profile_path = recovered_profile_path
self.marionette.quit()
self.marionette.instance.profile = recovered_profile_path
self.marionette.start_session()
self.marionette.set_context("chrome")
self.wait_for_post_recovery()
self.logger.info("Post-recovery complete")
self.logger.info("Step 5: Verifying recovered profile has copied metadata")
self.init_selectable_profile_service()
recovered_profile_metadata = self.run_async(
"""
const { SelectableProfileService } = ChromeUtils.importESModule(
"resource:///modules/profiles/SelectableProfileService.sys.mjs"
);
const { FileUtils } = ChromeUtils.importESModule(
"resource://gre/modules/FileUtils.sys.mjs"
);
let filePath = new FileUtils.File(arguments[0]);
let profile = await SelectableProfileService.getProfileByPath(filePath);
if (!profile) return null;
return {
id: profile.id,
name: profile.name,
avatar: profile.avatar,
theme: profile.theme
};
""",
script_args=[recovered_profile_path],
)
self.assertIsNotNone(
recovered_profile_metadata, "Should have recovered profile metadata"
)
self.assertEqual(
recovered_profile_metadata["name"],
custom_name,
"Recovered profile should have the old profile's name",
)
self.assertEqual(
recovered_profile_metadata["avatar"],
custom_avatar,
"Recovered profile should have the old profile's avatar",
)
self.logger.info(
f"Verified metadata was copied: name={recovered_profile_metadata['name']}, "
f"avatar={recovered_profile_metadata['avatar']}"
)
self.logger.info("Step 6: Cleaning up")
self.cleanup_selectable_profiles()
self.logger.info(
"=== Test: Replace Current Profile - Legacy Backup Copies Metadata PASSED ==="
)
|