Skip to content

Commit 7453cbf

Browse files
authored
Fix session-file parsing logic (#957)
1 parent 25bf475 commit 7453cbf

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/offline/LogSessionDataProvider.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ namespace MAT_NS_BEGIN
140140
}
141141
std::vector<std::string> v;
142142
StringUtils::SplitString(content, '\n', v);
143-
if (v.size() != 2) {
143+
if (v.size() != 3) {
144144
return false;
145145
}
146146
remove_eol(v[0]);
@@ -157,7 +157,7 @@ namespace MAT_NS_BEGIN
157157
{
158158
uint64_t res = 0ull;
159159
char *endptr = nullptr;
160-
res = std::strtol(s.c_str(), &endptr, 10);
160+
res = std::strtoll(s.c_str(), &endptr, 10);
161161
if (errno == ERANGE && (res == LONG_MAX || res == 0 ))
162162
{
163163
LOG_WARN ("Converted value falls out of uint64_t range.");
@@ -184,8 +184,8 @@ namespace MAT_NS_BEGIN
184184
contents += toString(sessionFirstTimeLaunch);
185185
contents += '\n';
186186
contents += sessionSDKUid;
187+
// Valid line ends with newline as per posix specs ( https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206)
187188
contents += '\n';
188-
189189
//TBD (labhas) - validate if file is NOT a symlink/junction before trying to write.
190190
if (!MAT::FileWrite(path.c_str(), contents.c_str()))
191191
{

tests/functests/LogSessionDataFuncTests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void ConstructSesFile(const char* sessionFile, const std::string& contents)
4444
void ConstructSesFile(const char* sessionFile, const std::string& utcTimeMs, const std::string& skuID)
4545
{
4646
std::ostringstream stream;
47-
stream << utcTimeMs << '\n' << skuID;
47+
stream << utcTimeMs << '\n' << skuID << '\n';
4848
ConstructSesFile(sessionFile, stream.str());
4949
}
5050

@@ -118,4 +118,3 @@ TEST(LogSessionDataFuncTests, Constructor_InvalidSessionFileExists_NewFileWritte
118118
ASSERT_EQ(logSessionData->getSessionFirstTime(), properties.first);
119119
ASSERT_EQ(logSessionData->getSessionSDKUid(), properties.second);
120120
}
121-

tests/unittests/LogSessionDataTests.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class TestLogSessionDataProvider : public LogSessionDataProvider
1818
};
1919

2020
const char* const PathToTestSesFile = "";
21+
const char* const PathToNonEmptyTestSesFile = "sesfile";
22+
2123
std::string sessionSDKUid;
2224
uint64_t sessionFirstTimeLaunch;
2325

@@ -30,7 +32,6 @@ TEST(LogSessionDataTests, parse_EmptyString_ReturnsFalse)
3032

3133
TEST(LogSessionDataTests, parse_OneLine_ReturnsFalse)
3234
{
33-
3435
TestLogSessionDataProvider logSessionDataProvider(PathToTestSesFile);
3536
ASSERT_FALSE(logSessionDataProvider.parse(std::string {"foo" }, sessionFirstTimeLaunch, sessionSDKUid));
3637
}
@@ -54,9 +55,32 @@ TEST(LogSessionDataTests, parse_TwoLinesFirstLaunchTooLarge_ReturnsFalse)
5455
sessionFirstTimeLaunch, sessionSDKUid));
5556
}
5657

58+
TEST(LogSessionDataTests, parse_MissingNewLineAtEnd_ReturnsFalse)
59+
{
60+
TestLogSessionDataProvider logSessionDataProvider(PathToTestSesFile);
61+
ASSERT_FALSE(logSessionDataProvider.parse(std::string { "1234567890\nbar" }, sessionFirstTimeLaunch, sessionSDKUid));
62+
}
63+
5764
TEST(LogSessionDataTests, parse_ValidInput_ReturnsTrue)
5865
{
5966
TestLogSessionDataProvider logSessionDataProvider(PathToTestSesFile);
60-
ASSERT_TRUE(logSessionDataProvider.parse(std::string { "1234567890\nbar" }, sessionFirstTimeLaunch, sessionSDKUid));
67+
ASSERT_TRUE(logSessionDataProvider.parse(std::string { "1234567890\nbar\n" }, sessionFirstTimeLaunch, sessionSDKUid));
68+
ASSERT_EQ(sessionFirstTimeLaunch, (uint64_t)1234567890);
69+
ASSERT_EQ(sessionSDKUid, "bar");
70+
}
71+
72+
TEST(LogSessionDataTests, getLogSessionData_ValidInput_SessionDataPersists)
73+
{
74+
TestLogSessionDataProvider logSessionDataProvider1(PathToNonEmptyTestSesFile);
75+
logSessionDataProvider1.CreateLogSessionData();
76+
auto logSessionData1 = logSessionDataProvider1.GetLogSessionData();
77+
78+
// Create another provider instance and validate session data is not re-generated
79+
TestLogSessionDataProvider logSessionDataProvider2(PathToNonEmptyTestSesFile);
80+
logSessionDataProvider2.CreateLogSessionData();
81+
auto logSessionData2 = logSessionDataProvider2.GetLogSessionData();
82+
83+
ASSERT_EQ(logSessionData1->getSessionFirstTime(), logSessionData2->getSessionFirstTime());
84+
ASSERT_EQ(logSessionData1->getSessionSDKUid(), logSessionData2->getSessionSDKUid());
6185
}
6286

0 commit comments

Comments
 (0)