t_extattr.c revision 1.1 1 1.1 christos /* $NetBSD: t_extattr.c,v 1.1 2020/04/10 22:58:47 christos 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.1 christos __RCSID("$NetBSD: t_extattr.c,v 1.1 2020/04/10 22:58:47 christos 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 ATF_TC_WITH_CLEANUP(extattr);
55 1.1 christos ATF_TC_HEAD(extattr, tc)
56 1.1 christos {
57 1.1 christos atf_tc_set_md_var(tc, "descr", "test extended attribute support in "
58 1.1 christos "ffsv2");
59 1.1 christos atf_tc_set_md_var(tc, "timeout", "5");
60 1.1 christos }
61 1.1 christos
62 1.1 christos #define IMGNAME "extattr.img"
63 1.1 christos
64 1.1 christos #define G "/garage"
65 1.1 christos #define M "mercedes"
66 1.1 christos #define C "carburator"
67 1.1 christos #define E "engine"
68 1.1 christos #define T "trunk"
69 1.1 christos #define S "suitcase"
70 1.1 christos
71 1.1 christos static const char *attrs[] = { E, T };
72 1.1 christos
73 1.1 christos static void
74 1.1 christos check_list(const char *buf, ssize_t nr)
75 1.1 christos {
76 1.1 christos size_t p = 0;
77 1.1 christos for (size_t i = 0; i < __arraycount(attrs); i++) {
78 1.1 christos size_t l = buf[p++];
79 1.1 christos if (l != strlen(attrs[i]))
80 1.1 christos atf_tc_fail("got invalid len %zu expected %zu", l,
81 1.1 christos strlen(attrs[i]));
82 1.1 christos if (strncmp(&buf[p], attrs[i], l) != 0)
83 1.1 christos atf_tc_fail("got invalid str %.*s expected %s", (int)l,
84 1.1 christos &buf[p], attrs[i]);
85 1.1 christos p += l;
86 1.1 christos }
87 1.1 christos }
88 1.1 christos
89 1.1 christos // Make it ffsv2
90 1.1 christos const char *newfs = "newfs -O 2 -F -s 10000 " IMGNAME;
91 1.1 christos #define FAKEBLK "/dev/formula1"
92 1.1 christos
93 1.1 christos ATF_TC_BODY(extattr, tc)
94 1.1 christos {
95 1.1 christos struct ufs_args args;
96 1.1 christos ssize_t nr;
97 1.1 christos int fd;
98 1.1 christos char buf[512];
99 1.1 christos
100 1.1 christos if (system(newfs) == -1)
101 1.1 christos atf_tc_fail_errno("newfs failed");
102 1.1 christos
103 1.1 christos memset(&args, 0, sizeof(args));
104 1.1 christos args.fspec = __UNCONST(FAKEBLK);
105 1.1 christos
106 1.1 christos rump_init();
107 1.1 christos if (rump_sys_mkdir(G, 0777) == -1)
108 1.1 christos atf_tc_fail_errno("cannot create mountpoint");
109 1.1 christos rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK);
110 1.1 christos if (rump_sys_mount(MOUNT_FFS, G, 0, &args, sizeof(args))==-1)
111 1.1 christos atf_tc_fail_errno("rump_sys_mount failed");
112 1.1 christos
113 1.1 christos /* create extattr */
114 1.1 christos if (rump_sys_chdir(G) == 1)
115 1.1 christos atf_tc_fail_errno("chdir");
116 1.1 christos if ((fd = rump_sys_open(M, O_RDWR | O_CREAT, 0600)) == -1)
117 1.1 christos atf_tc_fail_errno("open");
118 1.1 christos if (rump_sys_write(fd, "hi mom\n", 7) != 7)
119 1.1 christos atf_tc_fail_errno("write");
120 1.1 christos
121 1.1 christos if (rump_sys_extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, E, C, 11) == -1)
122 1.1 christos atf_tc_fail_errno("exattr_set_fd");
123 1.1 christos if ((nr = rump_sys_extattr_get_file(M, EXTATTR_NAMESPACE_USER,
124 1.1 christos E, buf, sizeof(buf))) != 11)
125 1.1 christos atf_tc_fail_errno("extattr_get_file");
126 1.1 christos if (strcmp(buf, C) != 0)
127 1.1 christos atf_tc_fail("got invalid str %s expected %s", buf, C);
128 1.1 christos
129 1.1 christos if (rump_sys_extattr_set_file(M, EXTATTR_NAMESPACE_USER, T, S, 9) == -1)
130 1.1 christos atf_tc_fail_errno("extattr_set_file");
131 1.1 christos if ((nr = rump_sys_extattr_get_fd(fd, EXTATTR_NAMESPACE_USER,
132 1.1 christos T, buf, sizeof(buf))) != 9)
133 1.1 christos atf_tc_fail_errno("extattr_get_fd");
134 1.1 christos if (strcmp(buf, S) != 0)
135 1.1 christos atf_tc_fail("got invalid str %s expected %s", buf, S);
136 1.1 christos
137 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
138 1.1 christos buf, sizeof(buf))) != 13)
139 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr);
140 1.1 christos check_list(buf, __arraycount(attrs));
141 1.1 christos
142 1.1 christos if ((nr = rump_sys_extattr_list_file(M, EXTATTR_NAMESPACE_USER,
143 1.1 christos buf, sizeof(buf))) != 13)
144 1.1 christos atf_tc_fail_errno("extattr_list_file %zd", nr);
145 1.1 christos check_list(buf, __arraycount(attrs));
146 1.1 christos
147 1.1 christos if (rump_sys_extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, T) == -1)
148 1.1 christos atf_tc_fail_errno("extattr_delete_fd");
149 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
150 1.1 christos buf, sizeof(buf))) != 7)
151 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr);
152 1.1 christos check_list(buf, 1);
153 1.1 christos
154 1.1 christos if (rump_sys_extattr_delete_file(M, EXTATTR_NAMESPACE_USER, E) == -1)
155 1.1 christos atf_tc_fail_errno("extattr_delete_file");
156 1.1 christos if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
157 1.1 christos buf, sizeof(buf))) != 0)
158 1.1 christos atf_tc_fail_errno("extattr_list_fd %zd", nr);
159 1.1 christos
160 1.1 christos if (rump_sys_close(fd) == -1)
161 1.1 christos atf_tc_fail_errno("close");
162 1.1 christos if (rump_sys_unlink(M) == -1)
163 1.1 christos atf_tc_fail_errno("unlink");
164 1.1 christos }
165 1.1 christos
166 1.1 christos ATF_TC_CLEANUP(extattr, tc)
167 1.1 christos {
168 1.1 christos
169 1.1 christos unlink(IMGNAME);
170 1.1 christos }
171 1.1 christos
172 1.1 christos ATF_TP_ADD_TCS(tp)
173 1.1 christos {
174 1.1 christos ATF_TP_ADD_TC(tp, extattr);
175 1.1 christos return atf_no_error();
176 1.1 christos }
177