1 /* Test program for returning the canonical absolute name of a given file.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This file must be run from within a directory called "stdlib". */
27 #include <sys/param.h>
30 /* Prototype for our test function. */
31 extern int do_test (int argc
, char *argv
[]);
32 #include <test-skeleton.c>
35 # define PATH_MAX 4096
37 static char cwd
[PATH_MAX
];
38 static size_t cwd_len
;
44 {"SYMLINK_LOOP", "SYMLINK_LOOP"},
46 {"SYMLINK_2", "//////./../../etc"},
47 {"SYMLINK_3", "SYMLINK_1"},
48 {"SYMLINK_4", "SYMLINK_2"},
49 {"SYMLINK_5", "doesNotExist"},
53 const char * in
, * out
, * resolved
;
58 {"/////////////////////////////////", "/"},
59 {"/.././.././.././..///", "/"},
61 {"/etc/../etc", "/etc"},
63 {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT
},
64 {"./././././././././.", "."},
65 {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT
},
66 {"./doesExist", "./doesExist"},
67 {"./doesExist/", "./doesExist"},
69 {"./doesExist/../doesExist", "./doesExist"},
70 {"foobar", 0, "./foobar", ENOENT
},
72 {"./foobar", 0, "./foobar", ENOENT
},
73 {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP
},
75 {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP
},
77 {"SYMLINK_1/foobar", 0, "./foobar", ENOENT
},
78 {"SYMLINK_2", "/etc"},
81 {"SYMLINK_4", "/etc"},
82 {"../stdlib/SYMLINK_1", "."},
83 {"../stdlib/SYMLINK_2", "/etc"},
84 {"../stdlib/SYMLINK_3", "."},
85 {"../stdlib/SYMLINK_4", "/etc"},
87 {"./SYMLINK_5", 0, "./doesNotExist", ENOENT
},
88 {"SYMLINK_5", 0, "./doesNotExist", ENOENT
},
89 {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT
},
90 {"doesExist/../../stdlib/doesExist", "./doesExist"},
91 {"doesExist/.././../stdlib/.", "."},
93 {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR
},
94 {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR
},
99 check_path (const char * result
, const char * expected
)
104 return (expected
== NULL
);
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);
113 good
= (strcmp (expected
, result
) == 0);
120 do_test (int argc
, char ** argv
)
126 getcwd (cwd
, sizeof (buf
));
127 cwd_len
= strlen (cwd
);
130 if (realpath (NULL
, buf
) != NULL
|| errno
!= EINVAL
)
132 printf ("%s: expected return value NULL and errno set to EINVAL"
133 " for realpath(NULL,...)\n", argv
[0]);
138 /* This is now allowed. The test is invalid. */
140 if (realpath ("/", NULL
) != NULL
|| errno
!= EINVAL
)
142 printf ("%s: expected return value NULL and errno set to EINVAL"
143 " for realpath(...,NULL)\n", argv
[0]);
149 if (realpath ("", buf
) != NULL
|| errno
!= ENOENT
)
151 printf ("%s: expected return value NULL and set errno to ENOENT"
152 " for realpath(\"\",...)\n", argv
[0]);
156 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
157 symlink (symlinks
[i
].value
, symlinks
[i
].name
);
159 int has_dir
= mkdir ("doesExist", 0777) == 0;
161 int fd
= has_dir
? creat ("doesExist/someFile", 0777) : -1;
163 for (i
= 0; i
< (int) (sizeof (tests
) / sizeof (tests
[0])); ++i
)
166 result
= realpath (tests
[i
].in
, buf
);
168 if (!check_path (result
, tests
[i
].out
))
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");
177 /* Verify buf contents if the call succeeded or failed with ENOENT. */
178 if ((result
!= NULL
|| errno
== ENOENT
)
179 && !check_path (buf
, tests
[i
].out
? tests
[i
].out
: tests
[i
].resolved
))
181 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
182 argv
[0], i
, tests
[i
].out
? tests
[i
].out
: tests
[i
].resolved
,
188 if (!tests
[i
].out
&& errno
!= tests
[i
].error
)
190 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
191 argv
[0], i
, tests
[i
].error
, errno
);
196 char *result2
= realpath (tests
[i
].in
, NULL
);
197 if ((result2
== NULL
&& result
!= NULL
)
198 || (result2
!= NULL
&& strcmp (result
, result2
) != 0))
201 %s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
202 argv
[0], result2
, result
);
208 getcwd (buf
, sizeof (buf
));
209 if (strcmp (buf
, cwd
))
211 printf ("%s: current working directory changed from %s to %s\n",
219 unlink ("doesExist/someFile");
225 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
226 unlink (symlinks
[i
].name
);
230 printf ("%d errors.\n", errors
);