source: branches/libc-0.6/src/libctests/glibc/stdlib/test-canon.c

Last change on this file was 2089, checked in by bird, 20 years ago

testcase adjustments.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* Test program for returning the canonical absolute name of a given file.
2 Copyright (C) 1996,1997,2000,2002,2004,2005 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
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
31/* Prototype for our test function. */
32extern int do_test (int argc, char *argv[]);
33#include <test-skeleton.c>
34
35#ifndef PATH_MAX
36# define PATH_MAX 4096
37#endif
38static char cwd[PATH_MAX];
39static size_t cwd_len;
40
41struct {
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
53struct {
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
99static int
100check_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 {
115 good = (strcmp (expected, result) == 0);
116#ifdef __EMX__
117 if (!good && expected[0] == '/' && result[1] == ':' && result[2] == '/')
118 good = (strcmp (expected, result + 2) == 0);
119#endif
120 }
121
122 return good;
123}
124
125
126int
127do_test (int argc, char ** argv)
128{
129 char * result;
130 int i, errors = 0;
131 char buf[PATH_MAX];
132
133 getcwd (cwd, sizeof(buf));
134 cwd_len = strlen (cwd);
135
136 errno = 0;
137 if (realpath (NULL, buf) != 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
144#if 0
145 /* This is now allowed. The test is invalid. */
146 errno = 0;
147 if (realpath ("/", NULL) != NULL || errno != EINVAL)
148 {
149 printf ("%s: expected return value NULL and errno set to EINVAL"
150 " for realpath(...,NULL)\n", argv[0]);
151 ++errors;
152 }
153#endif
154
155 errno = 0;
156 if (realpath ("", buf) != NULL || errno != ENOENT)
157 {
158 printf ("%s: expected return value NULL and set errno to ENOENT"
159 " for realpath(\"\",...)\n", argv[0]);
160 ++errors;
161 }
162
163 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
164 symlink (symlinks[i].value, symlinks[i].name);
165
166 int rmdir_etc = mkdir ("/etc", 0777) == 0;
167
168 int has_dir = mkdir ("doesExist", 0777) == 0;
169
170 int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
171
172 for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
173 {
174 buf[0] = '\0';
175 result = realpath (tests[i].in, buf);
176
177 if (!check_path (result, tests[i].out))
178 {
179 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
180 argv[0], i, tests[i].out ? tests[i].out : "NULL",
181 result ? result : "NULL");
182 ++errors;
183 continue;
184 }
185
186 if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
187 {
188 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
189 argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
190 buf);
191 ++errors;
192 continue;
193 }
194
195 if (!tests[i].out && errno != tests[i].error)
196 {
197 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
198 argv[0], i, tests[i].error, errno);
199 ++errors;
200 continue;
201 }
202
203 char *result2 = realpath (tests[i].in, NULL);
204 if ((result2 == NULL && result != NULL)
205 || (result2 != NULL && strcmp (result, result2) != 0))
206 {
207 printf ("\
208%s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
209 argv[0], result2, result);
210 ++errors;
211 }
212 free (result2);
213 }
214
215 getcwd (buf, sizeof(buf));
216 if (strcmp (buf, cwd))
217 {
218 printf ("%s: current working directory changed from %s to %s\n",
219 argv[0], cwd, buf);
220 ++errors;
221 }
222
223#ifdef __EMX__
224 if (fd >= 0) /* cannot unlink open files */
225 close (fd);
226#endif
227 if (fd >= 0)
228 unlink ("doesExist/someFile");
229
230 if (has_dir)
231 rmdir ("doesExist");
232
233 if (rmdir_etc)
234 rmdir ("/etc");
235
236 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
237 unlink (symlinks[i].name);
238
239 if (errors != 0)
240 {
241 printf ("%d errors.\n", errors);
242 return EXIT_FAILURE;
243 }
244
245 puts ("No errors.");
246 return EXIT_SUCCESS;
247}
Note: See TracBrowser for help on using the repository browser.