From 28183ed24fe1e85eaa9df0317d6233bb9d37e48f Mon Sep 17 00:00:00 2001 From: Gabriele Contini <contini.mailing@gmail.com> Date: 周日, 24 11月 2019 21:25:23 +0800 Subject: [PATCH] tests ok --- src/library/locate/EnvironmentVarData.hpp | 10 ++-- test/functional/date_test.cpp | 11 ++--- src/library/licensecc.cpp | 4 +- src/library/LicenseReader.cpp | 7 ++- src/library/locate/ApplicationFolder.hpp | 6 +- src/library/locate/EnvironmentVarData.cpp | 22 ++++------- src/library/locate/LocatorStrategy.hpp | 28 +++++-------- test/library/test_reader.ini | 4 +- src/library/locate/ApplicationFolder.cpp | 14 ++---- src/library/locate/EnvironmentVarLocation.hpp | 9 ++-- 10 files changed, 49 insertions(+), 66 deletions(-) diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp index 0bb9262..9aa53ab 100644 --- a/src/library/LicenseReader.cpp +++ b/src/library/LicenseReader.cpp @@ -54,6 +54,8 @@ } bool atLeastOneLicenseComplete = false; + const string product_up = toupper_copy(product); + const char *productNamePtr = product_up.c_str(); for (unique_ptr<locate::LocatorStrategy> &locator : locator_strategies) { vector<string> licenseLocations = locator->license_locations(eventRegistry); if (licenseLocations.size() == 0) { @@ -68,7 +70,6 @@ eventRegistry.addEvent(FILE_FORMAT_NOT_RECOGNIZED, *it); continue; } - const char *productNamePtr = product.c_str(); const int sectionSize = ini.GetSectionSize(productNamePtr); if (sectionSize <= 0) { eventRegistry.addEvent(PRODUCT_NOT_LICENSED, *it); @@ -85,8 +86,8 @@ * sig = XXXXXXXXXX (mandatory, 1024) * application_data = xxxxxxxxx (optional string 16) */ - const char *license_signature = ini.GetValue(productNamePtr, "sig", nullptr); - long license_version = ini.GetLongValue(productNamePtr, "lic_ver", -1); + const char *license_signature = ini.GetValue(productNamePtr, LICENSE_SIGNATURE, nullptr); + long license_version = ini.GetLongValue(productNamePtr, LICENSE_VERSION, -1); if (license_signature != nullptr && license_version == 200) { CSimpleIniA::TNamesDepend keys; ini.GetAllKeys(productNamePtr, keys); diff --git a/src/library/licensecc.cpp b/src/library/licensecc.cpp index fe1b3a7..5cc512b 100644 --- a/src/library/licensecc.cpp +++ b/src/library/licensecc.cpp @@ -24,13 +24,13 @@ static void mergeLicenses(const vector<LicenseInfo>& licenses, LicenseInfo* license_out) { if (license_out != nullptr) { - int days_left = -1; + int days_left = INT_MIN; for (auto it = licenses.begin(); it != licenses.end(); it++) { // choose the license that expires later... if (!it->has_expiry) { *license_out = *it; break; - } else if (days_left < it->days_left) { + } else if (days_left < (int)it->days_left) { *license_out = *it; days_left = it->days_left; } diff --git a/src/library/locate/ApplicationFolder.cpp b/src/library/locate/ApplicationFolder.cpp index e8535d6..215d477 100644 --- a/src/library/locate/ApplicationFolder.cpp +++ b/src/library/locate/ApplicationFolder.cpp @@ -22,17 +22,13 @@ namespace locate { using namespace std; -ApplicationFolder::ApplicationFolder() : - LocatorStrategy("ApplicationFolder") { -} +ApplicationFolder::ApplicationFolder() : LocatorStrategy("ApplicationFolder") {} -ApplicationFolder::~ApplicationFolder() { -} +ApplicationFolder::~ApplicationFolder() {} -const vector<string> ApplicationFolder::license_locations( - EventRegistry &eventRegistry) { +const vector<string> ApplicationFolder::license_locations(EventRegistry &eventRegistry) { vector<string> diskFiles; - char fname[MAX_PATH] = { 0 }; + char fname[MAX_PATH] = {0}; const FUNCTION_RETURN fret = getModuleName(fname); if (fret == FUNC_RET_OK) { const string module_name = remove_extension(fname); @@ -51,5 +47,5 @@ return diskFiles; } -} +} // namespace locate } /* namespace license */ diff --git a/src/library/locate/ApplicationFolder.hpp b/src/library/locate/ApplicationFolder.hpp index 649d8da..3e2163d 100644 --- a/src/library/locate/ApplicationFolder.hpp +++ b/src/library/locate/ApplicationFolder.hpp @@ -15,14 +15,14 @@ namespace license { namespace locate { -class ApplicationFolder: public LocatorStrategy { +class ApplicationFolder : public LocatorStrategy { public: ApplicationFolder(); - virtual const std::vector<std::string> license_locations(EventRegistry& eventRegistry); + const virtual std::vector<std::string> license_locations(EventRegistry& eventRegistry); virtual ~ApplicationFolder(); }; -} +} // namespace locate } /* namespace license */ #endif /* SRC_LIBRARY_RETRIEVERS_APPLICATIONFOLDER_H_ */ diff --git a/src/library/locate/EnvironmentVarData.cpp b/src/library/locate/EnvironmentVarData.cpp index 486175b..6e47d21 100644 --- a/src/library/locate/EnvironmentVarData.cpp +++ b/src/library/locate/EnvironmentVarData.cpp @@ -23,15 +23,11 @@ using namespace std; -EnvironmentVarData::EnvironmentVarData() : - LocatorStrategy("EnvironmentVarData") { -} +EnvironmentVarData::EnvironmentVarData() : LocatorStrategy("EnvironmentVarData") {} -EnvironmentVarData::~EnvironmentVarData() { -} +EnvironmentVarData::~EnvironmentVarData() {} -const vector<string> EnvironmentVarData::license_locations( - EventRegistry &eventRegistry) { +const vector<string> EnvironmentVarData::license_locations(EventRegistry &eventRegistry) { vector<string> diskFiles; char *env_var_value = getenv(LICENSE_DATA_ENV_VAR); if (env_var_value != nullptr && env_var_value[0] != '\0') { @@ -44,24 +40,22 @@ isBase64 = (licenseFormat == BASE64); } } else { - eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, - LICENSE_LOCATION_ENV_VAR); + eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, LICENSE_LOCATION_ENV_VAR); } return diskFiles; } -const std::string EnvironmentVarData::retrieve_license_content( - const std::string &licenseLocation) const { +const std::string EnvironmentVarData::retrieve_license_content(const std::string &licenseLocation) const { string tmpVal = getenv(LICENSE_LOCATION_ENV_VAR); if (isBase64) { int flen = 0; unsigned char *raw = unbase64(tmpVal.c_str(), tmpVal.length(), &flen); - string str = string(reinterpret_cast<char*>(raw)); + string str = string(reinterpret_cast<char *>(raw)); free(raw); return str; } return tmpVal; } -} -} +} // namespace locate +} // namespace license diff --git a/src/library/locate/EnvironmentVarData.hpp b/src/library/locate/EnvironmentVarData.hpp index 5698975..2709c9c 100644 --- a/src/library/locate/EnvironmentVarData.hpp +++ b/src/library/locate/EnvironmentVarData.hpp @@ -12,18 +12,18 @@ namespace license { namespace locate { -class EnvironmentVarData: public LocatorStrategy { +class EnvironmentVarData : public LocatorStrategy { private: bool isBase64 = false; public: EnvironmentVarData(); - virtual const std::vector<std::string> license_locations(EventRegistry& eventRegistr); - virtual const std::string retrieve_license_content(const std::string &licenseLocation) const; + const virtual std::vector<std::string> license_locations(EventRegistry& eventRegistr); + const virtual std::string retrieve_license_content(const std::string& licenseLocation) const; virtual ~EnvironmentVarData(); }; -} -} +} // namespace locate +} // namespace license #endif diff --git a/src/library/locate/EnvironmentVarLocation.hpp b/src/library/locate/EnvironmentVarLocation.hpp index b2f9b6a..636b827 100644 --- a/src/library/locate/EnvironmentVarLocation.hpp +++ b/src/library/locate/EnvironmentVarLocation.hpp @@ -13,15 +13,14 @@ namespace license { namespace locate { -class EnvironmentVarLocation: public LocatorStrategy { - +class EnvironmentVarLocation : public LocatorStrategy { public: EnvironmentVarLocation(); - virtual const std::vector<std::string> license_locations(EventRegistry& eventRegistry); + const virtual std::vector<std::string> license_locations(EventRegistry& eventRegistry); virtual ~EnvironmentVarLocation(); }; -} -} +} // namespace locate +} // namespace license #endif /* SRC_LIBRARY_LOCATE_ENVIRONMENTVARLOCATION_H_ */ diff --git a/src/library/locate/LocatorStrategy.hpp b/src/library/locate/LocatorStrategy.hpp index 278c6a3..2cfc1e5 100644 --- a/src/library/locate/LocatorStrategy.hpp +++ b/src/library/locate/LocatorStrategy.hpp @@ -16,29 +16,25 @@ * * Usage: * <ol> - * <li> call licenseLocations to get a list of available locations (the returned format is defined by the class, it's usually the file name)</li> - * <li> iterate over the returned vector and call retrieveLicense to get the content of the license</li> + * <li> call licenseLocations to get a list of available locations (the returned format is defined by the class, it's + * usually the file name)</li> <li> iterate over the returned vector and call retrieveLicense to get the content of the + * license</li> * </ol> */ class LocatorStrategy { protected: const std::string m_strategy_name; - inline LocatorStrategy(const std::string &strategyName) : - m_strategy_name(strategyName) { - } -public: + inline LocatorStrategy(const std::string &strategyName) : m_strategy_name(strategyName) {} - virtual const std::string get_strategy_name() const { - return m_strategy_name; - } +public: + const virtual std::string get_strategy_name() const { return m_strategy_name; } /** * Try to find licenses * @param eventRegistry * @return * A list of identifiers for call retrieve_license_content. */ - virtual const std::vector<std::string> license_locations( - EventRegistry &eventRegistry) = 0; + const virtual std::vector<std::string> license_locations(EventRegistry &eventRegistry) = 0; /** * Default implementation is to retrieve the license from file. @@ -49,12 +45,10 @@ * @return * a string containing the license data in INI format. */ - virtual const std::string retrieve_license_content( - const std::string &licenseLocationId) const; - inline virtual ~LocatorStrategy() { - } + const virtual std::string retrieve_license_content(const std::string &licenseLocationId) const; + inline virtual ~LocatorStrategy() {} }; -} -} +} // namespace locate +} // namespace license #endif diff --git a/test/functional/date_test.cpp b/test/functional/date_test.cpp index 2c9c43d..15fbdd0 100644 --- a/test/functional/date_test.cpp +++ b/test/functional/date_test.cpp @@ -1,4 +1,4 @@ -#define BOOST_TEST_MODULE date_test +#define BOOST_TEST_MODULE test_date #include <boost/test/unit_test.hpp> #include <boost/filesystem.hpp> @@ -18,11 +18,10 @@ namespace test { BOOST_AUTO_TEST_CASE(license_not_expired) { - const string licLocation(PROJECT_TEST_TEMP_DIR "/not_expired.lic"); vector<string> extraArgs; extraArgs.push_back("-e"); extraArgs.push_back("2050-10-10"); - generate_license(licLocation, extraArgs); + const string licLocation = generate_license("not_expired.lic", extraArgs); /* */ LicenseInfo license; LicenseLocation licenseLocation; @@ -32,15 +31,14 @@ BOOST_CHECK_EQUAL(result, LICENSE_OK); BOOST_CHECK_EQUAL(license.has_expiry, true); BOOST_CHECK_EQUAL(license.linked_to_pc, false); + BOOST_CHECK_GT(license.days_left, 0); } BOOST_AUTO_TEST_CASE(license_expired) { - const string licLocation(PROJECT_TEST_TEMP_DIR "/expired.lic"); - remove(licLocation.c_str()); vector<string> extraArgs; extraArgs.push_back("-e"); extraArgs.push_back("2013-10-10"); - generate_license(licLocation, extraArgs); + const string licLocation = generate_license("expired", extraArgs); /* */ LicenseInfo license; LicenseLocation licenseLocation; @@ -51,6 +49,7 @@ BOOST_CHECK_EQUAL(result, PRODUCT_EXPIRED); BOOST_CHECK_EQUAL(license.has_expiry, true); BOOST_CHECK_EQUAL(license.linked_to_pc, false); + BOOST_CHECK_EQUAL(license.days_left, 0); } } // namespace test diff --git a/test/library/test_reader.ini b/test/library/test_reader.ini index 6700638..a3ed87e 100644 --- a/test/library/test_reader.ini +++ b/test/library/test_reader.ini @@ -1,3 +1,3 @@ [PRODUCT] -license_version = 100 -license_signature = qAz \ No newline at end of file +lic_ver = 200 +sig = qAz -- Gitblit v1.9.1