* [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API
@ 2021-08-06 7:24 zhanglianjie
2021-08-10 11:20 ` Cyril Hrubis
0 siblings, 1 reply; 5+ messages in thread
From: zhanglianjie @ 2021-08-06 7:24 UTC (permalink / raw)
To: ltp
Signed-off-by: zhanglianjie <zhanglianjie@uniontech.com>
diff --git a/testcases/kernel/syscalls/chmod/chmod03.c b/testcases/kernel/syscalls/chmod/chmod03.c
index f53e437e4..1e1d829da 100644
--- a/testcases/kernel/syscalls/chmod/chmod03.c
+++ b/testcases/kernel/syscalls/chmod/chmod03.c
@@ -1,23 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2001
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Name: chmod03
+/*\
+ * [Description]
*
* Test Description:
* Verify that, chmod(2) will succeed to change the mode of a file
@@ -31,56 +20,10 @@
* chmod() should return value 0 on success and succeeds to change
* the mode of specified file with sticky bit set on it.
*
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Create temporary directory.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * Loop if the proper options are given.
- * Execute system call
- * Check return code, if system call failed (return=-1)
- * Log the errno and Issue a FAIL message.
- * Otherwise,
- * Verify the Functionality of system call
- * if successful,
- * Issue Functionality-Pass message.
- * Otherwise,
- * Issue Functionality-Fail message.
- * Cleanup:
- * Print errno log and/or timing stats if options given
- * Delete the temporary directory created.
- *
- * Usage: <for command-line>
- * chmod03 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS:
- * This test should be run by 'non-super-user' only.
- *
*/
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
#include <pwd.h>
-
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#define PERMS 01777 /*
@@ -89,95 +32,53 @@
*/
#define TESTFILE "testfile"
-char *TCID = "chmod03";
-int TST_TOTAL = 1;
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
+static char nobody_uid[] = "nobody";
+static struct passwd *ltpuser;
-void setup(); /* Main setup function for the test */
-void cleanup(); /* Main cleanup function for the test */
-
-int main(int ac, char **av)
+static void verify_chmod(void)
{
struct stat stat_buf;
- int lc;
mode_t file_mode;
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- TEST(chmod(TESTFILE, PERMS));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
- TESTFILE, PERMS);
- continue;
- }
- if (stat(TESTFILE, &stat_buf) < 0) {
- tst_brkm(TFAIL | TERRNO, cleanup,
- "stat(%s) failed", TESTFILE);
- }
+ TST_EXP_PASS_SILENT(chmod(TESTFILE, PERMS), "chmod(%s, %#o) failed",
+ TESTFILE, PERMS);
+ if (TST_PASS) {
+ SAFE_STAT(TESTFILE, &stat_buf);
file_mode = stat_buf.st_mode;
/* Verify STICKY BIT set on testfile */
if ((file_mode & PERMS) != PERMS) {
- tst_resm(TFAIL, "%s: Incorrect modes 0%3o, "
- "Expected 0777", TESTFILE, file_mode);
+ tst_res(TFAIL, "%s: Incorrect modes 0%3o, "
+ "Expected 0777", TESTFILE, file_mode);
} else {
- tst_resm(TPASS, "Functionality of "
- "chmod(%s, %#o) successful",
- TESTFILE, PERMS);
+ tst_res(TPASS, "Functionality of "
+ "chmod(%s, %#o) successful",
+ TESTFILE, PERMS);
}
}
-
- cleanup();
- tst_exit();
}
-void setup(void)
+static void setup(void)
{
int fd;
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- tst_require_root();
- ltpuser = getpwnam(nobody_uid);
- if (ltpuser == NULL)
- tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
- SAFE_SETUID(NULL, ltpuser->pw_uid);
-
- TEST_PAUSE;
-
- tst_tmpdir();
+ ltpuser = SAFE_GETPWNAM(nobody_uid);
+ SAFE_SETEUID(ltpuser->pw_uid);
/*
* Create a test file under temporary directory with specified
* mode permissios and set the ownership of the test file to the
* uid/gid of guest user.
*/
- if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
- tst_brkm(TBROK | TERRNO, cleanup,
- "open(%s, O_RDWR|O_CREAT, %#o) failed",
- TESTFILE, FILE_MODE);
- }
+ fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
- SAFE_CLOSE(cleanup, fd);
+ SAFE_CLOSE(fd);
}
-/*
- * void
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- * Delete the testfile and temporary directory created in setup().
- */
-void cleanup(void)
-{
-
- tst_rmdir();
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = verify_chmod,
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+};
-}
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API
2021-08-06 7:24 [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API zhanglianjie
@ 2021-08-10 11:20 ` Cyril Hrubis
0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2021-08-10 11:20 UTC (permalink / raw)
To: ltp
Hi!
First of all, the chmod03 and chmod04 are nearly identical, the only
difference seems to be that one of them works with directory and the
second one with a file. We should merge these two tests into a one.
> diff --git a/testcases/kernel/syscalls/chmod/chmod03.c b/testcases/kernel/syscalls/chmod/chmod03.c
> index f53e437e4..1e1d829da 100644
> --- a/testcases/kernel/syscalls/chmod/chmod03.c
> +++ b/testcases/kernel/syscalls/chmod/chmod03.c
> @@ -1,23 +1,12 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (c) International Business Machines Corp., 2001
> *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
> - * the GNU General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + * 07/2001 Ported by Wayne Boyer
> */
>
> -/*
> - * Test Name: chmod03
> +/*\
> + * [Description]
> *
> * Test Description:
> * Verify that, chmod(2) will succeed to change the mode of a file
> @@ -31,56 +20,10 @@
> * chmod() should return value 0 on success and succeeds to change
> * the mode of specified file with sticky bit set on it.
> *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Create temporary directory.
> - * Pause for SIGUSR1 if option specified.
> - *
> - * Test:
> - * Loop if the proper options are given.
> - * Execute system call
> - * Check return code, if system call failed (return=-1)
> - * Log the errno and Issue a FAIL message.
> - * Otherwise,
> - * Verify the Functionality of system call
> - * if successful,
> - * Issue Functionality-Pass message.
> - * Otherwise,
> - * Issue Functionality-Fail message.
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> - * Delete the temporary directory created.
> - *
> - * Usage: <for command-line>
> - * chmod03 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -e : Turn on errno logging.
> - * -f : Turn off functionality Testing.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -P x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> - *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> - *
> - * RESTRICTIONS:
> - * This test should be run by 'non-super-user' only.
> - *
> */
>
> -#include <stdio.h>
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <fcntl.h>
> -#include <errno.h>
> -#include <string.h>
> -#include <signal.h>
> #include <pwd.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> +#include "tst_test.h"
>
> #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
> #define PERMS 01777 /*
> @@ -89,95 +32,53 @@
> */
> #define TESTFILE "testfile"
>
> -char *TCID = "chmod03";
> -int TST_TOTAL = 1;
> -char nobody_uid[] = "nobody";
> -struct passwd *ltpuser;
> +static char nobody_uid[] = "nobody";
> +static struct passwd *ltpuser;
There is no need to keep these as global variables if they are used only
in the setup.
> -void setup(); /* Main setup function for the test */
> -void cleanup(); /* Main cleanup function for the test */
> -
> -int main(int ac, char **av)
> +static void verify_chmod(void)
> {
> struct stat stat_buf;
> - int lc;
> mode_t file_mode;
>
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> -
> - TEST(chmod(TESTFILE, PERMS));
> -
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
> - TESTFILE, PERMS);
> - continue;
> - }
> - if (stat(TESTFILE, &stat_buf) < 0) {
> - tst_brkm(TFAIL | TERRNO, cleanup,
> - "stat(%s) failed", TESTFILE);
> - }
> + TST_EXP_PASS_SILENT(chmod(TESTFILE, PERMS), "chmod(%s, %#o) failed",
> + TESTFILE, PERMS);
> + if (TST_PASS) {
> + SAFE_STAT(TESTFILE, &stat_buf);
> file_mode = stat_buf.st_mode;
>
> /* Verify STICKY BIT set on testfile */
> if ((file_mode & PERMS) != PERMS) {
> - tst_resm(TFAIL, "%s: Incorrect modes 0%3o, "
> - "Expected 0777", TESTFILE, file_mode);
> + tst_res(TFAIL, "%s: Incorrect modes 0%3o, "
> + "Expected 0777", TESTFILE, file_mode);
> } else {
> - tst_resm(TPASS, "Functionality of "
> - "chmod(%s, %#o) successful",
> - TESTFILE, PERMS);
> + tst_res(TPASS, "Functionality of "
> + "chmod(%s, %#o) successful",
> + TESTFILE, PERMS);
> }
> }
Most of the comments for chmod01 apply here as well.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 5+ messages in thread
* [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API
2021-08-10 1:52 zhanglianjie
2021-08-10 9:42 ` Cyril Hrubis
@ 2021-08-10 9:43 ` Cyril Hrubis
1 sibling, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2021-08-10 9:43 UTC (permalink / raw)
To: ltp
Hi!
And also fixed the subject, which should have been chroot03 not chmod03.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 5+ messages in thread
* [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API
2021-08-10 1:52 zhanglianjie
@ 2021-08-10 9:42 ` Cyril Hrubis
2021-08-10 9:43 ` Cyril Hrubis
1 sibling, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2021-08-10 9:42 UTC (permalink / raw)
To: ltp
Hi!
Pushed with a minor changes, thanks.
> +} tcases[] = {
> + {bad_dir, ENAMETOOLONG, "chroot(pathname more than VFS_MAXNAMELEN)"},
> + {fname, ENOTDIR, "chroot(not a directory)"},
> + {nonexistent_dir, ENOENT, "chroot(path does not exists)"},
> + {(char *)-1, EFAULT, "chroot(a path pointing to an invalid address)"},
> + {symbolic_dir, ELOOP, "chroot(two symbolic directory who point to each other)"}
> };
I've shortened the descriptions here a bit.
> /*
> * create two symbolic directory who point to each other to
> * test ELOOP.
> */
And also remove this comment that have been commenting the obvious.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 5+ messages in thread
* [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API
@ 2021-08-10 1:52 zhanglianjie
2021-08-10 9:42 ` Cyril Hrubis
2021-08-10 9:43 ` Cyril Hrubis
0 siblings, 2 replies; 5+ messages in thread
From: zhanglianjie @ 2021-08-10 1:52 UTC (permalink / raw)
To: ltp
Signed-off-by: zhanglianjie <zhanglianjie@uniontech.com>
diff --git a/testcases/kernel/syscalls/chroot/chroot03.c b/testcases/kernel/syscalls/chroot/chroot03.c
index b904e4ac9..0e509f415 100644
--- a/testcases/kernel/syscalls/chroot/chroot03.c
+++ b/testcases/kernel/syscalls/chroot/chroot03.c
@@ -1,168 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2001
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Testcase to test whether chroot(2) sets errno correctly.
- *
- * 1. Test for ENAMETOOLONG:
- * Create a bad directory name with length more than
- * VFS_MAXNAMELEN (Linux kernel variable), and pass it as the
- * path to chroot(2).
- *
- * 2. Test for ENOENT:
- * Attempt to chroot(2) on a non-existent directory
- *
- * 3. Test for ENOTDIR:
- * Attempt to chdir(2) on a file.
- *
- * 4. Test for EFAULT:
- * The pathname parameter to chroot() points to an invalid address,
- * chroot(2) fails with EPERM.
+/*\
+ * [Description]
*
- * 5. Test for ELOOP:
- * Too many symbolic links were encountered When resolving the
- * pathname parameter.
- *
- * 07/2001 Ported by Wayne Boyer
+ * Testcase to test whether chroot(2) sets errno correctly.
+ *
+ * - to test whether chroot() is setting ENAMETOOLONG if the
+ * pathname is more than VFS_MAXNAMELEN.
+ * - to test whether chroot() is setting ENOTDIR if the argument
+ * is not a directory.
+ * - to test whether chroot() is setting ENOENT if the directory
+ * does not exist.
+ * - attempt to chroot to a path pointing to an invalid address
+ * and expect EFAULT as errno.
+ * - to test whether chroot() is setting ELOOP if the two
+ * symbolic directory who point to each other.
*/
#include <stdio.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include "test.h"
-#include <fcntl.h>
-#include "safe_macros.h"
-
-char *TCID = "chroot03";
+#include "tst_test.h"
-static int fd;
static char fname[255];
static char nonexistent_dir[100] = "testdir";
static char bad_dir[] = "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
static char symbolic_dir[] = "sym_dir1";
-struct test_case_t {
+static struct tcase {
char *dir;
int error;
-} TC[] = {
- /*
- * to test whether chroot() is setting ENAMETOOLONG if the
- * pathname is more than VFS_MAXNAMELEN
- */
- {
- bad_dir, ENAMETOOLONG},
- /*
- * to test whether chroot() is setting ENOTDIR if the argument
- * is not a directory.
- */
- {
- fname, ENOTDIR},
- /*
- * to test whether chroot() is setting ENOENT if the directory
- * does not exist.
- */
- {
- nonexistent_dir, ENOENT},
-#if !defined(UCLINUX)
- /*
- * attempt to chroot to a path pointing to an invalid address
- * and expect EFAULT as errno
- */
- {
- (char *)-1, EFAULT},
-#endif
- {symbolic_dir, ELOOP}
+ char *desc;
+} tcases[] = {
+ {bad_dir, ENAMETOOLONG, "chroot(pathname more than VFS_MAXNAMELEN)"},
+ {fname, ENOTDIR, "chroot(not a directory)"},
+ {nonexistent_dir, ENOENT, "chroot(path does not exists)"},
+ {(char *)-1, EFAULT, "chroot(a path pointing to an invalid address)"},
+ {symbolic_dir, ELOOP, "chroot(two symbolic directory who point to each other)"}
};
-int TST_TOTAL = ARRAY_SIZE(TC);
-
-static char *bad_addr;
-
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void verify_chroot(unsigned int n)
{
- int lc;
- int i;
+ struct tcase *tc = &tcases[n];
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- for (i = 0; i < TST_TOTAL; i++) {
- TEST(chroot(TC[i].dir));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "call succeeded unexpectedly");
- continue;
- }
-
- if (TEST_ERRNO == TC[i].error) {
- tst_resm(TPASS | TTERRNO, "failed as expected");
- } else {
- tst_resm(TFAIL | TTERRNO,
- "didn't fail as expected (expected errno "
- "= %d : %s)",
- TC[i].error, strerror(TC[i].error));
- }
- }
- }
-
- cleanup();
- tst_exit();
+ TST_EXP_FAIL(chroot(tc->dir), tc->error, "%s", tc->desc);
}
static void setup(void)
{
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
- TEST_PAUSE;
- tst_tmpdir();
+ unsigned int i;
- /*
- * create a file and use it to test whether chroot() is setting
- * ENOTDIR if the argument is not a directory.
- */
(void)sprintf(fname, "tfile_%d", getpid());
- fd = SAFE_CREAT(cleanup, fname, 0777);
+ SAFE_TOUCH(fname, 0666, NULL);
-#if !defined(UCLINUX)
- bad_addr = mmap(0, 1, PROT_NONE,
- MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
- if (bad_addr == MAP_FAILED)
- tst_brkm(TBROK, cleanup, "mmap failed");
-
- TC[3].dir = bad_addr;
-#endif
+ for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+ if (tcases[i].error == EFAULT)
+ tcases[3].dir = tst_get_bad_addr(NULL);
+ }
+
/*
* create two symbolic directory who point to each other to
* test ELOOP.
*/
- SAFE_SYMLINK(cleanup, "sym_dir1/", "sym_dir2");
- SAFE_SYMLINK(cleanup, "sym_dir2/", "sym_dir1");
+ SAFE_SYMLINK("sym_dir1/", "sym_dir2");
+ SAFE_SYMLINK("sym_dir2/", "sym_dir1");
}
-static void cleanup(void)
-{
- close(fd);
- tst_rmdir();
-}
+static struct tst_test test = {
+ .setup = setup,
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = verify_chroot,
+ .needs_tmpdir = 1,
+};
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-08-10 11:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-06 7:24 [LTP] [PATCH 3/4] syscalls/chmod03: Convert to new API zhanglianjie
2021-08-10 11:20 ` Cyril Hrubis
2021-08-10 1:52 zhanglianjie
2021-08-10 9:42 ` Cyril Hrubis
2021-08-10 9:43 ` Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.