All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase
@ 2016-11-02 10:59 Dejan Jovicevic
  2016-11-02 10:59 ` [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(), Dejan Jovicevic
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

v2 -> v3:
    - Removed unnecessary null-termination for longpathname in 
    llistxattr02 new testcase.
    - Modified the code to be more readable and maintainable,
    this resulted in creating a new patch that pretty much does
    the same thing for llistxattr() tests.
    - Started usign corresponding safe macros in the setup of all
    of the listxattr related tests. This resulted in a new patch
    that implements these safe macros.

v1 -> v2:
    - Modified the setup for the new testcase in llistxattr02 and 
    a testcase in listxattr02.
    - Modified commit messages.

This series introduces new testcase for llistxattr02 and tests for
flistxattr() and listxattr(). Safe macros are added for functions
that set extended attribute values, and are being used in appropriate
tests. The code in tests for llistxattr() was slightly modified to be
more readable and easier to maintain, this practice was followed in
creating new tests for flistxattr() and listxattr().

Dejan Jovicevic (5):
  safe_macros: add safe_setxattr(), safe_lsetxattr() and     
    safe_fsetxattr()
  llistxattr: moved to using safe macros
  llistxattr: improved code readability and stability
  llistxattr: new testcase for long path name
  flistxattr and listxattr: added tests

 include/tst_safe_macros.h                          |  12 +++
 lib/safe_macros.c                                  |  66 +++++++++++++
 runtest/syscalls                                   |   8 ++
 testcases/kernel/syscalls/.gitignore               |   6 ++
 testcases/kernel/syscalls/flistxattr/Makefile      |  23 +++++
 .../kernel/syscalls/flistxattr/flistxattr01.c      | 102 ++++++++++++++++++++
 .../kernel/syscalls/flistxattr/flistxattr02.c      | 104 ++++++++++++++++++++
 .../kernel/syscalls/flistxattr/flistxattr03.c      |  97 +++++++++++++++++++
 testcases/kernel/syscalls/listxattr/Makefile       |  23 +++++
 testcases/kernel/syscalls/listxattr/listxattr01.c  |  94 ++++++++++++++++++
 testcases/kernel/syscalls/listxattr/listxattr02.c  | 105 +++++++++++++++++++++
 testcases/kernel/syscalls/listxattr/listxattr03.c  |  89 +++++++++++++++++
 .../kernel/syscalls/llistxattr/llistxattr01.c      |  46 +++------
 .../kernel/syscalls/llistxattr/llistxattr02.c      |  53 +++++------
 .../kernel/syscalls/llistxattr/llistxattr03.c      |  24 ++---
 15 files changed, 773 insertions(+), 79 deletions(-)
 create mode 100644 testcases/kernel/syscalls/flistxattr/Makefile
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr01.c
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr02.c
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr03.c
 create mode 100644 testcases/kernel/syscalls/listxattr/Makefile
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr01.c
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr02.c
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr03.c

-- 
1.9.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [LTP]  [PATCH v3 1/5] safe_macros: add safe_setxattr(),
  2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
@ 2016-11-02 10:59 ` Dejan Jovicevic
  2016-11-02 15:37   ` Cyril Hrubis
  2016-11-02 10:59 ` [LTP] [PATCH v3 2/5] llistxattr: moved to using safe macros Dejan Jovicevic
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

Added SAFE_SETXATTR(), SAFE_LSETXATTR() and SAFE_FSETXATTR()
macros to simplify error checking in test preparation. Instead
of calling the system function, checking for their return value
and aborting the test if the operation has failed, just use
corresponding safe macro.

Signed-off-by: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
---
 include/tst_safe_macros.h | 12 +++++++++
 lib/safe_macros.c         | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index d1519f9..936f4dc 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -433,4 +433,16 @@ static inline sighandler_t safe_signal(const char *file, const int lineno,
 	         "execlp(%s, %s, ...) failed", file, arg); \
 	} while (0)
 
+#define SAFE_SETXATTR(path, name, value, size, flags) \
+	safe_setxattr(__FILE__, __LINE__, NULL, (path), (name), (value), (size), \
+				(flags))
+
+#define SAFE_LSETXATTR(path, name, value, size, flags) \
+	safe_lsetxattr(__FILE__, __LINE__, NULL, (path), (name), (value), (size), \
+				(flags))
+
+#define SAFE_FSETXATTR(fd, name, value, size, flags) \
+	safe_fsetxattr(__FILE__, __LINE__, NULL, (fd), (name), (value), (size), \
+				(flags))
+
 #endif /* SAFE_MACROS_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 5a05c84..e1d01b9 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -794,3 +794,69 @@ struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn
 	errno = err;
 	return rval;
 }
+
+int safe_setxattr(const char *file, const int lineno, void (cleanup_fn)(void),
+				const char *path, const char *name, const void *value,
+				size_t size, int flags)
+{
+	int rval;
+
+	rval = setxattr(path, name, value, size, flags);
+
+	if (rval) {
+		if (errno == ENOTSUP) {
+			tst_brkm(TCONF, cleanup_fn,
+				 "%s:%d: no xattr support in fs or mounted "
+				 "without user_xattr option", file, lineno);
+		}
+
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: setxattr() failed",
+			     file, lineno);
+	}
+
+	return rval;
+}
+
+int safe_lsetxattr(const char *file, const int lineno, void (cleanup_fn)(void),
+				const char *path, const char *name, const void *value,
+				size_t size, int flags)
+{
+	int rval;
+
+	rval = lsetxattr(path, name, value, size, flags);
+
+	if (rval) {
+		if (errno == ENOTSUP) {
+			tst_brkm(TCONF, cleanup_fn,
+				 "%s:%d: no xattr support in fs or mounted "
+				 "without user_xattr option", file, lineno);
+		}
+
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: lsetxattr() failed",
+			     file, lineno);
+	}
+
+	return rval;
+}
+
+int safe_fsetxattr(const char *file, const int lineno, void (cleanup_fn)(void),
+				int fd, const char *name, const void *value, size_t size,
+				int flags)
+{
+	int rval;
+
+	rval = fsetxattr(fd, name, value, size, flags);
+
+	if (rval) {
+		if (errno == ENOTSUP) {
+			tst_brkm(TCONF, cleanup_fn,
+				 "%s:%d: no xattr support in fs or mounted "
+				 "without user_xattr option", file, lineno);
+		}
+
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: fsetxattr() failed",
+			     file, lineno);
+	}
+
+	return rval;
+}
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [LTP]  [PATCH v3 2/5] llistxattr: moved to using safe macros
  2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
  2016-11-02 10:59 ` [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(), Dejan Jovicevic
@ 2016-11-02 10:59 ` Dejan Jovicevic
  2016-11-02 15:38   ` Cyril Hrubis
  2016-11-02 10:59 ` [LTP] [PATCH v3 3/5] llistxattr: improved code readability and Dejan Jovicevic
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

Apply newly created SAFE_LSETXATTR() macro to all files
that test llistxattr().

Signed-off-by: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
---
 .../kernel/syscalls/llistxattr/llistxattr01.c      | 25 ++--------------------
 .../kernel/syscalls/llistxattr/llistxattr02.c      | 10 +--------
 .../kernel/syscalls/llistxattr/llistxattr03.c      | 10 +--------
 3 files changed, 4 insertions(+), 41 deletions(-)

diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr01.c b/testcases/kernel/syscalls/llistxattr/llistxattr01.c
index 03d7f85..ef27991 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr01.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr01.c
@@ -43,27 +43,6 @@
 #define VALUE_SIZE	4
 #define KEY_SIZE    17
 
-static void set_xattr(const char *path, const char *key)
-{
-	int n;
-
-	n = lsetxattr(path, key, VALUE, VALUE_SIZE, XATTR_CREATE);
-	if (n == -1) {
-		if (errno == ENOTSUP) {
-			tst_brk(TCONF,
-				 "no xattr support in fs or mounted "
-				 "without user_xattr option");
-		}
-
-		if (errno == EEXIST) {
-			tst_brk(TBROK, "exist attribute %s", key);
-		} else {
-			tst_brk(TBROK | TERRNO,
-				 "lsetxattr() failed");
-		}
-	}
-}
-
 static int has_attribute(const char *list, int llen, const char *attr)
 {
 	int i;
@@ -106,9 +85,9 @@ static void setup(void)
 
 	SAFE_SYMLINK("testfile", "symlink");
 
-	set_xattr("testfile", SECURITY_KEY1);
+	SAFE_LSETXATTR("testfile", SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
 
-	set_xattr("symlink", SECURITY_KEY2);
+	SAFE_LSETXATTR("symlink", SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr02.c b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
index ec4ba6e..7c09b5b 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr02.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
@@ -86,15 +86,7 @@ static void setup(void)
 
 	SAFE_SYMLINK("testfile", "symlink");
 
-	n = lsetxattr("symlink", SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
-	if (n == -1) {
-		if (errno == ENOTSUP) {
-			tst_brk(TCONF, "no xattr support in fs or "
-				 "mounted without user_xattr option");
-		} else {
-			tst_brk(TBROK | TERRNO, "lsetxattr() failed");
-		}
-	}
+	SAFE_LSETXATTR("symlink", SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr03.c b/testcases/kernel/syscalls/llistxattr/llistxattr03.c
index 9b65de9..da16048 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr03.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr03.c
@@ -77,15 +77,7 @@ static void setup(void)
 
 	SAFE_TOUCH(filename[1], 0644, NULL);
 
-	ret = lsetxattr(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
-	if (ret == -1) {
-		if (errno == ENOTSUP) {
-			tst_brk(TCONF, "no xattr support in fs or "
-				 "mounted without user_xattr option");
-		} else {
-			tst_brk(TBROK | TERRNO, "lsetxattr() failed");
-		}
-	}
+	SAFE_LSETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
 }
 
 static struct tst_test test = {
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [LTP]  [PATCH v3 3/5] llistxattr: improved code readability and
  2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
  2016-11-02 10:59 ` [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(), Dejan Jovicevic
  2016-11-02 10:59 ` [LTP] [PATCH v3 2/5] llistxattr: moved to using safe macros Dejan Jovicevic
@ 2016-11-02 10:59 ` Dejan Jovicevic
  2016-11-02 15:44   ` Cyril Hrubis
  2016-11-02 10:59 ` [LTP] [PATCH v3 4/5] llistxattr: new testcase for long path name Dejan Jovicevic
  2016-11-02 10:59 ` [LTP] [PATCH v3 5/5] flistxattr and listxattr: added tests Dejan Jovicevic
  4 siblings, 1 reply; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

Changed bits of code, such as hardcoded magic constants,
unnecessary else branches, way of passing array size to
a function, removed unnecessary comments, moved to using
macros instead of string literals where seemed to be fit.

This was done to make the code easier to read and understand,
and make it easier to maintain in case of changes

Signed-off-by: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
---
 .../kernel/syscalls/llistxattr/llistxattr01.c      | 25 +++++++-------
 .../kernel/syscalls/llistxattr/llistxattr02.c      | 38 ++++++++++------------
 .../kernel/syscalls/llistxattr/llistxattr03.c      | 14 +++-----
 3 files changed, 36 insertions(+), 41 deletions(-)

diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr01.c b/testcases/kernel/syscalls/llistxattr/llistxattr01.c
index ef27991..9161b94 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr01.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr01.c
@@ -40,8 +40,10 @@
 #define SECURITY_KEY1	"security.ltptest1"
 #define SECURITY_KEY2	"security.ltptest2"
 #define VALUE	"test"
-#define VALUE_SIZE	4
-#define KEY_SIZE    17
+#define VALUE_SIZE	(sizeof(VALUE) - 1)
+#define KEY_SIZE    (sizeof(SECURITY_KEY1) - 1)
+#define TESTFILE    "testfile"
+#define SYMLINK    "symlink"
 
 static int has_attribute(const char *list, int llen, const char *attr)
 {
@@ -56,22 +58,21 @@ static int has_attribute(const char *list, int llen, const char *attr)
 
 static void verify_llistxattr(void)
 {
-	int size = 64;
-	char buf[size];
+	char buf[64];
 
-	TEST(llistxattr("symlink", buf, size));
+	TEST(llistxattr(SYMLINK, buf, sizeof(buf)));
 	if (TEST_RETURN == -1) {
-		tst_res(TFAIL | TERRNO, "llistxattr() failed");
+		tst_res(TFAIL | TTERRNO, "llistxattr() failed");
 		return;
 	}
 
-	if (has_attribute(buf, size, SECURITY_KEY1)) {
+	if (has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
 		tst_res(TFAIL, "get file attribute %s unexpectlly",
 			 SECURITY_KEY1);
 		return;
 	}
 
-	if (!has_attribute(buf, size, SECURITY_KEY2)) {
+	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY2)) {
 		tst_res(TFAIL, "missing attribute %s", SECURITY_KEY2);
 		return;
 	}
@@ -81,13 +82,13 @@ static void verify_llistxattr(void)
 
 static void setup(void)
 {
-	SAFE_TOUCH("testfile", 0644, NULL);
+	SAFE_TOUCH(TESTFILE, 0644, NULL);
 
-	SAFE_SYMLINK("testfile", "symlink");
+	SAFE_SYMLINK(TESTFILE, SYMLINK);
 
-	SAFE_LSETXATTR("testfile", SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
+	SAFE_LSETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
 
-	SAFE_LSETXATTR("symlink", SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
+	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr02.c b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
index 7c09b5b..191ec36 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr02.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
@@ -43,18 +43,17 @@
 
 #define SECURITY_KEY	"security.ltptest"
 #define VALUE	"test"
-#define VALUE_SIZE	4
+#define VALUE_SIZE	(sizeof(VALUE) - 1)
+#define TESTFILE   "testfile"
+#define SYMLINK    "symlink"
 
 static struct test_case {
 	const char *path;
 	size_t size;
 	int exp_err;
 } tc[] = {
-	/* test1 */
-	{"symlink", 1, ERANGE},
-	/* test2 */
+	{SYMLINK, 1, ERANGE},
 	{"", 20, ENOENT},
-	/* test3 */
 	{(char *)-1, 20, EFAULT}
 };
 
@@ -63,30 +62,29 @@ static void verify_llistxattr(unsigned int n)
 	struct test_case *t = tc + n;
 	char buf[t->size];
 
-	TEST(llistxattr(t->path, buf, t->size));
-	if (TEST_RETURN != -1) {
+	TEST(llistxattr(t->path, buf, sizeof(buf)));
+	if (TEST_RETURN == 0) {
 		tst_res(TFAIL, "llistxattr() succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != t->exp_err) {
+		tst_res(TFAIL | TTERRNO, "llistxattr() failed "
+			 "unexpectedlly, expected %s",
+			 tst_strerrno(t->exp_err));
 	} else {
-		if (TEST_ERRNO != t->exp_err) {
-			tst_res(TFAIL | TTERRNO, "llistxattr() failed "
-				 "unexpectedlly, expected %s",
-				 tst_strerrno(t->exp_err));
-		} else {
-			tst_res(TPASS | TTERRNO,
-				 "llistxattr() failed as expected");
-		}
+		tst_res(TPASS | TTERRNO,
+			 "llistxattr() failed as expected");
 	}
 }
 
 static void setup(void)
 {
-	int n;
-
-	SAFE_TOUCH("testfile", 0644, NULL);
+	SAFE_TOUCH(TESTFILE, 0644, NULL);
 
-	SAFE_SYMLINK("testfile", "symlink");
+	SAFE_SYMLINK(TESTFILE, SYMLINK);
 
-	SAFE_LSETXATTR("symlink", SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr03.c b/testcases/kernel/syscalls/llistxattr/llistxattr03.c
index da16048..e2c651e 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr03.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr03.c
@@ -37,7 +37,7 @@
 
 #define SECURITY_KEY	"security.ltptest"
 #define VALUE	"test"
-#define VALUE_SIZE	4
+#define VALUE_SIZE	(sizeof(VALUE) - 1)
 
 static const char *filename[] = {"testfile1", "testfile2"};
 
@@ -46,11 +46,9 @@ static int check_suitable_buf(const char *name, long size)
 	int n;
 	char buf[size];
 
-	n = llistxattr(name, buf, size);
-	if (n == -1)
-		return 0;
-	else
-		return 1;
+	n = llistxattr(name, buf, sizeof(buf));
+
+	return n != -1;
 }
 
 static void verify_llistxattr(unsigned int n)
@@ -59,7 +57,7 @@ static void verify_llistxattr(unsigned int n)
 
 	TEST(llistxattr(name, NULL, 0));
 	if (TEST_RETURN == -1) {
-		tst_res(TFAIL | TERRNO, "llistxattr() failed");
+		tst_res(TFAIL | TTERRNO, "llistxattr() failed");
 		return;
 	}
 
@@ -71,8 +69,6 @@ static void verify_llistxattr(unsigned int n)
 
 static void setup(void)
 {
-	int ret;
-
 	SAFE_TOUCH(filename[0], 0644, NULL);
 
 	SAFE_TOUCH(filename[1], 0644, NULL);
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [LTP]  [PATCH v3 4/5] llistxattr: new testcase for long path name
  2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
                   ` (2 preceding siblings ...)
  2016-11-02 10:59 ` [LTP] [PATCH v3 3/5] llistxattr: improved code readability and Dejan Jovicevic
@ 2016-11-02 10:59 ` Dejan Jovicevic
  2016-11-02 15:47   ` Cyril Hrubis
  2016-11-02 10:59 ` [LTP] [PATCH v3 5/5] flistxattr and listxattr: added tests Dejan Jovicevic
  4 siblings, 1 reply; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

Added a test case that checks if the path name, given by the
first argument of the function, is too long. If that is the
case, the function should return -1 and errno should be set
to ENAMETOOLONG.

Signed-off-by: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
---
 testcases/kernel/syscalls/llistxattr/llistxattr02.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/syscalls/llistxattr/llistxattr02.c b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
index 191ec36..04d8c97 100644
--- a/testcases/kernel/syscalls/llistxattr/llistxattr02.c
+++ b/testcases/kernel/syscalls/llistxattr/llistxattr02.c
@@ -22,11 +22,13 @@
 * to hold the result.
 * 2) llistxattr(2) fails if path is an empty string.
 * 3) llistxattr(2) fails when attempted to read from a invalid address.
+* 4) llistxattr(2) fails if path is longer than allowed.
 *
 * Expected Result:
 * 1) llistxattr(2) should return -1 and set errno to ERANGE.
 * 2) llistxattr(2) should return -1 and set errno to ENOENT.
 * 3) llistxattr(2) should return -1 and set errno to EFAULT.
+* 4) llistxattr(2) should return -1 and set errno to ENAMETOOLONG.
 */
 
 #include "config.h"
@@ -47,6 +49,8 @@
 #define TESTFILE   "testfile"
 #define SYMLINK    "symlink"
 
+char longpathname[PATH_MAX + 2];
+
 static struct test_case {
 	const char *path;
 	size_t size;
@@ -54,7 +58,8 @@ static struct test_case {
 } tc[] = {
 	{SYMLINK, 1, ERANGE},
 	{"", 20, ENOENT},
-	{(char *)-1, 20, EFAULT}
+	{(char *)-1, 20, EFAULT},
+	{longpathname, 20, ENAMETOOLONG}
 };
 
 static void verify_llistxattr(unsigned int n)
@@ -85,6 +90,8 @@ static void setup(void)
 	SAFE_SYMLINK(TESTFILE, SYMLINK);
 
 	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+
+	memset(&longpathname, 'a', sizeof(longpathname) - 1);
 }
 
 static struct tst_test test = {
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [LTP]  [PATCH v3 5/5] flistxattr and listxattr: added tests
  2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
                   ` (3 preceding siblings ...)
  2016-11-02 10:59 ` [LTP] [PATCH v3 4/5] llistxattr: new testcase for long path name Dejan Jovicevic
@ 2016-11-02 10:59 ` Dejan Jovicevic
  2016-11-02 16:13   ` Cyril Hrubis
  4 siblings, 1 reply; 11+ messages in thread
From: Dejan Jovicevic @ 2016-11-02 10:59 UTC (permalink / raw)
  To: ltp

Tests for these two system calls were not present.

flistxattr(), listxattr() and llistxattr() are identical,
the only difference is in the first argument:
    - in listxattr() it refers to the file in the path.
    - in llistxattr() in the case of a symbolic link, the list
    of names of extended attributes associated with the link
    itself is retrieved, not the file that it refers to.
    - in flistxattr() the open file referred to by fd is
    interrogated in place of path.

Because of these similarities, implemented tests for flistxattr()
and listxattr() are based on the existing tests for llistxattr(),
with some adjustments to fit the appropriate system call.

Signed-off-by: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
---
 runtest/syscalls                                   |   8 ++
 testcases/kernel/syscalls/.gitignore               |   6 ++
 testcases/kernel/syscalls/flistxattr/Makefile      |  23 +++++
 .../kernel/syscalls/flistxattr/flistxattr01.c      | 102 ++++++++++++++++++++
 .../kernel/syscalls/flistxattr/flistxattr02.c      | 104 ++++++++++++++++++++
 .../kernel/syscalls/flistxattr/flistxattr03.c      |  97 +++++++++++++++++++
 testcases/kernel/syscalls/listxattr/Makefile       |  23 +++++
 testcases/kernel/syscalls/listxattr/listxattr01.c  |  94 ++++++++++++++++++
 testcases/kernel/syscalls/listxattr/listxattr02.c  | 105 +++++++++++++++++++++
 testcases/kernel/syscalls/listxattr/listxattr03.c  |  89 +++++++++++++++++
 10 files changed, 651 insertions(+)
 create mode 100644 testcases/kernel/syscalls/flistxattr/Makefile
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr01.c
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr02.c
 create mode 100644 testcases/kernel/syscalls/flistxattr/flistxattr03.c
 create mode 100644 testcases/kernel/syscalls/listxattr/Makefile
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr01.c
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr02.c
 create mode 100644 testcases/kernel/syscalls/listxattr/listxattr03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 7c84296..c56d49d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -272,6 +272,10 @@ fcntl34_64 fcntl34_64
 fdatasync01 fdatasync01
 fdatasync02 fdatasync02
 
+flistxattr01 flistxattr01
+flistxattr02 flistxattr02
+flistxattr03 flistxattr03
+
 flock01 flock01
 flock02 flock02
 flock03 flock03
@@ -523,6 +527,10 @@ linkat02 linkat02
 
 listen01 listen01
 
+listxattr01 listxattr01
+listxattr02 listxattr02
+listxattr03 listxattr03
+
 llistxattr01 llistxattr01
 llistxattr02 llistxattr02
 llistxattr03 llistxattr03
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index f53cc05..aaccbfe 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -233,6 +233,9 @@
 /fcntl/fcntl34_64
 /fdatasync/fdatasync01
 /fdatasync/fdatasync02
+/flistxattr/flistxattr01
+/flistxattr/flistxattr02
+/flistxattr/flistxattr03
 /flock/flock01
 /flock/flock02
 /flock/flock03
@@ -482,6 +485,9 @@
 /linkat/linkat01
 /linkat/linkat02
 /listen/listen01
+/listxattr/listxattr01
+/listxattr/listxattr02
+/listxattr/listxattr03
 /llistxattr/llistxattr01
 /llistxattr/llistxattr02
 /llistxattr/llistxattr03
diff --git a/testcases/kernel/syscalls/flistxattr/Makefile b/testcases/kernel/syscalls/flistxattr/Makefile
new file mode 100644
index 0000000..b794ec0
--- /dev/null
+++ b/testcases/kernel/syscalls/flistxattr/Makefile
@@ -0,0 +1,23 @@
+#
+#  Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+#  Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
+#
+#  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.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/flistxattr/flistxattr01.c b/testcases/kernel/syscalls/flistxattr/flistxattr01.c
new file mode 100644
index 0000000..5084f7d
--- /dev/null
+++ b/testcases/kernel/syscalls/flistxattr/flistxattr01.c
@@ -0,0 +1,102 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: verify_flistxattr01
+*
+* Description:
+*     The testcase checks the basic functionality of the flistxattr(2).
+*     flistxattr(2) retrieves the list of extended attribute names
+*     associated with the file itself in the filesystem.
+*
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+#include <string.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY1 "security.ltptest1"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+#define KEY_SIZE      (sizeof(SECURITY_KEY1) - 1)
+
+static int fd;
+
+static int has_attribute(const char *list, int llen, const char *attr)
+{
+	int i;
+
+	for (i = 0; i < llen; i += strlen(list + i) + 1) {
+		if (!strcmp(list + i, attr))
+			return 1;
+	}
+	return 0;
+}
+
+static void verify_flistxattr(void)
+{
+	char buf[64];
+
+	TEST(flistxattr(fd, buf, sizeof(buf)));
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
+		return;
+	}
+
+	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
+		tst_res(TFAIL, "missing attribute %s",
+			 SECURITY_KEY1);
+		return;
+	}
+
+	tst_res(TPASS, "flistxattr() succeeded");
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
+
+	SAFE_FSETXATTR(fd, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
+}
+
+static void cleanup(void)
+{
+	if (fd > 0 && close(fd))
+		tst_res(TWARN | TERRNO, "failed to close file");
+}
+
+static struct tst_test test = {
+	.tid = "flistxattr01",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = verify_flistxattr,
+	.setup = setup,
+	.cleanup = cleanup,
+};
+
+#else
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif /* HAVE_SYS_XATTR_H */
+
diff --git a/testcases/kernel/syscalls/flistxattr/flistxattr02.c b/testcases/kernel/syscalls/flistxattr/flistxattr02.c
new file mode 100644
index 0000000..f8a761e
--- /dev/null
+++ b/testcases/kernel/syscalls/flistxattr/flistxattr02.c
@@ -0,0 +1,104 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: flistxattr02
+*
+* Description:
+*     1) flistxattr(2) fails if the size of the list buffer is too small
+*     to hold the result.
+*     2) flistxattr(2) fails if fd is an invalid file descriptor.
+*
+* Expected Result:
+*     1) flistxattr(2) should return -1 and set errno to ERANGE.
+*     2) flistxattr(2) should return -1 and set errno to EBADF.
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY  "security.ltptest"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+
+static int fd1;
+static int fd2 = -1;
+
+static struct test_case {
+	int *fd;
+	size_t size;
+	int exp_err;
+} tc[] = {
+	{&fd1, 1, ERANGE},
+	{&fd2, 20, EBADF}
+};
+
+static void verify_flistxattr(unsigned int n)
+{
+	struct test_case *t = tc + n;
+	char buf[t->size];
+
+	TEST(flistxattr(*t->fd, buf, t->size));
+	if (TEST_RETURN == 0) {
+		tst_res(TFAIL, "flistxattr() succeeded unexpectedly");
+		return;
+	}
+
+	if (t->exp_err != TEST_ERRNO) {
+		tst_res(TFAIL | TTERRNO, "flistxattr() failed "
+			 "unexpectedlly, expected %s",
+			 tst_strerrno(t->exp_err));
+	} else {
+		tst_res(TPASS | TTERRNO,
+			 "flistxattr() failed as expected");
+	}
+}
+
+static void setup(void)
+{
+	fd1 = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
+
+	SAFE_FSETXATTR(fd1, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+}
+
+static void cleanup(void)
+{
+	if (fd1 > 0 && close(fd1))
+		tst_res(TWARN | TERRNO, "failed to close file");
+}
+
+static struct tst_test test = {
+	.tid = "flistxattr02",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test = verify_flistxattr,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+	.cleanup = cleanup,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif
diff --git a/testcases/kernel/syscalls/flistxattr/flistxattr03.c b/testcases/kernel/syscalls/flistxattr/flistxattr03.c
new file mode 100644
index 0000000..b2745be
--- /dev/null
+++ b/testcases/kernel/syscalls/flistxattr/flistxattr03.c
@@ -0,0 +1,97 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: flistxattr03
+*
+* Description:
+*     flistxattr is identical to listxattr. an empty buffer of size zero
+*     can return the current size of the list of extended attribute names,
+*     which can be used to estimate a suitable buffer.
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY  "security.ltptest"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+
+static int fd[] = {0, 0};
+
+static int check_suitable_buf(const int file, long size)
+{
+	int n;
+	char buf[size];
+
+	n = flistxattr(file, buf, sizeof(buf));
+
+	return n != -1;
+}
+
+static void verify_flistxattr(unsigned int n)
+{
+	TEST(flistxattr(fd[n], NULL, 0));
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
+		return;
+	}
+
+	if (check_suitable_buf(fd[n], TEST_RETURN))
+		tst_res(TPASS, "flistxattr() succeed with suitable buffer");
+	else
+		tst_res(TFAIL, "flistxattr() failed with small buffer");
+}
+
+static void setup(void)
+{
+	fd[0] = SAFE_OPEN("testfile1", O_RDWR | O_CREAT, 0644);
+
+	fd[1] = SAFE_OPEN("testfile2", O_RDWR | O_CREAT, 0644);
+
+	SAFE_FSETXATTR(fd[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+}
+
+static void cleanup(void)
+{
+	if (fd[1] > 0 && close(fd[1]))
+		tst_res(TWARN | TERRNO, "failed to close file");
+	if (fd[0] > 0 && close(fd[0]))
+		tst_res(TWARN | TERRNO, "failed to close file");
+}
+
+static struct tst_test test = {
+	.tid = "flistxattr03",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test = verify_flistxattr,
+	.tcnt = ARRAY_SIZE(fd),
+	.setup = setup,
+	.cleanup = cleanup,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif
diff --git a/testcases/kernel/syscalls/listxattr/Makefile b/testcases/kernel/syscalls/listxattr/Makefile
new file mode 100644
index 0000000..b794ec0
--- /dev/null
+++ b/testcases/kernel/syscalls/listxattr/Makefile
@@ -0,0 +1,23 @@
+#
+#  Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+#  Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
+#
+#  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.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/listxattr/listxattr01.c b/testcases/kernel/syscalls/listxattr/listxattr01.c
new file mode 100644
index 0000000..110b6de
--- /dev/null
+++ b/testcases/kernel/syscalls/listxattr/listxattr01.c
@@ -0,0 +1,94 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: listxattr01
+*
+* Description:
+*     The testcase checks the basic functionality of the listxattr(2).
+*     listxattr(2) retrieves the list of extended attribute names
+*     associated with the file itself in the filesystem.
+*
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+#include <string.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY1 "security.ltptest1"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+#define KEY_SIZE      (sizeof(SECURITY_KEY1) - 1)
+#define TESTFILE      "testfile"
+
+static int has_attribute(const char *list, int llen, const char *attr)
+{
+	int i;
+
+	for (i = 0; i < llen; i += strlen(list + i) + 1) {
+		if (!strcmp(list + i, attr))
+			return 1;
+	}
+	return 0;
+}
+
+static void verify_listxattr(void)
+{
+	char buf[64];
+
+	TEST(listxattr(TESTFILE, buf, sizeof(buf)));
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL | TTERRNO, "listxattr() failed");
+		return;
+	}
+
+	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
+		tst_res(TFAIL, "missing attribute %s",
+			 SECURITY_KEY1);
+		return;
+	}
+
+	tst_res(TPASS, "listxattr() succeeded");
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(TESTFILE, 0644, NULL);
+
+	SAFE_SETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
+}
+
+static struct tst_test test = {
+	.tid = "listxattr01",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = verify_listxattr,
+	.setup = setup,
+};
+
+#else
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif /* HAVE_SYS_XATTR_H */
+
diff --git a/testcases/kernel/syscalls/listxattr/listxattr02.c b/testcases/kernel/syscalls/listxattr/listxattr02.c
new file mode 100644
index 0000000..6a980f5
--- /dev/null
+++ b/testcases/kernel/syscalls/listxattr/listxattr02.c
@@ -0,0 +1,105 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: listxattr02
+*
+* Description:
+*     1) listxattr(2) fails if the size of the list buffer is too small
+*     to hold the result.
+*     2) listxattr(2) fails if path is an empty string.
+*     3) listxattr(2) fails when attempted to read from a invalid address.
+*     4) listxattr(2) fails if path is longer than allowed.
+*
+* Expected Result:
+*     1) listxattr(2) should return -1 and set errno to ERANGE.
+*     2) listxattr(2) should return -1 and set errno to ENOENT.
+*     3) listxattr(2) should return -1 and set errno to EFAULT.
+*     4) listxattr(2) should return -1 and set errno to ENAMETOOLONG.
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY  "security.ltptest"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+#define TESTFILE      "testfile"
+
+char longpathname[PATH_MAX + 2];
+
+static struct test_case {
+	const char *path;
+	size_t size;
+	int exp_err;
+} tc[] = {
+	{TESTFILE, 1, ERANGE},
+	{"", 20, ENOENT},
+	{(char *)-1, 20, EFAULT},
+	{longpathname, 20, ENAMETOOLONG}
+};
+
+static void verify_listxattr(unsigned int n)
+{
+	struct test_case *t = tc + n;
+	char buf[t->size];
+
+	TEST(listxattr(t->path, buf, sizeof(buf)));
+	if (TEST_RETURN == 0) {
+		tst_res(TFAIL, "listxattr() succeeded unexpectedly");
+		return;
+	}
+
+	if (t->exp_err != TEST_ERRNO) {
+		tst_res(TFAIL | TTERRNO, "listxattr() failed "
+			 "unexpectedlly, expected %s",
+			 tst_strerrno(t->exp_err));
+	} else {
+		tst_res(TPASS | TTERRNO,
+			 "listxattr() failed as expected");
+	}
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(TESTFILE, 0644, NULL);
+
+	SAFE_SETXATTR(TESTFILE, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+
+	memset(&longpathname, 'a', sizeof(longpathname) - 1);
+}
+
+static struct tst_test test = {
+	.tid = "listxattr02",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test = verify_listxattr,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif
diff --git a/testcases/kernel/syscalls/listxattr/listxattr03.c b/testcases/kernel/syscalls/listxattr/listxattr03.c
new file mode 100644
index 0000000..d8c0cdc
--- /dev/null
+++ b/testcases/kernel/syscalls/listxattr/listxattr03.c
@@ -0,0 +1,89 @@
+/*
+* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
+* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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
+* alone with this program.
+*/
+
+/*
+* Test Name: listxattr03
+*
+* Description:
+*     An empty buffer of size zero can return the current size of the list
+*     of extended attribute names, which can be used to estimate a suitable buffer.
+*/
+
+#include "config.h"
+#include <errno.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define SECURITY_KEY  "security.ltptest"
+#define VALUE         "test"
+#define VALUE_SIZE    (sizeof(VALUE) - 1)
+
+static const char * const filename[] = {"testfile1", "testfile2"};
+
+static int check_suitable_buf(const char *name, long size)
+{
+	int n;
+	char buf[size];
+
+	n = listxattr(name, buf, sizeof(buf));
+
+	return n != -1;
+}
+
+static void verify_listxattr(unsigned int n)
+{
+	const char *name = filename[n];
+
+	TEST(listxattr(name, NULL, 0));
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL | TTERRNO, "listxattr() failed");
+		return;
+	}
+
+	if (check_suitable_buf(name, TEST_RETURN))
+		tst_res(TPASS, "listxattr() succeed with suitable buffer");
+	else
+		tst_res(TFAIL, "listxattr() failed with small buffer");
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(filename[0], 0644, NULL);
+
+	SAFE_TOUCH(filename[1], 0644, NULL);
+
+	SAFE_SETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
+}
+
+static struct tst_test test = {
+	.tid = "listxattr03",
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test = verify_listxattr,
+	.tcnt = ARRAY_SIZE(filename),
+	.setup = setup,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
+#endif
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(),
  2016-11-02 10:59 ` [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(), Dejan Jovicevic
@ 2016-11-02 15:37   ` Cyril Hrubis
  0 siblings, 0 replies; 11+ messages in thread
From: Cyril Hrubis @ 2016-11-02 15:37 UTC (permalink / raw)
  To: ltp

Hi!
> +#define SAFE_SETXATTR(path, name, value, size, flags) \
> +	safe_setxattr(__FILE__, __LINE__, NULL, (path), (name), (value), (size), \
> +				(flags))
> +
> +#define SAFE_LSETXATTR(path, name, value, size, flags) \
> +	safe_lsetxattr(__FILE__, __LINE__, NULL, (path), (name), (value), (size), \
> +				(flags))
> +
> +#define SAFE_FSETXATTR(fd, name, value, size, flags) \
> +	safe_fsetxattr(__FILE__, __LINE__, NULL, (fd), (name), (value), (size), \
> +				(flags))

You are missing the function prototype for the safe_*setxattr()
functions. And since these functions will not be exported to the old
library you should add them here instead of the safe_macros_fn.h header.
And, for the same reason, you don't have to pass NULL as a cleanup
callback to these functions. You can just pass NULL to the tst_brkm() in
the safe_macros.c.

>  #endif /* SAFE_MACROS_H__ */
> diff --git a/lib/safe_macros.c b/lib/safe_macros.c
> index 5a05c84..e1d01b9 100644
> --- a/lib/safe_macros.c
> +++ b/lib/safe_macros.c
> @@ -794,3 +794,69 @@ struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn
>  	errno = err;
>  	return rval;
>  }
> +
> +int safe_setxattr(const char *file, const int lineno, void (cleanup_fn)(void),
> +				const char *path, const char *name, const void *value,
> +				size_t size, int flags)
> +{
> +	int rval;
> +
> +	rval = setxattr(path, name, value, size, flags);
> +
> +	if (rval) {
> +		if (errno == ENOTSUP) {
> +			tst_brkm(TCONF, cleanup_fn,
> +				 "%s:%d: no xattr support in fs or mounted "
> +				 "without user_xattr option", file, lineno);
> +		}
> +
> +		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: setxattr() failed",
> +			     file, lineno);
> +	}
> +
> +	return rval;
> +}
> +
> +int safe_lsetxattr(const char *file, const int lineno, void (cleanup_fn)(void),
> +				const char *path, const char *name, const void *value,
> +				size_t size, int flags)
> +{
> +	int rval;
> +
> +	rval = lsetxattr(path, name, value, size, flags);
> +
> +	if (rval) {
> +		if (errno == ENOTSUP) {
> +			tst_brkm(TCONF, cleanup_fn,
> +				 "%s:%d: no xattr support in fs or mounted "
> +				 "without user_xattr option", file, lineno);
> +		}
> +
> +		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: lsetxattr() failed",
> +			     file, lineno);
> +	}
> +
> +	return rval;
> +}
> +
> +int safe_fsetxattr(const char *file, const int lineno, void (cleanup_fn)(void),
> +				int fd, const char *name, const void *value, size_t size,
> +				int flags)
> +{
> +	int rval;
> +
> +	rval = fsetxattr(fd, name, value, size, flags);
> +
> +	if (rval) {
> +		if (errno == ENOTSUP) {
> +			tst_brkm(TCONF, cleanup_fn,
> +				 "%s:%d: no xattr support in fs or mounted "
> +				 "without user_xattr option", file, lineno);
> +		}
> +
> +		tst_brkm(TBROK | TERRNO, cleanup_fn, "%s:%d: fsetxattr() failed",
> +			     file, lineno);
> +	}
> +
> +	return rval;
> +}

Otherwise these safe macros looks like a good idea to me.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [LTP] [PATCH v3 2/5] llistxattr: moved to using safe macros
  2016-11-02 10:59 ` [LTP] [PATCH v3 2/5] llistxattr: moved to using safe macros Dejan Jovicevic
@ 2016-11-02 15:38   ` Cyril Hrubis
  0 siblings, 0 replies; 11+ messages in thread
From: Cyril Hrubis @ 2016-11-02 15:38 UTC (permalink / raw)
  To: ltp

Hi!
This looks good.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [LTP] [PATCH v3 3/5] llistxattr: improved code readability and
  2016-11-02 10:59 ` [LTP] [PATCH v3 3/5] llistxattr: improved code readability and Dejan Jovicevic
@ 2016-11-02 15:44   ` Cyril Hrubis
  0 siblings, 0 replies; 11+ messages in thread
From: Cyril Hrubis @ 2016-11-02 15:44 UTC (permalink / raw)
  To: ltp

Hi!
>  static void verify_llistxattr(unsigned int n)
> @@ -59,7 +57,7 @@ static void verify_llistxattr(unsigned int n)
>  
>  	TEST(llistxattr(name, NULL, 0));
>  	if (TEST_RETURN == -1) {
> -		tst_res(TFAIL | TERRNO, "llistxattr() failed");
> +		tst_res(TFAIL | TTERRNO, "llistxattr() failed");

The TERRNO -> TTERRNO has been fixed by:

https://github.com/linux-test-project/ltp/commit/00b4cdeed78d43e3f9dca769ef444f68cbb681c1

Please rebase this patch.

>  		return;
>  	}
>  
> @@ -71,8 +69,6 @@ static void verify_llistxattr(unsigned int n)
>  
>  static void setup(void)
>  {
> -	int ret;
> -

This removal should have been part of the prevous patch, right?

>  	SAFE_TOUCH(filename[0], 0644, NULL);
>  
>  	SAFE_TOUCH(filename[1], 0644, NULL);

Otherwise it looks good.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [LTP] [PATCH v3 4/5] llistxattr: new testcase for long path name
  2016-11-02 10:59 ` [LTP] [PATCH v3 4/5] llistxattr: new testcase for long path name Dejan Jovicevic
@ 2016-11-02 15:47   ` Cyril Hrubis
  0 siblings, 0 replies; 11+ messages in thread
From: Cyril Hrubis @ 2016-11-02 15:47 UTC (permalink / raw)
  To: ltp

> +char longpathname[PATH_MAX + 2];

Should be static char.

>  static struct test_case {
>  	const char *path;
>  	size_t size;
> @@ -54,7 +58,8 @@ static struct test_case {
>  } tc[] = {
>  	{SYMLINK, 1, ERANGE},
>  	{"", 20, ENOENT},
> -	{(char *)-1, 20, EFAULT}
> +	{(char *)-1, 20, EFAULT},
> +	{longpathname, 20, ENAMETOOLONG}
>  };
>  
>  static void verify_llistxattr(unsigned int n)
> @@ -85,6 +90,8 @@ static void setup(void)
>  	SAFE_SYMLINK(TESTFILE, SYMLINK);
>  
>  	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
> +
> +	memset(&longpathname, 'a', sizeof(longpathname) - 1);

No need for the & here (in C array is a pointer to a first member and
&array == array).

Otherwise it's fine.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [LTP] [PATCH v3 5/5] flistxattr and listxattr: added tests
  2016-11-02 10:59 ` [LTP] [PATCH v3 5/5] flistxattr and listxattr: added tests Dejan Jovicevic
@ 2016-11-02 16:13   ` Cyril Hrubis
  0 siblings, 0 replies; 11+ messages in thread
From: Cyril Hrubis @ 2016-11-02 16:13 UTC (permalink / raw)
  To: ltp

Hi!
> +#
> +#  Copyright (c) 2016 RT-RK Institute for Computer Based Systems
> +#  Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
> +#
> +#  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.
> +#
> +#  You should have received a copy of the GNU General Public License
> +#  along with this program.
> +#
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/flistxattr/flistxattr01.c b/testcases/kernel/syscalls/flistxattr/flistxattr01.c
> new file mode 100644
> index 0000000..5084f7d
> --- /dev/null
> +++ b/testcases/kernel/syscalls/flistxattr/flistxattr01.c
> @@ -0,0 +1,102 @@
> +/*
> +* Copyright (c) 2016 RT-RK Institute for Computer Based Systems
> +* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.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.

FYI the GPLv2+ (any later version) is slightly prefered (that is the one
in the Makefile above).

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2016-11-02 16:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-02 10:59 [LTP] [PATCH v3 0/5] Listxattr related calls: new tests and testcase Dejan Jovicevic
2016-11-02 10:59 ` [LTP] [PATCH v3 1/5] safe_macros: add safe_setxattr(), Dejan Jovicevic
2016-11-02 15:37   ` Cyril Hrubis
2016-11-02 10:59 ` [LTP] [PATCH v3 2/5] llistxattr: moved to using safe macros Dejan Jovicevic
2016-11-02 15:38   ` Cyril Hrubis
2016-11-02 10:59 ` [LTP] [PATCH v3 3/5] llistxattr: improved code readability and Dejan Jovicevic
2016-11-02 15:44   ` Cyril Hrubis
2016-11-02 10:59 ` [LTP] [PATCH v3 4/5] llistxattr: new testcase for long path name Dejan Jovicevic
2016-11-02 15:47   ` Cyril Hrubis
2016-11-02 10:59 ` [LTP] [PATCH v3 5/5] flistxattr and listxattr: added tests Dejan Jovicevic
2016-11-02 16:13   ` Cyril Hrubis

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.