All of lore.kernel.org
 help / color / mirror / Atom feed
From: tangmeng <tangmeng@uniontech.com>
To: ltp@lists.linux.it
Cc: tangmeng <tangmeng@uniontech.com>
Subject: [LTP] [PATCH 2/2] fchownat/fchownat02: Convert to new API
Date: Wed, 17 Nov 2021 11:39:51 +0800	[thread overview]
Message-ID: <20211117033951.16352-2-tangmeng@uniontech.com> (raw)
In-Reply-To: <20211117033951.16352-1-tangmeng@uniontech.com>

Signed-off-by: tangmeng <tangmeng@uniontech.com>
---
 .../kernel/syscalls/fchownat/fchownat02.c     | 121 +++++-------------
 1 file changed, 34 insertions(+), 87 deletions(-)

diff --git a/testcases/kernel/syscalls/fchownat/fchownat02.c b/testcases/kernel/syscalls/fchownat/fchownat02.c
index d19f3f3b8..116d4ca37 100644
--- a/testcases/kernel/syscalls/fchownat/fchownat02.c
+++ b/testcases/kernel/syscalls/fchownat/fchownat02.c
@@ -1,132 +1,69 @@
+// 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.
  */
-/*
- * Test Description:
- *   Verify that,
- *   The flag of fchownat() is AT_SYMLINK_NOFOLLOW and the pathname would
- *   not be dereferenced if the pathname is a symbolic link.
+
+/*\
+ * [Description]
+ *
+ * Verify that,
+ * The flag of fchownat() is AT_SYMLINK_NOFOLLOW and the pathname would
+ * not be dereferenced if the pathname is a symbolic link.
  */

 #define _GNU_SOURCE

-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
 #include "fchownat.h"
 #include "lapi/fcntl.h"

 #define TESTFILE	"testfile"
 #define TESTFILE_LINK	"testfile_link"

-char *TCID = "fchownat02";
-int TST_TOTAL = 1;
-
 static int dir_fd;
 static uid_t set_uid = 1000;
 static gid_t set_gid = 1000;
-static void setup(void);
-static void cleanup(void);
-static void test_verify(void);
-static void fchownat_verify(void);
-
-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();
-	}
-
-	cleanup();
-	tst_exit();
-}

 static void setup(void)
 {
 	struct stat c_buf, l_buf;

-	if ((tst_kvercmp(2, 6, 16)) < 0)
-		tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
-
-	tst_require_root();
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);

-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	SAFE_TOUCH(TESTFILE, 0600, NULL);

-	TEST_PAUSE;
+	SAFE_SYMLINK(TESTFILE, TESTFILE_LINK);

-	tst_tmpdir();
+	SAFE_STAT(TESTFILE_LINK, &c_buf);

-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK);
-
-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
-
-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);

 	if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) {
-		tst_brkm(TBROK | TERRNO, cleanup,
+		tst_brk(TBROK | TERRNO,
 			 "link_uid(%d) == set_uid(%d) or link_gid(%d) == "
 			 "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid,
 			 set_gid);
 	}
 }

-static void fchownat_verify(void)
-{
-	TEST(fchownat(dir_fd, TESTFILE_LINK, set_uid, set_gid,
-		      AT_SYMLINK_NOFOLLOW));
-
-	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL | TTERRNO, "fchownat() failed, errno=%d : %s",
-			 TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		test_verify();
-	}
-}
-
-static void test_verify(void)
+static void verify_fchownat(void)
 {
 	struct stat c_buf, l_buf;

-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
+	TST_EXP_PASS_SILENT(fchownat(dir_fd, TESTFILE_LINK, set_uid, set_gid,
+		      AT_SYMLINK_NOFOLLOW));
+
+	SAFE_STAT(TESTFILE_LINK, &c_buf);

-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);

 	if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid &&
 	    c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) {
-		tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
+		tst_res(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
 	} else {
-		tst_resm(TFAIL,
+		tst_res(TFAIL,
 			 "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d "
 			 "link_uid=%d set_uid=%d | gid=%d link_gid=%d "
 			 "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid,
@@ -136,5 +73,15 @@ static void test_verify(void)

 static void cleanup(void)
 {
-	tst_rmdir();
+       if (dir_fd > 0)
+               close(dir_fd);
 }
+
+static struct tst_test test = {
+        .min_kver = "2.6.16",
+        .test_all = verify_fchownat,
+        .setup = setup,
+        .cleanup = cleanup,
+        .needs_tmpdir = 1,
+	.needs_root = 1,
+};
--
2.20.1




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

  reply	other threads:[~2021-11-17  3:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-17  3:39 [LTP] [PATCH 1/2] fchownat/fchownat01: Convert to new API tangmeng
2021-11-17  3:39 ` tangmeng [this message]
2021-12-08 16:18   ` [LTP] [PATCH 2/2] fchownat/fchownat02: " Cyril Hrubis
2021-12-08 16:06 ` [LTP] [PATCH 1/2] fchownat/fchownat01: " Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211117033951.16352-2-tangmeng@uniontech.com \
    --to=tangmeng@uniontech.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.