Skip to content

Commit c4a3cc9

Browse files
Add format_detector unit tests
1 parent 50f7a27 commit c4a3cc9

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ target_sources(tests
6060
tests/pe_bliss2/image_signature_tests.cpp
6161
tests/pe_bliss2/image_tests.cpp
6262
tests/pe_bliss2/input_buffer_mock.h
63+
tests/pe_bliss2/format_detector_tests.cpp
6364
tests/pe_bliss2/load_config_loader_tests.cpp
6465
tests/pe_bliss2/optional_header_tests.cpp
6566
tests/pe_bliss2/output_buffer_mock.h

tests/tests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
<ClCompile Include="tests\pe_bliss2\endian_convert_tests.cpp" />
263263
<ClCompile Include="tests\pe_bliss2\error_list_tests.cpp" />
264264
<ClCompile Include="tests\pe_bliss2\file_header_tests.cpp" />
265+
<ClCompile Include="tests\pe_bliss2\format_detector_tests.cpp" />
265266
<ClCompile Include="tests\pe_bliss2\image_builder_tests.cpp" />
266267
<ClCompile Include="tests\pe_bliss2\image_helper.cpp" />
267268
<ClCompile Include="tests\pe_bliss2\image_loader_tests.cpp" />

tests/tests.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@
426426
<ClCompile Include="tests\pe_bliss2\trustlet_policy_metadata_tests.cpp">
427427
<Filter>Source Files\tests\pe_bliss2</Filter>
428428
</ClCompile>
429+
<ClCompile Include="tests\pe_bliss2\format_detector_tests.cpp">
430+
<Filter>Source Files\tests\pe_bliss2</Filter>
431+
</ClCompile>
429432
</ItemGroup>
430433
<ItemGroup>
431434
<ClInclude Include="tests\buffers\input_buffer_helpers.h">
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "pe_bliss2/image/format_detector.h"
2+
3+
#include <array>
4+
#include <cstddef>
5+
#include <memory>
6+
7+
#include "gtest/gtest.h"
8+
9+
#include "buffers/input_memory_buffer.h"
10+
11+
using namespace pe_bliss;
12+
using namespace pe_bliss::image;
13+
14+
TEST(FormatDetectorTests, Empty)
15+
{
16+
std::array<std::byte, 1> arr{};
17+
buffers::input_memory_buffer buf(arr.data(), 0u);
18+
ASSERT_FALSE(format_detector::looks_like_pe(buf));
19+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
20+
}
21+
22+
TEST(FormatDetectorTests, InvalidDosHeader)
23+
{
24+
static constexpr const char header_data[] =
25+
"MX??????????????????????????????????????????????????????????"
26+
"\x04\x00\x00\x00??????";
27+
buffers::input_memory_buffer buf(
28+
reinterpret_cast<const std::byte*>(header_data),
29+
std::size(header_data));
30+
ASSERT_FALSE(format_detector::looks_like_pe(buf));
31+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
32+
}
33+
34+
TEST(FormatDetectorTests, InvalidElfanew)
35+
{
36+
static constexpr const char header_data[] =
37+
"MZ??????????????????????????????????????????????????????????"
38+
"\x64\x00\x00\x00??????";
39+
buffers::input_memory_buffer buf(
40+
reinterpret_cast<const std::byte*>(header_data),
41+
std::size(header_data));
42+
ASSERT_FALSE(format_detector::looks_like_pe(buf));
43+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
44+
}
45+
46+
TEST(FormatDetectorTests, LooksLikePe)
47+
{
48+
static constexpr const char header_data[] =
49+
"MZ??????????????????????????????????????????????????????????"
50+
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00";
51+
buffers::input_memory_buffer buf(
52+
reinterpret_cast<const std::byte*>(header_data),
53+
std::size(header_data));
54+
ASSERT_TRUE(format_detector::looks_like_pe(buf));
55+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
56+
}
57+
58+
TEST(FormatDetectorTests, DetectFormatInvalidMagic)
59+
{
60+
static constexpr const char header_data[] =
61+
"MZ??????????????????????????????????????????????????????????"
62+
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
63+
"ab";
64+
buffers::input_memory_buffer buf(
65+
reinterpret_cast<const std::byte*>(header_data),
66+
std::size(header_data));
67+
ASSERT_TRUE(format_detector::looks_like_pe(buf));
68+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
69+
}
70+
71+
TEST(FormatDetectorTests, DetectFormatPe64Magic)
72+
{
73+
static constexpr const char header_data[] =
74+
"MZ??????????????????????????????????????????????????????????"
75+
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
76+
"\x0b\x02";
77+
buffers::input_memory_buffer buf(
78+
reinterpret_cast<const std::byte*>(header_data),
79+
std::size(header_data));
80+
ASSERT_TRUE(format_detector::looks_like_pe(buf));
81+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::pe64);
82+
}
83+
84+
TEST(FormatDetectorTests, DetectFormatPe32Magic)
85+
{
86+
static constexpr const char header_data[] =
87+
"MZ??????????????????????????????????????????????????????????"
88+
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
89+
"\x0b\x01";
90+
buffers::input_memory_buffer buf(
91+
reinterpret_cast<const std::byte*>(header_data),
92+
std::size(header_data));
93+
ASSERT_TRUE(format_detector::looks_like_pe(buf));
94+
ASSERT_EQ(format_detector::detect_format(buf), detected_format::pe32);
95+
}

0 commit comments

Comments
 (0)