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