From 3ba1ee60337c5e74027acb6f89a16e5ff3aa345c Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: 周一, 14 4月 2014 15:47:14 +0800 Subject: [PATCH] test with date --- src/library/CMakeLists.txt | 5 src/library/ini/CMakeLists.txt | 9 + src/library/api/license++.h | 2 src/library/ini/ConvertUTF.c | 0 test/library/LicenseReader_test.cpp | 2 src/library/ini/SimpleIni.h | 0 src/library/license++.cpp | 8 src/library/ini/ConvertUTF.h | 0 src/library/LicenseReader.h | 12 + test/library/CMakeLists.txt | 16 +++ /dev/null | 15 --- .cproject | 2 src/library/base/StringUtils.h | 1 test/functional/CMakeLists.txt | 24 ++++ src/library/LicenseReader.cpp | 54 ++++++---- src/license-generator/license-generator.cpp | 31 +----- test/functional/standard-license_test.cpp | 26 +---- src/license-generator/license-generator.h | 3 test/license-generator/license-generator_test.cpp | 2 test/library/test_reader.ini | 0 src/license-generator/LicenseSigner.h | 2 src/library/base/StringUtils.cpp | 33 ++++++ 22 files changed, 141 insertions(+), 106 deletions(-) diff --git a/.cproject b/.cproject index 3b0d4ad..f18522c 100644 --- a/.cproject +++ b/.cproject @@ -26,7 +26,7 @@ </builder> <tool id="cdt.managedbuild.tool.gnu.archiver.base.1063978701" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/> <tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.1465609732" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base"> - <option id="gnu.cpp.compiler.option.preprocessor.def.1664583179" name="Defined symbols (-D)" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols"> + <option id="gnu.cpp.compiler.option.preprocessor.def.1664583179" name="Defined symbols (-D)" superClass="gnu.cpp.compiler.option.preprocessor.def" useByScannerDiscovery="false" valueType="definedSymbols"> <listOptionValue builtIn="false" value="__GXX_EXPERIMENTAL_CXX0X__"/> </option> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.18539696" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 57642dd..4322104 100644 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -4,15 +4,16 @@ add_subdirectory("os/linux") ENDIF(WIN32) add_subdirectory("base") -add_subdirectory("reader") +add_subdirectory("ini") ADD_LIBRARY(license++_static STATIC license++.cpp + LicenseReader.cpp ) target_link_libraries( license++_static - reader + ini os ) diff --git a/src/library/reader/LicenseReader.cpp b/src/library/LicenseReader.cpp similarity index 91% rename from src/library/reader/LicenseReader.cpp rename to src/library/LicenseReader.cpp index 458fb75..7ad0ede 100644 --- a/src/library/reader/LicenseReader.cpp +++ b/src/library/LicenseReader.cpp @@ -5,12 +5,11 @@ * Author: devel */ -#include "LicenseReader.h" -#include "../base/StringUtils.h" -#include "../base/public-key.h" -#include <build_properties.h> -#define SI_SUPPORT_IOSTREAMS -#include "SimpleIni.h" +#ifdef _WIN32 +# pragma warning(disable: 4786) +#else +# include <unistd.h> +#endif #include <cstring> #include <ctime> #include <vector> @@ -19,22 +18,18 @@ #include <fstream> #include <sstream> #include <stdlib.h> +#include "LicenseReader.h" +#include "base/StringUtils.h" +#include "base/public-key.h" +#include <build_properties.h> -#ifdef _WIN32 -# pragma warning(disable: 4786) -#else -# include <unistd.h> -#endif - -#define SI_SUPPORT_IOSTREAMS -#include "SimpleIni.h" -#include "../os/os.hpp" +#include "os/os.hpp" namespace license { FullLicenseInfo::FullLicenseInfo(const string& source, const string& product, - const string& license_signature, int licenseVersion, time_t from_date, - time_t to_date, // + const string& license_signature, int licenseVersion, // + time_t from_date, time_t to_date, // const string& client_signature, unsigned int from_sw_version, unsigned int to_sw_version, const string& extra_data) : source(source), product(product), // @@ -59,12 +54,12 @@ } else { er.addEvent(LICENSE_CORRUPTED, SEVERITY_ERROR); } - if(has_expiry){ + if (has_expiry) { time_t now = time(NULL); - if(this->to_date<now){ + if (this->to_date < now) { er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR, ""); } - if(this->from_date>now){ + if (this->from_date > now) { er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR); } } @@ -92,6 +87,16 @@ LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) : licenseLocation(licenseLocation) { +} + +time_t LicenseReader::read_date(const char * productName, const char * ini_key, + const CSimpleIniA& ini) const { + string from_date_str = ini.GetValue(productName, ini_key); + time_t from_date = FullLicenseInfo::UNUSED_TIME; + if (from_date_str.length() > 0) { + from_date = seconds_from_epoch(from_date_str.c_str()); + } + return from_date; } EventRegistry LicenseReader::readLicenses(const string &product, @@ -136,8 +141,10 @@ long license_version = ini.GetLongValue(productNamePtr, "license_version", -1); if (license_signature != NULL && license_version > 0) { + time_t from_date = read_date(productNamePtr, "from_date", ini); + time_t to_date = read_date(productNamePtr, "to_date", ini); FullLicenseInfo licInfo(*it, product, license_signature, - (int) license_version); + (int) license_version, from_date, to_date); licenseInfoOut.push_back(licInfo); atLeastOneLicenseComplete = true; } else { @@ -321,9 +328,10 @@ strftime(buff, 20, "%Y-%m-%d", localtime(&this->from_date)); ini.SetValue(product.c_str(), "from_date", buff); } + char buff2[20]; if (this->to_date != UNUSED_TIME) { - strftime(buff, 20, "%Y-%m-%d", localtime(&this->to_date)); - ini.SetValue(product.c_str(), "to_date", buff); + strftime(buff2, 20, "%Y-%m-%d", localtime(&this->to_date)); + ini.SetValue(product.c_str(), "to_date", buff2); } if (this->extra_data.length() > 0) { ini.SetValue(product.c_str(), "extra_data", this->extra_data.c_str()); diff --git a/src/library/reader/LicenseReader.h b/src/library/LicenseReader.h similarity index 89% rename from src/library/reader/LicenseReader.h rename to src/library/LicenseReader.h index 9ab92ca..bcc6022 100644 --- a/src/library/reader/LicenseReader.h +++ b/src/library/LicenseReader.h @@ -8,9 +8,11 @@ #ifndef LICENSEREADER_H_ #define LICENSEREADER_H_ -#include "../api/datatypes.h" -#include "../base/EventRegistry.h" -#include "../os/os.hpp" +#include "api/datatypes.h" +#include "base/EventRegistry.h" +#include "os/os.hpp" +#define SI_SUPPORT_IOSTREAMS +#include "ini/SimpleIni.h" #include <string> #include <ctime> namespace license { @@ -39,7 +41,7 @@ FullLicenseInfo(const string& source, const string& product, const string& license_signature, int licenseVersion, time_t from_date = UNUSED_TIME, - time_t tp_date = UNUSED_TIME, // + time_t to_date = UNUSED_TIME, // const string& client_signature = "", // unsigned int from_sw_version = UNUSED_SOFTWARE_VERSION, unsigned int to_sw_version = UNUSED_SOFTWARE_VERSION, @@ -70,6 +72,8 @@ */ class LicenseReader { private: + time_t read_date(const char * productName, const char * ini_key, + const CSimpleIniA& ini) const; const LicenseLocation licenseLocation; EventRegistry getLicenseDiskFiles(vector<string>& diskFiles); vector<string> filterExistingFiles(vector<string> licensePositions); diff --git a/src/library/api/license++.h b/src/library/api/license++.h index 8ec099a..713692a 100644 --- a/src/library/api/license++.h +++ b/src/library/api/license++.h @@ -43,7 +43,7 @@ * @param license[out] optional, can be NULL. */ -DllExport EVENT_TYPE acquire_license(char * productName, +DllExport EVENT_TYPE acquire_license(const char * productName, LicenseLocation licenseLocation, LicenseInfo* license); /** diff --git a/src/library/base/StringUtils.cpp b/src/library/base/StringUtils.cpp index 7cd274b..14b9bc6 100644 --- a/src/library/base/StringUtils.cpp +++ b/src/library/base/StringUtils.cpp @@ -9,6 +9,7 @@ #include "StringUtils.h" #include <iostream> #include <string> +#include <cstring> #include <algorithm> namespace license { @@ -28,8 +29,38 @@ string toupper_copy(const string& lowercase) { string cp(lowercase); - std::transform(cp.begin(), cp.end(), cp.begin(), (int(*)(int))toupper); + std::transform(cp.begin(), cp.end(), cp.begin(), (int (*)(int))toupper); return cp; } +time_t seconds_from_epoch(const char* timeString) { + int year, month, day; + tm tm; + if (strlen(timeString) == 8) { + int nfield = sscanf(timeString, "%4d%2d%2d", &year, &month, &day); + if (nfield != 3) { + throw invalid_argument("Date not recognized"); + } + } else if (strlen(timeString) == 10) { + int nfield = sscanf(timeString, "%4d-%2d-%2d", &year, &month, &day); + if (nfield != 3) { + int nfield = sscanf(timeString, "%4d/%2d/%2d", &year, &month, &day); + if (nfield != 3) { + throw invalid_argument("Date not recognized"); + } + } + } else{ + throw invalid_argument("Date not recognized"); + } + tm.tm_isdst = -1; + tm.tm_year = year - 1900; + tm.tm_mon = month - 1; + tm.tm_mday = day; + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + tm.tm_yday = -1; + tm.tm_wday = -1; + return mktime(&tm); +} } /* namespace license */ diff --git a/src/library/base/StringUtils.h b/src/library/base/StringUtils.h index 9bfe3f8..a7d3cbe 100644 --- a/src/library/base/StringUtils.h +++ b/src/library/base/StringUtils.h @@ -22,6 +22,7 @@ string toupper_copy(const string& lowercase); +time_t seconds_from_epoch(const char* s); } /* namespace license */ #endif /* STRINGUTILS_H_ */ diff --git a/src/library/ini/CMakeLists.txt b/src/library/ini/CMakeLists.txt new file mode 100644 index 0000000..b08ac95 --- /dev/null +++ b/src/library/ini/CMakeLists.txt @@ -0,0 +1,9 @@ + +ADD_LIBRARY(ini STATIC + ConvertUTF.c +) + +target_link_libraries( + ini +) + diff --git a/src/library/reader/ConvertUTF.c b/src/library/ini/ConvertUTF.c similarity index 100% rename from src/library/reader/ConvertUTF.c rename to src/library/ini/ConvertUTF.c diff --git a/src/library/reader/ConvertUTF.h b/src/library/ini/ConvertUTF.h similarity index 100% rename from src/library/reader/ConvertUTF.h rename to src/library/ini/ConvertUTF.h diff --git a/src/library/reader/SimpleIni.h b/src/library/ini/SimpleIni.h similarity index 100% rename from src/library/reader/SimpleIni.h rename to src/library/ini/SimpleIni.h diff --git a/src/library/license++.cpp b/src/library/license++.cpp index f0b20d0..03f2f2c 100644 --- a/src/library/license++.cpp +++ b/src/library/license++.cpp @@ -10,7 +10,7 @@ #include <stdlib.h> #include <cstring> #include "api/license++.h" -#include "reader/LicenseReader.h" +#include "LicenseReader.h" using namespace std; DllExport void print_error(char out_buffer[256], LicenseInfo* licenseInfo) { @@ -39,8 +39,8 @@ } } -EVENT_TYPE acquire_license(char * product, LicenseLocation licenseLocation, - LicenseInfo* license) { +EVENT_TYPE acquire_license(const char * product, + LicenseLocation licenseLocation, LicenseInfo* license) { license::LicenseReader lr = license::LicenseReader(licenseLocation); vector<license::FullLicenseInfo> licenses; license::EventRegistry er = lr.readLicenses(string(product), licenses); @@ -50,7 +50,7 @@ vector<license::FullLicenseInfo> licenses_ok; for (auto it = licenses.begin(); it != licenses.end(); it++) { license::EventRegistry validation_er = it->validate(0); - if (er.isGood()) { + if (validation_er.isGood()) { licenses_ok.push_back(*it); } else { licenses_with_errors.push_back(*it); diff --git a/src/library/reader/CMakeLists.txt b/src/library/reader/CMakeLists.txt deleted file mode 100644 index d637498..0000000 --- a/src/library/reader/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ - -ADD_LIBRARY(reader STATIC - ConvertUTF.c - LicenseReader.cpp -) - -target_link_libraries( - reader - os - base - ${EXTERNAL_LIBS} -) - diff --git a/src/license-generator/LicenseSigner.h b/src/license-generator/LicenseSigner.h index 70d0764..899960e 100644 --- a/src/license-generator/LicenseSigner.h +++ b/src/license-generator/LicenseSigner.h @@ -8,7 +8,7 @@ #ifndef LICENSESIGNER_H_ #define LICENSESIGNER_H_ -#include "../library/reader/LicenseReader.h" +#include "../library/LicenseReader.h" namespace license { diff --git a/src/license-generator/license-generator.cpp b/src/license-generator/license-generator.cpp index a967e6f..f6e3e80 100644 --- a/src/license-generator/license-generator.cpp +++ b/src/license-generator/license-generator.cpp @@ -1,6 +1,7 @@ #include <build_properties.h> #include "LicenseSigner.h" #include "license-generator.h" +#include "../library/base/StringUtils.h" #include <stdlib.h> #include <stdio.h> #include <iostream> @@ -75,7 +76,7 @@ if (vm.count("end_date")) { const std::string dt_end = vm["end_date"].as<string>(); try { - end_date = seconds_from_epoch(dt_end); + end_date = seconds_from_epoch(dt_end.c_str()); begin_date = time(NULL); } catch (invalid_argument &e) { cerr << endl << "End date not recognized: " << dt_end @@ -86,7 +87,7 @@ if (vm.count("begin_date")) { const std::string begin_date_str = vm["begin_date"].as<string>(); try { - begin_date = seconds_from_epoch(begin_date_str); + begin_date = seconds_from_epoch(begin_date_str.c_str()); } catch (invalid_argument &e) { cerr << endl << "Begin date not recognized: " << begin_date_str << " Please enter a valid date in format YYYYMMDD" << endl; @@ -110,9 +111,9 @@ if (vm.count("extra_data")) { extra_data = vm["extra_data"].as<string>(); } - unsigned int from_sw_version = end_date = vm["start_version"].as< + unsigned int from_sw_version = vm["start_version"].as< unsigned int>(); - unsigned int to_sw_version = end_date = + unsigned int to_sw_version = vm["end_version"].as<unsigned int>(); if (vm.count("product") == 0) { cerr << endl << "Parameter [product] not found. " << endl; @@ -183,28 +184,6 @@ return 0; } -const std::locale formats[] = { std::locale(std::locale::classic(), - new bt::time_input_facet("%Y-%m-%d")), // -std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%m/%d")), // -std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d")) }; -const size_t formats_n = sizeof(formats) / sizeof(formats[0]); -time_t LicenseGenerator::seconds_from_epoch(const std::string& s) { - bt::ptime pt; - for (size_t i = 0; i < formats_n; ++i) { - std::istringstream is(s); - is.imbue(formats[i]); - is >> pt; - if (pt != bt::ptime()) { - break; - } - } - if (pt == bt::ptime()) { - throw invalid_argument(string("Date not regognized") + s); - } - bt::ptime timet_start(boost::gregorian::date(1970, 1, 1)); - bt::time_duration diff = pt - timet_start; - return diff.ticks() / bt::time_duration::rep_type::ticks_per_second; -} } diff --git a/src/license-generator/license-generator.h b/src/license-generator/license-generator.h index 69e7930..4bc5f11 100644 --- a/src/license-generator/license-generator.h +++ b/src/license-generator/license-generator.h @@ -9,7 +9,7 @@ #define LICENSE_GENERATOR_H_ #include <boost/program_options.hpp> -#include "../library/reader/LicenseReader.h" +#include "../library/LicenseReader.h" namespace license { @@ -23,7 +23,6 @@ LicenseGenerator(); static void printHelp(const char* prog_name, const po::options_description& options); static po::options_description configureProgramOptions(); - static time_t seconds_from_epoch(const std::string& s); static vector<FullLicenseInfo> parseLicenseInfo(po::variables_map vm); static void generateAndOutptuLicenses(const po::variables_map& vm, ostream& outputFile); diff --git a/test/functional/CMakeLists.txt b/test/functional/CMakeLists.txt index 4e3773d..a343dc6 100644 --- a/test/functional/CMakeLists.txt +++ b/test/functional/CMakeLists.txt @@ -1,14 +1,32 @@ +ADD_LIBRARY(license_generator_snippet STATIC + generate-license.cpp +) +target_link_libraries( + license_generator_snippet + license_generator_lib +) add_executable( standard_license_test standard-license_test.cpp ) - target_link_libraries( standard_license_test license++_static - license_generator_lib + license_generator_snippet ) - SET_TARGET_PROPERTIES(standard_license_test PROPERTIES LINK_SEARCH_START_STATIC ON) + +add_executable( + date_test + date_test.cpp +) +target_link_libraries( + date_test + license++_static + license_generator_snippet +) +SET_TARGET_PROPERTIES(date_test PROPERTIES LINK_SEARCH_START_STATIC ON) + ADD_TEST(standard_license_test ${EXECUTABLE_OUTPUT_PATH}/standard_license_test) +ADD_TEST(date_test ${EXECUTABLE_OUTPUT_PATH}/date_test) \ No newline at end of file diff --git a/test/functional/standard-license_test.cpp b/test/functional/standard-license_test.cpp index dc24a98..4e332cd 100644 --- a/test/functional/standard-license_test.cpp +++ b/test/functional/standard-license_test.cpp @@ -6,34 +6,18 @@ #include "../../../src/library/api/license++.h" #include <build_properties.h> #include <boost/filesystem.hpp> -#include "../../src/library/reader/SimpleIni.h" - +#include "../../src/library/ini/SimpleIni.h" +#include "generate-license.h" namespace fs = boost::filesystem; using namespace license; using namespace std; -void generate_license(const string& prod_name, const string& fname) { - int argc = 4; - const char** argv = new const char*[argc + 1]; - argv[0] = "lic-generator"; - argv[1] = "-o"; - argv[2] = fname.c_str(); - argv[3] = "test"; - int retCode = LicenseGenerator::generateLicense(argc, argv); - delete (argv); - BOOST_CHECK_EQUAL(retCode, 0); - BOOST_ASSERT(fs::exists(fname)); - CSimpleIniA ini; - SI_Error rc = ini.LoadFile(fname.c_str()); - BOOST_CHECK_GE(rc,0); - int sectionSize = ini.GetSectionSize(prod_name.c_str()); - BOOST_CHECK_GT(sectionSize,0); -} BOOST_AUTO_TEST_CASE( standard_lic_file ) { const string licLocation(PROJECT_TEST_TEMP_DIR "/standard_license.lic"); - generate_license(string("TEST"), licLocation); + vector<string> extraArgs; + generate_license(licLocation, extraArgs); /* */ LicenseInfo license; LicenseLocation licenseLocation; @@ -47,3 +31,5 @@ BOOST_CHECK_EQUAL(license.linked_to_pc, false); } + + diff --git a/test/library/CMakeLists.txt b/test/library/CMakeLists.txt index 6a1f0d0..c8c638c 100644 --- a/test/library/CMakeLists.txt +++ b/test/library/CMakeLists.txt @@ -1 +1,15 @@ -add_subdirectory(reader) \ No newline at end of file +link_directories ( ${Boost_LIBRARY_DIRS} ) + +add_executable( + license_reader_test + LicenseReader_test.cpp +) + +target_link_libraries( + license_reader_test + license++_static + ${Boost_LIBRARIES} +) + +SET_TARGET_PROPERTIES(license_reader_test PROPERTIES LINK_SEARCH_START_STATIC ON) +ADD_TEST(license_reader_test ${EXECUTABLE_OUTPUT_PATH}/LicenseReader_test) diff --git a/test/library/reader/LicenseReader_test.cpp b/test/library/LicenseReader_test.cpp similarity index 97% rename from test/library/reader/LicenseReader_test.cpp rename to test/library/LicenseReader_test.cpp index 1e7501a..f4dcfca 100644 --- a/test/library/reader/LicenseReader_test.cpp +++ b/test/library/LicenseReader_test.cpp @@ -3,7 +3,7 @@ //#define BOOST_TEST_MAIN #define BOOST_TEST_DYN_LINK #include <boost/test/unit_test.hpp> -#include "../../../src/library/reader/LicenseReader.h" +#include "../../src/library/LicenseReader.h" #include <build_properties.h> using namespace license; diff --git a/test/library/reader/CMakeLists.txt b/test/library/reader/CMakeLists.txt deleted file mode 100644 index 5c0f908..0000000 --- a/test/library/reader/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -link_directories ( ${Boost_LIBRARY_DIRS} ) - -add_executable( - license_reader_test - LicenseReader_test.cpp -) - -target_link_libraries( - license_reader_test - reader - ${Boost_LIBRARIES} -) - -SET_TARGET_PROPERTIES(license_reader_test PROPERTIES LINK_SEARCH_START_STATIC ON) -ADD_TEST(license_reader_test ${EXECUTABLE_OUTPUT_PATH}/LicenseReader_test) diff --git a/test/library/reader/test_reader.ini b/test/library/test_reader.ini similarity index 100% rename from test/library/reader/test_reader.ini rename to test/library/test_reader.ini diff --git a/test/license-generator/license-generator_test.cpp b/test/license-generator/license-generator_test.cpp index ec10ef7..cdfb3ea 100644 --- a/test/license-generator/license-generator_test.cpp +++ b/test/license-generator/license-generator_test.cpp @@ -5,7 +5,7 @@ #include "../../../src/license-generator/license-generator.h" #include <build_properties.h> #include <boost/filesystem.hpp> -#include "../../src/library/reader/SimpleIni.h" +#include "../../src/library/ini/SimpleIni.h" namespace fs = boost::filesystem; using namespace license; -- Gitblit v1.9.1