[LTP] [PATCH] setresuid05.c: Rewrite using new LTP API

Avinesh Kumar akumar@suse.de
Wed Dec 7 16:53:07 CET 2022


Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 .../kernel/syscalls/setresuid/setresuid05.c   | 107 +++++-------------
 1 file changed, 26 insertions(+), 81 deletions(-)

diff --git a/testcases/kernel/syscalls/setresuid/setresuid05.c b/testcases/kernel/syscalls/setresuid/setresuid05.c
index c00d07b19..efdcbd18f 100644
--- a/testcases/kernel/syscalls/setresuid/setresuid05.c
+++ b/testcases/kernel/syscalls/setresuid/setresuid05.c
@@ -1,104 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * 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.
- */
-/*
- * Test Description:
- *  Verify that,
- *	File system UID is always set to the same value as the (possibly new)
- *	effective UID.
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
-#define _GNU_SOURCE
+/*\
+ * [Description]
+ *
+ * Verify that after updating euid with setresuid(), any file creation
+ * also gets the new euid as its owner user ID.
+ */
 
-#include <errno.h>
-#include <unistd.h>
 #include <pwd.h>
 #include <sys/stat.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "compat_16.h"
+#include "tst_test.h"
+#include "compat_tst_16.h"
 
-TCID_DEFINE(setresuid05);
-int TST_TOTAL = 1;
+#define TEMP_FILE	"testfile"
 static struct passwd *ltpuser;
-static void setup(void);
-static void setresuid_verify(void);
-static void cleanup(void);
-
-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++)
-			setresuid_verify();
-	}
-
-	cleanup();
-	tst_exit();
-}
 
 static void setup(void)
 {
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
-
-	UID16_CHECK(ltpuser->pw_uid, "setresuid", cleanup)
+	ltpuser = SAFE_GETPWNAM("nobody");
+	UID16_CHECK(ltpuser->pw_uid, "setresuid");
 }
 
-static void setresuid_verify(void)
+static void run(void)
 {
 	struct stat buf;
 
-	TEST(SETRESUID(cleanup, -1, ltpuser->pw_uid, -1));
-
-	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL | TTERRNO, "setresuid failed unexpectedly");
-		return;
-	}
+	TST_EXP_PASS(SETRESUID(-1, ltpuser->pw_uid, -1));
 
-	SAFE_TOUCH(cleanup, "test_file", 0644, NULL);
+	SAFE_TOUCH(TEMP_FILE, 0644, NULL);
+	SAFE_STAT(TEMP_FILE, &buf);
 
-	SAFE_STAT(cleanup, "test_file", &buf);
+	TST_EXP_EQ_LI(ltpuser->pw_uid, buf.st_uid);
 
-	if (ltpuser->pw_uid == buf.st_uid) {
-		tst_resm(TPASS, "setresuid succeeded as expected");
-	} else {
-		tst_resm(TFAIL,
-			 "setresuid failed unexpectedly; euid(%d) - st_uid(%d)",
-			 ltpuser->pw_uid, buf.st_uid);
-	}
+	SAFE_UNLINK(TEMP_FILE);
+	TST_EXP_PASS_SILENT(SETRESUID(-1, 0, -1));
 }
 
-static void cleanup(void)
-{
-	if (seteuid(0) < 0)
-		tst_resm(TWARN | TERRNO, "seteuid failed");
-
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_root = 1,
+	.needs_tmpdir = 1
+};
-- 
2.38.1



More information about the ltp mailing list