1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_833286_atomic_backup.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// This tests are for a sessionstore.js atomic backup. 1.8 +// Each test will wait for a write to the Session Store 1.9 +// before executing. 1.10 + 1.11 +let tmp = {}; 1.12 +Cu.import("resource://gre/modules/osfile.jsm", tmp); 1.13 +Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp); 1.14 + 1.15 +const {OS, SessionFile} = tmp; 1.16 + 1.17 +const PREF_SS_INTERVAL = "browser.sessionstore.interval"; 1.18 +// Full paths for sessionstore.js and sessionstore.bak. 1.19 +const path = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"); 1.20 +const backupPath = OS.Path.join(OS.Constants.Path.profileDir, 1.21 + "sessionstore.bak"); 1.22 + 1.23 +// A text decoder. 1.24 +let gDecoder = new TextDecoder(); 1.25 +// Global variables that contain sessionstore.js and sessionstore.bak data for 1.26 +// comparison between tests. 1.27 +let gSSData; 1.28 +let gSSBakData; 1.29 + 1.30 + 1.31 + 1.32 +add_task(function* testAfterFirstWrite() { 1.33 + // Ensure sessionstore.bak is not created. We start with a clean 1.34 + // profile so there was nothing to move to sessionstore.bak before 1.35 + // initially writing sessionstore.js 1.36 + let ssExists = yield OS.File.exists(path); 1.37 + let ssBackupExists = yield OS.File.exists(backupPath); 1.38 + ok(ssExists, "sessionstore.js should exist."); 1.39 + ok(!ssBackupExists, "sessionstore.bak should not have been created, yet"); 1.40 + 1.41 + // Save sessionstore.js data to compare to the sessionstore.bak data in the 1.42 + // next test. 1.43 + let array = yield OS.File.read(path); 1.44 + gSSData = gDecoder.decode(array); 1.45 + 1.46 + // Manually move to the backup since the first write has already happened 1.47 + // and a backup would not be triggered again. 1.48 + yield OS.File.move(path, backupPath); 1.49 + 1.50 + yield forceSaveState(); 1.51 +}); 1.52 + 1.53 +add_task(function* testReadBackup() { 1.54 + // Ensure sessionstore.bak is finally created. 1.55 + let ssExists = yield OS.File.exists(path); 1.56 + let ssBackupExists = yield OS.File.exists(backupPath); 1.57 + ok(ssExists, "sessionstore.js exists."); 1.58 + ok(ssBackupExists, "sessionstore.bak should now be created."); 1.59 + 1.60 + // Read sessionstore.bak data. 1.61 + let array = yield OS.File.read(backupPath); 1.62 + gSSBakData = gDecoder.decode(array); 1.63 + 1.64 + // Make sure that the sessionstore.bak is identical to the last 1.65 + // sessionstore.js. 1.66 + is(gSSBakData, gSSData, "sessionstore.js is backed up correctly."); 1.67 + 1.68 + // Read latest sessionstore.js. 1.69 + array = yield OS.File.read(path); 1.70 + gSSData = gDecoder.decode(array); 1.71 + 1.72 + // Read sessionstore.js with SessionFile.read. 1.73 + let ssDataRead = yield SessionFile.read(); 1.74 + is(ssDataRead, gSSData, "SessionFile.read read sessionstore.js correctly."); 1.75 + 1.76 + // Remove sessionstore.js to test fallback onto sessionstore.bak. 1.77 + yield OS.File.remove(path); 1.78 + ssExists = yield OS.File.exists(path); 1.79 + ok(!ssExists, "sessionstore.js should be removed now."); 1.80 + 1.81 + // Read sessionstore.bak with SessionFile.read. 1.82 + ssDataRead = yield SessionFile.read(); 1.83 + is(ssDataRead, gSSBakData, 1.84 + "SessionFile.read read sessionstore.bak correctly."); 1.85 + 1.86 + yield forceSaveState(); 1.87 +}); 1.88 + 1.89 +add_task(function* testBackupUnchanged() { 1.90 + // Ensure sessionstore.bak is backed up only once. 1.91 + 1.92 + // Read sessionstore.bak data. 1.93 + let array = yield OS.File.read(backupPath); 1.94 + let ssBakData = gDecoder.decode(array); 1.95 + // Ensure the sessionstore.bak did not change. 1.96 + is(ssBakData, gSSBakData, "sessionstore.bak is unchanged."); 1.97 +}); 1.98 + 1.99 +add_task(function* cleanup() { 1.100 + // Cleaning up after the test: removing the sessionstore.bak file. 1.101 + yield OS.File.remove(backupPath); 1.102 +});