1 /* $NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $");
45 static char path[] = "node";
47 ATF_TC_WITH_CLEANUP(mknod_err);
48 ATF_TC_HEAD(mknod_err, tc)
50 atf_tc_set_md_var(tc, "descr",
51 "Test error conditions of mknod(2) (PR kern/45111)");
52 atf_tc_set_md_var(tc, "require.user", "root");
55 ATF_TC_BODY(mknod_err, tc)
57 char buf[PATH_MAX + 1];
59 (void)memset(buf, 'x', sizeof(buf));
63 * As of FreeBSD 6.0 device nodes may be created in regular file systems but
64 * such nodes cannot be used to access devices. As a result an invalid dev
65 * argument is unchecked.
68 ATF_REQUIRE_ERRNO(EINVAL, mknod(path, S_IFCHR, -1) == -1);
72 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mknod(buf, S_IFCHR, 0) == -1);
75 ATF_REQUIRE_ERRNO(EFAULT, mknod((char *)-1, S_IFCHR, 0) == -1);
78 ATF_REQUIRE_ERRNO(ENOENT, mknod("/a/b/c/d/e/f/g", S_IFCHR, 0) == -1);
81 ATF_TC_CLEANUP(mknod_err, tc)
86 ATF_TC_WITH_CLEANUP(mknod_exist);
87 ATF_TC_HEAD(mknod_exist, tc)
89 atf_tc_set_md_var(tc, "descr", "Test EEXIST from mknod(2)");
90 atf_tc_set_md_var(tc, "require.user", "root");
93 ATF_TC_BODY(mknod_exist, tc)
97 fd = open("/etc/passwd", O_RDONLY);
104 ATF_REQUIRE_ERRNO(EEXIST,
105 mknod("/etc/passwd", S_IFCHR, 0) == -1);
108 ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
111 ATF_REQUIRE_ERRNO(EEXIST, mknod(path, S_IFCHR, 0) == -1);
113 ATF_REQUIRE(unlink(path) == 0);
116 ATF_TC_CLEANUP(mknod_exist, tc)
121 ATF_TC_WITH_CLEANUP(mknod_perm);
122 ATF_TC_HEAD(mknod_perm, tc)
124 atf_tc_set_md_var(tc, "descr", "Test permissions of mknod(2)");
125 atf_tc_set_md_var(tc, "require.user", "unprivileged");
128 ATF_TC_BODY(mknod_perm, tc)
132 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFCHR, 0) == -1);
135 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFBLK, 0) == -1);
138 ATF_TC_CLEANUP(mknod_perm, tc)
143 ATF_TC_WITH_CLEANUP(mknod_stat);
144 ATF_TC_HEAD(mknod_stat, tc)
146 atf_tc_set_md_var(tc, "descr", "A basic test of mknod(2)");
147 atf_tc_set_md_var(tc, "require.user", "root");
150 ATF_TC_BODY(mknod_stat, tc)
154 (void)memset(&st, 0, sizeof(struct stat));
156 ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
157 ATF_REQUIRE(stat(path, &st) == 0);
159 if (S_ISCHR(st.st_mode) == 0)
160 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFCHR)");
162 ATF_REQUIRE(unlink(path) == 0);
164 (void)memset(&st, 0, sizeof(struct stat));
166 ATF_REQUIRE(mknod(path, S_IFBLK, 0) == 0);
167 ATF_REQUIRE(stat(path, &st) == 0);
169 if (S_ISBLK(st.st_mode) == 0)
170 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFBLK)");
172 ATF_REQUIRE(unlink(path) == 0);
174 (void)memset(&st, 0, sizeof(struct stat));
177 atf_tc_expect_fail("mknod does not allow S_IFREG");
179 ATF_REQUIRE(mknod(path, S_IFREG, 0) == 0);
180 ATF_REQUIRE(stat(path, &st) == 0);
182 if (S_ISREG(st.st_mode) == 0)
183 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFREG)");
185 ATF_REQUIRE(unlink(path) == 0);
188 ATF_TC_CLEANUP(mknod_stat, tc)
196 ATF_TP_ADD_TC(tp, mknod_err);
197 ATF_TP_ADD_TC(tp, mknod_exist);
198 ATF_TP_ADD_TC(tp, mknod_perm);
199 ATF_TP_ADD_TC(tp, mknod_stat);
201 return atf_no_error();