From 64249f13c9b12abdf2ffe292b951470b76c36e17 Mon Sep 17 00:00:00 2001 From: SerhiiGadzhilov <71632867+SerhiiGadzhilov@users.noreply.github.com> Date: Wed, 4 Nov 2020 13:10:18 +0300 Subject: [PATCH] SYNERGY-362 Eliminate using std::chrono (#6828) * SYNERGY-362 Eliminate using chrono due to compilation error on: centos7.6, ubuntu1604, debian9 * Remove chrono header * Update ChangeLog * SYNERGY-362 Tests compilation fix * SYNERGY-362 Fix code smell --- ChangeLog | 2 +- src/lib/shared/SerialKey.cpp | 19 ++++++++----- src/lib/shared/SerialKey.h | 3 +- src/test/unittests/shared/SerialKeyTests.cpp | 14 ++++++++-- .../unittests/synergy/ProtocolUtilTests.cpp | 28 +++++++++---------- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 87713359d..f08a2384c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,7 +22,7 @@ Enhancements: - #6803 Update Synergy icons - #6800 Update behaviour when unregistered - #6806 Move to Github Action for general PR builds and tests -- #6816 Improve license validation +- #6816 Improve license key validation v1.12.0-stable =========== diff --git a/src/lib/shared/SerialKey.cpp b/src/lib/shared/SerialKey.cpp index ed1854198..5f9e1f880 100644 --- a/src/lib/shared/SerialKey.cpp +++ b/src/lib/shared/SerialKey.cpp @@ -171,18 +171,23 @@ SerialKey::daysLeft(time_t currentTime) const return timeLeft / day + daysLeft; } -std::chrono::milliseconds +int SerialKey::getSpanLeft(time_t time) const { - std::chrono::milliseconds timeLeft{-1}; + int result{-1}; - if (isTemporary()){ - auto expire{std::chrono::system_clock::from_time_t(m_expireTime)}; - auto target{std::chrono::system_clock::from_time_t(time)}; - timeLeft = std::chrono::duration_cast(expire - target); + if (isTemporary() && !isExpired(time)){ + auto timeLeft = (m_expireTime - time) * 1000; + + if (timeLeft < INT_MAX){ + result = static_cast(timeLeft); + } + else{ + result = INT_MAX; + } } - return timeLeft; + return result; } std::string diff --git a/src/lib/shared/SerialKey.h b/src/lib/shared/SerialKey.h index 77f03ce01..c4330d441 100644 --- a/src/lib/shared/SerialKey.h +++ b/src/lib/shared/SerialKey.h @@ -19,7 +19,6 @@ #include #include -#include #include "EditionType.h" #include "SerialKeyType.h" #include "SerialKeyEdition.h" @@ -40,7 +39,7 @@ public: bool isTemporary() const; bool isValid() const; time_t daysLeft(time_t currentTime) const; - std::chrono::milliseconds getSpanLeft(time_t time = ::time(0)) const; + int getSpanLeft(time_t time = ::time(0)) const; std::string email() const; Edition edition() const; std::string toString() const; diff --git a/src/test/unittests/shared/SerialKeyTests.cpp b/src/test/unittests/shared/SerialKeyTests.cpp index cd34bdd37..b09629ee4 100644 --- a/src/test/unittests/shared/SerialKeyTests.cpp +++ b/src/test/unittests/shared/SerialKeyTests.cpp @@ -17,6 +17,7 @@ #define TEST_ENV +#include #include "shared/SerialKey.h" #include "test/global/gtest.h" @@ -229,7 +230,7 @@ TEST(SerialKeyTests, IsValidExpiredKey_false) TEST(SerialKeyTests, test_getSpanLeft) { SerialKey key; - std::chrono::milliseconds expected{-1}; + int expected{-1}; EXPECT_EQ(expected, key.getSpanLeft()); } @@ -237,8 +238,15 @@ TEST(SerialKeyTests, test_getSpanLeft_subscription) { // {v2;subscription;basic;Bob;1;email;company name;0;86400} SerialKey key("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); - std::chrono::milliseconds expected{1000}; - EXPECT_EQ(expected, key.getSpanLeft(86399)); + EXPECT_EQ(1000, key.getSpanLeft(86399)); + EXPECT_EQ(-1, key.getSpanLeft(86401)); +} + +TEST(SerialKeyTests, test_getSpanLeft_max_int) +{ + //"{v2;subscription;basic;Bob;1;email;company name;0;3147483647}" + SerialKey key("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B333134373438333634377D"); + EXPECT_EQ(INT_MAX, key.getSpanLeft(1)); } diff --git a/src/test/unittests/synergy/ProtocolUtilTests.cpp b/src/test/unittests/synergy/ProtocolUtilTests.cpp index fea36a76e..fa275b377 100644 --- a/src/test/unittests/synergy/ProtocolUtilTests.cpp +++ b/src/test/unittests/synergy/ProtocolUtilTests.cpp @@ -200,7 +200,7 @@ TEST_F(ProtocolUtilTests, readf_string) { const UInt8 Length = 200; const std::string Expected(Length, 'x'); - std::array StringSize = {0,0,0,Length}; + std::array StringSize{{0,0,0,Length}}; EXPECT_CALL(stream, read(_, _)) .WillOnce( @@ -225,8 +225,8 @@ class ReadfIntTestFixture : public ::testing::TestWithParam< std::tuple StreamData2Bytes = {0, 10}; - std::array StreamData4Bytes = {0, 0, 0, 10}; + std::array StreamData2Bytes{{0, 10}}; + std::array StreamData4Bytes{{0, 0, 0, 10}}; UInt8* getStreamData(int size) { @@ -287,7 +287,7 @@ TEST_P(ReadfIntVectorTestFixture, readf_int_vector) const std::vector Expected1Byte = {10,10}; const std::vector Expected2Bytes = {10,10}; const std::vector Expected4Bytes = {10,10}; - std::array StreamVectorSize = {0,0,0,2}; + std::array StreamVectorSize{{0,0,0,2}}; const char* Format = std::get<0>(GetParam()); int StreamDataSize = std::get<1>(GetParam()); @@ -345,7 +345,7 @@ TEST_P(ReadfIntAndStringTest, readf_int_and_string) const int ExpectedInt = 10; const UInt8 StringLength = 200; const std::string ExpectedString(StringLength, 'x'); - std::array StringSize = {0,0,0,StringLength}; + std::array StringSize{{0,0,0,StringLength}}; const char* Format = std::get<0>(GetParam()); int StreamDataSize = std::get<1>(GetParam()); @@ -400,10 +400,10 @@ INSTANTIATE_TEST_CASE_P( TEST_F(ProtocolUtilTests, readf_string_and_int4bytes) { const UInt8 ExpectedInt = 10; - std::array StreamIntData = {0,0,0,ExpectedInt}; + std::array StreamIntData{{0,0,0,ExpectedInt}}; const std::string ExpectedStr(32768, 'x'); - std::array Size = {0,0,128,0}; + std::array Size{{0,0,128,0}}; EXPECT_CALL(stream, read(_, _)) .WillOnce( @@ -434,11 +434,11 @@ TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes) { std::vector Actual = {}; const std::vector Expected4Bytes = {10,10}; - std::array StreamVectorSize = {0,0,0,2}; - std::array StreamData4Bytes = {0, 0, 0, 10}; + std::array StreamVectorSize{{0,0,0,2}}; + std::array StreamData4Bytes{{0, 0, 0, 10}}; const std::string ExpString(32768, 'x'); - std::array SizeString = {0,0,128,0}; + std::array SizeString{{0,0,128,0}}; EXPECT_CALL(stream, read(_, _)) .WillOnce( @@ -475,11 +475,11 @@ TEST_F(ProtocolUtilTests, readf_vector_int4bytes_and_string) { std::vector Actual4Bytes = {}; const std::vector Expected4Bytes = {10,10}; - std::array StreamVectorSize = {0,0,0,2}; - std::array StreamData4Bytes = {0, 0, 0, 10}; + std::array StreamVectorSize{{0,0,0,2}}; + std::array StreamData4Bytes{{0, 0, 0, 10}}; const std::string ExpectedString(32768, 'x'); - std::array StringSize = {0,0,128,0}; + std::array StringSize{{0,0,128,0}}; EXPECT_CALL(stream, read(_, _)) .WillOnce( @@ -605,7 +605,7 @@ TEST_F(ProtocolUtilTests, write_string_test) TEST_F(ProtocolUtilTests, write_raw_bytes_test) { const UInt32 Size = 5; - const std::array Expected = {10, 20, 30, 40, 50}; + const std::array Expected{{10, 20, 30, 40, 50}}; EXPECT_CALL(stream, write(EqVoidVectorInt1byte(Expected), Expected.size() + sizeof (UInt32))); ProtocolUtil::writef(&stream, "%S", Size, &Expected); }