1*433d6423SLionel Sambuc /* Tests for MINIX3 realpath(3) - by Erik van der Kouwe */
2*433d6423SLionel Sambuc #include <assert.h>
3*433d6423SLionel Sambuc #include <dirent.h>
4*433d6423SLionel Sambuc #include <errno.h>
5*433d6423SLionel Sambuc #include <libgen.h>
6*433d6423SLionel Sambuc #include <limits.h>
7*433d6423SLionel Sambuc #include <stdio.h>
8*433d6423SLionel Sambuc #include <stdlib.h>
9*433d6423SLionel Sambuc #include <string.h>
10*433d6423SLionel Sambuc #include <sys/stat.h>
11*433d6423SLionel Sambuc #include <unistd.h>
12*433d6423SLionel Sambuc
13*433d6423SLionel Sambuc int max_error = 3;
14*433d6423SLionel Sambuc #include "common.h"
15*433d6423SLionel Sambuc
16*433d6423SLionel Sambuc
17*433d6423SLionel Sambuc int subtest;
18*433d6423SLionel Sambuc static const char *executable;
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc
21*433d6423SLionel Sambuc #define ERR (e(__LINE__))
22*433d6423SLionel Sambuc
remove_last_path_component(char * path)23*433d6423SLionel Sambuc static char *remove_last_path_component(char *path)
24*433d6423SLionel Sambuc {
25*433d6423SLionel Sambuc char *current, *last;
26*433d6423SLionel Sambuc
27*433d6423SLionel Sambuc assert(path);
28*433d6423SLionel Sambuc
29*433d6423SLionel Sambuc /* find last slash */
30*433d6423SLionel Sambuc last = NULL;
31*433d6423SLionel Sambuc for (current = path; *current; current++)
32*433d6423SLionel Sambuc if (*current == '/')
33*433d6423SLionel Sambuc last = current;
34*433d6423SLionel Sambuc
35*433d6423SLionel Sambuc /* check path component */
36*433d6423SLionel Sambuc if (last)
37*433d6423SLionel Sambuc {
38*433d6423SLionel Sambuc if (strcmp(last + 1, ".") == 0) ERR;
39*433d6423SLionel Sambuc if (strcmp(last + 1, "..") == 0) ERR;
40*433d6423SLionel Sambuc }
41*433d6423SLionel Sambuc
42*433d6423SLionel Sambuc /* if only root path slash, we are done */
43*433d6423SLionel Sambuc if (last <= path)
44*433d6423SLionel Sambuc return NULL;
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc /* chop off last path component */
47*433d6423SLionel Sambuc *last = 0;
48*433d6423SLionel Sambuc return path;
49*433d6423SLionel Sambuc }
50*433d6423SLionel Sambuc
check_path_components(const char * path)51*433d6423SLionel Sambuc static int check_path_components(const char *path)
52*433d6423SLionel Sambuc {
53*433d6423SLionel Sambuc char buffer[PATH_MAX + 1], *bufferpos;
54*433d6423SLionel Sambuc struct stat statbuf;
55*433d6423SLionel Sambuc
56*433d6423SLionel Sambuc assert(strlen(path) < sizeof(buffer));
57*433d6423SLionel Sambuc
58*433d6423SLionel Sambuc bufferpos = buffer;
59*433d6423SLionel Sambuc while (*path)
60*433d6423SLionel Sambuc {
61*433d6423SLionel Sambuc /* copy next path segment */
62*433d6423SLionel Sambuc do
63*433d6423SLionel Sambuc {
64*433d6423SLionel Sambuc *(bufferpos++) = *(path++);
65*433d6423SLionel Sambuc } while (*path && *path != '/');
66*433d6423SLionel Sambuc *bufferpos = 0;
67*433d6423SLionel Sambuc
68*433d6423SLionel Sambuc /*
69*433d6423SLionel Sambuc * is this a valid path segment? if not, return errno.
70*433d6423SLionel Sambuc * one exception: the last path component need not exist
71*433d6423SLionel Sambuc * - unless it is actually a dangling symlink
72*433d6423SLionel Sambuc */
73*433d6423SLionel Sambuc if (stat(buffer, &statbuf) < 0 &&
74*433d6423SLionel Sambuc (*path || errno != ENOENT ||
75*433d6423SLionel Sambuc lstat(buffer, &statbuf) == 0))
76*433d6423SLionel Sambuc return errno;
77*433d6423SLionel Sambuc }
78*433d6423SLionel Sambuc
79*433d6423SLionel Sambuc return 0;
80*433d6423SLionel Sambuc }
81*433d6423SLionel Sambuc
check_realpath(const char * path,int expected_errno)82*433d6423SLionel Sambuc static void check_realpath(const char *path, int expected_errno)
83*433d6423SLionel Sambuc {
84*433d6423SLionel Sambuc char buffer[PATH_MAX + 1], *resolved_path;
85*433d6423SLionel Sambuc int expected_errno2;
86*433d6423SLionel Sambuc struct stat statbuf[2];
87*433d6423SLionel Sambuc
88*433d6423SLionel Sambuc assert(path);
89*433d6423SLionel Sambuc
90*433d6423SLionel Sambuc /* any errors in the path that realpath should report? */
91*433d6423SLionel Sambuc expected_errno2 = check_path_components(path);
92*433d6423SLionel Sambuc
93*433d6423SLionel Sambuc /* run realpath */
94*433d6423SLionel Sambuc errno = 0;
95*433d6423SLionel Sambuc resolved_path = realpath(path, buffer);
96*433d6423SLionel Sambuc
97*433d6423SLionel Sambuc /* do we get errors when expected? */
98*433d6423SLionel Sambuc if (expected_errno || expected_errno2)
99*433d6423SLionel Sambuc {
100*433d6423SLionel Sambuc if (resolved_path) ERR;
101*433d6423SLionel Sambuc if (errno != expected_errno && errno != expected_errno2) ERR;
102*433d6423SLionel Sambuc return;
103*433d6423SLionel Sambuc }
104*433d6423SLionel Sambuc
105*433d6423SLionel Sambuc /* do we get success when expected? */
106*433d6423SLionel Sambuc if (!resolved_path)
107*433d6423SLionel Sambuc {
108*433d6423SLionel Sambuc ERR;
109*433d6423SLionel Sambuc return;
110*433d6423SLionel Sambuc }
111*433d6423SLionel Sambuc errno = 0;
112*433d6423SLionel Sambuc
113*433d6423SLionel Sambuc /* do the paths point to the same file? (only check if exists) */
114*433d6423SLionel Sambuc if (stat(path, &statbuf[0]) < 0)
115*433d6423SLionel Sambuc {
116*433d6423SLionel Sambuc if (errno != ENOENT) { ERR; return; }
117*433d6423SLionel Sambuc }
118*433d6423SLionel Sambuc else
119*433d6423SLionel Sambuc {
120*433d6423SLionel Sambuc if (stat(resolved_path, &statbuf[1]) < 0) { ERR; return; }
121*433d6423SLionel Sambuc if (statbuf[0].st_dev != statbuf[1].st_dev) ERR;
122*433d6423SLionel Sambuc if (statbuf[0].st_ino != statbuf[1].st_ino) ERR;
123*433d6423SLionel Sambuc }
124*433d6423SLionel Sambuc
125*433d6423SLionel Sambuc /* is the path absolute? */
126*433d6423SLionel Sambuc if (resolved_path[0] != '/') ERR;
127*433d6423SLionel Sambuc
128*433d6423SLionel Sambuc /* is each path element allowable? */
129*433d6423SLionel Sambuc while (remove_last_path_component(resolved_path))
130*433d6423SLionel Sambuc {
131*433d6423SLionel Sambuc /* not a symlink? */
132*433d6423SLionel Sambuc if (lstat(resolved_path, &statbuf[1]) < 0) { ERR; return; }
133*433d6423SLionel Sambuc if ((statbuf[1].st_mode & S_IFMT) != S_IFDIR) ERR;
134*433d6423SLionel Sambuc }
135*433d6423SLionel Sambuc }
136*433d6423SLionel Sambuc
check_realpath_step_by_step(const char * path,int expected_errno)137*433d6423SLionel Sambuc static void check_realpath_step_by_step(const char *path, int expected_errno)
138*433d6423SLionel Sambuc {
139*433d6423SLionel Sambuc char buffer[PATH_MAX + 1];
140*433d6423SLionel Sambuc const char *path_current;
141*433d6423SLionel Sambuc
142*433d6423SLionel Sambuc assert(path);
143*433d6423SLionel Sambuc assert(strlen(path) < sizeof(buffer));
144*433d6423SLionel Sambuc
145*433d6423SLionel Sambuc /* check the absolute path */
146*433d6423SLionel Sambuc check_realpath(path, expected_errno);
147*433d6423SLionel Sambuc
148*433d6423SLionel Sambuc /* try with different CWDs */
149*433d6423SLionel Sambuc for (path_current = path; *path_current; path_current++)
150*433d6423SLionel Sambuc if (path_current[0] == '/' && path_current[1])
151*433d6423SLionel Sambuc {
152*433d6423SLionel Sambuc /* set CWD */
153*433d6423SLionel Sambuc memcpy(buffer, path, path_current - path + 1);
154*433d6423SLionel Sambuc buffer[path_current - path + 1] = 0;
155*433d6423SLionel Sambuc if (chdir(buffer) < 0) { ERR; continue; }
156*433d6423SLionel Sambuc
157*433d6423SLionel Sambuc /* perform test */
158*433d6423SLionel Sambuc check_realpath(path_current + 1, expected_errno);
159*433d6423SLionel Sambuc }
160*433d6423SLionel Sambuc }
161*433d6423SLionel Sambuc
pathncat(char * buffer,size_t size,const char * path1,const char * path2)162*433d6423SLionel Sambuc static char *pathncat(char *buffer, size_t size, const char *path1, const char *path2)
163*433d6423SLionel Sambuc {
164*433d6423SLionel Sambuc size_t len1, len2, lenslash;
165*433d6423SLionel Sambuc
166*433d6423SLionel Sambuc assert(buffer);
167*433d6423SLionel Sambuc assert(path1);
168*433d6423SLionel Sambuc assert(path2);
169*433d6423SLionel Sambuc
170*433d6423SLionel Sambuc /* check whether it fits */
171*433d6423SLionel Sambuc len1 = strlen(path1);
172*433d6423SLionel Sambuc len2 = strlen(path2);
173*433d6423SLionel Sambuc lenslash = (len1 > 0 && path1[len1 - 1] == '/') ? 0 : 1;
174*433d6423SLionel Sambuc if (len1 >= size || /* check individual components to avoid overflow */
175*433d6423SLionel Sambuc len2 >= size ||
176*433d6423SLionel Sambuc len1 + len2 + lenslash >= size)
177*433d6423SLionel Sambuc return NULL;
178*433d6423SLionel Sambuc
179*433d6423SLionel Sambuc /* perform the copy */
180*433d6423SLionel Sambuc memcpy(buffer, path1, len1);
181*433d6423SLionel Sambuc if (lenslash)
182*433d6423SLionel Sambuc buffer[len1] = '/';
183*433d6423SLionel Sambuc
184*433d6423SLionel Sambuc memcpy(buffer + len1 + lenslash, path2, len2 + 1);
185*433d6423SLionel Sambuc return buffer;
186*433d6423SLionel Sambuc }
187*433d6423SLionel Sambuc
check_realpath_recurse(const char * path,int depth)188*433d6423SLionel Sambuc static void check_realpath_recurse(const char *path, int depth)
189*433d6423SLionel Sambuc {
190*433d6423SLionel Sambuc DIR *dir;
191*433d6423SLionel Sambuc struct dirent *dirent;
192*433d6423SLionel Sambuc char pathsub[PATH_MAX + 1];
193*433d6423SLionel Sambuc struct stat st;
194*433d6423SLionel Sambuc
195*433d6423SLionel Sambuc /* check with the path itself */
196*433d6423SLionel Sambuc check_realpath_step_by_step(path, 0);
197*433d6423SLionel Sambuc
198*433d6423SLionel Sambuc /* don't go too deep */
199*433d6423SLionel Sambuc if (depth < 1)
200*433d6423SLionel Sambuc return;
201*433d6423SLionel Sambuc
202*433d6423SLionel Sambuc /* don't bother with non-directories. Due to timeouts in drivers we
203*433d6423SLionel Sambuc * might not get expected results and takes way too long */
204*433d6423SLionel Sambuc if (stat(path, &st) != 0) {
205*433d6423SLionel Sambuc /* dangling symlinks may cause legitimate failures here */
206*433d6423SLionel Sambuc if (lstat(path, &st) != 0) ERR;
207*433d6423SLionel Sambuc return;
208*433d6423SLionel Sambuc }
209*433d6423SLionel Sambuc if (!S_ISDIR(st.st_mode))
210*433d6423SLionel Sambuc return;
211*433d6423SLionel Sambuc
212*433d6423SLionel Sambuc /* loop through subdirectories (including . and ..) */
213*433d6423SLionel Sambuc if (!(dir = opendir(path)))
214*433d6423SLionel Sambuc {
215*433d6423SLionel Sambuc /* Opening some special files might result in errors when the
216*433d6423SLionel Sambuc * corresponding hardware is not present, or simply when access
217*433d6423SLionel Sambuc * rights prohibit access (e.g., /dev/log).
218*433d6423SLionel Sambuc */
219*433d6423SLionel Sambuc if (errno != ENOTDIR
220*433d6423SLionel Sambuc && errno != ENXIO && errno != EIO && errno != EACCES) {
221*433d6423SLionel Sambuc ERR;
222*433d6423SLionel Sambuc }
223*433d6423SLionel Sambuc return;
224*433d6423SLionel Sambuc }
225*433d6423SLionel Sambuc while ((dirent = readdir(dir)) != NULL)
226*433d6423SLionel Sambuc {
227*433d6423SLionel Sambuc /* build path */
228*433d6423SLionel Sambuc if (!pathncat(pathsub, sizeof(pathsub), path, dirent->d_name))
229*433d6423SLionel Sambuc {
230*433d6423SLionel Sambuc ERR;
231*433d6423SLionel Sambuc continue;
232*433d6423SLionel Sambuc }
233*433d6423SLionel Sambuc
234*433d6423SLionel Sambuc /* check path */
235*433d6423SLionel Sambuc check_realpath_recurse(pathsub, depth - 1);
236*433d6423SLionel Sambuc }
237*433d6423SLionel Sambuc if (closedir(dir) < 0) ERR;
238*433d6423SLionel Sambuc }
239*433d6423SLionel Sambuc
240*433d6423SLionel Sambuc #define PATH_DEPTH 4
241*433d6423SLionel Sambuc #define PATH_BASE "/."
242*433d6423SLionel Sambuc #define L(x) PATH_BASE "/link_" #x ".tmp"
243*433d6423SLionel Sambuc
244*433d6423SLionel Sambuc static char basepath[PATH_MAX + 1];
245*433d6423SLionel Sambuc
addbasepath(char * buffer,const char * path)246*433d6423SLionel Sambuc static char *addbasepath(char *buffer, const char *path)
247*433d6423SLionel Sambuc {
248*433d6423SLionel Sambuc size_t basepathlen, pathlen;
249*433d6423SLionel Sambuc
250*433d6423SLionel Sambuc /* assumption: both start with slash and neither end with it */
251*433d6423SLionel Sambuc assert(basepath[0] == '/');
252*433d6423SLionel Sambuc assert(basepath[strlen(basepath) - 1] != '/');
253*433d6423SLionel Sambuc assert(buffer);
254*433d6423SLionel Sambuc assert(path);
255*433d6423SLionel Sambuc assert(path[0] == '/');
256*433d6423SLionel Sambuc
257*433d6423SLionel Sambuc /* check result length */
258*433d6423SLionel Sambuc basepathlen = strlen(basepath);
259*433d6423SLionel Sambuc pathlen = strlen(path);
260*433d6423SLionel Sambuc if (basepathlen + pathlen > PATH_MAX)
261*433d6423SLionel Sambuc {
262*433d6423SLionel Sambuc printf("path too long\n");
263*433d6423SLionel Sambuc exit(-1);
264*433d6423SLionel Sambuc }
265*433d6423SLionel Sambuc
266*433d6423SLionel Sambuc /* concatenate base path and path */
267*433d6423SLionel Sambuc memcpy(buffer, basepath, basepathlen);
268*433d6423SLionel Sambuc memcpy(buffer + basepathlen, path, pathlen + 1);
269*433d6423SLionel Sambuc return buffer;
270*433d6423SLionel Sambuc }
271*433d6423SLionel Sambuc
test_dirname(const char * path,const char * exp)272*433d6423SLionel Sambuc static void test_dirname(const char *path, const char *exp)
273*433d6423SLionel Sambuc {
274*433d6423SLionel Sambuc char buffer[PATH_MAX];
275*433d6423SLionel Sambuc int i, j;
276*433d6423SLionel Sambuc size_t pathlen = strlen(path);
277*433d6423SLionel Sambuc char *pathout;
278*433d6423SLionel Sambuc
279*433d6423SLionel Sambuc assert(pathlen + 3 < PATH_MAX);
280*433d6423SLionel Sambuc
281*433d6423SLionel Sambuc /* try with no, one or two trailing slashes */
282*433d6423SLionel Sambuc for (i = 0; i < 3; i++)
283*433d6423SLionel Sambuc {
284*433d6423SLionel Sambuc /* no trailing slashes for empty string */
285*433d6423SLionel Sambuc if (pathlen < 1 && i > 0)
286*433d6423SLionel Sambuc continue;
287*433d6423SLionel Sambuc
288*433d6423SLionel Sambuc /* prepare buffer */
289*433d6423SLionel Sambuc strcpy(buffer, path);
290*433d6423SLionel Sambuc for (j = 0; j < i; j++)
291*433d6423SLionel Sambuc buffer[pathlen + j] = '/';
292*433d6423SLionel Sambuc
293*433d6423SLionel Sambuc buffer[pathlen + i] = 0;
294*433d6423SLionel Sambuc
295*433d6423SLionel Sambuc /* perform test */
296*433d6423SLionel Sambuc pathout = dirname(buffer);
297*433d6423SLionel Sambuc if (strcmp(pathout, exp) != 0)
298*433d6423SLionel Sambuc ERR;
299*433d6423SLionel Sambuc }
300*433d6423SLionel Sambuc }
301*433d6423SLionel Sambuc
main(int argc,char ** argv)302*433d6423SLionel Sambuc int main(int argc, char **argv)
303*433d6423SLionel Sambuc {
304*433d6423SLionel Sambuc char buffer1[PATH_MAX + 1], buffer2[PATH_MAX + 1];
305*433d6423SLionel Sambuc subtest = 1;
306*433d6423SLionel Sambuc
307*433d6423SLionel Sambuc /* initialize */
308*433d6423SLionel Sambuc start(43);
309*433d6423SLionel Sambuc executable = argv[0];
310*433d6423SLionel Sambuc getcwd(basepath, sizeof(basepath));
311*433d6423SLionel Sambuc
312*433d6423SLionel Sambuc /* prepare some symlinks to make it more difficult */
313*433d6423SLionel Sambuc if (symlink("/", addbasepath(buffer1, L(1))) < 0) ERR;
314*433d6423SLionel Sambuc if (symlink(basepath, addbasepath(buffer1, L(2))) < 0) ERR;
315*433d6423SLionel Sambuc
316*433d6423SLionel Sambuc /* perform some tests */
317*433d6423SLionel Sambuc check_realpath_recurse(basepath, PATH_DEPTH);
318*433d6423SLionel Sambuc
319*433d6423SLionel Sambuc /* now try with recursive symlinks */
320*433d6423SLionel Sambuc if (symlink(addbasepath(buffer1, L(3)), addbasepath(buffer2, L(3))) < 0) ERR;
321*433d6423SLionel Sambuc if (symlink(addbasepath(buffer1, L(5)), addbasepath(buffer2, L(4))) < 0) ERR;
322*433d6423SLionel Sambuc if (symlink(addbasepath(buffer1, L(4)), addbasepath(buffer2, L(5))) < 0) ERR;
323*433d6423SLionel Sambuc check_realpath_step_by_step(addbasepath(buffer1, L(3)), ELOOP);
324*433d6423SLionel Sambuc check_realpath_step_by_step(addbasepath(buffer1, L(4)), ELOOP);
325*433d6423SLionel Sambuc check_realpath_step_by_step(addbasepath(buffer1, L(5)), ELOOP);
326*433d6423SLionel Sambuc
327*433d6423SLionel Sambuc /* delete the symlinks */
328*433d6423SLionel Sambuc cleanup();
329*433d6423SLionel Sambuc
330*433d6423SLionel Sambuc /* also test dirname */
331*433d6423SLionel Sambuc test_dirname("", ".");
332*433d6423SLionel Sambuc test_dirname(".", ".");
333*433d6423SLionel Sambuc test_dirname("..", ".");
334*433d6423SLionel Sambuc test_dirname("x", ".");
335*433d6423SLionel Sambuc test_dirname("xy", ".");
336*433d6423SLionel Sambuc test_dirname("x/y", "x");
337*433d6423SLionel Sambuc test_dirname("xy/z", "xy");
338*433d6423SLionel Sambuc test_dirname("x/yz", "x");
339*433d6423SLionel Sambuc test_dirname("ab/cd", "ab");
340*433d6423SLionel Sambuc test_dirname("x//y", "x");
341*433d6423SLionel Sambuc test_dirname("xy//z", "xy");
342*433d6423SLionel Sambuc test_dirname("x//yz", "x");
343*433d6423SLionel Sambuc test_dirname("ab//cd", "ab");
344*433d6423SLionel Sambuc test_dirname("/", "/");
345*433d6423SLionel Sambuc test_dirname("/x", "/");
346*433d6423SLionel Sambuc test_dirname("/xy", "/");
347*433d6423SLionel Sambuc test_dirname("/x/y", "/x");
348*433d6423SLionel Sambuc test_dirname("/xy/z", "/xy");
349*433d6423SLionel Sambuc test_dirname("/x/yz", "/x");
350*433d6423SLionel Sambuc test_dirname("/ab/cd", "/ab");
351*433d6423SLionel Sambuc test_dirname("/x//y", "/x");
352*433d6423SLionel Sambuc test_dirname("/xy//z", "/xy");
353*433d6423SLionel Sambuc test_dirname("/x//yz", "/x");
354*433d6423SLionel Sambuc test_dirname("/ab//cd", "/ab");
355*433d6423SLionel Sambuc test_dirname("/usr/src", "/usr");
356*433d6423SLionel Sambuc test_dirname("/usr/src/test", "/usr/src");
357*433d6423SLionel Sambuc test_dirname("usr/src", "usr");
358*433d6423SLionel Sambuc test_dirname("usr/src/test", "usr/src");
359*433d6423SLionel Sambuc
360*433d6423SLionel Sambuc /* done */
361*433d6423SLionel Sambuc quit();
362*433d6423SLionel Sambuc return(-1); /* impossible */
363*433d6423SLionel Sambuc }
364