1 /* $NetBSD: t_umask.c,v 1.1 2011/07/07 06:57:54 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_umask.c,v 1.1 2011/07/07 06:57:54 jruoho 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);
132 (void)memset(&st
, 0, sizeof(struct stat
));
134 if (stat(path
, &st
) != 0) {
135 str
= "failed to stat(2)";
139 if ((st
.st_mode
& mask
[i
]) != 0) {
140 str
= "invalid umask(2)";
144 if (unlink(path
) != 0) {
145 str
= "failed to unlink(2)";
152 (void)umask(S_IWGRP
| S_IWOTH
);
155 atf_tc_fail("%s", str
);
158 ATF_TC_CLEANUP(umask_open
, tc
)
160 (void)umask(S_IWGRP
| S_IWOTH
);
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
)
175 for (i
= 0; i
< __arraycount(mask
); i
++) {
177 mode
= umask(mask
[i
]);
178 mode
= umask(mask
[i
]);
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
);
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();