1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Linaro Limited. All rights reserved. 4f08c3bdfSopenharmony_ci * Author: Rafael David Tinoco <rafael.tinoco@linaro.org> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * In the user.* namespace, only regular files and directories can 9f08c3bdfSopenharmony_ci * have extended attributes. Otherwise fgetxattr(2) will return -1 10f08c3bdfSopenharmony_ci * and set proper errno. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * There are 7 test cases: 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * 1. Get attribute from a regular file: 15f08c3bdfSopenharmony_ci * - fgetxattr(2) should succeed 16f08c3bdfSopenharmony_ci * - checks returned value to be the same as we set 17f08c3bdfSopenharmony_ci * 2. Get attribute from a directory: 18f08c3bdfSopenharmony_ci * - fgetxattr(2) should succeed 19f08c3bdfSopenharmony_ci * - checks returned value to be the same as we set 20f08c3bdfSopenharmony_ci * 3. Get attribute from a symlink which points to the regular file: 21f08c3bdfSopenharmony_ci * - fgetxattr(2) should succeed 22f08c3bdfSopenharmony_ci * - checks returned value to be the same as we set 23f08c3bdfSopenharmony_ci * 4. Get attribute from a FIFO: 24f08c3bdfSopenharmony_ci * - fgetxattr(2) should return -1 and set errno to ENODATA 25f08c3bdfSopenharmony_ci * 5. Get attribute from a char special file: 26f08c3bdfSopenharmony_ci * - fgetxattr(2) should return -1 and set errno to ENODATA 27f08c3bdfSopenharmony_ci * 6. Get attribute from a block special file: 28f08c3bdfSopenharmony_ci * - fgetxattr(2) should return -1 and set errno to ENODATA 29f08c3bdfSopenharmony_ci * 7. Get attribute from a UNIX domain socket: 30f08c3bdfSopenharmony_ci * - fgetxattr(2) should return -1 and set errno to ENODATA 31f08c3bdfSopenharmony_ci */ 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#include "config.h" 34f08c3bdfSopenharmony_ci#include <sys/types.h> 35f08c3bdfSopenharmony_ci#include <sys/stat.h> 36f08c3bdfSopenharmony_ci#include <sys/sysmacros.h> 37f08c3bdfSopenharmony_ci#include <sys/wait.h> 38f08c3bdfSopenharmony_ci#include <errno.h> 39f08c3bdfSopenharmony_ci#include <fcntl.h> 40f08c3bdfSopenharmony_ci#include <unistd.h> 41f08c3bdfSopenharmony_ci#include <signal.h> 42f08c3bdfSopenharmony_ci#include <stdio.h> 43f08c3bdfSopenharmony_ci#include <stdlib.h> 44f08c3bdfSopenharmony_ci#include <string.h> 45f08c3bdfSopenharmony_ci#include <sys/socket.h> 46f08c3bdfSopenharmony_ci#include <sys/un.h> 47f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H 48f08c3bdfSopenharmony_ci# include <sys/xattr.h> 49f08c3bdfSopenharmony_ci#endif 50f08c3bdfSopenharmony_ci#include "tst_test.h" 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H 53f08c3bdfSopenharmony_ci#define XATTR_TEST_KEY "user.testkey" 54f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE "this is a test value" 55f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE_SIZE 20 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci#define MNTPOINT "mntpoint" 58f08c3bdfSopenharmony_ci#define OFFSET 11 59f08c3bdfSopenharmony_ci#define FILENAME "fgetxattr02testfile" 60f08c3bdfSopenharmony_ci#define DIRNAME "fgetxattr02testdir" 61f08c3bdfSopenharmony_ci#define SYMLINK "fgetxattr02symlink" 62f08c3bdfSopenharmony_ci#define SYMLINKF "fgetxattr02symlinkfile" 63f08c3bdfSopenharmony_ci#define FIFO MNTPOINT"/fgetxattr02fifo" 64f08c3bdfSopenharmony_ci#define CHR MNTPOINT"/fgetxattr02chr" 65f08c3bdfSopenharmony_ci#define BLK MNTPOINT"/fgetxattr02blk" 66f08c3bdfSopenharmony_ci#define SOCK "fgetxattr02sock" 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistruct test_case { 69f08c3bdfSopenharmony_ci char *fname; 70f08c3bdfSopenharmony_ci int fd; 71f08c3bdfSopenharmony_ci int fflags; 72f08c3bdfSopenharmony_ci char *key; 73f08c3bdfSopenharmony_ci char *value; 74f08c3bdfSopenharmony_ci size_t size; 75f08c3bdfSopenharmony_ci char *ret_value; 76f08c3bdfSopenharmony_ci int flags; 77f08c3bdfSopenharmony_ci int exp_err; 78f08c3bdfSopenharmony_ci int exp_ret; 79f08c3bdfSopenharmony_ci int issocket; 80f08c3bdfSopenharmony_ci}; 81f08c3bdfSopenharmony_cistatic struct test_case tc[] = { 82f08c3bdfSopenharmony_ci { /* case 00, get attr from reg */ 83f08c3bdfSopenharmony_ci .fname = FILENAME, 84f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 85f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 86f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 87f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 88f08c3bdfSopenharmony_ci .ret_value = NULL, 89f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 90f08c3bdfSopenharmony_ci .exp_err = 0, 91f08c3bdfSopenharmony_ci .exp_ret = XATTR_TEST_VALUE_SIZE, 92f08c3bdfSopenharmony_ci }, 93f08c3bdfSopenharmony_ci { /* case 01, get attr from dir */ 94f08c3bdfSopenharmony_ci .fname = DIRNAME, 95f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 96f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 97f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 98f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 99f08c3bdfSopenharmony_ci .ret_value = NULL, 100f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 101f08c3bdfSopenharmony_ci .exp_err = 0, 102f08c3bdfSopenharmony_ci .exp_ret = XATTR_TEST_VALUE_SIZE, 103f08c3bdfSopenharmony_ci }, 104f08c3bdfSopenharmony_ci { /* case 02, get attr from symlink */ 105f08c3bdfSopenharmony_ci .fname = SYMLINK, 106f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 107f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 108f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 109f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 110f08c3bdfSopenharmony_ci .ret_value = NULL, 111f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 112f08c3bdfSopenharmony_ci .exp_err = 0, 113f08c3bdfSopenharmony_ci .exp_ret = XATTR_TEST_VALUE_SIZE, 114f08c3bdfSopenharmony_ci }, 115f08c3bdfSopenharmony_ci { /* case 03, get attr from fifo */ 116f08c3bdfSopenharmony_ci .fname = FIFO, 117f08c3bdfSopenharmony_ci .fflags = (O_RDONLY | O_NONBLOCK), 118f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 119f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 120f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 121f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 122f08c3bdfSopenharmony_ci .exp_err = ENODATA, 123f08c3bdfSopenharmony_ci .exp_ret = -1, 124f08c3bdfSopenharmony_ci }, 125f08c3bdfSopenharmony_ci { /* case 04, get attr from character special */ 126f08c3bdfSopenharmony_ci .fname = CHR, 127f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 128f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 129f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 130f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 131f08c3bdfSopenharmony_ci .ret_value = NULL, 132f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 133f08c3bdfSopenharmony_ci .exp_err = ENODATA, 134f08c3bdfSopenharmony_ci .exp_ret = -1, 135f08c3bdfSopenharmony_ci }, 136f08c3bdfSopenharmony_ci { /* case 05, get attr from block special */ 137f08c3bdfSopenharmony_ci .fname = BLK, 138f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 139f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 140f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 141f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 142f08c3bdfSopenharmony_ci .ret_value = NULL, 143f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 144f08c3bdfSopenharmony_ci .exp_err = ENODATA, 145f08c3bdfSopenharmony_ci .exp_ret = -1, 146f08c3bdfSopenharmony_ci }, 147f08c3bdfSopenharmony_ci { /* case 06, get attr from socket */ 148f08c3bdfSopenharmony_ci .fname = SOCK, 149f08c3bdfSopenharmony_ci .fflags = O_RDONLY, 150f08c3bdfSopenharmony_ci .key = XATTR_TEST_KEY, 151f08c3bdfSopenharmony_ci .value = XATTR_TEST_VALUE, 152f08c3bdfSopenharmony_ci .size = XATTR_TEST_VALUE_SIZE, 153f08c3bdfSopenharmony_ci .ret_value = NULL, 154f08c3bdfSopenharmony_ci .flags = XATTR_CREATE, 155f08c3bdfSopenharmony_ci .exp_err = ENODATA, 156f08c3bdfSopenharmony_ci .exp_ret = -1, 157f08c3bdfSopenharmony_ci .issocket = 1, 158f08c3bdfSopenharmony_ci }, 159f08c3bdfSopenharmony_ci}; 160f08c3bdfSopenharmony_ci 161f08c3bdfSopenharmony_cistatic void verify_fgetxattr(unsigned int i) 162f08c3bdfSopenharmony_ci{ 163f08c3bdfSopenharmony_ci const char *fname = strstr(tc[i].fname, "fgetxattr02") + OFFSET; 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci TEST(fgetxattr(tc[i].fd, tc[i].key, tc[i].ret_value, tc[i].size)); 166f08c3bdfSopenharmony_ci 167f08c3bdfSopenharmony_ci if (TST_RET == -1 && TST_ERR == EOPNOTSUPP) 168f08c3bdfSopenharmony_ci tst_brk(TCONF, "fgetxattr(2) not supported"); 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_ci if (TST_RET >= 0) { 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci if (tc[i].exp_ret == TST_RET) { 173f08c3bdfSopenharmony_ci tst_res(TPASS, "fgetxattr(2) on %s passed", 174f08c3bdfSopenharmony_ci fname); 175f08c3bdfSopenharmony_ci } else { 176f08c3bdfSopenharmony_ci tst_res(TFAIL, 177f08c3bdfSopenharmony_ci "fgetxattr(2) on %s passed unexpectedly %ld", 178f08c3bdfSopenharmony_ci fname, TST_RET); 179f08c3bdfSopenharmony_ci } 180f08c3bdfSopenharmony_ci 181f08c3bdfSopenharmony_ci if (strncmp(tc[i].ret_value, XATTR_TEST_VALUE, 182f08c3bdfSopenharmony_ci XATTR_TEST_VALUE_SIZE)) { 183f08c3bdfSopenharmony_ci tst_res(TFAIL, "wrong value, expect \"%s\" got \"%s\"", 184f08c3bdfSopenharmony_ci XATTR_TEST_VALUE, tc[i].ret_value); 185f08c3bdfSopenharmony_ci } 186f08c3bdfSopenharmony_ci 187f08c3bdfSopenharmony_ci tst_res(TPASS, "fgetxattr(2) on %s got the right value", 188f08c3bdfSopenharmony_ci fname); 189f08c3bdfSopenharmony_ci } 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_ci if (tc[i].exp_err == TST_ERR) { 192f08c3bdfSopenharmony_ci tst_res(TPASS | TTERRNO, "fgetxattr(2) on %s passed", 193f08c3bdfSopenharmony_ci fname); 194f08c3bdfSopenharmony_ci return; 195f08c3bdfSopenharmony_ci } 196f08c3bdfSopenharmony_ci 197f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "fgetxattr(2) failed on %s", fname); 198f08c3bdfSopenharmony_ci} 199f08c3bdfSopenharmony_ci 200f08c3bdfSopenharmony_cistatic void setup(void) 201f08c3bdfSopenharmony_ci{ 202f08c3bdfSopenharmony_ci size_t i = 0; 203f08c3bdfSopenharmony_ci struct sockaddr_un sun; 204f08c3bdfSopenharmony_ci 205f08c3bdfSopenharmony_ci dev_t chr_dev = makedev(1, 3); 206f08c3bdfSopenharmony_ci dev_t blk_dev = makedev(7, 3); 207f08c3bdfSopenharmony_ci 208f08c3bdfSopenharmony_ci SAFE_TOUCH(FILENAME, 0644, NULL); 209f08c3bdfSopenharmony_ci SAFE_TOUCH(SYMLINKF, 0644, NULL); 210f08c3bdfSopenharmony_ci SAFE_MKDIR(DIRNAME, 0644); 211f08c3bdfSopenharmony_ci SAFE_SYMLINK(SYMLINKF, SYMLINK); 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci /* root: mknod(2) needs it to create something other than a file */ 214f08c3bdfSopenharmony_ci SAFE_MKNOD(FIFO, S_IFIFO | 0777, 0); 215f08c3bdfSopenharmony_ci SAFE_MKNOD(CHR, S_IFCHR | 0777, chr_dev); 216f08c3bdfSopenharmony_ci SAFE_MKNOD(BLK, S_IFBLK | 0777, blk_dev); 217f08c3bdfSopenharmony_ci 218f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(tc); i++) { 219f08c3bdfSopenharmony_ci 220f08c3bdfSopenharmony_ci tc[i].ret_value = SAFE_MALLOC(tc[i].size); 221f08c3bdfSopenharmony_ci memset(tc[i].ret_value, 0, tc[i].size); 222f08c3bdfSopenharmony_ci 223f08c3bdfSopenharmony_ci if (tc[i].issocket) { 224f08c3bdfSopenharmony_ci /* differently than getxattr(2) calls, when dealing with 225f08c3bdfSopenharmony_ci * sockets, mknod(2) isn't enough to test fgetxattr(2). 226f08c3bdfSopenharmony_ci * we have to get a real unix socket in order for 227f08c3bdfSopenharmony_ci * open(2) to get a file desc. 228f08c3bdfSopenharmony_ci */ 229f08c3bdfSopenharmony_ci tc[i].fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0); 230f08c3bdfSopenharmony_ci 231f08c3bdfSopenharmony_ci memset(&sun, 0, sizeof(struct sockaddr_un)); 232f08c3bdfSopenharmony_ci sun.sun_family = AF_UNIX; 233f08c3bdfSopenharmony_ci strncpy(sun.sun_path, tc[i].fname, 234f08c3bdfSopenharmony_ci sizeof(sun.sun_path) - 1); 235f08c3bdfSopenharmony_ci 236f08c3bdfSopenharmony_ci SAFE_BIND(tc[i].fd, (const struct sockaddr *) &sun, 237f08c3bdfSopenharmony_ci sizeof(struct sockaddr_un)); 238f08c3bdfSopenharmony_ci } else { 239f08c3bdfSopenharmony_ci tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags); 240f08c3bdfSopenharmony_ci } 241f08c3bdfSopenharmony_ci 242f08c3bdfSopenharmony_ci if (tc[i].exp_ret >= 0) { 243f08c3bdfSopenharmony_ci SAFE_FSETXATTR(tc[i].fd, tc[i].key, tc[i].value, 244f08c3bdfSopenharmony_ci tc[i].size, tc[i].flags); 245f08c3bdfSopenharmony_ci } 246f08c3bdfSopenharmony_ci } 247f08c3bdfSopenharmony_ci} 248f08c3bdfSopenharmony_ci 249f08c3bdfSopenharmony_cistatic void cleanup(void) 250f08c3bdfSopenharmony_ci{ 251f08c3bdfSopenharmony_ci size_t i = 0; 252f08c3bdfSopenharmony_ci 253f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(tc); i++) { 254f08c3bdfSopenharmony_ci free(tc[i].ret_value); 255f08c3bdfSopenharmony_ci 256f08c3bdfSopenharmony_ci if (tc[i].fd > 0) 257f08c3bdfSopenharmony_ci SAFE_CLOSE(tc[i].fd); 258f08c3bdfSopenharmony_ci } 259f08c3bdfSopenharmony_ci} 260f08c3bdfSopenharmony_ci 261f08c3bdfSopenharmony_cistatic struct tst_test test = { 262f08c3bdfSopenharmony_ci .setup = setup, 263f08c3bdfSopenharmony_ci .test = verify_fgetxattr, 264f08c3bdfSopenharmony_ci .cleanup = cleanup, 265f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tc), 266f08c3bdfSopenharmony_ci .needs_devfs = 1, 267f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 268f08c3bdfSopenharmony_ci .needs_root = 1, 269f08c3bdfSopenharmony_ci}; 270f08c3bdfSopenharmony_ci 271f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */ 272f08c3bdfSopenharmony_ciTST_TEST_TCONF("<sys/xattr.h> does not exist"); 273f08c3bdfSopenharmony_ci#endif 274