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 both stat(n,buf) and
20 fstatat(AT_FDCWD,n,buf,0). 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. */
26 test_stat_func (int (*func
) (char const *, struct stat
*), bool print
)
30 char *cwd
= getcwd (NULL
, 0);
33 ASSERT (func (".", &st1
) == 0);
34 ASSERT (func ("./", &st2
) == 0);
35 #if !(defined _WIN32 && !defined __CYGWIN__ && !_GL_WINDOWS_STAT_INODES)
36 ASSERT (psame_inode (&st1
, &st2
));
38 ASSERT (func (cwd
, &st2
) == 0);
39 #if !(defined _WIN32 && !defined __CYGWIN__ && !_GL_WINDOWS_STAT_INODES)
40 ASSERT (psame_inode (&st1
, &st2
));
42 ASSERT (func ("/", &st1
) == 0);
43 ASSERT (func ("///", &st2
) == 0);
44 #if !(defined _WIN32 && !defined __CYGWIN__ && !_GL_WINDOWS_STAT_INODES)
45 ASSERT (psame_inode (&st1
, &st2
));
49 ASSERT (func ("", &st1
) == -1);
50 ASSERT (errno
== ENOENT
);
52 ASSERT (func ("nosuch", &st1
) == -1);
53 ASSERT (errno
== ENOENT
);
55 ASSERT (func ("nosuch/", &st1
) == -1);
56 ASSERT (errno
== ENOENT
);
58 ASSERT (close (creat (BASE
"file", 0600)) == 0);
59 ASSERT (func (BASE
"file", &st1
) == 0);
61 ASSERT (func (BASE
"file/", &st1
) == -1);
62 ASSERT (errno
== ENOTDIR
);
64 /* Now for some symlink tests, where supported. We set up:
69 then test behavior with trailing slash.
71 if (symlink (".", BASE
"link1") != 0)
73 ASSERT (unlink (BASE
"file") == 0);
75 fputs ("skipping test: symlinks not supported on this file system\n",
79 ASSERT (symlink (BASE
"file", BASE
"link2") == 0);
80 ASSERT (symlink (BASE
"nosuch", BASE
"link3") == 0);
81 ASSERT (symlink (BASE
"link4", BASE
"link4") == 0);
83 ASSERT (func (BASE
"link1/", &st1
) == 0);
84 ASSERT (S_ISDIR (st1
.st_mode
));
87 ASSERT (func (BASE
"link2/", &st1
) == -1);
88 ASSERT (errno
== ENOTDIR
);
91 ASSERT (func (BASE
"link3/", &st1
) == -1);
92 ASSERT (errno
== ENOENT
);
95 ASSERT (func (BASE
"link4/", &st1
) == -1);
96 ASSERT (errno
== ELOOP
);
99 ASSERT (unlink (BASE
"file") == 0);
100 ASSERT (unlink (BASE
"link1") == 0);
101 ASSERT (unlink (BASE
"link2") == 0);
102 ASSERT (unlink (BASE
"link3") == 0);
103 ASSERT (unlink (BASE
"link4") == 0);