1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that chmod(2) succeeds when used to change the mode permissions
11 * of a file or directory.
12 */
13
14 #include "tst_test.h"
15
16 #define MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
17 #define TESTFILE "testfile"
18 #define TESTDIR "testdir_1"
19
20 static int modes[] = {0, 07, 070, 0700, 0777, 02777, 04777, 06777};
21
22 static struct variant {
23 char *name;
24 unsigned int mode_mask;
25 char *desc;
26 } variants[] = {
27 {TESTFILE, S_IFREG, "verify permissions of file"},
28 {TESTDIR, S_IFDIR, "verify permissions of directory"},
29 };
30
verify_chmod(unsigned int n)31 static void verify_chmod(unsigned int n)
32 {
33 struct stat stat_buf;
34 int mode = modes[n];
35 struct variant *tc = &variants[tst_variant];
36
37 TST_EXP_PASS(chmod(tc->name, mode), "chmod(%s, %04o)",
38 tc->name, mode);
39
40 if (!TST_PASS)
41 return;
42
43 SAFE_STAT(tc->name, &stat_buf);
44 stat_buf.st_mode &= ~tc->mode_mask;
45
46 if (stat_buf.st_mode == (unsigned int)mode) {
47 tst_res(TPASS, "stat(%s) mode=%04o",
48 tc->name, stat_buf.st_mode);
49 } else {
50 tst_res(TFAIL, "stat(%s) mode=%04o",
51 tc->name, stat_buf.st_mode);
52 }
53 }
54
setup(void)55 static void setup(void)
56 {
57 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
58
59 if (tst_variant)
60 SAFE_MKDIR(variants[tst_variant].name, MODE);
61 else
62 SAFE_TOUCH(variants[tst_variant].name, MODE, NULL);
63 }
64
65 static struct tst_test test = {
66 .setup = setup,
67 .test_variants = ARRAY_SIZE(variants),
68 .tcnt = ARRAY_SIZE(modes),
69 .test = verify_chmod,
70 .needs_tmpdir = 1,
71 };
72