gcontini
2019-10-12 dda16735b94661b798d6c0fd3e41af944de0a1fe
refactoring - locators

separated the way we retrieve license from the validation part
24个文件已修改
16个文件已添加
1 文件已重命名
1个文件已删除
1175 ■■■■ 已修改文件
.gitignore 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
example/CMakeLists.txt 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/build_properties.h.in 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/CMakeLists.txt 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.cpp 199 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.hpp 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/api/Licensepp-features.h 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/api/datatypes.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/api/license++.h 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/CMakeLists.txt 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/EventRegistry.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/EventRegistry.h 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/FileUtils.cpp 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/FileUtils.hpp 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/StringUtils.cpp 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/StringUtils.h 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/license++.cpp 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/ApplicationFolder.cpp 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/ApplicationFolder.hpp 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/CMakeLists.txt 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/EnvironmentVarData.cpp 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/EnvironmentVarData.hpp 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/EnvironmentVarLocation.cpp 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/EnvironmentVarLocation.hpp 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/ExternalDefinition.cpp 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/ExternalDefinition.hpp 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/LocatorFactory.cpp 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/LocatorFactory.hpp 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/LocatorStrategy.cpp 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/locate/LocatorStrategy.hpp 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/tools/license-generator/CMakeLists.txt 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/tools/license-generator/license-generator.h 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/CMakeLists.txt 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/date_test.cpp 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/generate-license.cpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/generate-license.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/hijiaking_test.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/standard-license_test.cpp 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/CMakeLists.txt 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/LicenseLocator_test.cpp 212 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/LicenseReader_test.cpp 27 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/license-generator/license-generator_test.cpp 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -22,3 +22,4 @@
.settings
.project
*.out
/Default/
example/CMakeLists.txt
@@ -1,4 +1,3 @@
#cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 2.8.11)
link_directories (   "${CMAKE_CURRENT_SOURCE_DIR}/../install/lib" )
@@ -10,7 +9,7 @@
SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib )
else(MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a .so)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s -Wl,--exclude-libs,liblicensepp_static.a")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s -Wl,--exclude-libs,liblicensecc_static.a")
find_package(OpenSSL REQUIRED)
endif(MSVC)
@@ -18,7 +17,7 @@
add_executable(example example.cpp)
target_link_libraries(example licensepp_static os base tools_base)
target_link_libraries(example licensecc_static os base tools_base)
if(NOT MSVC)
target_link_libraries(example crypto pthread dl z)
src/build_properties.h.in
@@ -1,12 +1,24 @@
#ifndef BUILD_PROPERTIES_H_
#define BUILD_PROPERTIES_H_
//License retrieval configuration
#define FIND_LICENSE_NEAR_MODULE 1
#define FIND_LICENSE_WITH_ENV_VAR 1
#define LICENSE_LOCATION_ENV_VAR "LICENSE_LOCATION"
#define LICENSE_DATA_ENV_VAR "LICENSE_DATA"
//Internal data structures limits
#define MAX_LICENSE_LENGTH 256*1024
//Build locations and parameters
#define PROJECT_INT_VERSION @LICENSECC_INT_VERSION@
#define PROJECT_VERSION "@LICENSECC_VERSION@"
#define PROJECT_BINARY_DIR "@CMAKE_BINARY_DIR@"
#define PROJECT_SRC_DIR "@CMAKE_CURRENT_LIST_DIR@"
#define PROJECT_BASE_DIR "@CMAKE_SOURCE_DIR@"
#define PROJECT_TEST_SRC_DIR "@CMAKE_SOURCE_DIR@/test"
#define PROJECT_TEST_TEMP_DIR "Testing/Temporary"
#define PROJECT_TEST_TEMP_DIR "@CMAKE_BINARY_DIR@/Testing/Temporary"
#define BUILD_TYPE "@CMAKE_BUILD_TYPE@"
#endif
src/library/CMakeLists.txt
@@ -1,20 +1,23 @@
add_subdirectory("os")
add_subdirectory("base")
add_subdirectory("ini")
add_subdirectory("locate")
ADD_LIBRARY(licensepp_static STATIC
ADD_LIBRARY(licensecc_static STATIC
    license++.cpp
    LicenseReader.cpp
    pc-identifiers.c
)
target_link_libraries(
     licensepp_static
     licensecc_static
     ini
     locators
     os
)
install(TARGETS licensepp_static ARCHIVE DESTINATION lib)
install(TARGETS licensecc_static ARCHIVE DESTINATION lib)
install(FILES api/datatypes.h api/license++.h DESTINATION include/api)
install(FILES base/base.h DESTINATION include/base)
install(FILES pc-identifiers.h DESTINATION include/)
src/library/LicenseReader.cpp
@@ -10,6 +10,7 @@
#else
# include <unistd.h>
#endif
#include <cstring>
#include <ctime>
#include <vector>
@@ -17,14 +18,17 @@
#include <iterator>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <math.h>
#include "pc-identifiers.h"
#include "LicenseReader.h"
#include "build_properties.h"
#include "public-key.h"
#include "LicenseReader.hpp"
#include "base/StringUtils.h"
#include "base/logger.h"
#include "public-key.h"
#include <build_properties.h>
#include "locate/LocatorFactory.hpp"
namespace license {
@@ -91,42 +95,60 @@
            license->days_left = 999999;
        } else {
            strncpy(license->expiry_date, to_date.c_str(), 11);
            const double secs = difftime(
                seconds_from_epoch(to_date.c_str()),
            const double secs = difftime(seconds_from_epoch(to_date.c_str()),
                time(nullptr));
            license->days_left = round(secs / (60 * 60 * 24));
        }
    }
}
LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) :
        licenseLocation(licenseLocation) {
}
EventRegistry LicenseReader::readLicenses(const string &product,
        vector<FullLicenseInfo>& licenseInfoOut) {
    vector<string> diskFiles;
    EventRegistry result = getLicenseDiskFiles(diskFiles);
    if (!result.isGood()) {
        return result;
    vector<unique_ptr<locate::LocatorStrategy>> locator_strategies;
    FUNCTION_RETURN ret = locate::LocatorFactory::getActiveStrategies(
            locator_strategies, licenseLocation.licenseFileLocation);
    EventRegistry eventRegistry;
    if (ret != FUNC_RET_OK) {
        eventRegistry.addError(LICENSE_FILE_NOT_FOUND);
        return eventRegistry;
    }
    bool loadAtLeastOneFile = false;
    if (!eventRegistry.isGood()) {
        return eventRegistry;
    }
    bool atLeastOneFileFound = false;
    bool atLeastOneFileRecognized = false;
    bool atLeastOneProductLicensed = false;
    bool atLeastOneLicenseComplete = false;
    for (unique_ptr<locate::LocatorStrategy>& locator : locator_strategies) {
        vector<string> licenseLocations = locator->licenseLocations(
                eventRegistry);
        if (licenseLocations.size() == 0) {
            continue;
        }
        atLeastOneFileFound = true;
    CSimpleIniA ini;
    for (auto it = diskFiles.begin(); it != diskFiles.end(); it++) {
        for (auto it = licenseLocations.begin(); it != licenseLocations.end();
                it++) {
        ini.Reset();
        const SI_Error rc = ini.LoadFile((*it).c_str());
            string license = locator->retrieveLicense((*it).c_str());
            const SI_Error rc = ini.LoadData(license.c_str(), license.size());
        if (rc < 0) {
            result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SVRT_WARN, *it);
                eventRegistry.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SVRT_WARN,
                        *it);
            continue;
        } else {
            loadAtLeastOneFile = true;
                atLeastOneFileRecognized = true;
        }
        const char* productNamePtr = product.c_str();
        const int sectionSize = ini.GetSectionSize(productNamePtr);
        if (sectionSize <= 0) {
            result.addEvent(PRODUCT_NOT_LICENSED, SVRT_WARN, *it);
                eventRegistry.addEvent(PRODUCT_NOT_LICENSED, SVRT_WARN, *it);
            continue;
        } else {
            atLeastOneProductLicensed = true;
@@ -160,156 +182,35 @@
                    "from_sw_version",
                    FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
            const int to_sw_version = ini.GetLongValue(productNamePtr,
                    "to_sw_version", FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
                        "to_sw_version",
                        FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
            string extra_data = trim_copy(
                    ini.GetValue(productNamePtr, "extra_data", ""));
            FullLicenseInfo licInfo(*it, product, license_signature,
                    (int) license_version, from_date, to_date, client_signature,
                    from_sw_version, to_sw_version, extra_data);
                        (int) license_version, from_date, to_date,
                        client_signature, from_sw_version, to_sw_version,
                        extra_data);
            licenseInfoOut.push_back(licInfo);
            atLeastOneLicenseComplete = true;
        } else {
            result.addEvent(LICENSE_MALFORMED, SVRT_WARN, *it);
                eventRegistry.addEvent(LICENSE_MALFORMED, SVRT_WARN, *it);
        }
    }
    if (!loadAtLeastOneFile) {
        result.turnEventIntoError(FILE_FORMAT_NOT_RECOGNIZED);
    }
    if (!atLeastOneProductLicensed) {
        result.turnEventIntoError(PRODUCT_NOT_LICENSED);
    }
    if (!atLeastOneLicenseComplete) {
        result.turnEventIntoError(LICENSE_MALFORMED);
    }
    return result;
}
bool LicenseReader::findLicenseWithExplicitLocation(vector<string>& diskFiles,
        EventRegistry& eventRegistry) {
//bool hasFileLocation = false;
    bool licenseFoundWithExplicitLocation = false;
    if (licenseLocation.licenseFileLocation != nullptr
            && licenseLocation.licenseFileLocation[0] != '\0') {
        //hasFileLocation = true;
        const string varName(licenseLocation.licenseFileLocation);
        const vector<string> declared_positions = splitLicensePositions(varName);
        vector<string> existing_pos = filterExistingFiles(declared_positions);
        if (existing_pos.size() > 0) {
            if (existing_pos.size() > 0) {
                licenseFoundWithExplicitLocation = true;
                for (auto it = existing_pos.begin(); it != existing_pos.end();
                        ++it) {
                    diskFiles.push_back(*it);
                    eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
                }
            }
        } else {
            eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN, varName);
        }
    }
    return licenseFoundWithExplicitLocation;
}
bool LicenseReader::findFileWithEnvironmentVariable(vector<string>& diskFiles,
        EventRegistry& eventRegistry) {
    bool licenseFileFoundWithEnvVariable = false;
    if (licenseLocation.environmentVariableName != nullptr
            && licenseLocation.environmentVariableName[0] != '\0') {
        const string varName(licenseLocation.environmentVariableName);
        if (varName.length() > 0) {
            //var name is passed in by the calling application.
            char* env_var_value = getenv(varName.c_str());
            if (env_var_value != nullptr && env_var_value[0] != '\0') {
                const vector<string> declared_positions = splitLicensePositions(
                        string(env_var_value));
                vector<string> existing_pos = filterExistingFiles(
                        declared_positions);
                if (existing_pos.size() > 0) {
                    licenseFileFoundWithEnvVariable = true;
                    for (auto it = existing_pos.begin();
                            it != existing_pos.end(); ++it) {
                        diskFiles.push_back(*it);
                        eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO,
                                *it);
                    }
                } else {
                    eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
                            env_var_value);
                }
            } else {
                eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
                        SVRT_WARN);
            }
        } else {
            eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, SVRT_WARN);
        }
    }
    return licenseFileFoundWithEnvVariable;
}
EventRegistry LicenseReader::getLicenseDiskFiles(vector<string>& diskFiles) {
    EventRegistry eventRegistry;
    const bool licenseFoundWithExplicitLocation = findLicenseWithExplicitLocation(
            diskFiles, eventRegistry);
    bool foundNearModule = false;
    if (licenseLocation.openFileNearModule) {
        char fname[MAX_PATH] = { 0 };
        const FUNCTION_RETURN fret = getModuleName(fname);
        if (fret == FUNC_RET_OK) {
            const string temptativeLicense = string(fname) + ".lic";
            ifstream f(temptativeLicense.c_str());
            if (f.good()) {
                foundNearModule = true;
                diskFiles.push_back(temptativeLicense);
                eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO,
                        temptativeLicense);
            } else {
                eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
                        temptativeLicense);
            }
            f.close();
        } else {
            LOG_WARN("Error determining module name.");
        }
    }
    const bool licenseFileFoundWithEnvVariable = findFileWithEnvironmentVariable(
            diskFiles, eventRegistry);
    if (!foundNearModule && !licenseFoundWithExplicitLocation
            && !licenseFileFoundWithEnvVariable) {
    if (!atLeastOneFileFound) {
        eventRegistry.turnEventIntoError(ENVIRONMENT_VARIABLE_NOT_DEFINED);
        eventRegistry.turnEventIntoError(LICENSE_FILE_NOT_FOUND);
    } else if (!atLeastOneFileRecognized) {
        eventRegistry.turnEventIntoError(FILE_FORMAT_NOT_RECOGNIZED);
    } else if (!atLeastOneProductLicensed) {
        eventRegistry.turnEventIntoError(PRODUCT_NOT_LICENSED);
    } else    if (!atLeastOneLicenseComplete) {
        eventRegistry.turnEventIntoError(LICENSE_MALFORMED);
    }
    return eventRegistry;
}
vector<string> LicenseReader::filterExistingFiles(
        vector<string> licensePositions) {
    vector<string> existingFiles;
    for (auto it = licensePositions.begin(); it != licensePositions.end();
            it++) {
        ifstream f(it->c_str());
        if (f.good()) {
            existingFiles.push_back(*it);
        }
        f.close();
    }
    return existingFiles;
}
vector<string> LicenseReader::splitLicensePositions(string licensePositions) {
    std::stringstream streamToSplit(licensePositions);
    std::string segment;
    std::vector<string> seglist;
    while (std::getline(streamToSplit, segment, ';')) {
        seglist.push_back(segment);
    }
    return seglist;
}
LicenseReader::~LicenseReader() {
}
string FullLicenseInfo::printForSign() const {
src/library/LicenseReader.hpp
File was renamed from src/library/LicenseReader.h
@@ -11,6 +11,7 @@
#include "api/datatypes.h"
#include "base/EventRegistry.h"
#include "os/os.h"
#include "locate/LocatorStrategy.hpp"
#define SI_SUPPORT_IOSTREAMS
#include "ini/SimpleIni.h"
#include <string>
@@ -53,6 +54,7 @@
    time_t expires_on() const;
    time_t valid_from() const;
};
/**
 * This class it is responsible to read the licenses from the disk
 * (in future from network) examining all the possible LicenseLocation
@@ -74,14 +76,6 @@
class LicenseReader {
private:
    const LicenseLocation licenseLocation;
    EventRegistry getLicenseDiskFiles(vector<string>& diskFiles);
    vector<string> filterExistingFiles(vector<string> licensePositions);
    vector<string> splitLicensePositions(string licensePositions);
    bool findLicenseWithExplicitLocation(vector<string>& diskFiles,
            EventRegistry& eventRegistry);
    bool findFileWithEnvironmentVariable(vector<string>& diskFiles,
            EventRegistry& eventRegistry);
public:
    LicenseReader(const LicenseLocation& licenseLocation);
    EventRegistry readLicenses(const string &product,
src/library/api/Licensepp-features.h
File was deleted
src/library/api/datatypes.h
@@ -18,7 +18,6 @@
#define DllExport  __declspec( dllexport )
#endif
#define ENVIRONMENT_VAR_NAME_MAX 64
#define PC_IDENTIFIER_SIZE 18
#define PROPRIETARY_DATA_SIZE 16
src/library/api/license++.h
@@ -35,14 +35,21 @@
 */
void identify_pc(IDENTIFICATION_STRATEGY pc_id_method,
        char chbuffer[PC_IDENTIFIER_SIZE + 1]);
/*
 * The optional parameter License contains the information the program that uses the library
 * should display:
/**
 * This method is used to request the use of one license for a product.
 * In case of local license it's used to check if the product is licensed.
 * [In case of network licenses this will decrease the count of the available
 *  licenses]
 *
 * @return true if successful. False if there are errors.
 * @param licenseLocation[in] licenseLocation, either the name of the file
 * @return LICENSE_OK(0) if successful. Other values if there are errors.
 * @param productName[in]
 *             a vendor defined string containing the name of the product we want to request.
 * @param licenseLocation[in] otpional, can be NULL.
 *                     licenseLocation, either the name of the file
 *                                 or the name of the environment variable should be !='\0'
 * @param license[out] optional, can be NULL.
 * @param license[out] optional, can be NULL, if set it will return extra informations about the license.
 */
EVENT_TYPE acquire_license(const char * productName,
@@ -51,14 +58,14 @@
/**
 * Do nothing for now, useful for network licenses.
 * Should be called from time to time to confirm we're still using the
 * slicense.
 * license.
 */
EVENT_TYPE confirm_license(char * productName,
EVENT_TYPE confirm_license(char * featureName,
        LicenseLocation licenseLocation);
/**
 * Do nothing for now, useful for network licenses.
 */
EVENT_TYPE release_license(char * productName,
EVENT_TYPE release_license(char * featureName,
        LicenseLocation licenseLocation);
#ifdef __cplusplus
src/library/base/CMakeLists.txt
@@ -1,6 +1,7 @@
ADD_LIBRARY(base STATIC
    EventRegistry.cpp
    StringUtils.cpp
    FileUtils.cpp
    logger.c
)
src/library/base/EventRegistry.cpp
@@ -10,9 +10,9 @@
#include <string.h>
#include <algorithm>
namespace license {
using namespace std;
namespace license {
EventRegistry::EventRegistry() {
}
src/library/base/EventRegistry.h
@@ -13,7 +13,7 @@
#include <string>
namespace license {
using namespace std;
/*
AuditEvent error_event_builder(EVENT_TYPE event);
AuditEvent audit_event_builder(EVENT_TYPE event, SEVERITY severity);
@@ -25,7 +25,7 @@
    friend EventRegistry& operator<<(EventRegistry&, AuditEvent&);
    friend EventRegistry& operator<<(EventRegistry&, EventRegistry&);
    //TODO change into map
    vector<AuditEvent> logs;
    std::vector<AuditEvent> logs;
    //Forbid copy
    //EventRegistry(const EventRegistry& that) = delete;
public:
@@ -44,7 +44,7 @@
    void addError(EVENT_TYPE event);
    void addEvent(EVENT_TYPE event, SEVERITY severity);
    void addEvent(EVENT_TYPE event, SEVERITY severity,
            const string& eventParameter);
            const std::string &eventParameter);
    void exportLastEvents(AuditEvent* auditEvents,int nlogs);
};
src/library/base/FileUtils.cpp
New file
@@ -0,0 +1,45 @@
/*
 * FileUtils.cpp
 *
 *  Created on: Oct 8, 2019
 *      Author: devel
 */
#include <fstream>
#include <string>
#include <cerrno>
#include <iostream>
#include <algorithm>
#include "FileUtils.hpp"
namespace license {
using namespace std;
vector<string> filter_existing_files(const vector<string> &fileList) {
    vector<string> existingFiles;
    for (auto it = fileList.begin(); it != fileList.end(); it++) {
        ifstream f(it->c_str());
        if (f.good()) {
            existingFiles.push_back(*it);
        }
        f.close();
    }
    return existingFiles;
}
string get_file_contents(const char *filename, size_t max_size) {
    ifstream in(filename, std::ios::binary);
    if (in) {
        string contents;
        size_t index = in.seekg(0, ios::end).tellg();
        size_t limited_size = min(index, max_size);
        contents.resize(limited_size);
        in.seekg(0, ios::beg);
        in.read(&contents[0], limited_size);
        return contents;
    }
    throw(errno);
}
}
src/library/base/FileUtils.hpp
New file
@@ -0,0 +1,20 @@
/*
 * FileUtils.h
 *
 *  Created on: Apr 8, 2019
 *
 */
#ifndef FILEUTILS_H_
#define FILEUTILS_H_
#include <string>
#include <vector>
namespace license {
std::vector<std::string> filter_existing_files(const std::vector<std::string>& fileList);
std::string get_file_contents(const char *filename,size_t max_size);
} /* namespace license */
#endif
src/library/base/StringUtils.cpp
@@ -9,6 +9,7 @@
#include "StringUtils.h"
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <stdexcept>
@@ -68,4 +69,17 @@
    tm.tm_wday = -1;
    return mktime(&tm);
}
const vector<string> split_string(const string& licensePositions,char splitchar) {
    std::stringstream streamToSplit(licensePositions);
    std::string segment;
    std::vector<string> seglist;
    while (std::getline(streamToSplit, segment, splitchar)) {
        seglist.push_back(segment);
    }
    return seglist;
}
} /* namespace license */
src/library/base/StringUtils.h
@@ -8,6 +8,7 @@
#ifndef STRINGUTILS_H_
#define STRINGUTILS_H_
#include <string>
#include <vector>
namespace license {
using namespace std;
@@ -23,6 +24,13 @@
string toupper_copy(const string& lowercase);
time_t seconds_from_epoch(const char* s);
} /* namespace license */
/**
 * Split a string on a given character
 */
const vector<string> split_string(const string& licensePositions, const char splitchar);
}
/* namespace license */
#endif /* STRINGUTILS_H_ */
src/library/license++.cpp
@@ -9,8 +9,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "api/license++.h"
#include "LicenseReader.h"
#include "LicenseReader.hpp"
using namespace std;
void print_error(char out_buffer[256], LicenseInfo* licenseInfo) {
src/library/locate/ApplicationFolder.cpp
New file
@@ -0,0 +1,60 @@
/*
 * ApplicationFolder.cpp
 *
 *  Created on: Oct 12, 2019
 *      Author: Gabriele Contini
 */
#include <fstream>
#include <sstream>
#include <string>
#include <build_properties.h>
#include "../base/logger.h"
#include "../api/datatypes.h"
#include "../base/base.h"
#include "../base/EventRegistry.h"
#include "../base/FileUtils.hpp"
#include "../os/os.h"
#include "ApplicationFolder.hpp"
#include <iostream>
namespace license {
namespace locate {
using namespace std;
ApplicationFolder::ApplicationFolder() :
        LocatorStrategy("ApplicationFolder") {
}
ApplicationFolder::~ApplicationFolder() {
}
const vector<string> ApplicationFolder::licenseLocations(
        EventRegistry &eventRegistry) const {
    vector<string> diskFiles;
    char fname[MAX_PATH] = { 0 };
    const FUNCTION_RETURN fret = getModuleName(fname);
    cout << string(fname) << endl;
    cout << fret << endl;
    if (fret == FUNC_RET_OK) {
        const string temptativeLicense = string(fname) + ".lic";
        ifstream f(temptativeLicense.c_str());
        if (f.good()) {
            diskFiles.push_back(temptativeLicense);
            eventRegistry.addEvent((EVENT_TYPE) LICENSE_FILE_FOUND,
                    (SEVERITY) SVRT_INFO, temptativeLicense);
        } else {
            eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
                    temptativeLicense);
        }
        f.close();
    } else {
        LOG_WARN("Error determining module name.");
    }
    return diskFiles;
}
}
} /* namespace license */
src/library/locate/ApplicationFolder.hpp
New file
@@ -0,0 +1,28 @@
/*
 * ApplicationFolder.h
 *
 *  Created on: Oct 6, 2019
 *      Author: devel
 */
#ifndef SRC_LIBRARY_RETRIEVERS_APPLICATIONFOLDER_H_
#define SRC_LIBRARY_RETRIEVERS_APPLICATIONFOLDER_H_
#include <string>
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
class ApplicationFolder: public LocatorStrategy {
public:
    ApplicationFolder();
    virtual const std::vector<std::string> licenseLocations(EventRegistry& eventRegistry) const;
    virtual ~ApplicationFolder();
};
}
} /* namespace license */
#endif /* SRC_LIBRARY_RETRIEVERS_APPLICATIONFOLDER_H_ */
src/library/locate/CMakeLists.txt
New file
@@ -0,0 +1,18 @@
ADD_LIBRARY(locators STATIC
    ApplicationFolder.cpp
    EnvironmentVarLocation.cpp
    EnvironmentVarData.cpp
    ExternalDefinition.cpp
    LocatorStrategy.cpp
    LocatorFactory.cpp
)
add_dependencies( locators os base )
target_link_libraries(
     locators
     os
     base
)
src/library/locate/EnvironmentVarData.cpp
New file
@@ -0,0 +1,33 @@
/*
 * EnvironmentVarData.cpp
 *
 *  Created on: Oct 12, 2019
 *     Author: Gabriele Contini
 */
#include "EnvironmentVarData.hpp"
namespace license {
namespace locate {
using namespace std;
EnvironmentVarData::EnvironmentVarData() :
        LocatorStrategy("EnvironmentVarData") {
}
EnvironmentVarData::~EnvironmentVarData() {
}
const vector<string> EnvironmentVarData::licenseLocations(
        EventRegistry &eventRegistry) const {
    vector<string> diskFiles;
    return diskFiles;
}
const std::string EnvironmentVarData::retrieveLicense(const std::string &licenseLocation) const{
    return "";
}
}
}
src/library/locate/EnvironmentVarData.hpp
New file
@@ -0,0 +1,28 @@
/*
 * EnvironmentVarLocation.h
 *
 *  Created on: Oct 6, 2019
 *      Author: devel
 */
#ifndef SRC_LIBRARY_LOCATE_ENVIRONMENTVARDATA_H_
#define SRC_LIBRARY_LOCATE_ENVIRONMENTVARDATA_H_
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
class EnvironmentVarData: public LocatorStrategy {
private:
public:
    EnvironmentVarData();
    virtual const std::vector<std::string> licenseLocations(EventRegistry& eventRegistr) const;
    virtual const std::string retrieveLicense(const std::string &licenseLocation) const;
    virtual ~EnvironmentVarData();
};
}
}
#endif
src/library/locate/EnvironmentVarLocation.cpp
New file
@@ -0,0 +1,56 @@
/*
 * EnvironmentVarLocation.cpp
 *
 *  Created on: Oct 12, 2019
 *      Author: Gabriele Contini
 */
#include <build_properties.h>
#include "../base/FileUtils.hpp"
#include "../base/StringUtils.h"
#include "EnvironmentVarLocation.hpp"
namespace license {
namespace locate {
using namespace std;
EnvironmentVarLocation::EnvironmentVarLocation() :
        LocatorStrategy("EnvironmentVarLocation") {
}
EnvironmentVarLocation::~EnvironmentVarLocation() {
}
const vector<string> EnvironmentVarLocation::licenseLocations(
        EventRegistry &eventRegistry) const {
    vector<string> licenseFileFoundWithEnvVariable;
    const string varName(LICENSE_LOCATION_ENV_VAR);
    if (varName.length() > 0) {
        //var name is defined in header files.
        char *env_var_value = getenv(LICENSE_LOCATION_ENV_VAR);
        if (env_var_value != nullptr && env_var_value[0] != '\0') {
            const vector<string> declared_positions = license::split_string(
                    string(env_var_value), ';');
            vector<string> existing_pos = license::filter_existing_files(
                    declared_positions);
            if (existing_pos.size() > 0) {
                for (auto it = existing_pos.begin(); it != existing_pos.end();
                        ++it) {
                    licenseFileFoundWithEnvVariable.push_back(*it);
                    eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
                }
            } else {
                eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
                        env_var_value);
            }
        } else {
            eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, SVRT_WARN);
        }
    }
    return licenseFileFoundWithEnvVariable;
}
}
}
src/library/locate/EnvironmentVarLocation.hpp
New file
@@ -0,0 +1,27 @@
/*
 * EnvironmentVarLocation.h
 *
 *  Created on: Oct 6, 2019
 *      Author: devel
 */
#ifndef SRC_LIBRARY_LOCATE_ENVIRONMENTVARLOCATION_H_
#define SRC_LIBRARY_LOCATE_ENVIRONMENTVARLOCATION_H_
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
class EnvironmentVarLocation: public LocatorStrategy {
public:
    EnvironmentVarLocation();
    virtual const std::vector<std::string> licenseLocations(EventRegistry& eventRegistry) const;
    virtual ~EnvironmentVarLocation();
};
}
}
#endif /* SRC_LIBRARY_LOCATE_ENVIRONMENTVARLOCATION_H_ */
src/library/locate/ExternalDefinition.cpp
New file
@@ -0,0 +1,40 @@
/*
 * ExplicitDefinition.cpp
 *
 *  Created on: Oct 12, 2019
 *      Author: Gabriele Contini
 */
#include "../base/StringUtils.h"
#include "../base/FileUtils.hpp"
#include "ExternalDefinition.hpp"
namespace license {
namespace locate {
using namespace std;
ExternalDefinition::ExternalDefinition(const char *location) :
        LocatorStrategy("ExternalDefinition"), m_location(location) {
}
ExternalDefinition::~ExternalDefinition() {
}
const std::vector<std::string> ExternalDefinition::licenseLocations(
        EventRegistry &eventRegistry) const {
    const vector<string> declared_positions = license::split_string(m_location,
            ';');
    const vector<string> existing_pos = license::filter_existing_files(
            declared_positions);
    if (existing_pos.size() > 0) {
        for (auto it = existing_pos.begin(); it != existing_pos.end(); ++it) {
            eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
        }
    } else {
        eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN, m_location);
    }
    return existing_pos;
}
} /* namespace locate */
} /* namespace license */
src/library/locate/ExternalDefinition.hpp
New file
@@ -0,0 +1,28 @@
/*
 * ExplicitDefinition.hpp
 *
 *  Created on: Oct 12, 2019
 *      Author: devel
 */
#ifndef SRC_LIBRARY_LOCATE_EXTERNALDEFINITION_HPP_
#define SRC_LIBRARY_LOCATE_EXTERNALDEFINITION_HPP_
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
class ExternalDefinition: public LocatorStrategy {
private:
    const std::string m_location;
public:
    ExternalDefinition(const char* location);
    virtual const std::vector<std::string> licenseLocations(EventRegistry& eventRegistry) const;
    virtual ~ExternalDefinition();
};
} /* namespace locate */
} /* namespace license */
#endif /* SRC_LIBRARY_LOCATE_EXTERNALDEFINITION_HPP_ */
src/library/locate/LocatorFactory.cpp
New file
@@ -0,0 +1,48 @@
/*
 * LocatorFactory.cpp
 *
 *  Created on: Oct 13, 2019
 *      Author: Gabriele Contini
 */
#include "build_properties.h"
#include "LocatorStrategy.hpp"
#include "LocatorFactory.hpp"
#include "ApplicationFolder.hpp"
//#include "EnvironmentVarData.hpp"
#include "EnvironmentVarLocation.hpp"
#include "ExternalDefinition.hpp"
namespace license {
namespace locate {
using namespace std;
FUNCTION_RETURN LocatorFactory::getActiveStrategies(
        vector<unique_ptr<LocatorStrategy>> &strategies,
        const char *locationHint) {
#if(FIND_LICENSE_NEAR_MODULE)
    strategies.push_back(
            unique_ptr<LocatorStrategy>(
                    (LocatorStrategy*) new ApplicationFolder()));
#endif
#if(FIND_LICENSE_WITH_ENV_VAR)
    strategies.push_back(
            unique_ptr<LocatorStrategy>(
                    (LocatorStrategy*) new EnvironmentVarLocation()));
//    strategies.push_back(
//            unique_ptr<LocatorStrategy>(
//                    (LocatorStrategy*) new EnvironmentVarData()));
#endif
    if (locationHint != nullptr) {
        strategies.push_back(
                unique_ptr<LocatorStrategy>(
                        (LocatorStrategy*) new ExternalDefinition(locationHint)));
    }
    return strategies.size() > 0 ? FUNC_RET_OK : FUNC_RET_NOT_AVAIL;
}
}
}
src/library/locate/LocatorFactory.hpp
New file
@@ -0,0 +1,31 @@
#ifndef RETRIEVE_FACTORY_H_
#define RETRIEVE_FACTORY_H_
#include <cstddef>
#include <string>
#include <vector>
#include "../base/base.h"
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
class LocatorFactory {
private:
    inline LocatorFactory() {
    }
    inline ~LocatorFactory() {
    }
public:
    static FUNCTION_RETURN getActiveStrategies(
            std::vector<std::unique_ptr<LocatorStrategy>> &strategiesOut,
            const char *locationHint);
};
}
}
#endif
src/library/locate/LocatorStrategy.cpp
New file
@@ -0,0 +1,23 @@
/*
 * EnvironmentVarLocation.cpp
 *
 *  Created on: Oct 12, 2019
 *      Author: Gabriele Contini
 */
#include <build_properties.h>
#include "../base/FileUtils.hpp"
#include "LocatorStrategy.hpp"
namespace license {
namespace locate {
using namespace std;
const string LocatorStrategy::retrieveLicense(
        const string &licenseLocation) const {
    return get_file_contents(licenseLocation.c_str(), MAX_LICENSE_LENGTH);
}
}
}
src/library/locate/LocatorStrategy.hpp
New file
@@ -0,0 +1,52 @@
#ifndef RETRIEVER_H_
#define RETRIEVER_H_
#include <memory>
#include <cstddef>
#include <string>
#include <vector>
#include "../base/EventRegistry.h"
namespace license {
namespace locate {
/**
 * This class provides a common interface to find and retrieve
 * licenses from different sources and positions.
 *
 * 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>
 * </ol>
 */
class LocatorStrategy {
protected:
    const std::string m_strategy_name;
    inline LocatorStrategy(const std::string &strategyName) :
            m_strategy_name(strategyName) {
    }
public:
    virtual const std::string get_strategy_name() const {
        return m_strategy_name;
    }
    virtual const std::vector<std::string> licenseLocations(
            EventRegistry &eventRegistry) const = 0;
    /**
     * Default implementation is to retrieve the license from file.
     * Subclasses may override it.
     * @param licenseLocation
     * @return
     */
    virtual const std::string retrieveLicense(
            const std::string &licenseLocation) const;
    inline virtual ~LocatorStrategy() {
    }
};
}
}
#endif
src/tools/license-generator/CMakeLists.txt
@@ -10,7 +10,7 @@
target_link_libraries(
     license_generator_lib
     tools_base
     licensepp_static
     licensecc_static
     $<$<CONFIG:Debug>:${Boost_PROGRAM_OPTIONS_LIBRARY_DEBUG}>
     $<$<NOT:$<CONFIG:Debug>>:${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE}>     
     $<$<CONFIG:Debug>:${Boost_SYSTEM_LIBRARY_DEBUG}>
src/tools/license-generator/license-generator.h
@@ -9,7 +9,8 @@
#define LICENSE_GENERATOR_H_
#include <boost/program_options.hpp>
#include "../../library/LicenseReader.h"
#include "../../library/LicenseReader.hpp"
namespace license {
test/functional/CMakeLists.txt
@@ -14,7 +14,7 @@
target_link_libraries(
 standard_license_test
 licensepp_static
 licensecc_static
 license_generator_snippet
 ${Boost_LIBRARIES}
)
@@ -26,7 +26,7 @@
target_link_libraries(
 date_test
 licensepp_static
 licensecc_static
 license_generator_snippet
 ${Boost_LIBRARIES}
)
@@ -38,7 +38,7 @@
target_link_libraries(
 volid_test
 licensepp_static
 licensecc_static
 license_generator_snippet
 ${Boost_LIBRARIES}
)
test/functional/date_test.cpp
@@ -1,17 +1,19 @@
#define BOOST_TEST_MODULE date_test
//#define BOOST_TEST_MAIN
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>
#include <build_properties.h>
#include "../../src/tools/license-generator/license-generator.h"
#include "../../src/library/api/license++.h"
#include <build_properties.h>
#include <boost/filesystem.hpp>
#include "../../src/library/ini/SimpleIni.h"
#include "generate-license.h"
namespace fs = boost::filesystem;
using namespace license;
using namespace std;
namespace test {
BOOST_AUTO_TEST_CASE( license_not_expired ) {
    const string licLocation(PROJECT_TEST_TEMP_DIR "/not_expired.lic");
@@ -25,7 +27,8 @@
    licenseLocation.openFileNearModule = false;
    licenseLocation.licenseFileLocation = licLocation.c_str();
    licenseLocation.environmentVariableName = "";
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation, &license);
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation,
            &license);
    BOOST_CHECK_EQUAL(result, LICENSE_OK);
    BOOST_CHECK_EQUAL(license.has_expiry, true);
    BOOST_CHECK_EQUAL(license.linked_to_pc, false);
@@ -33,6 +36,7 @@
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");
@@ -43,8 +47,11 @@
    licenseLocation.openFileNearModule = false;
    licenseLocation.licenseFileLocation = licLocation.c_str();
    licenseLocation.environmentVariableName = "";
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation, &license);
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation,
            &license);
    BOOST_CHECK_EQUAL(result, PRODUCT_EXPIRED);
    BOOST_CHECK_EQUAL(license.has_expiry, true);
    BOOST_CHECK_EQUAL(license.linked_to_pc, false);
}
}
test/functional/generate-license.cpp
@@ -17,6 +17,7 @@
using namespace std;
void generate_license(const string& fname, const vector<string>& other_args) {
    remove(fname.c_str());
    const int argc = 4+other_args.size();
    const char** argv = new const char*[argc + 1];
    unsigned int i=0;
test/functional/generate-license.h
@@ -1,5 +1,3 @@
#include<string>
#include<vector>
test/functional/hijiaking_test.cpp
@@ -2,8 +2,8 @@
//#define BOOST_TEST_MAIN
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "../../../src/license-generator/license-generator.h"
#include "../../../src/library/api/license++.h"
#include "../../src/tools/license-generator/license-generator.h"
#include "../../src/library/api/license++.h"
#include <build_properties.h>
#include <boost/filesystem.hpp>
#include "../../src/library/ini/SimpleIni.h"
test/functional/standard-license_test.cpp
@@ -13,6 +13,7 @@
using namespace license;
using namespace std;
namespace test {
BOOST_AUTO_TEST_CASE( standard_lic_file ) {
    const string licLocation(PROJECT_TEST_TEMP_DIR "/standard_license.lic");
@@ -24,11 +25,27 @@
    licenseLocation.openFileNearModule=false;
    licenseLocation.licenseFileLocation = licLocation.c_str();
    licenseLocation.environmentVariableName = "";
    const EVENT_TYPE result = acquire_license("TEST",
            licenseLocation, & license);
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation,
            &license);
    BOOST_CHECK_EQUAL(result, LICENSE_OK);
    BOOST_CHECK_EQUAL(license.has_expiry, false);
    BOOST_CHECK_EQUAL(license.linked_to_pc, false);
}
BOOST_AUTO_TEST_CASE( pc_identifier ) {
    const string licLocation(PROJECT_TEST_TEMP_DIR "/pc_identifier.lic");
    const vector<string> extraArgs = { "-s", "Jaaa-aaaa-MG9F-ZhB1" };
    generate_license(licLocation, extraArgs);
    LicenseInfo license;
    LicenseLocation licenseLocation;
    licenseLocation.openFileNearModule = false;
    licenseLocation.licenseFileLocation = licLocation.c_str();
    licenseLocation.environmentVariableName = "";
    const EVENT_TYPE result = acquire_license("TEST", licenseLocation,
            &license);
    BOOST_CHECK_EQUAL(result, IDENTIFIERS_MISMATCH);
    BOOST_CHECK_EQUAL(license.has_expiry, false);
    BOOST_CHECK_EQUAL(license.linked_to_pc, true);
}
}
test/library/CMakeLists.txt
@@ -5,7 +5,7 @@
target_link_libraries(
 license_reader_test
 licensepp_static
 licensecc_static
 ${Boost_LIBRARIES}
)
@@ -32,3 +32,23 @@
    ADD_TEST(NAME os_linux_test COMMAND os_linux_test)
ENDIF(WIN32)
### LicenseLocator tests
add_executable(
 license_locator_test
 LicenseLocator_test.cpp
)
target_link_libraries(
 license_locator_test
 locators
 ${Boost_LIBRARIES}
)
IF( ( CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") AND CMAKE_CROSSCOMPILING)
#binfmt_misc doesn't work in my system :(
    ADD_TEST(NAME license_locator_test COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/license_locator_test)
ELSE()
    ADD_TEST(NAME license_locator_test COMMAND license_locator_test)
ENDIF()
test/library/LicenseLocator_test.cpp
New file
@@ -0,0 +1,212 @@
#define BOOST_TEST_MODULE "license_locator_test"
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <vector>
#include <boost/test/unit_test.hpp>
#include <stdlib.h>
#include <cstdio>
#include <build_properties.h>
#include "../../src/library/base/EventRegistry.h"
#include "../../src/library/locate/ApplicationFolder.hpp"
#include "../../src/library/locate/EnvironmentVarLocation.hpp"
#include "../../src/library/locate/ExternalDefinition.hpp"
#define MOCK_LICENSE PROJECT_TEST_SRC_DIR "/library/test_reader.ini"
namespace test {
using namespace license::locate;
using namespace std;
/*****************************************************************************
 * Application Folder tests
 *****************************************************************************/
BOOST_AUTO_TEST_CASE( read_license_near_module ) {
#ifdef _WIN32
#ifdef _DEBUG
    const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Debug";
#else
    const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Release";
#endif
    const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE ".exe";
#else
    const string testExeFolder = PROJECT_BINARY_DIR "/test/library";
    const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE;
#endif
    const string referenceLicenseFileName = testExeFolder + "/"
            + BOOST_TEST_MODULE ".lic";
    //Verify we're pointing the correct executable
    ifstream f(testExe.c_str());
    BOOST_REQUIRE_MESSAGE(f.good(), "File [" + testExe + "] NOT found");
    //copy test license near module
    std::ifstream src(MOCK_LICENSE, std::ios::binary);
    std::ofstream dst(referenceLicenseFileName, std::ios::binary);
    dst << src.rdbuf();
    dst.close();
    license::EventRegistry registry;
    ApplicationFolder applicationFolder;
    vector<string> licenseInfos = applicationFolder.licenseLocations(registry);
    BOOST_CHECK(registry.isGood());
    BOOST_REQUIRE_EQUAL(1, licenseInfos.size());
    string currentLocation = licenseInfos[0];
    BOOST_CHECK_MESSAGE(referenceLicenseFileName.compare(currentLocation) == 0,
            "file found at expected location");
    string licenseRealContent = applicationFolder.retrieveLicense(
            currentLocation);
    src.seekg(0, ios::beg);
    std::string referenceContent((std::istreambuf_iterator<char>(src)),
            std::istreambuf_iterator<char>());
    BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
            "File content is same");
    remove(referenceLicenseFileName.c_str());
}
/*****************************************************************************
 * External_Definition tests
 *****************************************************************************/
BOOST_AUTO_TEST_CASE( external_definition ) {
    //an application can define multiple license locations separated by ';'
    const char *applicationDefinedString =
    MOCK_LICENSE ";/this/one/doesnt/exist";
    //read test license
    std::ifstream src(MOCK_LICENSE, std::ios::binary);
    std::string referenceContent((std::istreambuf_iterator<char>(src)),
            std::istreambuf_iterator<char>());
    license::EventRegistry registry;
    ExternalDefinition externalDefinition(applicationDefinedString);
    vector<string> licenseInfos = externalDefinition.licenseLocations(registry);
    BOOST_CHECK(registry.isGood());
    BOOST_CHECK_EQUAL(1, licenseInfos.size());
    string currentLocation = licenseInfos[0];
    BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0,
            "file found at expected location");
    string licenseRealContent = externalDefinition.retrieveLicense(
            currentLocation);
    BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
            "File content is same");
}
/**
 * The license file doesn't exist. Chech that the locator reports the right error
 */
BOOST_AUTO_TEST_CASE( external_definition_not_found ) {
    const char *applicationDefinedString = PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
    license::EventRegistry registry;
    ExternalDefinition externalDefinition(applicationDefinedString);
    vector<string> licenseInfos = externalDefinition.licenseLocations(registry);
    BOOST_CHECK_MESSAGE(registry.isGood(),
            "No fatal error for now, only warnings");
    registry.turnEventIntoError(LICENSE_FILE_NOT_FOUND);
    BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
    BOOST_CHECK_EQUAL(0, licenseInfos.size());
    BOOST_CHECK_MESSAGE(
            registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND,
            "Error detected");
}
/*****************************************************************************
 * EnvironmentVarLocation tests
 *****************************************************************************/
BOOST_AUTO_TEST_CASE( environment_var_location ) {
    //an application can define multiple license locations separated by ';'
    const char *environment_variable_value =
    MOCK_LICENSE ";/this/one/doesnt/exist";
#ifdef _WIN32
    _putenv_s(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
#else
    setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
#endif
    //read test license
    std::ifstream src(MOCK_LICENSE, std::ios::binary);
    std::string referenceContent((std::istreambuf_iterator<char>(src)),
            std::istreambuf_iterator<char>());
    license::EventRegistry registry;
    EnvironmentVarLocation envVarLocationStrategy;
    vector<string> licenseInfos = envVarLocationStrategy.licenseLocations(
            registry);
    BOOST_CHECK(registry.isGood());
    BOOST_CHECK_EQUAL(1, licenseInfos.size());
    string currentLocation = licenseInfos[0];
    BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0,
            "file found at expected location");
    string licenseRealContent = envVarLocationStrategy.retrieveLicense(
            currentLocation);
    BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
            "File content is same");
#ifdef _WIN32
    _putenv_s(LICENSE_LOCATION_ENV_VAR, "");
#else
    unsetenv(LICENSE_LOCATION_ENV_VAR);
#endif
}
/**
 * The license file doesn't exist. Check that the locator reports the right error
 */
BOOST_AUTO_TEST_CASE( environment_var_location_not_found ) {
    const char *environment_variable_value =
    PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
#ifdef _WIN32
    _putenv_s(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
#else
    setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
#endif
    license::EventRegistry registry;
    EnvironmentVarLocation envVarLocationStrategy;
    vector<string> licenseInfos = envVarLocationStrategy.licenseLocations(
            registry);
    BOOST_CHECK_MESSAGE(registry.isGood(),
            "No fatal error for now, only warnings");
    registry.turnEventIntoError(LICENSE_FILE_NOT_FOUND);
    BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
    BOOST_CHECK_EQUAL(0, licenseInfos.size());
    BOOST_CHECK_MESSAGE(
            registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND,
            "Error detected");
#ifdef _WIN32
    _putenv_s(LICENSE_LOCATION_ENV_VAR, "");
#else
    unsetenv(LICENSE_LOCATION_ENV_VAR);
#endif
}
/**
 * The license file doesn't exist. Check that the locator reports the right error
 */
BOOST_AUTO_TEST_CASE( environment_var_location_not_defined ) {
#ifdef _WIN32
    _putenv_s(LICENSE_LOCATION_ENV_VAR, "");
#else
    setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
#endif
    license::EventRegistry registry;
    EnvironmentVarLocation environmentVarLocation;
    vector<string> licenseInfos = environmentVarLocation.licenseLocations(
            registry);
    BOOST_CHECK_MESSAGE(registry.isGood(),
            "No fatal error for now, only warnings");
    registry.turnEventIntoError(ENVIRONMENT_VARIABLE_NOT_DEFINED);
    BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
    BOOST_CHECK_EQUAL(0, licenseInfos.size());
    BOOST_CHECK_MESSAGE(
            registry.getLastFailure()->event_type
                    == ENVIRONMENT_VARIABLE_NOT_DEFINED, "Error detected");
}
}  //namespace test
test/library/LicenseReader_test.cpp
@@ -1,16 +1,16 @@
#define BOOST_TEST_MODULE license_reader_test
//#define BOOST_TEST_MAIN
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "license_reader_test"
#include <boost/test/unit_test.hpp>
#include "../../src/library/LicenseReader.h"
#include <build_properties.h>
#include <stdlib.h>
#include <build_properties.h>
#include "../../src/library/LicenseReader.hpp"
using namespace license;
BOOST_AUTO_TEST_CASE( read_single_file ) {
    const char * licLocation =
    PROJECT_TEST_SRC_DIR "/library/test_reader.ini";
    const char *licLocation = PROJECT_TEST_SRC_DIR  "/library/test_reader.ini";
    const LicenseLocation location = { licLocation, nullptr, false };
    LicenseReader licenseReader(location);
    vector<FullLicenseInfo> licenseInfos;
@@ -62,18 +62,5 @@
    BOOST_ASSERT(registry.getLastFailure()!=NULL);
    BOOST_CHECK_EQUAL(ENVIRONMENT_VARIABLE_NOT_DEFINED,
            registry.getLastFailure()->event_type);
}
BOOST_AUTO_TEST_CASE( read_env_var ) {
    char str[MAX_PATH];
    strcpy(str,"LIC_VAR=" PROJECT_TEST_SRC_DIR "/library/test_reader.ini");
    putenv(str);
    const LicenseLocation location = {nullptr, "LIC_VAR", false };
    LicenseReader licenseReader(location);
    vector<FullLicenseInfo> licenseInfos;
    const EventRegistry registry = licenseReader.readLicenses("PrODUCT",
            licenseInfos);
    BOOST_CHECK(registry.isGood());
    BOOST_CHECK_EQUAL(1, licenseInfos.size());
}
test/license-generator/license-generator_test.cpp
@@ -2,9 +2,10 @@
//#define BOOST_TEST_MAIN
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "../../src/tools/license-generator/license-generator.h"
#include <build_properties.h>
#include <boost/filesystem.hpp>
#include <build_properties.h>
#include "../../src/tools/license-generator/license-generator.h"
#include "../../src/library/ini/SimpleIni.h"
namespace fs = boost::filesystem;