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
));
62 ATF_REQUIRE_ERRNO(EINVAL
, mknod(path
, S_IFCHR
, -1) == -1);
65 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, mknod(buf
, S_IFCHR
, 0) == -1);
68 ATF_REQUIRE_ERRNO(EFAULT
, mknod((char *)-1, S_IFCHR
, 0) == -1);
71 ATF_REQUIRE_ERRNO(ENOENT
, mknod("/a/b/c/d/e/f/g", S_IFCHR
, 0) == -1);
74 ATF_TC_CLEANUP(mknod_err
, tc
)
79 ATF_TC_WITH_CLEANUP(mknod_exist
);
80 ATF_TC_HEAD(mknod_exist
, tc
)
82 atf_tc_set_md_var(tc
, "descr", "Test EEXIST from mknod(2)");
83 atf_tc_set_md_var(tc
, "require.user", "root");
86 ATF_TC_BODY(mknod_exist
, tc
)
90 fd
= open("/etc/passwd", O_RDONLY
);
97 ATF_REQUIRE_ERRNO(EEXIST
,
98 mknod("/etc/passwd", S_IFCHR
, 0) == -1);
101 ATF_REQUIRE(mknod(path
, S_IFCHR
, 0) == 0);
104 ATF_REQUIRE_ERRNO(EEXIST
, mknod(path
, S_IFCHR
, 0) == -1);
106 ATF_REQUIRE(unlink(path
) == 0);
109 ATF_TC_CLEANUP(mknod_exist
, tc
)
114 ATF_TC_WITH_CLEANUP(mknod_perm
);
115 ATF_TC_HEAD(mknod_perm
, tc
)
117 atf_tc_set_md_var(tc
, "descr", "Test permissions of mknod(2)");
118 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
121 ATF_TC_BODY(mknod_perm
, tc
)
125 ATF_REQUIRE_ERRNO(EPERM
, mknod(path
, S_IFCHR
, 0) == -1);
128 ATF_REQUIRE_ERRNO(EPERM
, mknod(path
, S_IFBLK
, 0) == -1);
131 ATF_TC_CLEANUP(mknod_perm
, tc
)
136 ATF_TC_WITH_CLEANUP(mknod_stat
);
137 ATF_TC_HEAD(mknod_stat
, tc
)
139 atf_tc_set_md_var(tc
, "descr", "A basic test of mknod(2)");
140 atf_tc_set_md_var(tc
, "require.user", "root");
143 ATF_TC_BODY(mknod_stat
, tc
)
147 (void)memset(&st
, 0, sizeof(struct stat
));
149 ATF_REQUIRE(mknod(path
, S_IFCHR
, 0) == 0);
150 ATF_REQUIRE(stat(path
, &st
) == 0);
152 if (S_ISCHR(st
.st_mode
) == 0)
153 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFCHR)");
155 ATF_REQUIRE(unlink(path
) == 0);
157 (void)memset(&st
, 0, sizeof(struct stat
));
159 ATF_REQUIRE(mknod(path
, S_IFBLK
, 0) == 0);
160 ATF_REQUIRE(stat(path
, &st
) == 0);
162 if (S_ISBLK(st
.st_mode
) == 0)
163 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFBLK)");
165 ATF_REQUIRE(unlink(path
) == 0);
167 (void)memset(&st
, 0, sizeof(struct stat
));
169 ATF_REQUIRE(mknod(path
, S_IFREG
, 0) == 0);
170 ATF_REQUIRE(stat(path
, &st
) == 0);
172 if (S_ISREG(st
.st_mode
) == 0)
173 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFREG)");
175 ATF_REQUIRE(unlink(path
) == 0);
178 ATF_TC_CLEANUP(mknod_stat
, tc
)
186 ATF_TP_ADD_TC(tp
, mknod_err
);
187 ATF_TP_ADD_TC(tp
, mknod_exist
);
188 ATF_TP_ADD_TC(tp
, mknod_perm
);
189 ATF_TP_ADD_TC(tp
, mknod_stat
);
191 return atf_no_error();