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 
24 #include <fstream>
25 #include <sys/mman.h>
26 
27 const auto c_dummy_add_md_failure_filename = "../cross/libdummy_add_md_failure.so";
28 const auto c_dummy_add_md_success_filename = "../cross/libdummy_add_md_success.so";
29 const auto c_dummy_get_drr_failure_filename = "../cross/libdummy_get_drr_failure.so";
30 const auto c_dummy_get_drr_success_filename = "../cross/libdummy_get_drr_success.so";
31 const auto c_dummy_misc_filename = "../cross/libdummy_misc.so";
32 const auto c_dummy_start_vmm_failure_filename = "../cross/libdummy_start_vmm_failure.so";
33 const auto c_dummy_start_vmm_success_filename = "../cross/libdummy_start_vmm_success.so";
34 const auto c_dummy_stop_vmm_failure_filename = "../cross/libdummy_stop_vmm_failure.so";
35 const auto c_dummy_stop_vmm_success_filename = "../cross/libdummy_stop_vmm_success.so";
36 
37 extern "C" int verify_no_mem_leaks(void);
38 
40  m_dummy_add_md_failure(nullptr),
41  m_dummy_add_md_success(nullptr),
42  m_dummy_get_drr_failure(nullptr),
43  m_dummy_get_drr_success(nullptr),
44  m_dummy_misc(nullptr),
45  m_dummy_start_vmm_failure(nullptr),
46  m_dummy_start_vmm_success(nullptr),
47  m_dummy_stop_vmm_failure(nullptr),
48  m_dummy_stop_vmm_success(nullptr),
49  m_dummy_add_md_failure_length(0),
50  m_dummy_add_md_success_length(0),
51  m_dummy_get_drr_failure_length(0),
52  m_dummy_get_drr_success_length(0),
53  m_dummy_misc_length(0),
54  m_dummy_start_vmm_failure_length(0),
55  m_dummy_start_vmm_success_length(0),
56  m_dummy_stop_vmm_failure_length(0),
57  m_dummy_stop_vmm_success_length(0)
58 {
59 }
60 
61 bool
63 {
64  std::ifstream dummy_add_md_failure_ifs(c_dummy_add_md_failure_filename, std::ifstream::ate);
65  std::ifstream dummy_add_md_success_ifs(c_dummy_add_md_success_filename, std::ifstream::ate);
66  std::ifstream dummy_get_drr_failure_ifs(c_dummy_get_drr_failure_filename, std::ifstream::ate);
67  std::ifstream dummy_get_drr_success_ifs(c_dummy_get_drr_success_filename, std::ifstream::ate);
68  std::ifstream dummy_misc_ifs(c_dummy_misc_filename, std::ifstream::ate);
69  std::ifstream dummy_start_vmm_failure_ifs(c_dummy_start_vmm_failure_filename, std::ifstream::ate);
70  std::ifstream dummy_start_vmm_success_ifs(c_dummy_start_vmm_success_filename, std::ifstream::ate);
71  std::ifstream dummy_stop_vmm_failure_ifs(c_dummy_stop_vmm_failure_filename, std::ifstream::ate);
72  std::ifstream dummy_stop_vmm_success_ifs(c_dummy_stop_vmm_success_filename, std::ifstream::ate);
73 
74  m_dummy_add_md_failure_length = static_cast<uint64_t>(dummy_add_md_failure_ifs.tellg());
75  m_dummy_add_md_success_length = static_cast<uint64_t>(dummy_add_md_success_ifs.tellg());
76  m_dummy_get_drr_failure_length = static_cast<uint64_t>(dummy_get_drr_failure_ifs.tellg());
77  m_dummy_get_drr_success_length = static_cast<uint64_t>(dummy_get_drr_success_ifs.tellg());
78  m_dummy_misc_length = static_cast<uint64_t>(dummy_misc_ifs.tellg());
79  m_dummy_start_vmm_failure_length = static_cast<uint64_t>(dummy_start_vmm_failure_ifs.tellg());
80  m_dummy_start_vmm_success_length = static_cast<uint64_t>(dummy_start_vmm_success_ifs.tellg());
81  m_dummy_stop_vmm_failure_length = static_cast<uint64_t>(dummy_stop_vmm_failure_ifs.tellg());
82  m_dummy_stop_vmm_success_length = static_cast<uint64_t>(dummy_stop_vmm_success_ifs.tellg());
83 
84  m_dummy_add_md_failure = std::make_unique<char[]>(m_dummy_add_md_failure_length);
85  m_dummy_add_md_success = std::make_unique<char[]>(m_dummy_add_md_success_length);
86  m_dummy_get_drr_failure = std::make_unique<char[]>(m_dummy_get_drr_failure_length);
87  m_dummy_get_drr_success = std::make_unique<char[]>(m_dummy_get_drr_success_length);
88  m_dummy_misc = std::make_unique<char[]>(m_dummy_misc_length);
89  m_dummy_start_vmm_failure = std::make_unique<char[]>(m_dummy_start_vmm_failure_length);
90  m_dummy_start_vmm_success = std::make_unique<char[]>(m_dummy_start_vmm_success_length);
91  m_dummy_stop_vmm_failure = std::make_unique<char[]>(m_dummy_stop_vmm_failure_length);
92  m_dummy_stop_vmm_success = std::make_unique<char[]>(m_dummy_stop_vmm_success_length);
93 
94  dummy_add_md_failure_ifs.seekg(0);
95  dummy_add_md_success_ifs.seekg(0);
96  dummy_get_drr_failure_ifs.seekg(0);
97  dummy_get_drr_success_ifs.seekg(0);
98  dummy_misc_ifs.seekg(0);
99  dummy_start_vmm_failure_ifs.seekg(0);
100  dummy_start_vmm_success_ifs.seekg(0);
101  dummy_stop_vmm_failure_ifs.seekg(0);
102  dummy_stop_vmm_success_ifs.seekg(0);
103 
104  dummy_add_md_failure_ifs.read(m_dummy_add_md_failure.get(), static_cast<int64_t>(m_dummy_add_md_failure_length));
105  dummy_add_md_success_ifs.read(m_dummy_add_md_success.get(), static_cast<int64_t>(m_dummy_add_md_success_length));
106  dummy_get_drr_failure_ifs.read(m_dummy_get_drr_failure.get(), static_cast<int64_t>(m_dummy_get_drr_failure_length));
107  dummy_get_drr_success_ifs.read(m_dummy_get_drr_success.get(), static_cast<int64_t>(m_dummy_get_drr_success_length));
108  dummy_misc_ifs.read(m_dummy_misc.get(), static_cast<int64_t>(m_dummy_misc_length));
109  dummy_start_vmm_failure_ifs.read(m_dummy_start_vmm_failure.get(), static_cast<int64_t>(m_dummy_start_vmm_failure_length));
110  dummy_start_vmm_success_ifs.read(m_dummy_start_vmm_success.get(), static_cast<int64_t>(m_dummy_start_vmm_success_length));
111  dummy_stop_vmm_failure_ifs.read(m_dummy_stop_vmm_failure.get(), static_cast<int64_t>(m_dummy_stop_vmm_failure_length));
112  dummy_stop_vmm_success_ifs.read(m_dummy_stop_vmm_success.get(), static_cast<int64_t>(m_dummy_stop_vmm_success_length));
113 
114  return true;
115 }
116 
117 bool
119 {
120  return true;
121 }
122 
123 bool
125 {
126  this->test_common_init();
127 
128  this->test_common_fini_unloaded();
129  this->test_common_fini_successful_start();
130  this->test_common_fini_successful_load();
131  this->test_common_fini_successful_add_module();
132  this->test_common_fini_corrupted();
133  this->test_common_fini_failed_load();
134  this->test_common_fini_failed_start();
135  this->test_common_fini_unload_failed();
136  this->test_common_fini_stop_failed();
137  this->test_common_fini_reset_failed();
138 
139  this->test_common_add_module_invalid_file();
140  this->test_common_add_module_invalid_file_size();
141  this->test_common_add_module_garbage_module();
142  this->test_common_add_module_add_when_already_loaded();
143  this->test_common_add_module_add_when_already_running();
144  this->test_common_add_module_add_when_corrupt();
145  this->test_common_add_module_add_too_many();
146  this->test_common_add_module_platform_alloc_fails();
147  this->test_common_add_module_load_elf_fails();
148 
149  this->test_common_load_successful_load();
150  this->test_common_load_load_when_already_loaded();
151  this->test_common_load_load_when_already_running();
152  this->test_common_load_load_when_corrupt();
153  this->test_common_load_fail_due_to_relocation_error();
154  this->test_common_load_fail_due_to_no_modules_added();
155  this->test_common_load_add_md_failed();
156  this->test_common_load_add_md_tls_failed();
157  this->test_common_load_tls_platform_alloc_failed();
158  this->test_common_load_stack_platform_alloc_failed();
159  this->test_common_load_loader_add_failed();
160  this->test_common_load_resolve_symbol_failed();
161  this->test_common_load_execute_symbol_failed();
162 
163  this->test_common_unload_unload_when_already_unloaded();
164  this->test_common_unload_unload_when_running();
165  this->test_common_unload_unload_when_corrupt();
166  this->test_common_unload_execute_symbol_failed();
167 
168  this->test_common_start_start_when_unloaded();
169  this->test_common_start_start_when_already_running();
170  this->test_common_start_start_when_corrupt();
171  this->test_common_start_start_when_start_vmm_missing();
172  this->test_common_start_start_vmm_failure();
173  this->test_common_start_set_affinity_failed();
174  this->test_common_start_vmcall_failed();
175 
176  this->test_common_stop_stop_when_unloaded();
177  this->test_common_stop_stop_when_not_running();
178  this->test_common_stop_stop_when_alread_stopped();
179  this->test_common_stop_stop_when_corrupt();
180  this->test_common_stop_stop_vmm_missing();
181  this->test_common_stop_stop_vmm_failure();
182  this->test_common_stop_set_affinity_failed();
183  this->test_common_stop_vmcall_failed();
184 
185  this->test_common_dump_invalid_drr();
186  this->test_common_dump_invalid_vcpuid();
187  this->test_common_dump_dump_when_unloaded();
188  this->test_common_dump_dump_when_corrupt();
189  this->test_common_dump_dump_when_loaded();
190  this->test_common_dump_get_drr_missing();
191  this->test_common_dump_get_drr_failure();
192 
193  this->test_common_vmcall_invalid_args();
194  this->test_common_vmcall_set_affinity_failure();
195  this->test_common_vmcall_success();
196  this->test_common_vmcall_success_event();
197  this->test_common_vmcall_vmcall_when_unloaded();
198  this->test_common_vmcall_vmcall_when_corrupt();
199  this->test_common_vmcall_vmcall_when_loaded();
200 
201  this->test_helper_common_vmm_status();
202  this->test_helper_get_file_invalid_index();
203  this->test_helper_get_file_success();
204  this->test_helper_symbol_length_null_symbol();
205  this->test_helper_symbol_length_success();
206  this->test_helper_resolve_symbol_invalid_name();
207  this->test_helper_resolve_symbol_invalid_sym();
208  this->test_helper_resolve_symbol_no_loaded_modules();
209  this->test_helper_resolve_symbol_missing_symbol();
210  this->test_helper_execute_symbol_invalid_arg();
211  this->test_helper_execute_symbol_missing_symbol();
212  this->test_helper_execute_symbol_sym_failed();
213  this->test_helper_execute_symbol_sym_success();
214  this->test_helper_add_md_to_memory_manager_null_module();
215  this->test_helper_load_elf_file_null_module();
216 
217  return verify_no_mem_leaks() != 0;
218 }
219 
220 int
221 main(int argc, char *argv[])
222 {
224 }
Definition: test.h:27
int main(int argc, char *argv[])
Definition: test.cpp:221
driver_entry_ut()
Definition: test.cpp:39
const auto c_dummy_stop_vmm_failure_filename
Definition: test.cpp:34
bool init() override
Definition: test.cpp:62
bool fini() override
Definition: test.cpp:118
const auto c_dummy_stop_vmm_success_filename
Definition: test.cpp:35
const auto c_dummy_add_md_failure_filename
Definition: test.cpp:27
int verify_no_mem_leaks(void)
Definition: platform.c:42
#define RUN_ALL_TESTS(ut)
Definition: unittest.h:246
const auto c_dummy_get_drr_success_filename
Definition: test.cpp:30
const auto c_dummy_start_vmm_success_filename
Definition: test.cpp:33
const auto c_dummy_add_md_success_filename
Definition: test.cpp:28
bool list() override
Definition: test.cpp:124
const auto c_dummy_start_vmm_failure_filename
Definition: test.cpp:32
const auto c_dummy_get_drr_failure_filename
Definition: test.cpp:29
const auto c_dummy_misc_filename
Definition: test.cpp:31