test_symlinks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const CWD = do_get_cwd();
  2. const DIR_TARGET = "target";
  3. const DIR_LINK = "link";
  4. const DIR_LINK_LINK = "link_link";
  5. const FILE_TARGET = "target.txt";
  6. const FILE_LINK = "link.txt";
  7. const FILE_LINK_LINK = "link_link.txt";
  8. const DOES_NOT_EXIST = "doesnotexist";
  9. const DANGLING_LINK = "dangling_link";
  10. const LOOP_LINK = "loop_link";
  11. const nsIFile = Components.interfaces.nsIFile;
  12. var process;
  13. function createSymLink(from, to) {
  14. if (!process) {
  15. var ln = Components.classes["@mozilla.org/file/local;1"]
  16. .createInstance(Components.interfaces.nsILocalFile);
  17. ln.initWithPath("/bin/ln");
  18. process = Components.classes["@mozilla.org/process/util;1"]
  19. .createInstance(Components.interfaces.nsIProcess);
  20. process.init(ln);
  21. }
  22. const args = ["-s", from, to];
  23. process.run(true, args, args.length);
  24. do_check_eq(process.exitValue, 0);
  25. }
  26. function makeSymLink(from, toName, relative) {
  27. var to = from.parent;
  28. to.append(toName);
  29. if (relative) {
  30. createSymLink(from.leafName, to.path);
  31. }
  32. else {
  33. createSymLink(from.path, to.path);
  34. }
  35. do_check_true(to.isSymlink());
  36. print("---");
  37. print(from.path);
  38. print(to.path);
  39. print(to.target);
  40. if (from.leafName != DOES_NOT_EXIST && from.isSymlink()) {
  41. // XXXjag wish I could set followLinks to false so we'd just get
  42. // the symlink's direct target instead of the final target.
  43. do_check_eq(from.target, to.target);
  44. }
  45. else {
  46. do_check_eq(from.path, to.target);
  47. }
  48. return to;
  49. }
  50. function setupTestDir(testDir, relative) {
  51. var targetDir = testDir.clone();
  52. targetDir.append(DIR_TARGET);
  53. if (testDir.exists()) {
  54. testDir.remove(true);
  55. }
  56. do_check_true(!testDir.exists());
  57. testDir.create(nsIFile.DIRECTORY_TYPE, 0o777);
  58. targetDir.create(nsIFile.DIRECTORY_TYPE, 0o777);
  59. var targetFile = testDir.clone();
  60. targetFile.append(FILE_TARGET);
  61. targetFile.create(nsIFile.NORMAL_FILE_TYPE, 0o666);
  62. var imaginary = testDir.clone();
  63. imaginary.append(DOES_NOT_EXIST);
  64. var loop = testDir.clone();
  65. loop.append(LOOP_LINK);
  66. var dirLink = makeSymLink(targetDir, DIR_LINK, relative);
  67. var fileLink = makeSymLink(targetFile, FILE_LINK, relative);
  68. makeSymLink(dirLink, DIR_LINK_LINK, relative);
  69. makeSymLink(fileLink, FILE_LINK_LINK, relative);
  70. makeSymLink(imaginary, DANGLING_LINK, relative);
  71. try {
  72. makeSymLink(loop, LOOP_LINK, relative);
  73. do_check_true(false);
  74. }
  75. catch (e) {
  76. }
  77. }
  78. function createSpaces(dirs, files, links) {
  79. function longest(a, b) {
  80. return a.length > b.length ? a : b;
  81. }
  82. return dirs.concat(files, links).reduce(longest, "").replace(/./g, " ");
  83. }
  84. function testSymLinks(testDir, relative) {
  85. setupTestDir(testDir, relative);
  86. const dirLinks = [DIR_LINK, DIR_LINK_LINK];
  87. const fileLinks = [FILE_LINK, FILE_LINK_LINK];
  88. const otherLinks = [DANGLING_LINK, LOOP_LINK];
  89. const dirs = [DIR_TARGET].concat(dirLinks);
  90. const files = [FILE_TARGET].concat(fileLinks);
  91. const links = otherLinks.concat(dirLinks, fileLinks);
  92. const spaces = createSpaces(dirs, files, links);
  93. const bools = {false: " false", true: " true "};
  94. print(spaces + " dir file symlink");
  95. var dirEntries = testDir.directoryEntries;
  96. while (dirEntries.hasMoreElements()) {
  97. const file = dirEntries.getNext().QueryInterface(nsIFile);
  98. const name = file.leafName;
  99. print(name + spaces.substring(name.length) + bools[file.isDirectory()] +
  100. bools[file.isFile()] + bools[file.isSymlink()]);
  101. do_check_eq(file.isDirectory(), dirs.indexOf(name) != -1);
  102. do_check_eq(file.isFile(), files.indexOf(name) != -1);
  103. do_check_eq(file.isSymlink(), links.indexOf(name) != -1);
  104. }
  105. }
  106. function run_test() {
  107. // Skip this test on Windows
  108. if (mozinfo.os == "win")
  109. return;
  110. var testDir = CWD;
  111. testDir.append("test_symlinks");
  112. testSymLinks(testDir, false);
  113. testSymLinks(testDir, true);
  114. }