openat: don’t close (-1)
[gnulib.git] / tests / test-areadlink.h
blobe4fc225d7cbaed2d408f93a2a376bee92ef99ea7
1 /* Tests of areadlink and friends.
2 Copyright (C) 2009-2024 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. */
26 static int
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. */
31 errno = 0;
32 ASSERT (func ("no_such", 1) == NULL);
33 ASSERT (errno == ENOENT);
34 errno = 0;
35 ASSERT (func ("no_such/", 1) == NULL);
36 ASSERT (errno == ENOENT);
37 errno = 0;
38 ASSERT (func ("", 1) == NULL);
39 ASSERT (errno == ENOENT || errno == EINVAL
40 || errno == EBADF /* Cygwin < 3.5 */);
41 errno = 0;
42 ASSERT (func (".", 1) == NULL);
43 ASSERT (errno == EINVAL);
44 errno = 0;
45 ASSERT (func ("./", 1) == NULL);
46 ASSERT (errno == EINVAL);
47 ASSERT (close (creat (BASE "file", 0600)) == 0);
48 errno = 0;
49 ASSERT (func (BASE "file", 1) == NULL);
50 ASSERT (errno == EINVAL);
51 errno = 0;
52 ASSERT (func (BASE "file/", 1) == NULL);
53 ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */
54 ASSERT (unlink (BASE "file") == 0);
56 /* Now test actual symlinks. */
57 if (symlink (BASE "dir", BASE "link"))
59 if (print)
60 fputs ("skipping test: symlinks not supported on this file system\n",
61 stderr);
62 return 77;
64 ASSERT (mkdir (BASE "dir", 0700) == 0);
65 errno = 0;
66 ASSERT (func (BASE "link/", 1) == NULL);
67 ASSERT (errno == EINVAL);
69 /* Too small a guess is okay. */
70 char *buf = func (BASE "link", 1);
71 ASSERT (buf);
72 ASSERT (strcmp (buf, BASE "dir") == 0);
73 free (buf);
74 /* Too large a guess is okay. */
75 buf = func (BASE "link", 10000000);
76 ASSERT (buf);
77 ASSERT (strcmp (buf, BASE "dir") == 0);
78 free (buf);
80 ASSERT (rmdir (BASE "dir") == 0);
81 ASSERT (unlink (BASE "link") == 0);
83 return 0;