1 1.3 chs /* $NetBSD: t_extattr.c,v 1.3 2022/11/17 06:40:40 chs Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by Christos Zoulas. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos #include <sys/cdefs.h> 32 1.3 chs __RCSID("$NetBSD: t_extattr.c,v 1.3 2022/11/17 06:40:40 chs Exp $"); 33 1.1 christos 34 1.1 christos #include <sys/types.h> 35 1.1 christos #include <sys/mount.h> 36 1.1 christos #include <sys/extattr.h> 37 1.1 christos 38 1.1 christos #include <atf-c.h> 39 1.1 christos #include <errno.h> 40 1.1 christos #include <fcntl.h> 41 1.1 christos #include <pthread.h> 42 1.1 christos #include <stdio.h> 43 1.1 christos #include <stdlib.h> 44 1.1 christos #include <unistd.h> 45 1.1 christos #include <string.h> 46 1.1 christos 47 1.1 christos #include <rump/rump.h> 48 1.1 christos #include <rump/rump_syscalls.h> 49 1.1 christos 50 1.1 christos #include <ufs/ufs/ufsmount.h> 51 1.1 christos 52 1.1 christos #include "h_macros.h" 53 1.1 christos 54 1.1 christos #define IMGNAME "extattr.img" 55 1.1 christos 56 1.1 christos #define G "/garage" 57 1.1 christos #define M "mercedes" 58 1.1 christos #define C "carburator" 59 1.1 christos #define E "engine" 60 1.1 christos #define T "trunk" 61 1.1 christos #define S "suitcase" 62 1.1 christos 63 1.1 christos static const char *attrs[] = { E, T }; 64 1.1 christos 65 1.1 christos static void 66 1.1 christos check_list(const char *buf, ssize_t nr) 67 1.1 christos { 68 1.1 christos size_t p = 0; 69 1.1 christos for (size_t i = 0; i < __arraycount(attrs); i++) { 70 1.1 christos size_t l = buf[p++]; 71 1.1 christos if (l != strlen(attrs[i])) 72 1.1 christos atf_tc_fail("got invalid len %zu expected %zu", l, 73 1.1 christos strlen(attrs[i])); 74 1.1 christos if (strncmp(&buf[p], attrs[i], l) != 0) 75 1.1 christos atf_tc_fail("got invalid str %.*s expected %s", (int)l, 76 1.1 christos &buf[p], attrs[i]); 77 1.1 christos p += l; 78 1.1 christos } 79 1.1 christos } 80 1.1 christos 81 1.3 chs // Make it ffsv2 with extattrs 82 1.3 chs const char *newfs = "newfs -O 2ea -F -s 10000 " IMGNAME; 83 1.1 christos #define FAKEBLK "/dev/formula1" 84 1.1 christos 85 1.2 christos static void 86 1.2 christos start(void) { 87 1.1 christos struct ufs_args args; 88 1.1 christos 89 1.1 christos if (system(newfs) == -1) 90 1.1 christos atf_tc_fail_errno("newfs failed"); 91 1.1 christos 92 1.1 christos memset(&args, 0, sizeof(args)); 93 1.1 christos args.fspec = __UNCONST(FAKEBLK); 94 1.1 christos 95 1.1 christos rump_init(); 96 1.1 christos if (rump_sys_mkdir(G, 0777) == -1) 97 1.1 christos atf_tc_fail_errno("cannot create mountpoint"); 98 1.1 christos rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK); 99 1.1 christos if (rump_sys_mount(MOUNT_FFS, G, 0, &args, sizeof(args))==-1) 100 1.1 christos atf_tc_fail_errno("rump_sys_mount failed"); 101 1.1 christos 102 1.1 christos /* create extattr */ 103 1.1 christos if (rump_sys_chdir(G) == 1) 104 1.1 christos atf_tc_fail_errno("chdir"); 105 1.2 christos } 106 1.2 christos 107 1.2 christos static void 108 1.2 christos finish(void) { 109 1.2 christos if (rump_sys_chdir("/") == 1) 110 1.2 christos atf_tc_fail_errno("chdir"); 111 1.2 christos if (rump_sys_unmount(G, 0) == -1) 112 1.2 christos atf_tc_fail_errno("unmount failed"); 113 1.2 christos } 114 1.2 christos 115 1.2 christos 116 1.2 christos ATF_TC_WITH_CLEANUP(extattr_simple); 117 1.2 christos ATF_TC_HEAD(extattr_simple, tc) 118 1.2 christos { 119 1.2 christos atf_tc_set_md_var(tc, "descr", "test extended attribute creation" 120 1.2 christos " and removal ffsv2"); 121 1.2 christos atf_tc_set_md_var(tc, "timeout", "5"); 122 1.2 christos } 123 1.2 christos 124 1.2 christos ATF_TC_BODY(extattr_simple, tc) 125 1.2 christos { 126 1.2 christos ssize_t nr; 127 1.2 christos int fd; 128 1.2 christos char buf[512]; 129 1.2 christos 130 1.2 christos start(); 131 1.2 christos 132 1.1 christos if ((fd = rump_sys_open(M, O_RDWR | O_CREAT, 0600)) == -1) 133 1.1 christos atf_tc_fail_errno("open"); 134 1.1 christos if (rump_sys_write(fd, "hi mom\n", 7) != 7) 135 1.1 christos atf_tc_fail_errno("write"); 136 1.1 christos 137 1.1 christos if (rump_sys_extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, E, C, 11) == -1) 138 1.1 christos atf_tc_fail_errno("exattr_set_fd"); 139 1.1 christos if ((nr = rump_sys_extattr_get_file(M, EXTATTR_NAMESPACE_USER, 140 1.1 christos E, buf, sizeof(buf))) != 11) 141 1.1 christos atf_tc_fail_errno("extattr_get_file"); 142 1.1 christos if (strcmp(buf, C) != 0) 143 1.1 christos atf_tc_fail("got invalid str %s expected %s", buf, C); 144 1.1 christos 145 1.1 christos if (rump_sys_extattr_set_file(M, EXTATTR_NAMESPACE_USER, T, S, 9) == -1) 146 1.1 christos atf_tc_fail_errno("extattr_set_file"); 147 1.1 christos if ((nr = rump_sys_extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, 148 1.1 christos T, buf, sizeof(buf))) != 9) 149 1.1 christos atf_tc_fail_errno("extattr_get_fd"); 150 1.1 christos if (strcmp(buf, S) != 0) 151 1.1 christos atf_tc_fail("got invalid str %s expected %s", buf, S); 152 1.1 christos 153 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, 154 1.1 christos buf, sizeof(buf))) != 13) 155 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr); 156 1.1 christos check_list(buf, __arraycount(attrs)); 157 1.1 christos 158 1.1 christos if ((nr = rump_sys_extattr_list_file(M, EXTATTR_NAMESPACE_USER, 159 1.1 christos buf, sizeof(buf))) != 13) 160 1.1 christos atf_tc_fail_errno("extattr_list_file %zd", nr); 161 1.1 christos check_list(buf, __arraycount(attrs)); 162 1.1 christos 163 1.1 christos if (rump_sys_extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, T) == -1) 164 1.1 christos atf_tc_fail_errno("extattr_delete_fd"); 165 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, 166 1.1 christos buf, sizeof(buf))) != 7) 167 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr); 168 1.1 christos check_list(buf, 1); 169 1.1 christos 170 1.1 christos if (rump_sys_extattr_delete_file(M, EXTATTR_NAMESPACE_USER, E) == -1) 171 1.1 christos atf_tc_fail_errno("extattr_delete_file"); 172 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, 173 1.1 christos buf, sizeof(buf))) != 0) 174 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr); 175 1.1 christos 176 1.1 christos if (rump_sys_close(fd) == -1) 177 1.1 christos atf_tc_fail_errno("close"); 178 1.1 christos if (rump_sys_unlink(M) == -1) 179 1.1 christos atf_tc_fail_errno("unlink"); 180 1.2 christos 181 1.2 christos finish(); 182 1.2 christos } 183 1.2 christos 184 1.2 christos ATF_TC_CLEANUP(extattr_simple, tc) 185 1.2 christos { 186 1.2 christos 187 1.2 christos unlink(IMGNAME); 188 1.2 christos } 189 1.2 christos 190 1.2 christos ATF_TC_WITH_CLEANUP(extattr_create_unlink); 191 1.2 christos ATF_TC_HEAD(extattr_create_unlink, tc) 192 1.2 christos { 193 1.2 christos atf_tc_set_md_var(tc, "descr", "test extended attribute creation" 194 1.2 christos " and unlinking file with ACLs"); 195 1.2 christos atf_tc_set_md_var(tc, "timeout", "5"); 196 1.2 christos } 197 1.2 christos 198 1.2 christos ATF_TC_BODY(extattr_create_unlink, tc) 199 1.2 christos { 200 1.2 christos int fd; 201 1.2 christos 202 1.2 christos start(); 203 1.2 christos if ((fd = rump_sys_open(M, O_RDWR | O_CREAT, 0600)) == -1) 204 1.2 christos atf_tc_fail_errno("open"); 205 1.2 christos 206 1.2 christos if (rump_sys_close(fd) == -1) 207 1.2 christos atf_tc_fail_errno("close"); 208 1.2 christos 209 1.2 christos /* create extattr */ 210 1.2 christos if (rump_sys_extattr_set_file(M, EXTATTR_NAMESPACE_USER, T, S, 9) == -1) 211 1.2 christos atf_tc_fail_errno("extattr_set_file"); 212 1.2 christos 213 1.2 christos if (rump_sys_unlink(M) == -1) 214 1.2 christos atf_tc_fail_errno("unlink"); 215 1.2 christos finish(); 216 1.2 christos 217 1.1 christos } 218 1.1 christos 219 1.2 christos ATF_TC_CLEANUP(extattr_create_unlink, tc) 220 1.1 christos { 221 1.1 christos 222 1.1 christos unlink(IMGNAME); 223 1.1 christos } 224 1.1 christos 225 1.1 christos ATF_TP_ADD_TCS(tp) 226 1.1 christos { 227 1.2 christos ATF_TP_ADD_TC(tp, extattr_simple); 228 1.2 christos ATF_TP_ADD_TC(tp, extattr_create_unlink); 229 1.1 christos return atf_no_error(); 230 1.1 christos } 231