libcam_test.c revision 321117
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: stable/11/lib/libcam/tests/libcam_test.c 321117 2017-07-18 08:30:51Z ngie $");
29
30#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <camlib.h>
34
35#include <atf-c.h>
36
37static const char *
38get_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
47static void
48cam_clear_error(void)
49{
50
51	strcpy(cam_errbuf, "");
52}
53
54static bool
55cam_has_error(void)
56{
57
58	return (strlen(cam_errbuf) != 0);
59}
60
61ATF_TC(cam_open_device_negative_test_O_RDONLY);
62ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
63{
64
65	atf_tc_set_md_var(tc, "descr",
66	    "test that cam_open_device(`cam_device`, O_RDONLY) fails to open "
67	    "the underlying pass(4) device (bug 217649)");
68	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
69	atf_tc_set_md_var(tc, "require.user", "root");
70}
71
72ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
73{
74	const char *cam_test_device;
75
76	cam_test_device = get_cam_test_device(tc);
77
78	cam_clear_error();
79	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
80	ATF_REQUIRE(cam_has_error());
81}
82
83ATF_TC(cam_open_device_negative_test_nonexistent);
84ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
85{
86
87	atf_tc_set_md_var(tc, "require.user", "root");
88}
89
90ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
91{
92
93	cam_clear_error();
94	ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
95	ATF_REQUIRE(cam_has_error());
96}
97
98ATF_TC(cam_open_device_negative_test_unprivileged);
99ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
100{
101
102	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
103	atf_tc_set_md_var(tc, "require.user", "unprivileged");
104}
105
106ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
107{
108	const char *cam_test_device;
109
110	cam_test_device = get_cam_test_device(tc);
111
112	cam_clear_error();
113	ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
114	ATF_REQUIRE(cam_has_error());
115
116	cam_clear_error();
117	ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
118	ATF_REQUIRE(cam_has_error());
119}
120
121ATF_TC(cam_open_device_positive_test);
122ATF_TC_HEAD(cam_open_device_positive_test, tc)
123{
124
125	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
126	atf_tc_set_md_var(tc, "require.user", "root");
127}
128
129ATF_TC_BODY(cam_open_device_positive_test, tc)
130{
131	struct cam_device *cam_dev;
132	const char *cam_test_device;
133
134	cam_test_device = get_cam_test_device(tc);
135
136	cam_clear_error();
137	cam_dev = cam_open_device(cam_test_device, O_RDWR);
138	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
139	    cam_errbuf);
140	ATF_REQUIRE(!cam_has_error());
141	cam_close_device(cam_dev);
142}
143
144ATF_TC(cam_close_device_negative_test_NULL);
145ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
146{
147
148	atf_tc_set_md_var(tc, "descr",
149	    "test that cam_close_device(NULL) succeeds without error");
150	atf_tc_set_md_var(tc, "require.user", "root");
151}
152
153ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
154{
155
156	cam_clear_error();
157	cam_close_device(NULL);
158	ATF_REQUIRE(!cam_has_error());
159}
160
161ATF_TC(cam_getccb_positive_test);
162ATF_TC_HEAD(cam_getccb_positive_test, tc)
163{
164
165	atf_tc_set_md_var(tc, "require.config", "cam_test_device");
166	atf_tc_set_md_var(tc, "require.user", "root");
167}
168
169ATF_TC_BODY(cam_getccb_positive_test, tc)
170{
171	union ccb *cam_ccb;
172	struct cam_device *cam_dev;
173	const char *cam_test_device;
174
175	cam_test_device = get_cam_test_device(tc);
176
177	cam_clear_error();
178	cam_dev = cam_open_device(cam_test_device, O_RDWR);
179	ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
180	    cam_errbuf);
181	ATF_REQUIRE(!cam_has_error());
182	cam_ccb = cam_getccb(cam_dev);
183	ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf);
184	ATF_REQUIRE(!cam_has_error());
185	cam_freeccb(cam_ccb);
186	cam_close_device(cam_dev);
187}
188
189ATF_TC(cam_freeccb_negative_test_NULL);
190ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
191{
192
193	atf_tc_set_md_var(tc, "descr",
194	    "test that cam_freeccb(NULL) succeeds without error");
195	atf_tc_set_md_var(tc, "require.user", "root");
196}
197
198ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
199{
200
201	cam_clear_error();
202	cam_freeccb(NULL);
203	ATF_REQUIRE(!cam_has_error());
204}
205
206ATF_TP_ADD_TCS(tp)
207{
208
209	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY);
210	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent);
211	ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged);
212	ATF_TP_ADD_TC(tp, cam_open_device_positive_test);
213	ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL);
214	ATF_TP_ADD_TC(tp, cam_getccb_positive_test);
215	ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL);
216
217	return (atf_no_error());
218}
219