[LTP] [PATCH v1] Add chmod08 test

Andrea Cervesato andrea.cervesato@suse.de
Fri Jan 19 10:09:48 CET 2024


From: Andrea Cervesato <andrea.cervesato@suse.com>

This test has been extracted from symlink01 and it verifies that
chmod() is working correctly on symlink() generated files.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                           |  2 +-
 testcases/kernel/syscalls/chmod/.gitignore |  1 +
 testcases/kernel/syscalls/chmod/chmod08.c  | 88 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/chmod/chmod08.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 00a0dd87e..cd2dbf3c1 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -65,11 +65,11 @@ chdir04 chdir04
 chdir05 chdir05
 
 chmod01 chmod01
-chmod01A symlink01 -T chmod01
 chmod03 chmod03
 chmod05 chmod05
 chmod06 chmod06
 chmod07 chmod07
+chmod08 chmod08
 
 chown01 chown01
 chown01_16 chown01_16
diff --git a/testcases/kernel/syscalls/chmod/.gitignore b/testcases/kernel/syscalls/chmod/.gitignore
index 27ddfce16..f295f4dcb 100644
--- a/testcases/kernel/syscalls/chmod/.gitignore
+++ b/testcases/kernel/syscalls/chmod/.gitignore
@@ -3,3 +3,4 @@
 /chmod05
 /chmod06
 /chmod07
+/chmod08
diff --git a/testcases/kernel/syscalls/chmod/chmod08.c b/testcases/kernel/syscalls/chmod/chmod08.c
new file mode 100644
index 000000000..f7c190776
--- /dev/null
+++ b/testcases/kernel/syscalls/chmod/chmod08.c
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
+ *    Author: David Fenner
+ *    Copilot: Jon Hendrickson
+ * Copyright (C) 2024 Andrea Cervesato andrea.cervesato@suse.com
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that chmod() is working correctly on symlink()
+ * generated files.
+ */
+
+#include "tst_test.h"
+
+#define PERMS 01777
+#define TESTFILE "myobject"
+
+static char testfile[PATH_MAX];
+
+static void test_chmod_symlink(void)
+{
+	char *symname = "my_symlink0";
+	struct stat oldsym_stat;
+	struct stat newsym_stat;
+
+	SAFE_TOUCH(testfile, 0644, NULL);
+	SAFE_SYMLINK(testfile, symname);
+	SAFE_STAT(symname, &oldsym_stat);
+
+	TST_EXP_PASS(chmod(symname, PERMS));
+	SAFE_STAT(symname, &newsym_stat);
+
+	TST_EXP_EQ_LI(newsym_stat.st_mode & PERMS, PERMS);
+	TST_EXP_EXPR(oldsym_stat.st_mode != newsym_stat.st_mode,
+		"file mode has changed");
+
+	SAFE_UNLINK(symname);
+	remove(testfile);
+}
+
+static void test_chmod_no_symlink(void)
+{
+	char *symname = "my_symlink1";
+
+	SAFE_SYMLINK("bc+eFhi!k", symname);
+	TST_EXP_FAIL(chmod(symname, 0644), ENOENT);
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_chmod_loop(void)
+{
+	char *symname = "my_symlink2";
+
+	SAFE_SYMLINK(symname, symname);
+	TST_EXP_FAIL(chmod(symname, 0644), ELOOP);
+
+	SAFE_UNLINK(symname);
+}
+
+static void setup(void)
+{
+	int tmplen;
+	char *tmpdir;
+
+	tmpdir = tst_get_tmpdir();
+	tmplen = strlen(tmpdir);
+
+	testfile[tmplen] = '/';
+	memcpy(testfile, tmpdir, tmplen);
+	memcpy(testfile + tmplen + 1, TESTFILE, strlen(TESTFILE));
+}
+
+static void run(void)
+{
+	test_chmod_symlink();
+	test_chmod_no_symlink();
+	test_chmod_loop();
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_tmpdir = 1,
+};
-- 
2.35.3



More information about the ltp mailing list