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