From 05a66271a105aab1b7b1ad91bdd3d1448fe13744 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Fri, 2 Oct 2020 15:34:11 +0300 Subject: [PATCH 1/6] SYNERGY-324 Update behaviour when unregistered --- src/gui/src/ActivationDialog.cpp | 19 +++++++++--------- src/gui/src/LicenseManager.cpp | 8 ++++---- src/gui/src/LicenseManager.h | 2 +- src/gui/src/MainWindow.cpp | 20 +++++++++---------- src/gui/src/MainWindow.h | 2 +- src/lib/shared/SerialKey.cpp | 13 ++++++++++++ src/lib/shared/SerialKey.h | 1 + src/test/unittests/shared/SerialKeyTests.cpp | 21 +++++++++++++++++++- 8 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/gui/src/ActivationDialog.cpp b/src/gui/src/ActivationDialog.cpp index 9033ad709..3e6d13942 100644 --- a/src/gui/src/ActivationDialog.cpp +++ b/src/gui/src/ActivationDialog.cpp @@ -43,16 +43,15 @@ ActivationDialog::~ActivationDialog() void ActivationDialog::reject() { - if (m_LicenseManager->activeEdition() == kUnregistered) { - CancelActivationDialog cancelActivationDialog(this); - if (QDialog::Accepted == cancelActivationDialog.exec()) { - m_LicenseManager->skipActivation(); - m_appConfig->activationHasRun(true); - } else { - return; - } - } - QDialog::reject(); + if (m_LicenseManager->activeEdition() == kUnregistered) { + CancelActivationDialog cancelActivationDialog(this); + if (QDialog::Accepted == cancelActivationDialog.exec()) { + m_LicenseManager->skipActivation(); + } else { + return; + } + } + QDialog::reject(); } void ActivationDialog::accept() diff --git a/src/gui/src/LicenseManager.cpp b/src/gui/src/LicenseManager.cpp index 1447b91bf..14fbbd53e 100644 --- a/src/gui/src/LicenseManager.cpp +++ b/src/gui/src/LicenseManager.cpp @@ -47,8 +47,8 @@ LicenseManager::setSerialKey(SerialKey serialKey, bool acceptExpired) emit serialKeyChanged(m_serialKey); emit showLicenseNotice(getLicenseNotice()); - if (m_serialKey.isExpired(currentTime)) { - emit LicenseExpired(); + if (m_serialKey.isValid()) { + emit InvalidLicense(); } if (m_serialKey.edition() != serialKey.edition()) { @@ -112,8 +112,8 @@ LicenseManager::refresh() m_AppConfig->clearSerialKey(); } } - if (m_serialKey.isExpired(::time(0))) { - emit LicenseExpired(); + if (m_serialKey.isValid()) { + emit InvalidLicense(); } } diff --git a/src/gui/src/LicenseManager.h b/src/gui/src/LicenseManager.h index 823354939..253de5424 100644 --- a/src/gui/src/LicenseManager.h +++ b/src/gui/src/LicenseManager.h @@ -50,7 +50,7 @@ private: signals: void serialKeyChanged (SerialKey) const; void editionChanged (Edition) const; - void LicenseExpired () const; + void InvalidLicense () const; void showLicenseNotice(const QString& notice) const; protected: diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index dee6eee77..e0800408f 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -174,8 +174,8 @@ MainWindow::MainWindow (AppConfig& appConfig, connect (m_LicenseManager, SIGNAL(showLicenseNotice(QString)), this, SLOT(showLicenseNotice(QString)), Qt::QueuedConnection); - connect (m_LicenseManager, SIGNAL(LicenseExpired()), - this, SLOT(LicenseExpired()), Qt::QueuedConnection); + connect (m_LicenseManager, SIGNAL(InvalidLicense()), + this, SLOT(InvalidLicense()), Qt::QueuedConnection); #endif connect (m_AppConfig, SIGNAL(sslToggled(bool)), @@ -613,8 +613,7 @@ void MainWindow::startSynergy() { #ifndef SYNERGY_ENTERPRISE SerialKey serialKey = m_LicenseManager->serialKey(); - time_t currentTime = ::time(0); - if (serialKey.isExpired(currentTime)) { + if (!serialKey.isValid()) { if (QDialog::Rejected == raiseActivationDialog()) { return; } @@ -1152,7 +1151,7 @@ void MainWindow::setEdition(Edition edition) } #ifndef SYNERGY_ENTERPRISE -void MainWindow::LicenseExpired() +void MainWindow::InvalidLicense() { stopSynergy(); m_AppConfig->activationHasRun(false); @@ -1379,12 +1378,11 @@ int MainWindow::raiseActivationDialog() void MainWindow::on_windowShown() { #ifndef SYNERGY_ENTERPRISE - time_t currentTime = ::time(0); - if (!m_AppConfig->activationHasRun() - && ((m_AppConfig->edition() == kUnregistered) || - (m_LicenseManager->serialKey().isExpired(currentTime)))) { - raiseActivationDialog(); - } + if (!m_AppConfig->activationHasRun()){ + if (!m_LicenseManager->serialKey().isValid()) { + raiseActivationDialog(); + } + } #endif } diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index 280ba22d4..c68dfb685 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -132,7 +132,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase public slots: void setEdition(Edition edition); #ifndef SYNERGY_ENTERPRISE - void LicenseExpired(); + void InvalidLicense(); void showLicenseNotice(const QString& message); #endif void appendLogRaw(const QString& text); diff --git a/src/lib/shared/SerialKey.cpp b/src/lib/shared/SerialKey.cpp index 08829ba01..92e9961c6 100644 --- a/src/lib/shared/SerialKey.cpp +++ b/src/lib/shared/SerialKey.cpp @@ -96,6 +96,19 @@ SerialKey::isTemporary() const return m_KeyType.isTemporary(); } +bool +SerialKey::isValid() const +{ + bool Valid = true; + + if (m_edition.getType() == kUnregistered || isExpired(::time(0))) + { + Valid = false; + } + + return Valid; +} + Edition SerialKey::edition() const { diff --git a/src/lib/shared/SerialKey.h b/src/lib/shared/SerialKey.h index abb16fca4..881509626 100644 --- a/src/lib/shared/SerialKey.h +++ b/src/lib/shared/SerialKey.h @@ -37,6 +37,7 @@ public: bool isExpired(time_t currentTime) const; bool isTrial() const; bool isTemporary() const; + bool isValid() const; time_t daysLeft(time_t currentTime) const; std::string email() const; Edition edition() const; diff --git a/src/test/unittests/shared/SerialKeyTests.cpp b/src/test/unittests/shared/SerialKeyTests.cpp index e2dcd005f..645d794af 100644 --- a/src/test/unittests/shared/SerialKeyTests.cpp +++ b/src/test/unittests/shared/SerialKeyTests.cpp @@ -70,7 +70,7 @@ TEST(SerialKeyTests, parse_validV2Serial_valid) bool r = serial.parse("{v2;trial;pro;Bob;1;email;company name;0;86400}"); EXPECT_EQ(true, r); EXPECT_EQ(kPro, serial.edition()); - EXPECT_FALSE(serial.isExpired(0)); + EXPECT_FALSE(serial.isExpired(0)); EXPECT_EQ(true, serial.daysLeft(0)); EXPECT_EQ(true, serial.isExpiring(1)); EXPECT_EQ(true, serial.isTrial()); @@ -207,4 +207,23 @@ TEST(SerialKeyTests, toStringV1BasicSerialKey) EXPECT_EQ(Expected, serial.toString()); } +TEST(SerialKeyTests, IsValidKey_false) +{ + //{v1;basic;Bob;1;email;company name;0;0} + SerialKey serial(kUnregistered); + EXPECT_EQ(false, serial.isValid()); +} + +TEST(SerialKeyTests, IsValidKey_true) +{ + SerialKey serial(kBasic); + EXPECT_EQ(true, serial.isValid()); +} + +TEST(SerialKeyTests, IsValidExpiredKey_false) +{ + SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); + EXPECT_EQ(false, serial.isValid()); +} + From 3d463d5703b71d325a83228ec546fc53771631d8 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Fri, 2 Oct 2020 15:40:39 +0300 Subject: [PATCH 2/6] SYNERGY-324 Update ChangeLog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index a0978ce24..b338a33bf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,7 @@ Enhancements: - #6750 Integrate SonarCloud for static analysis and test coverage - #6792 License key expiry - #6799 Synergy "Business" edition +- #6800 Update behaviour when unregistered v1.12.0-stable =========== From 8bd61d021c16cd0fb9ad4bf1775e87c1451d7d44 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Fri, 2 Oct 2020 15:55:44 +0300 Subject: [PATCH 3/6] SYNERGY-324 Fix sonar warning --- src/gui/src/MainWindow.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index e0800408f..577a7d837 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -1378,10 +1378,9 @@ int MainWindow::raiseActivationDialog() void MainWindow::on_windowShown() { #ifndef SYNERGY_ENTERPRISE - if (!m_AppConfig->activationHasRun()){ - if (!m_LicenseManager->serialKey().isValid()) { + if (!m_AppConfig->activationHasRun() && + !m_LicenseManager->serialKey().isValid()){ raiseActivationDialog(); - } } #endif } From 8deae1005d7de51cb841c252f58d416548af5557 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Fri, 2 Oct 2020 16:11:08 +0300 Subject: [PATCH 4/6] SYNERGY-324 Fixed wrong conditions --- src/gui/src/LicenseManager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gui/src/LicenseManager.cpp b/src/gui/src/LicenseManager.cpp index 14fbbd53e..a9633cb91 100644 --- a/src/gui/src/LicenseManager.cpp +++ b/src/gui/src/LicenseManager.cpp @@ -47,7 +47,7 @@ LicenseManager::setSerialKey(SerialKey serialKey, bool acceptExpired) emit serialKeyChanged(m_serialKey); emit showLicenseNotice(getLicenseNotice()); - if (m_serialKey.isValid()) { + if (!m_serialKey.isValid()) { emit InvalidLicense(); } @@ -112,7 +112,7 @@ LicenseManager::refresh() m_AppConfig->clearSerialKey(); } } - if (m_serialKey.isValid()) { + if (!m_serialKey.isValid()) { emit InvalidLicense(); } } From 836e0444d38af80f3404f0683e7e4214b28ba073 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Mon, 5 Oct 2020 16:29:33 +0300 Subject: [PATCH 5/6] SYNERGY-324 Removed tabs in SerialKey.h --- src/lib/shared/SerialKey.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/shared/SerialKey.h b/src/lib/shared/SerialKey.h index 881509626..cfaf19da6 100644 --- a/src/lib/shared/SerialKey.h +++ b/src/lib/shared/SerialKey.h @@ -37,7 +37,7 @@ public: bool isExpired(time_t currentTime) const; bool isTrial() const; bool isTemporary() const; - bool isValid() const; + bool isValid() const; time_t daysLeft(time_t currentTime) const; std::string email() const; Edition edition() const; @@ -63,7 +63,7 @@ private: unsigned m_userLimit; unsigned long long m_warnTime; unsigned long long m_expireTime; - SerialKeyEdition m_edition; + SerialKeyEdition m_edition; SerialKeyType m_KeyType; }; From 90fce9af2ab5c7d4c869ca4f20b1c3f67ec121b6 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Mon, 5 Oct 2020 18:34:03 +0300 Subject: [PATCH 6/6] SYNERGY-324 Replace tabs with spaces. --- src/gui/src/ActivationDialog.cpp | 26 +- src/gui/src/LicenseManager.cpp | 246 ++++++++--------- src/gui/src/LicenseManager.h | 10 +- src/gui/src/MainWindow.cpp | 10 +- src/lib/shared/SerialKey.cpp | 274 +++++++++---------- src/test/unittests/shared/SerialKeyTests.cpp | 72 ++--- 6 files changed, 319 insertions(+), 319 deletions(-) diff --git a/src/gui/src/ActivationDialog.cpp b/src/gui/src/ActivationDialog.cpp index 3e6d13942..0569449e1 100644 --- a/src/gui/src/ActivationDialog.cpp +++ b/src/gui/src/ActivationDialog.cpp @@ -30,10 +30,10 @@ ActivationDialog::ActivationDialog(QWidget* parent, AppConfig& appConfig, void ActivationDialog::refreshSerialKey() { - ui->m_pTextEditSerialKey->setText(m_appConfig->serialKey()); - ui->m_pTextEditSerialKey->setFocus(); - ui->m_pTextEditSerialKey->moveCursor(QTextCursor::End); - ui->m_trialLabel->setText(tr(m_LicenseManager->getLicenseNotice().toStdString().c_str())); + ui->m_pTextEditSerialKey->setText(m_appConfig->serialKey()); + ui->m_pTextEditSerialKey->setFocus(); + ui->m_pTextEditSerialKey->moveCursor(QTextCursor::End); + ui->m_trialLabel->setText(tr(m_LicenseManager->getLicenseNotice().toStdString().c_str())); } ActivationDialog::~ActivationDialog() @@ -43,15 +43,15 @@ ActivationDialog::~ActivationDialog() void ActivationDialog::reject() { - if (m_LicenseManager->activeEdition() == kUnregistered) { - CancelActivationDialog cancelActivationDialog(this); - if (QDialog::Accepted == cancelActivationDialog.exec()) { - m_LicenseManager->skipActivation(); - } else { - return; - } - } - QDialog::reject(); + if (m_LicenseManager->activeEdition() == kUnregistered) { + CancelActivationDialog cancelActivationDialog(this); + if (QDialog::Accepted == cancelActivationDialog.exec()) { + m_LicenseManager->skipActivation(); + } else { + return; + } + } + QDialog::reject(); } void ActivationDialog::accept() diff --git a/src/gui/src/LicenseManager.cpp b/src/gui/src/LicenseManager.cpp index a9633cb91..ee52f9a3a 100644 --- a/src/gui/src/LicenseManager.cpp +++ b/src/gui/src/LicenseManager.cpp @@ -23,208 +23,208 @@ #include LicenseManager::LicenseManager(AppConfig* appConfig) : - m_AppConfig(appConfig), - m_serialKey(appConfig->edition()) { + m_AppConfig(appConfig), + m_serialKey(appConfig->edition()) { } std::pair LicenseManager::setSerialKey(SerialKey serialKey, bool acceptExpired) { - std::pair ret (true, ""); - time_t currentTime = ::time(0); + std::pair ret (true, ""); + time_t currentTime = ::time(0); - if (!acceptExpired && serialKey.isExpired(currentTime)) { - ret.first = false; - ret.second = "Serial key expired"; - return ret; - } + if (!acceptExpired && serialKey.isExpired(currentTime)) { + ret.first = false; + ret.second = "Serial key expired"; + return ret; + } - if (serialKey != m_serialKey) { - using std::swap; - swap (serialKey, m_serialKey); - m_AppConfig->setSerialKey(QString::fromStdString - (m_serialKey.toString())); - emit serialKeyChanged(m_serialKey); + if (serialKey != m_serialKey) { + using std::swap; + swap (serialKey, m_serialKey); + m_AppConfig->setSerialKey(QString::fromStdString + (m_serialKey.toString())); + emit serialKeyChanged(m_serialKey); - emit showLicenseNotice(getLicenseNotice()); - if (!m_serialKey.isValid()) { - emit InvalidLicense(); - } + emit showLicenseNotice(getLicenseNotice()); + if (!m_serialKey.isValid()) { + emit InvalidLicense(); + } - if (m_serialKey.edition() != serialKey.edition()) { - m_AppConfig->setEdition(m_serialKey.edition()); - emit editionChanged(m_serialKey.edition()); - } - } + if (m_serialKey.edition() != serialKey.edition()) { + m_AppConfig->setEdition(m_serialKey.edition()); + emit editionChanged(m_serialKey.edition()); + } + } - return ret; + return ret; } void LicenseManager::notifyUpdate(QString fromVersion, QString toVersion) const { - if ((fromVersion == "Unknown") - && (m_serialKey == SerialKey(kUnregistered))) { - return; - } + if ((fromVersion == "Unknown") + && (m_serialKey == SerialKey(kUnregistered))) { + return; + } - ActivationNotifier* notifier = new ActivationNotifier(); - notifier->setUpdateInfo (fromVersion, toVersion, - QString::fromStdString(m_serialKey.toString())); + ActivationNotifier* notifier = new ActivationNotifier(); + notifier->setUpdateInfo (fromVersion, toVersion, + QString::fromStdString(m_serialKey.toString())); - QThread* thread = new QThread(); - connect(notifier, SIGNAL(finished()), thread, SLOT(quit())); - connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); + QThread* thread = new QThread(); + connect(notifier, SIGNAL(finished()), thread, SLOT(quit())); + connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - notifier->moveToThread(thread); - thread->start(); + notifier->moveToThread(thread); + thread->start(); - QMetaObject::invokeMethod(notifier, "notifyUpdate", - Qt::QueuedConnection); + QMetaObject::invokeMethod(notifier, "notifyUpdate", + Qt::QueuedConnection); } Edition LicenseManager::activeEdition() const { - return m_serialKey.edition(); + return m_serialKey.edition(); } QString LicenseManager::activeEditionName() const { - return getEditionName(activeEdition(), m_serialKey.isTrial()); + return getEditionName(activeEdition(), m_serialKey.isTrial()); } SerialKey LicenseManager::serialKey() const { - return m_serialKey; + return m_serialKey; } void LicenseManager::refresh() { - if (!m_AppConfig->serialKey().isEmpty()) { - try { - SerialKey serialKey (m_AppConfig->serialKey().toStdString()); - setSerialKey(serialKey, true); - } catch (...) { - m_AppConfig->clearSerialKey(); - } - } - if (!m_serialKey.isValid()) { - emit InvalidLicense(); - } + if (!m_AppConfig->serialKey().isEmpty()) { + try { + SerialKey serialKey (m_AppConfig->serialKey().toStdString()); + setSerialKey(serialKey, true); + } catch (...) { + m_AppConfig->clearSerialKey(); + } + } + if (!m_serialKey.isValid()) { + emit InvalidLicense(); + } } void LicenseManager::skipActivation() const { - notifyActivation ("skip:unknown"); + notifyActivation ("skip:unknown"); } QString LicenseManager::getEditionName(Edition const edition, bool trial) { - SerialKeyEdition KeyEdition(edition); - std::string name = KeyEdition.getDisplayName(); + SerialKeyEdition KeyEdition(edition); + std::string name = KeyEdition.getDisplayName(); - if (trial) { - name += " (Trial)"; - } + if (trial) { + name += " (Trial)"; + } - return QString::fromUtf8 (name.c_str(), static_cast(name.size())); + return QString::fromUtf8 (name.c_str(), static_cast(name.size())); } void LicenseManager::notifyActivation(QString identity) const { - ActivationNotifier* notifier = new ActivationNotifier(); - notifier->setIdentity(identity); + ActivationNotifier* notifier = new ActivationNotifier(); + notifier->setIdentity(identity); - QThread* thread = new QThread(); - connect(notifier, SIGNAL(finished()), thread, SLOT(quit())); - connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); + QThread* thread = new QThread(); + connect(notifier, SIGNAL(finished()), thread, SLOT(quit())); + connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - notifier->moveToThread(thread); - thread->start(); + notifier->moveToThread(thread); + thread->start(); - QMetaObject::invokeMethod(notifier, "notify", Qt::QueuedConnection); + QMetaObject::invokeMethod(notifier, "notify", Qt::QueuedConnection); } QString LicenseManager::getLicenseNotice() const { - QString Notice; + QString Notice; - if (m_serialKey.isTemporary()){ - if (m_serialKey.isTrial()){ - Notice = getTrialNotice(); - } - else{ - Notice = getTemporaryNotice(); - } - } + if (m_serialKey.isTemporary()){ + if (m_serialKey.isTrial()){ + Notice = getTrialNotice(); + } + else{ + Notice = getTemporaryNotice(); + } + } - return Notice; + return Notice; } QString LicenseManager::getTrialNotice() const { - QString Notice; + QString Notice; - if (m_serialKey.isExpired(::time(0))){ - Notice = "

Your %1 trial has expired. " - "" - "Buy now!

"; - Notice = Notice.arg(LicenseManager::getEditionName(activeEdition())); - } - else{ - Notice = "

%1 day%3 of " - "your %2 trial remain%5. " - "Buy now!" - "

"; + if (m_serialKey.isExpired(::time(0))){ + Notice = "

Your %1 trial has expired. " + "" + "Buy now!

"; + Notice = Notice.arg(LicenseManager::getEditionName(activeEdition())); + } + else{ + Notice = "

%1 day%3 of " + "your %2 trial remain%5. " + "Buy now!" + "

"; - time_t daysLeft = m_serialKey.daysLeft(::time(0)); - Notice = Notice - .arg (daysLeft) - .arg (LicenseManager::getEditionName(activeEdition())) - .arg ((daysLeft == 1) ? "" : "s") - .arg ((daysLeft == 1) ? "s" : ""); - } + time_t daysLeft = m_serialKey.daysLeft(::time(0)); + Notice = Notice + .arg (daysLeft) + .arg (LicenseManager::getEditionName(activeEdition())) + .arg ((daysLeft == 1) ? "" : "s") + .arg ((daysLeft == 1) ? "s" : ""); + } - return Notice; + return Notice; } QString LicenseManager::getTemporaryNotice() const { - QString Notice; + QString Notice; - if (m_serialKey.isExpired(::time(0))) { - Notice = "

Your license has expired. " - "" - "Renew now!

"; - } - else if (m_serialKey.isExpiring(::time(0))) { - Notice = "

%1 day%2 " - "before your license expires." - "Renew now!" - "

"; + if (m_serialKey.isExpired(::time(0))) { + Notice = "

Your license has expired. " + "" + "Renew now!

"; + } + else if (m_serialKey.isExpiring(::time(0))) { + Notice = "

%1 day%2 " + "before your license expires." + "Renew now!" + "

"; - time_t daysLeft = m_serialKey.daysLeft(::time(0)); - Notice = Notice.arg (daysLeft).arg ((daysLeft == 1) ? "" : "s"); - } + time_t daysLeft = m_serialKey.daysLeft(::time(0)); + Notice = Notice.arg (daysLeft).arg ((daysLeft == 1) ? "" : "s"); + } - return Notice; + return Notice; } diff --git a/src/gui/src/LicenseManager.h b/src/gui/src/LicenseManager.h index 253de5424..d0401a5a3 100644 --- a/src/gui/src/LicenseManager.h +++ b/src/gui/src/LicenseManager.h @@ -37,10 +37,10 @@ public: Edition activeEdition() const; QString activeEditionName() const; SerialKey serialKey() const; - void skipActivation() const; - void notifyUpdate(QString fromVersion, QString toVersion) const; - static QString getEditionName(Edition edition, bool trial = false); - void notifyActivation(QString identity) const; + void skipActivation() const; + void notifyUpdate(QString fromVersion, QString toVersion) const; + static QString getEditionName(Edition edition, bool trial = false); + void notifyActivation(QString identity) const; QString getLicenseNotice() const; private: @@ -50,7 +50,7 @@ private: signals: void serialKeyChanged (SerialKey) const; void editionChanged (Edition) const; - void InvalidLicense () const; + void InvalidLicense () const; void showLicenseNotice(const QString& notice) const; protected: diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 577a7d837..0b4b3b531 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -174,8 +174,8 @@ MainWindow::MainWindow (AppConfig& appConfig, connect (m_LicenseManager, SIGNAL(showLicenseNotice(QString)), this, SLOT(showLicenseNotice(QString)), Qt::QueuedConnection); - connect (m_LicenseManager, SIGNAL(InvalidLicense()), - this, SLOT(InvalidLicense()), Qt::QueuedConnection); + connect (m_LicenseManager, SIGNAL(InvalidLicense()), + this, SLOT(InvalidLicense()), Qt::QueuedConnection); #endif connect (m_AppConfig, SIGNAL(sslToggled(bool)), @@ -569,8 +569,8 @@ void MainWindow::checkSecureSocket(const QString& line) // obviously not very secure, since this can be tricked by injecting something // into the log. however, since we don't have IPC between core and GUI... patches welcome. const int index = line.indexOf(tlsCheckString, 0, Qt::CaseInsensitive); - if (index > 0) { - secureSocket(true); + if (index > 0) { + secureSocket(true); //Get the protocol version from the line m_SecureSocketVersion = line.mid(index + strlen(tlsCheckString)); @@ -613,7 +613,7 @@ void MainWindow::startSynergy() { #ifndef SYNERGY_ENTERPRISE SerialKey serialKey = m_LicenseManager->serialKey(); - if (!serialKey.isValid()) { + if (!serialKey.isValid()) { if (QDialog::Rejected == raiseActivationDialog()) { return; } diff --git a/src/lib/shared/SerialKey.cpp b/src/lib/shared/SerialKey.cpp index 92e9961c6..1db94308e 100644 --- a/src/lib/shared/SerialKey.cpp +++ b/src/lib/shared/SerialKey.cpp @@ -31,88 +31,88 @@ using namespace std; static std::string hexEncode (std::string const& str); SerialKey::SerialKey(Edition edition): - m_userLimit(1), - m_warnTime(ULLONG_MAX), - m_expireTime(ULLONG_MAX), - m_edition(edition) + m_userLimit(1), + m_warnTime(ULLONG_MAX), + m_expireTime(ULLONG_MAX), + m_edition(edition) { } SerialKey::SerialKey(std::string serial) : - m_userLimit(1), - m_warnTime(0), - m_expireTime(0), - m_edition(kBasic) + m_userLimit(1), + m_warnTime(0), + m_expireTime(0), + m_edition(kBasic) { - string plainText = decode(serial); - bool valid = false; - if (!plainText.empty()) { - valid = parse(plainText); - } - if (!valid) { - throw std::runtime_error ("Invalid serial key"); - } + string plainText = decode(serial); + bool valid = false; + if (!plainText.empty()) { + valid = parse(plainText); + } + if (!valid) { + throw std::runtime_error ("Invalid serial key"); + } } bool SerialKey::isExpiring(time_t currentTime) const { - bool result = false; + bool result = false; - if (isTemporary()) { - unsigned long long currentTimeAsLL = static_cast(currentTime); - if ((m_warnTime <= currentTimeAsLL) && (currentTimeAsLL < m_expireTime)) { - result = true; - } - } + if (isTemporary()) { + unsigned long long currentTimeAsLL = static_cast(currentTime); + if ((m_warnTime <= currentTimeAsLL) && (currentTimeAsLL < m_expireTime)) { + result = true; + } + } - return result; + return result; } bool SerialKey::isExpired(time_t currentTime) const { - bool result = false; + bool result = false; - if (isTemporary()) { - unsigned long long currentTimeAsLL = static_cast(currentTime); - if (m_expireTime <= currentTimeAsLL) { - result = true; - } - } + if (isTemporary()) { + unsigned long long currentTimeAsLL = static_cast(currentTime); + if (m_expireTime <= currentTimeAsLL) { + result = true; + } + } - return result; + return result; } bool SerialKey::isTrial() const { - return m_KeyType.isTrial(); + return m_KeyType.isTrial(); } bool SerialKey::isTemporary() const { - return m_KeyType.isTemporary(); + return m_KeyType.isTemporary(); } bool SerialKey::isValid() const { - bool Valid = true; + bool Valid = true; - if (m_edition.getType() == kUnregistered || isExpired(::time(0))) - { - Valid = false; - } + if (m_edition.getType() == kUnregistered || isExpired(::time(0))) + { + Valid = false; + } - return Valid; + return Valid; } Edition SerialKey::edition() const { - return m_edition.getType(); + return m_edition.getType(); } static std::string @@ -131,134 +131,134 @@ hexEncode (std::string const& str) { std::string SerialKey::toString() const { - std::ostringstream oss; - oss << "{"; - if (isTemporary()) { - if (isTrial()){ - oss << "v2;" << SerialKeyType::TRIAL << ";"; - } - else{ - oss << "v2;" << SerialKeyType::SUBSCRIPTION << ";"; - } - } else { - oss << "v1;"; - } - oss << m_edition.getName() << ";"; - oss << m_name << ";"; - oss << m_userLimit << ";"; - oss << m_email << ";"; - oss << m_company << ";"; - oss << (isTemporary() ? m_warnTime : 0) << ";"; - oss << (isTemporary() ? m_expireTime : 0); - oss << "}"; - return hexEncode(oss.str()); + std::ostringstream oss; + oss << "{"; + if (isTemporary()) { + if (isTrial()){ + oss << "v2;" << SerialKeyType::TRIAL << ";"; + } + else{ + oss << "v2;" << SerialKeyType::SUBSCRIPTION << ";"; + } + } else { + oss << "v1;"; + } + oss << m_edition.getName() << ";"; + oss << m_name << ";"; + oss << m_userLimit << ";"; + oss << m_email << ";"; + oss << m_company << ";"; + oss << (isTemporary() ? m_warnTime : 0) << ";"; + oss << (isTemporary() ? m_expireTime : 0); + oss << "}"; + return hexEncode(oss.str()); } time_t SerialKey::daysLeft(time_t currentTime) const { - unsigned long long timeLeft = 0; - unsigned long long const day = 60 * 60 * 24; + unsigned long long timeLeft = 0; + unsigned long long const day = 60 * 60 * 24; - unsigned long long currentTimeAsLL = static_cast(currentTime); - if (currentTimeAsLL < m_expireTime) { - timeLeft = m_expireTime - currentTimeAsLL; - } + unsigned long long currentTimeAsLL = static_cast(currentTime); + if (currentTimeAsLL < m_expireTime) { + timeLeft = m_expireTime - currentTimeAsLL; + } - unsigned long long daysLeft = 0; - daysLeft = timeLeft % day != 0 ? 1 : 0; + unsigned long long daysLeft = 0; + daysLeft = timeLeft % day != 0 ? 1 : 0; - return timeLeft / day + daysLeft; + return timeLeft / day + daysLeft; } std::string SerialKey::email() const { - return m_email; + return m_email; } std::string SerialKey::decode(const std::string& serial) { - static const char* const lut = "0123456789ABCDEF"; - string output; - size_t len = serial.length(); - if (len & 1) { - return output; - } + static const char* const lut = "0123456789ABCDEF"; + string output; + size_t len = serial.length(); + if (len & 1) { + return output; + } - output.reserve(len / 2); - for (size_t i = 0; i < len; i += 2) { + output.reserve(len / 2); + for (size_t i = 0; i < len; i += 2) { - char a = serial[i]; - char b = serial[i + 1]; + char a = serial[i]; + char b = serial[i + 1]; - const char* p = std::lower_bound(lut, lut + 16, a); - const char* q = std::lower_bound(lut, lut + 16, b); + const char* p = std::lower_bound(lut, lut + 16, a); + const char* q = std::lower_bound(lut, lut + 16, b); - if (*q != b || *p != a) { - return output; - } + if (*q != b || *p != a) { + return output; + } - output.push_back(static_cast(((p - lut) << 4) | (q - lut))); - } + output.push_back(static_cast(((p - lut) << 4) | (q - lut))); + } - return output; + return output; } bool SerialKey::parse(std::string plainSerial) { - string parityStart = plainSerial.substr(0, 1); - string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1); + string parityStart = plainSerial.substr(0, 1); + string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1); - bool valid = false; + bool valid = false; - // check for parity chars { and }, record parity result, then remove them. - if (parityStart == "{" && parityEnd == "}") { - plainSerial = plainSerial.substr(1, plainSerial.length() - 2); + // check for parity chars { and }, record parity result, then remove them. + if (parityStart == "{" && parityEnd == "}") { + plainSerial = plainSerial.substr(1, plainSerial.length() - 2); - // tokenize serialised subscription. - vector parts; - std::string::size_type pos = 0; - bool look = true; - while (look) { - std::string::size_type start = pos; - pos = plainSerial.find(";", pos); - if (pos == string::npos) { - pos = plainSerial.length(); - look = false; - } - parts.push_back(plainSerial.substr(start, pos - start)); - pos += 1; - } + // tokenize serialised subscription. + vector parts; + std::string::size_type pos = 0; + bool look = true; + while (look) { + std::string::size_type start = pos; + pos = plainSerial.find(";", pos); + if (pos == string::npos) { + pos = plainSerial.length(); + look = false; + } + parts.push_back(plainSerial.substr(start, pos - start)); + pos += 1; + } - if ((parts.size() == 8) - && (parts.at(0).find("v1") != string::npos)) { - // e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000} - m_edition.setType(parts.at(1)); - m_name = parts.at(2); - sscanf(parts.at(3).c_str(), "%d", &m_userLimit); - m_email = parts.at(4); - m_company = parts.at(5); - sscanf(parts.at(6).c_str(), "%lld", &m_warnTime); - sscanf(parts.at(7).c_str(), "%lld", &m_expireTime); - valid = true; - } - else if ((parts.size() == 9) - && (parts.at(0).find("v2") != string::npos)) { - // e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000} - m_KeyType.setKeyType(parts.at(1)); - m_edition.setType(parts.at(2)); - m_name = parts.at(3); - sscanf(parts.at(4).c_str(), "%d", &m_userLimit); - m_email = parts.at(5); - m_company = parts.at(6); - sscanf(parts.at(7).c_str(), "%lld", &m_warnTime); - sscanf(parts.at(8).c_str(), "%lld", &m_expireTime); - valid = true; - } - } + if ((parts.size() == 8) + && (parts.at(0).find("v1") != string::npos)) { + // e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000} + m_edition.setType(parts.at(1)); + m_name = parts.at(2); + sscanf(parts.at(3).c_str(), "%d", &m_userLimit); + m_email = parts.at(4); + m_company = parts.at(5); + sscanf(parts.at(6).c_str(), "%lld", &m_warnTime); + sscanf(parts.at(7).c_str(), "%lld", &m_expireTime); + valid = true; + } + else if ((parts.size() == 9) + && (parts.at(0).find("v2") != string::npos)) { + // e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000} + m_KeyType.setKeyType(parts.at(1)); + m_edition.setType(parts.at(2)); + m_name = parts.at(3); + sscanf(parts.at(4).c_str(), "%d", &m_userLimit); + m_email = parts.at(5); + m_company = parts.at(6); + sscanf(parts.at(7).c_str(), "%lld", &m_warnTime); + sscanf(parts.at(8).c_str(), "%lld", &m_expireTime); + valid = true; + } + } - return valid; + return valid; } diff --git a/src/test/unittests/shared/SerialKeyTests.cpp b/src/test/unittests/shared/SerialKeyTests.cpp index 645d794af..4c7b09f12 100644 --- a/src/test/unittests/shared/SerialKeyTests.cpp +++ b/src/test/unittests/shared/SerialKeyTests.cpp @@ -70,7 +70,7 @@ TEST(SerialKeyTests, parse_validV2Serial_valid) bool r = serial.parse("{v2;trial;pro;Bob;1;email;company name;0;86400}"); EXPECT_EQ(true, r); EXPECT_EQ(kPro, serial.edition()); - EXPECT_FALSE(serial.isExpired(0)); + EXPECT_FALSE(serial.isExpired(0)); EXPECT_EQ(true, serial.daysLeft(0)); EXPECT_EQ(true, serial.isExpiring(1)); EXPECT_EQ(true, serial.isTrial()); @@ -156,74 +156,74 @@ TEST(SerialKeyTests, daysLeft_expiredV2TrialBasicSerial_returnZero) //Subscription license tests TEST(SerialKeyTests, isExpiring_validV2SubscriptionBasicSerial_returnFalse) { - // {v2;subscription;basic;Bob;1;email;company name;1;86400} - SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B313B38363430307D"); - EXPECT_EQ(false, serial.isTrial()); - EXPECT_EQ(true, serial.isTemporary()); - EXPECT_FALSE(serial.isExpiring(0)); - EXPECT_EQ(kBasic, serial.edition()); + // {v2;subscription;basic;Bob;1;email;company name;1;86400} + SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B313B38363430307D"); + EXPECT_EQ(false, serial.isTrial()); + EXPECT_EQ(true, serial.isTemporary()); + EXPECT_FALSE(serial.isExpiring(0)); + EXPECT_EQ(kBasic, serial.edition()); } TEST(SerialKeyTests, isExpiring_expiringV2SubscriptionBasicSerial_returnTrue) { - // {v2;subscription;basic;Bob;1;email;company name;0;86400} - SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); - EXPECT_EQ(false, serial.isTrial()); - EXPECT_EQ(true, serial.isTemporary()); - EXPECT_EQ(true, serial.isExpiring(1)); + // {v2;subscription;basic;Bob;1;email;company name;0;86400} + SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); + EXPECT_EQ(false, serial.isTrial()); + EXPECT_EQ(true, serial.isTemporary()); + EXPECT_EQ(true, serial.isExpiring(1)); } TEST(SerialKeyTests, isExpired_expiredV2SubscriptionBasicSerial_returnTrue) { - // {v2;subscription;basic;Bob;1;email;company name;0;86400} - SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); - EXPECT_EQ(false, serial.isTrial()); - EXPECT_EQ(true, serial.isTemporary()); - EXPECT_EQ(true, serial.isExpired(86401)); + // {v2;subscription;basic;Bob;1;email;company name;0;86400} + SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); + EXPECT_EQ(false, serial.isTrial()); + EXPECT_EQ(true, serial.isTemporary()); + EXPECT_EQ(true, serial.isExpired(86401)); } //toString method tests TEST(SerialKeyTests, toStringV2SubscriptionBasicSerialKey) { - //{v2;subscription;basic;Bob;1;email;company name;0;86400} - const std::string Expected = "7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"; - SerialKey serial(Expected); - EXPECT_EQ(Expected, serial.toString()); + //{v2;subscription;basic;Bob;1;email;company name;0;86400} + const std::string Expected = "7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"; + SerialKey serial(Expected); + EXPECT_EQ(Expected, serial.toString()); } TEST(SerialKeyTests, toStringV2TrialBasicSerialKey) { - //{v2;trial;basic;Bob;1;email;company name;0;86400} - const std::string Expected = "7B76323B747269616C3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"; - SerialKey serial(Expected); - EXPECT_EQ(Expected, serial.toString()); + //{v2;trial;basic;Bob;1;email;company name;0;86400} + const std::string Expected = "7B76323B747269616C3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"; + SerialKey serial(Expected); + EXPECT_EQ(Expected, serial.toString()); } TEST(SerialKeyTests, toStringV1BasicSerialKey) { - //{v1;basic;Bob;1;email;company name;0;0} - const std::string Expected = "7B76313B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B307D"; - SerialKey serial(Expected); - EXPECT_EQ(Expected, serial.toString()); + //{v1;basic;Bob;1;email;company name;0;0} + const std::string Expected = "7B76313B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B307D"; + SerialKey serial(Expected); + EXPECT_EQ(Expected, serial.toString()); } TEST(SerialKeyTests, IsValidKey_false) { - //{v1;basic;Bob;1;email;company name;0;0} - SerialKey serial(kUnregistered); - EXPECT_EQ(false, serial.isValid()); + //{v1;basic;Bob;1;email;company name;0;0} + SerialKey serial(kUnregistered); + EXPECT_EQ(false, serial.isValid()); } TEST(SerialKeyTests, IsValidKey_true) { - SerialKey serial(kBasic); - EXPECT_EQ(true, serial.isValid()); + SerialKey serial(kBasic); + EXPECT_EQ(true, serial.isValid()); } TEST(SerialKeyTests, IsValidExpiredKey_false) { - SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); - EXPECT_EQ(false, serial.isValid()); + SerialKey serial("7B76323B737562736372697074696F6E3B62617369633B426F623B313B656D61696C3B636F6D70616E79206E616D653B303B38363430307D"); + EXPECT_EQ(false, serial.isValid()); }