[LTP] [PATCH v2 03/10] setxattr01: add setxattrat variant

Andrea Cervesato
2025-10-07T06:49:04Z

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/syscalls/setxattr/setxattr01.c | 61 +++++++++++++++++++------
1 file changed, 47 insertions(+), 14 deletions(-)

diff --git a/testcases/kernel/syscalls/setxattr/setxattr01.c b/testcases/kernel/syscalls/setxattr/setxattr01.c
index a405bf42e..ade405f37 100644
--- a/testcases/kernel/syscalls/setxattr/setxattr01.c
+++ b/testcases/kernel/syscalls/setxattr/setxattr01.c
@@ -20,6 +20,10 @@
*/

#include "config.h"
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
@@ -30,12 +34,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef HAVE_SYS_XATTR_H
-# include <sys/xattr.h>
-#endif
-#include "tst_test.h"
+#include "lapi/xattr.h"
+#include <sys/xattr.h>

-#ifdef HAVE_SYS_XATTR_H
#define XATTR_NAME_MAX 255
#define XATTR_NAME_LEN (XATTR_NAME_MAX + 2)
#define XATTR_SIZE_MAX 65536
@@ -43,11 +44,13 @@
#define XATTR_TEST_VALUE "this is a test value"
#define XATTR_TEST_VALUE_SIZE 20
#define MNTPOINT "mntpoint"
-#define FNAME MNTPOINT"/setxattr01testfile"
+#define FNAME_REL "setxattr01testfile"
+#define FNAME MNTPOINT"/"FNAME_REL

static char long_key[XATTR_NAME_LEN];
static char *long_value;
static char *xattr_value = XATTR_TEST_VALUE;
+static int mnt_fd = -1;

struct test_case {
char *key;
@@ -126,44 +129,64 @@ struct test_case tc[] = {

static void verify_setxattr(unsigned int i)
{
+ char *sysname;
+
/* some tests might require existing keys for each iteration */
if (tc[i].keyneeded) {
SAFE_SETXATTR(FNAME, tc[i].key, *tc[i].value, tc[i].size,
XATTR_CREATE);
}

- TEST(setxattr(FNAME, tc[i].key, *tc[i].value, tc[i].size, tc[i].flags));
+ if (tst_variant) {
+ sysname = "setxattrat";
+
+ struct xattr_args args = {
+ .value = (uint64_t)tc[i].value,
+ .size = tc[i].size,
+ .flags = tc[i].flags,
+ };
+
+ TEST(setxattrat(mnt_fd, FNAME_REL, AT_SYMLINK_NOFOLLOW,
+ tc[i].key, &args, sizeof(args)));
+ } else {
+ sysname = "setxattr";
+
+ TEST(setxattr(
+ FNAME,
+ tc[i].key, *tc[i].value, tc[i].size,
+ tc[i].flags));
+ }

if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
- tst_brk(TCONF, "setxattr(2) not supported");
+ tst_brk(TCONF, "%s(2) not supported", sysname);

/* success */

if (!tc[i].exp_err) {
if (TST_RET) {
tst_res(TFAIL | TTERRNO,
- "setxattr(2) failed with %li", TST_RET);
+ "%s(2) failed with %li", sysname, TST_RET);
return;
}

/* this is needed for subsequent iterations */
SAFE_REMOVEXATTR(FNAME, tc[i].key);

- tst_res(TPASS, "setxattr(2) passed");
+ tst_res(TPASS, "%s(2) passed", sysname);

return;
}

if (TST_RET == 0) {
- tst_res(TFAIL, "setxattr(2) passed unexpectedly");
+ tst_res(TFAIL, "%s(2) passed unexpectedly", sysname);
return;
}

/* error */

if (tc[i].exp_err != TST_ERR) {
- tst_res(TFAIL | TTERRNO, "setxattr(2) should fail with %s",
- tst_strerrno(tc[i].exp_err));
+ tst_res(TFAIL | TTERRNO, "%s(2) should fail with %s",
+ sysname, tst_strerrno(tc[i].exp_err));
return;
}

@@ -171,7 +194,7 @@ static void verify_setxattr(unsigned int i)
if (tc[i].keyneeded)
SAFE_REMOVEXATTR(FNAME, tc[i].key);

- tst_res(TPASS | TTERRNO, "setxattr(2) failed");
+ tst_res(TPASS | TTERRNO, "%s(2) failed", sysname);
}

static void setup(void)
@@ -192,12 +215,22 @@ static void setup(void)
if (!tc[i].key)
tc[i].key = tst_get_bad_addr(NULL);
}
+
+ mnt_fd = SAFE_OPEN(MNTPOINT, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+ if (mnt_fd != -1)
+ SAFE_CLOSE(mnt_fd);
}

static struct tst_test test = {
.setup = setup,
+ .cleanup = cleanup,
.test = verify_setxattr,
.tcnt = ARRAY_SIZE(tc),
+ .test_variants = 2,
.mntpoint = MNTPOINT,
.mount_device = 1,
.all_filesystems = 1,

--
2.51.0