Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // This tests are for a sessionstore.js atomic backup. |
michael@0 | 5 | // Each test will wait for a write to the Session Store |
michael@0 | 6 | // before executing. |
michael@0 | 7 | |
michael@0 | 8 | let tmp = {}; |
michael@0 | 9 | Cu.import("resource://gre/modules/osfile.jsm", tmp); |
michael@0 | 10 | Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp); |
michael@0 | 11 | |
michael@0 | 12 | const {OS, SessionFile} = tmp; |
michael@0 | 13 | |
michael@0 | 14 | const PREF_SS_INTERVAL = "browser.sessionstore.interval"; |
michael@0 | 15 | // Full paths for sessionstore.js and sessionstore.bak. |
michael@0 | 16 | const path = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"); |
michael@0 | 17 | const backupPath = OS.Path.join(OS.Constants.Path.profileDir, |
michael@0 | 18 | "sessionstore.bak"); |
michael@0 | 19 | |
michael@0 | 20 | // A text decoder. |
michael@0 | 21 | let gDecoder = new TextDecoder(); |
michael@0 | 22 | // Global variables that contain sessionstore.js and sessionstore.bak data for |
michael@0 | 23 | // comparison between tests. |
michael@0 | 24 | let gSSData; |
michael@0 | 25 | let gSSBakData; |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | add_task(function* testAfterFirstWrite() { |
michael@0 | 30 | // Ensure sessionstore.bak is not created. We start with a clean |
michael@0 | 31 | // profile so there was nothing to move to sessionstore.bak before |
michael@0 | 32 | // initially writing sessionstore.js |
michael@0 | 33 | let ssExists = yield OS.File.exists(path); |
michael@0 | 34 | let ssBackupExists = yield OS.File.exists(backupPath); |
michael@0 | 35 | ok(ssExists, "sessionstore.js should exist."); |
michael@0 | 36 | ok(!ssBackupExists, "sessionstore.bak should not have been created, yet"); |
michael@0 | 37 | |
michael@0 | 38 | // Save sessionstore.js data to compare to the sessionstore.bak data in the |
michael@0 | 39 | // next test. |
michael@0 | 40 | let array = yield OS.File.read(path); |
michael@0 | 41 | gSSData = gDecoder.decode(array); |
michael@0 | 42 | |
michael@0 | 43 | // Manually move to the backup since the first write has already happened |
michael@0 | 44 | // and a backup would not be triggered again. |
michael@0 | 45 | yield OS.File.move(path, backupPath); |
michael@0 | 46 | |
michael@0 | 47 | yield forceSaveState(); |
michael@0 | 48 | }); |
michael@0 | 49 | |
michael@0 | 50 | add_task(function* testReadBackup() { |
michael@0 | 51 | // Ensure sessionstore.bak is finally created. |
michael@0 | 52 | let ssExists = yield OS.File.exists(path); |
michael@0 | 53 | let ssBackupExists = yield OS.File.exists(backupPath); |
michael@0 | 54 | ok(ssExists, "sessionstore.js exists."); |
michael@0 | 55 | ok(ssBackupExists, "sessionstore.bak should now be created."); |
michael@0 | 56 | |
michael@0 | 57 | // Read sessionstore.bak data. |
michael@0 | 58 | let array = yield OS.File.read(backupPath); |
michael@0 | 59 | gSSBakData = gDecoder.decode(array); |
michael@0 | 60 | |
michael@0 | 61 | // Make sure that the sessionstore.bak is identical to the last |
michael@0 | 62 | // sessionstore.js. |
michael@0 | 63 | is(gSSBakData, gSSData, "sessionstore.js is backed up correctly."); |
michael@0 | 64 | |
michael@0 | 65 | // Read latest sessionstore.js. |
michael@0 | 66 | array = yield OS.File.read(path); |
michael@0 | 67 | gSSData = gDecoder.decode(array); |
michael@0 | 68 | |
michael@0 | 69 | // Read sessionstore.js with SessionFile.read. |
michael@0 | 70 | let ssDataRead = yield SessionFile.read(); |
michael@0 | 71 | is(ssDataRead, gSSData, "SessionFile.read read sessionstore.js correctly."); |
michael@0 | 72 | |
michael@0 | 73 | // Remove sessionstore.js to test fallback onto sessionstore.bak. |
michael@0 | 74 | yield OS.File.remove(path); |
michael@0 | 75 | ssExists = yield OS.File.exists(path); |
michael@0 | 76 | ok(!ssExists, "sessionstore.js should be removed now."); |
michael@0 | 77 | |
michael@0 | 78 | // Read sessionstore.bak with SessionFile.read. |
michael@0 | 79 | ssDataRead = yield SessionFile.read(); |
michael@0 | 80 | is(ssDataRead, gSSBakData, |
michael@0 | 81 | "SessionFile.read read sessionstore.bak correctly."); |
michael@0 | 82 | |
michael@0 | 83 | yield forceSaveState(); |
michael@0 | 84 | }); |
michael@0 | 85 | |
michael@0 | 86 | add_task(function* testBackupUnchanged() { |
michael@0 | 87 | // Ensure sessionstore.bak is backed up only once. |
michael@0 | 88 | |
michael@0 | 89 | // Read sessionstore.bak data. |
michael@0 | 90 | let array = yield OS.File.read(backupPath); |
michael@0 | 91 | let ssBakData = gDecoder.decode(array); |
michael@0 | 92 | // Ensure the sessionstore.bak did not change. |
michael@0 | 93 | is(ssBakData, gSSBakData, "sessionstore.bak is unchanged."); |
michael@0 | 94 | }); |
michael@0 | 95 | |
michael@0 | 96 | add_task(function* cleanup() { |
michael@0 | 97 | // Cleaning up after the test: removing the sessionstore.bak file. |
michael@0 | 98 | yield OS.File.remove(backupPath); |
michael@0 | 99 | }); |