Add tests for the bits of LogSessionData I'm touching, do a little refactoring to make this easier.

This commit is contained in:
Matt Koscumb 2019-04-15 10:38:09 -07:00
Родитель ddc132f7d8
Коммит e5724e8018
6 изменённых файлов: 97 добавлений и 28 удалений

Просмотреть файл

@ -41,34 +41,7 @@ namespace ARIASDK_NS_BEGIN {
if (MAT::FileExists(path.c_str()))
{
auto contents = MAT::FileGetContents(path.c_str());
if (!contents.empty())
{
vector<string> v;
StringUtils::SplitString(contents, '\n', v);
if (v.size() == 2)
{
remove_eol(v[0]);
remove_eol(v[1]);
try
{
// First launch time
m_sessionFirstTimeLaunch = std::stoull(v[0]);
// SDK UUID
m_sessionSDKUid = v[1];
recreate = false;
}
catch (const std::invalid_argument& invalidArgumentException)
{
LOG_ERROR("Non-integer data passed to std::stoull");
recreate = true;
}
catch (const std::out_of_range& outOfRangeException)
{
LOG_ERROR("Value passed to std::stoull was larger than unsigned long long could represent");
recreate = true;
}
}
}
recreate = !parse(contents);
}
if (recreate)
@ -88,5 +61,41 @@ namespace ARIASDK_NS_BEGIN {
}
bool LogSessionData::parse(const string& cacheContents)
{
if (cacheContents.empty())
{
return false;
}
vector<string> v;
StringUtils::SplitString(cacheContents, '\n', v);
if (v.size() != 2)
{
return false;
}
remove_eol(v[0]);
remove_eol(v[1]);
try
{
// First launch time
m_sessionFirstTimeLaunch = std::stoull(v[0]);
// SDK UUID
m_sessionSDKUid = v[1];
return true;
}
catch (const std::invalid_argument&)
{
LOG_ERROR("Non-integer data passed to std::stoull");
}
catch (const std::out_of_range&)
{
LOG_ERROR("Value passed to std::stoull was larger than unsigned long long could represent");
}
return false;
}
} ARIASDK_NS_END

Просмотреть файл

@ -41,6 +41,8 @@ namespace ARIASDK_NS_BEGIN
void open(const std::string& path);
bool parse(const std::string& cacheContents);
std::string m_sessionSDKUid;
unsigned long long m_sessionFirstTimeLaunch;
};

Просмотреть файл

@ -20,6 +20,7 @@ set(SRCS
HttpRequestEncoderTests.cpp
HttpResponseDecoderTests.cpp
HttpServerTests.cpp
LogSessionDataTests.cpp
Main.cpp
MemoryStorageTests.cpp
MetaStatsTests.cpp

Просмотреть файл

@ -0,0 +1,54 @@
// Copyright (c) Microsoft. All rights reserved.
#include "common/Common.hpp"
#include <LogSessionData.hpp>
using namespace testing;
using namespace Microsoft::Applications::Events;
class TestLogSessionData : public LogSessionData
{
public:
TestLogSessionData(const std::string& cacheFilePath)
: LogSessionData(cacheFilePath) { }
using LogSessionData::parse;
};
const char* const PathToTestSesFile = "%TEMP%\test";
TEST(LogSessionDataTests, parse_EmptyString_ReturnsFalse)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_FALSE(sessionData.parse(std::string {}));
}
TEST(LogSessionDataTests, parse_OneLine_ReturnsFalse)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_FALSE(sessionData.parse(std::string {"foo" }));
}
TEST(LogSessionDataTests, parse_ThreeLines_ReturnsFalse)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_FALSE(sessionData.parse(std::string { "foo\nbar\n\baz" }));
}
TEST(LogSessionDataTests, parse_TwoLinesFirstLaunchNotNumber_ReturnsFalse)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_FALSE(sessionData.parse(std::string { "foo\nbar" }));
}
TEST(LogSessionDataTests, parse_TwoLinesFirstLaunchTooLarge_ReturnsFalse)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_FALSE(sessionData.parse(std::string { "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\nbar" }));
}
TEST(LogSessionDataTests, parse_ValidInput_ReturnsTrue)
{
TestLogSessionData sessionData { std::string { PathToTestSesFile } };
ASSERT_TRUE(sessionData.parse(std::string { "1234567890\nbar" }));
}

Просмотреть файл

@ -416,6 +416,7 @@
<ItemGroup>
<ClCompile Include="DebugEventSourceTests.cpp" />
<ClCompile Include="EventPropertiesStorageTests.cpp" />
<ClCompile Include="LogSessionDataTests.cpp" />
<ClCompile Include="MemoryStorageTests.cpp" />
<ClInclude Include="$(ProjectDir)..\common\Common.hpp" />
<ClInclude Include="$(ProjectDir)..\common\HttpServer.hpp" />

Просмотреть файл

@ -36,6 +36,8 @@
</ClCompile>
<ClCompile Include="MemoryStorageTests.cpp" />
<ClCompile Include="EventPropertiesStorageTests.cpp" />
<ClCompile Include="DebugEventSourceTests.cpp" />
<ClCompile Include="LogSessionDataTests.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(ProjectDir)..\common\MockIHttpClient.hpp">