attribute: const/pure defaults to unsequenced/reproducible
[gnulib.git] / tests / test-unlink.h
blob800537db6d155e5dfc0478f2faea9e43544508bd
1 /* Tests of unlink.
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 unlink(n) and
20 unlinkat(AT_FDCWD,n,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, then warn before returning
23 status 77 when symlinks are unsupported. */
25 static int
26 test_unlink_func (int (*func) (char const *name), bool print)
28 /* Setup. */
29 ASSERT (mkdir (BASE "dir", 0700) == 0);
30 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
32 /* Basic error conditions. */
33 errno = 0;
34 ASSERT (func ("") == -1);
35 ASSERT (errno == ENOENT);
36 errno = 0;
37 ASSERT (func (BASE "nosuch") == -1);
38 ASSERT (errno == ENOENT);
39 errno = 0;
40 ASSERT (func (BASE "nosuch/") == -1);
41 ASSERT (errno == ENOENT);
42 /* Resulting errno after directories is rather varied across
43 implementations (EPERM, EINVAL, EACCES, EBUSY, EISDIR, ENOTSUP);
44 however, we must be careful to not attempt unlink on a directory
45 unless we know it must fail. */
46 if (cannot_unlink_dir ())
48 ASSERT (func (".") == -1);
49 ASSERT (func ("..") == -1);
50 ASSERT (func ("/") == -1);
51 ASSERT (func (BASE "dir") == -1);
52 ASSERT (mkdir (BASE "dir1", 0700) == 0);
53 ASSERT (func (BASE "dir1") == -1);
54 ASSERT (rmdir (BASE "dir1") == 0);
56 errno = 0;
57 ASSERT (func (BASE "dir/file/") == -1);
58 ASSERT (errno == ENOTDIR);
60 /* Test symlink behavior. Specifying trailing slash will attempt
61 unlink of a directory, so only attempt it if we know it must
62 fail. */
63 if (symlink (BASE "dir", BASE "link") != 0)
65 ASSERT (func (BASE "dir/file") == 0);
66 ASSERT (rmdir (BASE "dir") == 0);
67 if (print)
68 fputs ("skipping test: symlinks not supported on this file system\n",
69 stderr);
70 return 77;
72 if (cannot_unlink_dir ())
73 ASSERT (func (BASE "link/") == -1);
74 ASSERT (func (BASE "link") == 0);
75 ASSERT (symlink (BASE "dir/file", BASE "link") == 0);
76 errno = 0;
77 ASSERT (func (BASE "link/") == -1);
78 ASSERT (errno == ENOTDIR);
79 /* Order here proves unlink of a symlink does not follow through to
80 the file. */
81 ASSERT (func (BASE "link") == 0);
82 ASSERT (func (BASE "dir/file") == 0);
83 ASSERT (rmdir (BASE "dir") == 0);
85 return 0;