diff --git a/CMakeLists.txt b/CMakeLists.txt index 71e625e54..1c93656fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/test/snapshot.cpp b/src/test/snapshot.cpp new file mode 100644 index 000000000..6c818f57e --- /dev/null +++ b/src/test/snapshot.cpp @@ -0,0 +1,85 @@ +#include + +#include +#include +#include + +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); +}