refactoring - locators
separated the way we retrieve license from the validation part
24个文件已修改
16个文件已添加
1 文件已重命名
1个文件已删除
| | |
| | | .settings |
| | | .project |
| | | *.out |
| | | /Default/ |
| | |
| | | #cmake_minimum_required(VERSION 3.0) |
| | | cmake_minimum_required(VERSION 2.8.11) |
| | | |
| | | link_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/../install/lib" ) |
| | |
| | | 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) |
| | | |
| | |
| | | |
| | | 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) |
| | |
| | | #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 |
| | |
| | | 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/) |
| | |
| | | #else |
| | | # include <unistd.h> |
| | | #endif |
| | | |
| | | #include <cstring> |
| | | #include <ctime> |
| | | #include <vector> |
| | |
| | | #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 { |
| | | |
| | |
| | | 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; |
| | |
| | | "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 { |
File was renamed from src/library/LicenseReader.h |
| | |
| | | #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> |
| | |
| | | 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 |
| | |
| | | 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, |
| | |
| | | #define DllExport __declspec( dllexport ) |
| | | #endif |
| | | |
| | | #define ENVIRONMENT_VAR_NAME_MAX 64 |
| | | #define PC_IDENTIFIER_SIZE 18 |
| | | #define PROPRIETARY_DATA_SIZE 16 |
| | | |
| | |
| | | */ |
| | | 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, |
| | |
| | | /** |
| | | * 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 |
| | |
| | | ADD_LIBRARY(base STATIC |
| | | EventRegistry.cpp |
| | | StringUtils.cpp |
| | | FileUtils.cpp |
| | | logger.c |
| | | ) |
| | | |
| | |
| | | #include <string.h> |
| | | #include <algorithm> |
| | | |
| | | namespace license { |
| | | using namespace std; |
| | | |
| | | namespace license { |
| | | EventRegistry::EventRegistry() { |
| | | } |
| | | |
| | |
| | | #include <string> |
| | | |
| | | namespace license { |
| | | using namespace std; |
| | | |
| | | /* |
| | | AuditEvent error_event_builder(EVENT_TYPE event); |
| | | AuditEvent audit_event_builder(EVENT_TYPE event, SEVERITY severity); |
| | |
| | | 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: |
| | |
| | | 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); |
| | | |
| | | }; |
New file |
| | |
| | | /* |
| | | * 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); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | /* |
| | | * 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 |
| | |
| | | #include "StringUtils.h" |
| | | #include <iostream> |
| | | #include <string> |
| | | #include <sstream> |
| | | #include <cstring> |
| | | #include <algorithm> |
| | | #include <stdexcept> |
| | |
| | | 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 */ |
| | |
| | | #ifndef STRINGUTILS_H_ |
| | | #define STRINGUTILS_H_ |
| | | #include <string> |
| | | #include <vector> |
| | | |
| | | namespace license { |
| | | using namespace std; |
| | |
| | | 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_ */ |
| | |
| | | #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) { |
New file |
| | |
| | | /* |
| | | * 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 */ |
New file |
| | |
| | | /* |
| | | * 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_ */ |
New file |
| | |
| | | |
| | | 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 |
| | | ) |
| | | |
New file |
| | |
| | | /* |
| | | * 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 ""; |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | /* |
| | | * 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 |
New file |
| | |
| | | /* |
| | | * 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; |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | /* |
| | | * 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_ */ |
New file |
| | |
| | | /* |
| | | * 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 */ |
New file |
| | |
| | | /* |
| | | * 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_ */ |
New file |
| | |
| | | /* |
| | | * 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; |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | #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 |
New file |
| | |
| | | /* |
| | | * 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); |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | #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 |
| | |
| | | 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}> |
| | |
| | | #define LICENSE_GENERATOR_H_ |
| | | |
| | | #include <boost/program_options.hpp> |
| | | #include "../../library/LicenseReader.h" |
| | | |
| | | #include "../../library/LicenseReader.hpp" |
| | | |
| | | namespace license { |
| | | |
| | |
| | | |
| | | target_link_libraries( |
| | | standard_license_test |
| | | licensepp_static |
| | | licensecc_static |
| | | license_generator_snippet |
| | | ${Boost_LIBRARIES} |
| | | ) |
| | |
| | | |
| | | target_link_libraries( |
| | | date_test |
| | | licensepp_static |
| | | licensecc_static |
| | | license_generator_snippet |
| | | ${Boost_LIBRARIES} |
| | | ) |
| | |
| | | |
| | | target_link_libraries( |
| | | volid_test |
| | | licensepp_static |
| | | licensecc_static |
| | | license_generator_snippet |
| | | ${Boost_LIBRARIES} |
| | | ) |
| | |
| | | #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"); |
| | |
| | | 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); |
| | |
| | | |
| | | 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"); |
| | |
| | | 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); |
| | | } |
| | | |
| | | } |
| | |
| | | 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; |
| | |
| | | |
| | | |
| | | #include<string> |
| | | #include<vector> |
| | | |
| | |
| | | //#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" |
| | |
| | | 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"); |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | |
| | | target_link_libraries( |
| | | license_reader_test |
| | | licensepp_static |
| | | licensecc_static |
| | | ${Boost_LIBRARIES} |
| | | ) |
| | | |
| | |
| | | |
| | | 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() |
New file |
| | |
| | | #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 |
| | |
| | | #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; |
| | |
| | | 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()); |
| | | } |
| | | |
| | |
| | | //#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; |