toolkit/components/osfile/tests/xpcshell/test_unique.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.

     1 "use strict";
     3 Components.utils.import("resource://gre/modules/osfile.jsm");
     4 Components.utils.import("resource://gre/modules/Task.jsm");
     6 function run_test() {
     7   do_get_profile();
     8   run_next_test();
     9 }
    11 function testFiles(filename) {
    12   return Task.spawn(function() {
    13     const MAX_TRIES = 10;
    14     let profileDir = OS.Constants.Path.profileDir;
    15     let path = OS.Path.join(profileDir, filename);
    17     // Ensure that openUnique() uses the file name if there is no file with that name already.
    18     let openedFile = yield OS.File.openUnique(path);
    19     do_print("\nCreate new file: " + openedFile.path);
    20     yield openedFile.file.close();
    21     let exists = yield OS.File.exists(openedFile.path);
    22     do_check_true(exists);
    23     do_check_eq(path, openedFile.path);
    24     let fileInfo = yield OS.File.stat(openedFile.path);
    25     do_check_true(fileInfo.size == 0);
    27     // Ensure that openUnique() creates a new file name using a HEX number, as the original name is already taken.
    28     openedFile = yield OS.File.openUnique(path);
    29     do_print("\nCreate unique HEX file: " + openedFile.path);
    30     yield openedFile.file.close();
    31     exists = yield OS.File.exists(openedFile.path);
    32     do_check_true(exists);
    33     let fileInfo = yield OS.File.stat(openedFile.path);
    34     do_check_true(fileInfo.size == 0);
    36     // Ensure that openUnique() generates different file names each time, using the HEX number algorithm
    37     let filenames = new Set();
    38     for (let i=0; i < MAX_TRIES; i++) {
    39       openedFile = yield OS.File.openUnique(path);
    40       yield openedFile.file.close();
    41       filenames.add(openedFile.path);
    42     }
    44     do_check_eq(filenames.size, MAX_TRIES);
    46     // Ensure that openUnique() creates a new human readable file name using, as the original name is already taken.
    47     openedFile = yield OS.File.openUnique(path, {humanReadable : true});
    48     do_print("\nCreate unique Human Readable file: " + openedFile.path);
    49     yield openedFile.file.close();
    50     exists = yield OS.File.exists(openedFile.path);
    51     do_check_true(exists);
    52     let fileInfo = yield OS.File.stat(openedFile.path);
    53     do_check_true(fileInfo.size == 0);
    55     // Ensure that openUnique() generates different human readable file names each time
    56     filenames = new Set();
    57     for (let i=0; i < MAX_TRIES; i++) {
    58       openedFile = yield OS.File.openUnique(path, {humanReadable : true});
    59       yield openedFile.file.close();
    60       filenames.add(openedFile.path);
    61     }
    63     do_check_eq(filenames.size, MAX_TRIES);
    65     let exn;
    66     try {
    67       for (let i=0; i < 100; i++) {
    68         openedFile = yield OS.File.openUnique(path, {humanReadable : true});
    69         yield openedFile.file.close();
    70       }
    71     } catch (ex) {
    72       exn = ex;
    73     }
    75     do_print("Ensure that this raises the correct error");
    76     do_check_true(!!exn);
    77     do_check_true(exn instanceof OS.File.Error);
    78     do_check_true(exn.becauseExists);
    79   });
    80 }
    82 add_task(function test_unique() {
    83   OS.Shared.DEBUG = true;
    84   // Tests files with extension
    85   yield testFiles("dummy_unique_file.txt");
    86   // Tests files with no extension
    87   yield testFiles("dummy_unique_file_no_ext");
    88 });

mercurial