test.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 <new_delete.h>
24 
25 #include <vcpu/vcpu.h>
26 #include <vcpu/vcpu_factory.h>
27 #include <vcpu/vcpu_manager.h>
28 
29 bool make_vcpu_throws = false;
30 vcpu *g_vcpu = nullptr;
31 
32 class vcpu_factory_ut : public vcpu_factory
33 {
34 public:
35 
36  vcpu_factory_ut() noexcept = default;
37  ~vcpu_factory_ut() override = default;
38 
39  std::unique_ptr<vcpu> make_vcpu(vcpuid::type id, user_data *data) override
40  {
41  (void) id;
42  (void) data;
43 
44  if (make_vcpu_throws)
45  throw std::runtime_error("error");
46 
47  return std::unique_ptr<vcpu>(g_vcpu);
48  }
49 };
50 
52 {
53 }
54 
55 bool
57 {
58  g_vcm->set_factory(nullptr);
59  this->expect_exception([&] { g_vcm->create_vcpu(0); }, ""_ut_ree);
60 
61  g_vcm->set_factory(std::make_unique<vcpu_factory_ut>());
62 
63  return true;
64 }
65 
66 bool
68 {
69  return true;
70 }
71 
72 bool
74 {
75  this->test_vcpu_invalid_id();
76  this->test_vcpu_null_debug_ring();
77  this->test_vcpu_valid();
78  this->test_vcpu_write_empty_string();
79  this->test_vcpu_write_hello_world();
80  this->test_vcpu_init_null_attr();
81  this->test_vcpu_init_valid_attr();
82  this->test_vcpu_fini_null_attr();
83  this->test_vcpu_fini_valid_attr();
84  this->test_vcpu_fini_without_init_without_run();
85  this->test_vcpu_fini_with_init_without_run();
86  this->test_vcpu_fini_without_init_with_run();
87  this->test_vcpu_fini_with_init_with_run();
88  this->test_vcpu_run_null_attr();
89  this->test_vcpu_run_valid_attr();
90  this->test_vcpu_run_without_init();
91  this->test_vcpu_run_with_init();
92  this->test_vcpu_hlt_null_attr();
93  this->test_vcpu_hlt_valid_attr();
94  this->test_vcpu_hlt_without_run();
95  this->test_vcpu_hlt_with_run();
96  this->test_vcpu_id();
97  this->test_vcpu_is_bootstrap_vcpu();
98  this->test_vcpu_is_not_bootstrap_vcpu();
99  this->test_vcpu_is_host_vm_vcpu();
100  this->test_vcpu_is_not_host_vm_vcpu();
101  this->test_vcpu_is_guest_vm_vcpu();
102  this->test_vcpu_is_not_guest_vm_vcpu();
103  this->test_vcpu_is_running_vm_vcpu();
104  this->test_vcpu_is_not_running_vm_vcpu();
105  this->test_vcpu_is_initialized_vm_vcpu();
106  this->test_vcpu_is_not_initialized_vm_vcpu();
107 
108  this->test_vcpu_intel_x64_invalid_id();
109  this->test_vcpu_intel_x64_valid();
110  this->test_vcpu_intel_x64_init_null_params();
111  this->test_vcpu_intel_x64_init_valid_params();
112  this->test_vcpu_intel_x64_init_valid();
113  this->test_vcpu_intel_x64_init_vmcs_throws();
114  this->test_vcpu_intel_x64_fini_null_params();
115  this->test_vcpu_intel_x64_fini_valid_params();
116  this->test_vcpu_intel_x64_fini_valid();
117  this->test_vcpu_intel_x64_fini_no_init();
118  this->test_vcpu_intel_x64_run_launch();
119  this->test_vcpu_intel_x64_run_launch_is_host_vcpu();
120  this->test_vcpu_intel_x64_run_resume();
121  this->test_vcpu_intel_x64_run_no_init();
122  this->test_vcpu_intel_x64_run_vmxon_throws();
123  this->test_vcpu_intel_x64_run_vmcs_throws();
124  this->test_vcpu_intel_x64_hlt_no_init();
125  this->test_vcpu_intel_x64_hlt_no_run();
126  this->test_vcpu_intel_x64_hlt_valid();
127  this->test_vcpu_intel_x64_hlt_valid_is_host_vcpu();
128  this->test_vcpu_intel_x64_hlt_vmxon_throws();
129 
130  this->test_vcpu_manager_create_valid();
131  this->test_vcpu_manager_create_valid_twice_overwrites();
132  this->test_vcpu_manager_create_make_vcpu_returns_null();
133  this->test_vcpu_manager_create_make_vcpu_throws();
134  this->test_vcpu_manager_create_init_throws();
135  this->test_vcpu_manager_delete_valid();
136  this->test_vcpu_manager_delete_valid_twice();
137  this->test_vcpu_manager_delete_no_create();
138  this->test_vcpu_manager_delete_fini_throws();
139  this->test_vcpu_manager_run_valid();
140  this->test_vcpu_manager_run_valid_twice();
141  this->test_vcpu_manager_run_run_throws();
142  this->test_vcpu_manager_run_hlt_throws();
143  this->test_vcpu_manager_run_no_create();
144  this->test_vcpu_manager_hlt_valid();
145  this->test_vcpu_manager_hlt_valid_twice();
146  this->test_vcpu_manager_hlt_hlt_throws();
147  this->test_vcpu_manager_hlt_no_create();
148  this->test_vcpu_manager_write_null();
149  this->test_vcpu_manager_write_hello();
150  this->test_vcpu_manager_write_no_create();
151 
152  return true;
153 }
154 
155 int
156 main(int argc, char *argv[])
157 {
158  return RUN_ALL_TESTS(vcpu_ut);
159 }
#define expect_exception(f, e)
Definition: unittest.h:162
bool init() override
Definition: test.cpp:56
uint64_t type
Definition: vcpuid.h:31
int main(int argc, char *argv[])
Definition: test.cpp:221
bool fini() override
Definition: test.cpp:67
int64_t unsigned long void * data
Definition: test.h:27
bool make_vcpu_throws
Definition: test.cpp:29
Definition: vcpu.h:94
void uint64_t uint64_t uint64_t *rdx noexcept
#define RUN_ALL_TESTS(ut)
Definition: unittest.h:246
bool list() override
Definition: test.cpp:73
virtual std::unique_ptr< vcpu > make_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
vcpu_ut()
Definition: test.cpp:51
vcpu * g_vcpu
Definition: test.cpp:30
#define g_vcm
Definition: vcpu_manager.h:155