xref: /freebsd/lib/libcam/tests/libcam_test.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <camlib.h>
34 
35 #include <atf-c.h>
36 
37 static const char *
38 get_cam_test_device(const atf_tc_t *tc)
39 {
40 	const char *cam_test_device;
41 
42 	cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
43 
44 	return (cam_test_device);
45 }
46 
47 static void
48 cam_clear_error(void)
49 {
50 
51 	strcpy(cam_errbuf, "");
52 }
53 
54 static bool
55 cam_has_error(void)
56 {
57 
58 	return (strlen(cam_errbuf) != 0);
59 }
60 
61 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
62 ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
63 {
64 	char parsed_dev_name[DEV_IDLEN + 1];
65 	int parsed_unit;
66 
67 	ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name,
68 	    nitems(parsed_dev_name), &parsed_unit) == -1,
69 	    "cam_get_device succeeded unexpectedly");
70 }
71 
72 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
73 ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
74 {
75 	char parsed_dev_name[DEV_IDLEN + 1];
76 	int parsed_unit;
77 
78 	ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name,
79 	    nitems(parsed_dev_name), &parsed_unit) == -1,
80 	    "cam_get_device succeeded unexpectedly");
81 }
82 
83 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
84 ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
85 {
86 	char parsed_dev_name[DEV_IDLEN + 1];
87 	int parsed_unit;
88 
89 	ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
90 	    nitems(parsed_dev_name), &parsed_unit) == -1,
91 	    "cam_get_device succeeded unexpectedly");
92 }
93 
94 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
95 ATF_TC_BODY(cam_get_device_negative_test_root, tc)
96 {
97 	char parsed_dev_name[DEV_IDLEN + 1];
98 	int parsed_unit;
99 
100 	ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
101 	    nitems(parsed_dev_name), &parsed_unit) == -1,
102 	    "cam_get_device succeeded unexpectedly");
103 }
104 
105 ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
106 ATF_TC_BODY(cam_get_device_positive_test, tc)
107 {
108 	char expected_dev_name[] = "foo";
109 	char parsed_dev_name[DEV_IDLEN + 1];
110 	int expected_unit, parsed_unit;
111 
112 	expected_unit = 1;
113 
114 	ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name,
115 	    nitems(parsed_dev_name), &parsed_unit) == 0,
116 	    "cam_get_device failed");
117 	ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
118 	ATF_REQUIRE(parsed_unit == expected_unit);
119 
120 	strcpy(parsed_dev_name, "");
121 	parsed_unit = -1;
122 
123 	ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name,
124 	    nitems(parsed_dev_name), &parsed_unit) == 0,
125 	    "cam_get_device failed");
126 	ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
127 	ATF_REQUIRE(parsed_unit == expected_unit);
128 }
129 
130 ATF_TC(cam_open_device_negative_test_O_RDONLY);
131 ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
132 {
133 
134 	atf_tc_set_md_var(tc, "descr",
135 	    "test that cam_open_device(`cam_device`, O_RDONLY) fails to open "
136 	    "the underlying pass(4) device (bug 217649)");
137 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
138 	atf_tc_set_md_var(tc, "require.user", "root");
139 }
140 
141 ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
142 {
143 	const char *cam_test_device;
144 
145 	cam_test_device = get_cam_test_device(tc);
146 
147 	cam_clear_error();
148 	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
149 	ATF_REQUIRE(cam_has_error());
150 }
151 
152 ATF_TC(cam_open_device_negative_test_nonexistent);
153 ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
154 {
155 
156 	atf_tc_set_md_var(tc, "require.user", "root");
157 }
158 
159 ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
160 {
161 
162 	cam_clear_error();
163 	ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
164 	ATF_REQUIRE(cam_has_error());
165 }
166 
167 ATF_TC(cam_open_device_negative_test_unprivileged);
168 ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
169 {
170 
171 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
172 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
173 }
174 
175 ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
176 {
177 	const char *cam_test_device;
178 
179 	cam_test_device = get_cam_test_device(tc);
180 
181 	cam_clear_error();
182 	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
183 	ATF_REQUIRE(cam_has_error());
184 
185 	cam_clear_error();
186 	ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
187 	ATF_REQUIRE(cam_has_error());
188 }
189 
190 ATF_TC(cam_open_device_positive_test);
191 ATF_TC_HEAD(cam_open_device_positive_test, tc)
192 {
193 
194 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
195 	atf_tc_set_md_var(tc, "require.user", "root");
196 }
197 
198 ATF_TC_BODY(cam_open_device_positive_test, tc)
199 {
200 	struct cam_device *cam_dev;
201 	const char *cam_test_device;
202 
203 	cam_test_device = get_cam_test_device(tc);
204 
205 	cam_clear_error();
206 	cam_dev = cam_open_device(cam_test_device, O_RDWR);
207 	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
208 	    cam_errbuf);
209 	ATF_REQUIRE(!cam_has_error());
210 	cam_close_device(cam_dev);
211 }
212 
213 ATF_TC(cam_close_device_negative_test_NULL);
214 ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
215 {
216 
217 	atf_tc_set_md_var(tc, "descr",
218 	    "test that cam_close_device(NULL) succeeds without error");
219 	atf_tc_set_md_var(tc, "require.user", "root");
220 }
221 
222 ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
223 {
224 
225 	cam_clear_error();
226 	cam_close_device(NULL);
227 	ATF_REQUIRE(!cam_has_error());
228 }
229 
230 ATF_TC(cam_getccb_positive_test);
231 ATF_TC_HEAD(cam_getccb_positive_test, tc)
232 {
233 
234 	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
235 	atf_tc_set_md_var(tc, "require.user", "root");
236 }
237 
238 ATF_TC_BODY(cam_getccb_positive_test, tc)
239 {
240 	union ccb *cam_ccb;
241 	struct cam_device *cam_dev;
242 	const char *cam_test_device;
243 
244 	cam_test_device = get_cam_test_device(tc);
245 
246 	cam_clear_error();
247 	cam_dev = cam_open_device(cam_test_device, O_RDWR);
248 	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
249 	    cam_errbuf);
250 	ATF_REQUIRE(!cam_has_error());
251 	cam_ccb = cam_getccb(cam_dev);
252 	ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf);
253 	ATF_REQUIRE(!cam_has_error());
254 	cam_freeccb(cam_ccb);
255 	cam_close_device(cam_dev);
256 }
257 
258 ATF_TC(cam_freeccb_negative_test_NULL);
259 ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
260 {
261 
262 	atf_tc_set_md_var(tc, "descr",
263 	    "test that cam_freeccb(NULL) succeeds without error");
264 	atf_tc_set_md_var(tc, "require.user", "root");
265 }
266 
267 ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
268 {
269 
270 	cam_clear_error();
271 	cam_freeccb(NULL);
272 	ATF_REQUIRE(!cam_has_error());
273 }
274 
275 ATF_TP_ADD_TCS(tp)
276 {
277 
278 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path);
279 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path);
280 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path);
281 	ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root);
282 	ATF_TP_ADD_TC(tp, cam_get_device_positive_test);
283 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY);
284 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent);
285 	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged);
286 	ATF_TP_ADD_TC(tp, cam_open_device_positive_test);
287 	ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL);
288 	ATF_TP_ADD_TC(tp, cam_getccb_positive_test);
289 	ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL);
290 
291 	return (atf_no_error());
292 }
293