maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-mkdir.h
blobe22d07ad4fc327935257f1137ed123d500c21a94
1 /* Test of mkdir() function.
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 /* This file is designed to test both mkdir(a,b) and
18 mkdirat(AT_FDCWD,a,b). FUNC is the function to test. Assumes that
19 BASE and ASSERT are already defined, and that appropriate headers
20 are already included. If PRINT, warn before skipping tests with
21 status 77 when symlinks are unsupported. */
23 static int
24 test_mkdir (int (*func) (char const *, mode_t), bool print)
26 /* Test basic error handling. */
27 ASSERT (close (creat (BASE "file", 0600)) == 0);
28 errno = 0;
29 ASSERT (func (BASE "file", 0700) == -1);
30 ASSERT (errno == EEXIST);
31 errno = 0;
32 ASSERT (func (BASE "file/", 0700) == -1);
33 ASSERT (errno == ENOTDIR || errno == EEXIST);
34 errno = 0;
35 ASSERT (func (BASE "file/dir", 0700) == -1);
36 ASSERT (errno == ENOTDIR || errno == ENOENT || errno == EOPNOTSUPP);
37 ASSERT (unlink (BASE "file") == 0);
38 errno = 0;
39 ASSERT (func ("", 0700) == -1);
40 ASSERT (errno == ENOENT);
41 errno = 0;
42 ASSERT (func (BASE "dir/sub", 0700) == -1);
43 ASSERT (errno == ENOENT);
44 errno = 0;
45 ASSERT (func (BASE "dir/.", 0700) == -1);
46 ASSERT (errno == ENOENT);
47 errno = 0;
48 ASSERT (func (BASE "dir/.//", 0700) == -1);
49 ASSERT (errno == ENOENT);
51 /* Test trailing slash handling. */
52 ASSERT (func (BASE "dir", 0700) == 0);
53 errno = 0;
54 ASSERT (func (BASE "dir", 0700) == -1);
55 ASSERT (errno == EEXIST);
56 ASSERT (rmdir (BASE "dir") == 0);
57 ASSERT (func (BASE "dir/", 0700) == 0);
58 errno = 0;
59 ASSERT (func (BASE "dir/", 0700) == -1);
60 ASSERT (errno == EEXIST);
61 ASSERT (rmdir (BASE "dir") == 0);
63 /* Test symlink behavior. POSIX requires the creation of
64 directories through a dangling symlink with trailing slash, but
65 GNU does not yet implement that, so we support either behavior
66 for now. */
67 if (symlink (BASE "dir", BASE "link"))
69 if (print)
70 fputs ("skipping test: symlinks not supported on this file system\n",
71 stderr);
72 return 77;
74 errno = 0;
75 ASSERT (func (BASE "link", 0700) == -1);
76 ASSERT (errno == EEXIST);
78 int result;
79 errno = 0;
80 result = func (BASE "link/", 0700);
81 if (!result)
82 ASSERT (rmdir (BASE "dir") == 0);
83 else
85 ASSERT (result == -1);
86 ASSERT (errno == EEXIST);
87 errno = 0;
88 ASSERT (rmdir (BASE "dir") == -1);
89 ASSERT (errno == ENOENT);
92 errno = 0;
93 ASSERT (func (BASE "link/.", 0700) == -1);
94 ASSERT (errno == ENOENT);
95 ASSERT (unlink (BASE "link") == 0);
97 return 0;