Home | History | Annotate | Download | only in listxattr
      1 /*
      2 *  Copyright (c) 2016 RT-RK Institute for Computer Based Systems
      3 *  Author: Dejan Jovicevic <dejan.jovicevic (at) rt-rk.com>
      4 *
      5 *  This program is free software;  you can redistribute it and/or modify
      6 *  it under the terms of the GNU General Public License as published by
      7 *  the Free Software Foundation; either version 2 of the License, or
      8 *  (at your option) any later version.
      9 *
     10 *  This program is distributed in the hope that it will be useful,
     11 *  but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13 *  the GNU General Public License for more details.
     14 *
     15 *  You should have received a copy of the GNU General Public License
     16 *  along with this program.
     17 */
     18 
     19 /*
     20 * Test Name: listxattr01
     21 *
     22 * Description:
     23 * The testcase checks the basic functionality of the listxattr(2).
     24 * listxattr(2) retrieves the list of extended attribute names
     25 * associated with the file itself in the filesystem.
     26 *
     27 */
     28 
     29 #include "config.h"
     30 #include <errno.h>
     31 #include <sys/types.h>
     32 #include <string.h>
     33 
     34 #ifdef HAVE_SYS_XATTR_H
     35 # include <sys/xattr.h>
     36 #endif
     37 
     38 #include "tst_test.h"
     39 
     40 #ifdef HAVE_SYS_XATTR_H
     41 
     42 #define SECURITY_KEY1	"security.ltptest1"
     43 #define VALUE	"test"
     44 #define VALUE_SIZE	(sizeof(VALUE) - 1)
     45 #define KEY_SIZE    (sizeof(SECURITY_KEY1) - 1)
     46 #define TESTFILE    "testfile"
     47 
     48 static int has_attribute(const char *list, int llen, const char *attr)
     49 {
     50 	int i;
     51 
     52 	for (i = 0; i < llen; i += strlen(list + i) + 1) {
     53 		if (!strcmp(list + i, attr))
     54 			return 1;
     55 	}
     56 	return 0;
     57 }
     58 
     59 static void verify_listxattr(void)
     60 {
     61 	char buf[64];
     62 
     63 	TEST(listxattr(TESTFILE, buf, sizeof(buf)));
     64 	if (TEST_RETURN == -1) {
     65 		tst_res(TFAIL | TTERRNO, "listxattr() failed");
     66 		return;
     67 	}
     68 
     69 	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
     70 		tst_res(TFAIL, "missing attribute %s",
     71 			 SECURITY_KEY1);
     72 		return;
     73 	}
     74 
     75 	tst_res(TPASS, "listxattr() succeeded");
     76 }
     77 
     78 static void setup(void)
     79 {
     80 	SAFE_TOUCH(TESTFILE, 0644, NULL);
     81 
     82 	SAFE_SETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
     83 }
     84 
     85 static struct tst_test test = {
     86 	.tid = "listxattr01",
     87 	.needs_tmpdir = 1,
     88 	.needs_root = 1,
     89 	.test_all = verify_listxattr,
     90 	.setup = setup,
     91 };
     92 
     93 #else
     94 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
     95 #endif /* HAVE_SYS_XATTR_H */
     96