1 /*
2  * synergy -- mouse and keyboard sharing utility
3  * Copyright (C) 2016 Symless Ltd.
4  *
5  * This package is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * found in the file LICENSE that should have accompanied this file.
8  *
9  * This package is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <string>
21 #include <ctime>
22 #include "EditionType.h"
23 #include "SerialKeyType.h"
24 #include "SerialKeyEdition.h"
25 
26 #ifdef TEST_ENV
27 #include <gtest/gtest_prod.h>
28 #endif
29 
30 class SerialKey {
31     friend bool operator== (SerialKey const&, SerialKey const&);
32 public:
33     explicit SerialKey(Edition edition = kUnregistered);
34     explicit SerialKey(std::string serial);
35 
36     bool                isExpiring(time_t currentTime) const;
37     bool                isExpired(time_t currentTime) const;
38     bool                isTrial() const;
39     bool                isTemporary() const;
40     bool                isValid() const;
41     time_t              daysLeft(time_t currentTime) const;
42     int                 getSpanLeft(time_t time = ::time(0)) const;
43     std::string         email() const;
44     Edition             edition() const;
45     std::string         toString() const;
46 
47     static std::string  decode(const std::string& serial);
48 
49 private:
50     bool                parse(std::string plainSerial);
51 
52 #ifdef TEST_ENV
53 private:
54     FRIEND_TEST(SerialKeyTests, parse_noParty_invalid);
55     FRIEND_TEST(SerialKeyTests, parse_invalidPartsLenghth_invalid);
56     FRIEND_TEST(SerialKeyTests, parse_validV1Serial_valid);
57     FRIEND_TEST(SerialKeyTests, parse_validV2Serial_valid);
58 #endif
59 
60 private:
61     std::string           m_name;
62     std::string           m_email;
63     std::string           m_company;
64     unsigned              m_userLimit;
65     unsigned long long    m_warnTime;
66     unsigned long long    m_expireTime;
67     SerialKeyEdition      m_edition;
68     SerialKeyType         m_KeyType;
69 };
70 
71 
72 inline bool
73 operator== (SerialKey const& lhs, SerialKey const& rhs) {
74     return (lhs.m_name == rhs.m_name) &&
75             (lhs.m_email == rhs.m_email) &&
76             (lhs.m_company == rhs.m_company) &&
77             (lhs.m_userLimit == rhs.m_userLimit) &&
78             (lhs.m_warnTime == rhs.m_warnTime) &&
79             (lhs.m_expireTime == rhs.m_expireTime) &&
80             (lhs.m_edition == rhs.m_edition) &&
81             (lhs.m_KeyType == rhs.m_KeyType);
82 }
83 
84 inline bool
85 operator!= (SerialKey const& lhs, SerialKey const& rhs) {
86     return !(lhs == rhs);
87 }
88