JD2022-TU1/main/unittests/engine/GoTop/GoTopTest.cpp

192 lines
No EOL
6.2 KiB
C++

#include "precompiled_unittests_engine.h"
#if defined(ITF_USE_GOTOP)
#include "../extern/GoTop/sources/MemoryRandomizer.h"
#endif
#include "core/Archive.h"
#include "engine/GoTop/GoTopInterface.h"
#include "engine/GoTop/ProtectionManager.h"
namespace ITF
{
using namespace testing;
enum EStatsomeState
{
ST_NEED_TO_UPLOAD,
ST_UPLOADING,
ST_UP_TO_DATE
};
class DebugPlayer
{
public:
DebugPlayer() {}
DebugPlayer(u32 val)
: m_killStats(val) {}
u32 getKillStats() const { return m_killStats; }
private:
u32 m_killStats;
};
TEST(GoTop, test_GoTopOperations)
{
#if defined(ITF_USE_GOTOP)
alloca(gotop::RandomizeMemory());
#endif
//ProtectionManager needs to be created on stack, beacuse it expects the ProtectVar<> variables to be released before ProtectionManager
//is destructed. If not we get an assert in SecureBuffer stating that there exists protected variables which are not released because they are on stack.
ProtectionManager protectionMgr;
ProtectVar<DebugPlayer> m_protectVar;
ITF_VECTOR<ProtectVar<DebugPlayer> > m_protectedVector;
m_protectVar = DebugPlayer(100);
m_protectedVector.push_back(DebugPlayer(10));
m_protectedVector.push_back(DebugPlayer(20));
m_protectedVector.push_back(DebugPlayer(30));
m_protectedVector.push_back(DebugPlayer(40));
f32 delta = 0.0f;
f32 securityKeyChangeTimeout = (f32) protectionMgr.getLastKeyChangeTimeOut();
#if defined(ITF_USE_GOTOP)
gotop::EncryptedSecureData oldSecureData = m_protectVar.GetEncrypted();
ITF_VECTOR<gotop::EncryptedSecureData> oldSecureDataVector;
for(u32 index = 0; index < m_protectedVector.size(); ++index)
{
oldSecureDataVector.push_back(m_protectedVector[index].GetEncrypted());
}
#endif
while(true)
{
delta += 0.1f;
protectionMgr.update(delta);
EXPECT_EQ((u32) 100, m_protectVar.Get().getKillStats());
for(u32 index = 0; index < m_protectedVector.size(); ++index)
{
EXPECT_EQ((u32) 10 * (index + 1), m_protectedVector[index].Get().getKillStats());
}
if(delta > securityKeyChangeTimeout)
{
//Security keys should be changed by now.
//Validate actual data
EXPECT_EQ((u32) 100, m_protectVar.Get().getKillStats());
for(u32 index = 0; index < m_protectedVector.size(); ++index)
{
EXPECT_EQ((u32) 10 * (index + 1), m_protectedVector[index].Get().getKillStats());
}
#if defined(ITF_USE_GOTOP)
//Validate encrypted data change
EXPECT_NE(oldSecureData.m_data, m_protectVar.GetEncrypted().m_data);
for(u32 index = 0; index < m_protectedVector.size(); ++index)
{
EXPECT_NE(oldSecureDataVector[index].m_data, m_protectedVector[index].GetEncrypted().m_data);
}
#endif
break;
}
}
}
TEST(GoTop, test_GoTopArchiveMemory)
{
//ProtectionManager needs to be created on stack, beacuse it expects the ProtectVar<> variables to be released before ProtectionManager
//is destructed. If not we get an assert in SecureBuffer stating that there exists protected variables which are not released because they are on stack.
ProtectionManager protectionMgr;
//Check protected conversions
ProtectVar<u32> score = 10;
ProtectVar<const char*> protectStr = score.GetConverted< ProtectVar<const char*> >();
EXPECT_EQ(true, strcmp(protectStr.Get(), "10") == 0);
delete[] protectStr.Get();
ProtectVar<const char*> someChar = "Char";
EXPECT_EQ(true, strcmp(someChar.Get(), "Char") == 0);
someChar = "Changed";
EXPECT_EQ(true, strcmp(someChar.Get(), "Changed") == 0);
//Test for Archive memory
ProtectVar<bbool> somebbool = btrue;
ProtectVar<f32> someFloat = 10.5f;
ProtectVar<u32> someInt = 20;
ProtectVar<EStatsomeState> someState = ST_UP_TO_DATE;
ProtectVar<StringID> someStringID("StringID");
ITF_VECTOR<ProtectVar<f32> > someVecOfFloats;
someVecOfFloats.push_back(5.5f);
someVecOfFloats.push_back(1.2f);
someVecOfFloats.push_back(3.7f);
ArchiveMemory memory;
memory.serialize(somebbool);
memory.serialize(someFloat);
memory.serialize(someInt);
memory.serializeTyped<u32>(someState);
serializeValue(someStringID, memory);
someVecOfFloats.serialize(memory);
memory.rewindForReading();
somebbool = bfalse;
someFloat = 0.0f;
someInt = 0;
someState = ST_NEED_TO_UPLOAD;
someStringID = StringID::Invalid;
someVecOfFloats.clear();
memory.serialize(somebbool);
memory.serialize(someFloat);
memory.serialize(someInt);
memory.serializeTyped<u32>(someState);
serializeValue(someStringID, memory);
someVecOfFloats.serialize(memory);
EXPECT_EQ(btrue, somebbool);
EXPECT_EQ(true, fabs(someFloat - 10.5f) == 0);
EXPECT_EQ(true, someInt == 20);
EXPECT_EQ(true, someState == ST_UP_TO_DATE);
EXPECT_EQ(true, someStringID.Get() == StringID("StringID"));
EXPECT_EQ(true, fabs(someVecOfFloats[0] - 5.5f) == 0);
EXPECT_EQ(true, fabs(someVecOfFloats[1] - 1.2f) == 0);
EXPECT_EQ(true, fabs(someVecOfFloats[2] - 3.7f) == 0);
//Operator overloads working
EXPECT_EQ(true, ++someInt == 21);
EXPECT_EQ(true, --someInt == 20);
EXPECT_EQ(true, someInt++ == 20);
EXPECT_EQ(true, someInt == 21);
EXPECT_EQ(true, someInt-- == 21);
EXPECT_EQ(true, someInt == 20);
someInt += 1;
EXPECT_EQ(true, someInt == 21);
someInt -= 1;
EXPECT_EQ(true, someInt == 20);
someInt *= 2;
EXPECT_EQ(true, someInt == 40);
someInt /= 2;
EXPECT_EQ(true, someInt == 20);
}
}