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 <string.h>
39 : : #include <sys/types.h>
40 : : #include <sys/un.h>
41 : : #include <sys/socket.h>
42 : :
43 : : #include <wrappers.h>
44 : : #include "control-wrappers.h"
45 : : #include "util.h"
46 : : #include "test-suites.h"
47 : :
48 : : #include "tap-ctl.h"
49 : : #include "blktap2.h"
50 : :
51 : 1 : void test_tap_ctl_close_success(void **state)
52 : : {
53 : : int result;
54 : 1 : int test_pid = 1345;
55 : 1 : int test_minor = 17;
56 : 1 : int ipc_socket = 7;
57 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
58 : : struct mock_select_params write_select_params;
59 : : struct mock_select_params read_select_params;
60 : : struct mock_read_params read_params;
61 : : tapdisk_message_t write_message;
62 : : tapdisk_message_t read_message;
63 : : struct sockaddr_un saddr;
64 : :
65 : 1 : initialise_select_params(&write_select_params);
66 : 1 : initialise_select_params(&read_select_params);
67 : : memset(&read_message, 0, sizeof(read_message));
68 : : memset(&write_message, 0, sizeof(write_message));
69 : :
70 : : memset(&saddr, 0, sizeof(saddr));
71 : 1 : saddr.sun_family = AF_UNIX;
72 : : strcpy(saddr.sun_path, expected_sock_name);
73 : :
74 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
75 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
76 : 1 : expect_any(__wrap_socket, protocol);
77 : 1 : will_return(__wrap_socket, ipc_socket);
78 : :
79 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
80 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
81 : 1 : will_return(__wrap_connect, 0);
82 : :
83 : 1 : write_select_params.result = 1;
84 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
85 : 1 : expect_value(__wrap_select, timeout, NULL);
86 : 1 : will_return(__wrap_select, &write_select_params);
87 : :
88 : 1 : write_message.type = TAPDISK_MESSAGE_CLOSE;
89 : 1 : write_message.cookie = test_minor;
90 : 1 : expect_value(__wrap_write, fd, ipc_socket);
91 : 1 : expect_memory(__wrap_write, buf, &write_message, sizeof(write_message));
92 : 1 : will_return(__wrap_write, 1024);
93 : :
94 : 1 : read_select_params.result = 1;
95 : 1 : FD_SET(ipc_socket, &read_select_params.readfds);
96 : 1 : expect_value(__wrap_select, timeout, NULL);
97 : 1 : will_return(__wrap_select, &read_select_params);
98 : :
99 : 1 : read_message.type = TAPDISK_MESSAGE_CLOSE_RSP;
100 : 1 : read_message.cookie = test_minor;
101 : 1 : read_message.u.response.error = 0;
102 : 1 : read_params.result = sizeof(read_message);
103 : 1 : read_params.data = &read_message;
104 : 1 : expect_value(__wrap_read, fd, ipc_socket);
105 : 1 : will_return(__wrap_read, &read_params);
106 : :
107 : 1 : expect_value(__wrap_close, fd, ipc_socket);
108 : 1 : will_return(__wrap_close, 0);
109 : :
110 : : /* Call test API */
111 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, NULL);
112 : :
113 : 1 : assert_int_equal(0, result);
114 : 1 : }
115 : :
116 : 1 : void test_tap_ctl_force_close_success(void **state)
117 : : {
118 : : int result;
119 : 1 : int test_pid = 1345;
120 : 1 : int test_minor = 17;
121 : 1 : int ipc_socket = 7;
122 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
123 : : struct mock_select_params write_select_params;
124 : : struct mock_select_params read_select_params;
125 : : struct mock_read_params read_params;
126 : : tapdisk_message_t write_message;
127 : : tapdisk_message_t read_message;
128 : : struct sockaddr_un saddr;
129 : :
130 : 1 : initialise_select_params(&write_select_params);
131 : 1 : initialise_select_params(&read_select_params);
132 : : memset(&read_message, 0, sizeof(read_message));
133 : : memset(&write_message, 0, sizeof(write_message));
134 : :
135 : : memset(&saddr, 0, sizeof(saddr));
136 : 1 : saddr.sun_family = AF_UNIX;
137 : : strcpy(saddr.sun_path, expected_sock_name);
138 : :
139 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
140 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
141 : 1 : expect_any(__wrap_socket, protocol);
142 : 1 : will_return(__wrap_socket, ipc_socket);
143 : :
144 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
145 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
146 : 1 : will_return(__wrap_connect, 0);
147 : :
148 : 1 : write_select_params.result = 1;
149 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
150 : 1 : expect_value(__wrap_select, timeout, NULL);
151 : 1 : will_return(__wrap_select, &write_select_params);
152 : :
153 : 1 : write_message.type = TAPDISK_MESSAGE_FORCE_SHUTDOWN;
154 : 1 : write_message.cookie = test_minor;
155 : 1 : expect_value(__wrap_write, fd, ipc_socket);
156 : 1 : expect_memory(__wrap_write, buf, &write_message, sizeof(write_message));
157 : 1 : will_return(__wrap_write, 1024);
158 : :
159 : 1 : read_select_params.result = 1;
160 : 1 : FD_SET(ipc_socket, &read_select_params.readfds);
161 : 1 : expect_value(__wrap_select, timeout, NULL);
162 : 1 : will_return(__wrap_select, &read_select_params);
163 : :
164 : 1 : read_message.type = TAPDISK_MESSAGE_CLOSE_RSP;
165 : 1 : read_message.cookie = test_minor;
166 : 1 : read_message.u.response.error = 0;
167 : 1 : read_params.result = sizeof(read_message);
168 : 1 : read_params.data = &read_message;
169 : 1 : expect_value(__wrap_read, fd, ipc_socket);
170 : 1 : will_return(__wrap_read, &read_params);
171 : :
172 : 1 : expect_value(__wrap_close, fd, ipc_socket);
173 : 1 : will_return(__wrap_close, 0);
174 : :
175 : : /* Call test API */
176 : 1 : result = tap_ctl_close(test_pid, test_minor, -1, NULL);
177 : :
178 : 1 : assert_int_equal(0, result);
179 : 1 : }
180 : :
181 : 1 : void test_tap_ctl_close_connect_fail(void **state)
182 : : {
183 : : int result;
184 : 1 : int test_pid = 1345;
185 : 1 : int test_minor = 17;
186 : 1 : int ipc_socket = 7;
187 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
188 : : struct sockaddr_un saddr;
189 : :
190 : : memset(&saddr, 0, sizeof(saddr));
191 : 1 : saddr.sun_family = AF_UNIX;
192 : : strcpy(saddr.sun_path, expected_sock_name);
193 : :
194 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
195 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
196 : 1 : expect_any(__wrap_socket, protocol);
197 : 1 : will_return(__wrap_socket, ipc_socket);
198 : :
199 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
200 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
201 : 1 : will_return(__wrap_connect, ENOENT);
202 : :
203 : 1 : expect_value(__wrap_close, fd, ipc_socket);
204 : 1 : will_return(__wrap_close, 0);
205 : :
206 : : /* Call test API */
207 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, NULL);
208 : :
209 : 1 : assert_int_equal(-ENOENT, result);
210 : 1 : }
211 : :
212 : 1 : void test_tap_ctl_close_write_error(void **state)
213 : : {
214 : : int result;
215 : 1 : int test_pid = 1345;
216 : 1 : int test_minor = 17;
217 : 1 : int ipc_socket = 7;
218 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
219 : : struct mock_select_params write_select_params;
220 : : tapdisk_message_t message;
221 : : struct sockaddr_un saddr;
222 : :
223 : 1 : initialise_select_params(&write_select_params);
224 : : memset(&message, 0, sizeof(message));
225 : :
226 : : memset(&saddr, 0, sizeof(saddr));
227 : 1 : saddr.sun_family = AF_UNIX;
228 : : strcpy(saddr.sun_path, expected_sock_name);
229 : :
230 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
231 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
232 : 1 : expect_any(__wrap_socket, protocol);
233 : 1 : will_return(__wrap_socket, ipc_socket);
234 : :
235 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
236 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
237 : 1 : will_return(__wrap_connect, 0);
238 : :
239 : 1 : write_select_params.result = 1;
240 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
241 : 1 : expect_value(__wrap_select, timeout, NULL);
242 : 1 : will_return(__wrap_select, &write_select_params);
243 : :
244 : 1 : expect_value(__wrap_write, fd, ipc_socket);
245 : 1 : expect_any(__wrap_write, buf);
246 : 1 : will_return(__wrap_write, -EIO);
247 : :
248 : 1 : expect_value(__wrap_close, fd, ipc_socket);
249 : 1 : will_return(__wrap_close, 0);
250 : :
251 : : /* Call test API */
252 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, NULL);
253 : :
254 : 1 : assert_int_equal(-EIO, result);
255 : 1 : }
256 : :
257 : 1 : void test_tap_ctl_close_read_error(void **state)
258 : : {
259 : : int result;
260 : 1 : int test_pid = 1345;
261 : 1 : int test_minor = 17;
262 : 1 : int ipc_socket = 7;
263 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
264 : : struct mock_select_params write_select_params;
265 : : struct mock_select_params read_select_params;
266 : : struct mock_read_params read_params;
267 : : tapdisk_message_t write_message;
268 : : struct sockaddr_un saddr;
269 : :
270 : 1 : initialise_select_params(&write_select_params);
271 : 1 : initialise_select_params(&read_select_params);
272 : : memset(&write_message, 0, sizeof(write_message));
273 : :
274 : : memset(&saddr, 0, sizeof(saddr));
275 : 1 : saddr.sun_family = AF_UNIX;
276 : : strcpy(saddr.sun_path, expected_sock_name);
277 : :
278 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
279 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
280 : 1 : expect_any(__wrap_socket, protocol);
281 : 1 : will_return(__wrap_socket, ipc_socket);
282 : :
283 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
284 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
285 : 1 : will_return(__wrap_connect, 0);
286 : :
287 : 1 : write_select_params.result = 1;
288 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
289 : 1 : expect_value(__wrap_select, timeout, NULL);
290 : 1 : will_return(__wrap_select, &write_select_params);
291 : :
292 : 1 : write_message.type = TAPDISK_MESSAGE_CLOSE;
293 : 1 : write_message.cookie = test_minor;
294 : 1 : expect_value(__wrap_write, fd, ipc_socket);
295 : 1 : expect_memory(__wrap_write, buf, &write_message, sizeof(write_message));
296 : 1 : will_return(__wrap_write, 1024);
297 : :
298 : 1 : read_select_params.result = 1;
299 : 1 : FD_SET(ipc_socket, &read_select_params.readfds);
300 : 1 : expect_value(__wrap_select, timeout, NULL);
301 : 1 : will_return(__wrap_select, &read_select_params);
302 : :
303 : 1 : read_params.result = -EIO;
304 : 1 : read_params.data = NULL;
305 : 1 : expect_value(__wrap_read, fd, ipc_socket);
306 : 1 : will_return(__wrap_read, &read_params);
307 : :
308 : 1 : expect_value(__wrap_close, fd, ipc_socket);
309 : 1 : will_return(__wrap_close, 0);
310 : :
311 : : /* Call test API */
312 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, NULL);
313 : :
314 : 1 : assert_int_equal(-EIO, result);
315 : 1 : }
316 : :
317 : 1 : void test_tap_ctl_close_write_select_timeout(void **state)
318 : : {
319 : : int result;
320 : 1 : int test_pid = 1345;
321 : 1 : int test_minor = 17;
322 : 1 : int ipc_socket = 7;
323 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
324 : : struct mock_select_params write_select_params;
325 : : struct timeval write_timeout;
326 : : tapdisk_message_t message;
327 : : struct sockaddr_un saddr;
328 : :
329 : 1 : initialise_select_params(&write_select_params);
330 : : memset(&message, 0, sizeof(message));
331 : :
332 : : memset(&saddr, 0, sizeof(saddr));
333 : 1 : saddr.sun_family = AF_UNIX;
334 : : strcpy(saddr.sun_path, expected_sock_name);
335 : :
336 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
337 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
338 : 1 : expect_any(__wrap_socket, protocol);
339 : 1 : will_return(__wrap_socket, ipc_socket);
340 : :
341 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
342 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
343 : 1 : will_return(__wrap_connect, 0);
344 : :
345 : 1 : write_timeout.tv_sec = 30;
346 : 1 : write_timeout.tv_usec = 0;
347 : 1 : write_select_params.result = 0;
348 : 1 : expect_memory(__wrap_select, timeout, &write_timeout, sizeof(write_timeout));
349 : 1 : will_return(__wrap_select, &write_select_params);
350 : :
351 : 1 : expect_value(__wrap_close, fd, ipc_socket);
352 : 1 : will_return(__wrap_close, 0);
353 : :
354 : : /* Call test API */
355 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, &write_timeout);
356 : :
357 : 1 : assert_int_equal(-EIO, result);
358 : 1 : }
359 : :
360 : 1 : void test_tap_ctl_close_read_select_timeout(void **state)
361 : : {
362 : : int result;
363 : 1 : int test_pid = 1345;
364 : 1 : int test_minor = 17;
365 : 1 : int ipc_socket = 7;
366 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
367 : : struct mock_select_params write_select_params;
368 : : struct mock_select_params read_select_params;
369 : : struct timeval write_timeout;
370 : : struct timeval read_timeout;
371 : : tapdisk_message_t write_message;
372 : : struct sockaddr_un saddr;
373 : :
374 : 1 : initialise_select_params(&write_select_params);
375 : 1 : initialise_select_params(&read_select_params);
376 : : memset(&write_message, 0, sizeof(write_message));
377 : :
378 : : memset(&saddr, 0, sizeof(saddr));
379 : 1 : saddr.sun_family = AF_UNIX;
380 : : strcpy(saddr.sun_path, expected_sock_name);
381 : :
382 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
383 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
384 : 1 : expect_any(__wrap_socket, protocol);
385 : 1 : will_return(__wrap_socket, ipc_socket);
386 : :
387 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
388 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
389 : 1 : will_return(__wrap_connect, 0);
390 : :
391 : 1 : write_timeout.tv_sec = 30;
392 : 1 : write_timeout.tv_usec = 0;
393 : 1 : write_select_params.result = 1;
394 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
395 : 1 : expect_memory(__wrap_select, timeout, &write_timeout, sizeof(write_timeout));
396 : 1 : will_return(__wrap_select, &write_select_params);
397 : :
398 : 1 : write_message.type = TAPDISK_MESSAGE_CLOSE;
399 : 1 : write_message.cookie = test_minor;
400 : 1 : expect_value(__wrap_write, fd, ipc_socket);
401 : 1 : expect_memory(__wrap_write, buf, &write_message, sizeof(write_message));
402 : 1 : will_return(__wrap_write, 1024);
403 : :
404 : 1 : read_timeout.tv_sec = 30;
405 : 1 : read_timeout.tv_usec = 0;
406 : 1 : read_select_params.result = 1;
407 : 1 : expect_memory(__wrap_select, timeout, &read_timeout, sizeof(read_timeout));
408 : 1 : will_return(__wrap_select, &read_select_params);
409 : :
410 : 1 : expect_value(__wrap_close, fd, ipc_socket);
411 : 1 : will_return(__wrap_close, 0);
412 : :
413 : : /* Call test API */
414 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, &write_timeout);
415 : :
416 : 1 : assert_int_equal(-EIO, result);
417 : 1 : }
418 : :
419 : 1 : void test_tap_ctl_close_error_response(void **state)
420 : : {
421 : : int result;
422 : 1 : int test_pid = 1345;
423 : 1 : int test_minor = 17;
424 : 1 : int ipc_socket = 7;
425 : 1 : char *expected_sock_name = "/var/run/blktap-control/ctl1345";
426 : : struct mock_select_params write_select_params;
427 : : struct mock_select_params read_select_params;
428 : : struct mock_read_params read_params;
429 : : tapdisk_message_t write_message;
430 : : tapdisk_message_t read_message;
431 : : struct sockaddr_un saddr;
432 : :
433 : 1 : initialise_select_params(&write_select_params);
434 : 1 : initialise_select_params(&read_select_params);
435 : : memset(&read_message, 0, sizeof(read_message));
436 : : memset(&write_message, 0, sizeof(write_message));
437 : :
438 : : memset(&saddr, 0, sizeof(saddr));
439 : 1 : saddr.sun_family = AF_UNIX;
440 : : strcpy(saddr.sun_path, expected_sock_name);
441 : :
442 : 1 : expect_value(__wrap_socket, domain, AF_UNIX);
443 : 1 : expect_value(__wrap_socket, type, SOCK_STREAM);
444 : 1 : expect_any(__wrap_socket, protocol);
445 : 1 : will_return(__wrap_socket, ipc_socket);
446 : :
447 : 1 : expect_value(__wrap_connect, sockfd, ipc_socket);
448 : 1 : expect_memory(__wrap_connect, addr, &saddr, sizeof(saddr));
449 : 1 : will_return(__wrap_connect, 0);
450 : :
451 : 1 : write_select_params.result = 1;
452 : 1 : FD_SET(ipc_socket, &write_select_params.writefds);
453 : 1 : expect_value(__wrap_select, timeout, NULL);
454 : 1 : will_return(__wrap_select, &write_select_params);
455 : :
456 : 1 : write_message.type = TAPDISK_MESSAGE_CLOSE;
457 : 1 : write_message.cookie = test_minor;
458 : 1 : expect_value(__wrap_write, fd, ipc_socket);
459 : 1 : expect_memory(__wrap_write, buf, &write_message, sizeof(write_message));
460 : 1 : will_return(__wrap_write, 1024);
461 : :
462 : 1 : read_select_params.result = 1;
463 : 1 : FD_SET(ipc_socket, &read_select_params.readfds);
464 : 1 : expect_value(__wrap_select, timeout, NULL);
465 : 1 : will_return(__wrap_select, &read_select_params);
466 : :
467 : 1 : read_message.type = TAPDISK_MESSAGE_ERROR;
468 : 1 : read_message.cookie = test_minor;
469 : 1 : read_message.u.response.error = ENOENT;
470 : 1 : read_params.result = sizeof(read_message);
471 : 1 : read_params.data = &read_message;
472 : 1 : expect_value(__wrap_read, fd, ipc_socket);
473 : 1 : will_return(__wrap_read, &read_params);
474 : :
475 : 1 : expect_value(__wrap_close, fd, ipc_socket);
476 : 1 : will_return(__wrap_close, 0);
477 : :
478 : : /* Call test API */
479 : 1 : result = tap_ctl_close(test_pid, test_minor, 0, NULL);
480 : :
481 : 1 : assert_int_equal(-ENOENT, result);
482 : 1 : }
|