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

Yang Xu xuyang2018.jy@fujitsu.com
Sat Aug 5 06:08:02 CEST 2023


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

diff --git a/testcases/kernel/syscalls/readlinkat/readlinkat02.c b/testcases/kernel/syscalls/readlinkat/readlinkat02.c
index d30c1917a..cc2691715 100644
--- a/testcases/kernel/syscalls/readlinkat/readlinkat02.c
+++ b/testcases/kernel/syscalls/readlinkat/readlinkat02.c
@@ -1,39 +1,30 @@
+// 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 "tst_test.h"
+#include "lapi/fcntl.h"
 #include "lapi/readlinkat.h"
 #include "lapi/syscalls.h"
 
@@ -42,81 +33,55 @@
 #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



More information about the ltp mailing list