Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2017, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #include <stddef.h>
32 : : #include <stdarg.h>
33 : : #include <string.h>
34 : : #include <setjmp.h>
35 : : #include <cmocka.h>
36 : : #include <errno.h>
37 : :
38 : : #include <wrappers.h>
39 : : #include "control-wrappers.h"
40 : : #include "test-suites.h"
41 : :
42 : : #include "tap-ctl.h"
43 : : #include "blktap2.h"
44 : :
45 : : void *proc_misc_data = NULL;
46 : :
47 : : /*
48 : : * not including " 55 blktap/control\n"
49 : : */
50 : : char *basic_proc_misc =
51 : : "200 tun\n"
52 : : " 46 dlm_xapi-clusterd-lockspace\n"
53 : : " 47 dlm_plock\n"
54 : : " 48 dlm-monitor\n"
55 : : " 49 dlm-control\n"
56 : : "237 loop-control\n"
57 : : "236 device-mapper\n"
58 : : "130 watchdog\n"
59 : : " 50 nvme-fabrics\n"
60 : : " 51 memory_bandwidth\n"
61 : : " 52 network_throughput\n"
62 : : " 53 network_latency\n"
63 : : " 54 cpu_dma_latency\n"
64 : : " 1 psaux\n"
65 : : "183 hw_random\n"
66 : : "228 hpet\n"
67 : : " 56 xen/hypercall\n"
68 : : " 57 xen/privcmd\n"
69 : : "227 mcelog\n"
70 : : " 58 xen/gntalloc\n"
71 : : " 59 xen/gntdev\n"
72 : : " 60 xen/evtchn\n"
73 : : " 61 xen/xenbus_backend\n"
74 : : " 62 xen/xenbus\n"
75 : : "235 autofs\n"
76 : : " 63 vga_arbiter\n";
77 : :
78 : 5 : FILE *prepare_mock_misc(char *additional_data)
79 : : {
80 : 5 : size_t file_size = strlen(basic_proc_misc) + 1;
81 : :
82 [ + + ]: 5 : if (additional_data) {
83 : 4 : file_size += strnlen(additional_data, 1024);
84 : : }
85 : 5 : proc_misc_data = test_malloc(file_size);
86 : 5 : strncpy(proc_misc_data, basic_proc_misc, file_size);
87 [ + + ]: 5 : if (additional_data) {
88 : 4 : strncpy(proc_misc_data + strlen(basic_proc_misc),
89 : 4 : additional_data, file_size - strlen(basic_proc_misc));
90 : : }
91 : 5 : return fmemopen(proc_misc_data, file_size, "r");
92 : : }
93 : :
94 : 0 : void free_mock_misc(void)
95 : : {
96 : 5 : test_free(proc_misc_data);
97 : 0 : }
98 : :
99 : 1 : void test_tap_ctl_allocate_prep_dir_no_access(void **state)
100 : : {
101 : : int result;
102 : : int minor;
103 : : char *devname;
104 : :
105 : 1 : will_return(__wrap_access, ENOENT);
106 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
107 : 1 : will_return(__wrap_mkdir, EEXIST);
108 : 1 : expect_string(__wrap_mkdir, pathname, "/var");
109 : 1 : will_return(__wrap_mkdir, EEXIST);
110 : 1 : expect_string(__wrap_mkdir, pathname, "/var/run");
111 : 1 : will_return(__wrap_mkdir, EACCES);
112 : 1 : expect_string(__wrap_mkdir, pathname, "/var/run/blktap-control");
113 : :
114 : 1 : result = tap_ctl_allocate(&minor, &devname);
115 : :
116 : 1 : assert_int_equal(EACCES, result);
117 : 1 : }
118 : :
119 : 1 : void test_tap_ctl_allocate_no_device_info(void **state)
120 : : {
121 : : int result;
122 : : int minor;
123 : : char *devname;
124 : :
125 : 1 : FILE *proc_misc = prepare_mock_misc(NULL);
126 : :
127 : : /* Prepare Directory */
128 : 1 : will_return(__wrap_access, ENOENT);
129 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
130 : 1 : will_return(__wrap_mkdir, EEXIST);
131 : 1 : expect_string(__wrap_mkdir, pathname, "/var");
132 : 1 : will_return(__wrap_mkdir, EEXIST);
133 : 1 : expect_string(__wrap_mkdir, pathname, "/var/run");
134 : 1 : will_return(__wrap_mkdir, 0);
135 : 1 : expect_string(__wrap_mkdir, pathname, "/var/run/blktap-control");
136 : : /* Check Environment */
137 : 1 : will_return(__wrap_fopen, proc_misc);
138 : 1 : will_return(__wrap_flock, 0);
139 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
140 : 1 : will_return(__wrap_access, ENOENT);
141 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2/control");
142 : : /* Close check environment */
143 : 1 : will_return(__wrap_flock, 0);
144 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
145 : 1 : expect_value(__wrap_fclose, fp, proc_misc);
146 : :
147 : 1 : result = tap_ctl_allocate(&minor, &devname);
148 : :
149 : : free_mock_misc();
150 : :
151 : 1 : assert_int_equal(ENOSYS, result);
152 : 1 : }
153 : :
154 : 1 : void test_tap_ctl_allocate_make_device_fail(void **state)
155 : : {
156 : : int result;
157 : : int minor;
158 : : char *devname;
159 : :
160 : 1 : FILE *proc_misc = prepare_mock_misc(" 55 blktap/control\n");
161 : :
162 : : /* Prepare Directory */
163 : 1 : will_return(__wrap_access, 0);
164 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
165 : : /* Check Environment */
166 : 1 : will_return(__wrap_fopen, proc_misc);
167 : 1 : will_return(__wrap_flock, 0);
168 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
169 : 1 : will_return(__wrap_access, ENOENT);
170 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2/control");
171 : : /* Make Device/Prepare Directory*/
172 : 1 : will_return(__wrap_access, 0);
173 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
174 : 1 : will_return(__wrap_unlink, 0);
175 : 1 : expect_string(__wrap_unlink, pathname, "/dev/xen/blktap-2/control");
176 : 1 : will_return(__wrap___xmknod, EPERM);
177 : 1 : expect_string(__wrap___xmknod, pathname, "/dev/xen/blktap-2/control");
178 : : /* Close check environment */
179 : 1 : will_return(__wrap_flock, 0);
180 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
181 : 1 : expect_value(__wrap_fclose, fp, proc_misc);
182 : :
183 : 1 : result = tap_ctl_allocate(&minor, &devname);
184 : :
185 : : free_mock_misc();
186 : :
187 : 1 : assert_int_equal(EPERM, result);
188 : 1 : }
189 : :
190 : 1 : void test_tap_ctl_allocate_ring_create_fail(void **state)
191 : : {
192 : : int result;
193 : : int minor;
194 : 1 : char *devname = NULL;
195 : 1 : int dev_fd = 12;
196 : :
197 : 1 : FILE *proc_misc = prepare_mock_misc(" 55 blktap/control\n");
198 : :
199 : : /* Prepare Directory */
200 : 1 : will_return(__wrap_access, 0);
201 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
202 : : /* Check Environment */
203 : 1 : will_return(__wrap_fopen, proc_misc);
204 : 1 : will_return(__wrap_flock, 0);
205 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
206 : 1 : will_return(__wrap_access, ENOENT);
207 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2/control");
208 : : /* Make Device/Prepare Directory*/
209 : 1 : will_return(__wrap_access, 0);
210 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
211 : 1 : will_return(__wrap_unlink, 0);
212 : 1 : expect_string(__wrap_unlink, pathname, "/dev/xen/blktap-2/control");
213 : 1 : will_return(__wrap___xmknod, 0);
214 : 1 : expect_string(__wrap___xmknod, pathname, "/dev/xen/blktap-2/control");
215 : : /* Close check environment */
216 : 1 : will_return(__wrap_flock, 0);
217 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
218 : 1 : expect_value(__wrap_fclose, fp, proc_misc);
219 : : /* allocate device */
220 : 1 : will_return(__wrap_open, dev_fd);
221 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
222 : 1 : will_return(__wrap_ioctl, 0);
223 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
224 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_ALLOC_TAP);
225 : 1 : will_return(__wrap_close, 0);
226 : 1 : expect_value(__wrap_close, fd, dev_fd);
227 : : /* Make Device - ring */
228 : 1 : will_return(__wrap_access, ENOENT);
229 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
230 : 1 : will_return(__wrap_mkdir, EEXIST);
231 : 1 : expect_string(__wrap_mkdir, pathname, "/dev");
232 : 1 : will_return(__wrap_mkdir, EEXIST);
233 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen");
234 : 1 : will_return(__wrap_mkdir, EEXIST);
235 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen/blktap-2");
236 : 1 : will_return(__wrap_unlink, 0);
237 : 1 : expect_any(__wrap_unlink, pathname);
238 : 1 : will_return(__wrap___xmknod, EPERM);
239 : 1 : expect_any(__wrap___xmknod, pathname);
240 : : /* tap-ctl-free */
241 : 1 : will_return(__wrap_open, dev_fd);
242 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
243 : 1 : will_return(__wrap_ioctl, 0);
244 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
245 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_FREE_TAP);
246 : 1 : will_return(__wrap_close, 0);
247 : 1 : expect_value(__wrap_close, fd, dev_fd);
248 : :
249 : 1 : result = tap_ctl_allocate(&minor, &devname);
250 : :
251 : : free_mock_misc();
252 : :
253 : 1 : assert_int_equal(EPERM, result);
254 : 1 : }
255 : :
256 : 1 : void test_tap_ctl_allocate_io_device_fail(void **state)
257 : : {
258 : : int result;
259 : : int minor;
260 : 1 : char *devname = NULL;
261 : 1 : int dev_fd = 12;
262 : :
263 : 1 : FILE *proc_misc = prepare_mock_misc(" 55 blktap/control\n");
264 : :
265 : : /* Prepare Directory */
266 : 1 : will_return(__wrap_access, 0);
267 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
268 : : /* Check Environment */
269 : 1 : will_return(__wrap_fopen, proc_misc);
270 : 1 : will_return(__wrap_flock, 0);
271 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
272 : 1 : will_return(__wrap_access, ENOENT);
273 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2/control");
274 : : /* Make Device/Prepare Directory*/
275 : 1 : will_return(__wrap_access, 0);
276 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
277 : 1 : will_return(__wrap_unlink, 0);
278 : 1 : expect_string(__wrap_unlink, pathname, "/dev/xen/blktap-2/control");
279 : 1 : will_return(__wrap___xmknod, 0);
280 : 1 : expect_string(__wrap___xmknod, pathname, "/dev/xen/blktap-2/control");
281 : : /* Close check environment */
282 : 1 : will_return(__wrap_flock, 0);
283 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
284 : 1 : expect_value(__wrap_fclose, fp, proc_misc);
285 : : /* allocate device */
286 : 1 : will_return(__wrap_open, dev_fd);
287 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
288 : 1 : will_return(__wrap_ioctl, 0);
289 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
290 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_ALLOC_TAP);
291 : 1 : will_return(__wrap_close, 0);
292 : 1 : expect_value(__wrap_close, fd, dev_fd);
293 : : /* Make Device - ring */
294 : 1 : will_return(__wrap_access, ENOENT);
295 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
296 : 1 : will_return(__wrap_mkdir, EEXIST);
297 : 1 : expect_string(__wrap_mkdir, pathname, "/dev");
298 : 1 : will_return(__wrap_mkdir, EEXIST);
299 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen");
300 : 1 : will_return(__wrap_mkdir, EEXIST);
301 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen/blktap-2");
302 : 1 : will_return(__wrap_unlink, 0);
303 : 1 : expect_any(__wrap_unlink, pathname);
304 : 1 : will_return(__wrap___xmknod, 0);
305 : 1 : expect_any(__wrap___xmknod, pathname);
306 : :
307 : : /* Make Device - io device */
308 : 1 : will_return(__wrap_access, ENOENT);
309 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
310 : 1 : will_return(__wrap_mkdir, EEXIST);
311 : 1 : expect_string(__wrap_mkdir, pathname, "/dev");
312 : 1 : will_return(__wrap_mkdir, EEXIST);
313 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen");
314 : 1 : will_return(__wrap_mkdir, EEXIST);
315 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen/blktap-2");
316 : 1 : will_return(__wrap_unlink, 0);
317 : 1 : expect_any(__wrap_unlink, pathname);
318 : 1 : will_return(__wrap___xmknod, EPERM);
319 : 1 : expect_any(__wrap___xmknod, pathname);
320 : :
321 : : /* tap-ctl-free */
322 : 1 : will_return(__wrap_open, dev_fd);
323 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
324 : 1 : will_return(__wrap_ioctl, 0);
325 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
326 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_FREE_TAP);
327 : 1 : will_return(__wrap_close, 0);
328 : 1 : expect_value(__wrap_close, fd, dev_fd);
329 : :
330 : 1 : result = tap_ctl_allocate(&minor, &devname);
331 : :
332 : : free_mock_misc();
333 : :
334 : 1 : assert_int_equal(EPERM, result);
335 : 1 : }
336 : :
337 : 1 : void test_tap_ctl_allocate_success(void **state)
338 : : {
339 : : int result;
340 : : int minor;
341 : 1 : char *devname = NULL;
342 : 1 : int dev_fd = 12;
343 : :
344 : 1 : FILE *proc_misc = prepare_mock_misc(" 55 blktap/control\n");
345 : :
346 : : /* Prepare Directory */
347 : 1 : will_return(__wrap_access, 0);
348 : 1 : expect_string(__wrap_access, pathname, "/var/run/blktap-control");
349 : : /* Check Environment */
350 : 1 : will_return(__wrap_fopen, proc_misc);
351 : 1 : will_return(__wrap_flock, 0);
352 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
353 : 1 : will_return(__wrap_access, ENOENT);
354 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2/control");
355 : : /* Make Device/Prepare Directory*/
356 : 1 : will_return(__wrap_access, 0);
357 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
358 : 1 : will_return(__wrap_unlink, 0);
359 : 1 : expect_string(__wrap_unlink, pathname, "/dev/xen/blktap-2/control");
360 : 1 : will_return(__wrap___xmknod, 0);
361 : 1 : expect_string(__wrap___xmknod, pathname, "/dev/xen/blktap-2/control");
362 : : /* Close check environment */
363 : 1 : will_return(__wrap_flock, 0);
364 : 1 : expect_value(__wrap_flock, fd, fileno(proc_misc));
365 : 1 : expect_value(__wrap_fclose, fp, proc_misc);
366 : : /* allocate device */
367 : 1 : will_return(__wrap_open, dev_fd);
368 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
369 : 1 : will_return(__wrap_ioctl, 0);
370 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
371 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_ALLOC_TAP);
372 : 1 : will_return(__wrap_close, 0);
373 : 1 : expect_value(__wrap_close, fd, dev_fd);
374 : : /* Make Device - ring */
375 : 1 : will_return(__wrap_access, ENOENT);
376 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
377 : 1 : will_return(__wrap_mkdir, EEXIST);
378 : 1 : expect_string(__wrap_mkdir, pathname, "/dev");
379 : 1 : will_return(__wrap_mkdir, EEXIST);
380 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen");
381 : 1 : will_return(__wrap_mkdir, EEXIST);
382 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen/blktap-2");
383 : 1 : will_return(__wrap_unlink, 0);
384 : 1 : expect_any(__wrap_unlink, pathname);
385 : 1 : will_return(__wrap___xmknod, 0);
386 : 1 : expect_any(__wrap___xmknod, pathname);
387 : :
388 : : /* Make Device - io device */
389 : 1 : will_return(__wrap_access, ENOENT);
390 : 1 : expect_string(__wrap_access, pathname, "/dev/xen/blktap-2");
391 : 1 : will_return(__wrap_mkdir, EEXIST);
392 : 1 : expect_string(__wrap_mkdir, pathname, "/dev");
393 : 1 : will_return(__wrap_mkdir, EEXIST);
394 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen");
395 : 1 : will_return(__wrap_mkdir, EEXIST);
396 : 1 : expect_string(__wrap_mkdir, pathname, "/dev/xen/blktap-2");
397 : 1 : will_return(__wrap_unlink, 0);
398 : 1 : expect_any(__wrap_unlink, pathname);
399 : 1 : will_return(__wrap___xmknod, 0);
400 : 1 : expect_any(__wrap___xmknod, pathname);
401 : :
402 : :
403 : 1 : result = tap_ctl_allocate(&minor, &devname);
404 : :
405 : : free_mock_misc();
406 : :
407 : 1 : assert_int_equal(0, result);
408 : 1 : }
|