test_vcpu.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <test.h>
23 #include <vcpu/vcpu.h>
24 #include <debug_ring/debug_ring.h>
25 
26 void
27 vcpu_ut::test_vcpu_invalid_id()
28 {
29  this->expect_exception([&] { std::make_unique<vcpu>(vcpuid::reserved, nullptr); }, ""_ut_iae);
30 }
31 
32 void
33 vcpu_ut::test_vcpu_null_debug_ring()
34 {
35  this->expect_no_exception([&] { std::make_unique<vcpu>(0, nullptr); });
36 }
37 
38 void
39 vcpu_ut::test_vcpu_valid()
40 {
41  auto dr = std::unique_ptr<debug_ring>(nullptr);
42  this->expect_no_exception([&] { std::make_unique<vcpu>(0, std::move(dr)); });
43 }
44 
45 void
46 vcpu_ut::test_vcpu_write_empty_string()
47 {
48  char rb[DEBUG_RING_SIZE];
49  debug_ring_resources_t *drr = nullptr;
50  auto &&vc = std::make_unique<vcpu>(0);
51 
52  vc->write("");
53  get_drr(0, &drr);
54 
55  this->expect_true(debug_ring_read(drr, static_cast<char *>(rb), DEBUG_RING_SIZE) == 0);
56 }
57 
58 void
59 vcpu_ut::test_vcpu_write_hello_world()
60 {
61  char rb[DEBUG_RING_SIZE];
62  debug_ring_resources_t *drr = nullptr;
63  auto &&vc = std::make_unique<vcpu>(0);
64 
65  vc->write("hello world");
66  get_drr(0, &drr);
67 
68  this->expect_true(debug_ring_read(drr, static_cast<char *>(rb), DEBUG_RING_SIZE) == 11);
69 }
70 
71 void
72 vcpu_ut::test_vcpu_init_null_attr()
73 {
74  auto &&vc = std::make_unique<vcpu>(0);
75 
76  this->expect_false(vc->is_initialized());
77  vc->init(nullptr);
78  this->expect_true(vc->is_initialized());
79 }
80 
81 void
82 vcpu_ut::test_vcpu_init_valid_attr()
83 {
84  user_data data{};
85  auto &&vc = std::make_unique<vcpu>(0);
86 
87  this->expect_false(vc->is_initialized());
88  vc->init(&data);
89  this->expect_true(vc->is_initialized());
90 }
91 
92 void
93 vcpu_ut::test_vcpu_fini_null_attr()
94 {
95  auto &&vc = std::make_unique<vcpu>(0);
96 
97  vc->init();
98 
99  this->expect_true(vc->is_initialized());
100  vc->fini(nullptr);
101  this->expect_false(vc->is_initialized());
102 }
103 
104 void
105 vcpu_ut::test_vcpu_fini_valid_attr()
106 {
107  user_data data{};
108  auto &&vc = std::make_unique<vcpu>(0);
109 
110  vc->init();
111 
112  this->expect_true(vc->is_initialized());
113  vc->fini(&data);
114  this->expect_false(vc->is_initialized());
115 }
116 
117 void
118 vcpu_ut::test_vcpu_fini_without_init_without_run()
119 {
120  auto &&vc = std::make_unique<vcpu>(0);
121 
122  this->expect_false(vc->is_running());
123  this->expect_false(vc->is_initialized());
124  vc->fini();
125  this->expect_false(vc->is_running());
126  this->expect_false(vc->is_initialized());
127 }
128 
129 void
130 vcpu_ut::test_vcpu_fini_with_init_without_run()
131 {
132  auto &&vc = std::make_unique<vcpu>(0);
133 
134  vc->init();
135 
136  this->expect_false(vc->is_running());
137  this->expect_true(vc->is_initialized());
138  vc->fini();
139  this->expect_false(vc->is_running());
140  this->expect_false(vc->is_initialized());
141 }
142 
143 void
144 vcpu_ut::test_vcpu_fini_without_init_with_run()
145 {
146  auto &&vc = std::make_unique<vcpu>(0);
147 
148  vc->run();
149 
150  this->expect_true(vc->is_running());
151  this->expect_false(vc->is_initialized());
152  vc->fini();
153  this->expect_false(vc->is_running());
154  this->expect_false(vc->is_initialized());
155 }
156 
157 void
158 vcpu_ut::test_vcpu_fini_with_init_with_run()
159 {
160  auto &&vc = std::make_unique<vcpu>(0);
161 
162  vc->init();
163  vc->run();
164 
165  this->expect_true(vc->is_running());
166  this->expect_true(vc->is_initialized());
167  vc->fini();
168  this->expect_false(vc->is_running());
169  this->expect_false(vc->is_initialized());
170 }
171 
172 void
173 vcpu_ut::test_vcpu_run_null_attr()
174 {
175  auto &&vc = std::make_unique<vcpu>(0);
176 
177  this->expect_false(vc->is_running());
178  vc->run(nullptr);
179  this->expect_true(vc->is_running());
180 }
181 
182 void
183 vcpu_ut::test_vcpu_run_valid_attr()
184 {
185  user_data data{};
186  auto &&vc = std::make_unique<vcpu>(0);
187 
188  this->expect_false(vc->is_running());
189  vc->run(&data);
190  this->expect_true(vc->is_running());
191 }
192 
193 void
194 vcpu_ut::test_vcpu_run_without_init()
195 {
196  auto &&vc = std::make_unique<vcpu>(0);
197 
198  this->expect_false(vc->is_running());
199  vc->run();
200  this->expect_true(vc->is_running());
201 }
202 
203 void
204 vcpu_ut::test_vcpu_run_with_init()
205 {
206  auto &&vc = std::make_unique<vcpu>(0);
207 
208  vc->init();
209 
210  this->expect_false(vc->is_running());
211  vc->run();
212  this->expect_true(vc->is_running());
213 }
214 
215 void
216 vcpu_ut::test_vcpu_hlt_null_attr()
217 {
218  auto &&vc = std::make_unique<vcpu>(0);
219 
220  this->expect_false(vc->is_running());
221  vc->hlt(nullptr);
222  this->expect_false(vc->is_running());
223 }
224 
225 void
226 vcpu_ut::test_vcpu_hlt_valid_attr()
227 {
228  user_data data{};
229  auto &&vc = std::make_unique<vcpu>(0);
230 
231  this->expect_false(vc->is_running());
232  vc->hlt(&data);
233  this->expect_false(vc->is_running());
234 }
235 
236 void
237 vcpu_ut::test_vcpu_hlt_without_run()
238 {
239  auto &&vc = std::make_unique<vcpu>(0);
240 
241  this->expect_false(vc->is_running());
242  vc->hlt();
243  this->expect_false(vc->is_running());
244 }
245 
246 void
247 vcpu_ut::test_vcpu_hlt_with_run()
248 {
249  auto &&vc = std::make_unique<vcpu>(0);
250 
251  vc->run();
252 
253  this->expect_true(vc->is_running());
254  vc->hlt();
255  this->expect_false(vc->is_running());
256 }
257 
258 void
259 vcpu_ut::test_vcpu_id()
260 {
261  auto vc = std::make_unique<vcpu>(1);
262  this->expect_true(vc->id() == 1);
263 }
264 
265 void
266 vcpu_ut::test_vcpu_is_bootstrap_vcpu()
267 {
268  auto &&vc = std::make_unique<vcpu>(0);
269  this->expect_true(vc->is_bootstrap_vcpu());
270 }
271 
272 void
273 vcpu_ut::test_vcpu_is_not_bootstrap_vcpu()
274 {
275  auto vc = std::make_unique<vcpu>(1);
276  this->expect_false(vc->is_bootstrap_vcpu());
277 }
278 
279 void
280 vcpu_ut::test_vcpu_is_host_vm_vcpu()
281 {
282  auto vc = std::make_unique<vcpu>(1);
283  this->expect_true(vc->is_host_vm_vcpu());
284 }
285 
286 void
287 vcpu_ut::test_vcpu_is_not_host_vm_vcpu()
288 {
289  auto vc = std::make_unique<vcpu>(0x0000000100000000);
290  this->expect_false(vc->is_host_vm_vcpu());
291 }
292 
293 void
294 vcpu_ut::test_vcpu_is_guest_vm_vcpu()
295 {
296  auto vc = std::make_unique<vcpu>(0x0000000100000000);
297  this->expect_true(vc->is_guest_vm_vcpu());
298 }
299 
300 void
301 vcpu_ut::test_vcpu_is_not_guest_vm_vcpu()
302 {
303  auto vc = std::make_unique<vcpu>(1);
304  this->expect_false(vc->is_guest_vm_vcpu());
305 }
306 
307 void
308 vcpu_ut::test_vcpu_is_running_vm_vcpu()
309 {
310  auto &&vc = std::make_unique<vcpu>(0);
311 
312  vc->run();
313  this->expect_true(vc->is_running());
314 }
315 
316 void
317 vcpu_ut::test_vcpu_is_not_running_vm_vcpu()
318 {
319  auto &&vc = std::make_unique<vcpu>(0);
320  this->expect_false(vc->is_running());
321 }
322 
323 void
324 vcpu_ut::test_vcpu_is_initialized_vm_vcpu()
325 {
326  auto &&vc = std::make_unique<vcpu>(0);
327 
328  vc->init();
329  this->expect_true(vc->is_initialized());
330 }
331 
332 void
333 vcpu_ut::test_vcpu_is_not_initialized_vm_vcpu()
334 {
335  auto &&vc = std::make_unique<vcpu>(0);
336  this->expect_false(vc->is_initialized());
337 }
#define expect_exception(f, e)
Definition: unittest.h:162
int64_t get_drr(uint64_t vcpuid, struct debug_ring_resources_t **drr)
int64_t unsigned long void * data
#define expect_no_exception(f)
Definition: unittest.h:198
uint64_t debug_ring_read(struct debug_ring_resources_t *drr, char *str, uint64_t len)
debug_ring_resources_t * drr
constexpr const auto reserved
Definition: vcpuid.h:33
#define DEBUG_RING_SIZE
Definition: constants.h:206
debug_ring * dr
#define expect_false(a)
char rb[DEBUG_RING_SIZE]
#define expect_true(a)