Home | History | Annotate | Download | only in llistxattr
      1 /*
      2 * Copyright (c) 2016 Fujitsu Ltd.
      3 * Author: Xiao Yang <yangx.jy (at) cn.fujitsu.com>
      4 *
      5 * This program is free software; you can redistribute it and/or modify it
      6 * under the terms of version 2 of the GNU General Public License as
      7 * published by the Free Software Foundation.
      8 *
      9 * This program is distributed in the hope that it would be useful, but
     10 * WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12 *
     13 * You should have received a copy of the GNU General Public License
     14 * alone with this program.
     15 */
     16 
     17 /*
     18 * Test Name: llistxattr01
     19 *
     20 * Description:
     21 * The testcase checks the basic functionality of the llistxattr(2).
     22 * llistxattr(2) retrieves the list of extended attribute names
     23 * associated with the link itself in the filesystem.
     24 *
     25 */
     26 
     27 #include "config.h"
     28 #include <errno.h>
     29 #include <sys/types.h>
     30 #include <string.h>
     31 
     32 #ifdef HAVE_ATTR_XATTR_H
     33 # include <attr/xattr.h>
     34 #endif
     35 
     36 #include "tst_test.h"
     37 
     38 #ifdef HAVE_ATTR_XATTR_H
     39 
     40 #define SECURITY_KEY1	"security.ltptest1"
     41 #define SECURITY_KEY2	"security.ltptest2"
     42 #define VALUE	"test"
     43 #define VALUE_SIZE	4
     44 #define KEY_SIZE    17
     45 
     46 static void set_xattr(const char *path, const char *key)
     47 {
     48 	int n;
     49 
     50 	n = lsetxattr(path, key, VALUE, VALUE_SIZE, XATTR_CREATE);
     51 	if (n == -1) {
     52 		if (errno == ENOTSUP) {
     53 			tst_brk(TCONF,
     54 				 "no xattr support in fs or mounted "
     55 				 "without user_xattr option");
     56 		}
     57 
     58 		if (errno == EEXIST) {
     59 			tst_brk(TBROK, "exist attribute %s", key);
     60 		} else {
     61 			tst_brk(TBROK | TERRNO,
     62 				 "lsetxattr() failed");
     63 		}
     64 	}
     65 }
     66 
     67 static int has_attribute(const char *list, int llen, const char *attr)
     68 {
     69 	int i;
     70 
     71 	for (i = 0; i < llen; i += strlen(list + i) + 1) {
     72 		if (!strcmp(list + i, attr))
     73 			return 1;
     74 	}
     75 	return 0;
     76 }
     77 
     78 static void verify_llistxattr(void)
     79 {
     80 	int size = 64;
     81 	char buf[size];
     82 
     83 	TEST(llistxattr("symlink", buf, size));
     84 	if (TEST_RETURN == -1) {
     85 		tst_res(TFAIL | TERRNO, "llistxattr() failed");
     86 		return;
     87 	}
     88 
     89 	if (has_attribute(buf, size, SECURITY_KEY1)) {
     90 		tst_res(TFAIL, "get file attribute %s unexpectlly",
     91 			 SECURITY_KEY1);
     92 		return;
     93 	}
     94 
     95 	if (!has_attribute(buf, size, SECURITY_KEY2)) {
     96 		tst_res(TFAIL, "missing attribute %s", SECURITY_KEY2);
     97 		return;
     98 	}
     99 
    100 	tst_res(TPASS, "llistxattr() succeeded");
    101 }
    102 
    103 static void setup(void)
    104 {
    105 	SAFE_TOUCH("testfile", 0644, NULL);
    106 
    107 	SAFE_SYMLINK("testfile", "symlink");
    108 
    109 	set_xattr("testfile", SECURITY_KEY1);
    110 
    111 	set_xattr("symlink", SECURITY_KEY2);
    112 }
    113 
    114 static struct tst_test test = {
    115 	.tid = "llistxattr01",
    116 	.needs_tmpdir = 1,
    117 	.needs_root = 1,
    118 	.test_all = verify_llistxattr,
    119 	.setup = setup,
    120 };
    121 
    122 #else
    123 	TST_TEST_TCONF("<attr/xattr.h> does not exist.");
    124 #endif /* HAVE_ATTR_XATTR_H */
    125 
    126