test_helpers.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 <common.h>
25 #include <platform.h>
26 #include <constants.h>
27 #include <driver_entry_interface.h>
28 
29 // -----------------------------------------------------------------------------
30 // Expose Private Functions
31 // -----------------------------------------------------------------------------
32 
33 extern "C"
34 {
35  struct module_t *get_module(uint64_t index);
36  int64_t symbol_length(const char *sym);
37  int64_t resolve_symbol(const char *name, void **sym);
38  int64_t execute_symbol(const char *sym, uint64_t arg1, uint64_t arg2, uint64_t cpuid);
39  int64_t add_md_to_memory_manager(struct module_t *module);
40  int64_t load_elf_file(struct module_t *module);
41 }
42 
43 // -----------------------------------------------------------------------------
44 // Tests
45 // -----------------------------------------------------------------------------
46 
47 void
48 driver_entry_ut::test_helper_common_vmm_status()
49 {
51  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
52  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
53  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
54  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
63 }
64 
65 void
66 driver_entry_ut::test_helper_get_file_invalid_index()
67 {
68  this->expect_true(get_module(10000) == nullptr);
69 }
70 
71 void
72 driver_entry_ut::test_helper_get_file_success()
73 {
74  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
75  this->expect_true(get_module(0) != nullptr);
77 }
78 
79 void
80 driver_entry_ut::test_helper_symbol_length_null_symbol()
81 {
82  this->expect_true(symbol_length(nullptr) == 0);
83 }
84 
85 void
86 driver_entry_ut::test_helper_symbol_length_success()
87 {
88  this->expect_true(symbol_length("hello world") == 11);
89 }
90 
91 void
92 driver_entry_ut::test_helper_resolve_symbol_invalid_name()
93 {
94  void *sym;
95 
96  this->expect_true(resolve_symbol(nullptr, &sym) == BF_ERROR_INVALID_ARG);
97 }
98 
99 void
100 driver_entry_ut::test_helper_resolve_symbol_invalid_sym()
101 {
102  this->expect_true(resolve_symbol("sym", nullptr) == BF_ERROR_INVALID_ARG);
103 }
104 
105 void
106 driver_entry_ut::test_helper_resolve_symbol_no_loaded_modules()
107 {
108  void *sym;
109 
110  this->expect_true(resolve_symbol("invalid_symbol", &sym) == BF_ERROR_NO_MODULES_ADDED);
111 }
112 
113 void
114 driver_entry_ut::test_helper_resolve_symbol_missing_symbol()
115 {
116  void *sym;
117 
118  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
119  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
120  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
121  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
123  this->expect_true(resolve_symbol("invalid_symbol", &sym) == BFELF_ERROR_NO_SUCH_SYMBOL);
124  this->expect_true(common_fini() == BF_SUCCESS);
125 }
126 
127 void
128 driver_entry_ut::test_helper_execute_symbol_invalid_arg()
129 {
130  this->expect_true(execute_symbol(nullptr, 0, 0, 0) == BF_ERROR_INVALID_ARG);
131 }
132 
133 void
134 driver_entry_ut::test_helper_execute_symbol_missing_symbol()
135 {
136  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
137  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
138  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
139  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
141  this->expect_true(execute_symbol("invalid_symbol", 0, 0, 0) == BFELF_ERROR_NO_SUCH_SYMBOL);
142  this->expect_true(common_fini() == BF_SUCCESS);
143 }
144 
145 void
146 driver_entry_ut::test_helper_execute_symbol_sym_failed()
147 {
148  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
149  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
150  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
151  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
153  this->expect_true(execute_symbol("sym_that_returns_failure", 0, 0, 0) == -1);
154  this->expect_true(common_fini() == BF_SUCCESS);
155 }
156 
157 void
158 driver_entry_ut::test_helper_execute_symbol_sym_success()
159 {
160  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
161  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
162  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
163  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
165  this->expect_true(execute_symbol("sym_that_returns_success", 0, 0, 0) == 0);
166  this->expect_true(common_fini() == BF_SUCCESS);
167 }
168 
169 void
170 driver_entry_ut::test_helper_add_md_to_memory_manager_null_module()
171 {
173 }
174 
175 void
176 driver_entry_ut::test_helper_load_elf_file_null_module()
177 {
179 }
#define BF_SUCCESS
Definition: error_codes.h:105
int64_t common_add_module(const char *file, uint64_t fsize)
Definition: common.c:305
#define VMM_LOADED
int64_t common_load_vmm(void)
Definition: common.c:357
int64_t common_fini(void)
Definition: common.c:273
#define BFELF_ERROR_NO_SUCH_SYMBOL
Definition: error_codes.h:89
int64_t load_elf_file(struct module_t *module)
Definition: common.c:191
struct module_t * get_module(uint64_t index)
auto index(const T virt, const F from)
int64_t common_stop_vmm(void)
Definition: common.c:543
int64_t execute_symbol(const char *sym, uint64_t arg1, uint64_t arg2, uint64_t cpuid)
Definition: common.c:108
#define VMM_UNLOADED
int64_t common_start_vmm(void)
Definition: common.c:490
int64_t symbol_length(const char *sym)
Definition: common.c:73
#define BF_ERROR_NO_MODULES_ADDED
Definition: error_codes.h:108
constexpr const auto name
Definition: cpuid_x64.h:81
#define VMM_RUNNING
int64_t common_unload_vmm(void)
Definition: common.c:451
#define BF_ERROR_INVALID_ARG
Definition: error_codes.h:106
int64_t common_vmm_status(void)
Definition: common.c:227
int64_t resolve_symbol(const char *name, void **sym)
Definition: common.c:87
#define expect_true(a)
int64_t add_md_to_memory_manager(struct module_t *module)
Definition: common.c:152