From ef70d38ff7fcb932f2f32a33e4158bdd4a594b67 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Tue, 27 Feb 2018 16:59:05 +0100 Subject: [PATCH] src: Converted spaces to tabs in license sources --- src/lib_license/License.cpp | 45 +++--- src/lib_license/License.h | 66 ++++----- src/license_generator/Generator.cpp | 196 ++++++++++++------------- src/license_generator/Generator.h | 78 +++++----- src/license_generator/main.cpp | 218 ++++++++++++++-------------- src/test/GeneratorTestSuite.h | 200 ++++++++++++------------- 6 files changed, 401 insertions(+), 402 deletions(-) diff --git a/src/lib_license/License.cpp b/src/lib_license/License.cpp index 5e221ee5..e977eec1 100644 --- a/src/lib_license/License.cpp +++ b/src/lib_license/License.cpp @@ -146,14 +146,14 @@ bool License::isTestLicense() const bool License::isNonCommercialLicenseType() const { - for ( const std::string& nonCommercialLicenseType : NON_COMMERCIAL_LICENSE_TYPES) - { + for ( const std::string& nonCommercialLicenseType : NON_COMMERCIAL_LICENSE_TYPES) + { if (m_type == nonCommercialLicenseType) - { - return true; - } - } - return false; + { + return true; + } + } + return false; } size_t License::getNumberOfUsers() const @@ -209,21 +209,21 @@ std::string License::getUser() const int License::getTimeLeft() const { - const std::string dateText = "YYYY-MMM-DD"; + const std::string dateText = "YYYY-MMM-DD"; std::string dateString = m_expire; - if (dateString.length() != dateText.length()) - { - return -2; - } + if (dateString.length() != dateText.length()) + { + return -2; + } - boost::gregorian::date expireDate( - boost::gregorian::from_simple_string(dateString)); + boost::gregorian::date expireDate( + boost::gregorian::from_simple_string(dateString)); - boost::gregorian::date today = boost::gregorian::day_clock::local_day(); - boost::gregorian::days daysLeft = expireDate - today; + boost::gregorian::date today = boost::gregorian::day_clock::local_day(); + boost::gregorian::days daysLeft = expireDate - today; - return (daysLeft.days() < 0 ? -1 : daysLeft.days()); + return (daysLeft.days() < 0 ? -1 : daysLeft.days()); } void License::writeToFile(const std::string& filename) @@ -286,12 +286,12 @@ bool License::load(std::istream& stream) m_signature = ""; while (getline(stream, line, '\n')) { - std::string l = utility::trim(line); + std::string l = utility::trim(line); if (l == LicenseConstants::END_LICENSE_STRING) { break; } - if (l.size()) + if (l.size()) { m_signature += l; } @@ -350,7 +350,7 @@ bool License::isValid() const std::cout << "No public key loaded" << std::endl; return false; } - try + try { if (m_signature.empty()) { @@ -368,7 +368,6 @@ bool License::isValid() const if (isExpired()) { - std::cout << "License is expired" << std::endl; return false; } @@ -413,7 +412,7 @@ std::string License::getPublicKeyFilename() const { if (m_publicKeyFilename.empty()) { - return "public-sourcetrail" + KEY_FILEENDING; + return "public-sourcetrail" + KEY_FILEENDING; } return m_publicKeyFilename; } @@ -594,7 +593,7 @@ bool License::loadFromEncodedString(const std::string& encodedLicense, const std std::string License::getHashedLicense() const { - return getEncodeKey("fakeKey"); + return getEncodeKey("fakeKey"); } std::string License::getEncodeKey(const std::string applicationLocation) const diff --git a/src/lib_license/License.h b/src/lib_license/License.h index feca55b4..b862f8e0 100644 --- a/src/lib_license/License.h +++ b/src/lib_license/License.h @@ -16,53 +16,53 @@ namespace Botan namespace LicenseConstants { const char BEGIN_LICENSE_STRING[] = "-----BEGIN LICENSE-----"; const char END_LICENSE_STRING[] = "-----END LICENSE-----"; - const char TEST_LICENSE_STRING[] = "Test License"; + const char TEST_LICENSE_STRING[] = "Test License"; const char PRODUCT_STRING[] = "Product: Sourcetrail"; const char LICENSED_TO_STRING[] = "Licensed to: "; const char LICENSE_TYPE_STRING[] = "License type: "; const char VALID_UNTIL_STRING[] = "Valid until: "; const char VALID_UP_TO_STRING[] = "Valid up to version: "; const char SEPARATOR_STRING[] = "-"; - const int MINOR_VERSIONS_PER_YEAR = 4; + const int MINOR_VERSIONS_PER_YEAR = 4; } class License { public: - enum LICENSE_LINE - { - BEGIN_LICENSE_LINE = 0, + enum LICENSE_LINE + { + BEGIN_LICENSE_LINE = 0, SOURCETRAIL_LINE = 1, USER_LINE = 2, - TYPE_LINE = 3, + TYPE_LINE = 3, EXPIRE_LINE = 4, SEPARATOR_LINE=5, - HASH_LINE = 6, - FIRST_SIGNATURE_LINE = 7, - LAST_SIGNATURE_LINE = 13, - END_LICENSE_LINE = 14, - LICENSE_LINES = 15 - }; + HASH_LINE = 6, + FIRST_SIGNATURE_LINE = 7, + LAST_SIGNATURE_LINE = 13, + END_LICENSE_LINE = 14, + LICENSE_LINES = 15 + }; - License(); - ~License(); + License(); + ~License(); std::string getMessage(bool withNewlines = false) const; - std::string getSignature() const; - std::string getVersionLineWithoutPrefix() const; + std::string getSignature() const; + std::string getVersionLineWithoutPrefix() const; std::string getLine(std::istream& stream); std::string getLineWithoutDescription(LICENSE_LINE line) const; - void setLine(const LICENSE_LINE line, const std::string& value); + void setLine(const LICENSE_LINE line, const std::string& value); void setHashLine(const std::string& hash); void setTypeAndNumberOfUsers(const std::string& line); - std::string getLicenseInfo() const; - std::string getPublicKeyFilename() const; - std::string getVersion() const; + std::string getLicenseInfo() const; + std::string getPublicKeyFilename() const; + std::string getVersion() const; std::string getType() const; std::string getUser() const; - size_t getNumberOfUsers() const; + size_t getNumberOfUsers() const; void createHeader( const std::string& user, @@ -76,14 +76,14 @@ public: /// for non Test License -2 int getTimeLeft() const; - std::string getLicenseString() const; + std::string getLicenseString() const; std::string getLicenseEncodedString(const std::string& applicationLocation) const; - std::string getHashedLicense() const; + std::string getHashedLicense() const; - void writeToFile(const std::string& filename); - bool load(std::istream& stream); - bool loadFromString(const std::string& licenseText); - bool loadFromFile(const std::string& filename); + void writeToFile(const std::string& filename); + bool load(std::istream& stream); + bool loadFromString(const std::string& licenseText); + bool loadFromFile(const std::string& filename); bool loadFromEncodedString(const std::string& encodedLicense, const std::string& applicationLocation); bool loadPublicKey(); @@ -93,15 +93,15 @@ public: void setSignature(const std::string&); - bool isValid() const; + bool isValid() const; bool isExpired() const; bool isTestLicense() const; bool isNonCommercialLicenseType() const; - void print(); + void print(); - std::string hashLocation(const std::string&) const; - static bool checkLocation(const std::string&, const std::string&); + std::string hashLocation(const std::string&) const; + static bool checkLocation(const std::string&, const std::string&); private: std::string getEncodeKey(const std::string applicationLocation) const; @@ -122,9 +122,9 @@ private: std::string m_signature; - const std::string KEY_FILEENDING = ".pem"; + const std::string KEY_FILEENDING = ".pem"; - const std::vector NON_COMMERCIAL_LICENSE_TYPES = { { "Private/Academic Single User License" } }; + const std::vector NON_COMMERCIAL_LICENSE_TYPES = { { "Private/Academic Single User License" } }; }; #endif // SOURCETRAIL_LICENSE_H diff --git a/src/license_generator/Generator.cpp b/src/license_generator/Generator.cpp index 1bca54ef..61723f7e 100644 --- a/src/license_generator/Generator.cpp +++ b/src/license_generator/Generator.cpp @@ -27,7 +27,7 @@ const char PUBLIC_KEY_FILE[] = "public-sourcetrail.pem"; Generator::Generator() { - loadPrivateKeyFromString(PRIVATE_KEY); + loadPrivateKeyFromString(PRIVATE_KEY); } Generator::~Generator() @@ -41,20 +41,20 @@ void Generator::generateKeys() std::string Generator::getPrivateKeyFilename() { - if(m_privateKeyFile.empty()) - { - return PRIVATE_KEY_FILE; - } - return m_privateKeyFile; + if(m_privateKeyFile.empty()) + { + return PRIVATE_KEY_FILE; + } + return m_privateKeyFile; } std::string Generator::getPublicKeyFilename() { - if(m_publicKeyFile.empty()) - { - return PUBLIC_KEY_FILE; - } - return m_publicKeyFile; + if(m_publicKeyFile.empty()) + { + return PUBLIC_KEY_FILE; + } + return m_publicKeyFile; } std::string Generator::encodeLicense(const std::string& user, const int days) @@ -63,89 +63,89 @@ std::string Generator::encodeLicense(const std::string& user, const int days) boost::gregorian::days daysToTry(days); boost::gregorian::date expireDate = today + daysToTry; - createLicense(user, LicenseConstants::TEST_LICENSE_STRING, boost::gregorian::to_simple_string(expireDate), 0); + createLicense(user, LicenseConstants::TEST_LICENSE_STRING, boost::gregorian::to_simple_string(expireDate), 0); - return m_license->getLicenseString(); + return m_license->getLicenseString(); } std::string Generator::encodeLicense( - const std::string& user, - const std::string& licenseType, - size_t numberOfUsers, - const std::string& version + const std::string& user, + const std::string& licenseType, + size_t numberOfUsers, + const std::string& version ) { - m_license = nullptr; + m_license = nullptr; if (user.size() <= 0) { std::cout << "No user given" << std::endl; - return ""; + return ""; } if (licenseType.size() <= 0) { std::cout << "No license type given" << std::endl; - return ""; + return ""; } - if (!version.empty()) - { - Version tempVersion = Version::fromString(version); - if (tempVersion.isValid()) - { - createLicense(user, licenseType, tempVersion.toShortString(), numberOfUsers); - } - } + if (!version.empty()) + { + Version tempVersion = Version::fromString(version); + if (tempVersion.isValid()) + { + createLicense(user, licenseType, tempVersion.toShortString(), numberOfUsers); + } + } - if (!m_license) - { - createLicense(user, licenseType, getExpireVersion(), numberOfUsers); - } + if (!m_license) + { + createLicense(user, licenseType, getExpireVersion(), numberOfUsers); + } - return m_license->getLicenseString(); + return m_license->getLicenseString(); } void Generator::printLicenseAndWriteItToFile() { - if (m_license) - { - m_license->print(); - m_license->writeToFile("license.txt"); - } - else - { - std::cout << "nothing to print" << std::endl; - } + if (m_license) + { + m_license->print(); + m_license->writeToFile("license.txt"); + } + else + { + std::cout << "nothing to print" << std::endl; + } } bool Generator::verifyLicense(const std::string& filename) { - License license; - license.loadFromFile(filename); + License license; + license.loadFromFile(filename); // license.loadPublicKeyFromString(PUBLIC_KEY); - return license.isValid(); + return license.isValid(); } void Generator::setCustomPrivateKeyFile(const std::string& file) { - if(!file.empty()) - { - m_privateKeyFile = file; - } + if(!file.empty()) + { + m_privateKeyFile = file; + } } void Generator::setCustomPublicKeyFile(const std::string& file) { - if(!file.empty()) - { - m_publicKeyFile = file; - } + if(!file.empty()) + { + m_publicKeyFile = file; + } } std::string Generator::getPublicKeyPEMFileAsString() { - return Botan::X509::PEM_encode(*m_privateKey); + return Botan::X509::PEM_encode(*m_privateKey); } std::string Generator::getPrivateKeyPEMFileAsString() @@ -156,12 +156,12 @@ std::string Generator::getPrivateKeyPEMFileAsString() return ""; } - return Botan::PKCS8::PEM_encode(*m_privateKey, m_rng, PRIVATE_KEY_PASSWORD); + return Botan::PKCS8::PEM_encode(*m_privateKey, m_rng, PRIVATE_KEY_PASSWORD); } void Generator::writeKeysToFiles() { - std::string publicKeyFilename = getPublicKeyFilename(); + std::string publicKeyFilename = getPublicKeyFilename(); if (publicKeyFilename.size() <= 0) { std::cout << "Failed to retrieve file name for public key" << std::endl; @@ -176,15 +176,15 @@ void Generator::writeKeysToFiles() } - std::cout << "public key filename: " << publicKeyFilename << std::endl; - std::ofstream pub(publicKeyFilename); - pub << getPublicKeyPEMFileAsString(); - std::cout << "public key created" << std::endl; + std::cout << "public key filename: " << publicKeyFilename << std::endl; + std::ofstream pub(publicKeyFilename); + pub << getPublicKeyPEMFileAsString(); + std::cout << "public key created" << std::endl; - std::cout << "private key filename: " << privateKeyFilename << std::endl; - std::ofstream priv(privateKeyFilename); - priv << getPrivateKeyPEMFileAsString(); - std::cout << "private key created" << std::endl; + std::cout << "private key filename: " << privateKeyFilename << std::endl; + std::ofstream priv(privateKeyFilename); + priv << getPrivateKeyPEMFileAsString(); + std::cout << "private key created" << std::endl; } bool Generator::loadPrivateKeyFromFile() @@ -194,14 +194,14 @@ bool Generator::loadPrivateKeyFromFile() return false; } - Botan::Private_Key* privateKey = Botan::PKCS8::load_key(getPrivateKeyFilename(), m_rng, PRIVATE_KEY_PASSWORD); - Botan::RSA_PrivateKey *rsaKey = dynamic_cast(privateKey); + Botan::Private_Key* privateKey = Botan::PKCS8::load_key(getPrivateKeyFilename(), m_rng, PRIVATE_KEY_PASSWORD); + Botan::RSA_PrivateKey *rsaKey = dynamic_cast(privateKey); if (!rsaKey) { - std::cout << "The key is not a RSA key" << std::endl; - return false; - } + std::cout << "The key is not a RSA key" << std::endl; + return false; + } m_privateKey = std::unique_ptr(rsaKey); @@ -216,67 +216,67 @@ bool Generator::loadPrivateKeyFromString(const std::string& key) return false; } - Botan::DataSource_Memory in(key); - Botan::Private_Key* privateKey= Botan::PKCS8::load_key(in, m_rng, PRIVATE_KEY_PASSWORD); - Botan::RSA_PrivateKey *rsaKey = dynamic_cast(privateKey); + Botan::DataSource_Memory in(key); + Botan::Private_Key* privateKey= Botan::PKCS8::load_key(in, m_rng, PRIVATE_KEY_PASSWORD); + Botan::RSA_PrivateKey *rsaKey = dynamic_cast(privateKey); if (!rsaKey) { - std::cout << "The key is not a RSA key" << std::endl; - return false; - } + std::cout << "The key is not a RSA key" << std::endl; + return false; + } m_privateKey = std::unique_ptr(rsaKey); - return (m_privateKey != NULL); + return (m_privateKey != NULL); } Botan::RSA_PrivateKey *Generator::getPrivateKey() const { - return m_privateKey.get(); + return m_privateKey.get(); } void Generator::createLicense( - const std::string& user, + const std::string& user, const std::string& type, - const std::string& expiration, - size_t numberOfUsers + const std::string& expiration, + size_t numberOfUsers ) { m_license = std::make_unique(); m_license->createHeader(user, type, expiration, numberOfUsers); - Botan::AutoSeeded_RNG rng; + Botan::AutoSeeded_RNG rng; std::string pass9 = Botan::generate_passhash9(m_license->getExpireLine(), m_rng); m_license->setHashLine(pass9); - //encode message - const std::string emsa = "EMSA4(SHA-256)"; - Botan::PK_Signer signer(*(m_privateKey.get()), rng, emsa); - Botan::DataSource_Memory in(m_license->getMessage()); - Botan::byte buffer[4096] = {0}; + //encode message + const std::string emsa = "EMSA4(SHA-256)"; + Botan::PK_Signer signer(*(m_privateKey.get()), rng, emsa); + Botan::DataSource_Memory in(m_license->getMessage()); + Botan::byte buffer[4096] = {0}; - while (size_t got = in.read(buffer, sizeof(buffer))) - { - signer.update(buffer, got); - } + while (size_t got = in.read(buffer, sizeof(buffer))) + { + signer.update(buffer, got); + } - const std::string signature = Botan::base64_encode(signer.signature(rng)); - m_license->setSignature(signature); + const std::string signature = Botan::base64_encode(signer.signature(rng)); + m_license->setSignature(signature); } int Generator::mapMonthToVersion(int month) { - return (month-1)/3+1; + return (month-1)/3+1; } std::string Generator::getExpireVersion(int versions) { - boost::gregorian::date today = boost::gregorian::day_clock::local_day(); - int monthNumber = today.month().as_number(); - Version version(today.year(), mapMonthToVersion(monthNumber)); - version += versions; - return version.toShortString(); + boost::gregorian::date today = boost::gregorian::day_clock::local_day(); + int monthNumber = today.month().as_number(); + Version version(today.year(), mapMonthToVersion(monthNumber)); + version += versions; + return version.toShortString(); } diff --git a/src/license_generator/Generator.h b/src/license_generator/Generator.h index 35b0fa4d..fe498a6e 100644 --- a/src/license_generator/Generator.h +++ b/src/license_generator/Generator.h @@ -15,53 +15,53 @@ class License; class Generator { public: - Generator(); - ~Generator(); + Generator(); + ~Generator(); - std::string encodeLicense( - const std::string& user, - const std::string& licenseType, - size_t numberOfUsers = 0, - const std::string& version = "" - ); - std::string encodeLicense( - const std::string& user, - const int days - ); - void printLicenseAndWriteItToFile(); - bool verifyLicense(const std::string& filename = "license.txt"); - void generateKeys(); - void writeKeysToFiles(); - void setCustomPrivateKeyFile(const std::string& file); - void setCustomPublicKeyFile(const std::string& file); + std::string encodeLicense( + const std::string& user, + const std::string& licenseType, + size_t numberOfUsers = 0, + const std::string& version = "" + ); + std::string encodeLicense( + const std::string& user, + const int days + ); + void printLicenseAndWriteItToFile(); + bool verifyLicense(const std::string& filename = "license.txt"); + void generateKeys(); + void writeKeysToFiles(); + void setCustomPrivateKeyFile(const std::string& file); + void setCustomPublicKeyFile(const std::string& file); - std::string getPublicKeyPEMFileAsString(); - std::string getPrivateKeyPEMFileAsString(); - bool loadPrivateKeyFromFile(); - bool loadPrivateKeyFromString(const std::string& key); + std::string getPublicKeyPEMFileAsString(); + std::string getPrivateKeyPEMFileAsString(); + bool loadPrivateKeyFromFile(); + bool loadPrivateKeyFromString(const std::string& key); - Botan::RSA_PrivateKey* getPrivateKey() const; - void createLicense( - const std::string& user, + Botan::RSA_PrivateKey* getPrivateKey() const; + void createLicense( + const std::string& user, const std::string& type, - const std::string& expiration, - size_t numberOfUsers - ); + const std::string& expiration, + size_t numberOfUsers + ); - std::string getExpireVersion(int versions = 4); - void setVersionLine(int year, int minorVersion); + std::string getExpireVersion(int versions = 4); + void setVersionLine(int year, int minorVersion); private: - int mapMonthToVersion(int month); - std::string getPrivateKeyFilename(); - std::string getPublicKeyFilename(); + int mapMonthToVersion(int month); + std::string getPrivateKeyFilename(); + std::string getPublicKeyFilename(); - //Botan - Botan::AutoSeeded_RNG m_rng; + //Botan + Botan::AutoSeeded_RNG m_rng; - std::string m_privateKeyFile; - std::string m_publicKeyFile; - std::unique_ptr m_license; - std::unique_ptr m_privateKey; + std::string m_privateKeyFile; + std::string m_publicKeyFile; + std::unique_ptr m_license; + std::unique_ptr m_privateKey; }; diff --git a/src/license_generator/main.cpp b/src/license_generator/main.cpp index 235cf734..6b9f5583 100644 --- a/src/license_generator/main.cpp +++ b/src/license_generator/main.cpp @@ -13,144 +13,144 @@ namespace po = boost::program_options; bool process_command_line(int argc, char** argv) { - try - { - std::string user; - std::string version; - std::string privateKeyFile; - std::string publicKeyFile; - std::string licenseFile = ""; - std::string type = ""; + try + { + std::string user; + std::string version; + std::string privateKeyFile; + std::string publicKeyFile; + std::string licenseFile = ""; + std::string type = ""; int days = 0; - int numberOfUsers = 0; + int numberOfUsers = 0; - po::options_description modes_description("Sourcetrail Generator Modes"); - modes_description.add_options() - ("key,k", "Generate the private and public key") - ("generate,g", po::value(&user), "Generate a License, USERNAME as value") - ("check,c", "Validate a License"); + po::options_description modes_description("Sourcetrail Generator Modes"); + modes_description.add_options() + ("key,k", "Generate the private and public key") + ("generate,g", po::value(&user), "Generate a License, USERNAME as value") + ("check,c", "Validate a License"); - po::options_description keygen_description("Options Keygeneration"); - keygen_description.add_options() - ("version,v", po::value(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid") - ("users,u", po::value(&numberOfUsers), "Number of users") - ("seats,s", po::value(&numberOfUsers), "Number of users (prev. seats)") - ("licenseType,t", po::value(&type), "License Type of ") - ("testLicense,e", po::value(&days), "Generates a test license for days"); + po::options_description keygen_description("Options Keygeneration"); + keygen_description.add_options() + ("version,v", po::value(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid") + ("users,u", po::value(&numberOfUsers), "Number of users") + ("seats,s", po::value(&numberOfUsers), "Number of users (prev. seats)") + ("licenseType,t", po::value(&type), "License Type of ") + ("testLicense,e", po::value(&days), "Generates a test license for days"); - po::options_description hidden_description("Hidden Options"); - hidden_description.add_options() - ("public-file", po::value(&publicKeyFile), "Custom public key file") - ("private-file", po::value(&privateKeyFile), "Custom private key file") - ("license-file", po::value(&licenseFile), "Custom license") - ("hidden", "Print this help message"); + po::options_description hidden_description("Hidden Options"); + hidden_description.add_options() + ("public-file", po::value(&publicKeyFile), "Custom public key file") + ("private-file", po::value(&privateKeyFile), "Custom private key file") + ("license-file", po::value(&licenseFile), "Custom license") + ("hidden", "Print this help message"); - po::options_description desc("Sourcetrail Generator"); - desc.add_options() - ("help,h", "Print this help message"); - desc.add(modes_description).add(keygen_description); + po::options_description desc("Sourcetrail Generator"); + desc.add_options() + ("help,h", "Print this help message"); + desc.add(modes_description).add(keygen_description); - po::options_description allDescriptions("Sourcetrail Generator"); - allDescriptions.add_options(); - allDescriptions.add(modes_description).add(keygen_description).add(hidden_description); + po::options_description allDescriptions("Sourcetrail Generator"); + allDescriptions.add_options(); + allDescriptions.add(modes_description).add(keygen_description).add(hidden_description); - po::variables_map vm; + po::variables_map vm; - po::store(po::parse_command_line(argc,argv,desc), vm); - po::notify(vm); + po::store(po::parse_command_line(argc,argv,desc), vm); + po::notify(vm); - // display help if no argument or the help argument is given - if (vm.count("help")) - { - std::cout << desc << std::endl; - return 1; - } + // display help if no argument or the help argument is given + if (vm.count("help")) + { + std::cout << desc << std::endl; + return 1; + } - // no mode chosen -> display help - if ( vm.size() == 0 || !(vm.count("hidden") || vm.count("key") || vm.count("check") || vm.count("generate"))) - { - std::cout << "*****************************\nNo mode chosen, display help: " - << "\n*****************************\n\n" << desc << std::endl; + // no mode chosen -> display help + if ( vm.size() == 0 || !(vm.count("hidden") || vm.count("key") || vm.count("check") || vm.count("generate"))) + { + std::cout << "*****************************\nNo mode chosen, display help: " + << "\n*****************************\n\n" << desc << std::endl; - return 1; - } + return 1; + } - if (vm.count("hidden")) - { - std::cout << allDescriptions << std::endl; - return 1; - } + if (vm.count("hidden")) + { + std::cout << allDescriptions << std::endl; + return 1; + } - Generator keygen; + Generator keygen; - // make sure there are no negative amount of users - if (vm.count("users") || vm.count("seats")) - { - if (numberOfUsers < 0) - { - std::cout << "Invalid amount of users. (Must be > 0)" << std::endl; - return false; - } - } + // make sure there are no negative amount of users + if (vm.count("users") || vm.count("seats")) + { + if (numberOfUsers < 0) + { + std::cout << "Invalid amount of users. (Must be > 0)" << std::endl; + return false; + } + } - if (vm.count("key")) - { - keygen.generateKeys(); - keygen.writeKeysToFiles(); - } + if (vm.count("key")) + { + keygen.generateKeys(); + keygen.writeKeysToFiles(); + } - if (vm.count("public-file")) - { - keygen.setCustomPublicKeyFile(publicKeyFile); - } + if (vm.count("public-file")) + { + keygen.setCustomPublicKeyFile(publicKeyFile); + } - if (vm.count("private-file")) - { - keygen.setCustomPrivateKeyFile(privateKeyFile); - } - else - { - keygen.loadPrivateKeyFromString(PRIVATE_KEY); - } + if (vm.count("private-file")) + { + keygen.setCustomPrivateKeyFile(privateKeyFile); + } + else + { + keygen.loadPrivateKeyFromString(PRIVATE_KEY); + } - if (vm.count("generate")) - { + if (vm.count("generate")) + { if (vm.count("testLicense")) { - keygen.encodeLicense(user, days); - keygen.printLicenseAndWriteItToFile(); + keygen.encodeLicense(user, days); + keygen.printLicenseAndWriteItToFile(); } else { - keygen.encodeLicense(user, type, numberOfUsers, version); - keygen.printLicenseAndWriteItToFile(); + keygen.encodeLicense(user, type, numberOfUsers, version); + keygen.printLicenseAndWriteItToFile(); } - } + } - if (vm.count("check")) - { - if (keygen.verifyLicense()) - { - std::cout << "License valid" << std::endl; - } - else - { - std::cout << "License not valid" << std::endl; - } - } - } - catch (std::exception& e) - { - std::cout << "Exception caught: " << e.what() << std::endl; - return false; - } + if (vm.count("check")) + { + if (keygen.verifyLicense()) + { + std::cout << "License valid" << std::endl; + } + else + { + std::cout << "License not valid" << std::endl; + } + } + } + catch (std::exception& e) + { + std::cout << "Exception caught: " << e.what() << std::endl; + return false; + } - return true; + return true; } int main(int argc, char *argv[]) { - process_command_line(argc, argv); + process_command_line(argc, argv); - return 0; + return 0; } diff --git a/src/test/GeneratorTestSuite.h b/src/test/GeneratorTestSuite.h index 89dbbe5c..f995b103 100644 --- a/src/test/GeneratorTestSuite.h +++ b/src/test/GeneratorTestSuite.h @@ -7,144 +7,144 @@ class GeneratorTestSuite : public CxxTest::TestSuite { public: - void test_create_Keys_with_Version_and_check() - { - Generator generator; - generator.generateKeys(); + void test_create_Keys_with_Version_and_check() + { + Generator generator; + generator.generateKeys(); - std::string privateKey = generator.getPrivateKeyPEMFileAsString(); - std::string publicKey = generator.getPublicKeyPEMFileAsString(); + std::string privateKey = generator.getPrivateKeyPEMFileAsString(); + std::string publicKey = generator.getPublicKeyPEMFileAsString(); // normally the size is 1886 // sometimes it could happen that the size is 1866 // to prevent the test of failing both sizes are valid TS_ASSERT(privateKey.size() == 1886 || privateKey.size() == 1866); - TS_ASSERT_EQUALS(publicKey.size(), 451); - } + TS_ASSERT_EQUALS(publicKey.size(), 451); + } - void test_load_private_Key_from_file() - { - Generator generator; - generator.setCustomPrivateKeyFile("./data/GeneratorTestSuite/private-v2.pem"); - bool ok = generator.loadPrivateKeyFromFile(); + void test_load_private_Key_from_file() + { + Generator generator; + generator.setCustomPrivateKeyFile("./data/GeneratorTestSuite/private-v2.pem"); + bool ok = generator.loadPrivateKeyFromFile(); - TS_ASSERT(ok); - } + TS_ASSERT(ok); + } - void test_load_private_key_from_string() - { - Generator generator; - bool ok = generator.loadPrivateKeyFromString(m_privateKey); + void test_load_private_key_from_string() + { + Generator generator; + bool ok = generator.loadPrivateKeyFromString(m_privateKey); - TS_ASSERT(ok); - } + TS_ASSERT(ok); + } void test_create_volume_license_and_validate() { - Generator generator; + Generator generator; generator.generateKeys(); generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); - License license; - license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20)); + License license; + license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20)); TS_ASSERT_EQUALS(license.getNumberOfUsers(), 20); - } + } - void test_create_test_license_and_validate() - { - Generator generator; - generator.generateKeys(); - generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); + void test_create_test_license_and_validate() + { + Generator generator; + generator.generateKeys(); + generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); - License license; + License license; license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); - license.loadFromString(generator.encodeLicense("User", 10)); + license.loadFromString(generator.encodeLicense("User", 10)); - TS_ASSERT(license.isValid()); - TS_ASSERT_EQUALS(license.getTimeLeft(), 10); + TS_ASSERT(license.isValid()); + TS_ASSERT_EQUALS(license.getTimeLeft(), 10); license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); - license.loadFromString(generator.encodeLicense("User", -10)); + license.loadFromString(generator.encodeLicense("User", -10)); - TS_ASSERT(!license.isValid()); - // -1 and not -10 since it means it is expired - TS_ASSERT_EQUALS(license.getTimeLeft(), -1); - } + TS_ASSERT(!license.isValid()); + // -1 and not -10 since it means it is expired + TS_ASSERT_EQUALS(license.getTimeLeft(), -1); + } - void test_create_licenses_and_check_the_license_info() - { - Generator generator; - generator.generateKeys(); + void test_create_licenses_and_check_the_license_info() + { + Generator generator; + generator.generateKeys(); - License license; + License license; - license.loadFromString(generator.encodeLicense("TestUser", "Private License", 1, "2017.4")); + license.loadFromString(generator.encodeLicense("TestUser", "Private License", 1, "2017.4")); std::string testInfo = "TestUser\nPrivate License\nValid up to version: 2017.4\n1 user"; - TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); + TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); - license.loadFromString(generator.encodeLicense("TestUser", "Volume License", 20, "2017.3")); + license.loadFromString(generator.encodeLicense("TestUser", "Volume License", 20, "2017.3")); testInfo = "TestUser\nVolume License\nValid up to version: 2017.3\n20 users"; - TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); + TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); - license.loadFromString(generator.encodeLicense("TestUser", "Private/Academic Single User License", 0, "2018.1")); + license.loadFromString(generator.encodeLicense("TestUser", "Private/Academic Single User License", 0, "2018.1")); testInfo = "TestUser\nPrivate/Academic Single User License\nValid up to version: 2018.1\nnot registered for commercial development"; - TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); - } + TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); + } - void test_create_license_and_validate() - { - Generator generator; - generator.generateKeys(); + void test_create_license_and_validate() + { + Generator generator; + generator.generateKeys(); - generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); - License license; - license.loadFromString(generator.encodeLicense("TestUser", "Private License", 20, "2017.3")); - license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); + generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); + License license; + license.loadFromString(generator.encodeLicense("TestUser", "Private License", 20, "2017.3")); + license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); TS_ASSERT_EQUALS(license.getUser(), "TestUser"); TS_ASSERT_EQUALS(license.getType(), "Private License"); - TS_ASSERT(license.isValid()); - } + TS_ASSERT(license.isValid()); + } private: - std::string m_privateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" - "MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQMWEDuADurEQ4T8kJO\n" - "AgMDDUACASAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEGNDHYI3xozW5JBg\n" - "pOLkhIMEggTQPwClVPQbk+AVqOAXndQqCs0Ad+eihbekCHt5zkRrYl6JbLq9fvfs\n" - "kGgn1j7xtFZflcZimlLaao8pLWybJfa3xGSJnlYrJsGVMlmKJw/ee3fZG7y4/Tgo\n" - "FytAIP51He4YAZ+4fOXyOoAT0+k2DBekZHeMADX7eXVpCLQXuoQpECvf2IPd/de4\n" - "QKS12FRTd7Hkq7DwY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OC7MsOt3N\n" - "8oChFJB1Gls5+RU6Ef27az+RE6uSumToT1HKgYDYX1TN68WWjT7Pa0lUMtZ4Bv1f\n" - "rXMH8igPXg62kzh/hjnEtl8y2+ySAs7RnZ0vUywaKcI0Tu5tqJSLtXSfJKAPmmzE\n" - "QYsg70kpufXccznTYvLSbM99FIbC6s+cSkJ43Ku4dwbEal+9FfqK639i2X9hdsLc\n" - "bYXIb0Fguo2IbWruBeSIc8lukOKCOVjanVZhfOCev8ye/Wz+SS8zbEixWZMzMCzC\n" - "if4dF/CUHnJK32IVlqKr9XGEe8ympqwMqN0eOdJn8IB+BQGCcaF6805D3R/OG4FU\n" - "hedhIvg1cODtoi8ri41Pz7o1x9Ia0zZr7oxhQuSgHiTfRfFN0xhCFZ+sIfGNpgUs\n" - "54IZGQlI0nQHOK8h2NI0wp3uo+fDEjjVIjQxINOSBa5awSReBpBUW2I=\n" - "-----END ENCRYPTED PRIVATE KEY-----"; - std::string m_publicKey = "-----BEGIN PUBLIC KEY-----\n" - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAugYah7LOU0ssdSnyDA8h\n" - "nOeW2lwE+NmZGxIKnqZKYNePY7Tg0c1pI0lfgJ2WYtxbubDFNDYk6bJF6mFg3jjN\n" - "gCdocj6pyyibIISbRst+gl/1FwI8vbIkfkJBoZtftO5mKVBbO5mmrxm32fDQs1vo\n" - "zgVxcWx3LXW87pzdQQYRACdkLSxPd+ADs+KNv6UxlOEvucDaenX3ckl3HdcWruL8\n" - "xdYoM7z/C+PRShSGvY3wB7Y6A5IcdBmzsTR6xayCmTzzW83dmFzjCX5DSOJLHxq7\n" - "+Fs26xZU83P1boX7eg4TjMViTxJwsCCW/2wfsZAoF0cSYCxhrkSdMHroH7Edz1PM\n" - "fwIDAQAB\n" - "-----END PUBLIC KEY-----\n"; + std::string m_privateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" + "MIIFNTBfBgkqhkiG9w0BBQ0wUjAxB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NXOhwDxQXGVrMXEjRzJKmqy2kpMFX/v38iKfqIx+pHMOs+/+ESwwp\n" + "frKZ2mUwa7DdDv6fZwPUPtZk/6HyqiDoJcoiN05IxdPt6SXGPsseQWcwTdiHhDX2\n" + "3YsTJK0O/CAf/ouqWQiZMAifTZtalTArTvTtlTE7TpAT3I0OmwNG/nI1v/orHBbZ\n" + "M7sa9mlBDMzE4rTHIYMOttNKLxkXRChjgO2JfFDLxscSMw8wsrbGTbKh7VGFgg7Q\n" + "Tj6KAGPiU7WbaXiIhO4adgYp3Bpbo15BhhO5bdaYzxBjOhYw1ShLEgBOC7MsOt3N\n" + "8oChFJB1Gls5+RU6Ef27az+RE6uSumToT1HKgYDYX1TN68WWjT7Pa0lUMtZ4Bv1f\n" + "rXMH8igPXg62kzh/hjnEtl8y2+ySAs7RnZ0vUywaKcI0Tu5tqJSLtXSfJKAPmmzE\n" + "QYsg70kpufXccznTYvLSbM99FIbC6s+cSkJ43Ku4dwbEal+9FfqK639i2X9hdsLc\n" + "bYXIb0Fguo2IbWruBeSIc8lukOKCOVjanVZhfOCev8ye/Wz+SS8zbEixWZMzMCzC\n" + "if4dF/CUHnJK32IVlqKr9XGEe8ympqwMqN0eOdJn8IB+BQGCcaF6805D3R/OG4FU\n" + "hedhIvg1cODtoi8ri41Pz7o1x9Ia0zZr7oxhQuSgHiTfRfFN0xhCFZ+sIfGNpgUs\n" + "54IZGQlI0nQHOK8h2NI0wp3uo+fDEjjVIjQxINOSBa5awSReBpBUW2I=\n" + "-----END ENCRYPTED PRIVATE KEY-----"; + std::string m_publicKey = "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAugYah7LOU0ssdSnyDA8h\n" + "nOeW2lwE+NmZGxIKnqZKYNePY7Tg0c1pI0lfgJ2WYtxbubDFNDYk6bJF6mFg3jjN\n" + "gCdocj6pyyibIISbRst+gl/1FwI8vbIkfkJBoZtftO5mKVBbO5mmrxm32fDQs1vo\n" + "zgVxcWx3LXW87pzdQQYRACdkLSxPd+ADs+KNv6UxlOEvucDaenX3ckl3HdcWruL8\n" + "xdYoM7z/C+PRShSGvY3wB7Y6A5IcdBmzsTR6xayCmTzzW83dmFzjCX5DSOJLHxq7\n" + "+Fs26xZU83P1boX7eg4TjMViTxJwsCCW/2wfsZAoF0cSYCxhrkSdMHroH7Edz1PM\n" + "fwIDAQAB\n" + "-----END PUBLIC KEY-----\n"; };