xpcom/tests/unit/test_symlinks.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 const CWD = do_get_cwd();
     2 function checkOS(os) {
     3   const nsILocalFile_ = "nsILocalFile" + os;
     4   return nsILocalFile_ in Components.interfaces &&
     5          CWD instanceof Components.interfaces[nsILocalFile_];
     6 }
     8 const DIR_TARGET     = "target";
     9 const DIR_LINK       = "link";
    10 const DIR_LINK_LINK  = "link_link";
    11 const FILE_TARGET    = "target.txt";
    12 const FILE_LINK      = "link.txt";
    13 const FILE_LINK_LINK = "link_link.txt";
    15 const DOES_NOT_EXIST = "doesnotexist";
    16 const DANGLING_LINK  = "dangling_link";
    17 const LOOP_LINK      = "loop_link";
    19 const isWin = checkOS("Win");
    20 const isMac = checkOS("Mac");
    21 const isUnix = !(isWin || isMac);
    23 const nsIFile = Components.interfaces.nsIFile;
    25 var process;
    26 function createSymLink(from, to) {
    27   if (!process) {
    28     var ln = Components.classes["@mozilla.org/file/local;1"]
    29                        .createInstance(Components.interfaces.nsILocalFile);
    30     ln.initWithPath("/bin/ln");
    32     process = Components.classes["@mozilla.org/process/util;1"]
    33                         .createInstance(Components.interfaces.nsIProcess);
    34     process.init(ln);
    35   }
    37   const args = ["-s", from, to];
    38   process.run(true, args, args.length);
    39   do_check_eq(process.exitValue, 0);
    40 }
    42 function makeSymLink(from, toName, relative) {
    43   var to = from.parent;
    44   to.append(toName);
    46   if (relative) {
    47     createSymLink(from.leafName, to.path);
    48   }
    49   else {
    50     createSymLink(from.path, to.path);
    51   }
    53   do_check_true(to.isSymlink());
    55   print("---");
    56   print(from.path);
    57   print(to.path);
    58   print(to.target);
    60   if (from.leafName != DOES_NOT_EXIST && from.isSymlink()) {
    61     // XXXjag wish I could set followLinks to false so we'd just get
    62     // the symlink's direct target instead of the final target.
    63     do_check_eq(from.target, to.target);
    64   }
    65   else {
    66     do_check_eq(from.path, to.target);
    67   }
    69   return to;
    70 }
    72 function setupTestDir(testDir, relative) {
    73   var targetDir = testDir.clone();
    74   targetDir.append(DIR_TARGET);
    76   if (testDir.exists()) {
    77     testDir.remove(true);
    78   }
    79   do_check_true(!testDir.exists());
    81   testDir.create(nsIFile.DIRECTORY_TYPE, 0777);
    83   targetDir.create(nsIFile.DIRECTORY_TYPE, 0777);
    85   var targetFile = testDir.clone();
    86   targetFile.append(FILE_TARGET);
    87   targetFile.create(nsIFile.NORMAL_FILE_TYPE, 0666);
    89   var imaginary = testDir.clone();
    90   imaginary.append(DOES_NOT_EXIST);
    92   var loop = testDir.clone();
    93   loop.append(LOOP_LINK);
    95   var dirLink  = makeSymLink(targetDir, DIR_LINK, relative);
    96   var fileLink = makeSymLink(targetFile, FILE_LINK, relative);
    98   makeSymLink(dirLink, DIR_LINK_LINK, relative);
    99   makeSymLink(fileLink, FILE_LINK_LINK, relative);
   101   makeSymLink(imaginary, DANGLING_LINK, relative);
   103   try {
   104     makeSymLink(loop, LOOP_LINK, relative);
   105     do_check_true(false);
   106   }
   107   catch (e) {
   108   }
   109 }
   111 function createSpaces(dirs, files, links) {
   112   function longest(a, b) a.length > b.length ? a : b;
   113   return dirs.concat(files, links).reduce(longest, "").replace(/./g, " ");
   114 }
   116 function testSymLinks(testDir, relative) {
   117   setupTestDir(testDir, relative);
   119   const dirLinks   = [DIR_LINK, DIR_LINK_LINK];
   120   const fileLinks  = [FILE_LINK, FILE_LINK_LINK];
   121   const otherLinks = [DANGLING_LINK, LOOP_LINK];
   122   const dirs  = [DIR_TARGET].concat(dirLinks);
   123   const files = [FILE_TARGET].concat(fileLinks);
   124   const links = otherLinks.concat(dirLinks, fileLinks);
   126   const spaces = createSpaces(dirs, files, links);
   127   const bools = {false: " false", true: " true "};
   128   print(spaces + " dir   file  symlink");
   129   var dirEntries = testDir.directoryEntries;
   130   while (dirEntries.hasMoreElements()) {
   131     const file = dirEntries.getNext().QueryInterface(nsIFile);
   132     const name = file.leafName;
   133     print(name + spaces.substring(name.length) + bools[file.isDirectory()] +
   134           bools[file.isFile()] + bools[file.isSymlink()]);
   135     do_check_eq(file.isDirectory(), dirs.indexOf(name) != -1);
   136     do_check_eq(file.isFile(), files.indexOf(name) != -1);
   137     do_check_eq(file.isSymlink(), links.indexOf(name) != -1);
   138   }
   139 }
   141 function run_test() {
   142   // Skip this test on Windows
   143   if (isWin)
   144     return;
   146   var testDir = CWD;
   147   testDir.append("test_symlinks");
   149   testSymLinks(testDir, false);
   150   testSymLinks(testDir, true);
   151 }

mercurial