1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3* Copyright (c) 2016 RT-RK Institute for Computer Based Systems 4* Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com> 5*/ 6 7/* 8* Test Name: listxattr03 9* 10* Description: 11* An empty buffer of size zero can return the current size of the list 12* of extended attribute names, which can be used to estimate a suitable buffer. 13*/ 14 15#include "config.h" 16#include <errno.h> 17#include <sys/types.h> 18 19#ifdef HAVE_SYS_XATTR_H 20# include <sys/xattr.h> 21#endif 22 23#include "tst_test.h" 24 25#ifdef HAVE_SYS_XATTR_H 26 27#define SECURITY_KEY "security.ltptest" 28#define VALUE "test" 29#define VALUE_SIZE (sizeof(VALUE) - 1) 30 31static const char * const filename[] = {"testfile1", "testfile2"}; 32 33static int check_suitable_buf(const char *name, long size) 34{ 35 int n; 36 char buf[size]; 37 38 n = listxattr(name, buf, sizeof(buf)); 39 40 return n != -1; 41} 42 43static void verify_listxattr(unsigned int n) 44{ 45 const char *name = filename[n]; 46 47 TEST(listxattr(name, NULL, 0)); 48 if (TST_RET == -1) { 49 tst_res(TFAIL | TTERRNO, "listxattr() failed"); 50 return; 51 } 52 53 if (check_suitable_buf(name, TST_RET)) 54 tst_res(TPASS, "listxattr() succeed with suitable buffer"); 55 else 56 tst_res(TFAIL, "listxattr() failed with small buffer"); 57} 58 59static void setup(void) 60{ 61 SAFE_TOUCH(filename[0], 0644, NULL); 62 63 SAFE_TOUCH(filename[1], 0644, NULL); 64 65 SAFE_SETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE); 66} 67 68static struct tst_test test = { 69 .needs_tmpdir = 1, 70 .needs_root = 1, 71 .test = verify_listxattr, 72 .tcnt = ARRAY_SIZE(filename), 73 .setup = setup, 74}; 75 76#else /* HAVE_SYS_XATTR_H */ 77 TST_TEST_TCONF("<sys/xattr.h> does not exist."); 78#endif 79