toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.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 "use strict";
michael@0 2
michael@0 3 Components.utils.import("resource://gre/modules/osfile.jsm");
michael@0 4 Components.utils.import("resource://gre/modules/FileUtils.jsm");
michael@0 5 Components.utils.import("resource://gre/modules/NetUtil.jsm");
michael@0 6 Components.utils.import("resource://gre/modules/Promise.jsm");
michael@0 7 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 8
michael@0 9 function run_test() {
michael@0 10 do_test_pending();
michael@0 11 run_next_test();
michael@0 12 }
michael@0 13
michael@0 14 /**
michael@0 15 * A file that we know exists and that can be used for reading.
michael@0 16 */
michael@0 17 let EXISTING_FILE = "test_osfile_async_copy.js";
michael@0 18
michael@0 19 /**
michael@0 20 * Fetch asynchronously the contents of a file using xpcom.
michael@0 21 *
michael@0 22 * Used for comparing xpcom-based results to os.file-based results.
michael@0 23 *
michael@0 24 * @param {string} path The _absolute_ path to the file.
michael@0 25 * @return {promise}
michael@0 26 * @resolves {string} The contents of the file.
michael@0 27 */
michael@0 28 let reference_fetch_file = function reference_fetch_file(path) {
michael@0 29 let promise = Promise.defer();
michael@0 30 let file = new FileUtils.File(path);
michael@0 31 NetUtil.asyncFetch(file,
michael@0 32 function(stream, status) {
michael@0 33 if (!Components.isSuccessCode(status)) {
michael@0 34 promise.reject(status);
michael@0 35 return;
michael@0 36 }
michael@0 37 let result, reject;
michael@0 38 try {
michael@0 39 result = NetUtil.readInputStreamToString(stream, stream.available());
michael@0 40 } catch (x) {
michael@0 41 reject = x;
michael@0 42 }
michael@0 43 stream.close();
michael@0 44 if (reject) {
michael@0 45 promise.reject(reject);
michael@0 46 } else {
michael@0 47 promise.resolve(result);
michael@0 48 }
michael@0 49 });
michael@0 50 return promise.promise;
michael@0 51 };
michael@0 52
michael@0 53 /**
michael@0 54 * Compare asynchronously the contents two files using xpcom.
michael@0 55 *
michael@0 56 * Used for comparing xpcom-based results to os.file-based results.
michael@0 57 *
michael@0 58 * @param {string} a The _absolute_ path to the first file.
michael@0 59 * @param {string} b The _absolute_ path to the second file.
michael@0 60 *
michael@0 61 * @resolves {null}
michael@0 62 */
michael@0 63 let reference_compare_files = function reference_compare_files(a, b) {
michael@0 64 let a_contents = yield reference_fetch_file(a);
michael@0 65 let b_contents = yield reference_fetch_file(b);
michael@0 66 // Not using do_check_eq to avoid dumping the whole file to the log.
michael@0 67 // It is OK to === compare here, as both variables contain a string.
michael@0 68 do_check_true(a_contents === b_contents);
michael@0 69 };
michael@0 70
michael@0 71 /**
michael@0 72 * Test to ensure that OS.File.copy works.
michael@0 73 */
michael@0 74 function test_copymove(options = {}) {
michael@0 75 let source = OS.Path.join((yield OS.File.getCurrentDirectory()),
michael@0 76 EXISTING_FILE);
michael@0 77 let dest = OS.Path.join(OS.Constants.Path.tmpDir,
michael@0 78 "test_osfile_async_copy_dest.tmp");
michael@0 79 let dest2 = OS.Path.join(OS.Constants.Path.tmpDir,
michael@0 80 "test_osfile_async_copy_dest2.tmp");
michael@0 81 try {
michael@0 82 // 1. Test copy.
michael@0 83 yield OS.File.copy(source, dest, options);
michael@0 84 yield reference_compare_files(source, dest);
michael@0 85 // 2. Test subsequent move.
michael@0 86 yield OS.File.move(dest, dest2);
michael@0 87 yield reference_compare_files(source, dest2);
michael@0 88 // 3. Check that the moved file was really moved.
michael@0 89 do_check_eq((yield OS.File.exists(dest)), false);
michael@0 90 } finally {
michael@0 91 try {
michael@0 92 yield OS.File.remove(dest);
michael@0 93 } catch (ex if ex.becauseNoSuchFile) {
michael@0 94 // ignore
michael@0 95 }
michael@0 96 try {
michael@0 97 yield OS.File.remove(dest2);
michael@0 98 } catch (ex if ex.becauseNoSuchFile) {
michael@0 99 // ignore
michael@0 100 }
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104 // Regular copy test.
michael@0 105 add_task(test_copymove);
michael@0 106 // Userland copy test.
michael@0 107 add_task(test_copymove.bind(null, {unixUserland: true}));
michael@0 108
michael@0 109 add_task(do_test_finished);

mercurial