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