Backed out changeset ac839ddcb82f (bug 1745354) for causing failures on browser_backup_recovery.js. CLOSED TREE

This commit is contained in:
criss 2021-12-14 09:12:19 +02:00
parent d21d507036
commit 53fd094b40
2 changed files with 99 additions and 44 deletions

View File

@ -6,12 +6,27 @@
var EXPORTED_SYMBOLS = ["SessionMigration"];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(
this,
"E10SUtils",
"resource://gre/modules/E10SUtils.jsm"
);
// An encoder to UTF-8.
XPCOMUtils.defineLazyGetter(this, "gEncoder", function() {
return new TextEncoder();
});
// A decoder.
XPCOMUtils.defineLazyGetter(this, "gDecoder", function() {
return new TextDecoder();
});
var SessionMigrationInternal = {
/**
* Convert the original session restore state into a minimal state. It will
@ -65,15 +80,21 @@ var SessionMigrationInternal = {
* Asynchronously read session restore state (JSON) from a path
*/
readState(aPath) {
return IOUtils.readJSON(aPath, { decompress: true });
return (async function() {
let bytes = await OS.File.read(aPath, { compression: "lz4" });
let text = gDecoder.decode(bytes);
let state = JSON.parse(text);
return state;
})();
},
/**
* Asynchronously write session restore state as JSON to a path
*/
writeState(aPath, aState) {
return IOUtils.writeJSON(aPath, aState, {
compress: true,
tmpPath: `${aPath}.tmp`,
let bytes = gEncoder.encode(JSON.stringify(aState));
return OS.File.writeAtomic(aPath, bytes, {
tmpPath: aPath + ".tmp",
compression: "lz4",
});
},
};

View File

@ -5,16 +5,21 @@
// Each test will wait for a write to the Session Store
// before executing.
var OS = ChromeUtils.import("resource://gre/modules/osfile.jsm", {}).OS;
var { File, Constants, Path } = OS;
const PREF_SS_INTERVAL = "browser.sessionstore.interval";
const Paths = SessionFile.Paths;
// A text decoder.
var gDecoder = new TextDecoder();
// Global variables that contain sessionstore.jsonlz4 and sessionstore.baklz4 data for
// comparison between tests.
var gSSData;
var gSSBakData;
function promiseRead(path) {
return IOUtils.readUTF8(path, { decompress: true });
return File.read(path, { encoding: "utf-8", compression: "lz4" });
}
async function reInitSessionFile() {
@ -33,32 +38,28 @@ add_task(async function test_creation() {
// Cancel all pending session saves so they won't get in our way.
SessionSaver.cancel();
let PROFILE_DIR = await PathUtils.getProfileDir();
// Create dummy sessionstore backups
let OLD_BACKUP = PathUtils.join(PROFILE_DIR, "sessionstore.baklz4");
let OLD_UPGRADE_BACKUP = PathUtils.join(
PROFILE_DIR,
let OLD_BACKUP = Path.join(Constants.Path.profileDir, "sessionstore.baklz4");
let OLD_UPGRADE_BACKUP = Path.join(
Constants.Path.profileDir,
"sessionstore.baklz4-0000000"
);
await IOUtils.writeUTF8(OLD_BACKUP, "sessionstore.bak");
await IOUtils.writeUTF8(OLD_UPGRADE_BACKUP, "sessionstore upgrade backup");
await File.writeAtomic(OLD_BACKUP, "sessionstore.bak");
await File.writeAtomic(OLD_UPGRADE_BACKUP, "sessionstore upgrade backup");
await reInitSessionFile();
// Ensure none of the sessionstore files and backups exists
for (let k of Paths.loadOrder) {
ok(
!(await IOUtils.exists(Paths[k])),
!(await File.exists(Paths[k])),
"After wipe " + k + " sessionstore file doesn't exist"
);
}
ok(!(await File.exists(OLD_BACKUP)), "After wipe, old backup doesn't exist");
ok(
!(await IOUtils.exists(OLD_BACKUP)),
"After wipe, old backup doesn't exist"
);
ok(
!(await IOUtils.exists(OLD_UPGRADE_BACKUP)),
!(await File.exists(OLD_UPGRADE_BACKUP)),
"After wipe, old upgrade backup doesn't exist"
);
@ -74,11 +75,11 @@ add_task(async function test_creation() {
await SessionSaver.run();
ok(
await IOUtils.exists(Paths.recovery),
await File.exists(Paths.recovery),
"After write, recovery sessionstore file exists again"
);
ok(
!(await IOUtils.exists(Paths.recoveryBackup)),
!(await File.exists(Paths.recoveryBackup)),
"After write, recoveryBackup sessionstore doesn't exist"
);
ok(
@ -86,7 +87,7 @@ add_task(async function test_creation() {
"Recovery sessionstore file contains the required tab"
);
ok(
!(await IOUtils.exists(Paths.clean)),
!(await File.exists(Paths.clean)),
"After first write, clean shutdown " +
"sessionstore doesn't exist, since we haven't shutdown yet"
);
@ -100,7 +101,7 @@ add_task(async function test_creation() {
await SessionSaver.run();
ok(
await IOUtils.exists(Paths.recovery),
await File.exists(Paths.recovery),
"After second write, recovery sessionstore file still exists"
);
ok(
@ -108,14 +109,14 @@ add_task(async function test_creation() {
"Recovery sessionstore file contains the latest url"
);
ok(
await IOUtils.exists(Paths.recoveryBackup),
await File.exists(Paths.recoveryBackup),
"After write, recoveryBackup sessionstore now exists"
);
let backup = await promiseRead(Paths.recoveryBackup);
ok(!backup.includes(URL2), "Recovery backup doesn't contain the latest url");
ok(backup.includes(URL), "Recovery backup contains the original url");
ok(
!(await IOUtils.exists(Paths.clean)),
!(await File.exists(Paths.clean)),
"After first write, clean shutdown " +
"sessionstore doesn't exist, since we haven't shutdown yet"
);
@ -124,17 +125,17 @@ add_task(async function test_creation() {
await SessionFile.read(); // Reinitializes SessionFile
await SessionSaver.run();
ok(
!(await IOUtils.exists(Paths.clean)),
!(await File.exists(Paths.clean)),
"After second write, clean shutdown " +
"sessionstore doesn't exist, since we haven't shutdown yet"
);
ok(
!(await IOUtils.exists(Paths.upgradeBackup)),
!(await File.exists(Paths.upgradeBackup)),
"After second write, clean " +
"shutdown sessionstore doesn't exist, since we haven't shutdown yet"
);
ok(
!(await IOUtils.exists(Paths.nextUpgradeBackup)),
!(await File.exists(Paths.nextUpgradeBackup)),
"After second write, clean " +
"shutdown sessionstore doesn't exist, since we haven't shutdown yet"
);
@ -166,8 +167,11 @@ add_task(async function test_recovery() {
// Create Paths.recovery, ensure that we can recover from it.
let SOURCE = await promiseSource("Paths.recovery");
await IOUtils.makeDirectory(Paths.backups);
await IOUtils.writeUTF8(Paths.recovery, SOURCE, { compress: true });
await File.makeDir(Paths.backups);
await File.writeAtomic(Paths.recovery, SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
is(
(await SessionFile.read()).source,
SOURCE,
@ -176,9 +180,15 @@ add_task(async function test_recovery() {
info("Corrupting recovery file, attempting to recover from recovery backup");
SOURCE = await promiseSource("Paths.recoveryBackup");
await IOUtils.makeDirectory(Paths.backups);
await IOUtils.writeUTF8(Paths.recoveryBackup, SOURCE, { compress: true });
await IOUtils.writeUTF8(Paths.recovery, "<Invalid JSON>", { compress: true });
await File.makeDir(Paths.backups);
await File.writeAtomic(Paths.recoveryBackup, SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
await File.writeAtomic(Paths.recovery, "<Invalid JSON>", {
encoding: "utf-8",
compression: "lz4",
});
is(
(await SessionFile.read()).source,
SOURCE,
@ -198,25 +208,34 @@ add_task(async function test_recovery_inaccessible() {
);
let SOURCE_RECOVERY = await promiseSource("Paths.recovery");
let SOURCE = await promiseSource("Paths.recoveryBackup");
await IOUtils.makeDirectory(Paths.backups);
await IOUtils.writeUTF8(Paths.recoveryBackup, SOURCE, { compress: true });
await File.makeDir(Paths.backups);
await File.writeAtomic(Paths.recoveryBackup, SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
// Write a valid recovery file but make it inaccessible.
await IOUtils.writeUTF8(Paths.recovery, SOURCE_RECOVERY, { compress: true });
await IOUtils.setPermissions(Paths.recovery, 0);
await File.writeAtomic(Paths.recovery, SOURCE_RECOVERY, {
encoding: "utf-8",
compression: "lz4",
});
await File.setPermissions(Paths.recovery, { unixMode: 0 });
is(
(await SessionFile.read()).source,
SOURCE,
"Recovered the correct source from the recovery file"
);
await IOUtils.setPermissions(Paths.recovery, 0o644);
await File.setPermissions(Paths.recovery, { unixMode: 0o644 });
});
add_task(async function test_clean() {
await reInitSessionFile();
let SOURCE = await promiseSource("Paths.clean");
await IOUtils.writeUTF8(Paths.clean, SOURCE, { compress: true });
await File.writeAtomic(Paths.clean, SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
await SessionFile.read();
await SessionSaver.run();
is(
@ -242,8 +261,11 @@ add_task(async function test_version() {
);
// Create Paths.clean file
await IOUtils.makeDirectory(Paths.backups);
await IOUtils.writeUTF8(Paths.clean, SOURCE, { compress: true });
await File.makeDir(Paths.backups);
await File.writeAtomic(Paths.clean, SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
info("Attempting to recover from the clean file");
// Ensure that we can recover from Paths.recovery
@ -274,15 +296,21 @@ add_task(async function test_version_fallback() {
"Found backup sessionstore format version"
);
await IOUtils.makeDirectory(Paths.backups);
await File.makeDir(Paths.backups);
info(
"Modifying format version number to something incorrect, to make sure that we disregard the file."
);
let parsedSource = JSON.parse(SOURCE);
parsedSource.version[0] = "bookmarks";
await IOUtils.writeJSON(Paths.clean, parsedSource, { compress: true });
await IOUtils.writeUTF8(Paths.cleanBackup, BACKUP_SOURCE, { compress: true });
await File.writeAtomic(Paths.clean, JSON.stringify(parsedSource), {
encoding: "utf-8",
compression: "lz4",
});
await File.writeAtomic(Paths.cleanBackup, BACKUP_SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
is(
(await SessionFile.read()).source,
BACKUP_SOURCE,
@ -294,8 +322,14 @@ add_task(async function test_version_fallback() {
);
parsedSource = JSON.parse(SOURCE);
parsedSource.version[1] = Number.MAX_SAFE_INTEGER;
await IOUtils.writeJSON(Paths.clean, parsedSource, { compress: true });
await IOUtils.writeUTF8(Paths.cleanBackup, BACKUP_SOURCE, { compress: true });
await File.writeAtomic(Paths.clean, JSON.stringify(parsedSource), {
encoding: "utf-8",
compression: "lz4",
});
await File.writeAtomic(Paths.cleanBackup, BACKUP_SOURCE, {
encoding: "utf-8",
compression: "lz4",
});
is(
(await SessionFile.read()).source,
BACKUP_SOURCE,