All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API
@ 2023-08-21 11:38 Yang Xu
  2023-08-21 11:38 ` [LTP] [PATCH v2 2/2] syscalls/readlinkat02: " Yang Xu
  2023-08-24  9:11 ` [LTP] [PATCH v2 1/2] syscalls/readlinkat01: " Cyril Hrubis
  0 siblings, 2 replies; 5+ messages in thread
From: Yang Xu @ 2023-08-21 11:38 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 .../kernel/syscalls/readlinkat/readlinkat01.c | 169 +++++++-----------
 1 file changed, 63 insertions(+), 106 deletions(-)

diff --git a/testcases/kernel/syscalls/readlinkat/readlinkat01.c b/testcases/kernel/syscalls/readlinkat/readlinkat01.c
index 985890ebe..402a16a4c 100644
--- a/testcases/kernel/syscalls/readlinkat/readlinkat01.c
+++ b/testcases/kernel/syscalls/readlinkat/readlinkat01.c
@@ -1,143 +1,100 @@
-/******************************************************************************
- *
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
  * Copyright (c) International Business Machines  Corp., 2006
- *  Author: Yi Yang <yyangcdl@cn.ibm.com>
  * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
+ * Copyright (c) Linux Test Project, 2003-2023
+ * Author: Yi Yang <yyangcdl@cn.ibm.com>
+ */
+
+/*\
+ * [Description]
  *
- * 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.
+ * Check the basic functionality of the readlinkat() system call.
  *
- * 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.
+ * - faccessat() passes if dir_fd is file descriptor to the directory and
+ *   pathname is interpreted to the symbolic link file.
  *
- * 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
+ * - faccessat() passes if dir_fd is file descriptor to the directory and
+ *   pathname is absolute path of the file.
  *
- * This test case will verify basic function of readlinkat
- * added by kernel 2.6.16 or up.
+ * - faccessat() passes if dir_fd is file descriptor to the file and
+ *   pathname is absolute path of the file.
  *
- *****************************************************************************/
-
-#define _GNU_SOURCE
+ * - faccessat() passes if dir_fd is AT_FDCWD and pathname is interpreted
+ *   relative to the symbolic link file.
+ *
+ * - faccessat() passes if dir_fd is AT_FDCWD and pathname is absolute
+ *   path of the file.
+ */
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <fcntl.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/readlinkat.h"
-
-static void setup(void);
-static void cleanup(void);
+#include <stdio.h>
+#include "tst_test.h"
 
-char *TCID = "readlinkat01";
+#define TEST_FILE       "readlink_file"
+#define TEST_SYMLINK    "readlink_symlink"
 
-static int dir_fd, fd;
-static int fd_invalid = 100;
+static int file_fd, dir_fd;
 static int fd_atcwd = AT_FDCWD;
+static const char *abspath;
+static const char *testsymlink;
 
-#define TEST_SYMLINK "readlink_symlink"
-#define TEST_FILE "readlink_file"
-
-static char abspath[1024];
-
-static struct test_case {
-	int *dir_fd;
-	const char *path;
+static struct tcase {
+	int *fd;
+	const char **path;
 	const char *exp_buf;
 	int exp_ret;
 	int exp_errno;
-} test_cases[] = {
-	{&dir_fd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
-	{&dir_fd, abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
-	{&fd, TEST_SYMLINK, NULL, -1, ENOTDIR},
-	{&fd_invalid, TEST_SYMLINK, NULL, -1, EBADF},
-	{&fd_atcwd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
+} tcases[] = {
+	{&dir_fd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
+	{&dir_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
+	{&file_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
+	{&fd_atcwd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
+	{&fd_atcwd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
 };
 
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void verify_readlinkat(struct test_case *test)
+static void verify_readlinkat(unsigned int i)
 {
 	char buf[1024];
+	struct tcase *tc = &tcases[i];
 
 	memset(buf, 0, sizeof(buf));
 
-	TEST(readlinkat(*test->dir_fd, test->path, buf, sizeof(buf)));
-
-	if (TEST_RETURN != test->exp_ret) {
-		tst_resm(TFAIL | TTERRNO,
-		         "readlinkat() returned %ld, expected %d",
-		         TEST_RETURN, test->exp_ret);
-		return;
-	}
-
-	if (TEST_ERRNO != test->exp_errno) {
-		tst_resm(TFAIL | TTERRNO,
-		         "readlinkat() returned %ld, expected %d",
-		         TEST_RETURN, test->exp_ret);
-		return;
-	}
-
-	if (test->exp_ret > 0 && strcmp(test->exp_buf, buf)) {
-		tst_resm(TFAIL, "Unexpected buffer have '%s', expected '%s'",
-		         buf, test->exp_buf);
-		return;
-	}
-
-	tst_resm(TPASS | TTERRNO, "readlinkat() returned %ld", TEST_RETURN);
-}
-
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		for (i = 0; i < TST_TOTAL; i++)
-			verify_readlinkat(&test_cases[i]);
-	}
-
-	cleanup();
-	tst_exit();
+	TST_EXP_POSITIVE(readlinkat(*tc->fd, *tc->path, buf, sizeof(buf)),
+		     "readlinkat(%d, %s, %s, %ld)",
+		     *tc->fd, *tc->path, buf, sizeof(buf));
 }
 
 static void setup(void)
 {
-	tst_tmpdir();
 	char *tmpdir = tst_get_tmpdir();
 
-	snprintf(abspath, sizeof(abspath), "%s/" TEST_SYMLINK, tmpdir);
+	abspath = tst_aprintf("%s/" TEST_SYMLINK, tmpdir);
 	free(tmpdir);
 
-	fd = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT, 0600);
-	SAFE_SYMLINK(cleanup, TEST_FILE, TEST_SYMLINK);
-	dir_fd = SAFE_OPEN(cleanup, ".", O_DIRECTORY);
-
-	TEST_PAUSE;
+	file_fd = SAFE_OPEN(TEST_FILE, O_CREAT, 0600);
+	SAFE_SYMLINK(TEST_FILE, TEST_SYMLINK);
+	dir_fd = SAFE_OPEN(".", O_DIRECTORY);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0 && close(fd))
-		tst_resm(TWARN | TERRNO, "Failed to close fd");
-
-	if (dir_fd > 0 && close(dir_fd))
-		tst_resm(TWARN | TERRNO, "Failed to close dir_fd");
+	if (file_fd > -1)
+		SAFE_CLOSE(file_fd);
 
-	tst_rmdir();
+	if (dir_fd > -1)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.test = verify_readlinkat,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&abspath, .size = sizeof(char)},
+		{&testsymlink, .str = TEST_SYMLINK},
+		{},
+	},
+	.tcnt = ARRAY_SIZE(tcases),
+};
-- 
2.39.1


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

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

* [LTP] [PATCH v2 2/2] syscalls/readlinkat02: Convert to new API
  2023-08-21 11:38 [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API Yang Xu
@ 2023-08-21 11:38 ` Yang Xu
  2023-08-24 11:12   ` Cyril Hrubis
  2023-08-24  9:11 ` [LTP] [PATCH v2 1/2] syscalls/readlinkat01: " Cyril Hrubis
  1 sibling, 1 reply; 5+ messages in thread
From: Yang Xu @ 2023-08-21 11:38 UTC (permalink / raw)
  To: ltp

1.Move the error test of readlinkat01 to readlinkat02
2.Use TST_ macros

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 .../kernel/syscalls/readlinkat/readlinkat02.c | 126 ++++++------------
 1 file changed, 44 insertions(+), 82 deletions(-)

diff --git a/testcases/kernel/syscalls/readlinkat/readlinkat02.c b/testcases/kernel/syscalls/readlinkat/readlinkat02.c
index d30c1917a..2044f9d13 100644
--- a/testcases/kernel/syscalls/readlinkat/readlinkat02.c
+++ b/testcases/kernel/syscalls/readlinkat/readlinkat02.c
@@ -1,122 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2014 Fujitsu Ltd.
+ * Copyright (c) Linux Test Project, 2003-2023
  * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [Description]
  *
- * 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.
+ * Test for EINVAL, ENOTDIR, EBADF errors.
  *
- * 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 Library General Public License for more details.
+ * - readlinkat() fails with EINVAL if the bufsiz is 0.
  *
- * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * - readlinkat() fails with EINVAL if the named file is not a symbolic link.
  *
- */
-/*
- * Test Description:
- *  Verify that,
- *   1. bufsiz is 0, EINVAL should be returned.
- *   2. The named file is not a symbolic link, EINVAL should be returned.
- *   3. The component of the path prefix is not a directory, ENOTDIR should be
- *	returned.
- *   4. pathname is relative and dirfd is a file descriptor referring to a file
- *	other than a directory, ENOTDIR should be returned.
+ * - readlinkat() fails with ENOTDIR if the component of the path prefix is
+ *   not a directory.
+ *
+ * - readlinkat() fails with ENOTDIR if the pathname is relative and
+ *   dirfd is a file descriptor referring to a fileother than a directory.
+ *
+ * - readlinkat() fails with EBADF if the file descriptor is invalid.
  */
 
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/readlinkat.h"
-#include "lapi/syscalls.h"
+#include "tst_test.h"
 
 #define TEST_FILE	"test_file"
 #define SYMLINK_FILE	"symlink_file"
 #define BUFF_SIZE	256
 
 static int file_fd, dir_fd;
+static int fd_invalid = -1;
 
-static struct test_case_t {
-	int *dirfd;
+static struct tcase {
+	int *fd;
 	const char *pathname;
 	size_t bufsiz;
 	int exp_errno;
-} test_cases[] = {
+} tcases[] = {
 	{&dir_fd, SYMLINK_FILE, 0, EINVAL},
 	{&dir_fd, TEST_FILE, BUFF_SIZE, EINVAL},
 	{&file_fd, SYMLINK_FILE, BUFF_SIZE, ENOTDIR},
 	{&dir_fd, "test_file/test_file", BUFF_SIZE, ENOTDIR},
+	{&fd_invalid, SYMLINK_FILE, BUFF_SIZE, EBADF},
 };
 
-char *TCID = "readlinkat02";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void setup(void);
-static void cleanup(void);
-static void readlinkat_verify(const struct test_case_t *);
-
-int main(int argc, char **argv)
+static void verify_readlinkat(unsigned int i)
 {
-	int i, lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
+	char buf[BUFF_SIZE];
+	struct tcase *tc = &tcases[i];
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			readlinkat_verify(&test_cases[i]);
-	}
+	memset(buf, 0, sizeof(buf));
 
-	cleanup();
-	tst_exit();
+	TST_EXP_FAIL(readlinkat(*tc->fd, tc->pathname, buf, tc->bufsiz),
+		     tc->exp_errno, "readlinkat(%d, %s, NULL, %ld)",
+		     *tc->fd, tc->pathname, tc->bufsiz);
 }
 
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	dir_fd = SAFE_OPEN(".", O_RDONLY);
 
-	TEST_PAUSE;
+	file_fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0644);
 
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_RDONLY);
-
-	file_fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0644);
-
-	SAFE_SYMLINK(cleanup, TEST_FILE, SYMLINK_FILE);
-}
-
-static void readlinkat_verify(const struct test_case_t *test)
-{
-	char buf[BUFF_SIZE];
-	TEST(readlinkat(*test->dirfd, test->pathname, buf, test->bufsiz));
-
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "readlinkat succeeded unexpectedly");
-		return;
-	}
-
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO, "readlinkat failed as expected");
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "readlinkat failed unexpectedly; expected: %d - %s",
-			 test->exp_errno, strerror(test->exp_errno));
-	}
+	SAFE_SYMLINK(TEST_FILE, SYMLINK_FILE);
 }
 
 static void cleanup(void)
 {
-	close(dir_fd);
-	close(file_fd);
+	if (file_fd > -1)
+		SAFE_CLOSE(file_fd);
 
-	tst_rmdir();
+	if (dir_fd > -1)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.test = verify_readlinkat,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tcases),
+};
-- 
2.39.1


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

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

* Re: [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API
  2023-08-21 11:38 [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API Yang Xu
  2023-08-21 11:38 ` [LTP] [PATCH v2 2/2] syscalls/readlinkat02: " Yang Xu
@ 2023-08-24  9:11 ` Cyril Hrubis
  2023-08-25  6:21   ` Yang Xu (Fujitsu)
  1 sibling, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2023-08-24  9:11 UTC (permalink / raw)
  To: Yang Xu; +Cc: ltp

Hi!
> +static struct tcase {
> +	int *fd;
> +	const char **path;
>  	const char *exp_buf;
>  	int exp_ret;
>  	int exp_errno;

The last three fields are not unused anymore.

> -} test_cases[] = {
> -	{&dir_fd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> -	{&dir_fd, abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> -	{&fd, TEST_SYMLINK, NULL, -1, ENOTDIR},
> -	{&fd_invalid, TEST_SYMLINK, NULL, -1, EBADF},
> -	{&fd_atcwd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> +} tcases[] = {
> +	{&dir_fd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> +	{&dir_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> +	{&file_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> +	{&fd_atcwd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
> +	{&fd_atcwd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>  };

Looking at readlinkat() manual page thre is a special case where the
pathname is empty string and the call operates on the dirfd, i.e.
attempts to resolve a directory symlink. Can we please add that case
as well? Can be done in follow up patch...

> -int TST_TOTAL = ARRAY_SIZE(test_cases);
> -
> -static void verify_readlinkat(struct test_case *test)
> +static void verify_readlinkat(unsigned int i)
>  {
>  	char buf[1024];
> +	struct tcase *tc = &tcases[i];
>  
>  	memset(buf, 0, sizeof(buf));
>  
> -	TEST(readlinkat(*test->dir_fd, test->path, buf, sizeof(buf)));
> -
> -	if (TEST_RETURN != test->exp_ret) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "readlinkat() returned %ld, expected %d",
> -		         TEST_RETURN, test->exp_ret);
> -		return;
> -	}
> -
> -	if (TEST_ERRNO != test->exp_errno) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "readlinkat() returned %ld, expected %d",
> -		         TEST_RETURN, test->exp_ret);
> -		return;
> -	}
> -
> -	if (test->exp_ret > 0 && strcmp(test->exp_buf, buf)) {
> -		tst_resm(TFAIL, "Unexpected buffer have '%s', expected '%s'",
> -		         buf, test->exp_buf);
> -		return;
> -	}
> -
> -	tst_resm(TPASS | TTERRNO, "readlinkat() returned %ld", TEST_RETURN);
> -}
> -
> -int main(int ac, char **av)
> -{
> -	int lc;
> -	int i;
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		for (i = 0; i < TST_TOTAL; i++)
> -			verify_readlinkat(&test_cases[i]);
> -	}
> -
> -	cleanup();
> -	tst_exit();
> +	TST_EXP_POSITIVE(readlinkat(*tc->fd, *tc->path, buf, sizeof(buf)),
> +		     "readlinkat(%d, %s, %s, %ld)",
> +		     *tc->fd, *tc->path, buf, sizeof(buf));

Can we check that the buf was filled in with the right file name here as
well? We did that in the original test. I guess that we can just do:

	if (strcmp(buf, TEST_FILE))
		tst_res(TPASS, "The filename in buffer is correct");
	else
		tst_res(TFAIL, "Wrong filename in buffer '%s'", buf);

>  }
>  
>  static void setup(void)
>  {
> -	tst_tmpdir();
>  	char *tmpdir = tst_get_tmpdir();
>  
> -	snprintf(abspath, sizeof(abspath), "%s/" TEST_SYMLINK, tmpdir);
> +	abspath = tst_aprintf("%s/" TEST_SYMLINK, tmpdir);
>  	free(tmpdir);
>  
> -	fd = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT, 0600);
> -	SAFE_SYMLINK(cleanup, TEST_FILE, TEST_SYMLINK);
> -	dir_fd = SAFE_OPEN(cleanup, ".", O_DIRECTORY);
> -
> -	TEST_PAUSE;
> +	file_fd = SAFE_OPEN(TEST_FILE, O_CREAT, 0600);
> +	SAFE_SYMLINK(TEST_FILE, TEST_SYMLINK);
> +	dir_fd = SAFE_OPEN(".", O_DIRECTORY);
>  }
>  
>  static void cleanup(void)
>  {
> -	if (fd > 0 && close(fd))
> -		tst_resm(TWARN | TERRNO, "Failed to close fd");
> -
> -	if (dir_fd > 0 && close(dir_fd))
> -		tst_resm(TWARN | TERRNO, "Failed to close dir_fd");
> +	if (file_fd > -1)
> +		SAFE_CLOSE(file_fd);
>  
> -	tst_rmdir();
> +	if (dir_fd > -1)
> +		SAFE_CLOSE(dir_fd);
>  }
> +
> +static struct tst_test test = {
> +	.test = verify_readlinkat,
> +	.needs_tmpdir = 1,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.bufs = (struct tst_buffers []) {
> +		{&abspath, .size = sizeof(char)},

Again, the abspath is initialized dynamically in setup() no need to
allocate it here.

> +		{&testsymlink, .str = TEST_SYMLINK},
> +		{},
> +	},
> +	.tcnt = ARRAY_SIZE(tcases),
> +};
> -- 
> 2.39.1
> 
> 
> -- 
> 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] 5+ messages in thread

* Re: [LTP] [PATCH v2 2/2] syscalls/readlinkat02: Convert to new API
  2023-08-21 11:38 ` [LTP] [PATCH v2 2/2] syscalls/readlinkat02: " Yang Xu
@ 2023-08-24 11:12   ` Cyril Hrubis
  0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2023-08-24 11:12 UTC (permalink / raw)
  To: Yang Xu; +Cc: ltp

Hi!
I've addeds ENOENT case to the test and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API
  2023-08-24  9:11 ` [LTP] [PATCH v2 1/2] syscalls/readlinkat01: " Cyril Hrubis
@ 2023-08-25  6:21   ` Yang Xu (Fujitsu)
  0 siblings, 0 replies; 5+ messages in thread
From: Yang Xu (Fujitsu) @ 2023-08-25  6:21 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp@lists.linux.it


Hi Cyril
> Hi!
>> +static struct tcase {
>> +	int *fd;
>> +	const char **path;
>>   	const char *exp_buf;
>>   	int exp_ret;
>>   	int exp_errno;
> The last three fields are not unused anymore.
Agree.
>> -} test_cases[] = {
>> -	{&dir_fd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> -	{&dir_fd, abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> -	{&fd, TEST_SYMLINK, NULL, -1, ENOTDIR},
>> -	{&fd_invalid, TEST_SYMLINK, NULL, -1, EBADF},
>> -	{&fd_atcwd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> +} tcases[] = {
>> +	{&dir_fd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> +	{&dir_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> +	{&file_fd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> +	{&fd_atcwd, &testsymlink, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>> +	{&fd_atcwd, &abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
>>   };
> Looking at readlinkat() manual page thre is a special case where the
> pathname is empty string and the call operates on the dirfd, i.e.
> attempts to resolve a directory symlink. Can we please add that case
> as well? Can be done in follow up patch...
ok.I'll update it in the v3 version.
>> -int TST_TOTAL = ARRAY_SIZE(test_cases);
>> -
>> -static void verify_readlinkat(struct test_case *test)
>> +static void verify_readlinkat(unsigned int i)
>>   {
>>   	char buf[1024];
>> +	struct tcase *tc = &tcases[i];
>>   
>>   	memset(buf, 0, sizeof(buf));
>>   
>> -	TEST(readlinkat(*test->dir_fd, test->path, buf, sizeof(buf)));
>> -
>> -	if (TEST_RETURN != test->exp_ret) {
>> -		tst_resm(TFAIL | TTERRNO,
>> -		         "readlinkat() returned %ld, expected %d",
>> -		         TEST_RETURN, test->exp_ret);
>> -		return;
>> -	}
>> -
>> -	if (TEST_ERRNO != test->exp_errno) {
>> -		tst_resm(TFAIL | TTERRNO,
>> -		         "readlinkat() returned %ld, expected %d",
>> -		         TEST_RETURN, test->exp_ret);
>> -		return;
>> -	}
>> -
>> -	if (test->exp_ret > 0 && strcmp(test->exp_buf, buf)) {
>> -		tst_resm(TFAIL, "Unexpected buffer have '%s', expected '%s'",
>> -		         buf, test->exp_buf);
>> -		return;
>> -	}
>> -
>> -	tst_resm(TPASS | TTERRNO, "readlinkat() returned %ld", TEST_RETURN);
>> -}
>> -
>> -int main(int ac, char **av)
>> -{
>> -	int lc;
>> -	int i;
>> -
>> -	tst_parse_opts(ac, av, NULL, NULL);
>> -
>> -	setup();
>> -
>> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
>> -		for (i = 0; i < TST_TOTAL; i++)
>> -			verify_readlinkat(&test_cases[i]);
>> -	}
>> -
>> -	cleanup();
>> -	tst_exit();
>> +	TST_EXP_POSITIVE(readlinkat(*tc->fd, *tc->path, buf, sizeof(buf)),
>> +		     "readlinkat(%d, %s, %s, %ld)",
>> +		     *tc->fd, *tc->path, buf, sizeof(buf));
> Can we check that the buf was filled in with the right file name here as
> well? We did that in the original test. I guess that we can just do:
>
> 	if (strcmp(buf, TEST_FILE))
> 		tst_res(TPASS, "The filename in buffer is correct");
> 	else
> 		tst_res(TFAIL, "Wrong filename in buffer '%s'", buf);

ok.I'll add this.

and i think "if (strcmp(buf, TEST_FILE))" should be changed to "if 
(strcmp(buf, TEST_FILE) == 0)".

>>   }
>>   
>>   static void setup(void)
>>   {
>> -	tst_tmpdir();
>>   	char *tmpdir = tst_get_tmpdir();
>>   
>> -	snprintf(abspath, sizeof(abspath), "%s/" TEST_SYMLINK, tmpdir);
>> +	abspath = tst_aprintf("%s/" TEST_SYMLINK, tmpdir);
>>   	free(tmpdir);
>>   
>> -	fd = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT, 0600);
>> -	SAFE_SYMLINK(cleanup, TEST_FILE, TEST_SYMLINK);
>> -	dir_fd = SAFE_OPEN(cleanup, ".", O_DIRECTORY);
>> -
>> -	TEST_PAUSE;
>> +	file_fd = SAFE_OPEN(TEST_FILE, O_CREAT, 0600);
>> +	SAFE_SYMLINK(TEST_FILE, TEST_SYMLINK);
>> +	dir_fd = SAFE_OPEN(".", O_DIRECTORY);
>>   }
>>   
>>   static void cleanup(void)
>>   {
>> -	if (fd > 0 && close(fd))
>> -		tst_resm(TWARN | TERRNO, "Failed to close fd");
>> -
>> -	if (dir_fd > 0 && close(dir_fd))
>> -		tst_resm(TWARN | TERRNO, "Failed to close dir_fd");
>> +	if (file_fd > -1)
>> +		SAFE_CLOSE(file_fd);
>>   
>> -	tst_rmdir();
>> +	if (dir_fd > -1)
>> +		SAFE_CLOSE(dir_fd);
>>   }
>> +
>> +static struct tst_test test = {
>> +	.test = verify_readlinkat,
>> +	.needs_tmpdir = 1,
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.bufs = (struct tst_buffers []) {
>> +		{&abspath, .size = sizeof(char)},
> Again, the abspath is initialized dynamically in setup() no need to
> allocate it here.

ok.

Thanks for the patient review.

Best Regards
Yang Xu

>> +		{&testsymlink, .str = TEST_SYMLINK},
>> +		{},
>> +	},
>> +	.tcnt = ARRAY_SIZE(tcases),
>> +};
>> -- 
>> 2.39.1
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

end of thread, other threads:[~2023-08-25  6:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-21 11:38 [LTP] [PATCH v2 1/2] syscalls/readlinkat01: Convert to new API Yang Xu
2023-08-21 11:38 ` [LTP] [PATCH v2 2/2] syscalls/readlinkat02: " Yang Xu
2023-08-24 11:12   ` Cyril Hrubis
2023-08-24  9:11 ` [LTP] [PATCH v2 1/2] syscalls/readlinkat01: " Cyril Hrubis
2023-08-25  6:21   ` Yang Xu (Fujitsu)

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.