open-license-manager
2014-04-14 3ba1ee60337c5e74027acb6f89a16e5ff3aa345c
test with date
13个文件已修改
1个文件已添加
7 文件已重命名
2个文件已删除
252 ■■■■ 已修改文件
.cproject 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/CMakeLists.txt 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.cpp 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.h 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/api/license++.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/StringUtils.cpp 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/StringUtils.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/ini/CMakeLists.txt 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/ini/ConvertUTF.c 补丁 | 查看 | 原始文档 | blame | 历史
src/library/ini/ConvertUTF.h 补丁 | 查看 | 原始文档 | blame | 历史
src/library/ini/SimpleIni.h 补丁 | 查看 | 原始文档 | blame | 历史
src/library/license++.cpp 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/reader/CMakeLists.txt 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/license-generator/LicenseSigner.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/license-generator/license-generator.cpp 31 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/license-generator/license-generator.h 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/CMakeLists.txt 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/standard-license_test.cpp 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/CMakeLists.txt 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/LicenseReader_test.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/reader/CMakeLists.txt 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/test_reader.ini 补丁 | 查看 | 原始文档 | blame | 历史
test/license-generator/license-generator_test.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
.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"/>
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
)
src/library/LicenseReader.cpp
File was renamed from src/library/reader/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), //
@@ -94,6 +89,16 @@
}
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,
        vector<FullLicenseInfo>& licenseInfoOut) {
    vector<string> diskFiles;
@@ -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());
src/library/LicenseReader.h
File was renamed from src/library/reader/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);
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);
/**
src/library/base/StringUtils.cpp
@@ -9,6 +9,7 @@
#include "StringUtils.h"
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
namespace license {
@@ -32,4 +33,34 @@
    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 */
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_ */
src/library/ini/CMakeLists.txt
New file
@@ -0,0 +1,9 @@
ADD_LIBRARY(ini STATIC
    ConvertUTF.c
)
target_link_libraries(
     ini
)
src/library/ini/ConvertUTF.c
src/library/ini/ConvertUTF.h
src/library/ini/SimpleIni.h
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);
src/library/reader/CMakeLists.txt
File was deleted
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 {
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;
}
}
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);
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)
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);
}
test/library/CMakeLists.txt
@@ -1 +1,15 @@
add_subdirectory(reader)
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)
test/library/LicenseReader_test.cpp
File was renamed from test/library/reader/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;
test/library/reader/CMakeLists.txt
File was deleted
test/library/test_reader.ini
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;