mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-19 11:43:26 +00:00
Compare commits
12 Commits
v5.1.0.rc3
...
29f3e2565d
| Author | SHA1 | Date | |
|---|---|---|---|
| 29f3e2565d | |||
| e083b15e0b | |||
| ed9da79d08 | |||
| 2002bcca83 | |||
| ffacba71e2 | |||
| 6081a9c426 | |||
| 8bbd504356 | |||
| 1d54039f57 | |||
| 93fc93d4f6 | |||
| b8a578774c | |||
| bfa6c77776 | |||
| 1341ef9925 |
@@ -6,7 +6,9 @@
|
|||||||
|
|
||||||
#include "omath/linear_algebra/vector3.hpp"
|
#include "omath/linear_algebra/vector3.hpp"
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
@@ -28,10 +30,20 @@ namespace omath::pathfinding
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
|
||||||
[[nodiscard]] std::vector<uint8_t> serialize() const noexcept;
|
// Events -- per-vertex optional tag (e.g. "jump", "teleport")
|
||||||
|
void set_event(const Vector3<float>& vertex, const std::string_view& event_id);
|
||||||
|
void clear_event(const Vector3<float>& vertex);
|
||||||
|
|
||||||
void deserialize(const std::vector<uint8_t>& raw) noexcept;
|
[[nodiscard]]
|
||||||
|
std::optional<std::string> get_event(const Vector3<float>& vertex) const noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] std::string serialize() const noexcept;
|
||||||
|
|
||||||
|
void deserialize(const std::string& raw);
|
||||||
|
|
||||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_vertex_map;
|
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_vertex_map;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<Vector3<float>, std::string> m_vertex_events;
|
||||||
};
|
};
|
||||||
} // namespace omath::pathfinding
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
#include <omath/utility/pe_pattern_scan.hpp>
|
#include <omath/utility/pe_pattern_scan.hpp>
|
||||||
#include <omath/utility/section_scan_result.hpp>
|
#include <omath/utility/section_scan_result.hpp>
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua
|
||||||
{
|
{
|
||||||
@@ -101,4 +100,5 @@ namespace omath::lua
|
|||||||
return *result;
|
return *result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} // namespace omath::lua
|
} // namespace omath::lua
|
||||||
|
#endif
|
||||||
@@ -3,9 +3,9 @@
|
|||||||
//
|
//
|
||||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <sstream>
|
||||||
#include <limits>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
{
|
{
|
||||||
std::expected<Vector3<float>, std::string>
|
std::expected<Vector3<float>, std::string>
|
||||||
@@ -30,77 +30,72 @@ namespace omath::pathfinding
|
|||||||
return m_vertex_map.empty();
|
return m_vertex_map.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> NavigationMesh::serialize() const noexcept
|
void NavigationMesh::set_event(const Vector3<float>& vertex, const std::string_view& event_id)
|
||||||
{
|
{
|
||||||
std::vector<std::uint8_t> raw;
|
if (!m_vertex_map.contains(vertex))
|
||||||
|
throw std::invalid_argument(std::format("Vertex '{}' not found", vertex));
|
||||||
|
|
||||||
// Pre-calculate total size for better performance
|
m_vertex_events[vertex] = event_id;
|
||||||
std::size_t total_size = 0;
|
|
||||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
|
||||||
{
|
|
||||||
total_size += sizeof(vertex) + sizeof(std::uint16_t) + sizeof(Vector3<float>) * neighbors.size();
|
|
||||||
}
|
|
||||||
raw.reserve(total_size);
|
|
||||||
|
|
||||||
auto dump_to_vector = [&raw]<typename T>(const T& t)
|
|
||||||
{
|
|
||||||
const auto* byte_ptr = reinterpret_cast<const std::uint8_t*>(&t);
|
|
||||||
raw.insert(raw.end(), byte_ptr, byte_ptr + sizeof(T));
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
|
||||||
{
|
|
||||||
// Clamp neighbors count to fit in uint16_t (prevents silent data corruption)
|
|
||||||
// NOTE: If neighbors.size() > 65535, only the first 65535 neighbors will be serialized.
|
|
||||||
// This is a limitation of the current serialization format using uint16_t for count.
|
|
||||||
const auto clamped_count =
|
|
||||||
std::min<std::size_t>(neighbors.size(), std::numeric_limits<std::uint16_t>::max());
|
|
||||||
const auto neighbors_count = static_cast<std::uint16_t>(clamped_count);
|
|
||||||
|
|
||||||
dump_to_vector(vertex);
|
|
||||||
dump_to_vector(neighbors_count);
|
|
||||||
|
|
||||||
// Only serialize up to the clamped count
|
|
||||||
for (std::size_t i = 0; i < clamped_count; ++i)
|
|
||||||
dump_to_vector(neighbors[i]);
|
|
||||||
}
|
|
||||||
return raw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NavigationMesh::deserialize(const std::vector<uint8_t>& raw) noexcept
|
void NavigationMesh::clear_event(const Vector3<float>& vertex)
|
||||||
{
|
{
|
||||||
auto load_from_vector = [](const std::vector<uint8_t>& vec, std::size_t& offset, auto& value)
|
m_vertex_events.erase(vertex);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> NavigationMesh::get_event(const Vector3<float>& vertex) const noexcept
|
||||||
|
{
|
||||||
|
const auto it = m_vertex_events.find(vertex);
|
||||||
|
if (it == m_vertex_events.end())
|
||||||
|
return std::nullopt;
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialization format per vertex line:
|
||||||
|
// x y z neighbor_count event_id
|
||||||
|
// where event_id is "-" when no event is set.
|
||||||
|
// Neighbor lines follow: nx ny nz
|
||||||
|
|
||||||
|
std::string NavigationMesh::serialize() const noexcept
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||||
{
|
{
|
||||||
if (offset + sizeof(value) > vec.size())
|
const auto event_it = m_vertex_events.find(vertex);
|
||||||
throw std::runtime_error("Deserialize: Invalid input data size.");
|
const std::string& event = (event_it != m_vertex_events.end()) ? event_it->second : "-";
|
||||||
|
|
||||||
std::copy_n(vec.data() + offset, sizeof(value), reinterpret_cast<uint8_t*>(&value));
|
oss << vertex.x << ' ' << vertex.y << ' ' << vertex.z << ' ' << neighbors.size() << ' ' << event << '\n';
|
||||||
offset += sizeof(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
for (const auto& n : neighbors)
|
||||||
|
oss << n.x << ' ' << n.y << ' ' << n.z << '\n';
|
||||||
|
}
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NavigationMesh::deserialize(const std::string& raw)
|
||||||
|
{
|
||||||
m_vertex_map.clear();
|
m_vertex_map.clear();
|
||||||
|
m_vertex_events.clear();
|
||||||
|
std::istringstream iss(raw);
|
||||||
|
|
||||||
std::size_t offset = 0;
|
Vector3<float> vertex;
|
||||||
|
std::size_t neighbors_count;
|
||||||
while (offset < raw.size())
|
std::string event;
|
||||||
|
while (iss >> vertex.x >> vertex.y >> vertex.z >> neighbors_count >> event)
|
||||||
{
|
{
|
||||||
Vector3<float> vertex;
|
|
||||||
load_from_vector(raw, offset, vertex);
|
|
||||||
|
|
||||||
std::uint16_t neighbors_count;
|
|
||||||
load_from_vector(raw, offset, neighbors_count);
|
|
||||||
|
|
||||||
std::vector<Vector3<float>> neighbors;
|
std::vector<Vector3<float>> neighbors;
|
||||||
neighbors.reserve(neighbors_count);
|
neighbors.reserve(neighbors_count);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < neighbors_count; ++i)
|
for (std::size_t i = 0; i < neighbors_count; ++i)
|
||||||
{
|
{
|
||||||
Vector3<float> neighbor;
|
Vector3<float> n;
|
||||||
load_from_vector(raw, offset, neighbor);
|
if (!(iss >> n.x >> n.y >> n.z))
|
||||||
neighbors.push_back(neighbor);
|
throw std::runtime_error("Deserialize: Unexpected end of data.");
|
||||||
|
neighbors.push_back(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_vertex_map.emplace(vertex, std::move(neighbors));
|
m_vertex_map.emplace(vertex, std::move(neighbors));
|
||||||
|
|
||||||
|
if (event != "-")
|
||||||
|
m_vertex_events.emplace(vertex, std::move(event));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace omath::pathfinding
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ project(unit_tests)
|
|||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
|
|
||||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/general/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/engines/*.cpp")
|
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/general/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/engines/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
|
||||||
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES} main.cpp)
|
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES} main.cpp)
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
|
|||||||
192
tests/general/mem_fd_helper.hpp
Normal file
192
tests/general/mem_fd_helper.hpp
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
#pragma once
|
||||||
|
// Cross-platform helper for creating binary test "files" without writing to disk where possible.
|
||||||
|
//
|
||||||
|
// Strategy:
|
||||||
|
// - Linux (non-Android, or Android API >= 30): memfd_create → /proc/self/fd/<N> (no disk I/O)
|
||||||
|
// - All other platforms: anonymous temp file via std::tmpfile(), accessed via /proc/self/fd/<N>
|
||||||
|
// on Linux, or a named temp file (cleaned up on destruction) elsewhere.
|
||||||
|
//
|
||||||
|
// Usage:
|
||||||
|
// auto f = MemFdFile::create(myVector);
|
||||||
|
// ASSERT_TRUE(f.valid());
|
||||||
|
// scanner.scan_for_pattern_in_file(f.path(), ...);
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# if defined(__ANDROID__)
|
||||||
|
# if __ANDROID_API__ >= 30
|
||||||
|
# include <sys/mman.h>
|
||||||
|
# define OMATH_TEST_USE_MEMFD 1
|
||||||
|
# endif
|
||||||
|
// Android < 30: fall through to tmpfile() path below
|
||||||
|
# else
|
||||||
|
// Desktop Linux: memfd_create available since glibc 2.27 / kernel 3.17
|
||||||
|
# include <sys/mman.h>
|
||||||
|
# define OMATH_TEST_USE_MEMFD 1
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class MemFdFile
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MemFdFile() = default;
|
||||||
|
|
||||||
|
~MemFdFile()
|
||||||
|
{
|
||||||
|
#if defined(OMATH_TEST_USE_MEMFD)
|
||||||
|
if (m_fd >= 0)
|
||||||
|
::close(m_fd);
|
||||||
|
#else
|
||||||
|
if (!m_temp_path.empty())
|
||||||
|
std::filesystem::remove(m_temp_path);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
MemFdFile(const MemFdFile&) = delete;
|
||||||
|
MemFdFile& operator=(const MemFdFile&) = delete;
|
||||||
|
|
||||||
|
MemFdFile(MemFdFile&& o) noexcept
|
||||||
|
: m_path(std::move(o.m_path))
|
||||||
|
#if defined(OMATH_TEST_USE_MEMFD)
|
||||||
|
, m_fd(o.m_fd)
|
||||||
|
#else
|
||||||
|
, m_temp_path(std::move(o.m_temp_path))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if defined(OMATH_TEST_USE_MEMFD)
|
||||||
|
o.m_fd = -1;
|
||||||
|
#else
|
||||||
|
o.m_temp_path.clear();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool valid() const { return !m_path.empty(); }
|
||||||
|
|
||||||
|
[[nodiscard]] const std::filesystem::path& path() const { return m_path; }
|
||||||
|
|
||||||
|
static MemFdFile create(const std::vector<std::uint8_t>& data)
|
||||||
|
{
|
||||||
|
return create(data.data(), data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
static MemFdFile create(const std::uint8_t* data, std::size_t size)
|
||||||
|
{
|
||||||
|
MemFdFile f;
|
||||||
|
|
||||||
|
#if defined(OMATH_TEST_USE_MEMFD)
|
||||||
|
f.m_fd = static_cast<int>(::memfd_create("test_bin", 0));
|
||||||
|
if (f.m_fd < 0)
|
||||||
|
return f;
|
||||||
|
|
||||||
|
if (!write_all(f.m_fd, data, size))
|
||||||
|
{
|
||||||
|
::close(f.m_fd);
|
||||||
|
f.m_fd = -1;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
f.m_path = "/proc/self/fd/" + std::to_string(f.m_fd);
|
||||||
|
|
||||||
|
#else
|
||||||
|
// Portable fallback: write to a uniquely-named temp file and delete on destruction
|
||||||
|
const auto tmp_dir = std::filesystem::temp_directory_path();
|
||||||
|
std::mt19937_64 rng(std::random_device{}());
|
||||||
|
const auto unique_name = "omath_test_" + std::to_string(rng()) + ".bin";
|
||||||
|
f.m_temp_path = (tmp_dir / unique_name).string();
|
||||||
|
f.m_path = f.m_temp_path;
|
||||||
|
|
||||||
|
std::ofstream out(f.m_temp_path, std::ios::binary | std::ios::trunc);
|
||||||
|
if (!out.is_open())
|
||||||
|
{
|
||||||
|
f.m_temp_path.clear();
|
||||||
|
f.m_path.clear();
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
out.write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));
|
||||||
|
if (!out)
|
||||||
|
{
|
||||||
|
out.close();
|
||||||
|
std::filesystem::remove(f.m_temp_path);
|
||||||
|
f.m_temp_path.clear();
|
||||||
|
f.m_path.clear();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::filesystem::path m_path;
|
||||||
|
|
||||||
|
#if defined(OMATH_TEST_USE_MEMFD)
|
||||||
|
int m_fd = -1;
|
||||||
|
|
||||||
|
static bool write_all(int fd, const std::uint8_t* data, std::size_t size)
|
||||||
|
{
|
||||||
|
std::size_t written = 0;
|
||||||
|
while (written < size)
|
||||||
|
{
|
||||||
|
const auto n = ::write(fd, data + written, size - written);
|
||||||
|
if (n <= 0)
|
||||||
|
return false;
|
||||||
|
written += static_cast<std::size_t>(n);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
std::string m_temp_path;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Build a minimal PE binary in-memory with a single .text section.
|
||||||
|
// Layout (all offsets compile-time):
|
||||||
|
// 0x00: DOS header (64 B) 0x40: pad 0x80: NT sig 0x84: FileHeader (20 B)
|
||||||
|
// 0x98: OptionalHeader (0xF0 B) 0x188: SectionHeader (44 B) 0x1B4: section data
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
inline std::vector<std::uint8_t> build_minimal_pe(const std::vector<std::uint8_t>& section_bytes)
|
||||||
|
{
|
||||||
|
constexpr std::uint32_t e_lfanew = 0x80u;
|
||||||
|
constexpr std::uint16_t size_opt = 0xF0u;
|
||||||
|
constexpr std::size_t nt_off = e_lfanew;
|
||||||
|
constexpr std::size_t fh_off = nt_off + 4;
|
||||||
|
constexpr std::size_t oh_off = fh_off + 20;
|
||||||
|
constexpr std::size_t sh_off = oh_off + size_opt;
|
||||||
|
constexpr std::size_t data_off = sh_off + 44;
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> buf(data_off + section_bytes.size(), 0u);
|
||||||
|
|
||||||
|
buf[0] = 'M'; buf[1] = 'Z';
|
||||||
|
std::memcpy(buf.data() + 0x3Cu, &e_lfanew, 4);
|
||||||
|
|
||||||
|
buf[nt_off] = 'P'; buf[nt_off + 1] = 'E';
|
||||||
|
|
||||||
|
const std::uint16_t machine = 0x8664u, num_sections = 1u;
|
||||||
|
std::memcpy(buf.data() + fh_off, &machine, 2);
|
||||||
|
std::memcpy(buf.data() + fh_off + 2, &num_sections, 2);
|
||||||
|
std::memcpy(buf.data() + fh_off + 16, &size_opt, 2);
|
||||||
|
|
||||||
|
const std::uint16_t magic = 0x20Bu;
|
||||||
|
std::memcpy(buf.data() + oh_off, &magic, 2);
|
||||||
|
|
||||||
|
const char name[8] = {'.','t','e','x','t',0,0,0};
|
||||||
|
std::memcpy(buf.data() + sh_off, name, 8);
|
||||||
|
|
||||||
|
const auto vsize = static_cast<std::uint32_t>(section_bytes.size());
|
||||||
|
const std::uint32_t vaddr = 0x1000u;
|
||||||
|
const auto ptr_raw = static_cast<std::uint32_t>(data_off);
|
||||||
|
std::memcpy(buf.data() + sh_off + 8, &vsize, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 12, &vaddr, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 16, &vsize, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 20, &ptr_raw, 4);
|
||||||
|
|
||||||
|
std::memcpy(buf.data() + data_off, section_bytes.data(), section_bytes.size());
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
@@ -8,6 +8,29 @@
|
|||||||
using namespace omath;
|
using namespace omath;
|
||||||
using namespace omath::pathfinding;
|
using namespace omath::pathfinding;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static NavigationMesh make_linear_chain(int length)
|
||||||
|
{
|
||||||
|
// 0 -> 1 -> 2 -> ... -> length-1 (directed)
|
||||||
|
NavigationMesh nav;
|
||||||
|
for (int i = 0; i < length; ++i)
|
||||||
|
{
|
||||||
|
const Vector3<float> v{static_cast<float>(i), 0.f, 0.f};
|
||||||
|
if (i + 1 < length)
|
||||||
|
nav.m_vertex_map[v] = {Vector3<float>{static_cast<float>(i + 1), 0.f, 0.f}};
|
||||||
|
else
|
||||||
|
nav.m_vertex_map[v] = {};
|
||||||
|
}
|
||||||
|
return nav;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Basic reachability
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
TEST(AStarExtra, TrivialNeighbor)
|
TEST(AStarExtra, TrivialNeighbor)
|
||||||
{
|
{
|
||||||
NavigationMesh nav;
|
NavigationMesh nav;
|
||||||
@@ -78,7 +101,7 @@ TEST(AStarExtra, LongerPathAvoidsBlock)
|
|||||||
constexpr Vector3<float> goal = idx(2, 1);
|
constexpr Vector3<float> goal = idx(2, 1);
|
||||||
const auto path = Astar::find_path(start, goal, nav);
|
const auto path = Astar::find_path(start, goal, nav);
|
||||||
ASSERT_FALSE(path.empty());
|
ASSERT_FALSE(path.empty());
|
||||||
EXPECT_EQ(path.front(), goal); // Astar convention: single-element or endpoint present
|
EXPECT_EQ(path.front(), goal);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AstarTests, TrivialDirectNeighborPath)
|
TEST(AstarTests, TrivialDirectNeighborPath)
|
||||||
@@ -91,9 +114,6 @@ TEST(AstarTests, TrivialDirectNeighborPath)
|
|||||||
nav.m_vertex_map.emplace(v2, std::vector<Vector3<float>>{v1});
|
nav.m_vertex_map.emplace(v2, std::vector<Vector3<float>>{v1});
|
||||||
|
|
||||||
const auto path = Astar::find_path(v1, v2, nav);
|
const auto path = Astar::find_path(v1, v2, nav);
|
||||||
// Current A* implementation returns the end vertex as the reconstructed
|
|
||||||
// path (single-element) in the simple neighbor scenario. Assert that the
|
|
||||||
// endpoint is present and reachable.
|
|
||||||
ASSERT_EQ(path.size(), 1u);
|
ASSERT_EQ(path.size(), 1u);
|
||||||
EXPECT_EQ(path.front(), v2);
|
EXPECT_EQ(path.front(), v2);
|
||||||
}
|
}
|
||||||
@@ -133,4 +153,155 @@ TEST(unit_test_a_star, finding_right_path)
|
|||||||
mesh.m_vertex_map[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
mesh.m_vertex_map[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
||||||
mesh.m_vertex_map[{0.f, 3.f, 0.f}] = {};
|
mesh.m_vertex_map[{0.f, 3.f, 0.f}] = {};
|
||||||
std::ignore = omath::pathfinding::Astar::find_path({}, {0.f, 3.f, 0.f}, mesh);
|
std::ignore = omath::pathfinding::Astar::find_path({}, {0.f, 3.f, 0.f}, mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Directed edges
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(AstarTests, DirectedEdge_ForwardPathExists)
|
||||||
|
{
|
||||||
|
// A -> B only; path from A to B should succeed
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[a] = {b};
|
||||||
|
nav.m_vertex_map[b] = {}; // no edge back
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(a, b, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), b);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AstarTests, DirectedEdge_ReversePathMissing)
|
||||||
|
{
|
||||||
|
// A -> B only; path from B to A should fail
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[a] = {b};
|
||||||
|
nav.m_vertex_map[b] = {};
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(b, a, nav);
|
||||||
|
EXPECT_TRUE(path.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Vertex snapping
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(AstarTests, OffMeshStart_SnapsToNearestVertex)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> v1{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> v2{10.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[v1] = {v2};
|
||||||
|
nav.m_vertex_map[v2] = {v1};
|
||||||
|
|
||||||
|
// Start is slightly off v1 but closer to it than to v2
|
||||||
|
constexpr Vector3<float> off_start{0.1f, 0.f, 0.f};
|
||||||
|
const auto path = Astar::find_path(off_start, v2, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), v2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AstarTests, OffMeshEnd_SnapsToNearestVertex)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> v1{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> v2{10.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[v1] = {v2};
|
||||||
|
nav.m_vertex_map[v2] = {v1};
|
||||||
|
|
||||||
|
// Goal is slightly off v2 but closer to it than to v1
|
||||||
|
constexpr Vector3<float> off_goal{9.9f, 0.f, 0.f};
|
||||||
|
const auto path = Astar::find_path(v1, off_goal, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), v2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Cycle handling
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(AstarTests, CyclicGraph_FindsPathWithoutLooping)
|
||||||
|
{
|
||||||
|
// Triangle: A <-> B <-> C <-> A
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> c{0.5f, 1.f, 0.f};
|
||||||
|
nav.m_vertex_map[a] = {b, c};
|
||||||
|
nav.m_vertex_map[b] = {a, c};
|
||||||
|
nav.m_vertex_map[c] = {a, b};
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(a, c, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AstarTests, SelfLoopVertex_DoesNotBreakSearch)
|
||||||
|
{
|
||||||
|
// Vertex with itself as a neighbor
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[a] = {a, b}; // self-loop on a
|
||||||
|
nav.m_vertex_map[b] = {a};
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(a, b, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Longer chains
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(AstarTests, LinearChain_ReachesEnd)
|
||||||
|
{
|
||||||
|
constexpr int kLength = 10;
|
||||||
|
const NavigationMesh nav = make_linear_chain(kLength);
|
||||||
|
|
||||||
|
const Vector3<float> start{0.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> goal{static_cast<float>(kLength - 1), 0.f, 0.f};
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(start, goal, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), goal);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AstarTests, LinearChain_MidpointReachable)
|
||||||
|
{
|
||||||
|
constexpr int kLength = 6;
|
||||||
|
const NavigationMesh nav = make_linear_chain(kLength);
|
||||||
|
|
||||||
|
const Vector3<float> start{0.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> mid{3.f, 0.f, 0.f};
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(start, mid, nav);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), mid);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Serialize -> pathfind integration
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(AstarTests, PathfindAfterSerializeDeserialize)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
constexpr Vector3<float> c{2.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map[a] = {b};
|
||||||
|
nav.m_vertex_map[b] = {a, c};
|
||||||
|
nav.m_vertex_map[c] = {b};
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
const auto path = Astar::find_path(a, c, nav2);
|
||||||
|
ASSERT_FALSE(path.empty());
|
||||||
|
EXPECT_EQ(path.back(), c);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@
|
|||||||
#include <omath/utility/macho_pattern_scan.hpp>
|
#include <omath/utility/macho_pattern_scan.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "mem_fd_helper.hpp"
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
|
|
||||||
@@ -16,11 +16,12 @@ namespace
|
|||||||
// Mach-O magic numbers
|
// Mach-O magic numbers
|
||||||
constexpr std::uint32_t mh_magic_64 = 0xFEEDFACF;
|
constexpr std::uint32_t mh_magic_64 = 0xFEEDFACF;
|
||||||
constexpr std::uint32_t mh_magic_32 = 0xFEEDFACE;
|
constexpr std::uint32_t mh_magic_32 = 0xFEEDFACE;
|
||||||
constexpr std::uint32_t lc_segment = 0x1;
|
constexpr std::uint32_t lc_segment = 0x1;
|
||||||
constexpr std::uint32_t lc_segment_64 = 0x19;
|
constexpr std::uint32_t lc_segment_64 = 0x19;
|
||||||
|
|
||||||
constexpr std::string_view segment_name = "__TEXT";
|
constexpr std::string_view segment_name = "__TEXT";
|
||||||
constexpr std::string_view section_name = "__text";
|
constexpr std::string_view section_name = "__text";
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
struct MachHeader64
|
struct MachHeader64
|
||||||
{
|
{
|
||||||
@@ -107,249 +108,174 @@ namespace
|
|||||||
};
|
};
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
// Helper function to create a minimal 64-bit Mach-O file with a __text section
|
// Build a minimal 64-bit Mach-O binary in-memory with a __text section
|
||||||
bool write_minimal_macho64_file(const std::string& path, const std::vector<std::uint8_t>& section_bytes)
|
std::vector<std::uint8_t> build_minimal_macho64(const std::vector<std::uint8_t>& section_bytes)
|
||||||
{
|
{
|
||||||
std::ofstream f(path, std::ios::binary);
|
constexpr std::size_t load_cmd_size = sizeof(SegmentCommand64) + sizeof(Section64);
|
||||||
if (!f.is_open())
|
const std::size_t section_offset = sizeof(MachHeader64) + load_cmd_size;
|
||||||
return false;
|
|
||||||
|
|
||||||
// Calculate sizes
|
std::vector<std::uint8_t> buf(section_offset + section_bytes.size(), 0u);
|
||||||
constexpr std::size_t header_size = sizeof(MachHeader64);
|
|
||||||
constexpr std::size_t segment_size = sizeof(SegmentCommand64);
|
|
||||||
constexpr std::size_t section_size = sizeof(Section64);
|
|
||||||
constexpr std::size_t load_cmd_size = segment_size + section_size;
|
|
||||||
// Section data will start after headers
|
|
||||||
const std::size_t section_offset = header_size + load_cmd_size;
|
|
||||||
|
|
||||||
// Create Mach-O header
|
auto* header = reinterpret_cast<MachHeader64*>(buf.data());
|
||||||
MachHeader64 header{};
|
header->magic = mh_magic_64;
|
||||||
header.magic = mh_magic_64;
|
header->cputype = 0x01000007; // CPU_TYPE_X86_64
|
||||||
header.cputype = 0x01000007; // CPU_TYPE_X86_64
|
header->cpusubtype = 0x3;
|
||||||
header.cpusubtype = 0x3; // CPU_SUBTYPE_X86_64_ALL
|
header->filetype = 0x2; // MH_EXECUTE
|
||||||
header.filetype = 0x2; // MH_EXECUTE
|
header->ncmds = 1;
|
||||||
header.ncmds = 1;
|
header->sizeofcmds = static_cast<std::uint32_t>(load_cmd_size);
|
||||||
header.sizeofcmds = static_cast<std::uint32_t>(load_cmd_size);
|
|
||||||
header.flags = 0;
|
|
||||||
header.reserved = 0;
|
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(&header), sizeof(header));
|
auto* segment = reinterpret_cast<SegmentCommand64*>(buf.data() + sizeof(MachHeader64));
|
||||||
|
segment->cmd = lc_segment_64;
|
||||||
|
segment->cmdsize = static_cast<std::uint32_t>(load_cmd_size);
|
||||||
|
std::ranges::copy(segment_name, segment->segname);
|
||||||
|
segment->vmaddr = 0x100000000;
|
||||||
|
segment->vmsize = section_bytes.size();
|
||||||
|
segment->fileoff = section_offset;
|
||||||
|
segment->filesize = section_bytes.size();
|
||||||
|
segment->maxprot = 7;
|
||||||
|
segment->initprot = 5;
|
||||||
|
segment->nsects = 1;
|
||||||
|
|
||||||
// Create segment command
|
auto* section = reinterpret_cast<Section64*>(buf.data() + sizeof(MachHeader64) + sizeof(SegmentCommand64));
|
||||||
SegmentCommand64 segment{};
|
std::ranges::copy(section_name, section->sectname);
|
||||||
segment.cmd = lc_segment_64;
|
std::ranges::copy(segment_name, section->segname);
|
||||||
segment.cmdsize = static_cast<std::uint32_t>(load_cmd_size);
|
section->addr = 0x100000000;
|
||||||
std::ranges::copy(segment_name, segment.segname);
|
section->size = section_bytes.size();
|
||||||
segment.vmaddr = 0x100000000;
|
section->offset = static_cast<std::uint32_t>(section_offset);
|
||||||
segment.vmsize = section_bytes.size();
|
|
||||||
segment.fileoff = section_offset;
|
|
||||||
segment.filesize = section_bytes.size();
|
|
||||||
segment.maxprot = 7; // VM_PROT_ALL
|
|
||||||
segment.initprot = 5; // VM_PROT_READ | VM_PROT_EXECUTE
|
|
||||||
segment.nsects = 1;
|
|
||||||
segment.flags = 0;
|
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(&segment), sizeof(segment));
|
std::memcpy(buf.data() + section_offset, section_bytes.data(), section_bytes.size());
|
||||||
|
return buf;
|
||||||
// Create section
|
|
||||||
Section64 section{};
|
|
||||||
std::ranges::copy(section_name, section.sectname);
|
|
||||||
std::ranges::copy(segment_name, segment.segname);
|
|
||||||
section.addr = 0x100000000;
|
|
||||||
section.size = section_bytes.size();
|
|
||||||
section.offset = static_cast<std::uint32_t>(section_offset);
|
|
||||||
section.align = 0;
|
|
||||||
section.reloff = 0;
|
|
||||||
section.nreloc = 0;
|
|
||||||
section.flags = 0;
|
|
||||||
section.reserved1 = 0;
|
|
||||||
section.reserved2 = 0;
|
|
||||||
section.reserved3 = 0;
|
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(§ion), sizeof(section));
|
|
||||||
|
|
||||||
// Write section data
|
|
||||||
f.write(reinterpret_cast<const char*>(section_bytes.data()), static_cast<std::streamsize>(section_bytes.size()));
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to create a minimal 32-bit Mach-O file with a __text section
|
// Build a minimal 32-bit Mach-O binary in-memory with a __text section
|
||||||
bool write_minimal_macho32_file(const std::string& path, const std::vector<std::uint8_t>& section_bytes)
|
std::vector<std::uint8_t> build_minimal_macho32(const std::vector<std::uint8_t>& section_bytes)
|
||||||
{
|
{
|
||||||
std::ofstream f(path, std::ios::binary);
|
constexpr std::size_t load_cmd_size = sizeof(SegmentCommand32) + sizeof(Section32);
|
||||||
if (!f.is_open())
|
constexpr std::size_t section_offset = sizeof(MachHeader32) + load_cmd_size;
|
||||||
return false;
|
|
||||||
|
|
||||||
// Calculate sizes
|
std::vector<std::uint8_t> buf(section_offset + section_bytes.size(), 0u);
|
||||||
constexpr std::size_t header_size = sizeof(MachHeader32);
|
|
||||||
constexpr std::size_t segment_size = sizeof(SegmentCommand32);
|
|
||||||
constexpr std::size_t section_size = sizeof(Section32);
|
|
||||||
constexpr std::size_t load_cmd_size = segment_size + section_size;
|
|
||||||
|
|
||||||
// Section data will start after headers
|
auto* header = reinterpret_cast<MachHeader32*>(buf.data());
|
||||||
constexpr std::size_t section_offset = header_size + load_cmd_size;
|
header->magic = mh_magic_32;
|
||||||
|
header->cputype = 0x7;
|
||||||
|
header->cpusubtype = 0x3;
|
||||||
|
header->filetype = 0x2;
|
||||||
|
header->ncmds = 1;
|
||||||
|
header->sizeofcmds = static_cast<std::uint32_t>(load_cmd_size);
|
||||||
|
|
||||||
// Create Mach-O header
|
auto* segment = reinterpret_cast<SegmentCommand32*>(buf.data() + sizeof(MachHeader32));
|
||||||
MachHeader32 header{};
|
segment->cmd = lc_segment;
|
||||||
header.magic = mh_magic_32;
|
segment->cmdsize = static_cast<std::uint32_t>(load_cmd_size);
|
||||||
header.cputype = 0x7; // CPU_TYPE_X86
|
std::ranges::copy(segment_name, segment->segname);
|
||||||
header.cpusubtype = 0x3; // CPU_SUBTYPE_X86_ALL
|
segment->vmaddr = 0x1000;
|
||||||
header.filetype = 0x2; // MH_EXECUTE
|
segment->vmsize = static_cast<std::uint32_t>(section_bytes.size());
|
||||||
header.ncmds = 1;
|
segment->fileoff = static_cast<std::uint32_t>(section_offset);
|
||||||
header.sizeofcmds = static_cast<std::uint32_t>(load_cmd_size);
|
segment->filesize = static_cast<std::uint32_t>(section_bytes.size());
|
||||||
header.flags = 0;
|
segment->maxprot = 7;
|
||||||
|
segment->initprot = 5;
|
||||||
|
segment->nsects = 1;
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(&header), sizeof(header));
|
auto* section = reinterpret_cast<Section32*>(buf.data() + sizeof(MachHeader32) + sizeof(SegmentCommand32));
|
||||||
|
std::ranges::copy(section_name, section->sectname);
|
||||||
|
std::ranges::copy(segment_name, section->segname);
|
||||||
|
section->addr = 0x1000;
|
||||||
|
section->size = static_cast<std::uint32_t>(section_bytes.size());
|
||||||
|
section->offset = static_cast<std::uint32_t>(section_offset);
|
||||||
|
|
||||||
// Create segment command
|
std::memcpy(buf.data() + section_offset, section_bytes.data(), section_bytes.size());
|
||||||
SegmentCommand32 segment{};
|
return buf;
|
||||||
segment.cmd = lc_segment;
|
|
||||||
segment.cmdsize = static_cast<std::uint32_t>(load_cmd_size);
|
|
||||||
std::ranges::copy(segment_name, segment.segname);
|
|
||||||
segment.vmaddr = 0x1000;
|
|
||||||
segment.vmsize = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
segment.fileoff = static_cast<std::uint32_t>(section_offset);
|
|
||||||
segment.filesize = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
segment.maxprot = 7; // VM_PROT_ALL
|
|
||||||
segment.initprot = 5; // VM_PROT_READ | VM_PROT_EXECUTE
|
|
||||||
segment.nsects = 1;
|
|
||||||
segment.flags = 0;
|
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(&segment), sizeof(segment));
|
|
||||||
|
|
||||||
// Create section
|
|
||||||
Section32 section{};
|
|
||||||
std::ranges::copy(section_name, section.sectname);
|
|
||||||
std::ranges::copy(segment_name, segment.segname);
|
|
||||||
section.addr = 0x1000;
|
|
||||||
section.size = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
section.offset = static_cast<std::uint32_t>(section_offset);
|
|
||||||
section.align = 0;
|
|
||||||
section.reloff = 0;
|
|
||||||
section.nreloc = 0;
|
|
||||||
section.flags = 0;
|
|
||||||
section.reserved1 = 0;
|
|
||||||
section.reserved2 = 0;
|
|
||||||
|
|
||||||
f.write(reinterpret_cast<const char*>(§ion), sizeof(section));
|
|
||||||
|
|
||||||
// Write section data
|
|
||||||
f.write(reinterpret_cast<const char*>(section_bytes.data()), static_cast<std::streamsize>(section_bytes.size()));
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// Test scanning for a pattern that exists in a 64-bit Mach-O file
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanFindsPattern64)
|
TEST(unit_test_macho_pattern_scan_file, ScanFindsPattern64)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho64.bin";
|
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5, 0x90, 0x90};
|
||||||
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5, 0x90, 0x90}; // push rbp; mov rbp, rsp; nop; nop
|
const auto f = MemFdFile::create(build_minimal_macho64(bytes));
|
||||||
ASSERT_TRUE(write_minimal_macho64_file(path.data(), bytes));
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 48 89 E5", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 48 89 E5", "__text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
|
||||||
EXPECT_EQ(res->target_offset, 0);
|
EXPECT_EQ(res->target_offset, 0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning for a pattern that exists in a 32-bit Mach-O file
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanFindsPattern32)
|
TEST(unit_test_macho_pattern_scan_file, ScanFindsPattern32)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho32.bin";
|
const std::vector<std::uint8_t> bytes = {0x55, 0x89, 0xE5, 0x90, 0x90};
|
||||||
const std::vector<std::uint8_t> bytes = {0x55, 0x89, 0xE5, 0x90, 0x90}; // push ebp; mov ebp, esp; nop; nop
|
const auto f = MemFdFile::create(build_minimal_macho32(bytes));
|
||||||
ASSERT_TRUE(write_minimal_macho32_file(path.data(), bytes));
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 89 E5", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 89 E5", "__text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
|
||||||
EXPECT_EQ(res->target_offset, 0);
|
EXPECT_EQ(res->target_offset, 0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning for a pattern that does not exist
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanMissingPattern)
|
TEST(unit_test_macho_pattern_scan_file, ScanMissingPattern)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho_missing.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x00, 0x01, 0x02, 0x03};
|
const std::vector<std::uint8_t> bytes = {0x00, 0x01, 0x02, 0x03};
|
||||||
ASSERT_TRUE(write_minimal_macho64_file(path.data(), bytes));
|
const auto f = MemFdFile::create(build_minimal_macho64(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "FF EE DD", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "FF EE DD", "__text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning for a pattern at a non-zero offset
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanPatternAtOffset)
|
TEST(unit_test_macho_pattern_scan_file, ScanPatternAtOffset)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho_offset.bin";
|
const std::vector<std::uint8_t> bytes = {0x90, 0x90, 0x90, 0x55, 0x48, 0x89, 0xE5};
|
||||||
const std::vector<std::uint8_t> bytes = {0x90, 0x90, 0x90, 0x55, 0x48, 0x89, 0xE5}; // nops then pattern
|
const auto f = MemFdFile::create(build_minimal_macho64(bytes));
|
||||||
ASSERT_TRUE(write_minimal_macho64_file(path.data(), bytes));
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 48 89 E5", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 48 89 E5", "__text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
|
||||||
EXPECT_EQ(res->target_offset, 3);
|
EXPECT_EQ(res->target_offset, 3);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning with wildcards
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanWithWildcard)
|
TEST(unit_test_macho_pattern_scan_file, ScanWithWildcard)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho_wildcard.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5, 0x90};
|
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5, 0x90};
|
||||||
ASSERT_TRUE(write_minimal_macho64_file(path.data(), bytes));
|
const auto f = MemFdFile::create(build_minimal_macho64(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 ? 89 E5", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 ? 89 E5", "__text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning a non-existent file
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanNonExistentFile)
|
TEST(unit_test_macho_pattern_scan_file, ScanNonExistentFile)
|
||||||
{
|
{
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file("/non/existent/file.bin", "55 48", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file("/non/existent/file.bin", "55 48", "__text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning an invalid (non-Mach-O) file
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanInvalidFile)
|
TEST(unit_test_macho_pattern_scan_file, ScanInvalidFile)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_invalid_macho.bin";
|
|
||||||
std::ofstream f(path.data(), std::ios::binary);
|
|
||||||
const std::vector<std::uint8_t> garbage = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
|
const std::vector<std::uint8_t> garbage = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
|
||||||
f.write(reinterpret_cast<const char*>(garbage.data()), static_cast<std::streamsize>(garbage.size()));
|
const auto f = MemFdFile::create(garbage);
|
||||||
f.close();
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 48", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 48", "__text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning for a non-existent section
|
|
||||||
TEST(unit_test_macho_pattern_scan_file, ScanNonExistentSection)
|
TEST(unit_test_macho_pattern_scan_file, ScanNonExistentSection)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_macho_nosect.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5};
|
const std::vector<std::uint8_t> bytes = {0x55, 0x48, 0x89, 0xE5};
|
||||||
ASSERT_TRUE(write_minimal_macho64_file(path.data(), bytes));
|
const auto f = MemFdFile::create(build_minimal_macho64(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_file(path, "55 48", "__nonexistent");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_file(f.path(), "55 48", "__nonexistent");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning with null module base address
|
|
||||||
TEST(unit_test_macho_pattern_scan_loaded, ScanNullModule)
|
TEST(unit_test_macho_pattern_scan_loaded, ScanNullModule)
|
||||||
{
|
{
|
||||||
const auto res = MachOPatternScanner::scan_for_pattern_in_loaded_module(nullptr, "55 48", "__text");
|
const auto res = MachOPatternScanner::scan_for_pattern_in_loaded_module(nullptr, "55 48", "__text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test scanning in loaded module with invalid magic
|
|
||||||
TEST(unit_test_macho_pattern_scan_loaded, ScanInvalidMagic)
|
TEST(unit_test_macho_pattern_scan_loaded, ScanInvalidMagic)
|
||||||
{
|
{
|
||||||
std::vector<std::uint8_t> invalid_data(256, 0x00);
|
std::vector<std::uint8_t> invalid_data(256, 0x00);
|
||||||
|
|||||||
@@ -7,19 +7,18 @@ using namespace omath::pathfinding;
|
|||||||
TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
||||||
{
|
{
|
||||||
NavigationMesh nav;
|
NavigationMesh nav;
|
||||||
Vector3<float> a{0.f,0.f,0.f};
|
Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
Vector3<float> b{1.f,0.f,0.f};
|
Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
Vector3<float> c{0.f,1.f,0.f};
|
Vector3<float> c{0.f, 1.f, 0.f};
|
||||||
|
|
||||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b,c});
|
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b, c});
|
||||||
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{a});
|
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{a});
|
||||||
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{a});
|
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{a});
|
||||||
|
|
||||||
auto data = nav.serialize();
|
std::string data = nav.serialize();
|
||||||
NavigationMesh nav2;
|
NavigationMesh nav2;
|
||||||
EXPECT_NO_THROW(nav2.deserialize(data));
|
EXPECT_NO_THROW(nav2.deserialize(data));
|
||||||
|
|
||||||
// verify neighbors preserved
|
|
||||||
EXPECT_EQ(nav2.m_vertex_map.size(), nav.m_vertex_map.size());
|
EXPECT_EQ(nav2.m_vertex_map.size(), nav.m_vertex_map.size());
|
||||||
EXPECT_EQ(nav2.get_neighbors(a).size(), 2u);
|
EXPECT_EQ(nav2.get_neighbors(a).size(), 2u);
|
||||||
}
|
}
|
||||||
@@ -27,7 +26,223 @@ TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
|||||||
TEST(NavigationMeshTests, GetClosestVertexWhenEmpty)
|
TEST(NavigationMeshTests, GetClosestVertexWhenEmpty)
|
||||||
{
|
{
|
||||||
const NavigationMesh nav;
|
const NavigationMesh nav;
|
||||||
constexpr Vector3<float> p{5.f,5.f,5.f};
|
constexpr Vector3<float> p{5.f, 5.f, 5.f};
|
||||||
const auto res = nav.get_closest_vertex(p);
|
const auto res = nav.get_closest_vertex(p);
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, SerializeEmptyMesh)
|
||||||
|
{
|
||||||
|
const NavigationMesh nav;
|
||||||
|
const std::string data = nav.serialize();
|
||||||
|
EXPECT_TRUE(data.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, DeserializeEmptyString)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
EXPECT_NO_THROW(nav.deserialize(""));
|
||||||
|
EXPECT_TRUE(nav.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, SerializeProducesHumanReadableText)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
nav.m_vertex_map.emplace(Vector3<float>{1.f, 2.f, 3.f}, std::vector<Vector3<float>>{{4.f, 5.f, 6.f}});
|
||||||
|
|
||||||
|
const std::string data = nav.serialize();
|
||||||
|
|
||||||
|
// Must contain the vertex and neighbor coords as plain text
|
||||||
|
EXPECT_NE(data.find("1"), std::string::npos);
|
||||||
|
EXPECT_NE(data.find("2"), std::string::npos);
|
||||||
|
EXPECT_NE(data.find("3"), std::string::npos);
|
||||||
|
EXPECT_NE(data.find("4"), std::string::npos);
|
||||||
|
EXPECT_NE(data.find("5"), std::string::npos);
|
||||||
|
EXPECT_NE(data.find("6"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, DeserializeRestoresNeighborValues)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{1.f, 2.f, 3.f};
|
||||||
|
const Vector3<float> n1{4.f, 5.f, 6.f};
|
||||||
|
const Vector3<float> n2{7.f, 8.f, 9.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{n1, n2});
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||||
|
const auto& neighbors = nav2.get_neighbors(v);
|
||||||
|
ASSERT_EQ(neighbors.size(), 2u);
|
||||||
|
EXPECT_EQ(neighbors[0], n1);
|
||||||
|
EXPECT_EQ(neighbors[1], n2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, DeserializeOverwritesPreviousData)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
// Load a different mesh into the same object
|
||||||
|
NavigationMesh other;
|
||||||
|
const Vector3<float> a{10.f, 20.f, 30.f};
|
||||||
|
other.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
nav.deserialize(other.serialize());
|
||||||
|
|
||||||
|
EXPECT_EQ(nav.m_vertex_map.size(), 1u);
|
||||||
|
EXPECT_EQ(nav.m_vertex_map.count(v), 0u);
|
||||||
|
EXPECT_EQ(nav.m_vertex_map.count(a), 1u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, RoundTripNegativeAndFractionalCoords)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{-1.5f, 0.25f, -3.75f};
|
||||||
|
const Vector3<float> n{100.f, -200.f, 0.001f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{n});
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||||
|
const auto& neighbors = nav2.get_neighbors(v);
|
||||||
|
ASSERT_EQ(neighbors.size(), 1u);
|
||||||
|
EXPECT_NEAR(neighbors[0].x, n.x, 1e-3f);
|
||||||
|
EXPECT_NEAR(neighbors[0].y, n.y, 1e-3f);
|
||||||
|
EXPECT_NEAR(neighbors[0].z, n.z, 1e-3f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, GetClosestVertexReturnsNearest)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> b{10.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||||
|
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
const auto res = nav.get_closest_vertex({1.f, 0.f, 0.f});
|
||||||
|
ASSERT_TRUE(res.has_value());
|
||||||
|
EXPECT_EQ(res.value(), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, VertexWithNoNeighborsRoundTrip)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{5.f, 5.f, 5.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||||
|
EXPECT_TRUE(nav2.get_neighbors(v).empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Vertex events
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, SetEventOnNonExistentVertexThrows)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{99.f, 99.f, 99.f};
|
||||||
|
EXPECT_THROW(nav.set_event(v, "jump"), std::invalid_argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, EventNotSetByDefault)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{0.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, SetAndGetEvent)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(v, "jump");
|
||||||
|
|
||||||
|
const auto event = nav.get_event(v);
|
||||||
|
ASSERT_TRUE(event.has_value());
|
||||||
|
EXPECT_EQ(event.value(), "jump");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, OverwriteEvent)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(v, "jump");
|
||||||
|
nav.set_event(v, "teleport");
|
||||||
|
|
||||||
|
EXPECT_EQ(nav.get_event(v).value(), "teleport");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, ClearEvent)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(v, "jump");
|
||||||
|
nav.clear_event(v);
|
||||||
|
|
||||||
|
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, EventRoundTripSerialization)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b});
|
||||||
|
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(b, "jump");
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
ASSERT_FALSE(nav2.get_event(a).has_value());
|
||||||
|
ASSERT_TRUE(nav2.get_event(b).has_value());
|
||||||
|
EXPECT_EQ(nav2.get_event(b).value(), "jump");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, MultipleEventsRoundTrip)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> b{1.f, 0.f, 0.f};
|
||||||
|
const Vector3<float> c{2.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||||
|
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||||
|
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(a, "spawn");
|
||||||
|
nav.set_event(c, "teleport");
|
||||||
|
|
||||||
|
NavigationMesh nav2;
|
||||||
|
nav2.deserialize(nav.serialize());
|
||||||
|
|
||||||
|
EXPECT_EQ(nav2.get_event(a).value(), "spawn");
|
||||||
|
EXPECT_FALSE(nav2.get_event(b).has_value());
|
||||||
|
EXPECT_EQ(nav2.get_event(c).value(), "teleport");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NavigationMeshTests, DeserializeClearsOldEvents)
|
||||||
|
{
|
||||||
|
NavigationMesh nav;
|
||||||
|
const Vector3<float> v{0.f, 0.f, 0.f};
|
||||||
|
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
nav.set_event(v, "jump");
|
||||||
|
|
||||||
|
// Deserialize a mesh that has no events
|
||||||
|
NavigationMesh empty_events;
|
||||||
|
empty_events.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||||
|
|
||||||
|
nav.deserialize(empty_events.serialize());
|
||||||
|
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,114 +1,28 @@
|
|||||||
// Unit test for PePatternScanner::scan_for_pattern_in_file using a synthetic PE-like file
|
// Unit test for PePatternScanner::scan_for_pattern_in_file using a synthetic PE-like file
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/utility/pe_pattern_scan.hpp>
|
#include <omath/utility/pe_pattern_scan.hpp>
|
||||||
#include <fstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <vector>
|
||||||
|
#include "mem_fd_helper.hpp"
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
|
|
||||||
// Helper: write a trivial PE-like file with DOS header and a single section named .text
|
|
||||||
static bool write_minimal_pe_file(const std::string& path, const std::vector<std::uint8_t>& section_bytes)
|
|
||||||
{
|
|
||||||
std::ofstream f(path, std::ios::binary);
|
|
||||||
if (!f.is_open()) return false;
|
|
||||||
|
|
||||||
// Write DOS header (e_magic = 0x5A4D, e_lfanew at offset 0x3C)
|
|
||||||
std::vector<std::uint8_t> dos(64, 0);
|
|
||||||
dos[0] = 'M'; dos[1] = 'Z';
|
|
||||||
// e_lfanew -> place NT headers right after DOS (offset 0x80)
|
|
||||||
std::uint32_t e_lfanew = 0x80;
|
|
||||||
std::memcpy(dos.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
|
||||||
f.write(reinterpret_cast<const char*>(dos.data()), dos.size());
|
|
||||||
|
|
||||||
// Pad up to e_lfanew
|
|
||||||
if (f.tellp() < static_cast<std::streampos>(e_lfanew))
|
|
||||||
{
|
|
||||||
std::vector<char> pad(e_lfanew - static_cast<std::uint32_t>(f.tellp()), 0);
|
|
||||||
f.write(pad.data(), pad.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
// NT headers signature 'PE\0\0'
|
|
||||||
f.put('P'); f.put('E'); f.put('\0'); f.put('\0');
|
|
||||||
|
|
||||||
// FileHeader: machine, num_sections
|
|
||||||
std::uint16_t machine = 0x8664; // x64
|
|
||||||
std::uint16_t num_sections = 1;
|
|
||||||
std::uint32_t dummy32 = 0;
|
|
||||||
std::uint32_t dummy32b = 0;
|
|
||||||
std::uint16_t size_optional = 0xF0; // reasonable
|
|
||||||
std::uint16_t characteristics = 0;
|
|
||||||
f.write(reinterpret_cast<const char*>(&machine), sizeof(machine));
|
|
||||||
f.write(reinterpret_cast<const char*>(&num_sections), sizeof(num_sections));
|
|
||||||
f.write(reinterpret_cast<const char*>(&dummy32), sizeof(dummy32));
|
|
||||||
f.write(reinterpret_cast<const char*>(&dummy32b), sizeof(dummy32b));
|
|
||||||
std::uint32_t num_symbols = 0;
|
|
||||||
f.write(reinterpret_cast<const char*>(&num_symbols), sizeof(num_symbols));
|
|
||||||
f.write(reinterpret_cast<const char*>(&size_optional), sizeof(size_optional));
|
|
||||||
f.write(reinterpret_cast<const char*>(&characteristics), sizeof(characteristics));
|
|
||||||
|
|
||||||
// OptionalHeader (x64) minimal: magic 0x20b, image_base, size_of_code, size_of_headers
|
|
||||||
std::uint16_t magic = 0x20b;
|
|
||||||
f.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
|
|
||||||
// filler for rest of optional header up to size_optional
|
|
||||||
std::vector<std::uint8_t> opt(size_optional - sizeof(magic), 0);
|
|
||||||
// set size_code near end
|
|
||||||
// we'll set image_base and size_code fields in reasonable positions for extractor
|
|
||||||
// For simplicity, leave zeros; extractor primarily uses optional_header.image_base and size_code later,
|
|
||||||
// but we will craft a SectionHeader that points to raw data we append below.
|
|
||||||
f.write(reinterpret_cast<const char*>(opt.data()), opt.size());
|
|
||||||
|
|
||||||
// Section header (name 8 bytes, then remaining 36 bytes)
|
|
||||||
char name[8] = {'.','t','e','x','t',0,0,0};
|
|
||||||
f.write(name, 8);
|
|
||||||
|
|
||||||
// Write placeholder bytes for the rest of the section header and remember its start
|
|
||||||
constexpr std::uint32_t section_header_rest = 36u;
|
|
||||||
const std::streampos header_rest_pos = f.tellp();
|
|
||||||
std::vector<char> placeholder(section_header_rest, 0);
|
|
||||||
f.write(placeholder.data(), placeholder.size());
|
|
||||||
|
|
||||||
// Now write section raw data and remember its file offset
|
|
||||||
const std::streampos data_pos = f.tellp();
|
|
||||||
f.write(reinterpret_cast<const char*>(section_bytes.data()), static_cast<std::streamsize>(section_bytes.size()));
|
|
||||||
|
|
||||||
// Patch section header fields: virtual_size, virtual_address, size_raw_data, ptr_raw_data
|
|
||||||
const std::uint32_t virtual_size = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
constexpr std::uint32_t virtual_address = 0x1000u;
|
|
||||||
const std::uint32_t size_raw_data = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
const std::uint32_t ptr_raw_data = static_cast<std::uint32_t>(data_pos);
|
|
||||||
|
|
||||||
// Seek back to the header_rest_pos and write fields in order
|
|
||||||
f.seekp(header_rest_pos, std::ios::beg);
|
|
||||||
f.write(reinterpret_cast<const char*>(&virtual_size), sizeof(virtual_size));
|
|
||||||
f.write(reinterpret_cast<const char*>(&virtual_address), sizeof(virtual_address));
|
|
||||||
f.write(reinterpret_cast<const char*>(&size_raw_data), sizeof(size_raw_data));
|
|
||||||
f.write(reinterpret_cast<const char*>(&ptr_raw_data), sizeof(ptr_raw_data));
|
|
||||||
|
|
||||||
// Seek back to end for consistency
|
|
||||||
f.seekp(0, std::ios::end);
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_file, ScanFindsPattern)
|
TEST(unit_test_pe_pattern_scan_file, ScanFindsPattern)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_pe.bin";
|
const std::vector<std::uint8_t> bytes = {0x55, 0x8B, 0xEC, 0x90, 0x90};
|
||||||
const std::vector<std::uint8_t> bytes = {0x55, 0x8B, 0xEC, 0x90, 0x90}; // pattern at offset 0
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path.data(), bytes));
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "55 8B EC", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "55 8B EC", ".text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_file, ScanMissingPattern)
|
TEST(unit_test_pe_pattern_scan_file, ScanMissingPattern)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_minimal_pe_2.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x00, 0x01, 0x02, 0x03};
|
const std::vector<std::uint8_t> bytes = {0x00, 0x01, 0x02, 0x03};
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path.data(), bytes));
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "FF EE DD", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "FF EE DD", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,120 +1,89 @@
|
|||||||
// Additional tests for PePatternScanner to exercise edge cases and loaded-module scanning
|
// Additional tests for PePatternScanner to exercise edge cases and loaded-module scanning
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/utility/pe_pattern_scan.hpp>
|
#include <omath/utility/pe_pattern_scan.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "mem_fd_helper.hpp"
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
|
|
||||||
static bool write_bytes(const std::string& path, const std::vector<std::uint8_t>& data)
|
|
||||||
{
|
|
||||||
std::ofstream f(path, std::ios::binary);
|
|
||||||
if (!f.is_open())
|
|
||||||
return false;
|
|
||||||
f.write(reinterpret_cast<const char*>(data.data()), data.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more, InvalidDosHeader)
|
TEST(unit_test_pe_pattern_scan_more, InvalidDosHeader)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_bad_dos.bin";
|
|
||||||
std::vector<std::uint8_t> data(128, 0);
|
std::vector<std::uint8_t> data(128, 0);
|
||||||
// write wrong magic
|
|
||||||
data[0] = 'N';
|
data[0] = 'N';
|
||||||
data[1] = 'Z';
|
data[1] = 'Z';
|
||||||
ASSERT_TRUE(write_bytes(path.data(), data));
|
const auto f = MemFdFile::create(data);
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "55 8B EC", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "55 8B EC", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more, InvalidNtSignature)
|
TEST(unit_test_pe_pattern_scan_more, InvalidNtSignature)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_bad_nt.bin";
|
|
||||||
std::vector<std::uint8_t> data(256, 0);
|
std::vector<std::uint8_t> data(256, 0);
|
||||||
// valid DOS header
|
|
||||||
data[0] = 'M';
|
data[0] = 'M';
|
||||||
data[1] = 'Z';
|
data[1] = 'Z';
|
||||||
// point e_lfanew to 0x80
|
|
||||||
constexpr std::uint32_t e_lfanew = 0x80;
|
constexpr std::uint32_t e_lfanew = 0x80;
|
||||||
std::memcpy(data.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
std::memcpy(data.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
||||||
// write garbage at e_lfanew (not 'PE\0\0')
|
|
||||||
data[e_lfanew + 0] = 'X';
|
data[e_lfanew + 0] = 'X';
|
||||||
data[e_lfanew + 1] = 'Y';
|
data[e_lfanew + 1] = 'Y';
|
||||||
data[e_lfanew + 2] = 'Z';
|
data[e_lfanew + 2] = 'Z';
|
||||||
data[e_lfanew + 3] = 'W';
|
data[e_lfanew + 3] = 'W';
|
||||||
ASSERT_TRUE(write_bytes(path.data(), data));
|
const auto f = MemFdFile::create(data);
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "55 8B EC", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "55 8B EC", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more, SectionNotFound)
|
TEST(unit_test_pe_pattern_scan_more, SectionNotFound)
|
||||||
{
|
{
|
||||||
// reuse minimal writer but with section named .data and search .text
|
// Minimal PE with a .data section; scanning for .text should fail
|
||||||
constexpr std::string_view path = "./test_section_not_found.bin";
|
constexpr std::uint32_t e_lfanew = 0x80u;
|
||||||
std::ofstream f(path.data(), std::ios::binary);
|
constexpr std::uint16_t size_opt = 0xF0u;
|
||||||
ASSERT_TRUE(f.is_open());
|
constexpr std::size_t nt_off = e_lfanew;
|
||||||
// DOS
|
constexpr std::size_t fh_off = nt_off + 4;
|
||||||
std::vector<std::uint8_t> dos(64, 0);
|
constexpr std::size_t oh_off = fh_off + 20;
|
||||||
dos[0] = 'M';
|
constexpr std::size_t sh_off = oh_off + size_opt;
|
||||||
dos[1] = 'Z';
|
constexpr std::size_t data_off = sh_off + 44;
|
||||||
std::uint32_t e_lfanew = 0x80;
|
|
||||||
std::memcpy(dos.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
|
||||||
f.write(reinterpret_cast<char*>(dos.data()), dos.size());
|
|
||||||
// pad
|
|
||||||
std::vector<char> pad(e_lfanew - static_cast<std::uint32_t>(f.tellp()), 0);
|
|
||||||
f.write(pad.data(), pad.size());
|
|
||||||
// NT sig
|
|
||||||
f.put('P');
|
|
||||||
f.put('E');
|
|
||||||
f.put('\0');
|
|
||||||
f.put('\0');
|
|
||||||
// FileHeader minimal
|
|
||||||
std::uint16_t machine = 0x8664;
|
|
||||||
std::uint16_t num_sections = 1;
|
|
||||||
std::uint32_t z = 0;
|
|
||||||
std::uint32_t z2 = 0;
|
|
||||||
std::uint32_t numsym = 0;
|
|
||||||
std::uint16_t size_opt = 0xF0;
|
|
||||||
std::uint16_t ch = 0;
|
|
||||||
f.write(reinterpret_cast<char*>(&machine), sizeof(machine));
|
|
||||||
f.write(reinterpret_cast<char*>(&num_sections), sizeof(num_sections));
|
|
||||||
f.write(reinterpret_cast<char*>(&z), sizeof(z));
|
|
||||||
f.write(reinterpret_cast<char*>(&z2), sizeof(z2));
|
|
||||||
f.write(reinterpret_cast<char*>(&numsym), sizeof(numsym));
|
|
||||||
f.write(reinterpret_cast<char*>(&size_opt), sizeof(size_opt));
|
|
||||||
f.write(reinterpret_cast<char*>(&ch), sizeof(ch));
|
|
||||||
// Optional header magic
|
|
||||||
std::uint16_t magic = 0x20b;
|
|
||||||
f.write(reinterpret_cast<char*>(&magic), sizeof(magic));
|
|
||||||
std::vector<std::uint8_t> opt(size_opt - sizeof(magic), 0);
|
|
||||||
f.write(reinterpret_cast<char*>(opt.data()), opt.size());
|
|
||||||
// Section header named .data
|
|
||||||
char name[8] = {'.', 'd', 'a', 't', 'a', 0, 0, 0};
|
|
||||||
f.write(name, 8);
|
|
||||||
std::uint32_t vs = 4, va = 0x1000, srd = 4, prd = 0x200;
|
|
||||||
f.write(reinterpret_cast<char*>(&vs), 4);
|
|
||||||
f.write(reinterpret_cast<char*>(&va), 4);
|
|
||||||
f.write(reinterpret_cast<char*>(&srd), 4);
|
|
||||||
f.write(reinterpret_cast<char*>(&prd), 4);
|
|
||||||
std::vector<char> rest(16, 0);
|
|
||||||
f.write(rest.data(), rest.size());
|
|
||||||
// section bytes
|
|
||||||
std::vector<std::uint8_t> sec = {0x00, 0x01, 0x02, 0x03};
|
|
||||||
f.write(reinterpret_cast<char*>(sec.data()), sec.size());
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
auto res = PePatternScanner::scan_for_pattern_in_file(path, "00 01", ".text");
|
const std::vector<std::uint8_t> sec_data = {0x00, 0x01, 0x02, 0x03};
|
||||||
|
std::vector<std::uint8_t> buf(data_off + sec_data.size(), 0u);
|
||||||
|
|
||||||
|
buf[0] = 'M'; buf[1] = 'Z';
|
||||||
|
std::memcpy(buf.data() + 0x3C, &e_lfanew, 4);
|
||||||
|
buf[nt_off] = 'P'; buf[nt_off + 1] = 'E';
|
||||||
|
|
||||||
|
const std::uint16_t machine = 0x8664u, num_sections = 1u;
|
||||||
|
std::memcpy(buf.data() + fh_off, &machine, 2);
|
||||||
|
std::memcpy(buf.data() + fh_off + 2, &num_sections, 2);
|
||||||
|
std::memcpy(buf.data() + fh_off + 16, &size_opt, 2);
|
||||||
|
|
||||||
|
const std::uint16_t magic = 0x20Bu;
|
||||||
|
std::memcpy(buf.data() + oh_off, &magic, 2);
|
||||||
|
|
||||||
|
const char name[8] = {'.','d','a','t','a',0,0,0};
|
||||||
|
std::memcpy(buf.data() + sh_off, name, 8);
|
||||||
|
|
||||||
|
const std::uint32_t vs = 4u, va = 0x1000u, srd = 4u, prd = static_cast<std::uint32_t>(data_off);
|
||||||
|
std::memcpy(buf.data() + sh_off + 8, &vs, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 12, &va, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 16, &srd, 4);
|
||||||
|
std::memcpy(buf.data() + sh_off + 20, &prd, 4);
|
||||||
|
std::memcpy(buf.data() + data_off, sec_data.data(), sec_data.size());
|
||||||
|
|
||||||
|
const auto f = MemFdFile::create(buf);
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "00 01", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more, LoadedModuleScanFinds)
|
TEST(unit_test_pe_pattern_scan_more, LoadedModuleScanFinds)
|
||||||
{
|
{
|
||||||
// Create an in-memory buffer that mimics loaded module layout
|
// Create an in-memory buffer that mimics loaded module layout
|
||||||
// Define local header structs matching those in source
|
|
||||||
struct DosHeader
|
struct DosHeader
|
||||||
{
|
{
|
||||||
std::uint16_t e_magic;
|
std::uint16_t e_magic;
|
||||||
@@ -158,9 +127,9 @@ TEST(unit_test_pe_pattern_scan_more, LoadedModuleScanFinds)
|
|||||||
std::uint32_t base_of_code;
|
std::uint32_t base_of_code;
|
||||||
std::uint64_t image_base;
|
std::uint64_t image_base;
|
||||||
std::uint32_t section_alignment;
|
std::uint32_t section_alignment;
|
||||||
std::uint32_t file_alignment; /* rest omitted */
|
std::uint32_t file_alignment;
|
||||||
std::uint32_t size_image;
|
std::uint32_t size_image;
|
||||||
std::uint32_t size_headers; /* keep space */
|
std::uint32_t size_headers;
|
||||||
std::uint8_t pad[200];
|
std::uint8_t pad[200];
|
||||||
};
|
};
|
||||||
struct SectionHeader
|
struct SectionHeader
|
||||||
@@ -188,44 +157,38 @@ TEST(unit_test_pe_pattern_scan_more, LoadedModuleScanFinds)
|
|||||||
};
|
};
|
||||||
|
|
||||||
const std::vector<std::uint8_t> pattern_bytes = {0xDE, 0xAD, 0xBE, 0xEF, 0x90};
|
const std::vector<std::uint8_t> pattern_bytes = {0xDE, 0xAD, 0xBE, 0xEF, 0x90};
|
||||||
constexpr std::uint32_t base_of_code = 0x200; // will place bytes at offset 0x200
|
constexpr std::uint32_t base_of_code = 0x200;
|
||||||
const std::uint32_t size_code = static_cast<std::uint32_t>(pattern_bytes.size());
|
const std::uint32_t size_code = static_cast<std::uint32_t>(pattern_bytes.size());
|
||||||
|
|
||||||
const std::uint32_t bufsize = 0x400 + size_code;
|
const std::uint32_t bufsize = 0x400 + size_code;
|
||||||
std::vector<std::uint8_t> buf(bufsize, 0);
|
std::vector<std::uint8_t> buf(bufsize, 0);
|
||||||
|
|
||||||
// DOS header
|
|
||||||
const auto dos = reinterpret_cast<DosHeader*>(buf.data());
|
const auto dos = reinterpret_cast<DosHeader*>(buf.data());
|
||||||
dos->e_magic = 0x5A4D;
|
dos->e_magic = 0x5A4D;
|
||||||
dos->e_lfanew = 0x80;
|
dos->e_lfanew = 0x80;
|
||||||
|
|
||||||
// NT headers
|
|
||||||
const auto nt = reinterpret_cast<ImageNtHeadersX64*>(buf.data() + dos->e_lfanew);
|
const auto nt = reinterpret_cast<ImageNtHeadersX64*>(buf.data() + dos->e_lfanew);
|
||||||
nt->signature = 0x4550; // 'PE\0\0'
|
nt->signature = 0x4550;
|
||||||
nt->file_header.machine = 0x8664;
|
nt->file_header.machine = 0x8664;
|
||||||
nt->file_header.num_sections = 1;
|
nt->file_header.num_sections = 1;
|
||||||
nt->file_header.size_optional_header = static_cast<std::uint16_t>(sizeof(OptionalHeaderX64));
|
nt->file_header.size_optional_header = static_cast<std::uint16_t>(sizeof(OptionalHeaderX64));
|
||||||
|
nt->optional_header.magic = 0x020B;
|
||||||
|
nt->optional_header.base_of_code = base_of_code;
|
||||||
|
nt->optional_header.size_code = size_code;
|
||||||
|
|
||||||
nt->optional_header.magic = 0x020B; // x64
|
|
||||||
nt->optional_header.base_of_code = base_of_code;
|
|
||||||
nt->optional_header.size_code = size_code;
|
|
||||||
|
|
||||||
// Compute section table offset: e_lfanew + 4 (sig) + FileHeader + OptionalHeader
|
|
||||||
const std::size_t section_table_off =
|
const std::size_t section_table_off =
|
||||||
static_cast<std::size_t>(dos->e_lfanew) + 4 + sizeof(FileHeader) + sizeof(OptionalHeaderX64);
|
static_cast<std::size_t>(dos->e_lfanew) + 4 + sizeof(FileHeader) + sizeof(OptionalHeaderX64);
|
||||||
nt->optional_header.size_headers = static_cast<std::uint32_t>(section_table_off + sizeof(SectionHeader));
|
nt->optional_header.size_headers = static_cast<std::uint32_t>(section_table_off + sizeof(SectionHeader));
|
||||||
|
|
||||||
// Section header (.text)
|
|
||||||
const auto sect = reinterpret_cast<SectionHeader*>(buf.data() + section_table_off);
|
const auto sect = reinterpret_cast<SectionHeader*>(buf.data() + section_table_off);
|
||||||
std::memset(sect, 0, sizeof(SectionHeader));
|
std::memset(sect, 0, sizeof(SectionHeader));
|
||||||
std::memcpy(sect->name, ".text", 5);
|
std::memcpy(sect->name, ".text", 5);
|
||||||
sect->virtual_size = size_code;
|
sect->virtual_size = size_code;
|
||||||
sect->virtual_address = base_of_code;
|
sect->virtual_address = base_of_code;
|
||||||
sect->size_raw_data = size_code;
|
sect->size_raw_data = size_code;
|
||||||
sect->ptr_raw_data = base_of_code;
|
sect->ptr_raw_data = base_of_code;
|
||||||
sect->characteristics = 0x60000020; // code | execute | read
|
sect->characteristics = 0x60000020;
|
||||||
|
|
||||||
// place code at base_of_code
|
|
||||||
std::memcpy(buf.data() + base_of_code, pattern_bytes.data(), pattern_bytes.size());
|
std::memcpy(buf.data() + base_of_code, pattern_bytes.data(), pattern_bytes.size());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_loaded_module(buf.data(), "DE AD BE EF", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_loaded_module(buf.data(), "DE AD BE EF", ".text");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/utility/pe_pattern_scan.hpp>
|
#include <omath/utility/pe_pattern_scan.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "mem_fd_helper.hpp"
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
|
|
||||||
@@ -19,95 +20,6 @@ struct TestFileHeader
|
|||||||
std::uint16_t characteristics;
|
std::uint16_t characteristics;
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool write_bytes(const std::string& path, const std::vector<std::uint8_t>& data)
|
|
||||||
{
|
|
||||||
std::ofstream f(path, std::ios::binary);
|
|
||||||
if (!f.is_open())
|
|
||||||
return false;
|
|
||||||
f.write(reinterpret_cast<const char*>(data.data()), data.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: write a trivial PE-like file with DOS header and a single section named .text
|
|
||||||
static bool write_minimal_pe_file(const std::string& path, const std::vector<std::uint8_t>& section_bytes)
|
|
||||||
{
|
|
||||||
std::ofstream f(path, std::ios::binary);
|
|
||||||
if (!f.is_open())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Write DOS header (e_magic = 0x5A4D, e_lfanew at offset 0x3C)
|
|
||||||
std::vector<std::uint8_t> dos(64, 0);
|
|
||||||
dos[0] = 'M';
|
|
||||||
dos[1] = 'Z';
|
|
||||||
std::uint32_t e_lfanew = 0x80;
|
|
||||||
std::memcpy(dos.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
|
||||||
f.write(reinterpret_cast<const char*>(dos.data()), dos.size());
|
|
||||||
|
|
||||||
// Pad up to e_lfanew
|
|
||||||
if (f.tellp() < static_cast<std::streampos>(e_lfanew))
|
|
||||||
{
|
|
||||||
std::vector<char> pad(e_lfanew - static_cast<std::uint32_t>(f.tellp()), 0);
|
|
||||||
f.write(pad.data(), pad.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
// NT headers signature 'PE\0\0'
|
|
||||||
f.put('P');
|
|
||||||
f.put('E');
|
|
||||||
f.put('\0');
|
|
||||||
f.put('\0');
|
|
||||||
|
|
||||||
// FileHeader minimal
|
|
||||||
std::uint16_t machine = 0x8664; // x64
|
|
||||||
std::uint16_t num_sections = 1;
|
|
||||||
std::uint32_t dummy32 = 0;
|
|
||||||
std::uint32_t dummy32b = 0;
|
|
||||||
std::uint16_t size_optional = 0xF0;
|
|
||||||
std::uint16_t characteristics = 0;
|
|
||||||
f.write(reinterpret_cast<const char*>(&machine), sizeof(machine));
|
|
||||||
f.write(reinterpret_cast<const char*>(&num_sections), sizeof(num_sections));
|
|
||||||
f.write(reinterpret_cast<const char*>(&dummy32), sizeof(dummy32));
|
|
||||||
f.write(reinterpret_cast<const char*>(&dummy32b), sizeof(dummy32b));
|
|
||||||
std::uint32_t num_symbols = 0;
|
|
||||||
f.write(reinterpret_cast<const char*>(&num_symbols), sizeof(num_symbols));
|
|
||||||
f.write(reinterpret_cast<const char*>(&size_optional), sizeof(size_optional));
|
|
||||||
f.write(reinterpret_cast<const char*>(&characteristics), sizeof(characteristics));
|
|
||||||
|
|
||||||
// OptionalHeader minimal filler
|
|
||||||
std::uint16_t magic = 0x20b;
|
|
||||||
f.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
|
|
||||||
std::vector<std::uint8_t> opt(size_optional - sizeof(magic), 0);
|
|
||||||
f.write(reinterpret_cast<const char*>(opt.data()), opt.size());
|
|
||||||
|
|
||||||
// Section header (name 8 bytes, then remaining 36 bytes)
|
|
||||||
char name[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
|
|
||||||
f.write(name, 8);
|
|
||||||
|
|
||||||
constexpr std::uint32_t section_header_rest = 36u;
|
|
||||||
const std::streampos header_rest_pos = f.tellp();
|
|
||||||
std::vector<char> placeholder(section_header_rest, 0);
|
|
||||||
f.write(placeholder.data(), placeholder.size());
|
|
||||||
|
|
||||||
// Now write section raw data and remember its file offset
|
|
||||||
const std::streampos data_pos = f.tellp();
|
|
||||||
f.write(reinterpret_cast<const char*>(section_bytes.data()), static_cast<std::streamsize>(section_bytes.size()));
|
|
||||||
|
|
||||||
// Patch section header fields
|
|
||||||
const std::uint32_t virtual_size = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
constexpr std::uint32_t virtual_address = 0x1000u;
|
|
||||||
const std::uint32_t size_raw_data = static_cast<std::uint32_t>(section_bytes.size());
|
|
||||||
const std::uint32_t ptr_raw_data = static_cast<std::uint32_t>(data_pos);
|
|
||||||
|
|
||||||
f.seekp(header_rest_pos, std::ios::beg);
|
|
||||||
f.write(reinterpret_cast<const char*>(&virtual_size), sizeof(virtual_size));
|
|
||||||
f.write(reinterpret_cast<const char*>(&virtual_address), sizeof(virtual_address));
|
|
||||||
f.write(reinterpret_cast<const char*>(&size_raw_data), sizeof(size_raw_data));
|
|
||||||
f.write(reinterpret_cast<const char*>(&ptr_raw_data), sizeof(ptr_raw_data));
|
|
||||||
f.seekp(0, std::ios::end);
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more2, LoadedModuleNullBaseReturnsNull)
|
TEST(unit_test_pe_pattern_scan_more2, LoadedModuleNullBaseReturnsNull)
|
||||||
{
|
{
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_loaded_module(nullptr, "DE AD");
|
const auto res = PePatternScanner::scan_for_pattern_in_loaded_module(nullptr, "DE AD");
|
||||||
@@ -116,7 +28,6 @@ TEST(unit_test_pe_pattern_scan_more2, LoadedModuleNullBaseReturnsNull)
|
|||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more2, LoadedModuleInvalidOptionalHeaderReturnsNull)
|
TEST(unit_test_pe_pattern_scan_more2, LoadedModuleInvalidOptionalHeaderReturnsNull)
|
||||||
{
|
{
|
||||||
// Construct in-memory buffer with DOS header but invalid optional header magic
|
|
||||||
std::vector<std::uint8_t> buf(0x200, 0);
|
std::vector<std::uint8_t> buf(0x200, 0);
|
||||||
struct DosHeader
|
struct DosHeader
|
||||||
{
|
{
|
||||||
@@ -128,19 +39,11 @@ TEST(unit_test_pe_pattern_scan_more2, LoadedModuleInvalidOptionalHeaderReturnsNu
|
|||||||
dos->e_magic = 0x5A4D;
|
dos->e_magic = 0x5A4D;
|
||||||
dos->e_lfanew = 0x80;
|
dos->e_lfanew = 0x80;
|
||||||
|
|
||||||
// Place an NT header with wrong optional magic at e_lfanew
|
|
||||||
const auto nt_ptr = buf.data() + dos->e_lfanew;
|
const auto nt_ptr = buf.data() + dos->e_lfanew;
|
||||||
// write signature
|
nt_ptr[0] = 'P'; nt_ptr[1] = 'E'; nt_ptr[2] = 0; nt_ptr[3] = 0;
|
||||||
nt_ptr[0] = 'P';
|
|
||||||
nt_ptr[1] = 'E';
|
|
||||||
nt_ptr[2] = 0;
|
|
||||||
nt_ptr[3] = 0;
|
|
||||||
// craft FileHeader with size_optional_header large enough
|
|
||||||
constexpr std::uint16_t size_opt = 0xE0;
|
constexpr std::uint16_t size_opt = 0xE0;
|
||||||
// file header starts at offset 4
|
std::memcpy(nt_ptr + 4 + 12, &size_opt, sizeof(size_opt));
|
||||||
std::memcpy(nt_ptr + 4 + 12, &size_opt,
|
|
||||||
sizeof(size_opt)); // size_optional_header located after 12 bytes into FileHeader
|
|
||||||
// write optional header magic to be invalid value
|
|
||||||
constexpr std::uint16_t bad_magic = 0x9999;
|
constexpr std::uint16_t bad_magic = 0x9999;
|
||||||
std::memcpy(nt_ptr + 4 + sizeof(std::uint32_t) + sizeof(std::uint16_t) + sizeof(std::uint16_t), &bad_magic,
|
std::memcpy(nt_ptr + 4 + sizeof(std::uint32_t) + sizeof(std::uint16_t) + sizeof(std::uint16_t), &bad_magic,
|
||||||
sizeof(bad_magic));
|
sizeof(bad_magic));
|
||||||
@@ -151,13 +54,11 @@ TEST(unit_test_pe_pattern_scan_more2, LoadedModuleInvalidOptionalHeaderReturnsNu
|
|||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more2, FileX86OptionalHeaderScanFindsPattern)
|
TEST(unit_test_pe_pattern_scan_more2, FileX86OptionalHeaderScanFindsPattern)
|
||||||
{
|
{
|
||||||
constexpr std::string_view path = "./test_pe_x86.bin";
|
|
||||||
const std::vector<std::uint8_t> pattern = {0xDE, 0xAD, 0xBE, 0xEF};
|
const std::vector<std::uint8_t> pattern = {0xDE, 0xAD, 0xBE, 0xEF};
|
||||||
|
const auto f = MemFdFile::create(build_minimal_pe(pattern));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
// Use helper from this file to write a consistent minimal PE file with .text section
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "DE AD BE EF", ".text");
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path.data(), pattern));
|
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "DE AD BE EF", ".text");
|
|
||||||
ASSERT_TRUE(res.has_value());
|
ASSERT_TRUE(res.has_value());
|
||||||
EXPECT_GE(res->virtual_base_addr, 0u);
|
EXPECT_GE(res->virtual_base_addr, 0u);
|
||||||
EXPECT_GE(res->raw_base_addr, 0u);
|
EXPECT_GE(res->raw_base_addr, 0u);
|
||||||
@@ -166,97 +67,73 @@ TEST(unit_test_pe_pattern_scan_more2, FileX86OptionalHeaderScanFindsPattern)
|
|||||||
|
|
||||||
TEST(unit_test_pe_pattern_scan_more2, FilePatternNotFoundReturnsNull)
|
TEST(unit_test_pe_pattern_scan_more2, FilePatternNotFoundReturnsNull)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_no_pattern.bin";
|
|
||||||
std::vector<std::uint8_t> data(512, 0);
|
std::vector<std::uint8_t> data(512, 0);
|
||||||
// minimal DOS/NT headers to make extract_section fail earlier or return empty data
|
data[0] = 'M'; data[1] = 'Z';
|
||||||
data[0] = 'M';
|
|
||||||
data[1] = 'Z';
|
|
||||||
constexpr std::uint32_t e_lfanew = 0x80;
|
constexpr std::uint32_t e_lfanew = 0x80;
|
||||||
std::memcpy(data.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
std::memcpy(data.data() + 0x3C, &e_lfanew, sizeof(e_lfanew));
|
||||||
// NT signature
|
data[e_lfanew + 0] = 'P'; data[e_lfanew + 1] = 'E';
|
||||||
data[e_lfanew + 0] = 'P';
|
|
||||||
data[e_lfanew + 1] = 'E';
|
|
||||||
data[e_lfanew + 2] = 0;
|
|
||||||
data[e_lfanew + 3] = 0;
|
|
||||||
// FileHeader: one section, size_optional_header set low
|
|
||||||
constexpr std::uint16_t num_sections = 1;
|
constexpr std::uint16_t num_sections = 1;
|
||||||
constexpr std::uint16_t size_optional_header = 0xE0;
|
constexpr std::uint16_t size_optional_header = 0xE0;
|
||||||
std::memcpy(data.data() + e_lfanew + 6, &num_sections, sizeof(num_sections));
|
std::memcpy(data.data() + e_lfanew + 6, &num_sections, sizeof(num_sections));
|
||||||
std::memcpy(data.data() + e_lfanew + 4 + 12, &size_optional_header, sizeof(size_optional_header));
|
std::memcpy(data.data() + e_lfanew + 4 + 12, &size_optional_header, sizeof(size_optional_header));
|
||||||
// Optional header magic x64
|
|
||||||
constexpr std::uint16_t magic = 0x020B;
|
constexpr std::uint16_t magic = 0x020B;
|
||||||
std::memcpy(data.data() + e_lfanew + 4 + sizeof(TestFileHeader), &magic, sizeof(magic));
|
std::memcpy(data.data() + e_lfanew + 4 + sizeof(TestFileHeader), &magic, sizeof(magic));
|
||||||
// Section header .text with small data that does not contain the pattern
|
|
||||||
constexpr std::size_t offset_to_segment_table = e_lfanew + 4 + sizeof(TestFileHeader) + size_optional_header;
|
constexpr std::size_t offset_to_segment_table = e_lfanew + 4 + sizeof(TestFileHeader) + size_optional_header;
|
||||||
constexpr char name[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
|
constexpr char name[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
|
||||||
std::memcpy(data.data() + offset_to_segment_table, name, 8);
|
std::memcpy(data.data() + offset_to_segment_table, name, 8);
|
||||||
std::uint32_t vs = 4, va = 0x1000, srd = 4, prd = 0x200;
|
std::uint32_t vs = 4, va = 0x1000, srd = 4, prd = 0x200;
|
||||||
std::memcpy(data.data() + offset_to_segment_table + 8, &vs, 4);
|
std::memcpy(data.data() + offset_to_segment_table + 8, &vs, 4);
|
||||||
std::memcpy(data.data() + offset_to_segment_table + 12, &va, 4);
|
std::memcpy(data.data() + offset_to_segment_table + 12, &va, 4);
|
||||||
std::memcpy(data.data() + offset_to_segment_table + 16, &srd, 4);
|
std::memcpy(data.data() + offset_to_segment_table + 16, &srd, 4);
|
||||||
std::memcpy(data.data() + offset_to_segment_table + 20, &prd, 4);
|
std::memcpy(data.data() + offset_to_segment_table + 20, &prd, 4);
|
||||||
// write file
|
|
||||||
ASSERT_TRUE(write_bytes(path, data));
|
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "AA BB CC", ".text");
|
const auto f = MemFdFile::create(data);
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "AA BB CC", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
// Extra tests for pe_pattern_scan edge cases (on-disk API)
|
|
||||||
|
|
||||||
TEST(PePatternScanMore2, PatternAtStartFound)
|
TEST(PePatternScanMore2, PatternAtStartFound)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_more_start.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x90, 0x01, 0x02, 0x03, 0x04};
|
const std::vector<std::uint8_t> bytes = {0x90, 0x01, 0x02, 0x03, 0x04};
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path, bytes));
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "90 01 02", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "90 01 02", ".text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PePatternScanMore2, PatternAtEndFound)
|
TEST(PePatternScanMore2, PatternAtEndFound)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_more_end.bin";
|
const std::vector<std::uint8_t> bytes = {0x00, 0x11, 0x22, 0x33, 0x44};
|
||||||
std::vector<std::uint8_t> bytes = {0x00, 0x11, 0x22, 0x33, 0x44};
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path, bytes));
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "22 33 44", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "22 33 44", ".text");
|
||||||
if (!res.has_value())
|
if (!res.has_value())
|
||||||
{
|
{
|
||||||
// Try to locate the section header and print the raw section bytes the scanner would read
|
// Debug: inspect section header via the memfd path
|
||||||
std::ifstream in(path, std::ios::binary);
|
std::ifstream in(f.path(), std::ios::binary);
|
||||||
ASSERT_TRUE(in.is_open());
|
if (in.is_open())
|
||||||
// search for ".text" name
|
|
||||||
in.seekg(0, std::ios::beg);
|
|
||||||
std::vector<char> filebuf((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
||||||
const auto it = std::search(filebuf.begin(), filebuf.end(), std::begin(".text"), std::end(".text") - 1);
|
|
||||||
if (it != filebuf.end())
|
|
||||||
{
|
{
|
||||||
const size_t pos = std::distance(filebuf.begin(), it);
|
std::vector<char> filebuf((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||||
// after name, next fields: virtual_size (4), virtual_address(4), size_raw_data(4), ptr_raw_data(4)
|
const auto it = std::search(filebuf.begin(), filebuf.end(), std::begin(".text"), std::end(".text") - 1);
|
||||||
const size_t meta_off = pos + 8;
|
if (it != filebuf.end())
|
||||||
uint32_t virtual_size{};
|
|
||||||
uint32_t virtual_address{};
|
|
||||||
uint32_t size_raw_data{};
|
|
||||||
uint32_t ptr_raw_data{};
|
|
||||||
std::memcpy(&virtual_size, filebuf.data() + meta_off, sizeof(virtual_size));
|
|
||||||
std::memcpy(&virtual_address, filebuf.data() + meta_off + 4, sizeof(virtual_address));
|
|
||||||
std::memcpy(&size_raw_data, filebuf.data() + meta_off + 8, sizeof(size_raw_data));
|
|
||||||
std::memcpy(&ptr_raw_data, filebuf.data() + meta_off + 12, sizeof(ptr_raw_data));
|
|
||||||
|
|
||||||
std::cerr << "Parsed section header: virtual_size=" << virtual_size << " virtual_address=0x" << std::hex
|
|
||||||
<< virtual_address << std::dec << " size_raw_data=" << size_raw_data
|
|
||||||
<< " ptr_raw_data=" << ptr_raw_data << "\n";
|
|
||||||
|
|
||||||
if (ptr_raw_data + size_raw_data <= filebuf.size())
|
|
||||||
{
|
{
|
||||||
std::cerr << "Extracted section bytes:\n";
|
const std::size_t pos = std::distance(filebuf.begin(), it);
|
||||||
for (size_t i = 0; i < size_raw_data; i += 16)
|
const std::size_t meta_off = pos + 8;
|
||||||
{
|
std::uint32_t virtual_size{}, virtual_address{}, size_raw_data{}, ptr_raw_data{};
|
||||||
std::fprintf(stderr, "%04zx: ", i);
|
std::memcpy(&virtual_size, filebuf.data() + meta_off, sizeof(virtual_size));
|
||||||
for (size_t j = 0; j < 16 && i + j < size_raw_data; ++j)
|
std::memcpy(&virtual_address, filebuf.data() + meta_off + 4, sizeof(virtual_address));
|
||||||
std::fprintf(stderr, "%02x ", static_cast<uint8_t>(filebuf[ptr_raw_data + i + j]));
|
std::memcpy(&size_raw_data, filebuf.data() + meta_off + 8, sizeof(size_raw_data));
|
||||||
std::fprintf(stderr, "\n");
|
std::memcpy(&ptr_raw_data, filebuf.data() + meta_off + 12, sizeof(ptr_raw_data));
|
||||||
}
|
std::cerr << "Parsed section header: virtual_size=" << virtual_size << " virtual_address=0x"
|
||||||
|
<< std::hex << virtual_address << std::dec << " size_raw_data=" << size_raw_data
|
||||||
|
<< " ptr_raw_data=" << ptr_raw_data << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,30 +142,30 @@ TEST(PePatternScanMore2, PatternAtEndFound)
|
|||||||
|
|
||||||
TEST(PePatternScanMore2, WildcardMatches)
|
TEST(PePatternScanMore2, WildcardMatches)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_more_wild.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0xDE, 0xAD, 0xBE, 0xEF};
|
const std::vector<std::uint8_t> bytes = {0xDE, 0xAD, 0xBE, 0xEF};
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path, bytes));
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "DE ?? BE", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "DE ?? BE", ".text");
|
||||||
EXPECT_TRUE(res.has_value());
|
EXPECT_TRUE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PePatternScanMore2, PatternLongerThanBuffer)
|
TEST(PePatternScanMore2, PatternLongerThanBuffer)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_more_small.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0xAA, 0xBB};
|
const std::vector<std::uint8_t> bytes = {0xAA, 0xBB};
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path, bytes));
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "AA BB CC", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "AA BB CC", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PePatternScanMore2, InvalidPatternParse)
|
TEST(PePatternScanMore2, InvalidPatternParse)
|
||||||
{
|
{
|
||||||
const std::string path = "./test_pe_more_invalid.bin";
|
|
||||||
const std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03};
|
const std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03};
|
||||||
ASSERT_TRUE(write_minimal_pe_file(path, bytes));
|
const auto f = MemFdFile::create(build_minimal_pe(bytes));
|
||||||
|
ASSERT_TRUE(f.valid());
|
||||||
|
|
||||||
const auto res = PePatternScanner::scan_for_pattern_in_file(path, "01 GG 03", ".text");
|
const auto res = PePatternScanner::scan_for_pattern_in_file(f.path(), "01 GG 03", ".text");
|
||||||
EXPECT_FALSE(res.has_value());
|
EXPECT_FALSE(res.has_value());
|
||||||
}
|
}
|
||||||
|
|||||||
66
tests/lua/pe_scanner_tests.lua
Normal file
66
tests/lua/pe_scanner_tests.lua
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
-- PatternScanner tests: generic scan over a Lua string buffer
|
||||||
|
|
||||||
|
function PatternScanner_FindsExactPattern()
|
||||||
|
local buf = "\x90\x01\x02\x03\x04"
|
||||||
|
local offset = omath.PatternScanner.scan(buf, "90 01 02")
|
||||||
|
assert(offset ~= nil, "expected pattern to be found")
|
||||||
|
assert(offset == 0, "expected offset 0, got " .. tostring(offset))
|
||||||
|
end
|
||||||
|
|
||||||
|
function PatternScanner_FindsPatternAtNonZeroOffset()
|
||||||
|
local buf = "\x00\x00\xAB\xCD\xEF"
|
||||||
|
local offset = omath.PatternScanner.scan(buf, "AB CD EF")
|
||||||
|
assert(offset ~= nil, "expected pattern to be found")
|
||||||
|
assert(offset == 2, "expected offset 2, got " .. tostring(offset))
|
||||||
|
end
|
||||||
|
|
||||||
|
function PatternScanner_WildcardMatches()
|
||||||
|
local buf = "\xDE\xAD\xBE\xEF"
|
||||||
|
local offset = omath.PatternScanner.scan(buf, "DE ?? BE")
|
||||||
|
assert(offset ~= nil, "expected wildcard match")
|
||||||
|
assert(offset == 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function PatternScanner_ReturnsNilWhenNotFound()
|
||||||
|
local buf = "\x01\x02\x03"
|
||||||
|
local offset = omath.PatternScanner.scan(buf, "AA BB CC")
|
||||||
|
assert(offset == nil, "expected nil for not-found pattern")
|
||||||
|
end
|
||||||
|
|
||||||
|
function PatternScanner_ReturnsNilForEmptyBuffer()
|
||||||
|
local offset = omath.PatternScanner.scan("", "90 01")
|
||||||
|
assert(offset == nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- PePatternScanner tests: scan_in_module uses FAKE_MODULE_BASE injected from C++
|
||||||
|
-- The fake module contains {0x90, 0x01, 0x02, 0x03, 0x04} placed at raw offset 0x200
|
||||||
|
|
||||||
|
function PeScanner_FindsExactPattern()
|
||||||
|
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 01 02")
|
||||||
|
assert(addr ~= nil, "expected pattern to be found in module")
|
||||||
|
local offset = addr - FAKE_MODULE_BASE
|
||||||
|
assert(offset == 0x200, string.format("expected offset 0x200, got 0x%X", offset))
|
||||||
|
end
|
||||||
|
|
||||||
|
function PeScanner_WildcardMatches()
|
||||||
|
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 ?? 02")
|
||||||
|
assert(addr ~= nil, "expected wildcard match in module")
|
||||||
|
local offset = addr - FAKE_MODULE_BASE
|
||||||
|
assert(offset == 0x200, string.format("expected offset 0x200, got 0x%X", offset))
|
||||||
|
end
|
||||||
|
|
||||||
|
function PeScanner_ReturnsNilWhenNotFound()
|
||||||
|
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "AA BB CC DD")
|
||||||
|
assert(addr == nil, "expected nil for not-found pattern")
|
||||||
|
end
|
||||||
|
|
||||||
|
function PeScanner_CustomSectionFallsBackToNil()
|
||||||
|
-- Request a section that doesn't exist in our fake module
|
||||||
|
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 01 02", ".rdata")
|
||||||
|
assert(addr == nil, "expected nil for wrong section name")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- SectionScanResult: verify the type is registered and tostring works on a C++-returned value
|
||||||
|
function SectionScanResult_TypeIsRegistered()
|
||||||
|
assert(omath.SectionScanResult ~= nil, "SectionScanResult type should be registered")
|
||||||
|
end
|
||||||
113
tests/lua/unit_test_lua_pe_scanner.cpp
Normal file
113
tests/lua/unit_test_lua_pe_scanner.cpp
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 10.03.2026.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <lua.hpp>
|
||||||
|
#include <omath/lua/lua.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
std::vector<std::uint8_t> make_fake_pe_module(std::uint32_t base_of_code, std::uint32_t size_code,
|
||||||
|
const std::vector<std::uint8_t>& code_bytes)
|
||||||
|
{
|
||||||
|
constexpr std::uint32_t e_lfanew = 0x80;
|
||||||
|
constexpr std::uint32_t nt_sig = 0x4550;
|
||||||
|
constexpr std::uint16_t opt_magic = 0x020B; // PE32+
|
||||||
|
constexpr std::uint16_t num_sections = 1;
|
||||||
|
constexpr std::uint16_t opt_hdr_size = 0xF0;
|
||||||
|
constexpr std::uint32_t section_table_off = e_lfanew + 4 + 20 + opt_hdr_size;
|
||||||
|
constexpr std::uint32_t section_hdr_size = 40;
|
||||||
|
constexpr std::uint32_t text_chars = 0x60000020;
|
||||||
|
|
||||||
|
const std::uint32_t headers_end = section_table_off + section_hdr_size;
|
||||||
|
const std::uint32_t code_end = base_of_code + size_code;
|
||||||
|
const std::uint32_t total_size = std::max(headers_end, code_end) + 0x100;
|
||||||
|
std::vector<std::uint8_t> buf(total_size, 0);
|
||||||
|
|
||||||
|
auto w16 = [&](std::size_t off, std::uint16_t v) { std::memcpy(buf.data() + off, &v, 2); };
|
||||||
|
auto w32 = [&](std::size_t off, std::uint32_t v) { std::memcpy(buf.data() + off, &v, 4); };
|
||||||
|
auto w64 = [&](std::size_t off, std::uint64_t v) { std::memcpy(buf.data() + off, &v, 8); };
|
||||||
|
|
||||||
|
w16(0x00, 0x5A4D);
|
||||||
|
w32(0x3C, e_lfanew);
|
||||||
|
w32(e_lfanew, nt_sig);
|
||||||
|
|
||||||
|
const std::size_t fh = e_lfanew + 4;
|
||||||
|
w16(fh + 2, num_sections);
|
||||||
|
w16(fh + 16, opt_hdr_size);
|
||||||
|
|
||||||
|
const std::size_t opt = fh + 20;
|
||||||
|
w16(opt + 0, opt_magic);
|
||||||
|
w32(opt + 4, size_code);
|
||||||
|
w32(opt + 20, base_of_code);
|
||||||
|
w64(opt + 24, 0);
|
||||||
|
w32(opt + 32, 0x1000);
|
||||||
|
w32(opt + 36, 0x200);
|
||||||
|
w32(opt + 56, code_end);
|
||||||
|
w32(opt + 60, headers_end);
|
||||||
|
w32(opt + 108, 0);
|
||||||
|
|
||||||
|
const std::size_t sh = section_table_off;
|
||||||
|
std::memcpy(buf.data() + sh, ".text", 5);
|
||||||
|
w32(sh + 8, size_code);
|
||||||
|
w32(sh + 12, base_of_code);
|
||||||
|
w32(sh + 16, size_code);
|
||||||
|
w32(sh + 20, base_of_code);
|
||||||
|
w32(sh + 36, text_chars);
|
||||||
|
|
||||||
|
if (base_of_code + code_bytes.size() <= buf.size())
|
||||||
|
std::memcpy(buf.data() + base_of_code, code_bytes.data(), code_bytes.size());
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
class LuaPeScanner : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
lua_State* L = nullptr;
|
||||||
|
std::vector<std::uint8_t> m_fake_module;
|
||||||
|
|
||||||
|
void SetUp() override
|
||||||
|
{
|
||||||
|
const std::vector<std::uint8_t> code = {0x90, 0x01, 0x02, 0x03, 0x04};
|
||||||
|
m_fake_module = make_fake_pe_module(0x200, static_cast<std::uint32_t>(code.size()), code);
|
||||||
|
|
||||||
|
L = luaL_newstate();
|
||||||
|
luaL_openlibs(L);
|
||||||
|
omath::lua::LuaInterpreter::register_lib(L);
|
||||||
|
|
||||||
|
lua_pushinteger(L, static_cast<lua_Integer>(
|
||||||
|
reinterpret_cast<std::uintptr_t>(m_fake_module.data())));
|
||||||
|
lua_setglobal(L, "FAKE_MODULE_BASE");
|
||||||
|
|
||||||
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/pe_scanner_tests.lua") != LUA_OK)
|
||||||
|
FAIL() << lua_tostring(L, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override { lua_close(L); }
|
||||||
|
|
||||||
|
void check(const char* func_name)
|
||||||
|
{
|
||||||
|
lua_getglobal(L, func_name);
|
||||||
|
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||||
|
{
|
||||||
|
FAIL() << lua_tostring(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(LuaPeScanner, PatternScanner_FindsExactPattern) { check("PatternScanner_FindsExactPattern"); }
|
||||||
|
TEST_F(LuaPeScanner, PatternScanner_FindsPatternAtOffset) { check("PatternScanner_FindsPatternAtNonZeroOffset"); }
|
||||||
|
TEST_F(LuaPeScanner, PatternScanner_WildcardMatches) { check("PatternScanner_WildcardMatches"); }
|
||||||
|
TEST_F(LuaPeScanner, PatternScanner_ReturnsNilWhenNotFound) { check("PatternScanner_ReturnsNilWhenNotFound"); }
|
||||||
|
TEST_F(LuaPeScanner, PatternScanner_ReturnsNilForEmptyBuffer){ check("PatternScanner_ReturnsNilForEmptyBuffer"); }
|
||||||
|
TEST_F(LuaPeScanner, PeScanner_FindsExactPattern) { check("PeScanner_FindsExactPattern"); }
|
||||||
|
TEST_F(LuaPeScanner, PeScanner_WildcardMatches) { check("PeScanner_WildcardMatches"); }
|
||||||
|
TEST_F(LuaPeScanner, PeScanner_ReturnsNilWhenNotFound) { check("PeScanner_ReturnsNilWhenNotFound"); }
|
||||||
|
TEST_F(LuaPeScanner, PeScanner_CustomSectionFallsBackToNil) { check("PeScanner_CustomSectionFallsBackToNil"); }
|
||||||
|
TEST_F(LuaPeScanner, SectionScanResult_TypeIsRegistered) { check("SectionScanResult_TypeIsRegistered"); }
|
||||||
Reference in New Issue
Block a user