test-canon.c 6.8 KB

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