test_crt.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 <crt.h>
25 #include <eh_frame_list.h>
26 
27 typedef void (*init_t)();
28 typedef void (*fini_t)();
29 
30 int64_t
32 {
33  (void) addr;
34  (void) size;
35 
37 }
38 
39 void
41 { }
42 
43 void
45 { }
46 
47 void
48 crt_ut::test_coveralls()
49 {
51  func1();
52  func2();
53 }
54 
55 void
56 crt_ut::test_local_init_invalid_arg()
57 {
58  MockRepository mocks;
59  mocks.NeverCallFunc(register_eh_frame);
60 
61  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
62  {
63  this->expect_true(local_init(nullptr) == CRT_FAILURE);
64  });
65 }
66 
67 void
68 crt_ut::test_local_init_invalid_addr()
69 {
70  int addr = 0;
71  section_info_t info;
72 
73  memset(&info, 0, sizeof(info));
74 
75  MockRepository mocks;
76  mocks.NeverCallFunc(func1);
77  mocks.NeverCallFunc(func2);
78  mocks.ExpectCallFunc(register_eh_frame).With(&addr, 100).Return(0);
79 
80  info.init_array_size = 16;
81  info.eh_frame_addr = &addr;
82  info.eh_frame_size = 100;
83 
84  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
85  {
86  this->expect_true(local_init(&info) == CRT_SUCCESS);
87  });
88 }
89 
90 void
91 crt_ut::test_local_init_invalid_size()
92 {
93  int addr = 0;
94  section_info_t info;
95 
96  memset(&info, 0, sizeof(info));
97 
98  MockRepository mocks;
99  mocks.ExpectCallFunc(func1);
100  mocks.NeverCallFunc(func2);
101  mocks.ExpectCallFunc(register_eh_frame).With(&addr, 100).Return(0);
102 
103  init_t func_list[2] = {static_cast<init_t>(func1), static_cast<init_t>(func2)};
104 
105  info.init_addr = reinterpret_cast<void *>(func1);
106  info.init_array_addr = static_cast<void *>(func_list);
107  info.eh_frame_addr = &addr;
108  info.eh_frame_size = 100;
109 
110  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
111  {
112  this->expect_true(local_init(&info) == CRT_SUCCESS);
113  });
114 }
115 
116 void
117 crt_ut::test_local_init_register_eh_frame_failure()
118 {
119  int addr = 0;
120  section_info_t info;
121 
122  memset(&info, 0, sizeof(info));
123 
124  MockRepository mocks;
125  mocks.NeverCallFunc(func1);
126  mocks.NeverCallFunc(func2);
127  mocks.ExpectCallFunc(register_eh_frame).With(&addr, 100).Return(REGISTER_EH_FRAME_FAILURE);
128 
129  info.eh_frame_addr = &addr;
130  info.eh_frame_size = 100;
131 
132  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
133  {
135  });
136 }
137 
138 void
139 crt_ut::test_local_init_valid_stop_at_size()
140 {
141  int addr = 0;
142  section_info_t info;
143 
144  memset(&info, 0, sizeof(info));
145 
146  MockRepository mocks;
147  mocks.ExpectCallFunc(func1);
148  mocks.ExpectCallFunc(func1);
149  mocks.ExpectCallFunc(func2);
150  mocks.ExpectCallFunc(register_eh_frame).With(&addr, 100).Return(0);
151 
152  init_t func_list[2] = {static_cast<init_t>(func1), static_cast<init_t>(func2)};
153 
154  info.init_addr = reinterpret_cast<void *>(func1);
155  info.init_array_addr = static_cast<void *>(func_list);
156  info.init_array_size = 16;
157  info.eh_frame_addr = &addr;
158  info.eh_frame_size = 100;
159 
160  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
161  {
162  this->expect_true(local_init(&info) == CRT_SUCCESS);
163  });
164 }
165 
166 void
167 crt_ut::test_local_init_valid_stop_at_null()
168 {
169  int addr = 0;
170  section_info_t info;
171 
172  memset(&info, 0, sizeof(info));
173 
174  MockRepository mocks;
175  mocks.ExpectCallFunc(func1);
176  mocks.ExpectCallFunc(func1);
177  mocks.ExpectCallFunc(func2);
178  mocks.ExpectCallFunc(register_eh_frame).With(&addr, 100).Return(0);
179 
180  init_t func_list[3] = {static_cast<init_t>(func1), static_cast<init_t>(func2), nullptr};
181 
182  info.init_addr = reinterpret_cast<void *>(func1);
183  info.init_array_addr = static_cast<void *>(func_list);
184  info.init_array_size = 32;
185  info.eh_frame_addr = &addr;
186  info.eh_frame_size = 100;
187 
188  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
189  {
190  this->expect_true(local_init(&info) == CRT_SUCCESS);
191  });
192 }
193 
194 void
195 crt_ut::test_local_init_catch_exception()
196 {
197  int addr = 0;
198  section_info_t info;
199 
200  memset(&info, 0, sizeof(info));
201 
202  MockRepository mocks;
203  mocks.ExpectCallFunc(func1).Throw(std::runtime_error("error"));
204  mocks.OnCallFunc(register_eh_frame).With(&addr, 100).Return(0);
205 
206  info.init_addr = reinterpret_cast<void *>(func1);
207  info.eh_frame_addr = &addr;
208  info.eh_frame_size = 100;
209 
210  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
211  {
212  this->expect_true(local_init(&info) == CRT_FAILURE);
213  });
214 }
215 
216 void
217 crt_ut::test_local_fini_invalid_arg()
218 {
219  this->expect_true(local_fini(nullptr) == CRT_FAILURE);
220 }
221 
222 void
223 crt_ut::test_local_fini_invalid_addr()
224 {
225  section_info_t info;
226 
227  memset(&info, 0, sizeof(info));
228 
229  MockRepository mocks;
230  mocks.NeverCallFunc(func1);
231  mocks.NeverCallFunc(func2);
232 
233  info.fini_array_size = 16;
234 
235  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
236  {
237  this->expect_true(local_fini(&info) == CRT_SUCCESS);
238  });
239 }
240 
241 void
242 crt_ut::test_local_fini_invalid_size()
243 {
244  section_info_t info;
245 
246  memset(&info, 0, sizeof(info));
247 
248  MockRepository mocks;
249  mocks.ExpectCallFunc(func1);
250  mocks.NeverCallFunc(func2);
251 
252  fini_t func_list[2] = {static_cast<fini_t>(func1), static_cast<fini_t>(func2)};
253 
254  info.fini_addr = reinterpret_cast<void *>(func1);
255  info.fini_array_addr = static_cast<void *>(func_list);
256 
257  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
258  {
259  this->expect_true(local_fini(&info) == CRT_SUCCESS);
260  });
261 }
262 
263 void
264 crt_ut::test_local_fini_valid_stop_at_size()
265 {
266  section_info_t info;
267 
268  memset(&info, 0, sizeof(info));
269 
270  MockRepository mocks;
271  mocks.ExpectCallFunc(func1);
272  mocks.ExpectCallFunc(func2);
273  mocks.ExpectCallFunc(func1);
274 
275  fini_t func_list[2] = {static_cast<fini_t>(func1), static_cast<fini_t>(func2)};
276 
277  info.fini_addr = reinterpret_cast<void *>(func1);
278  info.fini_array_addr = static_cast<void *>(func_list);
279  info.fini_array_size = 16;
280 
281  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
282  {
283  this->expect_true(local_fini(&info) == CRT_SUCCESS);
284  });
285 }
286 
287 void
288 crt_ut::test_local_fini_valid_stop_at_null()
289 {
290  section_info_t info;
291 
292  memset(&info, 0, sizeof(info));
293 
294  MockRepository mocks;
295  mocks.ExpectCallFunc(func1);
296  mocks.ExpectCallFunc(func2);
297  mocks.ExpectCallFunc(func1);
298 
299  fini_t func_list[3] = {static_cast<fini_t>(func1), static_cast<fini_t>(func2), nullptr};
300 
301  info.fini_addr = reinterpret_cast<void *>(func1);
302  info.fini_array_addr = static_cast<void *>(func_list);
303  info.fini_array_size = 32;
304 
305  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
306  {
307  this->expect_true(local_fini(&info) == CRT_SUCCESS);
308  });
309 }
310 
311 void
312 crt_ut::test_local_fini_catch_exception()
313 {
314  int addr = 0;
315  section_info_t info;
316 
317  memset(&info, 0, sizeof(info));
318 
319  MockRepository mocks;
320  mocks.ExpectCallFunc(func1).Throw(std::runtime_error("error"));
321  mocks.OnCallFunc(register_eh_frame).With(&addr, 100).Return(0);
322 
323  info.fini_addr = reinterpret_cast<void *>(func1);
324  info.eh_frame_addr = &addr;
325  info.eh_frame_size = 100;
326 
327  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
328  {
329  this->expect_true(local_fini(&info) == CRT_FAILURE);
330  });
331 }
#define RUN_UNITTEST_WITH_MOCKS(a, b)
Definition: unittest.h:229
void * memset(void *block, int c, size_t size)
void * eh_frame_addr
Definition: crt.h:118
uint64_t fini_array_size
Definition: crt.h:116
void * init_addr
Definition: crt.h:109
void * init_array_addr
Definition: crt.h:112
#define REGISTER_EH_FRAME_FAILURE
Definition: error_codes.h:67
void * fini_addr
Definition: crt.h:110
int64_t local_init(struct section_info_t *info)
Definition: dummy_misc.cpp:90
constexpr const auto size
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const auto addr
Definition: cpuid_x64.h:80
int64_t register_eh_frame(void *addr, uint64_t size) noexcept
Definition: test_crt.cpp:31
void * fini_array_addr
Definition: crt.h:115
uint64_t init_array_size
Definition: crt.h:113
void(* fini_t)()
Definition: test_crt.cpp:28
#define CRT_FAILURE
Definition: error_codes.h:60
void(* init_t)()
Definition: test_crt.cpp:27
#define CRT_SUCCESS
Definition: error_codes.h:59
uint64_t eh_frame_size
Definition: crt.h:119
#define REGISTER_EH_FRAME_SUCCESS
Definition: error_codes.h:66
void func1()
Definition: test_crt.cpp:40
void func2()
Definition: test_crt.cpp:44
#define expect_true(a)
int64_t local_fini(struct section_info_t *info)
Definition: dummy_misc.cpp:97