xref: /freebsd/lib/libcam/tests/libcam_test.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1 /*-
2  * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /* Tests functions in lib/libcam/camlib.c */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <camlib.h>
36 
37 #include <atf-c.h>
38 
39 static const char *
40 get_cam_test_device(const atf_tc_t *tc)
41 {
42 	const char *cam_test_device;
43 
44 	cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
45 
46 	return (cam_test_device);
47 }
48 
49 static void
50 cam_clear_error(void)
51 {
52 
53 	strcpy(cam_errbuf, "");
54 }
55 
56 static bool
57 cam_has_error(void)
58 {
59 
60 	return (strlen(cam_errbuf) != 0);
61 }
62 
63 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
64 ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
65 {
66 	char parsed_dev_name[DEV_IDLEN + 1];
67 	int parsed_unit;
68 
69 	ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name,
70 	    nitems(parsed_dev_name), &parsed_unit) == -1,
71 	    "cam_get_device succeeded unexpectedly");
72 }
73 
74 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
75 ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
76 {
77 	char parsed_dev_name[DEV_IDLEN + 1];
78 	int parsed_unit;
79 
80 	ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name,
81 	    nitems(parsed_dev_name), &parsed_unit) == -1,
82 	    "cam_get_device succeeded unexpectedly");
83 }
84 
85 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
86 ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
87 {
88 	char parsed_dev_name[DEV_IDLEN + 1];
89 	int parsed_unit;
90 
91 	ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
92 	    nitems(parsed_dev_name), &parsed_unit) == -1,
93 	    "cam_get_device succeeded unexpectedly");
94 }
95 
96 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
97 ATF_TC_BODY(cam_get_device_negative_test_root, tc)
98 {
99 	char parsed_dev_name[DEV_IDLEN + 1];
100 	int parsed_unit;
101 
102 	ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
103 	    nitems(parsed_dev_name), &parsed_unit) == -1,
104 	    "cam_get_device succeeded unexpectedly");
105 }
106 
107 ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
108 ATF_TC_BODY(cam_get_device_positive_test, tc)
109 {
110 	char expected_dev_name[] = "foo";
111 	char parsed_dev_name[DEV_IDLEN + 1];
112 	int expected_unit, parsed_unit;
113 
114 	expected_unit = 1;
115 
116 	ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name,
117 	    nitems(parsed_dev_name), &parsed_unit) == 0,
118 	    "cam_get_device failed");
119 	ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
120 	ATF_REQUIRE(parsed_unit == expected_unit);
121 
122 	strcpy(parsed_dev_name, "");
123 	parsed_unit = -1;
124 
125 	ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name,
126 	    nitems(parsed_dev_name), &parsed_unit) == 0,
127 	    "cam_get_device failed");
128 	ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
129 	ATF_REQUIRE(parsed_unit == expected_unit);
130 }
131 
132 /*
133  * sa(4) uniquely creates nsa and esa device nodes for non-rewind operations
134  * and eject-on-close operations.  cam_get_device must special case these nodes
135  * to always return the base device.
136  */
137 ATF_TC_WITHOUT_HEAD(cam_get_device_sa_test);
138 ATF_TC_BODY(cam_get_device_sa_test, tc)
139 {
140 	char parsed_dev_name[DEV_IDLEN + 1];
141 	int parsed_unit;
142 
143 	ATF_REQUIRE_MSG(cam_get_device("nsa99", parsed_dev_name,
144 	    nitems(parsed_dev_name), &parsed_unit) == 0,
145 	    "cam_get_device failed");
146 	ATF_REQUIRE_STREQ(parsed_dev_name, "sa");
147 	ATF_REQUIRE(parsed_unit == 99);
148 
149 	strcpy(parsed_dev_name, "");
150 	parsed_unit = -1;
151 
152 	ATF_REQUIRE_MSG(cam_get_device("esa99", parsed_dev_name,
153 	    nitems(parsed_dev_name), &parsed_unit) == 0,
154 	    "cam_get_device failed");
155 	ATF_REQUIRE_STREQ(parsed_dev_name, "sa");
156 	ATF_REQUIRE(parsed_unit == 99);
157 }
158 
159 ATF_TC(cam_open_device_negative_test_O_RDONLY);
160 ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
161 {
162 
163 	atf_tc_set_md_var(tc, "descr",
164 	    "test that cam_open_device(`cam_device`, O_RDONLY) fails to open "
165 	    "the underlying pass(4) device (bug 217649)");
166 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
167 	atf_tc_set_md_var(tc, "require.user", "root");
168 }
169 
170 ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
171 {
172 	const char *cam_test_device;
173 
174 	cam_test_device = get_cam_test_device(tc);
175 
176 	cam_clear_error();
177 	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
178 	ATF_REQUIRE(cam_has_error());
179 }
180 
181 ATF_TC(cam_open_device_negative_test_nonexistent);
182 ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
183 {
184 
185 	atf_tc_set_md_var(tc, "require.user", "root");
186 }
187 
188 ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
189 {
190 
191 	cam_clear_error();
192 	ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
193 	ATF_REQUIRE(cam_has_error());
194 }
195 
196 ATF_TC(cam_open_device_negative_test_unprivileged);
197 ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
198 {
199 
200 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
201 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
202 }
203 
204 ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
205 {
206 	const char *cam_test_device;
207 
208 	cam_test_device = get_cam_test_device(tc);
209 
210 	cam_clear_error();
211 	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
212 	ATF_REQUIRE(cam_has_error());
213 
214 	cam_clear_error();
215 	ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
216 	ATF_REQUIRE(cam_has_error());
217 }
218 
219 ATF_TC(cam_open_device_positive_test);
220 ATF_TC_HEAD(cam_open_device_positive_test, tc)
221 {
222 
223 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
224 	atf_tc_set_md_var(tc, "require.user", "root");
225 }
226 
227 ATF_TC_BODY(cam_open_device_positive_test, tc)
228 {
229 	struct cam_device *cam_dev;
230 	const char *cam_test_device;
231 
232 	cam_test_device = get_cam_test_device(tc);
233 
234 	cam_clear_error();
235 	cam_dev = cam_open_device(cam_test_device, O_RDWR);
236 	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
237 	    cam_errbuf);
238 	ATF_REQUIRE(!cam_has_error());
239 	cam_close_device(cam_dev);
240 }
241 
242 ATF_TC(cam_close_device_negative_test_NULL);
243 ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
244 {
245 
246 	atf_tc_set_md_var(tc, "descr",
247 	    "test that cam_close_device(NULL) succeeds without error");
248 	atf_tc_set_md_var(tc, "require.user", "root");
249 }
250 
251 ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
252 {
253 
254 	cam_clear_error();
255 	cam_close_device(NULL);
256 	ATF_REQUIRE(!cam_has_error());
257 }
258 
259 ATF_TC(cam_getccb_positive_test);
260 ATF_TC_HEAD(cam_getccb_positive_test, tc)
261 {
262 
263 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
264 	atf_tc_set_md_var(tc, "require.user", "root");
265 }
266 
267 ATF_TC_BODY(cam_getccb_positive_test, tc)
268 {
269 	union ccb *cam_ccb;
270 	struct cam_device *cam_dev;
271 	const char *cam_test_device;
272 
273 	cam_test_device = get_cam_test_device(tc);
274 
275 	cam_clear_error();
276 	cam_dev = cam_open_device(cam_test_device, O_RDWR);
277 	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
278 	    cam_errbuf);
279 	ATF_REQUIRE(!cam_has_error());
280 	cam_ccb = cam_getccb(cam_dev);
281 	ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf);
282 	ATF_REQUIRE(!cam_has_error());
283 	cam_freeccb(cam_ccb);
284 	cam_close_device(cam_dev);
285 }
286 
287 ATF_TC(cam_freeccb_negative_test_NULL);
288 ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
289 {
290 
291 	atf_tc_set_md_var(tc, "descr",
292 	    "test that cam_freeccb(NULL) succeeds without error");
293 	atf_tc_set_md_var(tc, "require.user", "root");
294 }
295 
296 ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
297 {
298 
299 	cam_clear_error();
300 	cam_freeccb(NULL);
301 	ATF_REQUIRE(!cam_has_error());
302 }
303 
304 ATF_TP_ADD_TCS(tp)
305 {
306 
307 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path);
308 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path);
309 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path);
310 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root);
311 	ATF_TP_ADD_TC(tp, cam_get_device_positive_test);
312 	ATF_TP_ADD_TC(tp, cam_get_device_sa_test);
313 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY);
314 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent);
315 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged);
316 	ATF_TP_ADD_TC(tp, cam_open_device_positive_test);
317 	ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL);
318 	ATF_TP_ADD_TC(tp, cam_getccb_positive_test);
319 	ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL);
320 
321 	return (atf_no_error());
322 }
323