test-canon.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Test program for returning the canonical absolute name of a given file.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by David Mosberger <davidm@azstarnet.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This file must be run from within a directory called "stdlib". */
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/param.h>
  24. #include <sys/stat.h>
  25. /* Prototype for our test function. */
  26. extern int do_test (int argc, char *argv[]);
  27. #include <test-skeleton.c>
  28. #ifndef PATH_MAX
  29. # define PATH_MAX 4096
  30. #endif
  31. static char cwd[PATH_MAX];
  32. static size_t cwd_len;
  33. struct {
  34. const char * name;
  35. const char * value;
  36. } symlinks[] = {
  37. {"SYMLINK_LOOP", "SYMLINK_LOOP"},
  38. {"SYMLINK_1", "."},
  39. {"SYMLINK_2", "//////./../../etc"},
  40. {"SYMLINK_3", "SYMLINK_1"},
  41. {"SYMLINK_4", "SYMLINK_2"},
  42. {"SYMLINK_5", "doesNotExist"},
  43. };
  44. struct {
  45. const char * in, * out, * resolved;
  46. int error;
  47. } tests[] = {
  48. /* 0 */
  49. {"/", "/"},
  50. {"/////////////////////////////////", "/"},
  51. {"/.././.././.././..///", "/"},
  52. {"/etc", "/etc"},
  53. {"/etc/../etc", "/etc"},
  54. /* 5 */
  55. {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT},
  56. {"./././././././././.", "."},
  57. {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT},
  58. {"./doesExist", "./doesExist"},
  59. {"./doesExist/", "./doesExist"},
  60. /* 10 */
  61. {"./doesExist/../doesExist", "./doesExist"},
  62. {"foobar", 0, "./foobar", ENOENT},
  63. {".", "."},
  64. {"./foobar", 0, "./foobar", ENOENT},
  65. {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
  66. /* 15 */
  67. {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
  68. {"SYMLINK_1", "."},
  69. {"SYMLINK_1/foobar", 0, "./foobar", ENOENT},
  70. {"SYMLINK_2", "/etc"},
  71. {"SYMLINK_3", "."},
  72. /* 20 */
  73. {"SYMLINK_4", "/etc"},
  74. {"../stdlib/SYMLINK_1", "."},
  75. {"../stdlib/SYMLINK_2", "/etc"},
  76. {"../stdlib/SYMLINK_3", "."},
  77. {"../stdlib/SYMLINK_4", "/etc"},
  78. /* 25 */
  79. {"./SYMLINK_5", 0, "./doesNotExist", ENOENT},
  80. {"SYMLINK_5", 0, "./doesNotExist", ENOENT},
  81. {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT},
  82. {"doesExist/../../stdlib/doesExist", "./doesExist"},
  83. {"doesExist/.././../stdlib/.", "."},
  84. /* 30 */
  85. {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR},
  86. {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR},
  87. };
  88. static int
  89. check_path (const char * result, const char * expected)
  90. {
  91. int good;
  92. if (!result)
  93. return (expected == NULL);
  94. if (!expected)
  95. return 0;
  96. if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
  97. good = (strncmp (result, cwd, cwd_len) == 0
  98. && strcmp (result + cwd_len, expected + 1) == 0);
  99. else
  100. good = (strcmp (expected, result) == 0);
  101. return good;
  102. }
  103. int
  104. do_test (int argc, char ** argv)
  105. {
  106. char * result;
  107. int i, errors = 0;
  108. char buf[PATH_MAX];
  109. getcwd (cwd, sizeof(buf));
  110. cwd_len = strlen (cwd);
  111. errno = 0;
  112. if (realpath (NULL, buf) != NULL || errno != EINVAL)
  113. {
  114. printf ("%s: expected return value NULL and errno set to EINVAL"
  115. " for realpath(NULL,...)\n", argv[0]);
  116. ++errors;
  117. }
  118. #if 0
  119. /* This is now allowed. The test is invalid. */
  120. errno = 0;
  121. if (realpath ("/", NULL) != NULL || errno != EINVAL)
  122. {
  123. printf ("%s: expected return value NULL and errno set to EINVAL"
  124. " for realpath(...,NULL)\n", argv[0]);
  125. ++errors;
  126. }
  127. #endif
  128. errno = 0;
  129. if (realpath ("", buf) != NULL || errno != ENOENT)
  130. {
  131. printf ("%s: expected return value NULL and set errno to ENOENT"
  132. " for realpath(\"\",...)\n", argv[0]);
  133. ++errors;
  134. }
  135. for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
  136. symlink (symlinks[i].value, symlinks[i].name);
  137. int has_dir = mkdir ("doesExist", 0777) == 0;
  138. int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
  139. for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
  140. {
  141. buf[0] = '\0';
  142. result = realpath (tests[i].in, buf);
  143. if (!check_path (result, tests[i].out))
  144. {
  145. printf ("%s: flunked test %d (expected `%s', got `%s')\n",
  146. argv[0], i, tests[i].out ? tests[i].out : "NULL",
  147. result ? result : "NULL");
  148. ++errors;
  149. continue;
  150. }
  151. if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
  152. {
  153. printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
  154. argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
  155. buf);
  156. ++errors;
  157. continue;
  158. }
  159. if (!tests[i].out && errno != tests[i].error)
  160. {
  161. printf ("%s: flunked test %d (expected errno %d, got %d)\n",
  162. argv[0], i, tests[i].error, errno);
  163. ++errors;
  164. continue;
  165. }
  166. char *result2 = realpath (tests[i].in, NULL);
  167. if ((result2 == NULL && result != NULL)
  168. || (result2 != NULL && strcmp (result, result2) != 0))
  169. {
  170. printf ("\
  171. %s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
  172. argv[0], result2, result);
  173. ++errors;
  174. }
  175. free (result2);
  176. }
  177. getcwd (buf, sizeof(buf));
  178. if (strcmp (buf, cwd))
  179. {
  180. printf ("%s: current working directory changed from %s to %s\n",
  181. argv[0], cwd, buf);
  182. ++errors;
  183. }
  184. if (fd >= 0)
  185. {
  186. close (fd);
  187. unlink ("doesExist/someFile");
  188. }
  189. if (has_dir)
  190. rmdir ("doesExist");
  191. for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
  192. unlink (symlinks[i].name);
  193. if (errors != 0)
  194. {
  195. printf ("%d errors.\n", errors);
  196. return EXIT_FAILURE;
  197. }
  198. puts ("No errors.");
  199. return EXIT_SUCCESS;
  200. }