test-canon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /* Prototype for our test function. */
  27. extern int do_test (int argc, char *argv[]);
  28. #include <test-skeleton.c>
  29. #ifndef PATH_MAX
  30. # define PATH_MAX 4096
  31. #endif
  32. static char cwd[PATH_MAX];
  33. static size_t cwd_len;
  34. struct {
  35. const char * name;
  36. const char * value;
  37. } symlinks[] = {
  38. {"SYMLINK_LOOP", "SYMLINK_LOOP"},
  39. {"SYMLINK_1", "."},
  40. {"SYMLINK_2", "//////./../../etc"},
  41. {"SYMLINK_3", "SYMLINK_1"},
  42. {"SYMLINK_4", "SYMLINK_2"},
  43. {"SYMLINK_5", "doesNotExist"},
  44. };
  45. struct {
  46. const char * in;
  47. const char * retval; /* what realpath should return */
  48. const char * retbuf; /* what realpath should store in buf */
  49. /* if both of the above are NULL, we won't check for result,
  50. * it's undefined */
  51. int error; /* expected errno value */
  52. } tests[] = {
  53. /* 0 */
  54. {"/", "/"},
  55. {"/////////////////////////////////", "/"},
  56. {"/.././.././.././..///", "/"},
  57. {"/etc", "/etc"},
  58. {"/etc/../etc", "/etc"},
  59. /* 5 */
  60. {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT},
  61. {"./././././././././.", "."},
  62. {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT},
  63. {"./doesExist", "./doesExist"},
  64. {"./doesExist/", "./doesExist"},
  65. /* 10 */
  66. {"./doesExist/../doesExist", "./doesExist"},
  67. {"foobar", 0, "./foobar", ENOENT},
  68. {".", "."},
  69. {"./foobar", 0, "./foobar", ENOENT},
  70. {"SYMLINK_LOOP", 0, 0, ELOOP},
  71. /* 15 */
  72. {"./SYMLINK_LOOP", 0, 0, ELOOP},
  73. {"SYMLINK_1", "."},
  74. {"SYMLINK_1/foobar", 0, "./foobar", ENOENT},
  75. {"SYMLINK_2", "/etc"},
  76. {"SYMLINK_3", "."},
  77. /* 20 */
  78. {"SYMLINK_4", "/etc"},
  79. {"../stdlib/SYMLINK_1", "."},
  80. {"../stdlib/SYMLINK_2", "/etc"},
  81. {"../stdlib/SYMLINK_3", "."},
  82. {"../stdlib/SYMLINK_4", "/etc"},
  83. /* 25 */
  84. {"./SYMLINK_5", 0, "./doesNotExist", ENOENT},
  85. {"SYMLINK_5", 0, "./doesNotExist", ENOENT},
  86. {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT},
  87. {"doesExist/../../stdlib/doesExist", "./doesExist"},
  88. {"doesExist/.././../stdlib/.", "."},
  89. #ifndef __UCLIBC__
  90. /* we dont check for ENOTDIR in readlink() which causes failures to
  91. * propogate up to realpath() ... so disable for now ... */
  92. /* 30 */
  93. {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR},
  94. {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR},
  95. #endif
  96. };
  97. static int
  98. check_path (const char * result, const char * expected)
  99. {
  100. int good;
  101. if (!result)
  102. return (expected == NULL);
  103. if (!expected)
  104. return 0;
  105. if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
  106. good = (strncmp (result, cwd, cwd_len) == 0
  107. && strcmp (result + cwd_len, expected + 1) == 0);
  108. else
  109. good = (strcmp (expected, result) == 0);
  110. return good;
  111. }
  112. int
  113. do_test (int argc, char ** argv)
  114. {
  115. char * result;
  116. int i, errors = 0;
  117. char buf[PATH_MAX];
  118. getcwd (cwd, sizeof(buf));
  119. cwd_len = strlen (cwd);
  120. #ifndef __UCLIBC__
  121. /* we choose to crash in uClibc when given a NULL */
  122. errno = 0;
  123. if (realpath (NULL, buf) != NULL || errno != EINVAL)
  124. {
  125. printf ("%s: expected return value NULL and errno set to EINVAL"
  126. " for realpath(NULL,...)\n", argv[0]);
  127. ++errors;
  128. }
  129. #endif
  130. #if 0
  131. /* This is now allowed. The test is invalid. */
  132. errno = 0;
  133. if (realpath ("/", NULL) != NULL || errno != EINVAL)
  134. {
  135. printf ("%s: expected return value NULL and errno set to EINVAL"
  136. " for realpath(...,NULL)\n", argv[0]);
  137. ++errors;
  138. }
  139. #endif
  140. errno = 0;
  141. if (realpath ("", buf) != NULL || errno != ENOENT)
  142. {
  143. printf ("%s: expected return value NULL and set errno to ENOENT"
  144. " for realpath(\"\",...)\n", argv[0]);
  145. ++errors;
  146. }
  147. for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
  148. symlink (symlinks[i].value, symlinks[i].name);
  149. int has_dir = mkdir ("doesExist", 0777) == 0;
  150. int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
  151. for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
  152. {
  153. buf[0] = '\0';
  154. errno = 0;
  155. result = realpath (tests[i].in, buf);
  156. if (!check_path (result, tests[i].retval))
  157. {
  158. printf ("%s: flunked test %d (expected `%s', got `%s')\n",
  159. argv[0], i, tests[i].retval ? tests[i].retval : "NULL",
  160. result ? result : "NULL");
  161. ++errors;
  162. continue;
  163. }
  164. if (result && !check_path (buf, tests[i].retval ? tests[i].retval : tests[i].retbuf))
  165. {
  166. printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
  167. argv[0], i, tests[i].retval ? tests[i].retval : tests[i].retbuf,
  168. buf);
  169. ++errors;
  170. continue;
  171. }
  172. if (errno != tests[i].error)
  173. {
  174. printf ("%s: flunked test %d (expected errno %d, got %d)\n",
  175. argv[0], i, tests[i].error, errno);
  176. ++errors;
  177. continue;
  178. }
  179. #ifndef __UCLIBC__
  180. /* we choose to crash in uClibc when given a NULL */
  181. char *result2 = realpath (tests[i].in, NULL);
  182. if ((result2 == NULL && result != NULL)
  183. || (result2 != NULL && strcmp (result, result2) != 0))
  184. {
  185. printf ("\
  186. %s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
  187. argv[0], result2, result);
  188. ++errors;
  189. }
  190. free (result2);
  191. #endif
  192. }
  193. getcwd (buf, sizeof(buf));
  194. if (strcmp (buf, cwd))
  195. {
  196. printf ("%s: current working directory changed from %s to %s\n",
  197. argv[0], cwd, buf);
  198. ++errors;
  199. }
  200. if (fd >= 0)
  201. {
  202. close (fd);
  203. unlink ("doesExist/someFile");
  204. }
  205. if (has_dir)
  206. rmdir ("doesExist");
  207. for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
  208. unlink (symlinks[i].name);
  209. if (errors != 0)
  210. {
  211. printf ("%d errors.\n", errors);
  212. return EXIT_FAILURE;
  213. }
  214. puts ("No errors.");
  215. return EXIT_SUCCESS;
  216. }