1 /* $NetBSD: t_umask.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
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.
18 *
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.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_umask.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $");
33
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36
37 #include <atf-c.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
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
57 };
58
59 ATF_TC_WITH_CLEANUP(umask_fork);
ATF_TC_HEAD(umask_fork,tc)60 ATF_TC_HEAD(umask_fork, tc)
61 {
62 atf_tc_set_md_var(tc, "descr", "Check that umask(2) is inherited");
63 }
64
ATF_TC_BODY(umask_fork,tc)65 ATF_TC_BODY(umask_fork, tc)
66 {
67 mode_t mode;
68 pid_t pid;
69 size_t i;
70 int sta;
71
72 for (i = 0; i < __arraycount(mask) - 1; i++) {
73
74 (void)umask(mask[i] | mask[i + 1]);
75
76 pid = fork();
77
78 if (pid < 0)
79 continue;
80
81 if (pid == 0) {
82
83 mode = umask(mask[i]);
84
85 if (mode != (mask[i] | mask[i + 1]))
86 _exit(EXIT_FAILURE);
87
88 _exit(EXIT_SUCCESS);
89 }
90
91 (void)wait(&sta);
92
93 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
94 goto fail;
95 }
96
97 return;
98
99 fail:
100 (void)umask(S_IWGRP | S_IWOTH);
101
102 atf_tc_fail("umask(2) was not inherited");
103 }
104
ATF_TC_CLEANUP(umask_fork,tc)105 ATF_TC_CLEANUP(umask_fork, tc)
106 {
107 (void)umask(S_IWGRP | S_IWOTH);
108 }
109
110 ATF_TC_WITH_CLEANUP(umask_open);
ATF_TC_HEAD(umask_open,tc)111 ATF_TC_HEAD(umask_open, tc)
112 {
113 atf_tc_set_md_var(tc, "descr", "A basic test of open(2) and umask(2)");
114 }
115
ATF_TC_BODY(umask_open,tc)116 ATF_TC_BODY(umask_open, tc)
117 {
118 const char *str = NULL;
119 struct stat st;
120 size_t i;
121 int fd;
122
123 for (i = 0; i < __arraycount(mask); i++) {
124
125 (void)umask(mask[i]);
126
127 fd = open(path, O_RDWR | O_CREAT, 0777);
128
129 if (fd < 0)
130 continue;
131
132 (void)memset(&st, 0, sizeof(struct stat));
133
134 if (stat(path, &st) != 0) {
135 str = "failed to stat(2)";
136 goto out;
137 }
138
139 if ((st.st_mode & mask[i]) != 0) {
140 str = "invalid umask(2)";
141 goto out;
142 }
143
144 if (unlink(path) != 0) {
145 str = "failed to unlink(2)";
146 goto out;
147 }
148
149 }
150
151 out:
152 (void)umask(S_IWGRP | S_IWOTH);
153
154 if (str != NULL)
155 atf_tc_fail("%s", str);
156 }
157
ATF_TC_CLEANUP(umask_open,tc)158 ATF_TC_CLEANUP(umask_open, tc)
159 {
160 (void)umask(S_IWGRP | S_IWOTH);
161 (void)unlink(path);
162 }
163
164 ATF_TC_WITH_CLEANUP(umask_previous);
ATF_TC_HEAD(umask_previous,tc)165 ATF_TC_HEAD(umask_previous, tc)
166 {
167 atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)");
168 }
169
ATF_TC_BODY(umask_previous,tc)170 ATF_TC_BODY(umask_previous, tc)
171 {
172 mode_t mode;
173 size_t i;
174
175 for (i = 0; i < __arraycount(mask); i++) {
176
177 mode = umask(mask[i]);
178 mode = umask(mask[i]);
179
180 if (mode != mask[i])
181 goto fail;
182 }
183
184 return;
185
186 fail:
187 (void)umask(S_IWGRP | S_IWOTH);
188
189 atf_tc_fail("umask(2) did not return the previous mask");
190 }
191
ATF_TC_CLEANUP(umask_previous,tc)192 ATF_TC_CLEANUP(umask_previous, tc)
193 {
194 (void)umask(S_IWGRP | S_IWOTH);
195 }
196
ATF_TP_ADD_TCS(tp)197 ATF_TP_ADD_TCS(tp)
198 {
199
200 ATF_TP_ADD_TC(tp, umask_fork);
201 ATF_TP_ADD_TC(tp, umask_open);
202 ATF_TP_ADD_TC(tp, umask_previous);
203
204 return atf_no_error();
205 }
206