public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v2] Add chmod08 test
@ 2024-02-20 13:13 Andrea Cervesato
  2024-06-27  9:33 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: Andrea Cervesato @ 2024-02-20 13:13 UTC (permalink / raw)
  To: ltp

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>
---
 testcases/kernel/syscalls/chmod/chmod08.c | 61 +++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 testcases/kernel/syscalls/chmod/chmod08.c

diff --git a/testcases/kernel/syscalls/chmod/chmod08.c b/testcases/kernel/syscalls/chmod/chmod08.c
new file mode 100644
index 000000000..f9ca4e45a
--- /dev/null
+++ b/testcases/kernel/syscalls/chmod/chmod08.c
@@ -0,0 +1,61 @@
+// 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 run(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 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 struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_tmpdir = 1,
+};
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [LTP] [PATCH v2] Add chmod08 test
  2024-02-20 13:13 [LTP] [PATCH v2] Add chmod08 test Andrea Cervesato
@ 2024-06-27  9:33 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2024-06-27  9:33 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi!
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  testcases/kernel/syscalls/chmod/chmod08.c | 61 +++++++++++++++++++++++
>  1 file changed, 61 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/chmod/chmod08.c
> 
> diff --git a/testcases/kernel/syscalls/chmod/chmod08.c b/testcases/kernel/syscalls/chmod/chmod08.c
> new file mode 100644
> index 000000000..f9ca4e45a
> --- /dev/null
> +++ b/testcases/kernel/syscalls/chmod/chmod08.c
> @@ -0,0 +1,61 @@
> +// 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 run(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);

This should be SAFE_UNLINK() as well.

> +}
> +
> +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));

This can easily overflow. If you want to use absolute path you should
allocate the buffer based on the tmpdir lenght...

> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test_all = run,
> +	.needs_tmpdir = 1,
> +};
> -- 
> 2.35.3
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-27  9:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-20 13:13 [LTP] [PATCH v2] Add chmod08 test Andrea Cervesato
2024-06-27  9:33 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox