All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v3 10/16] syscalls/setxattr01: Convert to the new library.
Date: Wed, 11 Oct 2017 16:41:24 +0200	[thread overview]
Message-ID: <20171011144130.29728-10-chrubis@suse.cz> (raw)
In-Reply-To: <20171011144130.29728-1-chrubis@suse.cz>

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/setxattr/setxattr01.c | 138 +++++++++---------------
 1 file changed, 51 insertions(+), 87 deletions(-)

diff --git a/testcases/kernel/syscalls/setxattr/setxattr01.c b/testcases/kernel/syscalls/setxattr/setxattr01.c
index 359991e3f..86df7ba73 100644
--- a/testcases/kernel/syscalls/setxattr/setxattr01.c
+++ b/testcases/kernel/syscalls/setxattr/setxattr01.c
@@ -57,10 +57,7 @@
 #ifdef HAVE_SYS_XATTR_H
 # include <sys/xattr.h>
 #endif
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "setxattr01";
+#include "tst_test.h"
 
 #ifdef HAVE_SYS_XATTR_H
 #define XATTR_NAME_MAX 255
@@ -69,157 +66,124 @@ char *TCID = "setxattr01";
 #define XATTR_TEST_KEY "user.testkey"
 #define XATTR_TEST_VALUE "this is a test value"
 #define XATTR_TEST_VALUE_SIZE 20
+#define FNAME "setxattr01testfile"
 
-static void setup(void);
-static void cleanup(void);
-
-char filename[BUFSIZ];
-char long_key[XATTR_NAME_LEN];
-char *long_value;
+static char long_key[XATTR_NAME_LEN];
+static char *long_value;
+static char *xattr_value = XATTR_TEST_VALUE;
 
 struct test_case {
-	char *fname;
 	char *key;
-	char *value;
+	char **value;
 	size_t size;
 	int flags;
 	int exp_err;
 };
 struct test_case tc[] = {
 	{			/* case 00, invalid flags */
-	 .fname = filename,
-	 .key = XATTR_TEST_KEY,
-	 .value = XATTR_TEST_VALUE,
+	 .key = long_key,
+	 .value = &xattr_value,
 	 .size = XATTR_TEST_VALUE_SIZE,
 	 .flags = ~0,
 	 .exp_err = EINVAL,
 	 },
 	{			/* case 01, replace non-existing attribute */
-	 .fname = filename,
 	 .key = XATTR_TEST_KEY,
-	 .value = XATTR_TEST_VALUE,
+	 .value = &xattr_value,
 	 .size = XATTR_TEST_VALUE_SIZE,
 	 .flags = XATTR_REPLACE,
 	 .exp_err = ENODATA,
 	 },
-	{			/* case 02, long key name, key will be set in setup() */
-	 .fname = filename,
-	 .key = NULL,
-	 .value = XATTR_TEST_VALUE,
+	{			/* case 02, long key name */
+	 .key = long_key,
+	 .value = &xattr_value,
 	 .size = XATTR_TEST_VALUE_SIZE,
 	 .flags = XATTR_CREATE,
 	 .exp_err = ERANGE,
 	 },
-	{			/* case 03, long value, value will be set in setup() */
-	 .fname = filename,
+	{			/* case 03, long value */
 	 .key = XATTR_TEST_KEY,
-	 .value = NULL,
+	 .value = &long_value,
 	 .size = XATTR_SIZE_MAX + 1,
 	 .flags = XATTR_CREATE,
 	 .exp_err = E2BIG,
 	 },
 	{			/* case 04, zero length value */
-	 .fname = filename,
 	 .key = XATTR_TEST_KEY,
-	 .value = XATTR_TEST_VALUE,
+	 .value = &xattr_value,
 	 .size = 0,
 	 .flags = XATTR_CREATE,
 	 .exp_err = 0,
 	 },
 	{			/* case 05, create existing attribute */
-	 .fname = filename,
 	 .key = XATTR_TEST_KEY,
-	 .value = XATTR_TEST_VALUE,
+	 .value = &xattr_value,
 	 .size = XATTR_TEST_VALUE_SIZE,
 	 .flags = XATTR_CREATE,
 	 .exp_err = EEXIST,
 	 },
 	{			/* case 06, replace existing attribute */
-	 .fname = filename,
 	 .key = XATTR_TEST_KEY,
-	 .value = XATTR_TEST_VALUE,
+	 .value = &xattr_value,
 	 .size = XATTR_TEST_VALUE_SIZE,
 	 .flags = XATTR_REPLACE,
 	 .exp_err = 0,
-	 },
+	},
 };
 
-int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
-
-int main(int argc, char *argv[])
+static void verify_setxattr(unsigned int i)
 {
-	int lc;
-	int i;
+	TEST(setxattr(FNAME, tc[i].key, *tc[i].value, tc[i].size, tc[i].flags));
 
-	tst_parse_opts(argc, argv, NULL, NULL);
+	if (TEST_RETURN == -1 && TEST_ERRNO == EOPNOTSUPP)
+		tst_brk(TCONF, "setxattr() not supported");
 
-	setup();
+	if (!tc[i].exp_err) {
+		if (TEST_RETURN) {
+			tst_res(TFAIL | TTERRNO,
+				"setxattr() failed with %li", TEST_RETURN);
+			return;
+		}
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+		tst_res(TPASS, "setxattr() passed");
+		return;
+	}
 
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value,
-				      tc[i].size, tc[i].flags));
+	if (TEST_RETURN == 0) {
+		tst_res(TFAIL, "setxattr() passed unexpectedly");
+		return;
+	}
 
-			if (TEST_ERRNO == tc[i].exp_err) {
-				tst_resm(TPASS | TTERRNO, "expected behavior");
-			} else {
-				tst_resm(TFAIL | TTERRNO, "unexpected behavior "
-					 "- expected errno %d - Got",
-					 tc[i].exp_err);
-			}
-		}
+	if (TEST_ERRNO != tc[i].exp_err) {
+		tst_res(TFAIL | TTERRNO, "setxattr() should fail with %s",
+			tst_strerrno(tc[i].exp_err));
+		return;
 	}
 
-	cleanup();
-	tst_exit();
+	tst_res(TPASS | TTERRNO, "setxattr() failed");
 }
 
 static void setup(void)
 {
-	int fd;
-
-	tst_require_root();
+	SAFE_TOUCH(FNAME, 0644, NULL);
 
-	tst_tmpdir();
-
-	/* Test for xattr support */
-	fd = SAFE_CREAT(cleanup, "testfile", 0644);
-	close(fd);
-	if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
-		if (errno == ENOTSUP)
-			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
-				 "mount without user_xattr option");
-
-	/* Create test file */
-	snprintf(filename, BUFSIZ, "setxattr01testfile");
-	fd = SAFE_CREAT(cleanup, filename, 0644);
-	close(fd);
-
-	/* Prepare test cases */
 	snprintf(long_key, 6, "%s", "user.");
 	memset(long_key + 5, 'k', XATTR_NAME_LEN - 5);
 	long_key[XATTR_NAME_LEN - 1] = '\0';
-	tc[2].key = long_key;
 
-	long_value = malloc(XATTR_SIZE_MAX + 2);
-	if (!long_value)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc failed");
+	long_value = SAFE_MALLOC(XATTR_SIZE_MAX + 2);
 	memset(long_value, 'v', XATTR_SIZE_MAX + 2);
 	long_value[XATTR_SIZE_MAX + 1] = '\0';
-	tc[3].value = long_value;
-
-	TEST_PAUSE;
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_setxattr,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
 #else /* HAVE_SYS_XATTR_H */
-int main(int argc, char *argv[])
-{
-	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
-}
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
 #endif
-- 
2.13.5


  parent reply	other threads:[~2017-10-11 14:41 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-11 14:41 [LTP] [PATCH v3 01/16] lib/tst_mkfs: Clear first 512k of the device Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 02/16] lib: Add interface to list supported filesystems Cyril Hrubis
2017-11-09 18:38   ` Sandeep Patil
2017-10-11 14:41 ` [LTP] [PATCH v3 03/16] SAFE_MOUNT: Handle FUSE mounts as well Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 04/16] lib/tst_test: Add .all_filesystems flag Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 05/16] lib/tst_fs: Add tst_fill_fs() Cyril Hrubis
2017-11-09 18:50   ` Sandeep Patil
2017-10-11 14:41 ` [LTP] [PATCH v3 06/16] syscalls/fallocate05: New test Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 07/16] syscalls/msync04: Run test for all filesystems Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 08/16] syscalls/fallocate04: Convert to the new library Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 09/16] syscalls/fallocate04: Run test for all filesystems Cyril Hrubis
2017-10-11 14:41 ` Cyril Hrubis [this message]
2017-10-11 14:41 ` [LTP] [PATCH v3 11/16] syscalls/setxattr01: " Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 12/16] syscallse/setxattr02: Convert to the new library Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 13/16] syscalls/fsync01: " Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 14/16] syscalls/fsync01: Run test for all filesystems Cyril Hrubis
2017-10-11 14:41 ` [LTP] [PATCH v3 15/16] fs/fs_fill: Add a test to fill a FS in a few threads Cyril Hrubis
2017-11-09 10:03   ` Li Wang
2017-11-09 10:19     ` Cyril Hrubis
2017-11-09 10:43       ` Li Wang
2017-11-09 10:50         ` Cyril Hrubis
2017-11-09 10:58           ` Li Wang
2017-10-11 14:41 ` [LTP] [PATCH v3 16/16] doc: Update device flags in test-writing-guidelines Cyril Hrubis
2017-11-09 18:51   ` Sandeep Patil
2017-11-01 12:24 ` [LTP] [PATCH v3 01/16] lib/tst_mkfs: Clear first 512k of the device Cyril Hrubis
2017-11-02  9:26   ` Jan Stancek
2017-11-02 13:15     ` 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=20171011144130.29728-10-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.