All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [RFC PATCH 8/8] syscalls/fchmod06: Convert to the new library
Date: Thu,  5 Apr 2018 16:01:54 +0200	[thread overview]
Message-ID: <20180405140154.6218-9-chrubis@suse.cz> (raw)
In-Reply-To: <20180405140154.6218-1-chrubis@suse.cz>

+ Make use of .needs_rofs

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/fchmod/fchmod06.c | 189 +++++++++-------------------
 1 file changed, 56 insertions(+), 133 deletions(-)

diff --git a/testcases/kernel/syscalls/fchmod/fchmod06.c b/testcases/kernel/syscalls/fchmod/fchmod06.c
index a5c6d911c..b5240086e 100644
--- a/testcases/kernel/syscalls/fchmod/fchmod06.c
+++ b/testcases/kernel/syscalls/fchmod/fchmod06.c
@@ -1,23 +1,22 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Author: Wayne Boyer
+ * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
  *
- *   Copyright (c) International Business Machines  Corp., 2001
- *   Author: Wayne Boyer
+ * 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.
  *
- *   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.
+ * 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.
  *
- *   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.
- *
- *   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
+ * 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
  */
-
 /*
  * Test that fchmod() fails and sets the proper errno values.
  */
@@ -25,152 +24,76 @@
 #ifndef _GNU_SOURCE
 # define _GNU_SOURCE
 #endif
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <unistd.h>
-#include <grp.h>
+#include <sys/types.h>
 #include <pwd.h>
-#include <sys/mount.h>
+#include "tst_test.h"
 
-#include "test.h"
-#include "safe_macros.h"
+#define MNT_POINT "mntpoint"
 
 static int fd1;
 static int fd2;
 static int fd3;
-static const char *device;
-static int mount_flag;
 
-static struct test_case_t {
-	char *name;
+static struct tcase {
 	int *fd;
 	int mode;
 	int exp_errno;
-} test_cases[] = {
-	{"EPERM", &fd1, 0644, EPERM},
-	{"EBADF", &fd2, 0644, EBADF},
-	{"EROFS", &fd3, 0644, EROFS},
+} tcases[] = {
+	{&fd1, 0644, EPERM},
+	{&fd2, 0644, EBADF},
+	{&fd3, 0644, EROFS},
 };
 
-char *TCID = "fchmod06";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void verify_fchmod(unsigned int i)
 {
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	struct tcase *tc = &tcases[i];
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
+	TEST(fchmod(*tc->fd, tc->mode));
 
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			TEST(fchmod(*test_cases[i].fd, test_cases[i].mode));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO == test_cases[i].exp_errno) {
-					tst_resm(TPASS | TTERRNO,
-						 "fchmod: test %s success",
-						 test_cases[i].name);
-				} else {
-					tst_resm(TFAIL | TTERRNO,
-						 "fchmod: test %s FAILED with "
-						 "unexpect errno: %d",
-						 test_cases[i].name,
-						 TEST_ERRNO);
-				}
-			} else {
-				tst_resm(TFAIL,
-					 "fchmod: test %s success unexpectly",
-					 test_cases[i].name);
-			}
-		}
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "fchmod() passed unexpectedly (%li)",
+		        TEST_RETURN);
+		return;
+	}
 
+	if (TEST_ERRNO == tcases[i].exp_errno) {
+		tst_res(TPASS | TTERRNO, "fchmod() failed expectedly");
+		return;
 	}
 
-	cleanup();
-	tst_exit();
+	tst_res(TFAIL | TTERRNO,
+	        "fchmod() failed unexpectedly, expected %i - %s",
+	        TEST_ERRNO, tst_strerrno(TEST_ERRNO));
 }
 
 static void setup(void)
 {
-	struct passwd *ltpuser;
-	const char *fs_type;
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	fs_type = tst_dev_fs_type();
-	device = tst_acquire_device(cleanup);
+	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
 
-	if (!device)
-		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
+	fd3 = SAFE_OPEN(MNT_POINT"/file", O_RDONLY);
+	fd1 = SAFE_OPEN("tfile_1", O_RDWR | O_CREAT, 0666);
+	fd2 = SAFE_OPEN("tfile_2", O_RDWR | O_CREAT, 0666);
+	SAFE_CLOSE(fd2);
 
-	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
-
-	SAFE_MKDIR(cleanup, "mntpoint", 0755);
-
-	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, 0, NULL);
-	mount_flag = 1;
-
-	/* Create a file in the file system, then remount it as read-only */
-	SAFE_TOUCH(cleanup, "mntpoint/tfile_3", 0644, NULL);
-
-	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type,
-		   MS_REMOUNT | MS_RDONLY, NULL);
-
-	fd3 = SAFE_OPEN(cleanup, "mntpoint/tfile_3", O_RDONLY);
-
-	fd1 = SAFE_OPEN(cleanup, "tfile_1", O_RDWR | O_CREAT, 0666);
-
-	fd2 = SAFE_OPEN(cleanup, "tfile_2", O_RDWR | O_CREAT, 0666);
-
-	SAFE_CLOSE(cleanup, fd2);
-
-	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
-
-	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+	SAFE_SETEUID(ltpuser->pw_uid);
 }
 
 static void cleanup(void)
 {
-	if (seteuid(0))
-		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
-
-	if (fd1 > 0 && close(fd1))
-		tst_resm(TWARN | TERRNO, "close(fd1) failed");
-
-	if (fd3 > 0 && close(fd3))
-		tst_resm(TWARN | TERRNO, "close(fd1) failed");
-
-	if (mount_flag && tst_umount("mntpoint") < 0) {
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "umount device:%s failed", device);
-	}
-
-	if (device)
-		tst_release_device(device);
+	if (fd1 > 0)
+		SAFE_CLOSE(fd1);
 
-	tst_rmdir();
+	if (fd3 > 0)
+		SAFE_CLOSE(fd3);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_fchmod,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_rofs = 1,
+	.mntpoint = MNT_POINT,
+};
-- 
2.13.6


      parent reply	other threads:[~2018-04-05 14:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-05 14:01 [LTP] Make use of .needs_rofs Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 1/8] lib/tst_test.c: mntpoint implies tmpdir Cyril Hrubis
2018-04-10  8:58   ` Xiao Yang
2018-04-10  9:14     ` Cyril Hrubis
2018-04-10  9:46       ` Xiao Yang
2018-04-10 10:21         ` Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 2/8] lib/tst_test: Populate the read-only fs Cyril Hrubis
2018-04-18 14:29   ` Cyril Hrubis
2018-04-23  8:45   ` Petr Vorel
2018-04-23 13:59     ` Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 3/8] syscalls/chmod06: Rewrite the new library Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 4/8] syscalls/rmdir02: Make use of .needs_rofs Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 5/8] syscalls/mkdirat02: Rewrite to the new library Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 6/8] syscalls/link08: " Cyril Hrubis
2018-04-05 14:01 ` [LTP] [RFC PATCH 7/8] syscalls/utimes01: " Cyril Hrubis
2018-04-05 14:01 ` Cyril Hrubis [this message]

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=20180405140154.6218-9-chrubis@suse.cz \
    --to=chrubis@suse.cz \
    --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.