test_backup_once.js (4529B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { SessionWriter } = ChromeUtils.importESModule( 7 "resource:///modules/sessionstore/SessionWriter.sys.mjs" 8 ); 9 10 // Make sure that we have a profile before initializing SessionFile. 11 const profd = do_get_profile(); 12 const { SessionFile } = ChromeUtils.importESModule( 13 "resource:///modules/sessionstore/SessionFile.sys.mjs" 14 ); 15 const Paths = SessionFile.Paths; 16 17 // We need a XULAppInfo to initialize SessionFile 18 const { updateAppInfo } = ChromeUtils.importESModule( 19 "resource://testing-common/AppInfo.sys.mjs" 20 ); 21 updateAppInfo({ 22 name: "SessionRestoreTest", 23 ID: "{230de50e-4cd1-11dc-8314-0800200c9a66}", 24 version: "1", 25 platformVersion: "", 26 }); 27 28 add_setup(async function () { 29 let source = do_get_file("data/sessionstore_valid.js"); 30 source.copyTo(profd, "sessionstore.js"); 31 await writeCompressedFile(Paths.clean.replace("jsonlz4", "js"), Paths.clean); 32 33 // Finish initialization of SessionFile 34 await SessionFile.read(); 35 }); 36 37 function promise_check_exist(path, shouldExist) { 38 return (async function () { 39 info( 40 "Ensuring that " + path + (shouldExist ? " exists" : " does not exist") 41 ); 42 if ((await IOUtils.exists(path)) != shouldExist) { 43 throw new Error( 44 "File" + path + " should " + (shouldExist ? "exist" : "not exist") 45 ); 46 } 47 })(); 48 } 49 50 function promise_check_contents(path, expect) { 51 return (async function () { 52 info("Checking whether " + path + " has the right contents"); 53 let actual = await IOUtils.readJSON(path, { 54 decompress: true, 55 }); 56 Assert.deepEqual( 57 actual, 58 expect, 59 `File ${path} contains the expected data.` 60 ); 61 })(); 62 } 63 64 function generateFileContents(id) { 65 let url = `http://example.com/test_backup_once#${id}_${Math.random()}`; 66 return { windows: [{ tabs: [{ entries: [{ url }], index: 1 }] }] }; 67 } 68 69 // Write to the store, and check that it creates: 70 // - $Path.recovery with the new data 71 // - $Path.nextUpgradeBackup with the old data 72 add_task(async function test_first_write_backup() { 73 let initial_content = generateFileContents("initial"); 74 let new_content = generateFileContents("test_1"); 75 76 info("Before the first write, none of the files should exist"); 77 await promise_check_exist(Paths.backups, false); 78 79 await IOUtils.makeDirectory(Paths.backups); 80 await IOUtils.writeJSON(Paths.clean, initial_content, { 81 compress: true, 82 }); 83 await SessionFile.write(new_content); 84 85 info("After first write, a few files should have been created"); 86 await promise_check_exist(Paths.backups, true); 87 await promise_check_exist(Paths.clean, false); 88 await promise_check_exist(Paths.cleanBackup, true); 89 await promise_check_exist(Paths.recovery, true); 90 await promise_check_exist(Paths.recoveryBackup, false); 91 await promise_check_exist(Paths.nextUpgradeBackup, true); 92 93 await promise_check_contents(Paths.recovery, new_content); 94 await promise_check_contents(Paths.nextUpgradeBackup, initial_content); 95 }); 96 97 // Write to the store again, and check that 98 // - $Path.clean is not written 99 // - $Path.recovery contains the new data 100 // - $Path.recoveryBackup contains the previous data 101 add_task(async function test_second_write_no_backup() { 102 let new_content = generateFileContents("test_2"); 103 let previous_backup_content = await IOUtils.readJSON(Paths.recovery, { 104 decompress: true, 105 }); 106 107 await IOUtils.remove(Paths.cleanBackup); 108 109 await SessionFile.write(new_content); 110 111 await promise_check_exist(Paths.backups, true); 112 await promise_check_exist(Paths.clean, false); 113 await promise_check_exist(Paths.cleanBackup, false); 114 await promise_check_exist(Paths.recovery, true); 115 await promise_check_exist(Paths.nextUpgradeBackup, true); 116 117 await promise_check_contents(Paths.recovery, new_content); 118 await promise_check_contents(Paths.recoveryBackup, previous_backup_content); 119 }); 120 121 // Make sure that we create $Paths.clean and remove $Paths.recovery* 122 // upon shutdown 123 add_task(async function test_shutdown() { 124 let output = generateFileContents("test_3"); 125 126 await IOUtils.writeUTF8(Paths.recovery, "I should disappear"); 127 await IOUtils.writeUTF8(Paths.recoveryBackup, "I should also disappear"); 128 129 await SessionWriter.write(output, { 130 isFinalWrite: true, 131 performShutdownCleanup: true, 132 }); 133 134 Assert.ok(!(await IOUtils.exists(Paths.recovery))); 135 Assert.ok(!(await IOUtils.exists(Paths.recoveryBackup))); 136 await promise_check_contents(Paths.clean, output); 137 });