diff mbox series

[v2,2/2] syscalls/lremovexattr: Add lremovexattr() test

Message ID 20181105002536.29715-2-rafael.tinoco@linaro.org
State Superseded
Headers show
Series [v2,1/2] syscalls/fremovexattr: Add fremovexattr() tests | expand

Commit Message

Rafael David Tinoco Nov. 5, 2018, 12:25 a.m. UTC
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 mbox series

Patch

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