]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/test-canon.c
cb6aeaa7e8dcc02f4b3e14736412d811217543ab
[thirdparty/glibc.git] / stdlib / test-canon.c
1 /* Test program for returning the canonical absolute name of a given file.
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 /* This file must be run from within a directory called "stdlib". */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <sys/stat.h>
30
31 /* Prototype for our test function. */
32 extern int do_test (int argc, char *argv[]);
33 #include <test-skeleton.c>
34
35 #ifndef PATH_MAX
36 # define PATH_MAX 4096
37 #endif
38 static char cwd[PATH_MAX];
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;
55 int error;
56 } tests[] = {
57 /* 0 */
58 {"/", "/"},
59 {"/////////////////////////////////", "/"},
60 {"/.././.././.././..///", "/"},
61 {"/etc", "/etc"},
62 {"/etc/../etc", "/etc"},
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"},
92 {"doesExist/.././../stdlib/.", "."},
93 /* 30 */
94 {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR},
95 {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR},
96 };
97
98
99 static int
100 check_path (const char * result, const char * expected)
101 {
102 int good;
103
104 if (!result)
105 return (expected == NULL);
106
107 if (!expected)
108 return 0;
109
110 if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
111 good = (strncmp (result, cwd, cwd_len) == 0
112 && strcmp (result + cwd_len, expected + 1) == 0);
113 else
114 good = (strcmp (expected, result) == 0);
115
116 return good;
117 }
118
119
120 int
121 do_test (int argc, char ** argv)
122 {
123 char * result;
124 int i, errors = 0;
125 char buf[PATH_MAX];
126
127 getcwd (cwd, sizeof (buf));
128 cwd_len = strlen (cwd);
129
130 errno = 0;
131 if (realpath (NULL, buf) != NULL || errno != EINVAL)
132 {
133 printf ("%s: expected return value NULL and errno set to EINVAL"
134 " for realpath(NULL,...)\n", argv[0]);
135 ++errors;
136 }
137
138 #if 0
139 /* This is now allowed. The test is invalid. */
140 errno = 0;
141 if (realpath ("/", NULL) != NULL || errno != EINVAL)
142 {
143 printf ("%s: expected return value NULL and errno set to EINVAL"
144 " for realpath(...,NULL)\n", argv[0]);
145 ++errors;
146 }
147 #endif
148
149 errno = 0;
150 if (realpath ("", buf) != NULL || errno != ENOENT)
151 {
152 printf ("%s: expected return value NULL and set errno to ENOENT"
153 " for realpath(\"\",...)\n", argv[0]);
154 ++errors;
155 }
156
157 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
158 symlink (symlinks[i].value, symlinks[i].name);
159
160 int has_dir = mkdir ("doesExist", 0777) == 0;
161
162 int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
163
164 for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
165 {
166 buf[0] = '\0';
167 result = realpath (tests[i].in, buf);
168
169 if (!check_path (result, tests[i].out))
170 {
171 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
172 argv[0], i, tests[i].out ? tests[i].out : "NULL",
173 result ? result : "NULL");
174 ++errors;
175 continue;
176 }
177
178 if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
179 {
180 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
181 argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
182 buf);
183 ++errors;
184 continue;
185 }
186
187 if (!tests[i].out && errno != tests[i].error)
188 {
189 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
190 argv[0], i, tests[i].error, errno);
191 ++errors;
192 continue;
193 }
194
195 char *result2 = realpath (tests[i].in, NULL);
196 if ((result2 == NULL && result != NULL)
197 || (result2 != NULL && strcmp (result, result2) != 0))
198 {
199 printf ("\
200 %s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
201 argv[0], result2, result);
202 ++errors;
203 }
204 free (result2);
205 }
206
207 getcwd (buf, sizeof (buf));
208 if (strcmp (buf, cwd))
209 {
210 printf ("%s: current working directory changed from %s to %s\n",
211 argv[0], cwd, buf);
212 ++errors;
213 }
214
215 if (fd >= 0)
216 {
217 close (fd);
218 unlink ("doesExist/someFile");
219 }
220
221 if (has_dir)
222 rmdir ("doesExist");
223
224 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
225 unlink (symlinks[i].name);
226
227 if (errors != 0)
228 {
229 printf ("%d errors.\n", errors);
230 return EXIT_FAILURE;
231 }
232
233 puts ("No errors.");
234 return EXIT_SUCCESS;
235 }