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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { EXIT_CODE } = ChromeUtils.importESModule(
"resource://gre/modules/BackgroundTasksManager.sys.mjs"
);
const { setTimeout } = ChromeUtils.importESModule(
"resource://gre/modules/Timer.sys.mjs"
);
/**
* Tests that attempts to remove the profile directories complete.
*/
add_setup(() => {
// Make sure the profile service is initialised.
let { didCreate } = selectStartupProfile();
Assert.ok(didCreate, "Should have created a new profile.");
});
function wait(ms) {
// There isn't really a way to detect that the background task has started waiting so we must rely
// on a timeout here.
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
return new Promise(resolve => setTimeout(resolve, ms));
}
function runDeletionTask(rootDir, localDir, timeout) {
let binary = Services.dirsvc.get("XREExeF", Ci.nsIFile);
binary.leafName = binary.leafName.replace(
"xpcshell",
AppConstants.MOZ_BUILD_APP == "browser" ? "firefox" : "thunderbird"
);
let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(binary);
process.noShell = true;
process.startHidden = true;
let args = [
"--backgroundtask",
"removeProfileFiles",
rootDir.path,
localDir.path,
String(timeout),
];
console.log("Executing", binary.path, ...args);
return new Promise(resolve => {
process.runwAsync(args, args.length, () => {
resolve(process.exitValue);
});
});
}
// Test that in-process file deletion works.
add_task(async () => {
let service = getProfileService();
let profile = service.createProfile(null, "Test2");
// Basic test that file deletion works.
await service.removeProfileFilesByPath(profile.rootDir, profile.localDir, 0);
Assert.ok(
!(await IOUtils.exists(profile.rootDir.path)),
"Should have deleted the root dir"
);
Assert.ok(
!(await IOUtils.exists(profile.localDir.path)),
"Should have deleted the local dir"
);
});
// Tests that background task profile deletion works.
add_task(
{
skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
},
async () => {
let service = getProfileService();
let profile = service.createProfile(null, "Test3");
let testRootFile = PathUtils.join(profile.rootDir.path, "test.txt");
await IOUtils.writeUTF8(testRootFile, "hello");
let testLocalFile = PathUtils.join(profile.localDir.path, "test.txt");
await IOUtils.writeUTF8(testLocalFile, "goodbye");
let exitCode = await runDeletionTask(profile.rootDir, profile.localDir, 0);
Assert.equal(
exitCode,
EXIT_CODE.SUCCESS,
"Process should have completed successfully"
);
Assert.ok(
!(await IOUtils.exists(profile.rootDir.path)),
"Should have deleted the root dir"
);
Assert.ok(
!(await IOUtils.exists(profile.localDir.path)),
"Should have deleted the local dir"
);
}
);
// Tests that the background task cannot delete a locked profile directory.
add_task(
{
skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
},
async () => {
let service = getProfileService();
let profile = service.createProfile(null, "Test4");
let testRootFile = PathUtils.join(profile.rootDir.path, "test.txt");
await IOUtils.writeUTF8(testRootFile, "hello");
let testLocalFile = PathUtils.join(profile.localDir.path, "test.txt");
await IOUtils.writeUTF8(testLocalFile, "goodbye");
let lock = profile.lock({});
// With no timeout the task will exit as soon as it fails to obtain the lock.
let exitCode = await runDeletionTask(profile.rootDir, profile.localDir, 0);
Assert.equal(
exitCode,
EXIT_CODE.EXCEPTION,
"Process should not have completed successfully"
);
Assert.ok(
await IOUtils.exists(testRootFile),
"Should not have deleted the root dir"
);
Assert.ok(
await IOUtils.exists(testLocalFile),
"Should not have deleted the local dir"
);
lock.unlock();
// But now it can succeed.
exitCode = await runDeletionTask(profile.rootDir, profile.localDir, 0);
Assert.equal(
exitCode,
EXIT_CODE.SUCCESS,
"Process should have completed successfully"
);
Assert.ok(
!(await IOUtils.exists(profile.rootDir.path)),
"Should have deleted the root dir"
);
Assert.ok(
!(await IOUtils.exists(profile.localDir.path)),
"Should have deleted the local dir"
);
}
);
// Tests that the background task can wait for the profile lock.
add_task(
{
skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
},
async () => {
let service = getProfileService();
let profile = service.createProfile(null, "Test5");
let testRootFile = PathUtils.join(profile.rootDir.path, "test.txt");
await IOUtils.writeUTF8(testRootFile, "hello");
let testLocalFile = PathUtils.join(profile.localDir.path, "test.txt");
await IOUtils.writeUTF8(testLocalFile, "goodbye");
let lock = profile.lock({});
let deletionPromise = runDeletionTask(
profile.rootDir,
profile.localDir,
20
);
// Wait a few seconds to allow the background task to start running. Allow
// longer for debug builds.
await wait(AppConstants.DEBUG ? 4000 : 1000);
Assert.ok(
await IOUtils.exists(testRootFile),
"Should not have deleted the root dir"
);
Assert.ok(
await IOUtils.exists(testLocalFile),
"Should not have deleted the local dir"
);
// Now unlock and the deletion should complete.
lock.unlock();
let exitCode = await deletionPromise;
Assert.equal(
exitCode,
EXIT_CODE.SUCCESS,
"Process should have completed successfully"
);
Assert.ok(
!(await IOUtils.exists(profile.rootDir.path)),
"Should have deleted the root dir"
);
Assert.ok(
!(await IOUtils.exists(profile.localDir.path)),
"Should have deleted the local dir"
);
}
);
|