From 7c79e1cd6962830af779d201f9bfab7747f1b807 Mon Sep 17 00:00:00 2001 From: sangsu Date: Fri, 1 Apr 2016 14:11:37 +0900 Subject: [PATCH] Add engine library loader classes Change-Id: Iaf17a44a9e120e8ae0587fae839be20da6e3eb0d Signed-off-by: sangsu --- CMakeLists.txt | 1 + src/CMakeLists.txt | 1 + src/framework/service/cs-loader.cpp | 332 ++++++++++++++++++++++ src/framework/service/cs-loader.h | 127 +++++++++ test/CMakeLists.txt | 2 + test/test-api-engine-content-screening.cpp | 27 +- test/test-cs-loader.cpp | 440 +++++++++++++++++++++++++++++ 7 files changed, 904 insertions(+), 26 deletions(-) create mode 100644 src/framework/service/cs-loader.cpp create mode 100644 src/framework/service/cs-loader.h create mode 100644 test/test-cs-loader.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ae9b67..fafea30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ ADD_DEFINITIONS("-DRO_DBSPACE=\"${RO_DBSPACE}\"") ADD_DEFINITIONS("-DRW_DBSPACE=\"${RW_DBSPACE}\"") ADD_DEFINITIONS("-DSAMPLE_ENGINE_RO_RES_DIR=\"${SAMPLE_ENGINE_RO_RES_DIR}\"") ADD_DEFINITIONS("-DSAMPLE_ENGINE_RW_WORKING_DIR=\"${SAMPLE_ENGINE_RW_WORKING_DIR}\"") +ADD_DEFINITIONS("-DSAMPLE_ENGINE_DIR=\"${SAMPLE_ENGINE_DIR}\"") ADD_DEFINITIONS("-DTEST_DIR=\"${TEST_DIR}\"") IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cacd176..87702a0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,7 @@ SET(${TARGET_CSR_SERVER}_SRCS framework/service/core-usage.cpp framework/service/file-system.cpp framework/service/app-deleter.cpp + framework/service/cs-loader.cpp framework/ui/askuser.cpp # question and response codes needed on both of diff --git a/src/framework/service/cs-loader.cpp b/src/framework/service/cs-loader.cpp new file mode 100644 index 0000000..8fa3b25 --- /dev/null +++ b/src/framework/service/cs-loader.cpp @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * @file csl-oader.cpp + * @author Sangsu Choi (sangsu.choi@samsung.com) + * @version 1.0 + * @brief + */ +#include "service/cs-loader.h" + +#include +#include + +#include "common/audit/logger.h" + +namespace Csr { + +namespace { + +int getValueCstr(std::string &value, + const std::function &getfunc) +{ + const char *cvalue = nullptr; + auto retval = getfunc(cvalue); + + if (retval == CSRE_ERROR_NONE && cvalue != nullptr) + value = cvalue; + + return retval; +} + +} // namespace anonymous + +int CsLoader::globalInit(const std::string &ro_res_dir, + const std::string &rw_working_dir) +{ + if (ro_res_dir.empty() || rw_working_dir.empty()) + throw std::invalid_argument("cs loader global init"); + + return m_pc.fpGlobalInit(ro_res_dir.c_str(), rw_working_dir.c_str()); +} + +int CsLoader::globalDeinit() +{ + return m_pc.fpGlobalDeinit(); +} + +int CsLoader::contextCreate(csre_cs_context_h &c) +{ + return m_pc.fpContextCreate(&c); +} + +int CsLoader::contextDestroy(csre_cs_context_h c) +{ + if (c == nullptr) + throw std::invalid_argument("cs loader context destroy"); + + return m_pc.fpContextDestroy(c); +} + +int CsLoader::scanData(csre_cs_context_h c, + const std::vector &data, + csre_cs_detected_h *pdetected) +{ + if (c == nullptr || data.empty() || pdetected == nullptr) + throw std::invalid_argument("cs loader scan data"); + + return m_pc.fpScanData(c, data.data(), data.size(), pdetected); +} + +int CsLoader::scanFile(csre_cs_context_h c, const std::string &filepath, + csre_cs_detected_h *pdetected) +{ + if (c == nullptr || filepath.empty() || pdetected == nullptr) + throw std::invalid_argument("cs loader scan file"); + + return m_pc.fpScanFile(c, filepath.c_str(), pdetected); +} + +int CsLoader::scanAppOnCloud(csre_cs_context_h c, const std::string &appdir, + csre_cs_detected_h *pdetected) +{ + if (c == nullptr || appdir.empty() || pdetected == nullptr) + throw std::invalid_argument("cs loader scan app on cloud"); + + return m_pc.fpScanAppOnCloud(c, appdir.c_str(), pdetected); +} + +int CsLoader::getSeverity(csre_cs_detected_h d, + csre_cs_severity_level_e *pseverity) +{ + if (d == nullptr || pseverity == nullptr) + throw std::invalid_argument("cs loader get severity"); + + return m_pc.fpGetSeverity(d, pseverity); +} + +int CsLoader::getThreatType(csre_cs_detected_h d, + csre_cs_threat_type_e *pthreat) +{ + if (d == nullptr || pthreat == nullptr) + throw std::invalid_argument("cs loader get threat type"); + + return m_pc.fpGetThreatType(d, pthreat); +} + +int CsLoader::getMalwareName(csre_cs_detected_h d, std::string &value) +{ + if (d == nullptr) + throw std::invalid_argument("cs loader get malware name"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetMalwareName(d, &cvalue); + }); +} + +int CsLoader::getDetailedUrl(csre_cs_detected_h d, std::string &value) +{ + if (d == nullptr) + throw std::invalid_argument("cs loader get detailed url"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetDetailedUrl(d, &cvalue); + }); +} + +int CsLoader::getTimestamp(csre_cs_detected_h d, time_t *timestamp) +{ + if (d == nullptr || timestamp == nullptr) + throw std::invalid_argument("cs loader get time stamp"); + + return m_pc.fpGetTimestamp(d, timestamp); +} + +int CsLoader::getErrorString(int ec, std::string &value) +{ + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetErrorString(ec, &cvalue); + }); +} + +int CsLoader::getEngineInfo(csre_cs_engine_h &e) +{ + return m_pc.fpGetEngineInfo(&e); +} + +int CsLoader::destroyEngine(csre_cs_engine_h e) +{ + return m_pc.fpDestroyEngine(e); +} + +int CsLoader::getEngineApiVersion(csre_cs_engine_h e, std::string &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get error string"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetEngineApiVersion(e, &cvalue); + }); +} + +int CsLoader::getEngineVendor(csre_cs_engine_h e, std::string &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get engine vendor"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetEngineVendor(e, &cvalue); + }); +} + +int CsLoader::getEngineName(csre_cs_engine_h e, std::string &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get engine name"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetEngineName(e, &cvalue); + }); +} + +int CsLoader::getEngineVersion(csre_cs_engine_h e, std::string &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get engine version"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetEngineName(e, &cvalue); + }); +} + +int CsLoader::getEngineDataVersion(csre_cs_engine_h e, std::string &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get engine version"); + + return getValueCstr(value, [&](const char *cvalue) { + return m_pc.fpGetEngineDataVersion(e, &cvalue); + }); +} + +int CsLoader::getEngineLatestUpdateTime(csre_cs_engine_h e, time_t *ptime) +{ + if (e == nullptr || ptime == nullptr) + throw std::invalid_argument("cs loader get latest update time"); + + return m_pc.fpGetEngineLatestUpdateTime(e, ptime); +} + +int CsLoader::getEngineActivated(csre_cs_engine_h e, + csre_cs_activated_e *pactivated) +{ + if (e == nullptr || pactivated == nullptr) + throw std::invalid_argument("cs loader get engine activated"); + + return m_pc.fpGetEngineActivated(e, pactivated); +} + +int CsLoader::getEngineVendorLogo(csre_cs_engine_h e, + std::vector &value) +{ + if (e == nullptr) + throw std::invalid_argument("cs loader get engine vendor logo"); + + unsigned char *cvalue = nullptr; + unsigned int size = 0; + auto retval = m_pc.fpGetEngineVendorLogo(e, &cvalue, &size); + + if (retval == CSRE_ERROR_NONE && cvalue != nullptr && size != 0) + value.assign(cvalue, cvalue + size); + + return retval; +} + +CsLoader::CsLoader(const std::string &enginePath) +{ + INFO("Load CS-Engine plugin start. engine path: " << enginePath); + + void *handle = dlopen(enginePath.c_str(), RTLD_LAZY); + + if (handle == nullptr) + throw std::logic_error(FORMAT("engine dlopen error. path: " << enginePath << + " errno: " << errno)); + + m_pc.dlhandle = handle; + + m_pc.fpGlobalInit = reinterpret_cast(dlsym(handle, + "csre_cs_global_initialize")); + m_pc.fpGlobalDeinit = reinterpret_cast(dlsym(handle, + "csre_cs_global_deinitialize")); + m_pc.fpContextCreate = reinterpret_cast(dlsym(handle, + "csre_cs_context_create")); + m_pc.fpContextDestroy = reinterpret_cast(dlsym(handle, + "csre_cs_context_destroy")); + m_pc.fpScanData = reinterpret_cast(dlsym(handle, + "csre_cs_scan_data")); + m_pc.fpScanFile = reinterpret_cast(dlsym(handle, + "csre_cs_scan_file")); + m_pc.fpScanAppOnCloud = reinterpret_cast(dlsym(handle, + "csre_cs_scan_app_on_cloud")); + m_pc.fpGetSeverity = reinterpret_cast(dlsym(handle, + "csre_cs_detected_get_severity")); + m_pc.fpGetThreatType = reinterpret_cast(dlsym(handle, + "csre_cs_detected_get_threat_type")); + m_pc.fpGetMalwareName = reinterpret_cast(dlsym(handle, + "csre_cs_detected_get_malware_name")); + m_pc.fpGetDetailedUrl = reinterpret_cast(dlsym(handle, + "csre_cs_detected_get_detailed_url")); + m_pc.fpGetTimestamp = reinterpret_cast(dlsym(handle, + "csre_cs_detected_get_timestamp")); + m_pc.fpGetErrorString = reinterpret_cast (dlsym(handle, + "csre_cs_get_error_string")); + m_pc.fpGetEngineInfo = reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_info")); + m_pc.fpDestroyEngine = reinterpret_cast(dlsym(handle, + "csre_cs_engine_destroy")); + m_pc.fpGetEngineApiVersion = reinterpret_cast(dlsym( + handle, "csre_cs_engine_get_api_version")); + m_pc.fpGetEngineVendor = reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_vendor")); + m_pc.fpGetEngineName = reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_version")); + m_pc.fpGetEngineVersion = reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_data_version")); + m_pc.fpGetEngineDataVersion = reinterpret_cast(dlsym( + handle, "csre_cs_engine_get_data_version")); + m_pc.fpGetEngineLatestUpdateTime = + reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_latest_update_time")); + m_pc.fpGetEngineActivated = reinterpret_cast(dlsym(handle, + "csre_cs_engine_get_activated")); + m_pc.fpGetEngineVendorLogo = reinterpret_cast(dlsym( + handle, "csre_cs_engine_get_vendor_logo")); + + if (m_pc.fpGlobalInit == nullptr || m_pc.fpGlobalDeinit == nullptr || + m_pc.fpContextCreate == nullptr || m_pc.fpContextDestroy == nullptr || + m_pc.fpScanData == nullptr || m_pc.fpScanFile == nullptr || + m_pc.fpScanAppOnCloud == nullptr || m_pc.fpGetSeverity == nullptr || + m_pc.fpGetThreatType == nullptr || m_pc.fpGetMalwareName == nullptr || + m_pc.fpGetDetailedUrl == nullptr || m_pc.fpGetTimestamp == nullptr || + m_pc.fpGetErrorString == nullptr || m_pc.fpGetEngineInfo == nullptr || + m_pc.fpGetEngineApiVersion == nullptr || m_pc.fpGetEngineVendor == nullptr || + m_pc.fpGetEngineName == nullptr || m_pc.fpGetEngineVersion == nullptr || + m_pc.fpGetEngineDataVersion == nullptr || + m_pc.fpGetEngineLatestUpdateTime == nullptr || + m_pc.fpGetEngineActivated == nullptr || m_pc.fpGetEngineVendorLogo == nullptr) { + dlclose(handle); + throw std::runtime_error(FORMAT("Failed to load funcs from engine library. " + "engine path: " << enginePath << "errno: " << + errno)); + } +} + +CsLoader::~CsLoader() +{ + dlclose(m_pc.dlhandle); +} + +} diff --git a/src/framework/service/cs-loader.h b/src/framework/service/cs-loader.h new file mode 100644 index 0000000..9520433 --- /dev/null +++ b/src/framework/service/cs-loader.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * @file cs-loader.h + * @author Sangsu Choi (sangsu.choi@samsung.com) + * @version 1.0 + * @brief + */ +#pragma once + +#include +#include +#include +#include + +#include "csre/content-screening-types.h" +#include "csre/content-screening-engine-info.h" + +namespace Csr { + +class CsLoader { +public: + CsLoader(const std::string &); + virtual ~CsLoader(); + int globalInit(const std::string &, const std::string &); + int globalDeinit(); + int contextCreate(csre_cs_context_h &); + int contextDestroy(csre_cs_context_h); + int scanData(csre_cs_context_h, const std::vector &, + csre_cs_detected_h *); + int scanFile(csre_cs_context_h, const std::string &, csre_cs_detected_h *); + int scanAppOnCloud(csre_cs_context_h, const std::string &, + csre_cs_detected_h *); + + int getSeverity(csre_cs_detected_h, csre_cs_severity_level_e *); + int getThreatType(csre_cs_detected_h, csre_cs_threat_type_e *); + int getMalwareName(csre_cs_detected_h, std::string &); + int getDetailedUrl(csre_cs_detected_h, std::string &); + int getTimestamp(csre_cs_detected_h, time_t *); + + int getErrorString(int, std::string &); + + int getEngineInfo(csre_cs_engine_h &); + int destroyEngine(csre_cs_engine_h); + int getEngineApiVersion(csre_cs_engine_h, std::string &); + int getEngineVendor(csre_cs_engine_h, std::string &); + int getEngineName(csre_cs_engine_h, std::string &); + int getEngineVersion(csre_cs_engine_h, std::string &); + int getEngineDataVersion(csre_cs_engine_h, std::string &); + int getEngineLatestUpdateTime(csre_cs_engine_h, time_t *); + int getEngineActivated(csre_cs_engine_h, csre_cs_activated_e *); + int getEngineVendorLogo(csre_cs_engine_h, std::vector &); + +private: + using FpGlobalInit = int(*)(const char *, const char *); + using FpGlobalDeinit = int(*)(); + using FpContextCreate = int(*)(csre_cs_context_h *); + using FpContextDestroy = int(*)(csre_cs_context_h); + using FpScanData = int(*)(csre_cs_context_h, const unsigned char *, size_t, + csre_cs_detected_h *); + using FpScanFile = int(*)(csre_cs_context_h, const char *, + csre_cs_detected_h *); + using FpScanAppOnCloud = int(*)(csre_cs_context_h, const char *, + csre_cs_detected_h *); + using FpGetSeverity = int(*)(csre_cs_detected_h, csre_cs_severity_level_e *); + using FpGetThreatType = int(*)(csre_cs_detected_h, csre_cs_threat_type_e *); + using FpGetMalwareName = int(*)(csre_cs_detected_h, const char **); + using FpGetDetailedUrl = int(*)(csre_cs_detected_h, const char **); + using FpGetTimestamp = int(*)(csre_cs_detected_h, time_t *); + using FpGetErrorString = int(*)(int, const char **); + using FpGetEngineInfo = int(*)(csre_cs_engine_h *); + using FpDestroyEngine = int(*)(csre_cs_engine_h); + using FpGetEngineApiVersion = int(*)(csre_cs_engine_h, const char **); + using FpGetEngineVendor = int(*)(csre_cs_engine_h, const char **); + using FpGetEngineName = int(*)(csre_cs_engine_h, const char **); + using FpGetEngineVersion = int(*)(csre_cs_engine_h, const char **); + using FpGetEngineDataVersion = int(*)(csre_cs_engine_h, const char **); + using FpGetEngineLatestUpdateTime = int(*)(csre_cs_engine_h, time_t *); + using FpGetEngineActivated = int(*)(csre_cs_engine_h, csre_cs_activated_e *); + using FpGetEngineVendorLogo = int(*)(csre_cs_engine_h, unsigned char **, + unsigned int *); + + struct PluginContext { + void *dlhandle; + + FpGlobalInit fpGlobalInit; + FpGlobalDeinit fpGlobalDeinit; + FpContextCreate fpContextCreate; + FpContextDestroy fpContextDestroy; + FpScanData fpScanData; + FpScanFile fpScanFile; + FpScanAppOnCloud fpScanAppOnCloud; + FpGetSeverity fpGetSeverity; + FpGetThreatType fpGetThreatType; + FpGetMalwareName fpGetMalwareName; + FpGetDetailedUrl fpGetDetailedUrl; + FpGetTimestamp fpGetTimestamp; + FpGetErrorString fpGetErrorString; + FpGetEngineInfo fpGetEngineInfo; + FpDestroyEngine fpDestroyEngine; + FpGetEngineApiVersion fpGetEngineApiVersion; + FpGetEngineVendor fpGetEngineVendor; + FpGetEngineName fpGetEngineName; + FpGetEngineVersion fpGetEngineVersion; + FpGetEngineDataVersion fpGetEngineDataVersion; + FpGetEngineLatestUpdateTime fpGetEngineLatestUpdateTime; + FpGetEngineActivated fpGetEngineActivated; + FpGetEngineVendorLogo fpGetEngineVendorLogo; + }; + + PluginContext m_pc; +}; + +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dcf213c..3df5dbb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,6 +38,7 @@ SET(${TARGET_CSR_TEST}_SRCS ${CSR_FW_SVC_SRC_PATH}/core-usage.cpp ${CSR_FW_SVC_SRC_PATH}/file-system.cpp ${CSR_FW_SVC_SRC_PATH}/app-deleter.cpp + ${CSR_FW_SVC_SRC_PATH}/cs-loader.cpp colour_log_formatter.cpp test-api-content-screening.cpp test-api-content-screening-async.cpp @@ -50,6 +51,7 @@ SET(${TARGET_CSR_TEST}_SRCS test-internal-file-system.cpp test-main.cpp test-common.cpp + test-cs-loader.cpp ) INCLUDE_DIRECTORIES( diff --git a/test/test-api-engine-content-screening.cpp b/test/test-api-engine-content-screening.cpp index f96f4f8..ecbdc28 100644 --- a/test/test-api-engine-content-screening.cpp +++ b/test/test-api-engine-content-screening.cpp @@ -99,32 +99,7 @@ inline void checkDetected(csre_cs_detected_h detected, EXCEPTION_GUARD_END } -class ScopedFile { -public: - explicit ScopedFile(const std::string &file) - { - int fd = open(file.c_str(), O_RDONLY); - BOOST_REQUIRE_MESSAGE(fd > 0, - "Cannot open file: " << file << " with errno: " << errno); - - m_fd = fd; - } - - virtual ~ScopedFile() - { - close(m_fd); - } - - int getFd(void) - { - return m_fd; - } - -private: - int m_fd; -}; - -} +} // namespace anonymous BOOST_AUTO_TEST_SUITE(API_ENGINE_CONTENT_SCREENING) diff --git a/test/test-cs-loader.cpp b/test/test-cs-loader.cpp new file mode 100644 index 0000000..408dc3b --- /dev/null +++ b/test/test-cs-loader.cpp @@ -0,0 +1,440 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * @file test-cs-loader.cpp + * @author Sangsu Choi (sangsu.choi@samsung.com) + * @version 1.0 + * @brief Content screening engine dynamic loading test + */ +#include "service/cs-loader.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include "test-common.h" + +#define TEST_FILE_NORMAL TEST_DIR "/test_normal_file" +#define TEST_FILE_MALWARE TEST_DIR "/test_malware_file" +#define TEST_FILE_RISKY TEST_DIR "/test_risky_file" +#define TEST_APP_ROOT TEST_DIR "/test_app" + +namespace { + +inline void checkDetected(csre_cs_detected_h detected, + csre_cs_severity_level_e expected_severity, + csre_cs_threat_type_e expected_threat_type, + const char *expected_malware_name, + const char *expected_detailed_url, + long expected_timestamp) +{ + EXCEPTION_GUARD_START + + CHECK_IS_NOT_NULL(detected); + + csre_cs_severity_level_e severity; + ASSERT_IF(csre_cs_detected_get_severity(detected, &severity), CSRE_ERROR_NONE); + BOOST_REQUIRE_MESSAGE(severity == expected_severity, + "severity isn't expected value. " + "val: " << severity << " expected: " << expected_severity); + + csre_cs_threat_type_e threat_type; + ASSERT_IF(csre_cs_detected_get_threat_type(detected, &threat_type), + CSRE_ERROR_NONE); + BOOST_REQUIRE_MESSAGE(threat_type == expected_threat_type, + "threat type isn't expected value. " + "val: " << threat_type << " expected: " << expected_threat_type); + + const char *malware_name = nullptr; + ASSERT_IF(csre_cs_detected_get_malware_name(detected, &malware_name), + CSRE_ERROR_NONE); + + if (expected_malware_name != nullptr) { + CHECK_IS_NOT_NULL(malware_name); + BOOST_REQUIRE_MESSAGE( + (strlen(malware_name) == strlen(expected_malware_name) && + memcmp(malware_name, expected_malware_name, strlen(malware_name)) == 0), + "malware_name isn't expected value. " + "val: " << malware_name << " expected: " << expected_malware_name); + } + + const char *detailed_url = nullptr; + ASSERT_IF(csre_cs_detected_get_detailed_url(detected, &detailed_url), + CSRE_ERROR_NONE); + + if (expected_detailed_url != nullptr) { + CHECK_IS_NOT_NULL(detailed_url); + BOOST_REQUIRE_MESSAGE( + (strlen(detailed_url) == strlen(expected_detailed_url) && + memcmp(detailed_url, expected_detailed_url, strlen(detailed_url)) == 0), + "detailed_url isn't expected value. " + "val: " << detailed_url << " expected: " << expected_detailed_url); + + } + + long timestamp; + ASSERT_IF(csre_cs_detected_get_timestamp(detected, ×tamp), + CSRE_ERROR_NONE); + + if (expected_timestamp != 0) + BOOST_REQUIRE_MESSAGE(timestamp == expected_timestamp, + "timestamp isn't expected value. " + "val: " << timestamp << " expected: " << expected_timestamp); + + EXCEPTION_GUARD_END +} + +template +struct Handle { + Handle() + { + BOOST_REQUIRE_MESSAGE(0, "Not specialized for handle template"); + } + + ~Handle() + { + BOOST_REQUIRE_MESSAGE(0, "Not specialized for handle template"); + } + + Csr::CsLoader loader; + T context; +}; + +template <> +struct Handle { + Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so") + { + ASSERT_IF( + loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR), + CSRE_ERROR_NONE); + ASSERT_IF(loader.contextCreate(context), CSRE_ERROR_NONE); + } + + ~Handle() + { + ASSERT_IF(loader.contextDestroy(context), CSRE_ERROR_NONE); + ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE); + } + + Csr::CsLoader loader; + csre_cs_context_h context; +}; + +template <> +struct Handle { + Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so") + { + ASSERT_IF( + loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR), + CSRE_ERROR_NONE); + ASSERT_IF(loader.getEngineInfo(context), CSRE_ERROR_NONE); + } + + ~Handle() + { + ASSERT_IF(loader.destroyEngine(context), CSRE_ERROR_NONE); + ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE); + } + + Csr::CsLoader loader; + csre_cs_engine_h context; +}; + +} // namespace anonymous + +BOOST_AUTO_TEST_SUITE(CS_LOADER) + +BOOST_AUTO_TEST_CASE(context_create_destroy) +{ + EXCEPTION_GUARD_START + + Handle h; + (void) h; + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_clear) +{ + EXCEPTION_GUARD_START + + Handle h; + + const char *cdata = + "abcd1234dfdfdf334dfdi8ffndsfdfdsfdasfagdfvdfdfafadfasdfsdfe"; + + csre_cs_detected_h detected; + std::vector data(cdata, cdata + strlen(cdata)); + ASSERT_IF(h.loader.scanData(h.context, data, &detected), CSRE_ERROR_NONE); + + CHECK_IS_NULL(detected); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_high) +{ + EXCEPTION_GUARD_START + + Handle h; + + const char *cdata = + "aabbccX5O!P%@AP[4\\PZX54(P^)7CC)7}$" + "EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*112233"; + + csre_cs_detected_h detected; + std::vector data(cdata, cdata + strlen(cdata)); + ASSERT_IF(h.loader.scanData(h.context, data, &detected), CSRE_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + + checkDetected(detected, + CSRE_CS_SEVERITY_HIGH, + CSRE_CS_THREAT_MALWARE, + "test_malware", + "http://high.malware.com", + 0); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_medium) +{ + EXCEPTION_GUARD_START + + Handle h; + + const char *cdata = "aabbccRISKY_MALWARE112233"; + + csre_cs_detected_h detected; + std::vector data(cdata, cdata + strlen(cdata)); + ASSERT_IF(h.loader.scanData(h.context, data, &detected), CSRE_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + + checkDetected(detected, + CSRE_CS_SEVERITY_MEDIUM, + CSRE_CS_THREAT_RISKY, + "test_risk", + nullptr, + 0); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_normal) +{ + EXCEPTION_GUARD_START + + Handle h; + + csre_cs_detected_h detected; + ASSERT_IF(h.loader.scanFile(h.context, TEST_FILE_NORMAL, &detected), + CSRE_ERROR_NONE); + + CHECK_IS_NULL(detected); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_malware) +{ + EXCEPTION_GUARD_START + + Handle h; + + csre_cs_detected_h detected; + ASSERT_IF(h.loader.scanFile(h.context, TEST_FILE_MALWARE, &detected), + CSRE_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + + checkDetected(detected, + CSRE_CS_SEVERITY_HIGH, + CSRE_CS_THREAT_MALWARE, + "test_malware", + "http://high.malware.com", + 0); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_risky) +{ + EXCEPTION_GUARD_START + + Handle h; + + csre_cs_detected_h detected; + ASSERT_IF(h.loader.scanFile(h.context, TEST_FILE_RISKY, &detected), + CSRE_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + + checkDetected(detected, + CSRE_CS_SEVERITY_MEDIUM, + CSRE_CS_THREAT_RISKY, + "test_risk", + "http://medium.malware.com", + 0); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_app_on_cloud) +{ + EXCEPTION_GUARD_START + + Handle h; + + csre_cs_detected_h detected; + ASSERT_IF(h.loader.scanAppOnCloud(h.context, TEST_APP_ROOT, &detected), + CSRE_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + + checkDetected(detected, + CSRE_CS_SEVERITY_HIGH, + CSRE_CS_THREAT_MALWARE, + "test_malware", + "http://high.malware.com", + 0); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(error_string_positive) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::string str; + + ASSERT_IF(h.loader.getErrorString(CSRE_ERROR_UNKNOWN, str), CSRE_ERROR_NONE); + ASSERT_IF(str.empty(), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_engine_info) +{ + EXCEPTION_GUARD_START + + Handle h; + (void) h; + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_vendor) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::string str; + ASSERT_IF(h.loader.getEngineVendor(h.context, str), CSRE_ERROR_NONE); + ASSERT_IF(str.empty(), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_vendor_logo) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::vector logo; + ASSERT_IF(h.loader.getEngineVendorLogo(h.context, logo), CSRE_ERROR_NONE); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_version) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::string str; + ASSERT_IF(h.loader.getEngineVersion(h.context, str), CSRE_ERROR_NONE); + ASSERT_IF(str.empty(), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_data_version) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::string str; + ASSERT_IF(h.loader.getEngineDataVersion(h.context, str), CSRE_ERROR_NONE); + ASSERT_IF(str.empty(), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_latest_update_time) +{ + EXCEPTION_GUARD_START + + Handle h; + + time_t time = 0; + ASSERT_IF(h.loader.getEngineLatestUpdateTime(h.context, &time), + CSRE_ERROR_NONE); + + struct tm t; + BOOST_MESSAGE(asctime(gmtime_r(&time, &t))); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_engine_activated) +{ + EXCEPTION_GUARD_START + + Handle h; + + csre_cs_activated_e activated; + ASSERT_IF(h.loader.getEngineActivated(h.context, &activated), CSRE_ERROR_NONE); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_api_version) +{ + EXCEPTION_GUARD_START + + Handle h; + + std::string str; + ASSERT_IF(h.loader.getEngineApiVersion(h.context, str), CSRE_ERROR_NONE); + ASSERT_IF(str == CSRE_CS_API_VERSION, true); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.7.4