2013-04-30 05:07:54 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
// This tests are for a sessionstore.js atomic backup.
|
2013-07-26 10:54:59 -07:00
|
|
|
// Each test will wait for a write to the Session Store
|
|
|
|
// before executing.
|
2013-04-30 05:07:54 -07:00
|
|
|
|
|
|
|
let tmp = {};
|
|
|
|
Cu.import("resource://gre/modules/osfile.jsm", tmp);
|
2013-10-25 02:52:42 -07:00
|
|
|
Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp);
|
2013-04-30 05:07:54 -07:00
|
|
|
|
2013-10-25 02:52:42 -07:00
|
|
|
const {OS, SessionFile} = tmp;
|
2013-04-30 05:07:54 -07:00
|
|
|
|
|
|
|
const PREF_SS_INTERVAL = "browser.sessionstore.interval";
|
|
|
|
// Full paths for sessionstore.js and sessionstore.bak.
|
|
|
|
const path = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js");
|
|
|
|
const backupPath = OS.Path.join(OS.Constants.Path.profileDir,
|
|
|
|
"sessionstore.bak");
|
|
|
|
|
|
|
|
// A text decoder.
|
|
|
|
let gDecoder = new TextDecoder();
|
|
|
|
// Global variables that contain sessionstore.js and sessionstore.bak data for
|
|
|
|
// comparison between tests.
|
|
|
|
let gSSData;
|
|
|
|
let gSSBakData;
|
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
// Wait for a state write to complete and then execute a callback.
|
2013-04-30 05:07:54 -07:00
|
|
|
function waitForSaveStateComplete(aSaveStateCallback) {
|
|
|
|
let topic = "sessionstore-state-write-complete";
|
|
|
|
|
|
|
|
function observer() {
|
|
|
|
Services.prefs.clearUserPref(PREF_SS_INTERVAL);
|
|
|
|
Services.obs.removeObserver(observer, topic);
|
|
|
|
executeSoon(function taskCallback() {
|
|
|
|
Task.spawn(aSaveStateCallback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.addObserver(observer, topic, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register next test callback and trigger state saving change.
|
|
|
|
function nextTest(testFunc) {
|
|
|
|
waitForSaveStateComplete(testFunc);
|
2013-07-26 10:54:59 -07:00
|
|
|
|
|
|
|
// We set the interval for session store state saves to be zero
|
|
|
|
// to cause a save ASAP.
|
2013-04-30 05:07:54 -07:00
|
|
|
Services.prefs.setIntPref(PREF_SS_INTERVAL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerCleanupFunction(function() {
|
|
|
|
// Cleaning up after the test: removing the sessionstore.bak file.
|
|
|
|
Task.spawn(function cleanupTask() {
|
|
|
|
yield OS.File.remove(backupPath);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function test() {
|
|
|
|
waitForExplicitFinish();
|
2013-07-26 10:54:59 -07:00
|
|
|
nextTest(testAfterFirstWrite);
|
2013-04-30 05:07:54 -07:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
function testAfterFirstWrite() {
|
|
|
|
// Ensure sessionstore.bak is not created. We start with a clean
|
|
|
|
// profile so there was nothing to move to sessionstore.bak before
|
|
|
|
// initially writing sessionstore.js
|
2013-04-30 05:07:54 -07:00
|
|
|
let ssExists = yield OS.File.exists(path);
|
|
|
|
let ssBackupExists = yield OS.File.exists(backupPath);
|
|
|
|
ok(ssExists, "sessionstore.js should exist.");
|
|
|
|
ok(!ssBackupExists, "sessionstore.bak should not have been created, yet");
|
|
|
|
|
|
|
|
// Save sessionstore.js data to compare to the sessionstore.bak data in the
|
|
|
|
// next test.
|
|
|
|
let array = yield OS.File.read(path);
|
|
|
|
gSSData = gDecoder.decode(array);
|
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
// Manually move to the backup since the first write has already happened
|
|
|
|
// and a backup would not be triggered again.
|
|
|
|
yield OS.File.move(path, backupPath);
|
2013-04-30 05:07:54 -07:00
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
nextTest(testReadBackup);
|
2013-04-30 05:07:54 -07:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
function testReadBackup() {
|
2013-04-30 05:07:54 -07:00
|
|
|
// Ensure sessionstore.bak is finally created.
|
|
|
|
let ssExists = yield OS.File.exists(path);
|
|
|
|
let ssBackupExists = yield OS.File.exists(backupPath);
|
|
|
|
ok(ssExists, "sessionstore.js exists.");
|
|
|
|
ok(ssBackupExists, "sessionstore.bak should now be created.");
|
|
|
|
|
|
|
|
// Read sessionstore.bak data.
|
|
|
|
let array = yield OS.File.read(backupPath);
|
|
|
|
gSSBakData = gDecoder.decode(array);
|
|
|
|
|
|
|
|
// Make sure that the sessionstore.bak is identical to the last
|
|
|
|
// sessionstore.js.
|
|
|
|
is(gSSBakData, gSSData, "sessionstore.js is backed up correctly.");
|
|
|
|
|
|
|
|
// Read latest sessionstore.js.
|
|
|
|
array = yield OS.File.read(path);
|
|
|
|
gSSData = gDecoder.decode(array);
|
|
|
|
|
2013-10-25 02:52:42 -07:00
|
|
|
// Read sessionstore.js with SessionFile.read.
|
|
|
|
let ssDataRead = yield SessionFile.read();
|
|
|
|
is(ssDataRead, gSSData, "SessionFile.read read sessionstore.js correctly.");
|
2013-04-30 05:07:54 -07:00
|
|
|
|
|
|
|
// Remove sessionstore.js to test fallback onto sessionstore.bak.
|
|
|
|
yield OS.File.remove(path);
|
|
|
|
ssExists = yield OS.File.exists(path);
|
|
|
|
ok(!ssExists, "sessionstore.js should be removed now.");
|
|
|
|
|
2013-10-25 02:52:42 -07:00
|
|
|
// Read sessionstore.bak with SessionFile.read.
|
|
|
|
ssDataRead = yield SessionFile.read();
|
2013-04-30 05:07:54 -07:00
|
|
|
is(ssDataRead, gSSBakData,
|
2013-10-25 02:52:42 -07:00
|
|
|
"SessionFile.read read sessionstore.bak correctly.");
|
2013-04-30 05:07:54 -07:00
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
nextTest(testBackupUnchanged);
|
2013-04-30 05:07:54 -07:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:59 -07:00
|
|
|
function testBackupUnchanged() {
|
2013-04-30 05:07:54 -07:00
|
|
|
// Ensure sessionstore.bak is backed up only once.
|
|
|
|
|
|
|
|
// Read sessionstore.bak data.
|
|
|
|
let array = yield OS.File.read(backupPath);
|
|
|
|
let ssBakData = gDecoder.decode(array);
|
|
|
|
// Ensure the sessionstore.bak did not change.
|
|
|
|
is(ssBakData, gSSBakData, "sessionstore.bak is unchanged.");
|
|
|
|
|
|
|
|
executeSoon(finish);
|
2013-07-12 04:46:14 -07:00
|
|
|
}
|