Add engine library loader classes 64/66364/1
authorsangsu <sangsu.choi@samsung.com>
Fri, 1 Apr 2016 05:11:37 +0000 (14:11 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 18 Apr 2016 11:21:30 +0000 (20:21 +0900)
Change-Id: Iaf17a44a9e120e8ae0587fae839be20da6e3eb0d
Signed-off-by: sangsu <sangsu.choi@samsung.com>
CMakeLists.txt
src/CMakeLists.txt
src/framework/service/cs-loader.cpp [new file with mode: 0644]
src/framework/service/cs-loader.h [new file with mode: 0644]
test/CMakeLists.txt
test/test-api-engine-content-screening.cpp
test/test-cs-loader.cpp [new file with mode: 0644]

index 7ae9b67..fafea30 100644 (file)
@@ -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")
index cacd176..87702a0 100644 (file)
@@ -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 (file)
index 0000000..8fa3b25
--- /dev/null
@@ -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 <stdexcept>
+#include <dlfcn.h>
+
+#include "common/audit/logger.h"
+
+namespace Csr {
+
+namespace {
+
+int getValueCstr(std::string &value,
+                                const std::function<int(const char *)> &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<unsigned char> &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<unsigned char> &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<FpGlobalInit>(dlsym(handle,
+                                               "csre_cs_global_initialize"));
+       m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
+                                                 "csre_cs_global_deinitialize"));
+       m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
+                                                  "csre_cs_context_create"));
+       m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
+                                                       "csre_cs_context_destroy"));
+       m_pc.fpScanData = reinterpret_cast<FpScanData>(dlsym(handle,
+                                         "csre_cs_scan_data"));
+       m_pc.fpScanFile = reinterpret_cast<FpScanFile>(dlsym(handle,
+                                         "csre_cs_scan_file"));
+       m_pc.fpScanAppOnCloud = reinterpret_cast<FpScanAppOnCloud>(dlsym(handle,
+                                                       "csre_cs_scan_app_on_cloud"));
+       m_pc.fpGetSeverity = reinterpret_cast<FpGetSeverity>(dlsym(handle,
+                                                "csre_cs_detected_get_severity"));
+       m_pc.fpGetThreatType = reinterpret_cast<FpGetThreatType>(dlsym(handle,
+                                                  "csre_cs_detected_get_threat_type"));
+       m_pc.fpGetMalwareName = reinterpret_cast<FpGetMalwareName>(dlsym(handle,
+                                                       "csre_cs_detected_get_malware_name"));
+       m_pc.fpGetDetailedUrl = reinterpret_cast<FpGetDetailedUrl>(dlsym(handle,
+                                                       "csre_cs_detected_get_detailed_url"));
+       m_pc.fpGetTimestamp = reinterpret_cast<FpGetTimestamp>(dlsym(handle,
+                                                 "csre_cs_detected_get_timestamp"));
+       m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
+                                                       "csre_cs_get_error_string"));
+       m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
+                                                  "csre_cs_engine_get_info"));
+       m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
+                                                  "csre_cs_engine_destroy"));
+       m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
+                                                                        handle, "csre_cs_engine_get_api_version"));
+       m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
+                                                        "csre_cs_engine_get_vendor"));
+       m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
+                                                  "csre_cs_engine_get_version"));
+       m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
+                                                         "csre_cs_engine_get_data_version"));
+       m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
+                                                                         handle, "csre_cs_engine_get_data_version"));
+       m_pc.fpGetEngineLatestUpdateTime =
+               reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
+                               "csre_cs_engine_get_latest_update_time"));
+       m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
+                                                               "csre_cs_engine_get_activated"));
+       m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(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 (file)
index 0000000..9520433
--- /dev/null
@@ -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 <ctime>
+#include <cstddef>
+#include <vector>
+#include <string>
+
+#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<unsigned char> &,
+                                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<unsigned char> &);
+
+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;
+};
+
+}
index dcf213c..3df5dbb 100644 (file)
@@ -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(
index f96f4f8..ecbdc28 100644 (file)
@@ -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 (file)
index 0000000..408dc3b
--- /dev/null
@@ -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 <cstring>
+#include <cstdio>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <boost/test/unit_test.hpp>
+
+#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, &timestamp),
+                         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 <typename T>
+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<csre_cs_context_h> {
+       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<csre_cs_engine_h> {
+       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<csre_cs_context_h> h;
+       (void) h;
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(scan_data_clear)
+{
+       EXCEPTION_GUARD_START
+
+       Handle<csre_cs_context_h> h;
+
+       const char *cdata =
+               "abcd1234dfdfdf334dfdi8ffndsfdfdsfdasfagdfvdfdfafadfasdfsdfe";
+
+       csre_cs_detected_h detected;
+       std::vector<unsigned char> 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<csre_cs_context_h> 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<unsigned char> 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<csre_cs_context_h> h;
+
+       const char *cdata = "aabbccRISKY_MALWARE112233";
+
+       csre_cs_detected_h detected;
+       std::vector<unsigned char> 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<csre_cs_context_h> 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<csre_cs_context_h> 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<csre_cs_context_h> 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<csre_cs_context_h> 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<csre_cs_context_h> 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<csre_cs_engine_h> h;
+       (void) h;
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(get_vendor)
+{
+       EXCEPTION_GUARD_START
+
+       Handle<csre_cs_engine_h> 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<csre_cs_engine_h> h;
+
+       std::vector<unsigned char> 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<csre_cs_engine_h> 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<csre_cs_engine_h> 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<csre_cs_engine_h> 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<csre_cs_engine_h> 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<csre_cs_engine_h> 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()