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