test_private.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 <gsl/gsl>
25 
26 void
27 bfelf_loader_ut::test_private_hash()
28 {
29  auto &&str = "strange char here: \200";
30  this->expect_true(private_hash(static_cast<const char *>(str)) != 0);
31 }
32 
33 void
34 bfelf_loader_ut::test_private_relocate_invalid_relocation_dyn()
35 {
36  bfelf_file_t ef;
37  auto &&data = get_test();
38  auto &&buff = std::get<0>(data);
39  auto &&size = std::get<1>(data);
40 
41  auto ret = bfelf_file_init(buff.get(), size, &ef);
42  this->expect_true(ret == BFELF_SUCCESS);
43 
44  bfelf_rela relatab_dyn[1] = {};
45  bfelf_loader_t loader = {};
46  bfelf_sym symtab[1] = {};
47 
48  auto file = "hello";
49  auto exec = std::make_unique<char[]>(100);
50 
51  ef.exec_addr = exec.get();
52  ef.exec_virt = exec.get();
53  ef.file = file;
54  ef.symtab = static_cast<bfelf_sym *>(symtab);
55  ef.symnum = 1;
56  ef.relatab_dyn = static_cast<bfelf_rela *>(relatab_dyn);
57  ef.relanum_dyn = 1;
58  ef.relanum_plt = 0;
59 
60  gsl::at(relatab_dyn, 0).r_info = 0xFFFFFFFF;
61  gsl::at(relatab_dyn, 0).r_offset = 0x0;
62 
63  gsl::at(symtab, 0).st_name = 0xFFFFF;
64  gsl::at(symtab, 0).st_value = 0x1;
65 
66  this->expect_true(private_relocate_symbols(&loader, &ef) == BFELF_ERROR_UNSUPPORTED_RELA);
67 }
68 
69 void
70 bfelf_loader_ut::test_private_relocate_invalid_relocation_plt()
71 {
72  bfelf_file_t ef;
73  auto &&data = get_test();
74  auto &&buff = std::get<0>(data);
75  auto &&size = std::get<1>(data);
76 
77  auto ret = bfelf_file_init(buff.get(), size, &ef);
78  this->expect_true(ret == BFELF_SUCCESS);
79 
80  bfelf_rela relatab_plt[1] = {};
81  bfelf_loader_t loader = {};
82  bfelf_sym symtab[1] = {};
83 
84  auto file = "hello";
85  auto exec = std::make_unique<char[]>(100);
86 
87  ef.exec_addr = exec.get();
88  ef.exec_virt = exec.get();
89  ef.file = file;
90  ef.symtab = static_cast<bfelf_sym *>(symtab);
91  ef.symnum = 1;
92  ef.relatab_plt = static_cast<bfelf_rela *>(relatab_plt);
93  ef.relanum_dyn = 0;
94  ef.relanum_plt = 1;
95 
96  gsl::at(relatab_plt, 0).r_info = 0xFFFFFFFF;
97  gsl::at(relatab_plt, 0).r_offset = 0x0;
98 
99  gsl::at(symtab, 0).st_name = 0xFFFFF;
100  gsl::at(symtab, 0).st_value = 0x1;
101 
102  this->expect_true(private_relocate_symbols(&loader, &ef) == BFELF_ERROR_UNSUPPORTED_RELA);
103 }
104 
105 void
106 bfelf_loader_ut::test_private_process_dynamic_section()
107 {
108  bfelf_file_t ef;
109  auto &&data = get_test();
110  auto &&buff = std::get<0>(data);
111  auto &&size = std::get<1>(data);
112 
113  auto ret = bfelf_file_init(buff.get(), size, &ef);
114  this->expect_true(ret == BFELF_SUCCESS);
115 
116  private_process_dynamic_section(&ef);
117 
118  ef.dynnum = 0;
119  private_process_dynamic_section(&ef);
120 
121  ef.dynoff = 0;
122  private_process_dynamic_section(&ef);
123 }
#define BFELF_ERROR_UNSUPPORTED_RELA
Definition: error_codes.h:91
Definition: file.h:35
file() noexcept=default
int64_t unsigned long void * data
constexpr const auto size
#define BFELF_SUCCESS
Definition: error_codes.h:80
#define expect_true(a)