1 /* $NetBSD: t_mkdir.c,v 1.2 2011/10/15 07:38:31 jruoho Exp $ */
4 * Copyright (c) 2008, 2011 The NetBSD Foundation, Inc.
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
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 $");
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];
61 (void)memset(buf
, 'x', sizeof(buf
));
63 fd
= open("/etc", O_RDONLY
);
70 ATF_REQUIRE_ERRNO(EEXIST
, mkdir("/etc", 0500) == -1);
74 ATF_REQUIRE_ERRNO(EFAULT
, mkdir((void *)-1, 0500) == -1);
77 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, mkdir(buf
, 0500) == -1);
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
)
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
;
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);
126 ATF_REQUIRE(pid
>= 0);
130 if (setuid(pw
->pw_uid
) != 0)
133 if (mkdir(path
, 0500) != 0)
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");
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.
189 for (test
= &tests
[0]; *test
!= NULL
; ++test
) {
191 (void)printf("Checking \"%s\"\n", *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);
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();