Skip to content

Commit

Permalink
add unit tests, add license
Browse files Browse the repository at this point in the history
  • Loading branch information
hero622 committed Dec 20, 2023
1 parent e7267a5 commit 29591ae
Show file tree
Hide file tree
Showing 9 changed files with 2,065 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Zyntex

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions photon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.33927.289
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "photon", "src\photon.vcxproj", "{FA989732-5D3F-4565-A5B2-ABD400BB34F0}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "photon.tests", "tests\photon.tests.vcxproj", "{BBF254DC-F075-4BF0-9863-66FD5B8A034C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand All @@ -15,6 +17,10 @@ Global
{FA989732-5D3F-4565-A5B2-ABD400BB34F0}.Debug|x86.Build.0 = Debug|Win32
{FA989732-5D3F-4565-A5B2-ABD400BB34F0}.Release|x86.ActiveCfg = Release|Win32
{FA989732-5D3F-4565-A5B2-ABD400BB34F0}.Release|x86.Build.0 = Release|Win32
{BBF254DC-F075-4BF0-9863-66FD5B8A034C}.Debug|x86.ActiveCfg = Debug|Win32
{BBF254DC-F075-4BF0-9863-66FD5B8A034C}.Debug|x86.Build.0 = Debug|Win32
{BBF254DC-F075-4BF0-9863-66FD5B8A034C}.Release|x86.ActiveCfg = Release|Win32
{BBF254DC-F075-4BF0-9863-66FD5B8A034C}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/photon-sdk
72 changes: 72 additions & 0 deletions tests/hook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "photon-sdk/photon.h"
#include "utest.h"

namespace dummy {
static int add( int a, int b ) {
return a + b;
}

class math {
public:
virtual int subtract( int a, int b ) {
return a - b;
}
};
} // namespace dummy

inline void *add_hk;
inline int ( *add )( int a, int b );
int add_hk_fn( int a, int b ) {
return a + b + 5;
}

decl_hk( int, subtract, int a, int b );
hk_fn( int, subtract, int a, int b ) {
return a - b + 5;
}

dummy::math *math;

UTEST( hook, inline_hook ) {
// test before hook
ASSERT_EQ( dummy::add( 5, 5 ), 10 );

// hook inline
hk_inline( add, &dummy::add );

// test after hook
ASSERT_EQ( dummy::add( 5, 5 ), 15 );
}

/*
* this is kinda unnecesarry since its technically
* still an inline hook but that might change
* in the future so might aswell have it here
*/
UTEST( hook, virtual_hook ) {
// instantiate dummy class
math = new dummy::math( );

// test before hook
ASSERT_EQ( math->subtract( 5, 5 ), 0 );

// hook virtual
hk_virtual( math, subtract, 0 );

// test after hook
ASSERT_EQ( math->subtract( 5, 5 ), 5 );
}

UTEST( hook, unhook ) {
// test before unhook
ASSERT_EQ( dummy::add( 5, 5 ), 15 );
ASSERT_EQ( math->subtract( 5, 5 ), 5 );

// unhook functions
unhk( subtract );
unhk( add );

// test after unhook
ASSERT_EQ( dummy::add( 5, 5 ), 10 );
ASSERT_EQ( math->subtract( 5, 5 ), 0 );
}
51 changes: 51 additions & 0 deletions tests/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "photon-sdk/photon.h"
#include "utest.h"

/*
* linux asm is written using intel syntax, use compiler flag "-masm=intel"
*/

namespace dummy {
#ifdef _WIN32
__declspec( naked ) int square( int num ) {
__asm push ebp; // 55
__asm mov ebp, esp; // 8b ec
__asm mov eax, dword ptr[ebp + 0x8]; // 8b 45 08
__asm imul eax, dword ptr[ebp + 0x8]; // 0f af 45 08
__asm pop ebp; // 5d
__asm ret; // c3
}
#else
# define __asm __asm__ __volatile__
__attribute__( ( naked ) ) int square( int num ) {
__asm( "push rbp" ); // 55
__asm( "mov rbp, rsp" ); // 48 89 e5
__asm( "mov dword ptr[rbp - 0x4], edi" ); // 89 7d fc
__asm( "mov eax, dword ptr[rbp - 0x4]" ); // 8b 45 fc
__asm( "imul eax, eax" ); // 0f af c0
__asm( "pop rbp" ); // 5d
__asm( "ret" ); // c3
}
# undef __asm
#endif
} // namespace dummy

UTEST( memory, pattern_scan ) {
// make sure the asm actually works lol
ASSERT_EQ( dummy::square( 2 ), 4 );

const uint8_t *square_addr = utils::memory::pattern_scan( os( "photon.tests.exe", "photon.tests" ), os( "55 8b ec 8b 45 08 0f af 45 08 5d c3", "55 48 80 e5 89 7d fc 8b 45 fc 0f af c0 5d c3" ) );

// check if its nullptr
ASSERT_NE( square_addr, nullptr );

// check if bytes match
ASSERT_EQ( square_addr[ 0 ], 0x55 );
ASSERT_EQ( square_addr[ 1 ], os( 0x8b, 0x48 ) );
ASSERT_EQ( square_addr[ 2 ], os( 0xec, 0x80 ) );
ASSERT_EQ( square_addr[ 3 ], os( 0x8b, 0xe5 ) );

// call function just to make sure
const auto square_fn = reinterpret_cast<int ( * )( int )>( square_addr );
ASSERT_EQ( square_fn( 2 ), 4 );
}
129 changes: 129 additions & 0 deletions tests/photon.tests.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{BBF254DC-F075-4BF0-9863-66FD5B8A034C}</ProjectGuid>
<RootNamespace>photon</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="Shared" />
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\</OutDir>
<IncludePath>$(SolutionDir)src;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\</OutDir>
<IncludePath>$(SolutionDir)src;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);SUBHOOK_STATIC</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)src\deps\subhook;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;subhook.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>call ..\copy.bat</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);SUBHOOK_STATIC</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)src\deps\subhook;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;subhook.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>call ..\copy.bat</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\src\core\convars\convars.cpp" />
<ClCompile Include="..\src\core\hooks\hooks.cpp" />
<ClCompile Include="..\src\core\huds\huds.cpp" />
<ClCompile Include="..\src\core\interfaces\interfaces.cpp" />
<ClCompile Include="..\src\core\menu\framework.cpp" />
<ClCompile Include="..\src\core\menu\gui.cpp" />
<ClCompile Include="..\src\core\mods\mods.cpp" />
<ClCompile Include="..\src\core\photon.cpp" />
<ClCompile Include="..\src\core\shared\ccon.cpp" />
<ClCompile Include="..\src\core\shared\event.cpp" />
<ClCompile Include="..\src\core\shared\hook.cpp" />
<ClCompile Include="..\src\core\shared\hud.cpp" />
<ClCompile Include="..\src\core\shared\input.cpp" />
<ClCompile Include="..\src\core\shared\menu.cpp" />
<ClCompile Include="..\src\core\shared\render.cpp" />
<ClCompile Include="..\src\core\source-sdk\plugin.cpp" />
<ClCompile Include="..\src\photon-sdk\utils\console.cpp" />
<ClCompile Include="..\src\photon-sdk\utils\memory.cpp" />
<ClCompile Include="..\src\photon-sdk\utils\string.cpp" />
<ClCompile Include="tests.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="hook.h" />
<ClInclude Include="utest.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
87 changes: 87 additions & 0 deletions tests/photon.tests.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\ccon.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\photon-sdk\utils\console.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\convars\convars.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\event.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\menu\framework.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\menu\gui.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\hook.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\hooks\hooks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\hud.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\huds\huds.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\input.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\interfaces\interfaces.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\photon-sdk\utils\memory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\menu.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\mods\mods.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\photon.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\source-sdk\plugin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\core\shared\render.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\photon-sdk\utils\string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="hook.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="utest.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading

0 comments on commit 29591ae

Please sign in to comment.