1 /* Tests of areadlink and friends.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 /* This file is designed to test areadlink(a),
20 areadlink_with_size(a,b), and areadlinkat(AT_FDCWD,a). FUNC is the
21 function to test; a length is always supplied, but may be ignored.
22 Assumes that BASE and ASSERT are already defined, and that
23 appropriate headers are already included. If PRINT, warn before
24 skipping symlink tests with status 77. */
27 test_areadlink (char * (*func
) (char const *, size_t), bool print
)
29 /* Sanity checks of failures. Mingw lacks symlink, but areadlink can
30 still distinguish between various errors. */
32 ASSERT (func ("no_such", 1) == NULL
);
33 ASSERT (errno
== ENOENT
);
35 ASSERT (func ("no_such/", 1) == NULL
);
36 ASSERT (errno
== ENOENT
);
38 ASSERT (func ("", 1) == NULL
);
39 ASSERT (errno
== ENOENT
|| errno
== EINVAL
);
41 ASSERT (func (".", 1) == NULL
);
42 ASSERT (errno
== EINVAL
);
44 ASSERT (func ("./", 1) == NULL
);
45 ASSERT (errno
== EINVAL
);
46 ASSERT (close (creat (BASE
"file", 0600)) == 0);
48 ASSERT (func (BASE
"file", 1) == NULL
);
49 ASSERT (errno
== EINVAL
);
51 ASSERT (func (BASE
"file/", 1) == NULL
);
52 ASSERT (errno
== ENOTDIR
|| errno
== EINVAL
); /* AIX yields EINVAL */
53 ASSERT (unlink (BASE
"file") == 0);
55 /* Now test actual symlinks. */
56 if (symlink (BASE
"dir", BASE
"link"))
59 fputs ("skipping test: symlinks not supported on this file system\n",
63 ASSERT (mkdir (BASE
"dir", 0700) == 0);
65 ASSERT (func (BASE
"link/", 1) == NULL
);
66 ASSERT (errno
== EINVAL
);
68 /* Too small a guess is okay. */
69 char *buf
= func (BASE
"link", 1);
71 ASSERT (strcmp (buf
, BASE
"dir") == 0);
73 /* Too large a guess is okay. */
74 buf
= func (BASE
"link", 10000000);
76 ASSERT (strcmp (buf
, BASE
"dir") == 0);
79 ASSERT (rmdir (BASE
"dir") == 0);
80 ASSERT (unlink (BASE
"link") == 0);