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