Allow ParseUuid to report errors and add tests

This commit is contained in:
heinrich5991 2021-01-22 21:04:18 +01:00
parent 0b2c457c37
commit 3539f2b183
5 changed files with 65 additions and 9 deletions

View file

@ -2147,6 +2147,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
test.h
thread.cpp
unix.cpp
uuid.cpp
)
set(TESTS_EXTRA
src/engine/client/blocklist_driver.cpp

View file

@ -53,12 +53,30 @@ void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength)
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
}
void ParseUuid(CUuid *pUuid, char *pBuffer)
int ParseUuid(CUuid *pUuid, const char *pBuffer)
{
unsigned char *p = pUuid->m_aData;
sscanf(pBuffer, "%02hhX%02hhX%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
&p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7],
&p[8], &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15]);
if(str_length(pBuffer) + 1 != UUID_MAXSTRSIZE)
{
return 2;
}
char aCopy[UUID_MAXSTRSIZE];
str_copy(aCopy, pBuffer, sizeof(aCopy));
// 01234567-9012-4567-9012-456789012345
if(aCopy[8] != '-' || aCopy[13] != '-' || aCopy[18] != '-' || aCopy[23] != '-')
{
return 1;
}
aCopy[8] = aCopy[13] = aCopy[18] = aCopy[23] = 0;
if(0 ||
str_hex_decode(pUuid->m_aData + 0, 4, aCopy + 0) ||
str_hex_decode(pUuid->m_aData + 4, 2, aCopy + 9) ||
str_hex_decode(pUuid->m_aData + 6, 2, aCopy + 14) ||
str_hex_decode(pUuid->m_aData + 8, 2, aCopy + 19) ||
str_hex_decode(pUuid->m_aData + 10, 6, aCopy + 24))
{
return 1;
}
return 0;
}
bool CUuid::operator==(const CUuid &Other) const

View file

@ -27,7 +27,8 @@ CUuid RandomUuid();
CUuid CalculateUuid(const char *pName);
// The buffer length should be at least UUID_MAXSTRSIZE.
void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength);
void ParseUuid(CUuid *pUuid, char *pBuffer);
// Returns nonzero on failure.
int ParseUuid(CUuid *pUuid, const char *pBuffer);
struct CName
{

View file

@ -1533,17 +1533,16 @@ bool CScore::LoadTeamThread(IDbConnection *pSqlServer, const ISqlData *pGameData
return true;
}
char aSaveID[UUID_MAXSTRSIZE];
memset(pResult->m_SaveID.m_aData, 0, sizeof(pResult->m_SaveID.m_aData));
if(!pSqlServer->IsNull(3))
{
char aSaveID[UUID_MAXSTRSIZE];
pSqlServer->GetString(3, aSaveID, sizeof(aSaveID));
if(str_length(aSaveID) + 1 != UUID_MAXSTRSIZE)
if(ParseUuid(&pResult->m_SaveID, aSaveID))
{
str_copy(pResult->m_aMessage, "Unable to load savegame: SaveID corrupted", sizeof(pResult->m_aMessage));
return true;
}
ParseUuid(&pResult->m_SaveID, aSaveID);
}
char aSaveString[65536];

37
src/test/uuid.cpp Normal file
View file

@ -0,0 +1,37 @@
#include <gtest/gtest.h>
#include <engine/shared/uuid_manager.h>
TEST(Uuid, FromToString)
{
char aUuid[UUID_MAXSTRSIZE];
CUuid Uuid;
EXPECT_FALSE(ParseUuid(&Uuid, "8d300ecf-5873-4297-bee5-95668fdff320"));
FormatUuid(Uuid, aUuid, sizeof(aUuid));
EXPECT_STREQ(aUuid, "8d300ecf-5873-4297-bee5-95668fdff320");
EXPECT_FALSE(ParseUuid(&Uuid, "00000000-0000-0000-0000-000000000000"));
FormatUuid(Uuid, aUuid, sizeof(aUuid));
EXPECT_STREQ(aUuid, "00000000-0000-0000-0000-000000000000");
EXPECT_FALSE(ParseUuid(&Uuid, "ffffffff-ffff-ffff-ffff-ffffffffffff"));
FormatUuid(Uuid, aUuid, sizeof(aUuid));
EXPECT_STREQ(aUuid, "ffffffff-ffff-ffff-ffff-ffffffffffff");
EXPECT_FALSE(ParseUuid(&Uuid, "01234567-89aB-cDeF-0123-456789AbCdEf"));
FormatUuid(Uuid, aUuid, sizeof(aUuid));
EXPECT_STREQ(aUuid, "01234567-89ab-cdef-0123-456789abcdef");
}
TEST(Uuid, ParseFailures)
{
CUuid Uuid;
EXPECT_TRUE(ParseUuid(&Uuid, ""));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef-0123-456789abcdeg"));
EXPECT_TRUE(ParseUuid(&Uuid, "0-0-0-0-0"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef-0123-456789abcde"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef-0123-456789abcdef0"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567+89ab-cdef-0123-456789abcdef"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab+cdef-0123-456789abcdef"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef+0123-456789abcdef"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef-0123+456789abcdef"));
EXPECT_TRUE(ParseUuid(&Uuid, "01234567-89ab-cdef-0123-456789abcdef "));
EXPECT_TRUE(ParseUuid(&Uuid, "0x01234567-89ab-cdef-0123-456789abcdef"));
}