Home | History | Annotate | Download | only in fremovexattr
      1 // SPDX-License-Identifier: GPL-2.0-or-later
      2 /*
      3  * Copyright (c) 2018 Linaro Limited. All rights reserved.
      4  * Author: Rafael David Tinoco <rafael.tinoco (at) linaro.org>
      5  */
      6 
      7 /*
      8  * Test Name: fremovexattr01
      9  *
     10  * Description:
     11  * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
     12  * identified by a name, from a file but, instead of using a filename path, it
     13  * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
     14  * removes, indeed, a previously set attribute key/value from a file.
     15  */
     16 
     17 #include "config.h"
     18 #include <errno.h>
     19 #include <stdlib.h>
     20 
     21 #ifdef HAVE_SYS_XATTR_H
     22 # include <sys/xattr.h>
     23 #endif
     24 
     25 #include "tst_test.h"
     26 
     27 #ifdef HAVE_SYS_XATTR_H
     28 
     29 #define ENOATTR ENODATA
     30 
     31 #define XATTR_TEST_KEY "user.testkey"
     32 #define XATTR_TEST_VALUE "this is a test value"
     33 #define XATTR_TEST_VALUE_SIZE 20
     34 
     35 #define MNTPOINT "mntpoint"
     36 #define FNAME MNTPOINT"/fremovexattr01testfile"
     37 
     38 static int fd = -1;
     39 static char got_value[XATTR_TEST_VALUE_SIZE];
     40 
     41 static void verify_fremovexattr(void)
     42 {
     43 	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
     44 			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
     45 
     46 	TEST(fremovexattr(fd, XATTR_TEST_KEY));
     47 
     48 	if (TST_RET != 0) {
     49 		tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
     50 		return;
     51 	}
     52 
     53 	TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, sizeof(got_value)));
     54 
     55 	if (TST_RET >= 0) {
     56 		tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
     57 		return;
     58 	}
     59 
     60 	if (TST_RET < 0 && TST_ERR != ENOATTR) {
     61 		tst_brk(TBROK | TTERRNO,
     62 			"fremovexattr(2) could not verify removal");
     63 		return;
     64 	}
     65 
     66 	tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
     67 }
     68 
     69 static void cleanup(void)
     70 {
     71 	if (fd > 0)
     72 		SAFE_CLOSE(fd);
     73 }
     74 
     75 static void setup(void)
     76 {
     77 	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);
     78 
     79 	TEST(fremovexattr(fd, XATTR_TEST_KEY));
     80 
     81 	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
     82 		tst_brk(TCONF, "fremovexattr(2) not supported");
     83 }
     84 
     85 static struct tst_test test = {
     86 	.setup = setup,
     87 	.test_all = verify_fremovexattr,
     88 	.cleanup = cleanup,
     89 	.mntpoint = MNTPOINT,
     90 	.mount_device = 1,
     91 	.all_filesystems = 1,
     92 	.needs_tmpdir = 1,
     93 	.needs_root = 1,
     94 };
     95 
     96 #else /* HAVE_SYS_XATTR_H */
     97 TST_TEST_TCONF("<sys/xattr.h> does not exist");
     98 #endif
     99