[LTP] [PATCH v2 1/3] fchownat01: Convert to new API

Yang Xu xuyang2018.jy@fujitsu.com
Fri Dec 22 10:44:53 CET 2023


Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 .../kernel/syscalls/fchownat/fchownat01.c     | 163 +++++++-----------
 1 file changed, 61 insertions(+), 102 deletions(-)

diff --git a/testcases/kernel/syscalls/fchownat/fchownat01.c b/testcases/kernel/syscalls/fchownat/fchownat01.c
index 7771c111a..c00c6063e 100644
--- a/testcases/kernel/syscalls/fchownat/fchownat01.c
+++ b/testcases/kernel/syscalls/fchownat/fchownat01.c
@@ -1,133 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
- *
- *   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.
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2006-2023
+ * Author: Yi Yang <yyangcdl@cn.ibm.com>
+ */
+
+/*\
+ * [Description]
  *
- *   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.
+ * Check the basic functionality of the faccessat() system call.
  *
- *   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
- */
-/*
- * DESCRIPTION
- *	This test case will verify basic function of fchownat
- *	added by kernel 2.6.16 or up.
+ * - fchownat() passes if dir_fd is file descriptor to the directory
+ *   where the file is located and pathname is relative path of the file.
+ * - fchownat() passes if pathname is absolute, then dirfd is ignored.
+ * - fchownat() passes if pathname is relative and dirfd is the special
+ *   value AT_FDCWD.
+ * - fchownat() passes if pathname is an empty string, operate on the file
+ *   referred to by dirfd.
+ * - fchownat() passes if pathname is a symbolic link, operate on the link
+ *   itself.
  */
 
 #define _GNU_SOURCE
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#include "tst_test.h"
 
-#define TESTFILE	"testfile"
-
-static void setup(void);
-static void cleanup(void);
+#define TESTFILE        "testfile"
+#define TESTFILE_LINK   "testfile_link"
 
 static int dir_fd;
-static int fd;
-static int no_fd = -1;
 static int cu_fd = AT_FDCWD;
+static char *testfile;
+static char *abspath;
+static char *symfile;
+static char *empty;
 
-static struct test_case_t {
-	int exp_ret;
-	int exp_errno;
-	int flag;
+static struct tcase {
 	int *fds;
-	char *filenames;
-} test_cases[] = {
-	{0, 0, 0, &dir_fd, TESTFILE},
-	{-1, ENOTDIR, 0, &fd, TESTFILE},
-	{-1, EBADF, 0, &no_fd, TESTFILE},
-	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
-	{0, 0, 0, &cu_fd, TESTFILE},
+	char **filenames;
+	int flag;
+} tcases[] = {
+	{&dir_fd, &testfile, 0},
+	{&dir_fd, &abspath, 0},
+	{&cu_fd, &testfile, 0},
+	{&cu_fd, &empty, AT_EMPTY_PATH},
+	{&dir_fd, &symfile, AT_SYMLINK_NOFOLLOW}
 };
 
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-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++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			fchownat_verify(&test_cases[i]);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
 
-	tst_tmpdir();
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
 
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
+	SAFE_SYMLINK(TESTFILE, TESTFILE_LINK);
 
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
+	char *tmpdir = tst_get_tmpdir();
 
-	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
+	abspath = tst_aprintf("%s/" TESTFILE, tmpdir);
+	free(tmpdir);
 }
 
-static void fchownat_verify(const struct test_case_t *test)
+static void fchownat_verify(unsigned int i)
 {
-	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-		      getegid(), test->flag));
+	struct tcase *tc = &tcases[i];
 
-	if (TEST_RETURN != test->exp_ret) {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() returned %ld, expected %d, errno=%d",
-			 TEST_RETURN, test->exp_ret, test->exp_errno);
-		return;
-	}
-
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			 "fchownat() returned the expected errno %d: %s",
-			 test->exp_ret, strerror(test->exp_errno));
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() failed unexpectedly; expected: %d - %s",
-			 test->exp_errno, strerror(test->exp_errno));
-	}
+	TST_EXP_PASS(fchownat(*tc->fds, *tc->filenames, geteuid(), getegid(),
+		     tc->flag), "fchownat(%d, %s, %d, %d, %d)",
+		     *tc->fds, *tc->filenames, geteuid(), getegid(), tc->flag);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0)
-		close(fd);
-
-	if (dir_fd > 0)
-		close(dir_fd);
-
-	tst_rmdir();
+	if (dir_fd > -1)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = fchownat_verify,
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&testfile, .str = TESTFILE},
+		{&empty, .str = ""},
+		{&symfile, .str = TESTFILE_LINK},
+		{},
+	},
+	.needs_tmpdir = 1,
+};
-- 
2.39.1



More information about the ltp mailing list