toolkit/components/osfile/tests/xpcshell/test_osfile_writeAtomic_backupTo_option.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 "use strict";
michael@0 5
michael@0 6 let {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
michael@0 7 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 8
michael@0 9 /**
michael@0 10 * Remove all temporary files and back up files, including
michael@0 11 * test_backupTo_option_with_tmpPath.tmp
michael@0 12 * test_backupTo_option_with_tmpPath.tmp.backup
michael@0 13 * test_backupTo_option_without_tmpPath.tmp
michael@0 14 * test_backupTo_option_without_tmpPath.tmp.backup
michael@0 15 * test_non_backupTo_option.tmp
michael@0 16 * test_non_backupTo_option.tmp.backup
michael@0 17 * test_backupTo_option_without_destination_file.tmp
michael@0 18 * test_backupTo_option_without_destination_file.tmp.backup
michael@0 19 * test_backupTo_option_with_backup_file.tmp
michael@0 20 * test_backupTo_option_with_backup_file.tmp.backup
michael@0 21 */
michael@0 22 function clearFiles() {
michael@0 23 return Task.spawn(function () {
michael@0 24 let files = ["test_backupTo_option_with_tmpPath.tmp",
michael@0 25 "test_backupTo_option_without_tmpPath.tmp",
michael@0 26 "test_non_backupTo_option.tmp",
michael@0 27 "test_backupTo_option_without_destination_file.tmp",
michael@0 28 "test_backupTo_option_with_backup_file.tmp"];
michael@0 29 for (let file of files) {
michael@0 30 let path = Path.join(Constants.Path.tmpDir, file);
michael@0 31 yield File.remove(path);
michael@0 32 yield File.remove(path + ".backup");
michael@0 33 }
michael@0 34 });
michael@0 35 }
michael@0 36
michael@0 37 function run_test() {
michael@0 38 run_next_test();
michael@0 39 }
michael@0 40
michael@0 41 add_task(function* init() {
michael@0 42 yield clearFiles();
michael@0 43 });
michael@0 44
michael@0 45 /**
michael@0 46 * test when
michael@0 47 * |backupTo| specified
michael@0 48 * |tmpPath| specified
michael@0 49 * destination file exists
michael@0 50 * @result destination file will be backed up
michael@0 51 */
michael@0 52 add_task(function* test_backupTo_option_with_tmpPath() {
michael@0 53 let DEFAULT_CONTENTS = "default contents" + Math.random();
michael@0 54 let WRITE_CONTENTS = "abc" + Math.random();
michael@0 55 let path = Path.join(Constants.Path.tmpDir,
michael@0 56 "test_backupTo_option_with_tmpPath.tmp");
michael@0 57 yield File.writeAtomic(path, DEFAULT_CONTENTS);
michael@0 58 yield File.writeAtomic(path, WRITE_CONTENTS, { tmpPath: path + ".tmp",
michael@0 59 backupTo: path + ".backup" });
michael@0 60 do_check_true((yield File.exists(path + ".backup")));
michael@0 61 let contents = yield File.read(path + ".backup");
michael@0 62 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
michael@0 63 });
michael@0 64
michael@0 65 /**
michael@0 66 * test when
michael@0 67 * |backupTo| specified
michael@0 68 * |tmpPath| not specified
michael@0 69 * destination file exists
michael@0 70 * @result destination file will be backed up
michael@0 71 */
michael@0 72 add_task(function* test_backupTo_option_without_tmpPath() {
michael@0 73 let DEFAULT_CONTENTS = "default contents" + Math.random();
michael@0 74 let WRITE_CONTENTS = "abc" + Math.random();
michael@0 75 let path = Path.join(Constants.Path.tmpDir,
michael@0 76 "test_backupTo_option_without_tmpPath.tmp");
michael@0 77 yield File.writeAtomic(path, DEFAULT_CONTENTS);
michael@0 78 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
michael@0 79 do_check_true((yield File.exists(path + ".backup")));
michael@0 80 let contents = yield File.read(path + ".backup");
michael@0 81 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
michael@0 82 });
michael@0 83
michael@0 84 /**
michael@0 85 * test when
michael@0 86 * |backupTo| not specified
michael@0 87 * |tmpPath| not specified
michael@0 88 * destination file exists
michael@0 89 * @result destination file will not be backed up
michael@0 90 */
michael@0 91 add_task(function* test_non_backupTo_option() {
michael@0 92 let DEFAULT_CONTENTS = "default contents" + Math.random();
michael@0 93 let WRITE_CONTENTS = "abc" + Math.random();
michael@0 94 let path = Path.join(Constants.Path.tmpDir,
michael@0 95 "test_non_backupTo_option.tmp");
michael@0 96 yield File.writeAtomic(path, DEFAULT_CONTENTS);
michael@0 97 yield File.writeAtomic(path, WRITE_CONTENTS);
michael@0 98 do_check_false((yield File.exists(path + ".backup")));
michael@0 99 });
michael@0 100
michael@0 101 /**
michael@0 102 * test when
michael@0 103 * |backupTo| specified
michael@0 104 * |tmpPath| not specified
michael@0 105 * destination file not exists
michael@0 106 * @result no back up file exists
michael@0 107 */
michael@0 108 add_task(function* test_backupTo_option_without_destination_file() {
michael@0 109 let DEFAULT_CONTENTS = "default contents" + Math.random();
michael@0 110 let WRITE_CONTENTS = "abc" + Math.random();
michael@0 111 let path = Path.join(Constants.Path.tmpDir,
michael@0 112 "test_backupTo_option_without_destination_file.tmp");
michael@0 113 yield File.remove(path);
michael@0 114 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
michael@0 115 do_check_false((yield File.exists(path + ".backup")));
michael@0 116 });
michael@0 117
michael@0 118 /**
michael@0 119 * test when
michael@0 120 * |backupTo| specified
michael@0 121 * |tmpPath| not specified
michael@0 122 * backup file exists
michael@0 123 * destination file exists
michael@0 124 * @result destination file will be backed up
michael@0 125 */
michael@0 126 add_task(function* test_backupTo_option_with_backup_file() {
michael@0 127 let DEFAULT_CONTENTS = "default contents" + Math.random();
michael@0 128 let WRITE_CONTENTS = "abc" + Math.random();
michael@0 129 let path = Path.join(Constants.Path.tmpDir,
michael@0 130 "test_backupTo_option_with_backup_file.tmp");
michael@0 131 yield File.writeAtomic(path, DEFAULT_CONTENTS);
michael@0 132
michael@0 133 yield File.writeAtomic(path + ".backup", new Uint8Array(1000));
michael@0 134
michael@0 135 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
michael@0 136 do_check_true((yield File.exists(path + ".backup")));
michael@0 137 let contents = yield File.read(path + ".backup");
michael@0 138 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
michael@0 139 });
michael@0 140
michael@0 141 add_task(function* cleanup() {
michael@0 142 yield clearFiles();
michael@0 143 });

mercurial