254 lines
No EOL
8.7 KiB
C++
254 lines
No EOL
8.7 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// (c) Copyright, Ubisoft Entertainment Inc. || Created by Technology Group //
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
#include "tests/config.h"
|
|
#if defined(US_SDK_S2S)
|
|
#error This file shall not be part of the solution
|
|
#endif
|
|
|
|
#include "tests/fixture/memoryCheckFixture.h"
|
|
#include <ubiservices/core/JSON/jsonWriter.h>
|
|
#include <ubiservices/core/types/dateTimeHelper.h>
|
|
#include <ubiservices/loggingHelper.h>
|
|
#include <ubiservices/services/tLog/tLog.h>
|
|
#include <ubiservices/services/tLog/tLogQueue.h>
|
|
|
|
namespace US_NS {;
|
|
US_DEFINE_TEST(TestTLog);
|
|
|
|
typedef LeakChecker TESTTLOG;
|
|
|
|
#if !defined(US_DLL)
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogKeyValueStringRenderContent)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLogKeyValueString tLog("key1", "value1");
|
|
ASSERT_EQ("key1", tLog.getKey());
|
|
ASSERT_EQ("key1=value1", tLog.renderContent());
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogKeyValueIntRenderContent)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLogKeyValueInt tLog("key1", 1);
|
|
ASSERT_EQ("key1", tLog.getKey());
|
|
ASSERT_EQ("key1=1", tLog.renderContent());
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogImportant)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tlog("gameStart", TLogLevel::Important);
|
|
ASSERT_TRUE(tlog.isImportant());
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogNonImportant)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tlog("gameStart", TLogLevel::Unimportant);
|
|
ASSERT_FALSE(tlog.isImportant());
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogNonImportantDefaultValue)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tlog("gameStart");
|
|
ASSERT_FALSE(tlog.isImportant());
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogRenderContentWithoutParameters)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tLog("gameStart");
|
|
ASSERT_EQ("gameStart", tLog.getName());
|
|
|
|
tLog.addField("key1", 1);
|
|
tLog.addField("key2", -2);
|
|
tLog.addField("key3", "value3");
|
|
|
|
JsonWriter writer(JsonWriterType::Array);
|
|
writer.addItemToArray(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
|
|
String renderedContent(writer.renderContent());
|
|
PRINT_LINE(renderedContent.getUtf8());
|
|
|
|
// DateTime is automatically set when TLog is created
|
|
// Note: Will have to update this test on Jan 1, 2100
|
|
ASSERT_TRUE(renderedContent.findSubstringNoCase("{\"log_name\":\"gameStart\",\"log_fields\":\"key1=1&key2=-2&key3=value3&serverID=0&clientIP=127.0.0.1&rail_id=12334454555&dtEventTime=20") != String::npos);
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogRenderContentWithDtEventTime)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tLog("gameStart");
|
|
ASSERT_EQ("gameStart", tLog.getName());
|
|
|
|
tLog.addField("key1", 1);
|
|
tLog.addField("key2", -2);
|
|
tLog.addField("key3", "value3");
|
|
|
|
tLog.addField("dtEventTime", "2018-01-01T12:01:02");
|
|
|
|
JsonWriter writer(JsonWriterType::Array);
|
|
writer.addItemToArray(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
|
|
String renderedContent(writer.renderContent());
|
|
PRINT_LINE(renderedContent.getUtf8());
|
|
|
|
// Note here T has been remove between date and time
|
|
ASSERT_TRUE(renderedContent.findSubstringNoCase("{\"log_name\":\"gameStart\",\"log_fields\":\"key1=1&key2=-2&key3=value3&dtEventTime=2018-01-01 12:01:02&serverID=0&clientIP=127.0.0.1&rail_id=12334454555") != String::npos);
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogRenderContentWithServerId)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tLog("gameStart");
|
|
ASSERT_EQ("gameStart", tLog.getName());
|
|
|
|
tLog.addField("key1", 1);
|
|
tLog.addField("key2", -2);
|
|
tLog.addField("key3", "value3");
|
|
tLog.addField("serverID", 2);
|
|
|
|
JsonWriter writer(JsonWriterType::Array);
|
|
writer.addItemToArray(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
|
|
String renderedContent(writer.renderContent());
|
|
PRINT_LINE(renderedContent.getUtf8());
|
|
|
|
// DateTime is automatically set when TLog is created
|
|
// Note: Will have to update this test on Jan 1, 2100
|
|
ASSERT_TRUE(renderedContent.findSubstringNoCase("{\"log_name\":\"gameStart\",\"log_fields\":\"key1=1&key2=-2&serverID=2&key3=value3&clientIP=127.0.0.1&rail_id=12334454555&dtEventTime=20") != String::npos);
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogRenderContentWithClientIp)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tLog("gameStart");
|
|
ASSERT_EQ("gameStart", tLog.getName());
|
|
|
|
tLog.addField("key1", 1);
|
|
tLog.addField("key2", -2);
|
|
tLog.addField("key3", "value3");
|
|
tLog.addField("clientIP", "255.255.255.0");
|
|
|
|
JsonWriter writer(JsonWriterType::Array);
|
|
writer.addItemToArray(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
|
|
String renderedContent(writer.renderContent());
|
|
PRINT_LINE(renderedContent.getUtf8());
|
|
|
|
// DateTime is automatically set when TLog is created
|
|
// Note: Will have to update this test on Jan 1, 2100
|
|
ASSERT_TRUE(renderedContent.findSubstringNoCase("{\"log_name\":\"gameStart\",\"log_fields\":\"key1=1&key2=-2&key3=value3&clientIP=255.255.255.0&serverID=0&rail_id=12334454555&dtEventTime=20") != String::npos);
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
TEST_F(TESTTLOG, ValidateTLogRenderContentWithRailId)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
TLog tLog("gameStart");
|
|
ASSERT_EQ("gameStart", tLog.getName());
|
|
|
|
tLog.addField("key1", 1);
|
|
tLog.addField("key2", -2);
|
|
tLog.addField("key3", "value3");
|
|
tLog.addField("rail_id", "9999999");
|
|
|
|
JsonWriter writer(JsonWriterType::Array);
|
|
writer.addItemToArray(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
|
|
String renderedContent(writer.renderContent());
|
|
PRINT_LINE(renderedContent.getUtf8());
|
|
|
|
// DateTime is automatically set when TLog is created
|
|
// Note: Will have to update this test on Jan 1, 2100
|
|
ASSERT_TRUE(renderedContent.findSubstringNoCase("{\"log_name\":\"gameStart\",\"log_fields\":\"key1=1&key2=-2&key3=value3&rail_id=9999999&serverID=0&clientIP=127.0.0.1&dtEventTime=20") != String::npos);
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
// Test validate the value (in key-value) has all escape chars escaped (|, & and =)
|
|
TEST_F(TESTTLOG, ValidateTLogRenderWithTencentEscapeCharInValue)
|
|
{
|
|
SAVE_ALLOC_COUNT(testCount);
|
|
{
|
|
{
|
|
TLog tLog("gameStart");
|
|
tLog.addField("NoEscape", "abc;def!hij?klm%mno#pqs");
|
|
|
|
Json data(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
const String& dataStr(data.renderContent().getUtf8());
|
|
EXPECT_TRUE(dataStr.findSubstringNoCase("NoEscape=abc;def!hij?klm%mno#pqs") != String::npos);
|
|
}
|
|
|
|
{
|
|
TLog tLog("gameStart");
|
|
tLog.addField("VerticalBar", "|abc||def|");
|
|
|
|
Json data(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
const String& dataStr(data.renderContent().getUtf8());
|
|
EXPECT_TRUE(dataStr.findSubstringNoCase("VerticalBar=%7Cabc%7C%7Cdef%7C") != String::npos);
|
|
}
|
|
|
|
{
|
|
TLog tLog("gameStart");
|
|
tLog.addField("Ampersand", "&abc&&def&");
|
|
|
|
Json data(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
const String& dataStr(data.renderContent().getUtf8());
|
|
EXPECT_TRUE(dataStr.findSubstringNoCase("Ampersand=%26abc%26%26def%26") != String::npos);
|
|
}
|
|
|
|
{
|
|
TLog tLog("gameStart");
|
|
tLog.addField("Equal", "=abc==def=");
|
|
|
|
Json data(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
const String& dataStr(data.renderContent().getUtf8());
|
|
EXPECT_TRUE(dataStr.findSubstringNoCase("Equal=%3Dabc%3D%3Ddef%3D") != String::npos);
|
|
}
|
|
|
|
{
|
|
TLog tLog("gameStart");
|
|
tLog.addField("Mixed", "|中=国&的&长=城|");
|
|
|
|
Json data(tLog.renderContent(0, "127.0.0.1", "12334454555"));
|
|
const String& dataStr(data.renderContent().getUtf8());
|
|
EXPECT_TRUE(dataStr.findSubstringNoCase("Mixed=%7C中%3D国%26的%26长%3D城%7C") != String::npos);
|
|
}
|
|
}
|
|
EXPECT_ALLOC_COUNT_PC_X1_EQ(1u, 1u, testCount);
|
|
}
|
|
|
|
#endif
|
|
|
|
}; |