| 1 |
/* Tests of readlink. |
| 2 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Eric Blake <[email protected]>, 2009. */ |
| 18 |
|
| 19 |
/* This file is designed to test both readlink(a,b,c) and |
| 20 |
readlinkat(AT_FDCWD,a,b,c). FUNC is the function to test. Assumes |
| 21 |
that BASE and ASSERT are already defined, and that appropriate |
| 22 |
headers are already included. If PRINT, warn before skipping |
| 23 |
symlink tests with status 77. */ |
| 24 |
|
| 25 |
static int |
| 26 |
test_readlink (ssize_t (*func) (char const *, char *, size_t), bool print) |
| 27 |
{ |
| 28 |
char buf[80]; |
| 29 |
|
| 30 |
/* Sanity checks of failures. Mingw lacks symlink, but readlink can |
| 31 |
still distinguish between various errors. */ |
| 32 |
memset (buf, 0xff, sizeof buf); |
| 33 |
errno = 0; |
| 34 |
ASSERT (func ("no_such", buf, sizeof buf) == -1); |
| 35 |
ASSERT (errno == ENOENT); |
| 36 |
errno = 0; |
| 37 |
ASSERT (func ("no_such/", buf, sizeof buf) == -1); |
| 38 |
ASSERT (errno == ENOENT); |
| 39 |
errno = 0; |
| 40 |
ASSERT (func ("", buf, sizeof buf) == -1); |
| 41 |
ASSERT (errno == ENOENT || errno == EINVAL |
| 42 |
|| errno == EBADF /* Cygwin < 3.5 */); |
| 43 |
errno = 0; |
| 44 |
ASSERT (func (".", buf, sizeof buf) == -1); |
| 45 |
ASSERT (errno == EINVAL); |
| 46 |
errno = 0; |
| 47 |
ASSERT (func ("./", buf, sizeof buf) == -1); |
| 48 |
ASSERT (errno == EINVAL); |
| 49 |
ASSERT (close (creat (BASE "file", 0600)) == 0); |
| 50 |
errno = 0; |
| 51 |
ASSERT (func (BASE "file", buf, sizeof buf) == -1); |
| 52 |
ASSERT (errno == EINVAL); |
| 53 |
errno = 0; |
| 54 |
ASSERT (func (BASE "file/", buf, sizeof buf) == -1); |
| 55 |
ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */ |
| 56 |
|
| 57 |
/* Now test actual symlinks. */ |
| 58 |
if (symlink (BASE "dir", BASE "link")) |
| 59 |
{ |
| 60 |
ASSERT (unlink (BASE "file") == 0); |
| 61 |
if (print) |
| 62 |
fputs ("skipping test: symlinks not supported on this file system\n", |
| 63 |
stderr); |
| 64 |
return 77; |
| 65 |
} |
| 66 |
ASSERT (mkdir (BASE "dir", 0700) == 0); |
| 67 |
errno = 0; |
| 68 |
ASSERT (func (BASE "link/", buf, sizeof buf) == -1); |
| 69 |
ASSERT (errno == EINVAL); |
| 70 |
ASSERT (symlink (BASE "link", BASE "link2") == 0); |
| 71 |
errno = 0; |
| 72 |
ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); |
| 73 |
ASSERT (errno == EINVAL); |
| 74 |
ASSERT (unlink (BASE "link2") == 0); |
| 75 |
ASSERT (symlink (BASE "file", BASE "link2") == 0); |
| 76 |
errno = 0; |
| 77 |
ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); |
| 78 |
ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */ |
| 79 |
ASSERT (unlink (BASE "file") == 0); |
| 80 |
ASSERT (unlink (BASE "link2") == 0); |
| 81 |
{ |
| 82 |
/* Up till now, no readlink has been successful, so buf should be |
| 83 |
unchanged. */ |
| 84 |
int i; |
| 85 |
for (i = 0; i < sizeof buf; i++) |
| 86 |
ASSERT (buf[i] == (char) 0xff); |
| 87 |
} |
| 88 |
{ |
| 89 |
size_t len = strlen (BASE "dir"); |
| 90 |
/* When passing too small of a buffer, expect the truncated |
| 91 |
length, or an ERANGE failure. However, a size of 0 is not |
| 92 |
portable enough to test. */ |
| 93 |
ssize_t result; |
| 94 |
errno = 0; |
| 95 |
result = readlink (BASE "link", buf, 1); |
| 96 |
if (result == -1) |
| 97 |
{ |
| 98 |
ASSERT (errno == ERANGE); |
| 99 |
ASSERT (buf[0] == (char) 0xff); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
ASSERT (result == 1); |
| 104 |
ASSERT (buf[0] == BASE[0]); |
| 105 |
} |
| 106 |
ASSERT (buf[1] == (char) 0xff); |
| 107 |
ASSERT (func (BASE "link", buf, len) == len); |
| 108 |
ASSERT (strncmp (buf, BASE "dir", len) == 0); |
| 109 |
ASSERT (buf[len] == (char) 0xff); |
| 110 |
ASSERT (func (BASE "link", buf, sizeof buf) == len); |
| 111 |
ASSERT (strncmp (buf, BASE "dir", len) == 0); |
| 112 |
/* POSIX says rest of buf is unspecified; but in practice, it is |
| 113 |
either left alone, or NUL-terminated. */ |
| 114 |
ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff); |
| 115 |
} |
| 116 |
{ |
| 117 |
/* On Cygwin 3.3.6, readlink("/dev/null") returns "\\Device\\Null", which |
| 118 |
is unusable. Verify that gnulib works around this nonsense. */ |
| 119 |
ssize_t result = func ("/dev/null", buf, sizeof buf); |
| 120 |
ASSERT (result == -1 || buf[0] != '\\'); |
| 121 |
} |
| 122 |
ASSERT (rmdir (BASE "dir") == 0); |
| 123 |
ASSERT (unlink (BASE "link") == 0); |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |