etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_mknod.c
blobb394b31c2182094d01151ce2bd0cafb00f7b8232
1 /* $NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by 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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $");
34 #include <sys/stat.h>
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
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));
61 errno = 0;
62 ATF_REQUIRE_ERRNO(EINVAL, mknod(path, S_IFCHR, -1) == -1);
64 errno = 0;
65 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mknod(buf, S_IFCHR, 0) == -1);
67 errno = 0;
68 ATF_REQUIRE_ERRNO(EFAULT, mknod((char *)-1, S_IFCHR, 0) == -1);
70 errno = 0;
71 ATF_REQUIRE_ERRNO(ENOENT, mknod("/a/b/c/d/e/f/g", S_IFCHR, 0) == -1);
74 ATF_TC_CLEANUP(mknod_err, tc)
76 (void)unlink(path);
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)
88 int fd;
90 fd = open("/etc/passwd", O_RDONLY);
92 if (fd >= 0) {
94 (void)close(fd);
96 errno = 0;
97 ATF_REQUIRE_ERRNO(EEXIST,
98 mknod("/etc/passwd", S_IFCHR, 0) == -1);
101 ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
103 errno = 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)
111 (void)unlink(path);
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)
124 errno = 0;
125 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFCHR, 0) == -1);
127 errno = 0;
128 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFBLK, 0) == -1);
131 ATF_TC_CLEANUP(mknod_perm, tc)
133 (void)unlink(path);
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)
145 struct stat st;
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)
180 (void)unlink(path);
183 ATF_TP_ADD_TCS(tp)
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();