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 : : #include <sys/file.h>
38 : : #include <sys/stat.h>
39 : :
40 : : #include <wrappers.h>
41 : : #include "control-wrappers.h"
42 : : #include "test-suites.h"
43 : :
44 : : #include "tap-ctl.h"
45 : : #include "blktap.h"
46 : :
47 : :
48 : 0 : void test_tap_ctl_allocate_prep_dir_no_access(void **state)
49 : : {
50 : : int result;
51 : : int minor;
52 : : char *devname;
53 : :
54 : 0 : will_return(__wrap_access, ENOENT);
55 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control");
56 : 0 : will_return(__wrap_mkdir, EEXIST);
57 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
58 : 0 : will_return(__wrap_mkdir, EACCES);
59 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
60 : :
61 : 0 : result = tap_ctl_allocate(&minor, &devname);
62 : :
63 : 0 : assert_int_equal(-EACCES, result);
64 : 0 : }
65 : :
66 : 0 : void test_tap_ctl_allocate_prep_runtime_dir_no_access(void **state)
67 : : {
68 : : int result;
69 : : int minor;
70 : : char *devname;
71 : :
72 : 0 : will_return(__wrap_access, ENOENT);
73 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control");
74 : 0 : will_return(__wrap_mkdir, EEXIST);
75 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
76 : 0 : will_return(__wrap_mkdir, 0);
77 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
78 : :
79 : :
80 : 0 : will_return(__wrap_access, ENOENT);
81 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
82 : 0 : will_return(__wrap_mkdir, EEXIST);
83 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
84 : 0 : will_return(__wrap_mkdir, 0);
85 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
86 : 0 : will_return(__wrap_mkdir, EACCES);
87 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
88 : :
89 : 0 : result = tap_ctl_allocate(&minor, &devname);
90 : :
91 : 0 : assert_int_equal(-EACCES, result);
92 : 0 : }
93 : :
94 : 0 : void test_tap_ctl_allocate_first_success(void **state)
95 : : {
96 : 0 : int dev_fd = 12;
97 : : int result;
98 : : int minor;
99 : : char *devname;
100 : :
101 : 0 : will_return(__wrap_access, ENOENT);
102 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control");
103 : 0 : will_return(__wrap_mkdir, EEXIST);
104 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
105 : 0 : will_return(__wrap_mkdir, 0);
106 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
107 : :
108 : :
109 : 0 : will_return(__wrap_access, ENOENT);
110 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
111 : 0 : will_return(__wrap_mkdir, EEXIST);
112 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
113 : 0 : will_return(__wrap_mkdir, 0);
114 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
115 : 0 : will_return(__wrap_mkdir, 0);
116 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
117 : :
118 : 0 : will_return(__wrap_open, dev_fd);
119 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
120 : :
121 : 0 : will_return(__wrap_flock, 0);
122 : 0 : expect_value(__wrap_flock, fd, dev_fd);
123 : 0 : expect_value(__wrap_flock, operation, LOCK_EX);
124 : :
125 : 0 : will_return(__wrap_stat, -1);
126 : 0 : will_return(__wrap_stat, ENOENT);
127 : 0 : expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
128 : :
129 : 0 : will_return(__wrap_open, 13);
130 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
131 : :
132 : 0 : will_return(__wrap_close, 0);
133 : 0 : expect_value(__wrap_close, fd, 13);
134 : :
135 : 0 : will_return(__wrap_flock, 0);
136 : 0 : expect_value(__wrap_flock, fd, 12);
137 : 0 : expect_value(__wrap_flock, operation, LOCK_UN);
138 : :
139 : 0 : will_return(__wrap_close, 0);
140 : 0 : expect_value(__wrap_close, fd, 12);
141 : :
142 : 0 : result = tap_ctl_allocate(&minor, &devname);
143 : :
144 : 0 : assert_int_equal(0, result);
145 : 0 : }
146 : :
147 : 0 : void test_tap_ctl_allocate_create_failed(void **state)
148 : : {
149 : 0 : int dev_fd = 12;
150 : : int result;
151 : : int minor;
152 : : char *devname;
153 : :
154 : 0 : will_return(__wrap_access, ENOENT);
155 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control");
156 : 0 : will_return(__wrap_mkdir, EEXIST);
157 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
158 : 0 : will_return(__wrap_mkdir, 0);
159 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
160 : :
161 : :
162 : 0 : will_return(__wrap_access, ENOENT);
163 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
164 : 0 : will_return(__wrap_mkdir, EEXIST);
165 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
166 : 0 : will_return(__wrap_mkdir, 0);
167 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
168 : 0 : will_return(__wrap_mkdir, 0);
169 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
170 : :
171 : 0 : will_return(__wrap_open, dev_fd);
172 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
173 : :
174 : 0 : will_return(__wrap_flock, 0);
175 : 0 : expect_value(__wrap_flock, fd, dev_fd);
176 : 0 : expect_value(__wrap_flock, operation, LOCK_EX);
177 : :
178 : 0 : will_return(__wrap_stat, -1);
179 : 0 : will_return(__wrap_stat, ENOENT);
180 : 0 : expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
181 : :
182 : 0 : will_return(__wrap_open, -1);
183 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
184 : :
185 : 0 : will_return(__wrap_flock, 0);
186 : 0 : expect_value(__wrap_flock, fd, 12);
187 : 0 : expect_value(__wrap_flock, operation, LOCK_UN);
188 : :
189 : 0 : will_return(__wrap_close, 0);
190 : 0 : expect_value(__wrap_close, fd, 12);
191 : :
192 : 0 : result = tap_ctl_allocate(&minor, &devname);
193 : :
194 : 0 : assert_int_equal(-ENOENT, result);
195 : 0 : }
196 : :
197 : 0 : void test_tap_ctl_allocate_one_exists_success(void **state)
198 : : {
199 : 0 : int dev_fd = 12;
200 : : int result;
201 : : int minor;
202 : : char *devname;
203 : : struct stat st_buf;
204 : :
205 : 0 : will_return(__wrap_access, ENOENT);
206 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control");
207 : 0 : will_return(__wrap_mkdir, EEXIST);
208 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
209 : 0 : will_return(__wrap_mkdir, 0);
210 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
211 : :
212 : :
213 : 0 : will_return(__wrap_access, ENOENT);
214 : 0 : expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
215 : 0 : will_return(__wrap_mkdir, EEXIST);
216 : 0 : expect_string(__wrap_mkdir, pathname, "/run");
217 : 0 : will_return(__wrap_mkdir, 0);
218 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
219 : 0 : will_return(__wrap_mkdir, 0);
220 : 0 : expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
221 : :
222 : 0 : will_return(__wrap_open, dev_fd);
223 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
224 : :
225 : 0 : will_return(__wrap_flock, 0);
226 : 0 : expect_value(__wrap_flock, fd, dev_fd);
227 : 0 : expect_value(__wrap_flock, operation, LOCK_EX);
228 : :
229 : 0 : will_return(__wrap_stat, 0);
230 : 0 : will_return(__wrap_stat, &st_buf);
231 : 0 : expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
232 : :
233 : 0 : will_return(__wrap_stat, -1);
234 : 0 : will_return(__wrap_stat, ENOENT);
235 : 0 : expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-1");
236 : :
237 : 0 : will_return(__wrap_open, 13);
238 : 0 : expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-1");
239 : :
240 : 0 : will_return(__wrap_close, 0);
241 : 0 : expect_value(__wrap_close, fd, 13);
242 : :
243 : 0 : will_return(__wrap_flock, 0);
244 : 0 : expect_value(__wrap_flock, fd, 12);
245 : 0 : expect_value(__wrap_flock, operation, LOCK_UN);
246 : :
247 : 0 : will_return(__wrap_close, 0);
248 : 0 : expect_value(__wrap_close, fd, 12);
249 : :
250 : 0 : result = tap_ctl_allocate(&minor, &devname);
251 : :
252 : 0 : assert_int_equal(0, result);
253 : 0 : }
|