1 /* $NetBSD: t_umask.c,v 1.2 2017/01/13 19:34:19 christos 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_umask.c,v 1.2 2017/01/13 19:34:19 christos Exp $");
43 static const char path[] = "umask";
44 static const mode_t mask[] = {
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)
72 for (i = 0; i < __arraycount(mask) - 1; i++) {
74 (void)umask(mask[i] | mask[i + 1]);
83 mode = umask(mask[i]);
85 if (mode != (mask[i] | mask[i + 1]))
93 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
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;
123 for (i = 0; i < __arraycount(mask); i++) {
125 (void)umask(mask[i]);
127 fd = open(path, O_RDWR | O_CREAT, 0777);
133 (void)memset(&st, 0, sizeof(struct stat));
135 if (stat(path, &st) != 0) {
136 str = "failed to stat(2)";
140 if ((st.st_mode & mask[i]) != 0) {
141 str = "invalid umask(2)";
145 if (unlink(path) != 0) {
146 str = "failed to unlink(2)";
153 (void)umask(S_IWGRP | S_IWOTH);
156 atf_tc_fail("%s", str);
159 ATF_TC_CLEANUP(umask_open, tc)
161 (void)umask(S_IWGRP | S_IWOTH);
165 ATF_TC_WITH_CLEANUP(umask_previous);
166 ATF_TC_HEAD(umask_previous, tc)
168 atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)");
171 ATF_TC_BODY(umask_previous, tc)
176 for (i = 0; i < __arraycount(mask); i++) {
178 mode = umask(mask[i]);
179 mode = umask(mask[i]);
188 (void)umask(S_IWGRP | S_IWOTH);
190 atf_tc_fail("umask(2) did not return the previous mask");
193 ATF_TC_CLEANUP(umask_previous, tc)
195 (void)umask(S_IWGRP | S_IWOTH);
201 ATF_TP_ADD_TC(tp, umask_fork);
202 ATF_TP_ADD_TC(tp, umask_open);
203 ATF_TP_ADD_TC(tp, umask_previous);
205 return atf_no_error();