test_vcpu_manager.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 <vcpu/vcpu_manager.h>
25 
26 extern bool make_vcpu_throws;
27 extern vcpu *g_vcpu;
28 
29 void
30 vcpu_ut::test_vcpu_manager_create_valid()
31 {
32  MockRepository mocks;
33  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
34 
35  mocks.OnCall(g_vcpu, vcpu::init);
36  mocks.OnCall(g_vcpu, vcpu::fini);
37 
38  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
39  {
40  this->expect_no_exception([&] { g_vcm->create_vcpu(0); });
41  g_vcm->delete_vcpu(0);
42  });
43 
44  g_vcpu = nullptr;
45 }
46 
47 void
48 vcpu_ut::test_vcpu_manager_create_valid_twice_overwrites()
49 {
50  MockRepository mocks;
51  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
52 
53  mocks.OnCall(g_vcpu, vcpu::init);
54  mocks.OnCall(g_vcpu, vcpu::fini);
55 
56  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
57  {
58  this->expect_no_exception([&] { g_vcm->create_vcpu(0); });
59  this->expect_no_exception([&] { g_vcm->create_vcpu(0); });
60  g_vcm->delete_vcpu(0);
61  });
62 
63  g_vcpu = nullptr;
64 }
65 
66 void
67 vcpu_ut::test_vcpu_manager_create_make_vcpu_returns_null()
68 {
69  this->expect_exception([&] { g_vcm->create_vcpu(0); }, ""_ut_ree);
70 }
71 
72 void
73 vcpu_ut::test_vcpu_manager_create_make_vcpu_throws()
74 {
75  make_vcpu_throws = true;
76  this->expect_exception([&] { g_vcm->create_vcpu(0); }, ""_ut_ree);
77  make_vcpu_throws = false;
78 }
79 
80 void
81 vcpu_ut::test_vcpu_manager_create_init_throws()
82 {
83  MockRepository mocks;
84  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
85 
86  mocks.OnCall(g_vcpu, vcpu::init).Throw(std::runtime_error("error"));
87  mocks.OnCall(g_vcpu, vcpu::fini);
88 
89  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
90  {
91  this->expect_exception([&] { g_vcm->create_vcpu(0); }, ""_ut_ree);
92  g_vcm->delete_vcpu(0);
93  });
94 
95  g_vcpu = nullptr;
96 }
97 
98 void
99 vcpu_ut::test_vcpu_manager_delete_valid()
100 {
101  MockRepository mocks;
102  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
103 
104  mocks.OnCall(g_vcpu, vcpu::init);
105  mocks.OnCall(g_vcpu, vcpu::fini);
106 
107  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
108  {
109  g_vcm->create_vcpu(0);
110  this->expect_no_exception([&] { g_vcm->delete_vcpu(0); });
111  });
112 
113  g_vcpu = nullptr;
114 }
115 
116 void
117 vcpu_ut::test_vcpu_manager_delete_valid_twice()
118 {
119  MockRepository mocks;
120  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
121 
122  mocks.OnCall(g_vcpu, vcpu::init);
123  mocks.OnCall(g_vcpu, vcpu::fini);
124 
125  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
126  {
127  g_vcm->create_vcpu(0);
128  this->expect_no_exception([&] { g_vcm->delete_vcpu(0); });
129  this->expect_no_exception([&] { g_vcm->delete_vcpu(0); });
130  });
131 
132  g_vcpu = nullptr;
133 }
134 
135 void
136 vcpu_ut::test_vcpu_manager_delete_no_create()
137 {
138  MockRepository mocks;
139  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
140 
141  mocks.OnCall(g_vcpu, vcpu::init);
142  mocks.OnCall(g_vcpu, vcpu::fini);
143 
144  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
145  {
146  this->expect_no_exception([&] { g_vcm->delete_vcpu(0); });
147  });
148 
149  g_vcpu = nullptr;
150 }
151 
152 void
153 vcpu_ut::test_vcpu_manager_delete_fini_throws()
154 {
155  MockRepository mocks;
156  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
157 
158  mocks.OnCall(g_vcpu, vcpu::init);
159  mocks.OnCall(g_vcpu, vcpu::fini).Throw(std::runtime_error("error"));
160 
161  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
162  {
163  g_vcm->create_vcpu(0);
164  this->expect_exception([&] { g_vcm->delete_vcpu(0); }, ""_ut_ree);
165  });
166 
167  g_vcpu = nullptr;
168 }
169 
170 void
171 vcpu_ut::test_vcpu_manager_run_valid()
172 {
173  MockRepository mocks;
174  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
175 
176  mocks.OnCall(g_vcpu, vcpu::init);
177  mocks.OnCall(g_vcpu, vcpu::fini);
178  mocks.OnCall(g_vcpu, vcpu::run);
179  mocks.OnCall(g_vcpu, vcpu::hlt);
180  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
181 
182  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
183  {
184  g_vcm->create_vcpu(0);
185  this->expect_no_exception([&] { g_vcm->run_vcpu(0); });
186  g_vcm->delete_vcpu(0);
187  });
188 
189  g_vcpu = nullptr;
190 }
191 
192 void
193 vcpu_ut::test_vcpu_manager_run_valid_twice()
194 {
195  MockRepository mocks;
196  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
197 
198  mocks.OnCall(g_vcpu, vcpu::init);
199  mocks.OnCall(g_vcpu, vcpu::fini);
200  mocks.OnCall(g_vcpu, vcpu::run);
201  mocks.OnCall(g_vcpu, vcpu::hlt);
202  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
203 
204  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
205  {
206  g_vcm->create_vcpu(0);
207  this->expect_no_exception([&] { g_vcm->run_vcpu(0); });
208  this->expect_no_exception([&] { g_vcm->run_vcpu(0); });
209  g_vcm->delete_vcpu(0);
210  });
211 
212  g_vcpu = nullptr;
213 }
214 
215 void
216 vcpu_ut::test_vcpu_manager_run_run_throws()
217 {
218  MockRepository mocks;
219  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
220 
221  mocks.OnCall(g_vcpu, vcpu::init);
222  mocks.OnCall(g_vcpu, vcpu::fini);
223  mocks.OnCall(g_vcpu, vcpu::run).Throw(std::runtime_error("error"));
224  mocks.OnCall(g_vcpu, vcpu::hlt);
225  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
226 
227  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
228  {
229  g_vcm->create_vcpu(0);
230  this->expect_exception([&] { g_vcm->run_vcpu(0); }, ""_ut_ree);
231  g_vcm->delete_vcpu(0);
232  });
233 
234  g_vcpu = nullptr;
235 }
236 
237 void
238 vcpu_ut::test_vcpu_manager_run_hlt_throws()
239 {
240  MockRepository mocks;
241  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
242 
243  mocks.OnCall(g_vcpu, vcpu::init);
244  mocks.OnCall(g_vcpu, vcpu::fini);
245  mocks.OnCall(g_vcpu, vcpu::run).Throw(std::runtime_error("error"));
246  mocks.OnCall(g_vcpu, vcpu::hlt).Throw(std::logic_error("error"));
247  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
248 
249  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
250  {
251  g_vcm->create_vcpu(0);
252  this->expect_exception([&] { g_vcm->run_vcpu(0); }, ""_ut_ree);
253  g_vcm->delete_vcpu(0);
254  });
255 
256  g_vcpu = nullptr;
257 }
258 
259 void
260 vcpu_ut::test_vcpu_manager_run_no_create()
261 {
262  MockRepository mocks;
263  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
264 
265  mocks.OnCall(g_vcpu, vcpu::init);
266  mocks.OnCall(g_vcpu, vcpu::fini);
267  mocks.OnCall(g_vcpu, vcpu::run);
268  mocks.OnCall(g_vcpu, vcpu::hlt);
269  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
270 
271  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
272  {
273  this->expect_no_exception([&] { g_vcm->run_vcpu(0); });
274  });
275 
276  g_vcpu = nullptr;
277 }
278 
279 void
280 vcpu_ut::test_vcpu_manager_hlt_valid()
281 {
282  MockRepository mocks;
283  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
284 
285  mocks.OnCall(g_vcpu, vcpu::init);
286  mocks.OnCall(g_vcpu, vcpu::fini);
287  mocks.OnCall(g_vcpu, vcpu::run);
288  mocks.OnCall(g_vcpu, vcpu::hlt);
289  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
290 
291  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
292  {
293  g_vcm->create_vcpu(0);
294  g_vcm->run_vcpu(0);
295 
296  mocks.OnCall(g_vcpu, vcpu::is_running).Return(true);
297 
298  this->expect_no_exception([&] { g_vcm->hlt_vcpu(0); });
299  g_vcm->delete_vcpu(0);
300  });
301 
302  g_vcpu = nullptr;
303 }
304 
305 void
306 vcpu_ut::test_vcpu_manager_hlt_valid_twice()
307 {
308  MockRepository mocks;
309  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
310 
311  mocks.OnCall(g_vcpu, vcpu::init);
312  mocks.OnCall(g_vcpu, vcpu::fini);
313  mocks.OnCall(g_vcpu, vcpu::run);
314  mocks.OnCall(g_vcpu, vcpu::hlt);
315  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
316 
317  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
318  {
319  g_vcm->create_vcpu(0);
320  g_vcm->run_vcpu(0);
321 
322  mocks.OnCall(g_vcpu, vcpu::is_running).Return(true);
323 
324  this->expect_no_exception([&] { g_vcm->hlt_vcpu(0); });
325  this->expect_no_exception([&] { g_vcm->hlt_vcpu(0); });
326  g_vcm->delete_vcpu(0);
327  });
328 
329  g_vcpu = nullptr;
330 }
331 
332 void
333 vcpu_ut::test_vcpu_manager_hlt_hlt_throws()
334 {
335  MockRepository mocks;
336  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
337 
338  mocks.OnCall(g_vcpu, vcpu::init);
339  mocks.OnCall(g_vcpu, vcpu::fini);
340  mocks.OnCall(g_vcpu, vcpu::run);
341  mocks.OnCall(g_vcpu, vcpu::hlt).Throw(std::runtime_error("error"));
342  mocks.OnCall(g_vcpu, vcpu::is_running).Return(false);
343 
344  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
345  {
346  g_vcm->create_vcpu(0);
347  g_vcm->run_vcpu(0);
348 
349  mocks.OnCall(g_vcpu, vcpu::is_running).Return(true);
350 
351  this->expect_exception([&] { g_vcm->hlt_vcpu(0); }, ""_ut_ree);
352  g_vcm->delete_vcpu(0);
353  });
354 
355  g_vcpu = nullptr;
356 }
357 
358 void
359 vcpu_ut::test_vcpu_manager_hlt_no_create()
360 {
361  MockRepository mocks;
362  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
363 
364  mocks.OnCall(g_vcpu, vcpu::init);
365  mocks.OnCall(g_vcpu, vcpu::fini);
366  mocks.OnCall(g_vcpu, vcpu::run);
367  mocks.OnCall(g_vcpu, vcpu::hlt);
368  mocks.OnCall(g_vcpu, vcpu::is_running).Return(true);
369 
370  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
371  {
372  this->expect_no_exception([&] { g_vcm->hlt_vcpu(0); });
373  });
374 
375  g_vcpu = nullptr;
376 }
377 
378 void
379 vcpu_ut::test_vcpu_manager_write_null()
380 {
381  MockRepository mocks;
382  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
383 
384  mocks.OnCall(g_vcpu, vcpu::init);
385  mocks.OnCall(g_vcpu, vcpu::fini);
386  mocks.ExpectCall(g_vcpu, vcpu::write).With(""_s);
387 
388  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
389  {
390  g_vcm->create_vcpu(0);
391  g_vcm->write(0, "");
392  g_vcm->delete_vcpu(0);
393  });
394 
395  g_vcpu = nullptr;
396 }
397 
398 void
399 vcpu_ut::test_vcpu_manager_write_hello()
400 {
401  MockRepository mocks;
402  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
403 
404  mocks.OnCall(g_vcpu, vcpu::init);
405  mocks.OnCall(g_vcpu, vcpu::fini);
406  mocks.ExpectCall(g_vcpu, vcpu::write).With("hello"_s);
407 
408  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
409  {
410  g_vcm->create_vcpu(0);
411  g_vcm->write(0, "hello");
412  g_vcm->delete_vcpu(0);
413  });
414 
415  g_vcpu = nullptr;
416 }
417 
418 void
419 vcpu_ut::test_vcpu_manager_write_no_create()
420 {
421  MockRepository mocks;
422  g_vcpu = bfn::mock_no_delete<vcpu>(mocks);
423 
424  mocks.OnCall(g_vcpu, vcpu::init);
425  mocks.OnCall(g_vcpu, vcpu::fini);
426  mocks.NeverCall(g_vcpu, vcpu::write);
427 
428  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
429  {
430  g_vcm->write(0, "hello");
431  });
432 
433  g_vcpu = nullptr;
434 }
#define expect_exception(f, e)
Definition: unittest.h:162
virtual bool is_running()
Definition: vcpu.h:219
#define RUN_UNITTEST_WITH_MOCKS(a, b)
Definition: unittest.h:229
#define expect_no_exception(f)
Definition: unittest.h:198
virtual void run(user_data *data=nullptr)
Definition: vcpu.cpp:58
Definition: vcpu.h:94
virtual void init(user_data *data=nullptr)
Definition: vcpu.cpp:39
bool make_vcpu_throws
Definition: test.cpp:29
virtual void hlt(user_data *data=nullptr)
Definition: vcpu.cpp:66
virtual void write(const std::string &str) noexcept
Definition: vcpu.cpp:74
virtual void fini(user_data *data=nullptr)
Definition: vcpu.cpp:47
#define g_vcm
Definition: vcpu_manager.h:155
vcpu * g_vcpu
Definition: test.cpp:30