summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys/t_mkdir.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/sys/t_mkdir.c')
-rw-r--r--src/regress/lib/libc/sys/t_mkdir.c213
1 files changed, 213 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_mkdir.c b/src/regress/lib/libc/sys/t_mkdir.c
new file mode 100644
index 0000000000..41df34ace9
--- /dev/null
+++ b/src/regress/lib/libc/sys/t_mkdir.c
@@ -0,0 +1,213 @@
1/* $OpenBSD: t_mkdir.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_mkdir.c,v 1.2 2011/10/15 07:38:31 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2008, 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe and Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/cdefs.h>
36__COPYRIGHT("@(#) Copyright (c) 2008\
37 The NetBSD Foundation, inc. All rights reserved.");
38__RCSID("$NetBSD: t_mkdir.c,v 1.2 2011/10/15 07:38:31 jruoho Exp $");
39
40#include <sys/stat.h>
41#include <sys/wait.h>
42
43#include "atf-c.h"
44#include <errno.h>
45#include <fcntl.h>
46#include <limits.h>
47#include <pwd.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53ATF_TC(mkdir_err);
54ATF_TC_HEAD(mkdir_err, tc)
55{
56 atf_tc_set_md_var(tc, "descr", "Checks errors from mkdir(2)");
57}
58
59ATF_TC_BODY(mkdir_err, tc)
60{
61 char buf[PATH_MAX + 1];
62 int fd;
63
64 (void)memset(buf, 'x', sizeof(buf));
65
66 fd = open("/etc", O_RDONLY);
67
68 if (fd >= 0) {
69
70 (void)close(fd);
71
72 errno = 0;
73 ATF_REQUIRE_ERRNO(EEXIST, mkdir("/etc", 0500) == -1);
74 }
75
76 errno = 0;
77 ATF_REQUIRE_ERRNO(EFAULT, mkdir((void *)-1, 0500) == -1);
78
79 errno = 0;
80 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkdir(buf, 0500) == -1);
81
82 errno = 0;
83 ATF_REQUIRE_ERRNO(ENOENT, mkdir("/a/b/c/d/e/f/g/h/i/j/k", 0500) == -1);
84}
85
86ATF_TC_WITH_CLEANUP(mkdir_perm);
87ATF_TC_HEAD(mkdir_perm, tc)
88{
89 atf_tc_set_md_var(tc, "descr", "Checks permissions with mkdir(2)");
90 atf_tc_set_md_var(tc, "require.user", "unprivileged");
91}
92
93ATF_TC_BODY(mkdir_perm, tc)
94{
95 errno = 0;
96 ATF_REQUIRE_ERRNO(EACCES, mkdir("/usr/__nonexistent__", 0500) == -1);
97}
98
99ATF_TC_CLEANUP(mkdir_perm, tc)
100{
101 (void)rmdir("/usr/__nonexistent__");
102}
103
104ATF_TC_WITH_CLEANUP(mkdir_mode);
105ATF_TC_HEAD(mkdir_mode, tc)
106{
107 atf_tc_set_md_var(tc, "descr", "Test that UIDs and GIDs are right "
108 "for a directory created with mkdir(2)");
109 atf_tc_set_md_var(tc, "require.user", "root");
110}
111
112ATF_TC_BODY(mkdir_mode, tc)
113{
114 static const char *path = "/tmp/mkdir";
115 struct stat st_a, st_b;
116 struct passwd *pw;
117 pid_t pid;
118 int sta;
119
120 (void)memset(&st_a, 0, sizeof(struct stat));
121 (void)memset(&st_b, 0, sizeof(struct stat));
122
123 pw = getpwnam("nobody");
124
125 ATF_REQUIRE(pw != NULL);
126 ATF_REQUIRE(stat("/tmp", &st_a) == 0);
127
128 pid = fork();
129 ATF_REQUIRE(pid >= 0);
130
131 if (pid == 0) {
132
133 if (setuid(pw->pw_uid) != 0)
134 _exit(EXIT_FAILURE);
135
136 if (mkdir(path, 0500) != 0)
137 _exit(EXIT_FAILURE);
138
139 _exit(EXIT_SUCCESS);
140 }
141
142 (void)sleep(1);
143 (void)wait(&sta);
144
145 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
146 atf_tc_fail("failed to create '%s'", path);
147
148 ATF_REQUIRE(stat(path, &st_b) == 0);
149 ATF_REQUIRE(rmdir(path) == 0);
150
151 /*
152 * The directory's owner ID should be set to the
153 * effective UID, whereas the group ID should be
154 * set to that of the parent directory.
155 */
156 if (st_b.st_uid != pw->pw_uid)
157 atf_tc_fail("invalid UID for '%s'", path);
158
159 if (st_b.st_gid != st_a.st_gid)
160 atf_tc_fail("GID did not follow the parent directory");
161}
162
163ATF_TC_CLEANUP(mkdir_mode, tc)
164{
165 (void)rmdir("/tmp/mkdir");
166}
167
168ATF_TC(mkdir_trail);
169ATF_TC_HEAD(mkdir_trail, tc)
170{
171 atf_tc_set_md_var(tc, "descr", "Checks mkdir(2) for trailing slashes");
172}
173
174ATF_TC_BODY(mkdir_trail, tc)
175{
176 const char *tests[] = {
177 /*
178 * IEEE 1003.1 second ed. 2.2.2.78:
179 *
180 * If the pathname refers to a directory, it may also have
181 * one or more trailing slashes. Multiple successive slashes
182 * are considered to be the same as one slash.
183 */
184 "dir1/",
185 "dir2//",
186
187 NULL,
188 };
189
190 const char **test;
191
192 for (test = &tests[0]; *test != NULL; ++test) {
193
194 (void)printf("Checking \"%s\"\n", *test);
195 (void)rmdir(*test);
196
197 ATF_REQUIRE(mkdir(*test, 0777) == 0);
198 ATF_REQUIRE(rename(*test, "foo") == 0);
199 ATF_REQUIRE(rename("foo/", *test) == 0);
200 ATF_REQUIRE(rmdir(*test) == 0);
201 }
202}
203
204ATF_TP_ADD_TCS(tp)
205{
206
207 ATF_TP_ADD_TC(tp, mkdir_err);
208 ATF_TP_ADD_TC(tp, mkdir_perm);
209 ATF_TP_ADD_TC(tp, mkdir_mode);
210 ATF_TP_ADD_TC(tp, mkdir_trail);
211
212 return atf_no_error();
213}