<commit_before>// Copyright <NAME> 2010-2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This file is part of HadesMem.
// <http://www.raptorfactor.com/> <<EMAIL>>

#include "hadesmem/patcher.hpp"

#define BOOST_TEST_MODULE patcher
#include "hadesmem/detail/warning_disable_prefix.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/assign/std/vector.hpp>
#include "hadesmem/detail/warning_disable_suffix.hpp"
using namespace boost::assign;

#include "hadesmem/alloc.hpp"
#include "hadesmem/error.hpp"
#include "hadesmem/read.hpp"
#include "hadesmem/module.hpp"
#include "hadesmem/process.hpp"

// Boost.Test causes the following warning under GCC:
// error: base class 'struct boost::unit_test::ut_detail::nil_t' has a 
// non-virtual destructor [-Werror=effc++]
#if defined(HADESMEM_GCC)
#pragma GCC diagnostic ignored "-Weffc++"
#endif // #if defined(HADESMEM_GCC)

// TODO: Patcher constructor tests.
// TODO: Improve hook target checks.
// TODO: Fix annoying UAC popup (with manifest).

namespace
{
  
std::shared_ptr<hadesmem::PatchDetour> g_ptr_detour_1;

DWORD GetCurrentProcessId_Hook()
{
  BOOST_CHECK(g_ptr_detour_1->GetTrampoline() != nullptr);
  auto const ptr_orig = reinterpret_cast<DWORD (WINAPI*)()>(
    reinterpret_cast<DWORD_PTR>(g_ptr_detour_1->GetTrampoline()));
  return ptr_orig() * 2;
}

}

BOOST_AUTO_TEST_CASE(patcher)
{
  hadesmem::Process const process(::GetCurrentProcessId());

  hadesmem::Allocator const test_mem_1(&process, 0x1000);
  std::vector<BYTE> data_1;
  data_1 += 0x00, 0x11, 0x22, 0x33, 0x44;
  BOOST_REQUIRE(data_1.size() == 5);
  hadesmem::PatchRaw patch_1(&process, test_mem_1.GetBase(), data_1);
  auto const orig_1 = hadesmem::ReadVector<BYTE>(process, 
    test_mem_1.GetBase(), 5);
  patch_1.Apply();<commit_msg>* Note to self.<commit_after>// Copyright <NAME> 2010-2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This file is part of HadesMem.
// <http://www.raptorfactor.com/> <<EMAIL>>

#include "hadesmem/patcher.hpp"

#define BOOST_TEST_MODULE patcher
#include "hadesmem/detail/warning_disable_prefix.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/assign/std/vector.hpp>
#include "hadesmem/detail/warning_disable_suffix.hpp"
using namespace boost::assign;

#include "hadesmem/alloc.hpp"
#include "hadesmem/error.hpp"
#include "hadesmem/read.hpp"
#include "hadesmem/module.hpp"
#include "hadesmem/process.hpp"

// Boost.Test causes the following warning under GCC:
// error: base class 'struct boost::unit_test::ut_detail::nil_t' has a 
// non-virtual destructor [-Werror=effc++]
#if defined(HADESMEM_GCC)
#pragma GCC diagnostic ignored "-Weffc++"
#endif // #if defined(HADESMEM_GCC)

// TODO: Patcher constructor tests.
// TODO: Improve hook target checks.
// TODO: Fix annoying UAC popup (with manifest).
// TODO: Test Patcher on functions using 'manually' generated code to 
// ensure all cases are covered, and that the tests are consistent (which 
// the Windows API testing is not...).

namespace
{
  
std::shared_ptr<hadesmem::PatchDetour> g_ptr_detour_1;

DWORD GetCurrentProcessId_Hook()
{
  BOOST_CHECK(g_ptr_detour_1->GetTrampoline() != nullptr);
  auto const ptr_orig = reinterpret_cast<DWORD (WINAPI*)()>(
    reinterpret_cast<DWORD_PTR>(g_ptr_detour_1->GetTrampoline()));
  return ptr_orig() * 2;
}

}

BOOST_AUTO_TEST_CASE(patcher)
{
  hadesmem::Process const process(::GetCurrentProcessId());

  hadesmem::Allocator const test_mem_1(&process, 0x1000);
  std::vector<BYTE> data_1;
  data_1 += 0x00, 0x11, 0x22, 0x33, 0x44;
  BOOST_REQUIRE(data_1.size() == 5);
  hadesmem::PatchRaw patch_1(&process, test_mem_1.GetBase(), data_1);
  auto const orig_1 = hadesmem::ReadVector<BYTE>(process, 
    test_mem_1.GetBase(), 5);
  patch_1.Apply();
{'fraction_non_alphanumeric': 0.14058154235145384, 'fraction_numerical': 0.025284450063211124, 'mean_word_length': 5.2993630573248405, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356595', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1396, 'n_words': 314}