]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/test-canon.c
ChangeLog entry for previous changeset
[thirdparty/glibc.git] / stdlib / test-canon.c
CommitLineData
394f4f17 1/* Test program for returning the canonical absolute name of a given file.
bfff8b1b 2 Copyright (C) 1996-2017 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 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
394f4f17
RM
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
e25054c4
AJ
30/* Prototype for our test function. */
31extern int do_test (int argc, char *argv[]);
32#include <test-skeleton.c>
33
a3f4b50b
UD
34#ifndef PATH_MAX
35# define PATH_MAX 4096
36#endif
2303f5fd 37static char cwd[PATH_MAX];
394f4f17
RM
38static size_t cwd_len;
39
40struct {
41 const char * name;
42 const char * value;
43} symlinks[] = {
44 {"SYMLINK_LOOP", "SYMLINK_LOOP"},
45 {"SYMLINK_1", "."},
46 {"SYMLINK_2", "//////./../../etc"},
47 {"SYMLINK_3", "SYMLINK_1"},
48 {"SYMLINK_4", "SYMLINK_2"},
49 {"SYMLINK_5", "doesNotExist"},
50};
51
52struct {
53 const char * in, * out, * resolved;
ec4b0518 54 int error;
394f4f17
RM
55} tests[] = {
56 /* 0 */
57 {"/", "/"},
58 {"/////////////////////////////////", "/"},
59 {"/.././.././.././..///", "/"},
60 {"/etc", "/etc"},
e25054c4 61 {"/etc/../etc", "/etc"},
394f4f17
RM
62 /* 5 */
63 {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT},
64 {"./././././././././.", "."},
65 {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT},
66 {"./doesExist", "./doesExist"},
67 {"./doesExist/", "./doesExist"},
68 /* 10 */
69 {"./doesExist/../doesExist", "./doesExist"},
70 {"foobar", 0, "./foobar", ENOENT},
71 {".", "."},
72 {"./foobar", 0, "./foobar", ENOENT},
73 {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
74 /* 15 */
75 {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
76 {"SYMLINK_1", "."},
77 {"SYMLINK_1/foobar", 0, "./foobar", ENOENT},
78 {"SYMLINK_2", "/etc"},
79 {"SYMLINK_3", "."},
80 /* 20 */
81 {"SYMLINK_4", "/etc"},
82 {"../stdlib/SYMLINK_1", "."},
83 {"../stdlib/SYMLINK_2", "/etc"},
84 {"../stdlib/SYMLINK_3", "."},
85 {"../stdlib/SYMLINK_4", "/etc"},
86 /* 25 */
87 {"./SYMLINK_5", 0, "./doesNotExist", ENOENT},
88 {"SYMLINK_5", 0, "./doesNotExist", ENOENT},
89 {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT},
90 {"doesExist/../../stdlib/doesExist", "./doesExist"},
0f888d8e
UD
91 {"doesExist/.././../stdlib/.", "."},
92 /* 30 */
93 {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR},
94 {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR},
394f4f17
RM
95};
96
97
cf3141a5 98static int
394f4f17
RM
99check_path (const char * result, const char * expected)
100{
101 int good;
102
103 if (!result)
104 return (expected == NULL);
105
106 if (!expected)
107 return 0;
108
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
115 return good;
116}
117
118
d68171ed 119int
e25054c4 120do_test (int argc, char ** argv)
394f4f17
RM
121{
122 char * result;
0f888d8e 123 int i, errors = 0;
394f4f17
RM
124 char buf[PATH_MAX];
125
126 getcwd (cwd, sizeof(buf));
127 cwd_len = strlen (cwd);
128
86187531
UD
129 errno = 0;
130 if (realpath (NULL, buf) != NULL || errno != EINVAL)
131 {
132 printf ("%s: expected return value NULL and errno set to EINVAL"
133 " for realpath(NULL,...)\n", argv[0]);
134 ++errors;
135 }
136
f7501ae6
UD
137#if 0
138 /* This is now allowed. The test is invalid. */
86187531
UD
139 errno = 0;
140 if (realpath ("/", NULL) != NULL || errno != EINVAL)
141 {
142 printf ("%s: expected return value NULL and errno set to EINVAL"
143 " for realpath(...,NULL)\n", argv[0]);
144 ++errors;
145 }
f7501ae6 146#endif
86187531
UD
147
148 errno = 0;
149 if (realpath ("", buf) != NULL || errno != ENOENT)
150 {
cc3fa755 151 printf ("%s: expected return value NULL and set errno to ENOENT"
86187531
UD
152 " for realpath(\"\",...)\n", argv[0]);
153 ++errors;
154 }
155
d68171ed 156 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
394f4f17
RM
157 symlink (symlinks[i].value, symlinks[i].name);
158
0f888d8e
UD
159 int has_dir = mkdir ("doesExist", 0777) == 0;
160
161 int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
394f4f17 162
d68171ed 163 for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
394f4f17
RM
164 {
165 buf[0] = '\0';
166 result = realpath (tests[i].in, buf);
167
168 if (!check_path (result, tests[i].out))
169 {
170 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
171 argv[0], i, tests[i].out ? tests[i].out : "NULL",
172 result ? result : "NULL");
173 ++errors;
174 continue;
175 }
176
177 if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
178 {
179 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
180 argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
181 buf);
182 ++errors;
183 continue;
184 }
185
ec4b0518 186 if (!tests[i].out && errno != tests[i].error)
394f4f17
RM
187 {
188 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
d68171ed 189 argv[0], i, tests[i].error, errno);
394f4f17
RM
190 ++errors;
191 continue;
192 }
71b1675e
UD
193
194 char *result2 = realpath (tests[i].in, NULL);
195 if ((result2 == NULL && result != NULL)
196 || (result2 != NULL && strcmp (result, result2) != 0))
197 {
198 printf ("\
199%s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
200 argv[0], result2, result);
201 ++errors;
202 }
203 free (result2);
394f4f17
RM
204 }
205
206 getcwd (buf, sizeof(buf));
207 if (strcmp (buf, cwd))
208 {
209 printf ("%s: current working directory changed from %s to %s\n",
210 argv[0], cwd, buf);
211 ++errors;
212 }
213
214 if (fd >= 0)
a1260d92
UD
215 {
216 close (fd);
217 unlink ("doesExist/someFile");
218 }
0f888d8e
UD
219
220 if (has_dir)
221 rmdir ("doesExist");
394f4f17 222
d68171ed 223 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
394f4f17
RM
224 unlink (symlinks[i].name);
225
d68171ed 226 if (errors != 0)
394f4f17
RM
227 {
228 printf ("%d errors.\n", errors);
e25054c4 229 return EXIT_FAILURE;
394f4f17 230 }
d68171ed
UD
231
232 puts ("No errors.");
233 return EXIT_SUCCESS;
394f4f17 234}