test_loader_relocate.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 void
25 bfelf_loader_ut::test_bfelf_loader_relocate_invalid_loader()
26 {
27  auto ret = bfelf_loader_relocate(nullptr);
29 }
30 
31 void
32 bfelf_loader_ut::test_bfelf_loader_relocate_no_files_added()
33 {
34  bfelf_loader_t loader;
35  memset(&loader, 0, sizeof(loader));
36 
37  auto ret = bfelf_loader_relocate(&loader);
38  this->expect_true(ret == BFELF_SUCCESS);
39 }
40 
41 void
42 bfelf_loader_ut::test_bfelf_loader_relocate_uninitialized_files()
43 {
44  auto ret = 0LL;
45  bfelf_file_t dummy_misc_ef;
46  bfelf_file_t dummy_code_ef;
47 
48  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
49  this->expect_true(ret == BFELF_SUCCESS);
50  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
51  this->expect_true(ret == BFELF_SUCCESS);
52 
53  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
54  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
55 
56  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
57  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
58 
59  bfelf_loader_t loader;
60  memset(&loader, 0, sizeof(loader));
61 
62  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
63  this->expect_true(ret == BFELF_SUCCESS);
64  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
65  this->expect_true(ret == BFELF_SUCCESS);
66 
67  memset(&dummy_misc_ef, 0, sizeof(dummy_misc_ef));
68  memset(&dummy_code_ef, 0, sizeof(dummy_code_ef));
69 
70  ret = bfelf_loader_relocate(&loader);
71  this->expect_true(ret == BFELF_SUCCESS);
72 }
73 
74 void
75 bfelf_loader_ut::test_bfelf_loader_relocate_twice()
76 {
77  auto ret = 0LL;
78  bfelf_file_t dummy_misc_ef;
79  bfelf_file_t dummy_code_ef;
80 
81  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
82  this->expect_true(ret == BFELF_SUCCESS);
83  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
84  this->expect_true(ret == BFELF_SUCCESS);
85 
86  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
87  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
88 
89  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
90  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
91 
92  bfelf_loader_t loader;
93  memset(&loader, 0, sizeof(loader));
94 
95  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
96  this->expect_true(ret == BFELF_SUCCESS);
97  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
98  this->expect_true(ret == BFELF_SUCCESS);
99 
100  ret = bfelf_loader_relocate(&loader);
101  this->expect_true(ret == BFELF_SUCCESS);
102 
103  ret = bfelf_loader_relocate(&loader);
104  this->expect_true(ret == BFELF_SUCCESS);
105 }
106 
107 void
108 bfelf_loader_ut::test_bfelf_loader_relocate_no_symbol()
109 {
110  auto ret = 0LL;
111  bfelf_file_t dummy_code_ef;
112 
113  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
114  this->expect_true(ret == BFELF_SUCCESS);
115 
116  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
117  auto &&dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
118 
119  bfelf_loader_t loader = {};
120 
121  ret = bfelf_loader_add(&loader, &dummy_code_ef, dummy_code_exec.get(), dummy_code_exec.get());
122  this->expect_true(ret == BFELF_SUCCESS);
123 
124  ret = bfelf_loader_relocate(&loader);
126 }
void * memset(void *block, int c, size_t size)
#define BFELF_ERROR_INVALID_ARG
Definition: error_codes.h:81
#define BFELF_ERROR_NO_SUCH_SYMBOL
Definition: error_codes.h:89
#define BFELF_SUCCESS
Definition: error_codes.h:80
#define expect_true(a)