1 /* 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>.
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.
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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 /* This file must be run from within a directory called "stdlib". */
29 #include <sys/param.h>
32 /* Prototype for our test function. */
33 extern int do_test (int argc
, char *argv
[]);
34 #include "../test-skeleton.c"
37 # define PATH_MAX 4096
39 static char cwd
[PATH_MAX
];
40 static size_t cwd_len
;
46 {"SYMLINK_LOOP", "SYMLINK_LOOP"},
48 {"SYMLINK_2", "//////./../../etc"},
49 {"SYMLINK_3", "SYMLINK_1"},
50 {"SYMLINK_4", "SYMLINK_2"},
51 {"SYMLINK_5", "doesNotExist"},
56 const char * retval
; /* what realpath should return */
57 const char * retbuf
; /* what realpath should store in buf */
58 /* if both of the above are NULL, we won't check for result,
60 int error
; /* expected errno value */
64 {"/////////////////////////////////", "/"},
65 {"/.././.././.././..///", "/"},
67 {"/etc/../etc", "/etc"},
69 {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT
},
70 {"./././././././././.", "."},
71 {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT
},
72 {"./doesExist", "./doesExist"},
73 {"./doesExist/", "./doesExist"},
75 {"./doesExist/../doesExist", "./doesExist"},
76 {"foobar", 0, "./foobar", ENOENT
},
78 {"./foobar", 0, "./foobar", ENOENT
},
79 {"SYMLINK_LOOP", 0, 0, ELOOP
},
81 {"./SYMLINK_LOOP", 0, 0, ELOOP
},
83 {"SYMLINK_1/foobar", 0, "./foobar", ENOENT
},
84 {"SYMLINK_2", "/etc"},
87 {"SYMLINK_4", "/etc"},
88 {"../stdlib/SYMLINK_1", "."},
89 {"../stdlib/SYMLINK_2", "/etc"},
90 {"../stdlib/SYMLINK_3", "."},
91 {"../stdlib/SYMLINK_4", "/etc"},
93 {"./SYMLINK_5", 0, "./doesNotExist", ENOENT
},
94 {"SYMLINK_5", 0, "./doesNotExist", ENOENT
},
95 {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT
},
96 {"doesExist/../../stdlib/doesExist", "./doesExist"},
97 {"doesExist/.././../stdlib/.", "."},
99 /* we dont check for ENOTDIR in readlink() which causes failures to
100 * propogate up to realpath() ... so disable for now ... */
102 {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR
},
103 {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR
},
109 check_path (const char * result
, const char * expected
)
114 return (expected
== NULL
);
119 if (expected
[0] == '.' && (expected
[1] == '/' || expected
[1] == '\0'))
120 good
= (strncmp (result
, cwd
, cwd_len
) == 0
121 && strcmp (result
+ cwd_len
, expected
+ 1) == 0);
123 good
= (strcmp (expected
, result
) == 0);
130 do_test (int argc
, char ** argv
)
136 getcwd (cwd
, sizeof(buf
));
137 cwd_len
= strlen (cwd
);
140 /* we choose to crash in uClibc when given a NULL */
142 if (realpath (NULL
, buf
) != NULL
|| errno
!= EINVAL
)
144 printf ("%s: expected return value NULL and errno set to EINVAL"
145 " for realpath(NULL,...)\n", argv
[0]);
151 /* This is now allowed. The test is invalid. */
153 if (realpath ("/", NULL
) != NULL
|| errno
!= EINVAL
)
155 printf ("%s: expected return value NULL and errno set to EINVAL"
156 " for realpath(...,NULL)\n", argv
[0]);
162 if (realpath ("", buf
) != NULL
|| errno
!= ENOENT
)
164 printf ("%s: expected return value NULL and set errno to ENOENT"
165 " for realpath(\"\",...)\n", argv
[0]);
169 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
170 symlink (symlinks
[i
].value
, symlinks
[i
].name
);
172 int has_dir
= mkdir ("doesExist", 0777) == 0;
174 int fd
= has_dir
? creat ("doesExist/someFile", 0777) : -1;
176 for (i
= 0; i
< (int) (sizeof (tests
) / sizeof (tests
[0])); ++i
)
180 result
= realpath (tests
[i
].in
, buf
);
182 if (!check_path (result
, tests
[i
].retval
))
184 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
185 argv
[0], i
, tests
[i
].retval
? tests
[i
].retval
: "NULL",
186 result
? result
: "NULL");
191 if (result
&& !check_path (buf
, tests
[i
].retval
? tests
[i
].retval
: tests
[i
].retbuf
))
193 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
194 argv
[0], i
, tests
[i
].retval
? tests
[i
].retval
: tests
[i
].retbuf
,
200 if (errno
!= tests
[i
].error
)
202 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
203 argv
[0], i
, tests
[i
].error
, errno
);
209 /* we choose to crash in uClibc when given a NULL */
210 char *result2
= realpath (tests
[i
].in
, NULL
);
211 if ((result2
== NULL
&& result
!= NULL
)
212 || (result2
!= NULL
&& strcmp (result
, result2
) != 0))
215 %s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
216 argv
[0], result2
, result
);
223 getcwd (buf
, sizeof(buf
));
224 if (strcmp (buf
, cwd
))
226 printf ("%s: current working directory changed from %s to %s\n",
234 unlink ("doesExist/someFile");
240 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
241 unlink (symlinks
[i
].name
);
245 printf ("%d errors.\n", errors
);