Add tests for snapshot crc overflow

This commit is contained in:
ChillerDragon 2024-06-26 20:36:52 +08:00
parent 248a24845e
commit b138cc9f13
2 changed files with 86 additions and 0 deletions

View file

@ -2804,6 +2804,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
secure_random.cpp
serverbrowser.cpp
serverinfo.cpp
snapshot.cpp
str.cpp
strip_path_and_extension.cpp
swap_endian.cpp

85
src/test/snapshot.cpp Normal file
View file

@ -0,0 +1,85 @@
#include <gtest/gtest.h>
#include <base/system.h>
#include <engine/shared/snapshot.h>
#include <game/generated/protocol.h>
TEST(Snapshot, CrcOneInt)
{
CSnapshotBuilder Builder;
Builder.Init();
CNetObj_Flag Flag;
void *pItem = Builder.NewItem(CNetObj_Flag::ms_MsgId, 0, sizeof(Flag));
ASSERT_FALSE(pItem == nullptr);
Flag.m_X = 4;
Flag.m_Y = 0;
Flag.m_Team = 0;
mem_copy(pItem, &Flag, sizeof(Flag));
char aData[CSnapshot::MAX_SIZE];
CSnapshot *pSnapshot = (CSnapshot *)aData;
Builder.Finish(pSnapshot);
ASSERT_EQ(pSnapshot->Crc(), 4);
}
TEST(Snapshot, CrcTwoInts)
{
CSnapshotBuilder Builder;
Builder.Init();
CNetObj_Flag Flag;
void *pItem = Builder.NewItem(CNetObj_Flag::ms_MsgId, 0, sizeof(Flag));
ASSERT_FALSE(pItem == nullptr);
Flag.m_X = 1;
Flag.m_Y = 1;
Flag.m_Team = 0;
mem_copy(pItem, &Flag, sizeof(Flag));
char aData[CSnapshot::MAX_SIZE];
CSnapshot *pSnapshot = (CSnapshot *)aData;
Builder.Finish(pSnapshot);
ASSERT_EQ(pSnapshot->Crc(), 2);
}
TEST(Snapshot, CrcBiggerInts)
{
CSnapshotBuilder Builder;
Builder.Init();
CNetObj_Flag Flag;
void *pItem = Builder.NewItem(CNetObj_Flag::ms_MsgId, 0, sizeof(Flag));
ASSERT_FALSE(pItem == nullptr);
Flag.m_X = 99999999;
Flag.m_Y = 1;
Flag.m_Team = 1;
mem_copy(pItem, &Flag, sizeof(Flag));
char aData[CSnapshot::MAX_SIZE];
CSnapshot *pSnapshot = (CSnapshot *)aData;
Builder.Finish(pSnapshot);
ASSERT_EQ(pSnapshot->Crc(), 100000001);
}
TEST(Snapshot, CrcOverflow)
{
CSnapshotBuilder Builder;
Builder.Init();
CNetObj_Flag Flag;
void *pItem = Builder.NewItem(CNetObj_Flag::ms_MsgId, 0, sizeof(Flag));
ASSERT_FALSE(pItem == nullptr);
Flag.m_X = 0xFFFFFFFF;
Flag.m_Y = 1;
Flag.m_Team = 1;
mem_copy(pItem, &Flag, sizeof(Flag));
char aData[CSnapshot::MAX_SIZE];
CSnapshot *pSnapshot = (CSnapshot *)aData;
Builder.Finish(pSnapshot);
ASSERT_EQ(pSnapshot->Crc(), 1);
}