test_file_get_section_info.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_file_get_section_info_invalid_elf_file()
26 {
27  section_info_t info;
28 
29  auto ret = bfelf_file_get_section_info(nullptr, &info);
31 }
32 
33 void
34 bfelf_loader_ut::test_bfelf_file_get_section_info_invalid_info()
35 {
36  bfelf_file_t ef;
37 
38  auto ret = bfelf_file_get_section_info(&ef, nullptr);
40 }
41 
42 void
43 bfelf_loader_ut::test_bfelf_file_get_section_info_expected_misc_resources()
44 {
45  auto ret = 0LL;
46  bfelf_file_t dummy_misc_ef;
47  bfelf_file_t dummy_code_ef;
48 
49  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
50  this->expect_true(ret == BFELF_SUCCESS);
51  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
52  this->expect_true(ret == BFELF_SUCCESS);
53 
54  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
55  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
56 
57  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
58  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
59 
60  bfelf_loader_t loader;
61  memset(&loader, 0, sizeof(loader));
62 
63  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
64  this->expect_true(ret == BFELF_SUCCESS);
65  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
66  this->expect_true(ret == BFELF_SUCCESS);
67 
68  ret = bfelf_loader_relocate(&loader);
69  this->expect_true(ret == BFELF_SUCCESS);
70 
71  section_info_t info;
72 
73  ret = bfelf_file_get_section_info(&dummy_misc_ef, &info);
74  this->expect_true(ret == BFELF_SUCCESS);
75 
76  this->expect_true(info.init_array_addr != nullptr);
77  this->expect_true(info.init_array_size != 0);
78 
79  this->expect_true(info.fini_array_addr != nullptr);
80  this->expect_true(info.fini_array_size != 0);
81 
82  this->expect_true(info.eh_frame_addr != nullptr);
83  this->expect_true(info.eh_frame_size != 0);
84 }
85 
86 void
87 bfelf_loader_ut::test_bfelf_file_get_section_info_expected_code_resources()
88 {
89  auto ret = 0LL;
90  bfelf_file_t dummy_misc_ef;
91  bfelf_file_t dummy_code_ef;
92 
93  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
94  this->expect_true(ret == BFELF_SUCCESS);
95  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
96  this->expect_true(ret == BFELF_SUCCESS);
97 
98  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
99  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
100 
101  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
102  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
103 
104  bfelf_loader_t loader;
105  memset(&loader, 0, sizeof(loader));
106 
107  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
108  this->expect_true(ret == BFELF_SUCCESS);
109  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
110  this->expect_true(ret == BFELF_SUCCESS);
111 
112  ret = bfelf_loader_relocate(&loader);
113  this->expect_true(ret == BFELF_SUCCESS);
114 
115  section_info_t info;
116 
117  ret = bfelf_file_get_section_info(&dummy_code_ef, &info);
118  this->expect_true(ret == BFELF_SUCCESS);
119 
120  this->expect_true(info.init_array_addr == nullptr);
121  this->expect_true(info.init_array_size == 0);
122 
123  this->expect_true(info.fini_array_addr == nullptr);
124  this->expect_true(info.fini_array_size == 0);
125 
126  this->expect_true(info.eh_frame_addr != nullptr);
127  this->expect_true(info.eh_frame_size != 0);
128 }
129 
130 void
131 bfelf_loader_ut::test_bfelf_file_get_section_info_init_fini()
132 {
133  auto ret = 0LL;
134  bfelf_file_t dummy_misc_ef;
135 
136  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
137  this->expect_true(ret == BFELF_SUCCESS);
138 
139  dummy_misc_ef.init = 10;
140  dummy_misc_ef.fini = 10;
141 
142  section_info_t info;
143 
144  ret = bfelf_file_get_section_info(&dummy_misc_ef, &info);
145  this->expect_true(ret == BFELF_SUCCESS);
146 }
void * memset(void *block, int c, size_t size)
#define BFELF_ERROR_INVALID_ARG
Definition: error_codes.h:81
void * eh_frame_addr
Definition: crt.h:118
uint64_t fini_array_size
Definition: crt.h:116
void * init_array_addr
Definition: crt.h:112
void * fini_array_addr
Definition: crt.h:115
uint64_t init_array_size
Definition: crt.h:113
#define BFELF_SUCCESS
Definition: error_codes.h:80
uint64_t eh_frame_size
Definition: crt.h:119
#define expect_true(a)