From 79b1b57045e86d9845e352c3f2aa0efbab6111a9 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 14 3月 2020 10:19:09 +0800 Subject: [PATCH] tests and improvements --- src/library/os/linux/execution_environment.cpp | 16 ++ src/library/hw_identifier/hw_identifier_facade.hpp | 6 + src/library/os/execution_environment_common.cpp | 22 ++- src/library/hw_identifier/hw_identifier_facade.cpp | 2 src/library/hw_identifier/identification_strategy.cpp | 1 src/inspector/inspector.cpp | 46 ++++++--- test/library/os/execution_environment_test.cpp | 48 +++++++++ src/library/os/linux/os-linux.c | 20 --- CMakeLists.txt | 20 ++- src/library/os/windows/execution_environment.cpp | 6 test/library/os/CMakeLists.txt | 13 ++ src/library/hw_identifier/disk_strategy.cpp | 37 +++--- src/library/os/execution_environment.hpp | 14 +- src/library/os/cpu_info_common.cpp | 1 test/functional/CMakeLists.txt | 1 src/library/os/windows/signature_verifier.cpp | 2 .travis.yml | 10 + src/library/hw_identifier/ethernet.cpp | 2 test/library/Os_Linux_test.cpp | 19 --- src/library/base/StringUtils.cpp | 2 test/library/os/cpu_info_test.cpp | 0 21 files changed, 183 insertions(+), 105 deletions(-) diff --git a/.travis.yml b/.travis.yml index cac6349..73b9f8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,8 @@ - os: linux dist: bionic name: "Ubuntu 18.04 - Travis CI VM" + env: + - VIRTUAL_ENV=VM addons: apt: packages: @@ -51,6 +53,8 @@ - os: linux dist: bionic name: "Ubuntu 18.04 - Travis CI VM - Debug build" + env: + - VIRTUAL_ENV=VM addons: apt: packages: @@ -72,7 +76,6 @@ script: - cmake --build . --target install - ctest - - ctest -T memcheck after_success: # Create lcov report - lcov --capture --directory . --output-file coverage.info @@ -80,7 +83,6 @@ - lcov --list coverage.info # debug info # Uploading report to CodeCov - bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports" - - os: linux dist: bionic name: "CentOS-7 Docker" @@ -110,7 +112,7 @@ - docker commit centos8_toconfigure centos8_configured script: - docker run --name centos8_make -v `pwd`:/root/open-license-manager centos8_configured /bin/bash -c \ - "cd /root/open-license-manager/build && make && make install && VIRT_ENV=CONTAINER make test" + "cd /root/open-license-manager/build && make && make install && VIRTUAL_ENV=DOCKER make test" - os: linux dist: bionic name: "Ubuntu 18.04 - MinGW cross-compile for Windows x64" @@ -218,7 +220,7 @@ on: branch: develop env: - - CTEST_OUTPUT_ON_FAILURE=1 + - CTEST_OUTPUT_ON_FAILURE=1 before_script: - cd build && cmake -DCMAKE_INSTALL_PREFIX=../../install .. diff --git a/CMakeLists.txt b/CMakeLists.txt index 3edd0cc..3cb66f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,24 +40,18 @@ ENDIF(${STATIC_RUNTIME}) find_package(OpenSSL COMPONENTS Crypto QUIET) IF(OPENSSL_FOUND) - MESSAGE(STATUS "Found openssl version ${OPENSSL_VERSION}") + MESSAGE(STATUS "Found openssl version ${OPENSSL_VERSION} -licensecc") ENDIF(OPENSSL_FOUND) if(UNIX) #this is true for all the linux systems but not for cross compiling "linux->windows" IF(${STATIC_RUNTIME}) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static") + #SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") ENDIF(${STATIC_RUNTIME}) IF(NOT OPENSSL_FOUND) MESSAGE(SEND_ERROR "Openssl required in Linux, please install it or specify -DOPENSSL_ROOT") ENDIF(NOT OPENSSL_FOUND) - #Zlib required when openssl version < 1.0.1f (centos 7) - IF(OPENSSL_VERSION VERSION_LESS 1.0.2) - SET ( ZLIB_USE_STATIC_LIBS ON ) - find_package(ZLIB REQUIRED) - list(APPEND EXTERNAL_LIBS ${ZLIB_LIBRARIES}) - MESSAGE(STATUS "Found zlib version ${ZLIB_VERSION_STRING} - licensecc") - ENDIF(OPENSSL_VERSION VERSION_LESS 1.0.2) find_package(Threads) if(THREADS_HAVE_PTHREAD_ARG) list(APPEND EXTERNAL_LIBS "-pthread") @@ -66,6 +60,16 @@ list(APPEND EXTERNAL_LIBS "${CMAKE_THREAD_LIBS_INIT}") endif() endif(THREADS_HAVE_PTHREAD_ARG) + + #Zlib required when openssl version < 1.0.1f (centos 7) + IF(OPENSSL_VERSION VERSION_LESS_EQUAL 1.0.2) + SET ( ZLIB_USE_STATIC_LIBS ON ) + find_package(ZLIB REQUIRED) + list(APPEND EXTERNAL_LIBS ${ZLIB_LIBRARIES}) + MESSAGE(STATUS "Found zlib version ${ZLIB_VERSION_STRING} - licensecc") + ELSE(OPENSSL_VERSION VERSION_LESS_EQUAL 1.0.2) + MESSAGE(STATUS "openssl ${OPENSSL_VERSION} zlib not required - licensecc") + ENDIF(OPENSSL_VERSION VERSION_LESS_EQUAL 1.0.2) find_program( MEMORYCHECK_COMMAND valgrind ) set( MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full" ) diff --git a/src/inspector/inspector.cpp b/src/inspector/inspector.cpp index d99b276..e5ee64d 100644 --- a/src/inspector/inspector.cpp +++ b/src/inspector/inspector.cpp @@ -4,21 +4,28 @@ #include <licensecc/licensecc.h> #include <fstream> #include "../library/os/cpu_info.hpp" +#include "../library/os/execution_environment.hpp" using namespace std; +using namespace license::os; -struct enum_hash { - template <typename T> - constexpr typename std::enable_if<std::is_enum<T>::value, std::size_t>::type operator()(T s) const noexcept { - return static_cast<std::size_t>(s); - } -}; +const map<int, string> stringByStrategyId = {{STRATEGY_DEFAULT, "DEFAULT"}, {STRATEGY_ETHERNET, "MAC"}, + {STRATEGY_IP_ADDRESS, "IP"}, {STRATEGY_DISK_NUM, "Disk1"}, + {STRATEGY_DISK_LABEL, "Disk2"}, {STRATEGY_NONE, "Custom"}}; -const map<LCC_API_IDENTIFICATION_STRATEGY, string, enum_hash> stringByStrategyId = { - {STRATEGY_DEFAULT, "DEFAULT"}, {STRATEGY_ETHERNET, "MAC"}, {STRATEGY_IP_ADDRESS, "IP"}, - {STRATEGY_DISK_NUM, "Disk1"}, {STRATEGY_DISK_LABEL, "Disk2"}, {STRATEGY_NONE, "Custom"}}; -const unordered_map<LCC_EVENT_TYPE, string, enum_hash> stringByEventType = { +const unordered_map<int, string> descByVirtDetail = {{BARE_TO_METAL, "No virtualization"}, + {VMWARE, "Vmware"}, + {VIRTUALBOX, "Virtualbox"}, + {V_XEN, "XEN"}, + {KVM, "KVM"}, + {HV, "Microsoft Hypervisor"}, + {V_OTHER, "Other type of vm"}}; + +const unordered_map<int, string> descByVirt = { + {VIRTUALIZATION::NONE, "No virtualization"}, {VIRTUALIZATION::VM, "VM"}, {VIRTUALIZATION::CONTAINER, "Container"}}; + +const unordered_map<int, string> stringByEventType = { {LICENSE_OK, "OK "}, {LICENSE_FILE_NOT_FOUND, "license file not found "}, {LICENSE_SERVER_NOT_FOUND, "license server can't be contacted "}, @@ -46,16 +53,25 @@ int main(int argc, char* argv[]) { license::os::CpuInfo cpu; - cout << "CpuVendor :" << cpu.vendor() << endl; - cout << "Virtual machine:" << cpu.cpu_virtual() << endl; - cout << "Cpu model : 0x" << std::hex << ((long)cpu.model()) << std::dec << endl; + cout << "CpuVendor :" << cpu.vendor() << endl; + cout << "Virtual machine :" << cpu.cpu_virtual() << endl; + cout << "Cpu model :0x" << std::hex << ((long)cpu.model()) << std::dec << endl; + cout << "Virt. detail cpu:" << descByVirtDetail.find(cpu.getVirtualizationDetail())->second << endl; + ExecutionEnvironment execEnv; + cout << "Running in cloud:" << execEnv.is_cloud() << endl; + cout << "Docker :" << execEnv.is_docker() << endl; + cout << "other container :" << execEnv.is_container() << endl; + cout << "Virtualiz. class:" << descByVirt.find(execEnv.getVirtualization())->second << endl; + + cout << "Bios vendor :" << execEnv.bios_vendor() << endl; + cout << "Bios description:" << execEnv.bios_description() << endl; + cout << "System vendor :" << execEnv.sys_vendor() << endl; char hw_identifier[LCC_API_PC_IDENTIFIER_SIZE + 1]; size_t bufSize = LCC_API_PC_IDENTIFIER_SIZE + 1; for (const auto& x : stringByStrategyId) { - if (identify_pc(x.first, hw_identifier, &bufSize)) { + if (identify_pc(static_cast<LCC_API_IDENTIFICATION_STRATEGY>(x.first), hw_identifier, &bufSize)) { std::cout << x.second << ':' << hw_identifier << std::endl; - } else { std::cout << x.second << ": NA" << endl; } diff --git a/src/library/base/StringUtils.cpp b/src/library/base/StringUtils.cpp index b09cdfe..1d5d27b 100644 --- a/src/library/base/StringUtils.cpp +++ b/src/library/base/StringUtils.cpp @@ -28,7 +28,7 @@ ++it; } std::string::const_reverse_iterator rit = string_to_trim.rbegin(); - while (rit.base() != it && isspace(*rit)) { + while (rit.base() != it && (isspace(*rit) || *rit == 0)) { ++rit; } return std::string(it, rit.base()); diff --git a/src/library/hw_identifier/disk_strategy.cpp b/src/library/hw_identifier/disk_strategy.cpp index 4fe212e..d9f6831 100644 --- a/src/library/hw_identifier/disk_strategy.cpp +++ b/src/library/hw_identifier/disk_strategy.cpp @@ -17,7 +17,6 @@ size_t disk_num = 0; size_t available_disk_info = 0; FUNCTION_RETURN result_diskinfos; - unsigned int i; result_diskinfos = getDiskInfos(nullptr, &disk_num); if (result_diskinfos != FUNC_RET_OK && result_diskinfos != FUNC_RET_BUFFER_TOO_SMALL) { @@ -33,12 +32,12 @@ } memset(diskInfos, 0, mem); result_diskinfos = getDiskInfos(diskInfos, &disk_num); - + if (result_diskinfos != FUNC_RET_OK) { free(diskInfos); return result_diskinfos; } - for (i = 0; i < disk_num; i++) { + for (unsigned int i = 0; i < disk_num; i++) { char firstChar = use_id ? diskInfos[i].label[0] : diskInfos[i].disk_sn[0]; available_disk_info += firstChar == 0 ? 0 : 1; } @@ -47,20 +46,22 @@ return FUNC_RET_NOT_AVAIL; } v_disk_id.reserve(available_disk_info); - //FIXME use preferred drive. - for (i = 0; i < disk_num; i++) { - array<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> a_disk_id; - a_disk_id.fill(0); - if (use_id) { - if (diskInfos[i].disk_sn[0] != 0) { - size_t size = min(a_disk_id.size(), sizeof(&diskInfos[i].disk_sn)); - memcpy(&a_disk_id[0], diskInfos[i].disk_sn, size); - v_disk_id.push_back(a_disk_id); - } - } else { - if (diskInfos[i].label[0] != 0) { - strncpy((char *)&a_disk_id[0], diskInfos[i].label, a_disk_id.size()-1); - v_disk_id.push_back(a_disk_id); + for (int j = 0; j < 2; j++) { + int preferred = (j == 0 ? 1 : 0); + for (unsigned int i = 0; i < disk_num; i++) { + array<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> a_disk_id; + a_disk_id.fill(0); + if (use_id) { + if (diskInfos[i].disk_sn[0] != 0 && diskInfos[i].preferred == preferred) { + size_t size = min(a_disk_id.size(), sizeof(&diskInfos[i].disk_sn)); + memcpy(&a_disk_id[0], diskInfos[i].disk_sn, size); + v_disk_id.push_back(a_disk_id); + } + } else { + if (diskInfos[i].label[0] != 0 && diskInfos[i].preferred == preferred) { + strncpy((char *)&a_disk_id[0], diskInfos[i].label, a_disk_id.size() - 1); + v_disk_id.push_back(a_disk_id); + } } } } @@ -81,7 +82,7 @@ FUNCTION_RETURN result = generate_disk_pc_id(data, m_use_id); vector<HwIdentifier> identifiers; if (result == FUNC_RET_OK) { - identifiers.resize(data.size()); + identifiers.reserve(data.size()); for (auto &it : data) { HwIdentifier pc_id; pc_id.set_identification_strategy(identification_strategy()); diff --git a/src/library/hw_identifier/ethernet.cpp b/src/library/hw_identifier/ethernet.cpp index ce1d6de..bd21bc9 100644 --- a/src/library/hw_identifier/ethernet.cpp +++ b/src/library/hw_identifier/ethernet.cpp @@ -63,7 +63,7 @@ FUNCTION_RETURN result = generate_ethernet_pc_id(data, use_ip); vector<HwIdentifier> identifiers; if (result == FUNC_RET_OK) { - identifiers.resize(data.size()); + identifiers.reserve(data.size()); for (auto &it : data) { HwIdentifier pc_id; pc_id.set_identification_strategy(identification_strategy()); diff --git a/src/library/hw_identifier/hw_identifier_facade.cpp b/src/library/hw_identifier/hw_identifier_facade.cpp index 755eb75..c7a2ec6 100644 --- a/src/library/hw_identifier/hw_identifier_facade.cpp +++ b/src/library/hw_identifier/hw_identifier_facade.cpp @@ -55,7 +55,7 @@ HwIdentifier pc_id; FUNCTION_RETURN result = strategy_ptr->identify_pc(pc_id); if (result != FUNC_RET_OK) { - /// FIXME + throw logic_error("strategy " + to_string(strategy_ptr->identification_strategy()) + " failed"); } os::ExecutionEnvironment exec; os::VIRTUALIZATION virtualization = exec.getVirtualization(); diff --git a/src/library/hw_identifier/hw_identifier_facade.hpp b/src/library/hw_identifier/hw_identifier_facade.hpp index bd198b3..c587c53 100644 --- a/src/library/hw_identifier/hw_identifier_facade.hpp +++ b/src/library/hw_identifier/hw_identifier_facade.hpp @@ -23,6 +23,12 @@ virtual ~HwIdentifierFacade(){}; public: static LCC_EVENT_TYPE validate_pc_signature(const std::string& str_code); + /** + * + * @throws logic_error if pc_identifier can't be generated. + * @param strategy + * @return + */ static std::string generate_user_pc_signature(LCC_API_IDENTIFICATION_STRATEGY strategy); }; diff --git a/src/library/hw_identifier/identification_strategy.cpp b/src/library/hw_identifier/identification_strategy.cpp index 49ed768..273c670 100644 --- a/src/library/hw_identifier/identification_strategy.cpp +++ b/src/library/hw_identifier/identification_strategy.cpp @@ -32,6 +32,7 @@ } return result; } + std::unique_ptr<IdentificationStrategy> IdentificationStrategy::get_strategy(LCC_API_IDENTIFICATION_STRATEGY strategy) { unique_ptr<IdentificationStrategy> result; switch (strategy) { diff --git a/src/library/os/cpu_info_common.cpp b/src/library/os/cpu_info_common.cpp index 119b77e..96ac6c6 100644 --- a/src/library/os/cpu_info_common.cpp +++ b/src/library/os/cpu_info_common.cpp @@ -29,6 +29,7 @@ } return is_virtual; } + VIRTUALIZATION_DETAIL CpuInfo::getVirtualizationDetail() const { string cpu_vendor = vendor(); auto it = virtual_cpu_names.find(cpu_vendor); diff --git a/src/library/os/execution_environment.hpp b/src/library/os/execution_environment.hpp index db36205..97172e4 100644 --- a/src/library/os/execution_environment.hpp +++ b/src/library/os/execution_environment.hpp @@ -38,11 +38,9 @@ class ExecutionEnvironment { private: - std::string sys_vendor; - std::string bios_vendor; - std::string bios_description; - // detect if it's a kind of container technology (docker or lxc) - bool is_container() const; + std::string m_sys_vendor; + std::string m_bios_vendor; + std::string m_bios_description; public: ExecutionEnvironment(); @@ -50,7 +48,13 @@ VIRTUALIZATION getVirtualization() const; bool is_cloud() const; bool is_docker() const; + // detect if it's a kind of container technology (docker or lxc) + bool is_container() const; CLOUD_PROVIDER getCloudProvider() const; + const std::string& bios_vendor() const { return m_bios_vendor; }; + const std::string& sys_vendor() const { return m_sys_vendor; }; + const std::string& bios_description() const { return m_bios_description; }; + // VIRTUALIZATION_DETAIL getVirtualizationDetail() const; //as reported by the bios }; } // namespace os diff --git a/src/library/os/execution_environment_common.cpp b/src/library/os/execution_environment_common.cpp index 9fa2a38..06f5c56 100644 --- a/src/library/os/execution_environment_common.cpp +++ b/src/library/os/execution_environment_common.cpp @@ -31,22 +31,28 @@ return result; } -bool ExecutionEnvironment::is_cloud() const { return getCloudProvider() != ON_PREMISE; } +bool ExecutionEnvironment::is_cloud() const { + CLOUD_PROVIDER prov = getCloudProvider(); + return prov != ON_PREMISE && prov != PROV_UNKNOWN; +} // TODO test and azure CLOUD_PROVIDER ExecutionEnvironment::getCloudProvider() const { CLOUD_PROVIDER result = PROV_UNKNOWN; - if (bios_description.size() > 0 || bios_vendor.size() > 0 || sys_vendor.size() > 0) { - if (bios_vendor.find("SEABIOS") != string::npos || bios_description.find("ALIBABA") != string::npos || - sys_vendor.find("ALIBABA") != string::npos) { + if (m_bios_description.size() > 0 || m_bios_vendor.size() > 0 || m_sys_vendor.size() > 0) { + if (m_bios_vendor.find("SEABIOS") != string::npos || m_bios_description.find("ALIBABA") != string::npos || + m_sys_vendor.find("ALIBABA") != string::npos) { result = ALI_CLOUD; - } else if (sys_vendor.find("GOOGLE") != string::npos || bios_description.find("GOOGLE") != string::npos) { + } else if (m_sys_vendor.find("GOOGLE") != string::npos || m_bios_description.find("GOOGLE") != string::npos) { result = GOOGLE_CLOUD; - } else if (bios_vendor.find("AWS") != string::npos || bios_description.find("AMAZON") != string::npos || - sys_vendor.find("AWS") != string::npos) { + } else if (m_bios_vendor.find("AWS") != string::npos || m_bios_description.find("AMAZON") != string::npos || + m_sys_vendor.find("AWS") != string::npos) { result = AWS; + } else if (m_bios_description.find("HP-COMPAQ") != string::npos || m_bios_description.find("ASUS") || + m_bios_description.find("DELL")) { + result = ON_PREMISE; } - } + } return result; } } // namespace os diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp index 9aad864..1f14d62 100644 --- a/src/library/os/linux/execution_environment.cpp +++ b/src/library/os/linux/execution_environment.cpp @@ -18,6 +18,7 @@ #include "../cpu_info.hpp" #include "../execution_environment.hpp" #include "../../base/file_utils.hpp" +#include "../../base/StringUtils.h" namespace license { namespace os { @@ -84,15 +85,24 @@ ExecutionEnvironment::ExecutionEnvironment() { try { - bios_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); + m_bios_vendor = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/sys_vendor", 256))); + } catch (...) { } try { - bios_description = get_file_contents("/sys/class/dmi/id/modalias", 256); + m_bios_description = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/modalias", 256))); + char last_char = m_bios_description[m_bios_description.length() - 1]; + if (last_char == '\r' || last_char == '\n') { + m_bios_description = m_bios_description.erase(m_bios_description.length() - 1); + } } catch (...) { } try { - sys_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); + m_sys_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); + char last_char = m_sys_vendor[m_sys_vendor.length() - 2]; + if (last_char == '\r' || last_char == '\n') { + m_sys_vendor = m_sys_vendor.erase(m_sys_vendor.length() - 1); + } } catch (...) { } } diff --git a/src/library/os/linux/os-linux.c b/src/library/os/linux/os-linux.c index 1dc23cb..57fb028 100644 --- a/src/library/os/linux/os-linux.c +++ b/src/library/os/linux/os-linux.c @@ -111,7 +111,9 @@ if (strcmp(ent->mnt_dir, "/") == 0) { strcpy(tmpDrives[drive_found].label, "root"); LOG_DEBUG("drive %s set to preferred\n", ent->mnt_fsname); - tmpDrives[drive_found].preferred = true; + tmpDrives[drive_found].preferred = 1; + } else { + tmpDrives[drive_found].preferred = 0; } } } @@ -169,22 +171,6 @@ } else { result = FUNC_RET_BUFFER_TOO_SMALL; } - /* - FILE *mounts = fopen(_PATH_MOUNTED, "r"); - if (mounts == NULL) { - return ERROR; - } - - while (fscanf(mounts, "%64s %64s %64s %1024[^\n]", device, name, type, - discard) != EOF) { - if (stat(device, &mount_stat) != 0) - continue; - if (filename_stat.st_dev == mount_stat.st_rdev) { - fprintf(stderr, "device: %s; name: %s; type: %s\n", device, name, - type); - } - } - */ free(statDrives); return result; } diff --git a/src/library/os/windows/execution_environment.cpp b/src/library/os/windows/execution_environment.cpp index 86513d5..03c789e 100644 --- a/src/library/os/windows/execution_environment.cpp +++ b/src/library/os/windows/execution_environment.cpp @@ -25,9 +25,9 @@ if (InitEntryPoints()) { BIOSReader reader; SystemInformation info = reader.readSystemInfo(); - sys_vendor = toupper_copy(info.Manufacturer); - bios_vendor = toupper_copy(info.ProductName); - bios_description = toupper_copy(info.SysVersion) + toupper_copy(info.family); + m_sys_vendor = toupper_copy(info.Manufacturer); + m_bios_vendor = toupper_copy(info.ProductName); + m_bios_description = toupper_copy(info.SysVersion) + toupper_copy(info.family); } } diff --git a/src/library/os/windows/signature_verifier.cpp b/src/library/os/windows/signature_verifier.cpp index a47c7dd..5e12727 100644 --- a/src/library/os/windows/signature_verifier.cpp +++ b/src/library/os/windows/signature_verifier.cpp @@ -139,7 +139,7 @@ BCRYPT_ALG_HANDLE hSignAlg = nullptr; vector<uint8_t> signatureBlob = unbase64(signatureBuffer); - DWORD dwSigLen = signatureBlob.size(); + DWORD dwSigLen = (DWORD) signatureBlob.size(); BYTE* sigBlob = &signatureBlob[0]; if (NT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hSignAlg, BCRYPT_RSA_ALGORITHM, NULL, 0))) { diff --git a/test/functional/CMakeLists.txt b/test/functional/CMakeLists.txt index bd64037..6d2397b 100644 --- a/test/functional/CMakeLists.txt +++ b/test/functional/CMakeLists.txt @@ -40,7 +40,6 @@ test_signature_verifier licensecc_static license_generator_snippet - ${EXTERNAL_LIBS} Boost::unit_test_framework Boost::filesystem Boost::system diff --git a/test/library/Os_Linux_test.cpp b/test/library/Os_Linux_test.cpp index 5306bb0..527da44 100644 --- a/test/library/Os_Linux_test.cpp +++ b/test/library/Os_Linux_test.cpp @@ -39,24 +39,5 @@ } } -// To test if virtualization is detected correctly define an env variable VIRT_ENV -// otherwise the test is skipped -BOOST_AUTO_TEST_CASE(test_virtualization) { - const char *env = getenv("VIRT_ENV"); - os::ExecutionEnvironment exec_env; - if (env != NULL) { - if (strcmp(env, "CONTAINER") == 0) { - os::VIRTUALIZATION virt = exec_env.getVirtualization(); - BOOST_CHECK_MESSAGE(virt == VIRTUALIZATION::CONTAINER, "container detected"); - } else if (strcmp(env, "VM") == 0) { - BOOST_FAIL("check for vm not implemented"); - } else if (strcmp(env, "NONE") == 0) { - os::VIRTUALIZATION virt = exec_env.getVirtualization(); - BOOST_CHECK_EQUAL(virt, VIRTUALIZATION::NONE); - } else { - BOOST_FAIL(string("value ") + env + " not supported: VM,CONTAINER,NONE"); - } - } -} } // namespace test } // namespace license diff --git a/test/library/os/CMakeLists.txt b/test/library/os/CMakeLists.txt index 4faf092..1af4e92 100644 --- a/test/library/os/CMakeLists.txt +++ b/test/library/os/CMakeLists.txt @@ -11,6 +11,19 @@ ADD_TEST(NAME test_network COMMAND test_network) +add_executable( test_execution_environment + execution_environment_test.cpp +) + +target_link_libraries( test_execution_environment + licensecc_static + Boost::unit_test_framework + Boost::filesystem + Boost::system +) + +ADD_TEST(NAME test_execution_environment COMMAND test_execution_environment) if(CODE_COVERAGE AND UNIX) target_compile_options(test_network PUBLIC -O0 -g --coverage) + target_compile_options(test_execution_environment PUBLIC -O0 -g --coverage) endif(CODE_COVERAGE AND UNIX) diff --git a/test/library/cpu_info_test.cpp b/test/library/os/cpu_info_test.cpp similarity index 100% rename from test/library/cpu_info_test.cpp rename to test/library/os/cpu_info_test.cpp diff --git a/test/library/os/execution_environment_test.cpp b/test/library/os/execution_environment_test.cpp new file mode 100644 index 0000000..1ca14b0 --- /dev/null +++ b/test/library/os/execution_environment_test.cpp @@ -0,0 +1,48 @@ +#define BOOST_TEST_MODULE network_test +#include <string> +#include <iostream> +#include <boost/test/unit_test.hpp> + +#include <licensecc_properties.h> +#include <licensecc_properties_test.h> +#include "../../src/library/base/StringUtils.h" +#include "../../src/library/os/network.hpp" +#include "../../src/library/os/execution_environment.hpp" + +namespace license { +namespace os { +namespace test { + +using namespace license::os; +using namespace std; + +// To test if virtualization is detected correctly define an env variable VIRT_ENV +// otherwise the test is skipped +BOOST_AUTO_TEST_CASE(test_virtualization) { + const char *env = getenv("VIRTUAL_ENV"); + os::ExecutionEnvironment exec_env; + bool docker = false; + if (env != nullptr) { + os::VIRTUALIZATION virt = exec_env.getVirtualization(); + if (strcmp(env, "CONTAINER") == 0 || (docker = (strcmp(env, "DOCKER") == 0))) { + BOOST_CHECK_MESSAGE(virt == VIRTUALIZATION::CONTAINER, "container detected"); + BOOST_CHECK_MESSAGE(exec_env.is_container(), "container detected"); + if (docker) { + BOOST_CHECK_MESSAGE(exec_env.is_docker(), "docker detected"); + } + } else if (strcmp(env, "VM") == 0) { + BOOST_CHECK_MESSAGE(virt == VIRTUALIZATION::VM, "VM detected"); + BOOST_CHECK_MESSAGE(!exec_env.is_container(), "VM is not a container"); + BOOST_CHECK_MESSAGE(!exec_env.is_docker(), "VM is not a docker"); + } else if (strcmp(env, "NONE") == 0) { + BOOST_CHECK_EQUAL(virt, VIRTUALIZATION::NONE); + BOOST_CHECK_MESSAGE(!exec_env.is_container(), "not a container"); + BOOST_CHECK_MESSAGE(!exec_env.is_docker(), "not a docker"); + } else { + BOOST_FAIL(string("value ") + env + " not supported: VM,DOCKER,CONTAINER,NONE"); + } + } +} +} // namespace test +} // namespace os +} // namespace license -- Gitblit v1.9.1