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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 "use strict";
     6 let {utils: Cu} = Components;
     8 let SHARED_PATH;
    10 let EXISTING_FILE = do_get_file("xpcshell.ini").path;
    12 add_task(function* init() {
    13   do_get_profile();
    14   SHARED_PATH = OS.Path.join(OS.Constants.Path.profileDir, "test_osfile_read.tmp");
    15 });
    18 // Check that OS.File.read() is executed after the previous operation
    19 add_test_pair(function* ordering() {
    20   let string1 = "Initial state " + Math.random();
    21   let string2 = "After writing " + Math.random();
    22   yield OS.File.writeAtomic(SHARED_PATH, string1);
    23   OS.File.writeAtomic(SHARED_PATH, string2);
    24   let string3 = yield OS.File.read(SHARED_PATH, { encoding: "utf-8" });
    25   do_check_eq(string3, string2);
    26 });
    28 add_test_pair(function* read_write_all() {
    29   let DEST_PATH = SHARED_PATH + Math.random();
    30   let TMP_PATH = DEST_PATH + ".tmp";
    32   let test_with_options = function(options, suffix) {
    33     return Task.spawn(function*() {
    34       do_print("Running test read_write_all with options " + JSON.stringify(options));
    35       let TEST = "read_write_all " + suffix;
    37       let optionsBackup = JSON.parse(JSON.stringify(options));
    39       // Check that read + writeAtomic performs a correct copy
    40       let currentDir = yield OS.File.getCurrentDirectory();
    41       let pathSource = OS.Path.join(currentDir, EXISTING_FILE);
    42       let contents = yield OS.File.read(pathSource);
    43       do_check_true(!!contents); // Content is not empty
    45       let bytesWritten = yield OS.File.writeAtomic(DEST_PATH, contents, options);
    46       do_check_eq(contents.byteLength, bytesWritten); // Correct number of bytes written
    48       // Check that options are not altered
    49       do_check_eq(JSON.stringify(options), JSON.stringify(optionsBackup));
    50       yield reference_compare_files(pathSource, DEST_PATH, TEST);
    52       // Check that temporary file was removed or never created exist
    53       do_check_false(new FileUtils.File(TMP_PATH).exists());
    55       // Check that writeAtomic fails if noOverwrite is true and the destination
    56       // file already exists!
    57       let view = new Uint8Array(contents.buffer, 10, 200);
    58       try {
    59         let opt = JSON.parse(JSON.stringify(options));
    60         opt.noOverwrite = true;
    61         yield OS.File.writeAtomic(DEST_PATH, view, opt);
    62         do_throw("With noOverwrite, writeAtomic should have refused to overwrite file (" + suffix + ")");
    63       } catch (err if err instanceof OS.File.Error && err.becauseExists) {
    64         do_print("With noOverwrite, writeAtomic correctly failed (" + suffix + ")");
    65       }
    66       yield reference_compare_files(pathSource, DEST_PATH, TEST);
    68       // Check that temporary file was removed or never created
    69       do_check_false(new FileUtils.File(TMP_PATH).exists());
    71       // Now write a subset
    72       let START = 10;
    73       let LENGTH = 100;
    74       view = new Uint8Array(contents.buffer, START, LENGTH);
    75       bytesWritten = yield OS.File.writeAtomic(DEST_PATH, view, options);
    76       do_check_eq(bytesWritten, LENGTH);
    78       let array2 = yield OS.File.read(DEST_PATH);
    79       let view1 = new Uint8Array(contents.buffer, START, LENGTH);
    80       do_check_eq(view1.length, array2.length);
    81       let decoder = new TextDecoder();
    82       do_check_eq(decoder.decode(view1), decoder.decode(array2));
    85       // Cleanup.
    86       yield OS.File.remove(DEST_PATH);
    87       yield OS.File.remove(TMP_PATH);
    88     });
    89   };
    91   yield test_with_options({tmpPath: TMP_PATH}, "Renaming, not flushing");
    92   yield test_with_options({tmpPath: TMP_PATH, flush: true}, "Renaming, flushing");
    93   yield test_with_options({}, "Not renaming, not flushing");
    94   yield test_with_options({flush: true}, "Not renaming, flushing");
    95 });
    98 function run_test() {
    99   run_next_test();
   100 }

mercurial