Skip to content

Commit f6afb35

Browse files
committed
Added tests for RSA functions
1 parent d86d97a commit f6afb35

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

MTA10_Server/core/Server.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#include "CServerImpl.h"
1515
#define ALLOC_STATS_MODULE_NAME "core"
1616
#include "SharedUtil.hpp"
17-
#if defined(_DEBUG)
18-
#include "SharedUtil.Tests.hpp"
19-
#endif
2017
#ifdef WIN_x86
2118
// TODO - 64 bit file hooks
2219
#include "SharedUtil.Win32Utf8FileHooks.hpp"
@@ -37,10 +34,6 @@ MTAEXPORT int Run ( int iArgumentCount, char* szArguments [] )
3734
}
3835
}
3936

40-
#if defined(_DEBUG)
41-
SharedUtil_Tests ();
42-
#endif
43-
4437
#ifdef WIN32
4538
// Disable critical error message boxes
4639
SetErrorMode ( SEM_FAILCRITICALERRORS );

MTA10_Server/mods/deathmatch/CServer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "SharedUtil.Thread.h"
1717
#include "SharedUtil.IntervalCounter.h"
1818
#include "SharedUtil.IntervalCounter.hpp"
19+
#if defined(_DEBUG)
20+
#include "SharedUtil.Tests.hpp"
21+
#endif
1922

2023
CServerInterface* g_pServerInterface = NULL;
2124
CNetServer* g_pNetServer = NULL;
@@ -41,6 +44,9 @@ void CServer::ServerInitialize ( CServerInterface* pServerInterface )
4144
g_pServerInterface = pServerInterface;
4245
g_pNetServer = pServerInterface->GetNetwork ();
4346
g_pRealNetServer = g_pNetServer;
47+
#if defined(_DEBUG)
48+
SharedUtil_Tests();
49+
#endif
4450
}
4551

4652

MTA10_Server/mods/deathmatch/StdInc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,6 @@ struct SAclRequest;
297297
#include "Config.h"
298298
#define SHOW_SELF_COMPILE_WARNING
299299
#include "../../version.h"
300+
301+
extern CNetServer* g_pRealNetServer;
302+
extern CGame* g_pGame;

Shared/sdk/SharedUtil.Tests.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void SharedUtil_WildcardMatch_Tests ( void );
2121
void SharedUtil_Collection_Tests ( void );
2222
void SharedUtil_String_Tests ( void );
2323
void SharedUtil_Hash_Tests ( void );
24+
void SharedUtil_RSA_Tests ( void );
25+
void RSATestCrypt ( const SString& input, const SString& keyEncrypt, const SString& keyDecrypt );
26+
2427

2528
///////////////////////////////////////////////////////////////
2629
//
@@ -38,6 +41,9 @@ void SharedUtil_Tests ( void )
3841
SharedUtil_Collection_Tests ();
3942
SharedUtil_String_Tests ();
4043
SharedUtil_Hash_Tests ();
44+
#ifndef MTA_CLIENT
45+
SharedUtil_RSA_Tests ();
46+
#endif
4147
}
4248

4349

@@ -899,3 +905,51 @@ void SharedUtil_Hash_Tests ( void )
899905

900906
FileDelete( szTempFilename );
901907
}
908+
909+
910+
#ifndef MTA_CLIENT
911+
///////////////////////////////////////////////////////////////
912+
//
913+
// SharedUtil_RSA_Tests
914+
//
915+
// Test behaviour of Raknet RSA code
916+
//
917+
///////////////////////////////////////////////////////////////
918+
void SharedUtil_RSA_Tests( void )
919+
{
920+
// Generate keys
921+
char privateKeyTemp[64];
922+
char publicKeyTemp[68];
923+
g_pRealNetServer->RSAGenerateKeys( privateKeyTemp, sizeof( privateKeyTemp ), publicKeyTemp, sizeof( publicKeyTemp ) );
924+
SStringX privateKey( privateKeyTemp, sizeof( privateKeyTemp ) );
925+
SStringX publicKey( publicKeyTemp, sizeof( publicKeyTemp ) );
926+
927+
// Short data length
928+
SString input = "hello";
929+
RSATestCrypt( input, publicKey, privateKey );
930+
RSATestCrypt( input, privateKey, publicKey );
931+
932+
// Long data length
933+
while( input.size() < 3000 )
934+
input += input + "goodbye";
935+
RSATestCrypt( input, publicKey, privateKey );
936+
RSATestCrypt( input, privateKey, publicKey );
937+
}
938+
939+
void RSATestCrypt( const SString& input, const SString& keyEncrypt, const SString& keyDecrypt )
940+
{
941+
// Encrypt
942+
std::vector < char > encryptedBuffer;
943+
encryptedBuffer.resize( input.size() + 100 );
944+
uint uiEncryptedSize = g_pRealNetServer->RSAEncryptData( &input[0], input.size(), &keyEncrypt[0], keyEncrypt.size(), &encryptedBuffer[0], encryptedBuffer.size() );
945+
946+
// Decrypt
947+
std::vector < char > decryptedBuffer;
948+
decryptedBuffer.resize( uiEncryptedSize );
949+
uint uiDecryptedSize = g_pRealNetServer->RSADecryptData( &encryptedBuffer[0], uiEncryptedSize, &keyDecrypt[0], keyDecrypt.size(), &decryptedBuffer[0], decryptedBuffer.size() );
950+
951+
// Check decrypted is the same as original
952+
assert( input.size() == uiDecryptedSize );
953+
assert( memcmp( &input[0], &decryptedBuffer[0], uiDecryptedSize ) == 0 );
954+
}
955+
#endif

0 commit comments

Comments
 (0)