Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020, 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 <setjmp.h>
34 : : #include <cmocka.h>
35 : : #include <errno.h>
36 : :
37 : : #include <wrappers.h>
38 : : #include "control-wrappers.h"
39 : : #include "test-suites.h"
40 : :
41 : : #include "tap-ctl.h"
42 : : #include "blktap2.h"
43 : :
44 : 1 : void test_tap_ctl_free_open_fail(void **state)
45 : : {
46 : 1 : int dev_fd = -1;
47 : : int result;
48 : :
49 : 1 : will_return(__wrap_open, dev_fd);
50 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
51 : :
52 : 1 : result = tap_ctl_free(0);
53 : :
54 : 1 : assert_int_equal(result, ENOENT);
55 : 1 : }
56 : :
57 : 1 : void test_tap_ctl_free_success(void **state)
58 : : {
59 : 1 : int dev_fd = 12;
60 : : int result;
61 : :
62 : 1 : will_return(__wrap_open, dev_fd);
63 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
64 : :
65 : 1 : will_return(__wrap_ioctl, 0);
66 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
67 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_FREE_TAP);
68 : :
69 : 1 : will_return(__wrap_close, 0);
70 : 1 : expect_value(__wrap_close, fd, dev_fd);
71 : :
72 : 1 : result = tap_ctl_free(0);
73 : :
74 : 1 : assert_int_equal(result, 0);
75 : 1 : }
76 : :
77 : 1 : void test_tap_ctl_free_ioctl_busy(void **state)
78 : : {
79 : 1 : int dev_fd = 12;
80 : : int result;
81 : :
82 : 1 : will_return(__wrap_open, dev_fd);
83 : 1 : expect_string(__wrap_open, pathname, "/dev/xen/blktap-2/control");
84 : :
85 : 1 : will_return(__wrap_ioctl, EBUSY);
86 : 1 : expect_value(__wrap_ioctl, fd, dev_fd);
87 : 1 : expect_value(__wrap_ioctl, request, BLKTAP2_IOCTL_FREE_TAP);
88 : :
89 : 1 : will_return(__wrap_close, 0);
90 : 1 : expect_value(__wrap_close, fd, dev_fd);
91 : :
92 : 1 : result = tap_ctl_free(0);
93 : :
94 : 1 : assert_int_equal(result, -EBUSY);
95 : 1 : }
|