[LTP] [PATCH v2 3/3] fchownat03: Move error tests from fchownat01

Andrea Cervesato andrea.cervesato@suse.com
Wed Jan 15 13:40:51 CET 2025


Hi!

This is a new test, so we require a proper commit description for it, 
even if it comes from fchownat01.
The commit name might be "Add new fchownat03 test" and description 
should contain what the test is made for and where it comes from.

On 1/15/25 10:12, Shiyang Ruan via ltp wrote:
> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
> ---
>   testcases/kernel/syscalls/fchownat/.gitignore |  1 +
>   .../kernel/syscalls/fchownat/fchownat03.c     | 77 +++++++++++++++++++
>   2 files changed, 78 insertions(+)
>   create mode 100644 testcases/kernel/syscalls/fchownat/fchownat03.c
>
> diff --git a/testcases/kernel/syscalls/fchownat/.gitignore b/testcases/kernel/syscalls/fchownat/.gitignore
> index 35c00345b..60fac7e69 100644
> --- a/testcases/kernel/syscalls/fchownat/.gitignore
> +++ b/testcases/kernel/syscalls/fchownat/.gitignore
> @@ -1,2 +1,3 @@
>   /fchownat01
>   /fchownat02
> +/fchownat03
> diff --git a/testcases/kernel/syscalls/fchownat/fchownat03.c b/testcases/kernel/syscalls/fchownat/fchownat03.c
> new file mode 100644
> index 000000000..3c50eb39e
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fchownat/fchownat03.c
> @@ -0,0 +1,77 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) International Business Machines  Corp., 2006
> + * Copyright (c) Linux Test Project, 2007-2025
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify that:
> + *
> + * 1. fchownat() returns -1 and sets errno to ENOTDIR if the file descriptor is
> + *    a file.
> + * 2. fchownat() returns -1 and sets errno to EBADF if the file descriptor
> + *    of the specified file is not valid.
> + * 3. fchownat() returns -1 and sets errno to EINVAL if the flag is not valid.
The list is kinda small, we have a huge number of cases which would be 
nice to extend. Please check fchownat(2) manual.
> + */
> +
> +#define _GNU_SOURCE
> +#include "tst_test.h"
> +
> +#define TESTFILE	"testfile"
> +#define TESTFILE2	"testfile2"
TESTFILE2 is never used in the test so it can be removed.
> +
> +static int fd;
> +static int no_fd = -1;
> +static int dir_fd;
> +
> +static struct tcase {
> +	int exp_errno;
> +	int flag;
> +	int *fd;
> +	char *filename;
> +} tcases[] = {
> +	{ ENOTDIR, 0, &fd, TESTFILE },
> +	{ EBADF, 0, &no_fd, TESTFILE },
> +	{ EINVAL, 9999, &dir_fd, TESTFILE },
> +};
> +
> +static void fchownat_verify(unsigned int n)
> +{
> +	struct tcase *tc = &tcases[n];
> +	uid_t euid = geteuid();
> +	gid_t egid = getegid();
> +	int fd = *tc->fd;
> +	int flag = tc->flag;
> +	const char *filename = tc->filename;
There's no need to create new variables for the tcases attributes access.
> +
> +	TST_EXP_FAIL(fchownat(fd, filename, euid, egid, flag),
> +			tc->exp_errno,
> +			"fchownat(%d, %s, %d, %d, %d)",
> +			fd, filename, euid, egid, flag);
> +}
> +
> +static void setup(void)
> +{
> +	SAFE_TOUCH(TESTFILE, 0600, NULL);
> +	fd = SAFE_OPEN(TESTFILE2, O_CREAT | O_RDWR, 0600);
> +	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
Better to initialize file descriptors to -1, then check here if they are 
!= -1 after setup() and test run.
> +
> +	if (dir_fd > 0)
> +		SAFE_CLOSE(dir_fd);
Same here.
> +}
> +
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.test = fchownat_verify,
> +	.tcnt = ARRAY_SIZE(tcases),
> +	.setup = setup,
> +	.cleanup = cleanup,
> +};


More information about the ltp mailing list