Skip to content

Commit

Permalink
Add format_detector unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dreamer committed Dec 15, 2024
1 parent 50f7a27 commit c4a3cc9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ target_sources(tests
tests/pe_bliss2/image_signature_tests.cpp
tests/pe_bliss2/image_tests.cpp
tests/pe_bliss2/input_buffer_mock.h
tests/pe_bliss2/format_detector_tests.cpp
tests/pe_bliss2/load_config_loader_tests.cpp
tests/pe_bliss2/optional_header_tests.cpp
tests/pe_bliss2/output_buffer_mock.h
Expand Down
1 change: 1 addition & 0 deletions tests/tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
<ClCompile Include="tests\pe_bliss2\endian_convert_tests.cpp" />
<ClCompile Include="tests\pe_bliss2\error_list_tests.cpp" />
<ClCompile Include="tests\pe_bliss2\file_header_tests.cpp" />
<ClCompile Include="tests\pe_bliss2\format_detector_tests.cpp" />
<ClCompile Include="tests\pe_bliss2\image_builder_tests.cpp" />
<ClCompile Include="tests\pe_bliss2\image_helper.cpp" />
<ClCompile Include="tests\pe_bliss2\image_loader_tests.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions tests/tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@
<ClCompile Include="tests\pe_bliss2\trustlet_policy_metadata_tests.cpp">
<Filter>Source Files\tests\pe_bliss2</Filter>
</ClCompile>
<ClCompile Include="tests\pe_bliss2\format_detector_tests.cpp">
<Filter>Source Files\tests\pe_bliss2</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="tests\buffers\input_buffer_helpers.h">
Expand Down
95 changes: 95 additions & 0 deletions tests/tests/pe_bliss2/format_detector_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "pe_bliss2/image/format_detector.h"

#include <array>
#include <cstddef>
#include <memory>

#include "gtest/gtest.h"

#include "buffers/input_memory_buffer.h"

using namespace pe_bliss;
using namespace pe_bliss::image;

TEST(FormatDetectorTests, Empty)
{
std::array<std::byte, 1> arr{};
buffers::input_memory_buffer buf(arr.data(), 0u);
ASSERT_FALSE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
}

TEST(FormatDetectorTests, InvalidDosHeader)
{
static constexpr const char header_data[] =
"MX??????????????????????????????????????????????????????????"
"\x04\x00\x00\x00??????";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_FALSE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
}

TEST(FormatDetectorTests, InvalidElfanew)
{
static constexpr const char header_data[] =
"MZ??????????????????????????????????????????????????????????"
"\x64\x00\x00\x00??????";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_FALSE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
}

TEST(FormatDetectorTests, LooksLikePe)
{
static constexpr const char header_data[] =
"MZ??????????????????????????????????????????????????????????"
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_TRUE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
}

TEST(FormatDetectorTests, DetectFormatInvalidMagic)
{
static constexpr const char header_data[] =
"MZ??????????????????????????????????????????????????????????"
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
"ab";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_TRUE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::none);
}

TEST(FormatDetectorTests, DetectFormatPe64Magic)
{
static constexpr const char header_data[] =
"MZ??????????????????????????????????????????????????????????"
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
"\x0b\x02";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_TRUE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::pe64);
}

TEST(FormatDetectorTests, DetectFormatPe32Magic)
{
static constexpr const char header_data[] =
"MZ??????????????????????????????????????????????????????????"
"\x50\x00\x00\x00??????XXXXXXXXXX\x50\x45\x00\x00????????????????????"
"\x0b\x01";
buffers::input_memory_buffer buf(
reinterpret_cast<const std::byte*>(header_data),
std::size(header_data));
ASSERT_TRUE(format_detector::looks_like_pe(buf));
ASSERT_EQ(format_detector::detect_format(buf), detected_format::pe32);
}

0 comments on commit c4a3cc9

Please sign in to comment.