test_common_add_module.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  int64_t load_elf_file(struct module_t *module);
36 }
37 
38 // -----------------------------------------------------------------------------
39 // Tests
40 // -----------------------------------------------------------------------------
41 
42 void
43 driver_entry_ut::test_common_add_module_invalid_file()
44 {
45  this->expect_true(common_add_module(nullptr, m_dummy_misc_length) == BF_ERROR_INVALID_ARG);
46 }
47 
48 void
49 driver_entry_ut::test_common_add_module_invalid_file_size()
50 {
51  this->expect_true(common_add_module(m_dummy_misc.get(), 0) == BF_ERROR_INVALID_ARG);
52 }
53 
54 void
55 driver_entry_ut::test_common_add_module_garbage_module()
56 {
57  auto file = "this is clearly not an ELF file!!!";
58 
60 }
61 
62 void
63 driver_entry_ut::test_common_add_module_add_when_already_loaded()
64 {
65  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
66  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
69  this->expect_true(common_add_module(m_dummy_get_drr_success.get(), m_dummy_get_drr_success_length) == BF_ERROR_VMM_INVALID_STATE);
71 }
72 
73 void
74 driver_entry_ut::test_common_add_module_add_when_already_running()
75 {
76  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
77  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
78  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
79  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
83  this->expect_true(common_add_module(m_dummy_get_drr_success.get(), m_dummy_get_drr_success_length) == BF_ERROR_VMM_INVALID_STATE);
85 }
86 
87 void
88 driver_entry_ut::test_common_add_module_add_when_corrupt()
89 {
90  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
91  this->expect_true(common_add_module(m_dummy_stop_vmm_failure.get(), m_dummy_stop_vmm_failure_length) == BF_SUCCESS);
92  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
93  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
98  this->expect_true(common_add_module(m_dummy_get_drr_success.get(), m_dummy_get_drr_success_length) == BF_ERROR_VMM_CORRUPTED);
99 
100  common_reset();
101 }
102 
103 void
104 driver_entry_ut::test_common_add_module_add_too_many()
105 {
106  for (auto i = 0U; i < MAX_NUM_MODULES; i++)
107  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
108 
109  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_ERROR_MAX_MODULES_REACHED);
110  this->expect_true(common_fini() == BF_SUCCESS);
111 }
112 
113 void
114 driver_entry_ut::test_common_add_module_platform_alloc_fails()
115 {
116  MockRepository mocks;
117  mocks.ExpectCallFunc(platform_alloc_rwe).Return(nullptr);
118 
119  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
120  {
121  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_ERROR_OUT_OF_MEMORY);
122  });
123 }
124 
125 void
126 driver_entry_ut::test_common_add_module_load_elf_fails()
127 {
128  MockRepository mocks;
129  mocks.ExpectCallFunc(load_elf_file).Return(-1);
130 
131  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
132  {
133  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == -1);
134  });
135 }
#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
Definition: file.h:35
#define RUN_UNITTEST_WITH_MOCKS(a, b)
Definition: unittest.h:229
#define BFELF_ERROR_INVALID_ARG
Definition: error_codes.h:81
int64_t common_load_vmm(void)
Definition: common.c:357
void * platform_alloc_rwe(uint64_t len)
Definition: platform.c:62
int64_t common_fini(void)
Definition: common.c:273
#define BF_ERROR_VMM_INVALID_STATE
Definition: error_codes.h:110
#define BF_ERROR_MAX_MODULES_REACHED
Definition: error_codes.h:109
#define MAX_NUM_MODULES
Definition: constants.h:177
int64_t load_elf_file(struct module_t *module)
Definition: common.c:191
int64_t common_start_vmm(void)
Definition: common.c:490
#define BF_ERROR_OUT_OF_MEMORY
Definition: error_codes.h:113
#define VMM_RUNNING
#define VMM_CORRUPT
#define BF_ERROR_INVALID_ARG
Definition: error_codes.h:106
int64_t common_vmm_status(void)
Definition: common.c:227
#define BF_ERROR_VMM_CORRUPTED
Definition: error_codes.h:114
#define expect_true(a)
int64_t common_reset(void)
Definition: common.c:233