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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
38 get_cam_test_device(const atf_tc_t *tc)
40 const char *cam_test_device;
42 cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
44 return (cam_test_device);
51 strcpy(cam_errbuf, "");
58 return (strlen(cam_errbuf) != 0);
61 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
62 ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
64 char parsed_dev_name[DEV_IDLEN + 1];
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");
72 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
73 ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
75 char parsed_dev_name[DEV_IDLEN + 1];
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");
83 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
84 ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
86 char parsed_dev_name[DEV_IDLEN + 1];
89 ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
90 nitems(parsed_dev_name), &parsed_unit) == -1,
91 "cam_get_device succeeded unexpectedly");
94 ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
95 ATF_TC_BODY(cam_get_device_negative_test_root, tc)
97 char parsed_dev_name[DEV_IDLEN + 1];
100 ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
101 nitems(parsed_dev_name), &parsed_unit) == -1,
102 "cam_get_device succeeded unexpectedly");
105 ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
106 ATF_TC_BODY(cam_get_device_positive_test, tc)
108 char expected_dev_name[] = "foo";
109 char parsed_dev_name[DEV_IDLEN + 1];
110 int expected_unit, parsed_unit;
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);
120 strcpy(parsed_dev_name, "");
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);
130 ATF_TC(cam_open_device_negative_test_O_RDONLY);
131 ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
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");
141 ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
143 const char *cam_test_device;
145 cam_test_device = get_cam_test_device(tc);
148 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
149 ATF_REQUIRE(cam_has_error());
152 ATF_TC(cam_open_device_negative_test_nonexistent);
153 ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
156 atf_tc_set_md_var(tc, "require.user", "root");
159 ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
163 ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
164 ATF_REQUIRE(cam_has_error());
167 ATF_TC(cam_open_device_negative_test_unprivileged);
168 ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
171 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
172 atf_tc_set_md_var(tc, "require.user", "unprivileged");
175 ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
177 const char *cam_test_device;
179 cam_test_device = get_cam_test_device(tc);
182 ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
183 ATF_REQUIRE(cam_has_error());
186 ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
187 ATF_REQUIRE(cam_has_error());
190 ATF_TC(cam_open_device_positive_test);
191 ATF_TC_HEAD(cam_open_device_positive_test, tc)
194 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
195 atf_tc_set_md_var(tc, "require.user", "root");
198 ATF_TC_BODY(cam_open_device_positive_test, tc)
200 struct cam_device *cam_dev;
201 const char *cam_test_device;
203 cam_test_device = get_cam_test_device(tc);
206 cam_dev = cam_open_device(cam_test_device, O_RDWR);
207 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
209 ATF_REQUIRE(!cam_has_error());
210 cam_close_device(cam_dev);
213 ATF_TC(cam_close_device_negative_test_NULL);
214 ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
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");
222 ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
226 cam_close_device(NULL);
227 ATF_REQUIRE(!cam_has_error());
230 ATF_TC(cam_getccb_positive_test);
231 ATF_TC_HEAD(cam_getccb_positive_test, tc)
234 atf_tc_set_md_var(tc, "require.config", "cam_test_device");
235 atf_tc_set_md_var(tc, "require.user", "root");
238 ATF_TC_BODY(cam_getccb_positive_test, tc)
241 struct cam_device *cam_dev;
242 const char *cam_test_device;
244 cam_test_device = get_cam_test_device(tc);
247 cam_dev = cam_open_device(cam_test_device, O_RDWR);
248 ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
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);
258 ATF_TC(cam_freeccb_negative_test_NULL);
259 ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
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");
267 ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
272 ATF_REQUIRE(!cam_has_error());
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);
291 return (atf_no_error());