2 * Copyright (c) 2017 Enji Cooper <ngie@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 /* Tests functions in lib/libcam/camlib.c */
28 #include <sys/cdefs.h>
37 get_cam_test_device(const atf_tc_t *tc)
39 const char *cam_test_device;
41 cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
43 return (cam_test_device);
50 strcpy(cam_errbuf, "");
57 return (strlen(cam_errbuf) != 0);
60 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
61 ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
63 char parsed_dev_name[DEV_IDLEN + 1];
66 ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name,
67 nitems(parsed_dev_name), &parsed_unit) == -1,
68 "cam_get_device succeeded unexpectedly");
71 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
72 ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
74 char parsed_dev_name[DEV_IDLEN + 1];
77 ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name,
78 nitems(parsed_dev_name), &parsed_unit) == -1,
79 "cam_get_device succeeded unexpectedly");
82 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
83 ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
85 char parsed_dev_name[DEV_IDLEN + 1];
88 ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
89 nitems(parsed_dev_name), &parsed_unit) == -1,
90 "cam_get_device succeeded unexpectedly");
93 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
94 ATF_TC_BODY(cam_get_device_negative_test_root, tc)
96 char parsed_dev_name[DEV_IDLEN + 1];
99 ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
100 nitems(parsed_dev_name), &parsed_unit) == -1,
101 "cam_get_device succeeded unexpectedly");
104 ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
105 ATF_TC_BODY(cam_get_device_positive_test, tc)
107 char expected_dev_name[] = "foo";
108 char parsed_dev_name[DEV_IDLEN + 1];
109 int expected_unit, parsed_unit;
113 ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name,
114 nitems(parsed_dev_name), &parsed_unit) == 0,
115 "cam_get_device failed");
116 ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
117 ATF_REQUIRE(parsed_unit == expected_unit);
119 strcpy(parsed_dev_name, "");
122 ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name,
123 nitems(parsed_dev_name), &parsed_unit) == 0,
124 "cam_get_device failed");
125 ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
126 ATF_REQUIRE(parsed_unit == expected_unit);
130 * sa(4) uniquely creates nsa and esa device nodes for non-rewind operations
131 * and eject-on-close operations. cam_get_device must special case these nodes
132 * to always return the base device.
134 ATF_TC_WITHOUT_HEAD(cam_get_device_sa_test);
135 ATF_TC_BODY(cam_get_device_sa_test, tc)
137 char parsed_dev_name[DEV_IDLEN + 1];
140 ATF_REQUIRE_MSG(cam_get_device("nsa99", parsed_dev_name,
141 nitems(parsed_dev_name), &parsed_unit) == 0,
142 "cam_get_device failed");
143 ATF_REQUIRE_STREQ(parsed_dev_name, "sa");
144 ATF_REQUIRE(parsed_unit == 99);
146 strcpy(parsed_dev_name, "");
149 ATF_REQUIRE_MSG(cam_get_device("esa99", parsed_dev_name,
150 nitems(parsed_dev_name), &parsed_unit) == 0,
151 "cam_get_device failed");
152 ATF_REQUIRE_STREQ(parsed_dev_name, "sa");
153 ATF_REQUIRE(parsed_unit == 99);
156 ATF_TC(cam_open_device_negative_test_O_RDONLY);
157 ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
160 atf_tc_set_md_var(tc, "descr",
161 "test that cam_open_device(`cam_device`, O_RDONLY) fails to open "
162 "the underlying pass(4) device (bug 217649)");
163 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
164 atf_tc_set_md_var(tc, "require.user", "root");
167 ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
169 const char *cam_test_device;
171 cam_test_device = get_cam_test_device(tc);
174 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
175 ATF_REQUIRE(cam_has_error());
178 ATF_TC(cam_open_device_negative_test_nonexistent);
179 ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
182 atf_tc_set_md_var(tc, "require.user", "root");
185 ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
189 ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
190 ATF_REQUIRE(cam_has_error());
193 ATF_TC(cam_open_device_negative_test_unprivileged);
194 ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
197 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
198 atf_tc_set_md_var(tc, "require.user", "unprivileged");
201 ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
203 const char *cam_test_device;
205 cam_test_device = get_cam_test_device(tc);
208 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
209 ATF_REQUIRE(cam_has_error());
212 ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
213 ATF_REQUIRE(cam_has_error());
216 ATF_TC(cam_open_device_positive_test);
217 ATF_TC_HEAD(cam_open_device_positive_test, tc)
220 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
221 atf_tc_set_md_var(tc, "require.user", "root");
224 ATF_TC_BODY(cam_open_device_positive_test, tc)
226 struct cam_device *cam_dev;
227 const char *cam_test_device;
229 cam_test_device = get_cam_test_device(tc);
232 cam_dev = cam_open_device(cam_test_device, O_RDWR);
233 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
235 ATF_REQUIRE(!cam_has_error());
236 cam_close_device(cam_dev);
239 ATF_TC(cam_close_device_negative_test_NULL);
240 ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
243 atf_tc_set_md_var(tc, "descr",
244 "test that cam_close_device(NULL) succeeds without error");
245 atf_tc_set_md_var(tc, "require.user", "root");
248 ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
252 cam_close_device(NULL);
253 ATF_REQUIRE(!cam_has_error());
256 ATF_TC(cam_getccb_positive_test);
257 ATF_TC_HEAD(cam_getccb_positive_test, tc)
260 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
261 atf_tc_set_md_var(tc, "require.user", "root");
264 ATF_TC_BODY(cam_getccb_positive_test, tc)
267 struct cam_device *cam_dev;
268 const char *cam_test_device;
270 cam_test_device = get_cam_test_device(tc);
273 cam_dev = cam_open_device(cam_test_device, O_RDWR);
274 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
276 ATF_REQUIRE(!cam_has_error());
277 cam_ccb = cam_getccb(cam_dev);
278 ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf);
279 ATF_REQUIRE(!cam_has_error());
280 cam_freeccb(cam_ccb);
281 cam_close_device(cam_dev);
284 ATF_TC(cam_freeccb_negative_test_NULL);
285 ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
288 atf_tc_set_md_var(tc, "descr",
289 "test that cam_freeccb(NULL) succeeds without error");
290 atf_tc_set_md_var(tc, "require.user", "root");
293 ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
298 ATF_REQUIRE(!cam_has_error());
304 ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path);
305 ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path);
306 ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path);
307 ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root);
308 ATF_TP_ADD_TC(tp, cam_get_device_positive_test);
309 ATF_TP_ADD_TC(tp, cam_get_device_sa_test);
310 ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY);
311 ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent);
312 ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged);
313 ATF_TP_ADD_TC(tp, cam_open_device_positive_test);
314 ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL);
315 ATF_TP_ADD_TC(tp, cam_getccb_positive_test);
316 ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL);
318 return (atf_no_error());