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 : : #include <sys/file.h>
37 : :
38 : : #include <wrappers.h>
39 : : #include "control-wrappers.h"
40 : : #include "test-suites.h"
41 : :
42 : : #include "tap-ctl.h"
43 : : #include "blktap.h"
44 : :
45 : 1 : void test_tap_ctl_free_open_fail(void **state)
46 : : {
47 : 1 : int dev_fd = -1;
48 : : int result;
49 : :
50 : 1 : will_return(__wrap_open, dev_fd);
51 : 1 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
52 : :
53 : 1 : result = tap_ctl_free(0);
54 : :
55 : 1 : assert_int_equal(result, -ENOENT);
56 : 1 : }
57 : :
58 : 1 : void test_tap_ctl_free_success(void **state)
59 : : {
60 : 1 : int dev_fd = 12;
61 : 1 : int marker_fd = 13;
62 : : int result;
63 : :
64 : 1 : will_return(__wrap_open, dev_fd);
65 : 1 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
66 : :
67 : 1 : will_return(__wrap_flock, 0);
68 : 1 : expect_value(__wrap_flock, fd, dev_fd);
69 : 1 : expect_value(__wrap_flock, operation, LOCK_EX);
70 : :
71 : : /* Open and lock, non-blocking, the marker file */
72 : 1 : will_return(__wrap_open, marker_fd);
73 : 1 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
74 : :
75 : 1 : will_return(__wrap_flock, 0);
76 : 1 : expect_value(__wrap_flock, fd, marker_fd);
77 : 1 : expect_value(__wrap_flock, operation, LOCK_EX | LOCK_NB);
78 : :
79 : 1 : will_return(__wrap_unlink, 0);
80 : 1 : expect_string(__wrap_unlink, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
81 : :
82 : 1 : will_return(__wrap_flock, 0);
83 : 1 : expect_value(__wrap_flock, fd, marker_fd);
84 : 1 : expect_value(__wrap_flock, operation, LOCK_UN);
85 : :
86 : 1 : will_return(__wrap_close, 0);
87 : 1 : expect_value(__wrap_close, fd, marker_fd);
88 : :
89 : 1 : will_return(__wrap_flock, 0);
90 : 1 : expect_value(__wrap_flock, fd, dev_fd);
91 : 1 : expect_value(__wrap_flock, operation, LOCK_UN);
92 : :
93 : 1 : will_return(__wrap_close, 0);
94 : 1 : expect_value(__wrap_close, fd, dev_fd);
95 : :
96 : 1 : result = tap_ctl_free(0);
97 : :
98 : 1 : assert_int_equal(result, 0);
99 : 1 : }
100 : :
101 : 1 : void test_tap_ctl_free_locked(void **state)
102 : : {
103 : 1 : int dev_fd = 12;
104 : 1 : int marker_fd = 13;
105 : : int result;
106 : :
107 : 1 : will_return(__wrap_open, dev_fd);
108 : 1 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
109 : :
110 : 1 : will_return(__wrap_flock, 0);
111 : 1 : expect_value(__wrap_flock, fd, dev_fd);
112 : 1 : expect_value(__wrap_flock, operation, LOCK_EX);
113 : :
114 : : /* Open and lock, non-blocking, the marker file */
115 : 1 : will_return(__wrap_open, marker_fd);
116 : 1 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
117 : :
118 : 1 : will_return(__wrap_flock, EAGAIN);
119 : 1 : expect_value(__wrap_flock, fd, marker_fd);
120 : 1 : expect_value(__wrap_flock, operation, LOCK_EX | LOCK_NB);
121 : :
122 : 1 : will_return(__wrap_flock, marker_fd);
123 : 1 : expect_value(__wrap_flock, fd, marker_fd);
124 : 1 : expect_value(__wrap_flock, operation, LOCK_UN);
125 : :
126 : 1 : will_return(__wrap_close, 0);
127 : 1 : expect_value(__wrap_close, fd, marker_fd);
128 : :
129 : 1 : will_return(__wrap_flock, 0);
130 : 1 : expect_value(__wrap_flock, fd, dev_fd);
131 : 1 : expect_value(__wrap_flock, operation, LOCK_UN);
132 : :
133 : 1 : will_return(__wrap_close, 0);
134 : 1 : expect_value(__wrap_close, fd, dev_fd);
135 : :
136 : 1 : result = tap_ctl_free(0);
137 : :
138 : 1 : assert_int_equal(result, -EAGAIN);
139 : 1 : }
140 : :
|