2 * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 /* Tests functions in lib/libcam/camlib.c */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
40 get_cam_test_device(const atf_tc_t *tc)
42 const char *cam_test_device;
44 cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
46 return (cam_test_device);
53 strcpy(cam_errbuf, "");
60 return (strlen(cam_errbuf) != 0);
63 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
64 ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
66 char parsed_dev_name[DEV_IDLEN + 1];
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");
74 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
75 ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
77 char parsed_dev_name[DEV_IDLEN + 1];
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");
85 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
86 ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
88 char parsed_dev_name[DEV_IDLEN + 1];
91 ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
92 nitems(parsed_dev_name), &parsed_unit) == -1,
93 "cam_get_device succeeded unexpectedly");
96 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
97 ATF_TC_BODY(cam_get_device_negative_test_root, tc)
99 char parsed_dev_name[DEV_IDLEN + 1];
102 ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
103 nitems(parsed_dev_name), &parsed_unit) == -1,
104 "cam_get_device succeeded unexpectedly");
107 ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
108 ATF_TC_BODY(cam_get_device_positive_test, tc)
110 char expected_dev_name[] = "foo";
111 char parsed_dev_name[DEV_IDLEN + 1];
112 int expected_unit, parsed_unit;
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);
122 strcpy(parsed_dev_name, "");
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);
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.
137 ATF_TC_WITHOUT_HEAD(cam_get_device_sa_test);
138 ATF_TC_BODY(cam_get_device_sa_test, tc)
140 char parsed_dev_name[DEV_IDLEN + 1];
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);
149 strcpy(parsed_dev_name, "");
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);
159 ATF_TC(cam_open_device_negative_test_O_RDONLY);
160 ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
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");
170 ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
172 const char *cam_test_device;
174 cam_test_device = get_cam_test_device(tc);
177 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
178 ATF_REQUIRE(cam_has_error());
181 ATF_TC(cam_open_device_negative_test_nonexistent);
182 ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
185 atf_tc_set_md_var(tc, "require.user", "root");
188 ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
192 ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
193 ATF_REQUIRE(cam_has_error());
196 ATF_TC(cam_open_device_negative_test_unprivileged);
197 ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
200 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
201 atf_tc_set_md_var(tc, "require.user", "unprivileged");
204 ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
206 const char *cam_test_device;
208 cam_test_device = get_cam_test_device(tc);
211 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
212 ATF_REQUIRE(cam_has_error());
215 ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
216 ATF_REQUIRE(cam_has_error());
219 ATF_TC(cam_open_device_positive_test);
220 ATF_TC_HEAD(cam_open_device_positive_test, tc)
223 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
224 atf_tc_set_md_var(tc, "require.user", "root");
227 ATF_TC_BODY(cam_open_device_positive_test, tc)
229 struct cam_device *cam_dev;
230 const char *cam_test_device;
232 cam_test_device = get_cam_test_device(tc);
235 cam_dev = cam_open_device(cam_test_device, O_RDWR);
236 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
238 ATF_REQUIRE(!cam_has_error());
239 cam_close_device(cam_dev);
242 ATF_TC(cam_close_device_negative_test_NULL);
243 ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
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");
251 ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
255 cam_close_device(NULL);
256 ATF_REQUIRE(!cam_has_error());
259 ATF_TC(cam_getccb_positive_test);
260 ATF_TC_HEAD(cam_getccb_positive_test, tc)
263 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
264 atf_tc_set_md_var(tc, "require.user", "root");
267 ATF_TC_BODY(cam_getccb_positive_test, tc)
270 struct cam_device *cam_dev;
271 const char *cam_test_device;
273 cam_test_device = get_cam_test_device(tc);
276 cam_dev = cam_open_device(cam_test_device, O_RDWR);
277 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
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);
287 ATF_TC(cam_freeccb_negative_test_NULL);
288 ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
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");
296 ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
301 ATF_REQUIRE(!cam_has_error());
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);
321 return (atf_no_error());