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