Update.
[glibc.git] / stdlib / test-canon.c
blob596de7d488dbe5c2909cb2befe29c7ab70068ca3
1 /* Test program for returning the canonical absolute name of a given file.
2 Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by David Mosberger <davidm@azstarnet.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This file must be run from within a directory called "stdlib". */
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>
31 /* Prototype for our test function. */
32 extern int do_test (int argc, char *argv[]);
33 #include <test-skeleton.c>
35 #ifndef PATH_MAX
36 # define PATH_MAX 4096
37 #endif
38 static char cwd[PATH_MAX];
39 static size_t cwd_len;
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"},
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/.", "."}
96 static int
97 check_path (const char * result, const char * expected)
99 int good;
101 if (!result)
102 return (expected == NULL);
104 if (!expected)
105 return 0;
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);
113 return good;
118 do_test (int argc, char ** argv)
120 char * result;
121 int fd, i, errors = 0;
122 char buf[PATH_MAX];
124 getcwd (cwd, sizeof(buf));
125 cwd_len = strlen (cwd);
127 errno = 0;
128 if (realpath (NULL, buf) != NULL || errno != EINVAL)
130 printf ("%s: expected return value NULL and errno set to EINVAL"
131 " for realpath(NULL,...)\n", argv[0]);
132 ++errors;
135 errno = 0;
136 if (realpath ("/", NULL) != NULL || errno != EINVAL)
138 printf ("%s: expected return value NULL and errno set to EINVAL"
139 " for realpath(...,NULL)\n", argv[0]);
140 ++errors;
143 errno = 0;
144 if (realpath ("", buf) != NULL || errno != ENOENT)
146 printf ("%s: expected return value NULL and set errno to ENOENT"
147 " for realpath(\"\",...)\n", argv[0]);
148 ++errors;
151 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
152 symlink (symlinks[i].value, symlinks[i].name);
154 fd = open("doesExist", O_CREAT | O_EXCL, 0777);
156 for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
158 buf[0] = '\0';
159 result = realpath (tests[i].in, buf);
161 if (!check_path (result, tests[i].out))
163 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
164 argv[0], i, tests[i].out ? tests[i].out : "NULL",
165 result ? result : "NULL");
166 ++errors;
167 continue;
170 if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
172 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
173 argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
174 buf);
175 ++errors;
176 continue;
179 if (!tests[i].out && errno != tests[i].error)
181 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
182 argv[0], i, tests[i].error, errno);
183 ++errors;
184 continue;
188 getcwd (buf, sizeof(buf));
189 if (strcmp (buf, cwd))
191 printf ("%s: current working directory changed from %s to %s\n",
192 argv[0], cwd, buf);
193 ++errors;
196 if (fd >= 0)
197 unlink("doesExist");
199 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
200 unlink (symlinks[i].name);
202 if (errors != 0)
204 printf ("%d errors.\n", errors);
205 return EXIT_FAILURE;
208 puts ("No errors.");
209 return EXIT_SUCCESS;