[LTP] [PATCH 2/2] syscalls/readdir21: Convert to new API

Cyril Hrubis chrubis@suse.cz
Thu Oct 7 13:54:27 CEST 2021


On Wed, Sep 29, 2021 at 04:32:49PM +0800, zhanglianjie wrote:
> Signed-off-by: zhanglianjie <zhanglianjie@uniontech.com>
> 
> diff --git a/testcases/kernel/syscalls/readdir/readdir21.c b/testcases/kernel/syscalls/readdir/readdir21.c
> index 205e0715b..1bc3cc199 100644
> --- a/testcases/kernel/syscalls/readdir/readdir21.c
> +++ b/testcases/kernel/syscalls/readdir/readdir21.c
> @@ -1,20 +1,12 @@
> +// SPDX-License-Identifier: GPL-2.0-only
>  /*
>   * Copyright (c) 2014 Fujitsu Ltd.
>   * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> - *
> - * This program is free software; you can redistribute it and/or modify it
> - * under the terms of version 2 of the GNU General Public License as
> - * published by the Free Software Foundation.
> - *
> - * This program is distributed in the hope that it would be useful, but
> - * WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> - *
> - * You should have received a copy of the GNU General Public License along
> - * with this program; if not, write the Free Software Foundation, Inc.,
> - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>   */
> -/*
> +
> +/*\
> + * [Description]
> + *
>   * Test Description:
>   *  Verify that,
>   *   1. Creat a directory and open it, then delete the directory, ENOENT would
> @@ -23,28 +15,16 @@
>   *   3. Invalid file descriptor fd, EBADF would return.
>   *   4. Argument points outside the calling process's address space, EFAULT
>   *	would return.

This should be reformatted so that it renders nicely in asccidoc as:


/*\
 * [Description]
 *
 * Verify that readdir will fail with:
 *
 * - ENOENT when passed a fd to a deleted directory
 * - ENOTDIR when passed fd that does not point to a directory
 * - EBADFD when passed an invalid fd
 * - EFAULT when passed invalid buffer pointer
 */

> - *
> - *  PS:
> - *   This file is for readdir(2) and the other files(readdir01.c and
> - *   readdir02.c) are for readdir(3).
>   */
> 
> -#define _GNU_SOURCE
> +//#define _GNU_SOURCE

Just remove it if it's not needed.

>  #include <stdio.h>
> -#include <sys/types.h>
>  #include <sys/stat.h>
> -#include <fcntl.h>
> -#include <errno.h>
> -#include <unistd.h>
> -#include <sys/mman.h>
> -#include "test.h"
> -#include "safe_macros.h"
> +#include "tst_test.h"
>  #include "lapi/syscalls.h"
>  #include "lapi/readdir.h"
> 
> -char *TCID = "readdir21";
> -
>  #define TEST_DIR	"test_dir"
>  #define TEST_DIR4	"test_dir4"
>  #define TEST_FILE	"test_file"
> @@ -55,96 +35,46 @@ static unsigned int del_dir_fd, file_fd;
>  static unsigned int invalid_fd = 999;
>  static unsigned int dir_fd;
>  static struct old_linux_dirent dirp;
> -static void setup(void);
> -static void cleanup(void);
> 
> -static struct test_case_t {
> +static struct tcase {
>  	unsigned int *fd;
>  	struct old_linux_dirent *dirp;
>  	unsigned int count;
>  	int exp_errno;
> -} test_cases[] = {
> +} tcases[] = {
>  	{&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT},
>  	{&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR},
>  	{&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF},
> -#if !defined(UCLINUX)
>  	{&dir_fd, (struct old_linux_dirent *)-1,
>  	 sizeof(struct old_linux_dirent), EFAULT},
> -#endif
>  };

I guess that we also miss EINVAL error here with invalid count.

> -int TST_TOTAL = ARRAY_SIZE(test_cases);
> -static void readdir_verify(const struct test_case_t *);
> -
> -int main(int argc, char **argv)
> -{
> -	int i, lc;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -		for (i = 0; i < TST_TOTAL; i++)
> -			readdir_verify(&test_cases[i]);
> -	}
> -
> -	cleanup();
> -	tst_exit();
> -}
> -
>  static void setup(void)
>  {
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	TEST_PAUSE;
> +	SAFE_MKDIR(TEST_DIR, DIR_MODE);
> +	del_dir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY | O_DIRECTORY);
> +	SAFE_RMDIR(TEST_DIR);
> 
> -	tst_tmpdir();
> +	file_fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0777);
> 
> -	SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE);
> -	del_dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_RDONLY | O_DIRECTORY);
> -	SAFE_RMDIR(cleanup, TEST_DIR);
> +	SAFE_MKDIR(TEST_DIR4, DIR_MODE);
> +	dir_fd = SAFE_OPEN(TEST_DIR4, O_RDONLY | O_DIRECTORY);
> 
> -	file_fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0777);
> -
> -	SAFE_MKDIR(cleanup, TEST_DIR4, DIR_MODE);
> -	dir_fd = SAFE_OPEN(cleanup, TEST_DIR4, O_RDONLY | O_DIRECTORY);
> -
> -#if !defined(UCLINUX)
> -	test_cases[3].dirp = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
> -				       MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> -#endif
> +	tcases[3].dirp = tst_get_bad_addr(NULL);

We shouldn't rely on the tcases structure order. We usually solve this
with a for loop in the setup that looks for EFAULT errno as with:

	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
		if (tcases[i].exp_errno == EBADFD)
			tcases[i].dirp = tst_get_bad_addr(NULL);
	}

>  }
> 
> -static void readdir_verify(const struct test_case_t *test)
> +static void verify_readdir(unsigned int nr)
>  {
> -	TEST(ltp_syscall(__NR_readdir, *test->fd, test->dirp, test->count));
> -
> -	if (TEST_RETURN != -1) {
> -		tst_resm(TFAIL, "readdir() succeeded unexpectedly");
> -		return;
> -	}
> +	struct tcase *tc = &tcases[nr];
> 
> -	if (TEST_ERRNO == test->exp_errno) {
> -		tst_resm(TPASS | TTERRNO, "readdir() failed as expected");
> -	} else {
> -		tst_resm(TFAIL | TTERRNO,
> -			 "readdir() failed unexpectedly; expected: %d - %s",
> -			 test->exp_errno, strerror(test->exp_errno));
> -	}
> +	TST_EXP_FAIL(tst_syscall(__NR_readdir, *tc->fd, tc->dirp, tc->count),
> +			tc->exp_errno, "readdir() failed unexpectedly, expected: %d - %s",
> +			tc->exp_errno, strerror(tc->exp_errno));

This produces a very confusing messages. The TST_EXP_*() macros should
print what is being tested, since the part that describes the result is
appended automatically.

So here we should probably add a short description to the tcases
structure and print it with the macro as:

...
} tcases[] = {
	{&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT, "directory deleted"},
...
}

...

	TST_EXP_FAIL(tst_syscall(__NR_readdir, *tc->fd, tc->dirp, tc->count),
	             tc->exp_errno, "readdir() with %s", tc->desc);

Which would produce:

	readdir21.c:70: TPASS: readdir() with directory deleted : ENOENT (2)

Instead of the confusing:

	readdir21.c:70: TPASS: readdir() failed unexpectedly, expected: 2 - No such file or directory : ENOENT (2)

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list