test_entry.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 <entry/entry.h>
24 #include <vcpu/vcpu_manager.h>
25 
26 #include <memory.h>
27 #include <eh_frame_list.h>
28 
29 void
30 entry_ut::test_start_vmm_success()
31 {
32  MockRepository mocks;
33  auto vcm = mocks.Mock<vcpu_manager>();
34  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
35 
36  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
37  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
38  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
39  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
40 
41  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
42  {
43  this->expect_no_exception([&] { start_vmm(0); });
44  });
45 }
46 
47 void
48 entry_ut::test_start_vmm_throws_general_exception()
49 {
50  MockRepository mocks;
51  auto vcm = mocks.Mock<vcpu_manager>();
52  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
53 
54  mocks.OnCall(vcm, vcpu_manager::create_vcpu).Throw(bfn::general_exception());
55  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
56  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
57  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
58 
59  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
60  {
61  this->expect_no_exception([&] { start_vmm(0); });
62  });
63 }
64 
65 void
66 entry_ut::test_start_vmm_throws_standard_exception()
67 {
68  MockRepository mocks;
69  auto vcm = mocks.Mock<vcpu_manager>();
70  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
71 
72  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
73  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
74  mocks.OnCall(vcm, vcpu_manager::run_vcpu).Throw(std::exception());
75  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
76 
77  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
78  {
79  this->expect_no_exception([&] { start_vmm(0); });
80  });
81 }
82 
83 void
84 entry_ut::test_start_vmm_throws_bad_alloc()
85 {
86  MockRepository mocks;
87  auto vcm = mocks.Mock<vcpu_manager>();
88  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
89 
90  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
91  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
92  mocks.OnCall(vcm, vcpu_manager::run_vcpu).Throw(std::bad_alloc());
93  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
94 
95  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
96  {
97  this->expect_no_exception([&] { start_vmm(0); });
98  });
99 }
100 
101 void
102 entry_ut::test_start_vmm_throws_any_exception()
103 {
104  MockRepository mocks;
105  auto vcm = mocks.Mock<vcpu_manager>();
106  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
107 
108  mocks.OnCall(vcm, vcpu_manager::create_vcpu).Throw(10);
109  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
110  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
111  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
112 
113  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
114  {
115  this->expect_no_exception([&] { start_vmm(0); });
116  });
117 }
118 
119 void
120 entry_ut::test_stop_vmm_success()
121 {
122  MockRepository mocks;
123  auto vcm = mocks.Mock<vcpu_manager>();
124  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
125 
126  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
127  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
128  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
129  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
130 
131  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
132  {
133  this->expect_no_exception([&] { stop_vmm(0); });
134  });
135 }
136 
137 void
138 entry_ut::test_stop_vmm_throws_general_exception()
139 {
140  MockRepository mocks;
141  auto vcm = mocks.Mock<vcpu_manager>();
142  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
143 
144  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
145  mocks.OnCall(vcm, vcpu_manager::delete_vcpu).Throw(bfn::general_exception());
146  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
147  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
148 
149  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
150  {
151  this->expect_no_exception([&] { stop_vmm(0); });
152  });
153 }
154 
155 void
156 entry_ut::test_stop_vmm_throws_standard_exception()
157 {
158  MockRepository mocks;
159  auto vcm = mocks.Mock<vcpu_manager>();
160  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
161 
162  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
163  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
164  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
165  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu).Throw(std::exception());
166 
167  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
168  {
169  this->expect_no_exception([&] { stop_vmm(0); });
170  });
171 }
172 
173 void
174 entry_ut::test_stop_vmm_throws_bad_alloc()
175 {
176  MockRepository mocks;
177  auto vcm = mocks.Mock<vcpu_manager>();
178  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
179 
180  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
181  mocks.OnCall(vcm, vcpu_manager::delete_vcpu);
182  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
183  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu).Throw(std::bad_alloc());
184 
185  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
186  {
187  this->expect_no_exception([&] { stop_vmm(0); });
188  });
189 }
190 
191 void
192 entry_ut::test_stop_vmm_throws_any_exception()
193 {
194  MockRepository mocks;
195  auto vcm = mocks.Mock<vcpu_manager>();
196  mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);
197 
198  mocks.OnCall(vcm, vcpu_manager::create_vcpu);
199  mocks.OnCall(vcm, vcpu_manager::delete_vcpu).Throw(10);
200  mocks.OnCall(vcm, vcpu_manager::run_vcpu);
201  mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
202 
203  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
204  {
205  this->expect_no_exception([&] { stop_vmm(0); });
206  });
207 }
virtual void run_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
#define RUN_UNITTEST_WITH_MOCKS(a, b)
Definition: unittest.h:229
int64_t stop_vmm(uint64_t arg)
#define expect_no_exception(f)
Definition: unittest.h:198
virtual void create_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
int64_t start_vmm(uint64_t arg)
virtual void hlt_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
virtual void delete_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
static vcpu_manager * instance() noexcept