1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by John George
5 */
6
7 /*
8 * umask(2) sets the mask from 0000 to 0777 while we create files,
9 * the previous value of the mask should be returned correctly,
10 * and the file mode should be correct for each creation mask.
11 */
12
13 #include <errno.h>
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include "tst_test.h"
18
verify_umask(void)19 static void verify_umask(void)
20 {
21 struct stat statbuf;
22 int mskval;
23 int fd;
24 int failflag = 0;
25 unsigned low9mode;
26
27 for (mskval = 0000; mskval < 01000; mskval++) {
28 TEST(umask(mskval));
29 if (TST_RET < 0 || TST_RET > 0777) {
30 tst_brk(TFAIL, "umask(%o) result outside range %ld",
31 mskval, TST_RET);
32 }
33
34 if (mskval > 0000 && TST_RET != mskval - 1) {
35 failflag = 1;
36 tst_res(TFAIL, "umask(%o) returned %ld, expected %d",
37 mskval, TST_RET, mskval - 1);
38 }
39
40 fd = SAFE_CREAT("testfile", 0777);
41 SAFE_CLOSE(fd);
42
43 SAFE_STAT("testfile", &statbuf);
44
45 low9mode = statbuf.st_mode & 0777;
46
47 if (low9mode != (~mskval & 0777)) {
48 failflag = 1;
49 tst_res(TFAIL, "File mode got %o, expected %o",
50 low9mode, ~mskval & 0777);
51 }
52
53 SAFE_UNLINK("testfile");
54 }
55
56 if (!failflag)
57 tst_res(TPASS, "All files created with correct mode");
58 }
59
60 static struct tst_test test = {
61 .test_all = verify_umask,
62 .needs_tmpdir = 1,
63 };
64