From 8a509f4ba94437278776b7e0581721cb9c73763e Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov <71632867+SerhiiGadzhilov@users.noreply.github.com> Date: Wed, 15 Jun 2022 13:55:41 +0300 Subject: [PATCH] SYNERGY1-1555 maintenance licences (#7190) * SYNERGY1-1555 Remove unused files * SYNERGY1-1555 Remove unused function isPermanent * SYNERGY1-1555 Update SerialKeyType with maintenance type * SYENRGY1-1555 Add method isMaintenance into SerialKey * SYNERGY1-1555 Check maintenance licenses * SYNERGY1-1555 Replace obsolete functions * SYNERGY1-1555 Fix Linux builds * Update ChangeLog * SYNERGY1-1555 Requested changes --- ChangeLog | 1 + src/gui/src/LicenseManager.cpp | 54 +++++++++++++++---- src/lib/shared/SerialKey.cpp | 12 +++++ src/lib/shared/SerialKey.h | 2 + src/lib/shared/SerialKeyParserV3.cpp | 8 --- src/lib/shared/SerialKeyParserV3.h | 30 ----------- src/lib/shared/SerialKeyType.cpp | 18 +++---- src/lib/shared/SerialKeyType.h | 4 +- src/test/unittests/shared/SerialKeyTests.cpp | 10 ++++ .../unittests/shared/SerialKeyTypeTests.cpp | 12 ++--- 10 files changed, 83 insertions(+), 68 deletions(-) delete mode 100644 src/lib/shared/SerialKeyParserV3.cpp delete mode 100644 src/lib/shared/SerialKeyParserV3.h diff --git a/ChangeLog b/ChangeLog index 48c9cb0f6..88d1dc9b8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ Bug fixes: Enhancements: - #7183 Refactoring for Synergy 3 licenses +- #7190 Maintenance license serial key support 1.14.4 ====== diff --git a/src/gui/src/LicenseManager.cpp b/src/gui/src/LicenseManager.cpp index fe8ae0d69..49d66c081 100644 --- a/src/gui/src/LicenseManager.cpp +++ b/src/gui/src/LicenseManager.cpp @@ -22,6 +22,47 @@ #include #include #include +#include +#include + +namespace { + +std::string +getMaintenanceMessage(const SerialKey& serialKey) +{ + auto expiration = QDateTime::fromTime_t(serialKey.getExpiration()).date(); + QString message = "The license key you used will only work with versions of Synergy released before %1." + "

To use this version, you’ll need to renew your Synergy maintenance license. " + "Renew today.

"; + auto formatedDate = QLocale("en_US").toString(expiration, "MMM dd yyyy"); + return message.arg(formatedDate).toStdString(); +} + +void +checkSerialKey(const SerialKey& serialKey, bool acceptExpired) +{ + if (serialKey.isMaintenance()) { + auto buildDate = QDateTime::fromString(__TIMESTAMP__).toTime_t(); + + if (buildDate > serialKey.getExpiration()) { + throw std::runtime_error(getMaintenanceMessage(serialKey)); + } + } + + if (!acceptExpired && serialKey.isExpired(::time(nullptr))) { + throw std::runtime_error("Serial key expired"); + } + + #ifdef SYNERGY_BUSINESS + if (!serialKey.isValid()) { + throw std::runtime_error("The serial key is not compatible with the business version of Synergy."); + } + #endif +} + +} + LicenseManager::LicenseManager(AppConfig* appConfig) : m_AppConfig(appConfig), @@ -31,17 +72,7 @@ LicenseManager::LicenseManager(AppConfig* appConfig) : void LicenseManager::setSerialKey(SerialKey serialKey, bool acceptExpired) { - time_t currentTime = ::time(0); - - if (!acceptExpired && serialKey.isExpired(currentTime)) { - throw std::runtime_error("Serial key expired"); - } - - #ifdef SYNERGY_BUSINESS - if (!serialKey.isValid()) { - throw std::runtime_error("The serial key is not compatible with the business version of Synergy."); - } - #endif + checkSerialKey(serialKey, acceptExpired); if (serialKey != m_serialKey) { using std::swap; @@ -108,6 +139,7 @@ LicenseManager::refresh() SerialKey serialKey (m_AppConfig->serialKey().toStdString()); setSerialKey(serialKey, true); } catch (...) { + m_serialKey = SerialKey(); m_AppConfig->clearSerialKey(); } } diff --git a/src/lib/shared/SerialKey.cpp b/src/lib/shared/SerialKey.cpp index 837a41ac9..01a9fa122 100644 --- a/src/lib/shared/SerialKey.cpp +++ b/src/lib/shared/SerialKey.cpp @@ -83,6 +83,12 @@ SerialKey::isTemporary() const return m_data.keyType.isTemporary(); } +bool +SerialKey::isMaintenance() const +{ + return m_data.keyType.isMaintenance(); +} + bool SerialKey::isValid() const { @@ -125,6 +131,12 @@ SerialKey::daysLeft(time_t currentTime) const return timeLeft / day + daysLeft; } +time_t +SerialKey::getExpiration() const +{ + return m_data.expireTime; +} + int SerialKey::getSpanLeft(time_t time) const { diff --git a/src/lib/shared/SerialKey.h b/src/lib/shared/SerialKey.h index 6fd23abdc..f3f10d05b 100644 --- a/src/lib/shared/SerialKey.h +++ b/src/lib/shared/SerialKey.h @@ -32,8 +32,10 @@ public: bool isExpired(time_t currentTime) const; bool isTrial() const; bool isTemporary() const; + bool isMaintenance() const; bool isValid() const; time_t daysLeft(time_t currentTime) const; + time_t getExpiration() const; int getSpanLeft(time_t time = ::time(0)) const; Edition edition() const; const std::string& toString() const; diff --git a/src/lib/shared/SerialKeyParserV3.cpp b/src/lib/shared/SerialKeyParserV3.cpp deleted file mode 100644 index f7acb49b0..000000000 --- a/src/lib/shared/SerialKeyParserV3.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "SerialKeyParserV3.h" - -bool -SerialKeyParserV3::parse(const std::string& plainSerial) -{ - setKey(plainSerial); - return true; -} diff --git a/src/lib/shared/SerialKeyParserV3.h b/src/lib/shared/SerialKeyParserV3.h deleted file mode 100644 index 3938a902c..000000000 --- a/src/lib/shared/SerialKeyParserV3.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * synergy -- mouse and keyboard sharing utility - * Copyright (C) 2022 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#pragma once - -#include "SerialKeyParser.h" - -class SerialKeyParserV3 : public SerialKeyParser -{ -public: - /** - * @brief parse serial key - * @param plainSerial encoded serial key - * @return true if parsed - */ - bool parse(const std::string& plainSerial) override; -}; diff --git a/src/lib/shared/SerialKeyType.cpp b/src/lib/shared/SerialKeyType.cpp index ff3ae7aa8..c12336725 100644 --- a/src/lib/shared/SerialKeyType.cpp +++ b/src/lib/shared/SerialKeyType.cpp @@ -19,6 +19,7 @@ const std::string SerialKeyType::TRIAL = "trial"; const std::string SerialKeyType::SUBSCRIPTION = "subscription"; +const std::string SerialKeyType::MAINTENANCE = "maintenance"; SerialKeyType::SerialKeyType() { @@ -28,16 +29,9 @@ SerialKeyType::SerialKeyType() void SerialKeyType::setKeyType(const std::string& Type) { - m_isTrial = false; - m_isTemporary = false; - - if (Type == SerialKeyType::TRIAL){ - m_isTrial = true; - m_isTemporary = true; - } - else if (Type == SerialKeyType::SUBSCRIPTION){ - m_isTemporary = true; - } + m_isTrial = (Type == SerialKeyType::TRIAL); + m_isTemporary = (m_isTrial || (Type == SerialKeyType::SUBSCRIPTION)); + m_isMaintenance = (Type == SerialKeyType::MAINTENANCE); } bool @@ -53,8 +47,8 @@ SerialKeyType::isTemporary() const } bool -SerialKeyType::isPermanent() const +SerialKeyType::isMaintenance() const { - return (!m_isTemporary); + return m_isMaintenance; } diff --git a/src/lib/shared/SerialKeyType.h b/src/lib/shared/SerialKeyType.h index bd1154d16..aeed90cca 100644 --- a/src/lib/shared/SerialKeyType.h +++ b/src/lib/shared/SerialKeyType.h @@ -25,17 +25,19 @@ private: public: static const std::string TRIAL; static const std::string SUBSCRIPTION; + static const std::string MAINTENANCE; SerialKeyType(); void setKeyType(const std::string& Type); bool isTrial() const; bool isTemporary() const; - bool isPermanent() const; + bool isMaintenance() const; private: bool m_isTrial = false; bool m_isTemporary = false; + bool m_isMaintenance = false; }; inline bool diff --git a/src/test/unittests/shared/SerialKeyTests.cpp b/src/test/unittests/shared/SerialKeyTests.cpp index 4cde4e01f..eaf757115 100644 --- a/src/test/unittests/shared/SerialKeyTests.cpp +++ b/src/test/unittests/shared/SerialKeyTests.cpp @@ -116,6 +116,16 @@ TEST(SerialKeyTests, isExpiring_expiringV2SubscriptionBasicSerial_returnTrue) EXPECT_EQ(false, serial.isTrial()); EXPECT_EQ(true, serial.isTemporary()); EXPECT_EQ(true, serial.isExpiring(1)); + EXPECT_FALSE(serial.isMaintenance()); +} + +TEST(SerialKeyTests, isExpiring_expiringV2MentenanceSerial_returnTrue) +{ + // {v2;maintenance;basic;Bob;1;email;company name;0;86400} + SerialKey serial("7B76323B6D61696E74656E616E63653B70726F3B736572686969206861647A68696C6F763B313B7365726869694073796D6C6573732E636F6D3B203B303B313635353132343139307D"); + EXPECT_FALSE(serial.isTrial()); + EXPECT_FALSE(serial.isTemporary()); + EXPECT_TRUE(serial.isMaintenance()); } TEST(SerialKeyTests, isExpired_expiredV2SubscriptionBasicSerial_returnTrue) diff --git a/src/test/unittests/shared/SerialKeyTypeTests.cpp b/src/test/unittests/shared/SerialKeyTypeTests.cpp index 66f1ca9ce..a29cb631f 100644 --- a/src/test/unittests/shared/SerialKeyTypeTests.cpp +++ b/src/test/unittests/shared/SerialKeyTypeTests.cpp @@ -26,7 +26,7 @@ TEST(SerialKeyTypeTests, TrialTemporaryKeyType_false) SerialKeyType KeyType; EXPECT_EQ(false, KeyType.isTrial()); EXPECT_EQ(false, KeyType.isTemporary()); - EXPECT_EQ(true, KeyType.isPermanent()); + EXPECT_FALSE(KeyType.isMaintenance()); } TEST(SerialKeyTypeTests, TrialTemporaryKeyType_true) @@ -35,7 +35,7 @@ TEST(SerialKeyTypeTests, TrialTemporaryKeyType_true) KeyType.setKeyType("trial"); EXPECT_EQ(true, KeyType.isTrial()); EXPECT_EQ(true, KeyType.isTemporary()); - EXPECT_EQ(false, KeyType.isPermanent()); + EXPECT_FALSE(KeyType.isMaintenance()); } TEST(SerialKeyTypeTests, TemporaryKeyType_true) @@ -44,14 +44,14 @@ TEST(SerialKeyTypeTests, TemporaryKeyType_true) KeyType.setKeyType("subscription"); EXPECT_EQ(false, KeyType.isTrial()); EXPECT_EQ(true, KeyType.isTemporary()); - EXPECT_EQ(false, KeyType.isPermanent()); + EXPECT_FALSE(KeyType.isMaintenance()); } -TEST(SerialKeyTypeTests, PermanentKeyType_true) +TEST(SerialKeyTypeTests, MaintanenceKeyType_true) { SerialKeyType KeyType; - KeyType.setKeyType(""); + KeyType.setKeyType("maintenance"); EXPECT_EQ(false, KeyType.isTrial()); EXPECT_EQ(false, KeyType.isTemporary()); - EXPECT_EQ(true, KeyType.isPermanent()); + EXPECT_TRUE(KeyType.isMaintenance()); }