1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/unit/test_symlinks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +const CWD = do_get_cwd(); 1.5 +function checkOS(os) { 1.6 + const nsILocalFile_ = "nsILocalFile" + os; 1.7 + return nsILocalFile_ in Components.interfaces && 1.8 + CWD instanceof Components.interfaces[nsILocalFile_]; 1.9 +} 1.10 + 1.11 +const DIR_TARGET = "target"; 1.12 +const DIR_LINK = "link"; 1.13 +const DIR_LINK_LINK = "link_link"; 1.14 +const FILE_TARGET = "target.txt"; 1.15 +const FILE_LINK = "link.txt"; 1.16 +const FILE_LINK_LINK = "link_link.txt"; 1.17 + 1.18 +const DOES_NOT_EXIST = "doesnotexist"; 1.19 +const DANGLING_LINK = "dangling_link"; 1.20 +const LOOP_LINK = "loop_link"; 1.21 + 1.22 +const isWin = checkOS("Win"); 1.23 +const isMac = checkOS("Mac"); 1.24 +const isUnix = !(isWin || isMac); 1.25 + 1.26 +const nsIFile = Components.interfaces.nsIFile; 1.27 + 1.28 +var process; 1.29 +function createSymLink(from, to) { 1.30 + if (!process) { 1.31 + var ln = Components.classes["@mozilla.org/file/local;1"] 1.32 + .createInstance(Components.interfaces.nsILocalFile); 1.33 + ln.initWithPath("/bin/ln"); 1.34 + 1.35 + process = Components.classes["@mozilla.org/process/util;1"] 1.36 + .createInstance(Components.interfaces.nsIProcess); 1.37 + process.init(ln); 1.38 + } 1.39 + 1.40 + const args = ["-s", from, to]; 1.41 + process.run(true, args, args.length); 1.42 + do_check_eq(process.exitValue, 0); 1.43 +} 1.44 + 1.45 +function makeSymLink(from, toName, relative) { 1.46 + var to = from.parent; 1.47 + to.append(toName); 1.48 + 1.49 + if (relative) { 1.50 + createSymLink(from.leafName, to.path); 1.51 + } 1.52 + else { 1.53 + createSymLink(from.path, to.path); 1.54 + } 1.55 + 1.56 + do_check_true(to.isSymlink()); 1.57 + 1.58 + print("---"); 1.59 + print(from.path); 1.60 + print(to.path); 1.61 + print(to.target); 1.62 + 1.63 + if (from.leafName != DOES_NOT_EXIST && from.isSymlink()) { 1.64 + // XXXjag wish I could set followLinks to false so we'd just get 1.65 + // the symlink's direct target instead of the final target. 1.66 + do_check_eq(from.target, to.target); 1.67 + } 1.68 + else { 1.69 + do_check_eq(from.path, to.target); 1.70 + } 1.71 + 1.72 + return to; 1.73 +} 1.74 + 1.75 +function setupTestDir(testDir, relative) { 1.76 + var targetDir = testDir.clone(); 1.77 + targetDir.append(DIR_TARGET); 1.78 + 1.79 + if (testDir.exists()) { 1.80 + testDir.remove(true); 1.81 + } 1.82 + do_check_true(!testDir.exists()); 1.83 + 1.84 + testDir.create(nsIFile.DIRECTORY_TYPE, 0777); 1.85 + 1.86 + targetDir.create(nsIFile.DIRECTORY_TYPE, 0777); 1.87 + 1.88 + var targetFile = testDir.clone(); 1.89 + targetFile.append(FILE_TARGET); 1.90 + targetFile.create(nsIFile.NORMAL_FILE_TYPE, 0666); 1.91 + 1.92 + var imaginary = testDir.clone(); 1.93 + imaginary.append(DOES_NOT_EXIST); 1.94 + 1.95 + var loop = testDir.clone(); 1.96 + loop.append(LOOP_LINK); 1.97 + 1.98 + var dirLink = makeSymLink(targetDir, DIR_LINK, relative); 1.99 + var fileLink = makeSymLink(targetFile, FILE_LINK, relative); 1.100 + 1.101 + makeSymLink(dirLink, DIR_LINK_LINK, relative); 1.102 + makeSymLink(fileLink, FILE_LINK_LINK, relative); 1.103 + 1.104 + makeSymLink(imaginary, DANGLING_LINK, relative); 1.105 + 1.106 + try { 1.107 + makeSymLink(loop, LOOP_LINK, relative); 1.108 + do_check_true(false); 1.109 + } 1.110 + catch (e) { 1.111 + } 1.112 +} 1.113 + 1.114 +function createSpaces(dirs, files, links) { 1.115 + function longest(a, b) a.length > b.length ? a : b; 1.116 + return dirs.concat(files, links).reduce(longest, "").replace(/./g, " "); 1.117 +} 1.118 + 1.119 +function testSymLinks(testDir, relative) { 1.120 + setupTestDir(testDir, relative); 1.121 + 1.122 + const dirLinks = [DIR_LINK, DIR_LINK_LINK]; 1.123 + const fileLinks = [FILE_LINK, FILE_LINK_LINK]; 1.124 + const otherLinks = [DANGLING_LINK, LOOP_LINK]; 1.125 + const dirs = [DIR_TARGET].concat(dirLinks); 1.126 + const files = [FILE_TARGET].concat(fileLinks); 1.127 + const links = otherLinks.concat(dirLinks, fileLinks); 1.128 + 1.129 + const spaces = createSpaces(dirs, files, links); 1.130 + const bools = {false: " false", true: " true "}; 1.131 + print(spaces + " dir file symlink"); 1.132 + var dirEntries = testDir.directoryEntries; 1.133 + while (dirEntries.hasMoreElements()) { 1.134 + const file = dirEntries.getNext().QueryInterface(nsIFile); 1.135 + const name = file.leafName; 1.136 + print(name + spaces.substring(name.length) + bools[file.isDirectory()] + 1.137 + bools[file.isFile()] + bools[file.isSymlink()]); 1.138 + do_check_eq(file.isDirectory(), dirs.indexOf(name) != -1); 1.139 + do_check_eq(file.isFile(), files.indexOf(name) != -1); 1.140 + do_check_eq(file.isSymlink(), links.indexOf(name) != -1); 1.141 + } 1.142 +} 1.143 + 1.144 +function run_test() { 1.145 + // Skip this test on Windows 1.146 + if (isWin) 1.147 + return; 1.148 + 1.149 + var testDir = CWD; 1.150 + testDir.append("test_symlinks"); 1.151 + 1.152 + testSymLinks(testDir, false); 1.153 + testSymLinks(testDir, true); 1.154 +}