PageRenderTime 59ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/test/stdlib/test-canon.c

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