[LTP] [PATCH v2] swapon02: Simplify code, add copyright, modify docparse

Yang Xu xuyang2018.jy@fujitsu.com
Wed Nov 29 07:12:12 CET 2023


Simplify permission-related test code, making structures look simpler

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 testcases/kernel/syscalls/swapon/swapon02.c | 66 +++++++--------------
 1 file changed, 22 insertions(+), 44 deletions(-)

diff --git a/testcases/kernel/syscalls/swapon/swapon02.c b/testcases/kernel/syscalls/swapon/swapon02.c
index d34c17a80..e5129723e 100644
--- a/testcases/kernel/syscalls/swapon/swapon02.c
+++ b/testcases/kernel/syscalls/swapon/swapon02.c
@@ -1,56 +1,39 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2002-2023
  */
 
 /*\
  * [Description]
  *
  * This test case checks whether swapon(2) system call returns
- *  1. ENOENT when the path does not exist
- *  2. EINVAL when the path exists but is invalid
- *  3. EPERM when user is not a superuser
- *  4. EBUSY when the specified path is already being used as a swap area
+ * - ENOENT when the path does not exist
+ * - EINVAL when the path exists but is invalid
+ * - EPERM when user is not a superuser
+ * - EBUSY when the specified path is already being used as a swap area
  */
 
-#include <errno.h>
 #include <pwd.h>
 
 #include "tst_test.h"
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-static void setup01(void);
-static void cleanup01(void);
-
 static uid_t nobody_uid;
 static int do_swapoff;
 
 static struct tcase {
 	char *err_desc;
 	int exp_errno;
-	char *exp_errval;
 	char *path;
-	void (*setup)(void);
-	void (*cleanup)(void);
 } tcases[] = {
-	{"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
-	{"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL},
-	{"Permission denied", EPERM, "EPERM", "./swapfile01", setup01, cleanup01},
-	{"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL},
+	{"Path does not exist", ENOENT, "./doesnotexist"},
+	{"Invalid path", EINVAL, "./notswap"},
+	{"Permission denied", EPERM, "./swapfile01"},
+	{"File already used", EBUSY, "./alreadyused"},
 };
 
-static void setup01(void)
-{
-	SAFE_SETEUID(nobody_uid);
-}
-
-static void cleanup01(void)
-{
-	SAFE_SETEUID(0);
-}
-
 static void setup(void)
 {
 	struct passwd *nobody;
@@ -70,7 +53,7 @@ static void setup(void)
 		do_swapoff = 1;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (do_swapoff && tst_syscall(__NR_swapoff, "alreadyused"))
 		tst_res(TWARN | TERRNO, "swapoff(alreadyused) failed");
@@ -79,24 +62,19 @@ void cleanup(void)
 static void verify_swapon(unsigned int i)
 {
 	struct tcase *tc = tcases + i;
-	if (tc->setup)
-		tc->setup();
-
-	TEST(tst_syscall(__NR_swapon, tc->path, 0));
-
-	if (tc->cleanup)
-		tc->cleanup();
-
-	if (TST_RET == -1 && TST_ERR == tc->exp_errno) {
-		tst_res(TPASS, "swapon(2) expected failure;"
-			 " Got errno - %s : %s",
-			 tc->exp_errval, tc->err_desc);
-		return;
+	if (tc->exp_errno == EPERM)
+		SAFE_SETEUID(nobody_uid);
+
+	TST_EXP_FAIL(tst_syscall(__NR_swapon, tc->path, 0), tc->exp_errno,
+		     "swapon(2) fail with %s", tc->err_desc);
+
+	if (tc->exp_errno == EPERM)
+		SAFE_SETEUID(0);
+
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "swapon(2) failed unexpectedly, expected: %s",
+			tst_strerrno(tc->exp_errno));
 	}
-
-	tst_res(TFAIL, "swapon(2) failed to produce expected error:"
-	         " %d, errno: %s and got %d.", tc->exp_errno,
-	         tc->exp_errval, TST_ERR);
 }
 
 static struct tst_test test = {
-- 
2.39.1



More information about the ltp mailing list