All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/fremovexattr: Add fremovexattr() tests
@ 2018-11-01 22:05 Rafael David Tinoco
  2018-11-04 23:45 ` Rafael David Tinoco
  0 siblings, 1 reply; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-01 22:05 UTC (permalink / raw)
  To: ltp

Fixes: #276

Following the same logic and tests used to test removexattr() syscall,
this commit implements tests for fremovexattr(). It only differs from
removexattr() on the given arguments: using a file descriptor instead of
the filename.

Kernel has different entry points for both, with slightly different
execution paths, mainly related to dealing with the passed file
descriptor.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/fremovexattr/.gitignore   |   2 +
 .../kernel/syscalls/fremovexattr/Makefile     |   8 ++
 .../syscalls/fremovexattr/fremovexattr01.c    | 105 ++++++++++++++
 .../syscalls/fremovexattr/fremovexattr02.c    | 130 ++++++++++++++++++
 5 files changed, 248 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/fremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 2ac9f7665..c48ada707 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -319,6 +319,9 @@ fork14 fork14
 
 fpathconf01 fpathconf01
 
+fremovexattr01 fremovexattr01
+fremovexattr02 fremovexattr02
+
 fstat01 fstat01
 fstat01_64 fstat01_64
 fstat02 fstat02
diff --git a/testcases/kernel/syscalls/fremovexattr/.gitignore b/testcases/kernel/syscalls/fremovexattr/.gitignore
new file mode 100644
index 000000000..455a9db2e
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/.gitignore
@@ -0,0 +1,2 @@
+fremovexattr01
+fremovexattr02
diff --git a/testcases/kernel/syscalls/fremovexattr/Makefile b/testcases/kernel/syscalls/fremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
new file mode 100644
index 000000000..f51a434cc
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr01
+ *
+ * Description:
+ * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
+ * identified by a name, from a file but, instead of using a filename path, it
+ * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
+ * removes, indeed, a previously set attribute key/value from a file.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_TEST_KEY "user.testkey"
+#define XATTR_TEST_VALUE "this is a test value"
+#define XATTR_TEST_VALUE_SIZE 20
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr01testfile"
+
+static int fd = -1;
+static char *got_value;
+
+static void verify_fremovexattr(void)
+{
+	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
+
+	TEST(fremovexattr(fd, XATTR_TEST_KEY));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP) {
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+		return;
+	}
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed with %li",
+				TST_RET);
+		return;
+	}
+
+	memset(got_value, 0, XATTR_TEST_VALUE_SIZE);
+
+	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, XATTR_TEST_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "fremovexattr(2) could not verify removal");
+		return;
+	}
+
+	tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
+}
+
+static void cleanup(void)
+{
+	free(got_value);
+
+	close(fd);
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(FNAME, 0644, NULL);
+
+	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
+
+	got_value = SAFE_MALLOC(XATTR_TEST_VALUE_SIZE);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_fremovexattr,
+	.cleanup = cleanup,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
new file mode 100644
index 000000000..5585b6abe
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr02
+ *
+ * Test cases::
+ * 1) fremovexattr(2) fails if the named attribute does not exist.
+ * 2) fremovexattr(2) fails if file descriptor is not valid.
+ * 3) fremovexattr(2) fails if named attribute has an invalid address.
+ *
+ * Expected Results:
+ * fremovexattr(2) should return -1 and set errno to ENODATA.
+ * fremovexattr(2) should return -1 and set errno to EBADF.
+ * fremovexattr(2) should return -1 and set errno to EFAULT.
+ */
+
+#include "config.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define XATTR_TEST_KEY "user.testkey"
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr02testfile"
+
+static int fd = -1;
+
+struct test_case {
+	int fd;
+	char *key;
+	char *value;
+	char *gotvalue;
+	int size;
+	int exp_ret;
+	int exp_err;
+};
+
+struct test_case tc[] = {
+	{				/* case 1: attribute does not exist */
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = ENODATA,
+	 },
+	{				/* case 2: file descriptor is invalid */
+	 .fd = -1,
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = EBADF,
+	 },
+	{				/* case 3: bad name attribute */
+	 .exp_ret = -1,
+	 .exp_err = EFAULT,
+	},
+};
+
+static void verify_fremovexattr(unsigned int i)
+{
+	TEST(fremovexattr(tc[i].fd, tc[i].key));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+
+	if (tc[i].exp_ret == TST_RET) {
+
+		if (tc[i].exp_err) {
+			if (tc[i].exp_err == TST_ERR) {
+				tst_res(TPASS, "fremovexattr(2) passed");
+				return;
+			}
+		} else  {
+			tst_res(TPASS, "fremovexattr(2) passed");
+			return;
+		}
+	}
+
+	tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	size_t i = 0;
+
+	SAFE_TOUCH(FNAME, 0644, NULL);
+	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
+
+	for (i = 0; i < ARRAY_SIZE(tc); i++) {
+
+		if (tc[i].fd != -1)
+			tc[i].fd = fd;
+
+		if (!tc[i].key && tc[i].exp_err == EFAULT)
+			tc[i].key = tst_get_bad_addr(cleanup);
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_fremovexattr,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tc),
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
-- 
2.19.1


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

* [LTP] [PATCH] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-01 22:05 [LTP] [PATCH] syscalls/fremovexattr: Add fremovexattr() tests Rafael David Tinoco
@ 2018-11-04 23:45 ` Rafael David Tinoco
  2018-11-05  0:25   ` [LTP] [PATCH v2 1/2] " Rafael David Tinoco
  0 siblings, 1 reply; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-04 23:45 UTC (permalink / raw)
  To: ltp

On Thu, Nov 1, 2018 at 7:05 PM, Rafael David Tinoco
<rafael.tinoco@linaro.org> wrote:
> Fixes: #276
>
> Following the same logic and tests used to test removexattr() syscall,
> this commit implements tests for fremovexattr(). It only differs from
> removexattr() on the given arguments: using a file descriptor instead of
> the filename.
>
> Kernel has different entry points for both, with slightly different
> execution paths, mainly related to dealing with the passed file
> descriptor.
>
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>

Sorry, Ignore this one please, sending out a v2 with a small fix PLUS
lremovexattr() test as well, both Fixing: #276. Thanks!

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

* [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-04 23:45 ` Rafael David Tinoco
@ 2018-11-05  0:25   ` Rafael David Tinoco
  2018-11-05  0:25     ` [LTP] [PATCH v2 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
  2018-11-07 15:38     ` [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
  0 siblings, 2 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-05  0:25 UTC (permalink / raw)
  To: ltp

Fixes: #276

Following the same logic and tests used to test removexattr() syscall,
this commit implements tests for fremovexattr(). It only differs from
removexattr() on the given arguments: using a file descriptor instead of
the filename.

Kernel has different entry points for both, with slightly different
execution paths, mainly related to dealing with the passed file
descriptor.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/fremovexattr/.gitignore   |   2 +
 .../kernel/syscalls/fremovexattr/Makefile     |   8 ++
 .../syscalls/fremovexattr/fremovexattr01.c    | 106 ++++++++++++++
 .../syscalls/fremovexattr/fremovexattr02.c    | 130 ++++++++++++++++++
 5 files changed, 249 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/fremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 2ac9f7665..c48ada707 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -319,6 +319,9 @@ fork14 fork14
 
 fpathconf01 fpathconf01
 
+fremovexattr01 fremovexattr01
+fremovexattr02 fremovexattr02
+
 fstat01 fstat01
 fstat01_64 fstat01_64
 fstat02 fstat02
diff --git a/testcases/kernel/syscalls/fremovexattr/.gitignore b/testcases/kernel/syscalls/fremovexattr/.gitignore
new file mode 100644
index 000000000..455a9db2e
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/.gitignore
@@ -0,0 +1,2 @@
+fremovexattr01
+fremovexattr02
diff --git a/testcases/kernel/syscalls/fremovexattr/Makefile b/testcases/kernel/syscalls/fremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
new file mode 100644
index 000000000..543016f01
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr01
+ *
+ * Description:
+ * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
+ * identified by a name, from a file but, instead of using a filename path, it
+ * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
+ * removes, indeed, a previously set attribute key/value from a file.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_TEST_KEY "user.testkey"
+#define XATTR_TEST_VALUE "this is a test value"
+#define XATTR_TEST_VALUE_SIZE 20
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr01testfile"
+
+static int fd = -1;
+static char *got_value;
+
+static void verify_fremovexattr(void)
+{
+	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
+
+	TEST(fremovexattr(fd, XATTR_TEST_KEY));
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+		return;
+	}
+
+	memset(got_value, 0, XATTR_TEST_VALUE_SIZE);
+
+	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, XATTR_TEST_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "fremovexattr(2) could not verify removal");
+		return;
+	}
+
+	tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
+}
+
+static void cleanup(void)
+{
+	free(got_value);
+
+	close(fd);
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(FNAME, 0644, NULL);
+
+	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
+
+	TEST(fremovexattr(fd, XATTR_TEST_KEY));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP) {
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+		return;
+	}
+
+	got_value = SAFE_MALLOC(XATTR_TEST_VALUE_SIZE);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_fremovexattr,
+	.cleanup = cleanup,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
new file mode 100644
index 000000000..5585b6abe
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr02
+ *
+ * Test cases::
+ * 1) fremovexattr(2) fails if the named attribute does not exist.
+ * 2) fremovexattr(2) fails if file descriptor is not valid.
+ * 3) fremovexattr(2) fails if named attribute has an invalid address.
+ *
+ * Expected Results:
+ * fremovexattr(2) should return -1 and set errno to ENODATA.
+ * fremovexattr(2) should return -1 and set errno to EBADF.
+ * fremovexattr(2) should return -1 and set errno to EFAULT.
+ */
+
+#include "config.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define XATTR_TEST_KEY "user.testkey"
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr02testfile"
+
+static int fd = -1;
+
+struct test_case {
+	int fd;
+	char *key;
+	char *value;
+	char *gotvalue;
+	int size;
+	int exp_ret;
+	int exp_err;
+};
+
+struct test_case tc[] = {
+	{				/* case 1: attribute does not exist */
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = ENODATA,
+	 },
+	{				/* case 2: file descriptor is invalid */
+	 .fd = -1,
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = EBADF,
+	 },
+	{				/* case 3: bad name attribute */
+	 .exp_ret = -1,
+	 .exp_err = EFAULT,
+	},
+};
+
+static void verify_fremovexattr(unsigned int i)
+{
+	TEST(fremovexattr(tc[i].fd, tc[i].key));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+
+	if (tc[i].exp_ret == TST_RET) {
+
+		if (tc[i].exp_err) {
+			if (tc[i].exp_err == TST_ERR) {
+				tst_res(TPASS, "fremovexattr(2) passed");
+				return;
+			}
+		} else  {
+			tst_res(TPASS, "fremovexattr(2) passed");
+			return;
+		}
+	}
+
+	tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	size_t i = 0;
+
+	SAFE_TOUCH(FNAME, 0644, NULL);
+	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
+
+	for (i = 0; i < ARRAY_SIZE(tc); i++) {
+
+		if (tc[i].fd != -1)
+			tc[i].fd = fd;
+
+		if (!tc[i].key && tc[i].exp_err == EFAULT)
+			tc[i].key = tst_get_bad_addr(cleanup);
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_fremovexattr,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tc),
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
-- 
2.19.1


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

* [LTP] [PATCH v2 2/2] syscalls/lremovexattr: Add lremovexattr() test
  2018-11-05  0:25   ` [LTP] [PATCH v2 1/2] " Rafael David Tinoco
@ 2018-11-05  0:25     ` Rafael David Tinoco
  2018-11-07 15:38     ` [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
  1 sibling, 0 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-05  0:25 UTC (permalink / raw)
  To: ltp

Fixes: #276

This commit implements a test for lremovexattr(). According to attr(5),
extended attributes are interpreted differently among files, directories
and symbolic links. User attributes are only allowed for regular files
and directories, thus the need to test security.* attributes being
removed from symbolic links.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   2 +
 .../kernel/syscalls/lremovexattr/.gitignore   |   1 +
 .../kernel/syscalls/lremovexattr/Makefile     |   8 +
 .../syscalls/lremovexattr/lremovexattr01.c    | 140 ++++++++++++++++++
 4 files changed, 151 insertions(+)
 create mode 100644 testcases/kernel/syscalls/lremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/lremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/lremovexattr/lremovexattr01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index c48ada707..77a36b1f5 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -581,6 +581,8 @@ llseek01 llseek01
 llseek02 llseek02
 llseek03 llseek03
 
+lremovexattr01 lremovexattr01
+
 lseek01 lseek01
 lseek02 lseek02
 lseek07 lseek07
diff --git a/testcases/kernel/syscalls/lremovexattr/.gitignore b/testcases/kernel/syscalls/lremovexattr/.gitignore
new file mode 100644
index 000000000..810f86214
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/.gitignore
@@ -0,0 +1 @@
+lremovexattr01
diff --git a/testcases/kernel/syscalls/lremovexattr/Makefile b/testcases/kernel/syscalls/lremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
new file mode 100644
index 000000000..3084cd318
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: lremovexattr01
+ *
+ * Description:
+ * lremovexattr(2) removes the extended attribute identified by a name and
+ * associated with a given path in the filesystem. Unlike removexattr(2),
+ * lremovexattr(2) removes the attribute from the symbolic link only, and not
+ * the file. This test verifies that a simple call to lremovexattr(2) removes,
+ * indeed, a previously set attribute key/value from a symbolic link, and the
+ * symbolic link _only_.
+ *
+ * Note:
+ * According to attr(5), extended attributes are interpreted differently from
+ * regular files, directories and symbolic links. User attributes are only
+ * allowed for regular files and directories, thus the need of using security.*
+ * attributes for this test.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_KEY		"security.key1"
+#define XATTR_VALUE		"file and link"
+#define XATTR_VALUE_SIZE	13
+
+#define MNTPOINT "mntpoint"
+#define FILENAME MNTPOINT"/lremovexattr01testfile"
+#define SYMLINK  MNTPOINT"/lremovexattr01symlink"
+
+static char *got_value;
+
+static void verify_lremovexattr(void)
+{
+	/* set attribute on both: file and symlink */
+
+	SAFE_SETXATTR(FILENAME, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	SAFE_LSETXATTR(SYMLINK, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	/* remove attribute from symlink only */
+
+	TEST(lremovexattr(SYMLINK, XATTR_KEY));
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "lremovexattr(2) failed");
+		return;
+	}
+
+	/* make sure attribute is gone from symlink */
+
+	memset(got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(lgetxattr(SYMLINK, XATTR_KEY, got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "lremovexattr(2) did not work");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "lgetxattr(2) failed unexpectedly");
+		return;
+	}
+
+	/* check if file is unchanged, like it should be */
+
+	memset(got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(getxattr(FILENAME, XATTR_KEY, got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET <= 0) {
+		tst_res(TFAIL, "lremovexattr(2) deleted file attribute");
+		return;
+	}
+
+	if (strcmp(got_value, XATTR_VALUE)) {
+		tst_res(TFAIL, "lremovexattr(2) changed file attribute");
+		return;
+	}
+
+	/* cleanup file attribute for iteration */
+
+	SAFE_REMOVEXATTR(FILENAME, XATTR_KEY);
+
+	tst_res(TPASS, "lremovexattr(2) removed attribute as expected");
+}
+
+static void cleanup(void)
+{
+	free(got_value);
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(FILENAME, 0644, NULL);
+
+	if (symlink(FILENAME, SYMLINK) < 0) {
+		tst_brk(TCONF, "symlink() not supported");
+		return;
+	}
+
+	got_value = SAFE_MALLOC(XATTR_VALUE_SIZE);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_lremovexattr,
+	.cleanup = cleanup,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
-- 
2.19.1


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

* [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-05  0:25   ` [LTP] [PATCH v2 1/2] " Rafael David Tinoco
  2018-11-05  0:25     ` [LTP] [PATCH v2 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
@ 2018-11-07 15:38     ` Cyril Hrubis
  2018-11-07 15:50       ` Rafael David Tinoco
  2018-11-08 18:33       ` [LTP] [PATCH v3 " Rafael David Tinoco
  1 sibling, 2 replies; 12+ messages in thread
From: Cyril Hrubis @ 2018-11-07 15:38 UTC (permalink / raw)
  To: ltp

Hi!
> +/*
> + * Test Name: fremovexattr01
> + *
> + * Description:
> + * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
> + * identified by a name, from a file but, instead of using a filename path, it
> + * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
> + * removes, indeed, a previously set attribute key/value from a file.
> + */
> +
> +#include "config.h"
> +#include <errno.h>
> +#include <stdlib.h>
> +
> +#ifdef HAVE_SYS_XATTR_H
> +# include <sys/xattr.h>
> +#endif
> +
> +#include "tst_test.h"
> +
> +#ifdef HAVE_SYS_XATTR_H
> +
> +#define ENOATTR ENODATA
> +
> +#define XATTR_TEST_KEY "user.testkey"
> +#define XATTR_TEST_VALUE "this is a test value"
> +#define XATTR_TEST_VALUE_SIZE 20
> +
> +#define MNTPOINT "mntpoint"
> +#define FNAME MNTPOINT"/fremovexattr01testfile"
> +
> +static int fd = -1;
> +static char *got_value;

Why not just static char got_value[XATTR_TEST_VALUE_SIZE]?

> +static void verify_fremovexattr(void)
> +{
> +	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
> +			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
> +
> +	TEST(fremovexattr(fd, XATTR_TEST_KEY));
> +
> +	if (TST_RET != 0) {
> +		tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
> +		return;
> +	}
> +
> +	memset(got_value, 0, XATTR_TEST_VALUE_SIZE);
> +
> +	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, XATTR_TEST_VALUE_SIZE));
> +
> +	if (TST_RET >= 0) {
> +		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
> +		return;
> +	}
> +
> +	if (TST_RET < 0 && TST_ERR != ENOATTR) {
> +		tst_brk(TBROK, "fremovexattr(2) could not verify removal");
> +		return;
> +	}
> +
> +	tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
> +}
> +
> +static void cleanup(void)
> +{
> +	free(got_value);
> +
> +	close(fd);
        ^
	if (fd > 0)
		SAFE_CLOSE(fd);

	would be slightly better here as it would emit
	warning if the close() has failed.

> +}
> +
> +static void setup(void)
> +{
> +	SAFE_TOUCH(FNAME, 0644, NULL);
> +
> +	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
                                      ^
				      open() is a variable argument
				      function and the third argument is
				      an integer but only in a case that
				      we passed O_CREAT in the flags, if
				      we are not creating the file
				      it should be omitted

So if we want to create the file, we don't have to call the SAFE_TOUCH()
but pass O_CREAT in the flags and pass the mode as third argument here
as:

	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);


> +	TEST(fremovexattr(fd, XATTR_TEST_KEY));
> +
> +	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP) {
> +		tst_brk(TCONF, "fremovexattr(2) not supported");
> +		return;
                ^
		No need for the return here.

> +	}
> +
> +	got_value = SAFE_MALLOC(XATTR_TEST_VALUE_SIZE);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test_all = verify_fremovexattr,
> +	.cleanup = cleanup,
> +	.mntpoint = MNTPOINT,
> +	.mount_device = 1,
> +	.all_filesystems = 1,
> +	.needs_tmpdir = 1,
> +	.needs_root = 1,
> +};
> +
> +#else /* HAVE_SYS_XATTR_H */
> +TST_TEST_TCONF("<sys/xattr.h> does not exist");
> +#endif
> diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
> new file mode 100644
> index 000000000..5585b6abe
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
> @@ -0,0 +1,130 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Test Name: fremovexattr02
> + *
> + * Test cases::
> + * 1) fremovexattr(2) fails if the named attribute does not exist.
> + * 2) fremovexattr(2) fails if file descriptor is not valid.
> + * 3) fremovexattr(2) fails if named attribute has an invalid address.
> + *
> + * Expected Results:
> + * fremovexattr(2) should return -1 and set errno to ENODATA.
> + * fremovexattr(2) should return -1 and set errno to EBADF.
> + * fremovexattr(2) should return -1 and set errno to EFAULT.
> + */
> +
> +#include "config.h"
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +
> +#ifdef HAVE_SYS_XATTR_H
> +# include <sys/xattr.h>
> +#endif
> +
> +#include "tst_test.h"
> +
> +#ifdef HAVE_SYS_XATTR_H
> +
> +#define XATTR_TEST_KEY "user.testkey"
> +
> +#define MNTPOINT "mntpoint"
> +#define FNAME MNTPOINT"/fremovexattr02testfile"
> +
> +static int fd = -1;
> +
> +struct test_case {
> +	int fd;
> +	char *key;
> +	char *value;
> +	char *gotvalue;
> +	int size;
> +	int exp_ret;
> +	int exp_err;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 1: attribute does not exist */
> +	 .key = XATTR_TEST_KEY,
> +	 .exp_ret = -1,
> +	 .exp_err = ENODATA,
> +	 },
> +	{				/* case 2: file descriptor is invalid */
> +	 .fd = -1,
> +	 .key = XATTR_TEST_KEY,
> +	 .exp_ret = -1,
> +	 .exp_err = EBADF,
> +	 },
> +	{				/* case 3: bad name attribute */
> +	 .exp_ret = -1,
> +	 .exp_err = EFAULT,
> +	},
> +};
> +
> +static void verify_fremovexattr(unsigned int i)
> +{
> +	TEST(fremovexattr(tc[i].fd, tc[i].key));
> +
> +	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
> +		tst_brk(TCONF, "fremovexattr(2) not supported");
> +
> +	if (tc[i].exp_ret == TST_RET) {
> +
> +		if (tc[i].exp_err) {
> +			if (tc[i].exp_err == TST_ERR) {
> +				tst_res(TPASS, "fremovexattr(2) passed");
> +				return;
> +			}
> +		} else  {
> +			tst_res(TPASS, "fremovexattr(2) passed");
> +			return;
> +		}
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
> +}
> +
> +static void cleanup(void)
> +{
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
> +}
> +
> +static void setup(void)
> +{
> +	size_t i = 0;
> +
> +	SAFE_TOUCH(FNAME, 0644, NULL);
> +	fd = SAFE_OPEN(FNAME, O_RDWR, NULL);

Here as well no need for the SAFE_TOUCH().

> +	for (i = 0; i < ARRAY_SIZE(tc); i++) {
> +
> +		if (tc[i].fd != -1)
> +			tc[i].fd = fd;
> +
> +		if (!tc[i].key && tc[i].exp_err == EFAULT)
> +			tc[i].key = tst_get_bad_addr(cleanup);
> +	}
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test = verify_fremovexattr,
> +	.cleanup = cleanup,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.mntpoint = MNTPOINT,
> +	.mount_device = 1,
> +	.all_filesystems = 1,
> +	.needs_tmpdir = 1,
> +	.needs_root = 1,
> +};

I'm wondering if we need the all_filesystem here, I guess that the
ENODATA test will reach down to the filesystem driver, so it's probably
relevant here.

> +#else /* HAVE_SYS_XATTR_H */
> +TST_TEST_TCONF("<sys/xattr.h> does not exist");
> +#endif
> -- 
> 2.19.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-07 15:38     ` [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
@ 2018-11-07 15:50       ` Rafael David Tinoco
  2018-11-08 18:33       ` [LTP] [PATCH v3 " Rafael David Tinoco
  1 sibling, 0 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-07 15:50 UTC (permalink / raw)
  To: ltp

>> +static int fd = -1;
>> +static char *got_value;
>
> Why not just static char got_value[XATTR_TEST_VALUE_SIZE]?

Of course =o) !! Will do.

>> +static void cleanup(void)
>> +{
>> +     free(got_value);
>> +
>> +     close(fd);
>         ^
>         if (fd > 0)
>                 SAFE_CLOSE(fd);

Missed this.

>
>         would be slightly better here as it would emit
>         warning if the close() has failed.
>
>> +}
>> +
>> +static void setup(void)
>> +{
>> +     SAFE_TOUCH(FNAME, 0644, NULL);
>> +
>> +     fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
>                                       ^
>                                       open() is a variable argument
>                                       function and the third argument is
>                                       an integer but only in a case that
>                                       we passed O_CREAT in the flags, if
>                                       we are not creating the file
>                                       it should be omitted

Sure! Auto mode, sorry.


>> +++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
>> ...
>> +     SAFE_TOUCH(FNAME, 0644, NULL);
>> +     fd = SAFE_OPEN(FNAME, O_RDWR, NULL);
>
> Here as well no need for the SAFE_TOUCH().

Same.

>> +static struct tst_test test = {
>> +     .setup = setup,
>> +     .test = verify_fremovexattr,
>> +     .cleanup = cleanup,
>> +     .tcnt = ARRAY_SIZE(tc),
>> +     .mntpoint = MNTPOINT,
>> +     .mount_device = 1,
>> +     .all_filesystems = 1,
>> +     .needs_tmpdir = 1,
>> +     .needs_root = 1,
>> +};
>
> I'm wondering if we need the all_filesystem here, I guess that the
> ENODATA test will reach down to the filesystem driver, so it's probably
> relevant here.

Yep.. that is what I though.

Tks a lot Cyril, will send a v3.

o/

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

* [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-07 15:38     ` [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
  2018-11-07 15:50       ` Rafael David Tinoco
@ 2018-11-08 18:33       ` Rafael David Tinoco
  2018-11-08 18:33         ` [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
  2018-11-15 16:33         ` [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
  1 sibling, 2 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-08 18:33 UTC (permalink / raw)
  To: ltp

Fixes: #276

Following the same logic and tests used to test removexattr() syscall,
this commit implements tests for fremovexattr(). It only differs from
removexattr() on the given arguments: using a file descriptor instead of
the filename.

Kernel has different entry points for both, with slightly different
execution paths, mainly related to dealing with the passed file
descriptor.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/fremovexattr/.gitignore   |   2 +
 .../kernel/syscalls/fremovexattr/Makefile     |   8 ++
 .../syscalls/fremovexattr/fremovexattr01.c    |  99 ++++++++++++++
 .../syscalls/fremovexattr/fremovexattr02.c    | 126 ++++++++++++++++++
 5 files changed, 238 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/fremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
 create mode 100644 testcases/kernel/syscalls/fremovexattr/fremovexattr02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 95bb47b96..24110b623 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -319,6 +319,9 @@ fork14 fork14
 
 fpathconf01 fpathconf01
 
+fremovexattr01 fremovexattr01
+fremovexattr02 fremovexattr02
+
 fstat01 fstat01
 fstat01_64 fstat01_64
 fstat02 fstat02
diff --git a/testcases/kernel/syscalls/fremovexattr/.gitignore b/testcases/kernel/syscalls/fremovexattr/.gitignore
new file mode 100644
index 000000000..455a9db2e
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/.gitignore
@@ -0,0 +1,2 @@
+fremovexattr01
+fremovexattr02
diff --git a/testcases/kernel/syscalls/fremovexattr/Makefile b/testcases/kernel/syscalls/fremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
new file mode 100644
index 000000000..96b92fb24
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr01
+ *
+ * Description:
+ * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
+ * identified by a name, from a file but, instead of using a filename path, it
+ * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
+ * removes, indeed, a previously set attribute key/value from a file.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_TEST_KEY "user.testkey"
+#define XATTR_TEST_VALUE "this is a test value"
+#define XATTR_TEST_VALUE_SIZE 20
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr01testfile"
+
+static int fd = -1;
+static char got_value[XATTR_TEST_VALUE_SIZE];
+
+static void verify_fremovexattr(void)
+{
+	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
+
+	TEST(fremovexattr(fd, XATTR_TEST_KEY));
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+		return;
+	}
+
+	memset(&got_value, 0, XATTR_TEST_VALUE_SIZE);
+
+	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, XATTR_TEST_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "fremovexattr(2) could not verify removal");
+		return;
+	}
+
+	tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);
+
+	TEST(fremovexattr(fd, XATTR_TEST_KEY));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_fremovexattr,
+	.cleanup = cleanup,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
new file mode 100644
index 000000000..8eb1e9a99
--- /dev/null
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: fremovexattr02
+ *
+ * Test cases::
+ * 1) fremovexattr(2) fails if the named attribute does not exist.
+ * 2) fremovexattr(2) fails if file descriptor is not valid.
+ * 3) fremovexattr(2) fails if named attribute has an invalid address.
+ *
+ * Expected Results:
+ * fremovexattr(2) should return -1 and set errno to ENODATA.
+ * fremovexattr(2) should return -1 and set errno to EBADF.
+ * fremovexattr(2) should return -1 and set errno to EFAULT.
+ */
+
+#include "config.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define XATTR_TEST_KEY "user.testkey"
+
+#define MNTPOINT "mntpoint"
+#define FNAME MNTPOINT"/fremovexattr02testfile"
+
+static int fd = -1;
+
+struct test_case {
+	int fd;
+	char *key;
+	int exp_ret;
+	int exp_err;
+};
+
+struct test_case tc[] = {
+	{				/* case 1: attribute does not exist */
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = ENODATA,
+	 },
+	{				/* case 2: file descriptor is invalid */
+	 .fd = -1,
+	 .key = XATTR_TEST_KEY,
+	 .exp_ret = -1,
+	 .exp_err = EBADF,
+	 },
+	{				/* case 3: bad name attribute */
+	 .exp_ret = -1,
+	 .exp_err = EFAULT,
+	},
+};
+
+static void verify_fremovexattr(unsigned int i)
+{
+	TEST(fremovexattr(tc[i].fd, tc[i].key));
+
+	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
+		tst_brk(TCONF, "fremovexattr(2) not supported");
+
+	if (tc[i].exp_ret == TST_RET) {
+
+		if (tc[i].exp_err) {
+			if (tc[i].exp_err == TST_ERR) {
+				tst_res(TPASS, "fremovexattr(2) passed");
+				return;
+			}
+		} else  {
+			tst_res(TPASS, "fremovexattr(2) passed");
+			return;
+		}
+	}
+
+	tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	size_t i = 0;
+
+	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);
+
+	for (i = 0; i < ARRAY_SIZE(tc); i++) {
+
+		if (tc[i].fd != -1)
+			tc[i].fd = fd;
+
+		if (!tc[i].key && tc[i].exp_err == EFAULT)
+			tc[i].key = tst_get_bad_addr(cleanup);
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_fremovexattr,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tc),
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
-- 
2.19.1


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

* [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test
  2018-11-08 18:33       ` [LTP] [PATCH v3 " Rafael David Tinoco
@ 2018-11-08 18:33         ` Rafael David Tinoco
  2018-11-16 19:37           ` Cyril Hrubis
  2018-11-15 16:33         ` [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
  1 sibling, 1 reply; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-08 18:33 UTC (permalink / raw)
  To: ltp

Fixes: #276

This commit implements a test for lremovexattr(). According to attr(5),
extended attributes are interpreted differently among files, directories
and symbolic links. User attributes are only allowed for regular files
and directories, thus the need to test security.* attributes being
removed from symbolic links.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   2 +
 .../kernel/syscalls/lremovexattr/.gitignore   |   1 +
 .../kernel/syscalls/lremovexattr/Makefile     |   8 ++
 .../syscalls/lremovexattr/lremovexattr01.c    | 132 ++++++++++++++++++
 4 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/syscalls/lremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/lremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/lremovexattr/lremovexattr01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 24110b623..26c142bf8 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -582,6 +582,8 @@ llseek01 llseek01
 llseek02 llseek02
 llseek03 llseek03
 
+lremovexattr01 lremovexattr01
+
 lseek01 lseek01
 lseek02 lseek02
 lseek07 lseek07
diff --git a/testcases/kernel/syscalls/lremovexattr/.gitignore b/testcases/kernel/syscalls/lremovexattr/.gitignore
new file mode 100644
index 000000000..810f86214
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/.gitignore
@@ -0,0 +1 @@
+lremovexattr01
diff --git a/testcases/kernel/syscalls/lremovexattr/Makefile b/testcases/kernel/syscalls/lremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
new file mode 100644
index 000000000..2cf46ebdf
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: lremovexattr01
+ *
+ * Description:
+ * lremovexattr(2) removes the extended attribute identified by a name and
+ * associated with a given path in the filesystem. Unlike removexattr(2),
+ * lremovexattr(2) removes the attribute from the symbolic link only, and not
+ * the file. This test verifies that a simple call to lremovexattr(2) removes,
+ * indeed, a previously set attribute key/value from a symbolic link, and the
+ * symbolic link _only_.
+ *
+ * Note:
+ * According to attr(5), extended attributes are interpreted differently from
+ * regular files, directories and symbolic links. User attributes are only
+ * allowed for regular files and directories, thus the need of using security.*
+ * attributes for this test.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_KEY		"security.key1"
+#define XATTR_VALUE		"file and link"
+#define XATTR_VALUE_SIZE	13
+
+#define MNTPOINT "mntpoint"
+#define FILENAME MNTPOINT"/lremovexattr01testfile"
+#define SYMLINK  MNTPOINT"/lremovexattr01symlink"
+
+static char got_value[XATTR_VALUE_SIZE];
+
+static void verify_lremovexattr(void)
+{
+	/* set attribute on both: file and symlink */
+
+	SAFE_SETXATTR(FILENAME, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	SAFE_LSETXATTR(SYMLINK, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	/* remove attribute from symlink only */
+
+	TEST(lremovexattr(SYMLINK, XATTR_KEY));
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "lremovexattr(2) failed");
+		return;
+	}
+
+	/* make sure attribute is gone from symlink */
+
+	memset(&got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(lgetxattr(SYMLINK, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "lremovexattr(2) did not work");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "lgetxattr(2) failed unexpectedly");
+		return;
+	}
+
+	/* check if file is unchanged, like it should be */
+
+	memset(&got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(getxattr(FILENAME, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET <= 0) {
+		tst_res(TFAIL, "lremovexattr(2) deleted file attribute");
+		return;
+	}
+
+	if (strcmp(got_value, XATTR_VALUE)) {
+		tst_res(TFAIL, "lremovexattr(2) changed file attribute");
+		return;
+	}
+
+	/* cleanup file attribute for iteration */
+
+	SAFE_REMOVEXATTR(FILENAME, XATTR_KEY);
+
+	tst_res(TPASS, "lremovexattr(2) removed attribute as expected");
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(FILENAME, 0644, NULL);
+
+	if (symlink(FILENAME, SYMLINK) < 0) {
+		tst_brk(TCONF, "symlink() not supported");
+		return;
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_lremovexattr,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif
-- 
2.19.1


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

* [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-08 18:33       ` [LTP] [PATCH v3 " Rafael David Tinoco
  2018-11-08 18:33         ` [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
@ 2018-11-15 16:33         ` Cyril Hrubis
  2018-11-16  0:33           ` Rafael David Tinoco
  1 sibling, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2018-11-15 16:33 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with minor simplification, mainly removing unreachable branches
from the second test, thanks.

Diff:

diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
index 96b92fb24..a3ea1aa2b 100644
--- a/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr01.c
@@ -50,9 +50,7 @@ static void verify_fremovexattr(void)
 		return;
 	}
 
-	memset(&got_value, 0, XATTR_TEST_VALUE_SIZE);
-
-	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, XATTR_TEST_VALUE_SIZE));
+	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, sizeof(got_value)));
 
 	if (TST_RET >= 0) {
 		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
@@ -60,7 +58,8 @@ static void verify_fremovexattr(void)
 	}
 
 	if (TST_RET < 0 && TST_ERR != ENOATTR) {
-		tst_brk(TBROK, "fremovexattr(2) could not verify removal");
+		tst_brk(TBROK | TTERRNO,
+			"fremovexattr(2) could not verify removal");
 		return;
 	}
 
diff --git a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
index 8eb1e9a99..534179eae 100644
--- a/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
+++ b/testcases/kernel/syscalls/fremovexattr/fremovexattr02.c
@@ -42,24 +42,20 @@ static int fd = -1;
 struct test_case {
 	int fd;
 	char *key;
-	int exp_ret;
 	int exp_err;
 };
 
 struct test_case tc[] = {
 	{				/* case 1: attribute does not exist */
 	 .key = XATTR_TEST_KEY,
-	 .exp_ret = -1,
 	 .exp_err = ENODATA,
 	 },
 	{				/* case 2: file descriptor is invalid */
 	 .fd = -1,
 	 .key = XATTR_TEST_KEY,
-	 .exp_ret = -1,
 	 .exp_err = EBADF,
 	 },
 	{				/* case 3: bad name attribute */
-	 .exp_ret = -1,
 	 .exp_err = EFAULT,
 	},
 };
@@ -71,20 +67,19 @@ static void verify_fremovexattr(unsigned int i)
 	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
 		tst_brk(TCONF, "fremovexattr(2) not supported");
 
-	if (tc[i].exp_ret == TST_RET) {
-
-		if (tc[i].exp_err) {
-			if (tc[i].exp_err == TST_ERR) {
-				tst_res(TPASS, "fremovexattr(2) passed");
-				return;
-			}
-		} else  {
-			tst_res(TPASS, "fremovexattr(2) passed");
-			return;
+	if (TST_RET == -1) {
+		if (tc[i].exp_err == TST_ERR) {
+			tst_res(TPASS | TTERRNO,
+				"fremovexattr(2) failed expectedly");
+		} else {
+			tst_res(TFAIL | TTERRNO,
+				"fremovexattr(2) should fail with %s",
+				tst_strerrno(tc[i].exp_err));
 		}
+		return;
 	}
 
-	tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
+	tst_res(TFAIL, "fremovexattr(2) returned %li", TST_RET);
 }

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests
  2018-11-15 16:33         ` [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
@ 2018-11-16  0:33           ` Rafael David Tinoco
  0 siblings, 0 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-16  0:33 UTC (permalink / raw)
  To: ltp

On 11/15/18 2:33 PM, Cyril Hrubis wrote:
> Hi!
> Pushed with minor simplification, mainly removing unreachable branches
> from the second test, thanks.
> 

Thanks a lot Cyril!

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

* [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test
  2018-11-08 18:33         ` [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
@ 2018-11-16 19:37           ` Cyril Hrubis
  2018-11-18 13:37             ` Rafael David Tinoco
  0 siblings, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2018-11-16 19:37 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with minor changes, see below, thanks.

diff --git a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
index 2cf46ebdf..26194f114 100644
--- a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
+++ b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
@@ -18,7 +18,7 @@
  * Note:
  * According to attr(5), extended attributes are interpreted differently from
  * regular files, directories and symbolic links. User attributes are only
- * allowed for regular files and directories, thus the need of using security.*
+ * allowed for regular files and directories, thus the need of using trusted.*
  * attributes for this test.
  */
 
@@ -39,7 +39,7 @@
 
 #define ENOATTR ENODATA
 
-#define XATTR_KEY		"security.key1"
+#define XATTR_KEY		"trusted.key1"
 #define XATTR_VALUE		"file and link"
 #define XATTR_VALUE_SIZE	13
 
@@ -79,8 +79,8 @@ static void verify_lremovexattr(void)
 		return;
 	}
 
-	if (TST_RET < 0 && TST_ERR != ENOATTR) {
-		tst_brk(TBROK, "lgetxattr(2) failed unexpectedly");
+	if (TST_ERR != ENOATTR) {
+		tst_brk(TBROK | TTERRNO, "lgetxattr(2) failed unexpectedly");
 		return;
 	}
 
@@ -111,10 +111,8 @@ static void setup(void)
 {
 	SAFE_TOUCH(FILENAME, 0644, NULL);
 
-	if (symlink(FILENAME, SYMLINK) < 0) {
+	if (symlink(FILENAME, SYMLINK) < 0)
 		tst_brk(TCONF, "symlink() not supported");
-		return;
-	}
 }
 
 static struct tst_test test = {



I've found that the trusted attributes works as well and cannot be
disabled in kernel (at least for ext3 security attributes have their own
CONFIG switches).

The rest is just removed unreachable or always true code.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test
  2018-11-16 19:37           ` Cyril Hrubis
@ 2018-11-18 13:37             ` Rafael David Tinoco
  0 siblings, 0 replies; 12+ messages in thread
From: Rafael David Tinoco @ 2018-11-18 13:37 UTC (permalink / raw)
  To: ltp

On 11/16/18 5:37 PM, Cyril Hrubis wrote:
> Hi!
> Pushed with minor changes, see below, thanks.

Thanks a lot Cyril!

Best,
-- 
Rafael D. Tinoco
Linaro Kernel Validation

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

end of thread, other threads:[~2018-11-18 13:37 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-01 22:05 [LTP] [PATCH] syscalls/fremovexattr: Add fremovexattr() tests Rafael David Tinoco
2018-11-04 23:45 ` Rafael David Tinoco
2018-11-05  0:25   ` [LTP] [PATCH v2 1/2] " Rafael David Tinoco
2018-11-05  0:25     ` [LTP] [PATCH v2 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
2018-11-07 15:38     ` [LTP] [PATCH v2 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
2018-11-07 15:50       ` Rafael David Tinoco
2018-11-08 18:33       ` [LTP] [PATCH v3 " Rafael David Tinoco
2018-11-08 18:33         ` [LTP] [PATCH v3 2/2] syscalls/lremovexattr: Add lremovexattr() test Rafael David Tinoco
2018-11-16 19:37           ` Cyril Hrubis
2018-11-18 13:37             ` Rafael David Tinoco
2018-11-15 16:33         ` [LTP] [PATCH v3 1/2] syscalls/fremovexattr: Add fremovexattr() tests Cyril Hrubis
2018-11-16  0:33           ` Rafael David Tinoco

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.