etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_umask.c
blob748cbdcf1ada128ce6767122d2f9a4dc5eeb4b61
1 /* $NetBSD: t_umask.c,v 1.1 2011/07/07 06:57:54 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_umask.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $");
34 #include <sys/stat.h>
35 #include <sys/wait.h>
37 #include <atf-c.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 static const char path[] = "umask";
44 static const mode_t mask[] = {
45 S_IRWXU,
46 S_IRUSR,
47 S_IWUSR,
48 S_IXUSR,
49 S_IRWXG,
50 S_IRGRP,
51 S_IWGRP,
52 S_IXGRP,
53 S_IRWXO,
54 S_IROTH,
55 S_IWOTH,
56 S_IXOTH
59 ATF_TC_WITH_CLEANUP(umask_fork);
60 ATF_TC_HEAD(umask_fork, tc)
62 atf_tc_set_md_var(tc, "descr", "Check that umask(2) is inherited");
65 ATF_TC_BODY(umask_fork, tc)
67 mode_t mode;
68 pid_t pid;
69 size_t i;
70 int sta;
72 for (i = 0; i < __arraycount(mask) - 1; i++) {
74 (void)umask(mask[i] | mask[i + 1]);
76 pid = fork();
78 if (pid < 0)
79 continue;
81 if (pid == 0) {
83 mode = umask(mask[i]);
85 if (mode != (mask[i] | mask[i + 1]))
86 _exit(EXIT_FAILURE);
88 _exit(EXIT_SUCCESS);
91 (void)wait(&sta);
93 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
94 goto fail;
97 return;
99 fail:
100 (void)umask(S_IWGRP | S_IWOTH);
102 atf_tc_fail("umask(2) was not inherited");
105 ATF_TC_CLEANUP(umask_fork, tc)
107 (void)umask(S_IWGRP | S_IWOTH);
110 ATF_TC_WITH_CLEANUP(umask_open);
111 ATF_TC_HEAD(umask_open, tc)
113 atf_tc_set_md_var(tc, "descr", "A basic test of open(2) and umask(2)");
116 ATF_TC_BODY(umask_open, tc)
118 const char *str = NULL;
119 struct stat st;
120 size_t i;
121 int fd;
123 for (i = 0; i < __arraycount(mask); i++) {
125 (void)umask(mask[i]);
127 fd = open(path, O_RDWR | O_CREAT, 0777);
129 if (fd < 0)
130 continue;
132 (void)memset(&st, 0, sizeof(struct stat));
134 if (stat(path, &st) != 0) {
135 str = "failed to stat(2)";
136 goto out;
139 if ((st.st_mode & mask[i]) != 0) {
140 str = "invalid umask(2)";
141 goto out;
144 if (unlink(path) != 0) {
145 str = "failed to unlink(2)";
146 goto out;
151 out:
152 (void)umask(S_IWGRP | S_IWOTH);
154 if (str != NULL)
155 atf_tc_fail("%s", str);
158 ATF_TC_CLEANUP(umask_open, tc)
160 (void)umask(S_IWGRP | S_IWOTH);
161 (void)unlink(path);
164 ATF_TC_WITH_CLEANUP(umask_previous);
165 ATF_TC_HEAD(umask_previous, tc)
167 atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)");
170 ATF_TC_BODY(umask_previous, tc)
172 mode_t mode;
173 size_t i;
175 for (i = 0; i < __arraycount(mask); i++) {
177 mode = umask(mask[i]);
178 mode = umask(mask[i]);
180 if (mode != mask[i])
181 goto fail;
184 return;
186 fail:
187 (void)umask(S_IWGRP | S_IWOTH);
189 atf_tc_fail("umask(2) did not return the previous mask");
192 ATF_TC_CLEANUP(umask_previous, tc)
194 (void)umask(S_IWGRP | S_IWOTH);
197 ATF_TP_ADD_TCS(tp)
200 ATF_TP_ADD_TC(tp, umask_fork);
201 ATF_TP_ADD_TC(tp, umask_open);
202 ATF_TP_ADD_TC(tp, umask_previous);
204 return atf_no_error();