Add init/deinit APIs to engine and refactor sample 50/66350/1
authorKyungwook Tak <k.tak@samsung.com>
Tue, 5 Apr 2016 02:23:06 +0000 (11:23 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 18 Apr 2016 11:11:59 +0000 (20:11 +0900)
sample engine refactored totally because engine handle
and context handle were tightly coupled and lot of c-style codes

Change-Id: Ie24a01880fc4b238aafddd32964a752c7cd469f6
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
13 files changed:
CMakeLists.txt
engine/content-screening/CMakeLists.txt
engine/content-screening/sample-engine.cpp
engine/web-protection/CMakeLists.txt
engine/web-protection/sample-engine.cpp
packaging/csr-framework.spec
src/include/csre/content-screening-engine-info.h
src/include/csre/content-screening.h
src/include/csre/web-protection-engine-info.h
src/include/csre/web-protection.h
test/test-api-engine-content-screening.cpp
test/test-api-engine-web-protection.cpp
test/test-main.cpp

index dd192f3..7ae9b67 100644 (file)
@@ -41,7 +41,8 @@ ADD_DEFINITIONS("-DINCLUDE_INSTALL_DIR=\"${INCLUDE_INSTALL_DIR}\"")
 ADD_DEFINITIONS("-DBIN_DIR=\"${BIN_DIR}\"")
 ADD_DEFINITIONS("-DRO_DBSPACE=\"${RO_DBSPACE}\"")
 ADD_DEFINITIONS("-DRW_DBSPACE=\"${RW_DBSPACE}\"")
-ADD_DEFINITIONS("-DSAMPLE_ENGINE_WORKING_DIR=\"${SAMPLE_ENGINE_WORKING_DIR}\"")
+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("-DTEST_DIR=\"${TEST_DIR}\"")
 
 IF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
index 515080a..3d10695 100644 (file)
@@ -42,4 +42,4 @@ TARGET_LINK_LIBRARIES(${TARGET_CSR_CS_ENGINE_SAMPLE}
 )
 
 INSTALL(TARGETS ${TARGET_CSR_CS_ENGINE_SAMPLE} DESTINATION ${SAMPLE_ENGINE_DIR})
-INSTALL(DIRECTORY resources/ DESTINATION ${SAMPLE_ENGINE_WORKING_DIR})
+INSTALL(DIRECTORY resources/ DESTINATION ${SAMPLE_ENGINE_RW_WORKING_DIR})
index ea6d09b..ced14db 100644 (file)
 #include "csre/content-screening.h"
 #include "csre/content-screening-engine-info.h"
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include <string>
+#include <vector>
+#include <memory>
+#include <functional>
+#include <list>
+#include <fstream>
+#include <iostream>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
-#include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
 
 #define API __attribute__((visibility("default")))
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #define PRIVATE_DB_NAME   "csret_cs_virus_signatures"
 #define PRIVATE_LOGO_FILE "vendor_logo.bmp"
-#define MAX_FILE_PATH_LEN PATH_MAX
-#define MAX_NAME_LEN      64
-#define MAX_VERSION_LEN   32
-#define MAX_URL_LEN       256
-#define MAX_SIG_LEN       256
 
 #define VENDOR_NAME       "TEST_VENDOR"
 #define ENGINE_NAME       "TEST_LOCAL_TCS_ENGINE"
 #define ENGINE_VERSION    "0.0.1"
 
-typedef struct __csret_cs_malware {
+using RawBuffer = std::vector<unsigned char>;
+
+// TODO(k.tak): make all engine functions exception-safe
+
+struct csret_cs_malware_s {
        csre_cs_severity_level_e severity;
        csre_cs_threat_type_e threat_type;
-       char malware_name[MAX_NAME_LEN];
-       char detailed_url[MAX_URL_LEN];
-       char signature[MAX_SIG_LEN];
-} csret_cs_malware_s;
-
-typedef struct __csret_cs_malware_list {
-       struct __csret_cs_malware_list *next;
-       csret_cs_malware_s malware;
-} csret_cs_malware_list_s;
+       std::string name;
+       std::string detailed_url;
+       std::string signature;
+};
 
-typedef struct __csret_cs_detected {
+struct csret_cs_detected_s {
        csret_cs_malware_s malware;
        time_t timestamp;
-} csret_cs_detected_s;
+};
 
-typedef struct __csret_cs_detected_list {
-       struct __csret_cs_detected_list *next;
-       csret_cs_detected_s *detected;
-} csret_cs_detected_list_s;
-
-typedef struct __csret_cs_context {
+struct csret_cs_context_s {
        int scan_on_data;
-       csret_cs_detected_list_s *detected_list;
-} csret_cs_context_s;
-
-typedef struct __csret_cs_engine {
-       char vendor_name[MAX_NAME_LEN];
-       char engine_name[MAX_NAME_LEN];
-       unsigned char *vendor_logo_image;
-       unsigned int image_size;
-       char engine_version[MAX_VERSION_LEN];
-       char data_version[MAX_VERSION_LEN];
-       time_t latest_update;
-} csret_cs_engine_s;
-
-typedef enum __csret_cs_internal_error {
-       CSRET_CS_ERROR_NO_SIGNATURE_FILE    = -0x0101,
-       CSRET_CS_ERROR_SIGNARUE_FILE_FORMAT = -0x0102,
-       CSRET_CS_ERROR_FILE_IO              = -0x0103
-} csret_cs_internal_error_e;
+       std::list<csret_cs_detected_s> detected_list;
+};
+
+struct csret_cs_engine_s {
+       std::string vendorName;
+       std::string engineName;
+       std::string apiVersion;
+       std::string engineVersion;
+       std::string dataVersion;
+       RawBuffer logoImage;
+       time_t latestUpdate;
+};
+
+enum csret_cs_internal_error_e {
+       CSRET_CS_ERROR_NO_SIGNATURE_FILE     = -0x0101,
+       CSRET_CS_ERROR_SIGNATURE_FILE_FORMAT = -0x0102,
+       CSRET_CS_ERROR_FILE_IO               = -0x0103
+};
 
 //==============================================================================
 // static variables
 //==============================================================================
-static csret_cs_engine_s       *engine_info = nullptr;
-static csret_cs_malware_list_s *virus_sig   = nullptr;
+static std::string g_resdir;
+static std::string g_workingdir;
+static std::list<csret_cs_malware_s> g_virus_sig;
 
 //==============================================================================
 // Utilities functions
 //==============================================================================
-
-char *csret_cs_extract_value(char *line, const char *key)
+std::string csret_cs_extract_value(const std::string &line, const std::string &key)
 {
-       if (line == nullptr || key == nullptr)
-               return nullptr;
-
-       auto found = strstr(line, key);
-       if (found != line)
-               return nullptr;
+       if (line.empty() || key.empty())
+               return std::string();
 
-       auto value = found + strlen(key);
+       auto pos = line.find(key);
+       if (pos == std::string::npos || pos != 0)
+               return std::string();
 
-       // remove end line char
-       for (auto current = found; current && *current; current++) {
-               if (*current == '\n') {
-                       *current = '\0';
-                       break;
-               }
-       }
-
-       return value;
+       return line.substr(key.length());
 }
 
-int csret_cs_read_virus_signatures(const char *path)
+int csret_cs_read_virus_signatures(const std::string &path)
 {
        // virus_signature file format
        // data_version=1.0.0 // it should be in the first line.
@@ -144,130 +122,92 @@ int csret_cs_read_virus_signatures(const char *path)
        // threat_type=RISKY
        // detailed_url=http://medium.malware.com
        // signature=RISKY_MALWARE
-
-       csret_cs_malware_list_s *curr_sig = nullptr;
-
-       FILE *fp = fopen(path, "r");
-       if (fp == nullptr)
+       std::ifstream f(path.c_str());
+       if (!f.is_open())
                return CSRET_CS_ERROR_NO_SIGNATURE_FILE;
 
-       char *line = nullptr;
-       size_t len = 0;
-       ssize_t read;
-       while ((read = getline(&line, &len, fp)) != -1) {
-               if (line == nullptr || strlen(line) == 0)
+       std::string line;
+       csret_cs_malware_s node;
+       while (std::getline(f, line)) {
+               if (line.empty() || line[0] == '#')
                        continue;
 
-               auto value = csret_cs_extract_value(line, "data_version=");
-
-               if (value != nullptr && engine_info != nullptr)
-                       strncpy(engine_info->data_version, value, sizeof(engine_info->data_version) - 1);
-
-               value = csret_cs_extract_value(line, "name=");
-
-               if (value != nullptr) {
-                       auto next_sig = (csret_cs_malware_list_s *) calloc(sizeof(csret_cs_malware_list_s), 1);
-
-                       if (curr_sig != nullptr)
-                               curr_sig->next = next_sig;
-                       else
-                               virus_sig = next_sig;
+               auto value = csret_cs_extract_value(line, "name=");
+               if (!value.empty()) {
+                       if (!node.name.empty()) {
+                               g_virus_sig.push_back(node);
+                               node = csret_cs_malware_s();
+                       }
 
-                       curr_sig = next_sig;
-                       strncpy(curr_sig->malware.malware_name, value, sizeof(curr_sig->malware.malware_name) - 1);
+                       node.name = std::move(value);
+                       continue;
                }
 
                value = csret_cs_extract_value(line, "severity=");
-
-               if (value != nullptr) {
-                       if (strcmp(value, "LOW") == 0)
-                               curr_sig->malware.severity = CSRE_CS_SEVERITY_LOW;
-                       else if (strcmp(value, "MEDIUM") == 0)
-                               curr_sig->malware.severity = CSRE_CS_SEVERITY_MEDIUM;
+               if (!value.empty()) {
+                       if (value.compare("LOW") == 0)
+                               node.severity = CSRE_CS_SEVERITY_LOW;
+                       else if (value.compare("MEDIUM") == 0)
+                               node.severity = CSRE_CS_SEVERITY_MEDIUM;
                        else
-                               curr_sig->malware.severity = CSRE_CS_SEVERITY_HIGH;
+                               node.severity = CSRE_CS_SEVERITY_HIGH;
+
+                       continue;
                }
 
                value = csret_cs_extract_value(line, "threat_type=");
-
-               if (value != nullptr) {
-                       if (strcmp(value, "MALWARE") == 0)
-                               curr_sig->malware.threat_type = CSRE_CS_THREAT_MALWARE;
-                       else if (strcmp(value, "RISKY") == 0)
-                               curr_sig->malware.threat_type = CSRE_CS_THREAT_RISKY;
+               if (!value.empty()) {
+                       if (value.compare("MALWARE") == 0)
+                               node.threat_type = CSRE_CS_THREAT_MALWARE;
+                       else if (value.compare("RISKY") == 0)
+                               node.threat_type = CSRE_CS_THREAT_RISKY;
                        else
-                               curr_sig->malware.threat_type = CSRE_CS_THREAT_GENERIC;
+                               node.threat_type = CSRE_CS_THREAT_GENERIC;
+
+                       continue;
                }
 
                value = csret_cs_extract_value(line, "detailed_url=");
-
-               if (value != nullptr)
-                       strncpy(curr_sig->malware.detailed_url, value, sizeof(curr_sig->malware.detailed_url) - 1);
+               if (!value.empty()) {
+                       node.detailed_url = std::move(value);
+                       continue;
+               }
 
                value = csret_cs_extract_value(line, "signature=");
-
-               if (value != nullptr)
-                       strncpy(curr_sig->malware.signature, value, sizeof(curr_sig->malware.signature) - 1);
+               if (!value.empty())
+                       node.signature = std::move(value);
        }
 
-       free(line);
-       fclose(fp);
+       if (!node.name.empty())
+               g_virus_sig.push_back(node);
+
        return CSRE_ERROR_NONE;
 }
 
-
-int csret_cs_read_binary_by_file(FILE *file, unsigned char **data, unsigned int *len)
+int csret_cs_read_binary(const std::string &path, RawBuffer &buffer)
 {
-       unsigned char *buffer;
-       long int fileLen;
-       int read;
-       int index = 0;
-
-       if (!file)
-               return CSRE_ERROR_FILE_NOT_FOUND;
-
-       //Get file length
-       fseek(file, 0, SEEK_END);
-       fileLen = ftell(file);
-
-       if (fileLen <= 0)
-               return CSRET_CS_ERROR_FILE_IO;
+       std::ifstream f(path.c_str(), std::ios::binary);
 
-       fseek(file, 0, SEEK_SET);
-       //Allocate memory
-       buffer = (unsigned char *)calloc(fileLen + 1, 1);
-
-       if (!buffer)
-               return CSRE_ERROR_OUT_OF_MEMORY;
+       if (!f.is_open()) {
+               buffer.clear();
+               return CSRE_ERROR_NONE;
+       }
 
-       //Read file contents into buffer
-       while ((read = fread(buffer + index, 1, fileLen, file)) > 0)
-               index += read;
+       f.seekg(0, f.end);
+       auto len = f.tellg();
+       f.seekg(0, f.beg);
 
-       fclose(file);
+       buffer.resize(len, 0);
+       f.read(reinterpret_cast<char *>(buffer.data()), buffer.size());
 
-       if (index != fileLen) {
-               free(buffer);
+       if (!f) {
+               buffer.clear();
                return CSRET_CS_ERROR_FILE_IO;
        }
 
-       *data = buffer;
-       *len = fileLen;
        return CSRE_ERROR_NONE;
 }
 
-int csret_cs_read_binary(const char *path, unsigned char **data, unsigned int *len)
-{
-       FILE *file = fopen(path, "rb");
-       return csret_cs_read_binary_by_file(file, data, len);
-}
-
-int csret_cs_read_binary_by_fd(int file_descriptor, unsigned char **data, unsigned int *len)
-{
-       FILE *file = fdopen(file_descriptor, "rb");
-       return csret_cs_read_binary_by_file(file, data, len);
-}
-
 time_t csret_cs_get_timestamp()
 {
        struct timeval tv;
@@ -275,191 +215,133 @@ time_t csret_cs_get_timestamp()
        return tv.tv_sec;
 }
 
-int csret_cs_init_engine(const char *root_dir)
+csret_cs_engine_s *csret_cs_init_engine()
 {
-       int ret = CSRE_ERROR_NONE;
-       char db_file_name[MAX_FILE_PATH_LEN] = {0, };
-       char logo_file_name[MAX_FILE_PATH_LEN] = {0, };
-       struct stat attrib;
-       engine_info = (csret_cs_engine_s *) calloc(sizeof(csret_cs_engine_s), 1);
-
-       if (engine_info == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
-
-       snprintf(engine_info->vendor_name, MAX_NAME_LEN, "%s", VENDOR_NAME);
-       snprintf(engine_info->engine_name, MAX_NAME_LEN, "%s", ENGINE_NAME);
-       snprintf(engine_info->engine_version, MAX_VERSION_LEN, "%s", ENGINE_VERSION);
-       snprintf(db_file_name, MAX_FILE_PATH_LEN, "%s/%s", root_dir, PRIVATE_DB_NAME);
-       snprintf(logo_file_name, MAX_FILE_PATH_LEN, "%s/%s", root_dir, PRIVATE_LOGO_FILE);
-       ret = csret_cs_read_binary(logo_file_name, &(engine_info->vendor_logo_image),
-                                                       &(engine_info->image_size));
-
-       if (ret == CSRE_ERROR_FILE_NOT_FOUND) {
-               engine_info->vendor_logo_image = nullptr;
-               engine_info->image_size = 0;
-               ret = CSRE_ERROR_NONE;
+       auto ptr = new csret_cs_engine_s;
+
+       ptr->vendorName = VENDOR_NAME;
+       ptr->engineName = ENGINE_NAME;
+       ptr->apiVersion = CSRE_CS_API_VERSION;
+       ptr->engineVersion = ENGINE_VERSION;
+       ptr->dataVersion = ENGINE_VERSION;
+
+       int ret = csret_cs_read_binary(g_resdir + "/" PRIVATE_LOGO_FILE, ptr->logoImage);
+       if (ret != CSRE_ERROR_NONE) {
+               delete ptr;
+               return nullptr;
        }
 
-       stat(db_file_name, &attrib);
-       engine_info->latest_update = attrib.st_mtime;
+       struct stat attrib;
+       stat(PRIVATE_DB_NAME, &attrib);
+       ptr->latestUpdate = attrib.st_mtime;
 
-       return ret;
+       return ptr;
 }
 
-int csret_cs_compare_data(const unsigned char *data, unsigned int data_len,
-                                          const char *virus_signature, unsigned int sig_len)
+int csret_cs_compare_data(const RawBuffer &data, const std::string &needle)
 {
-       unsigned int i, j;
-
-       if (data_len < sig_len)
+       if (data.size() < needle.length())
                return -1;
 
-       for (i = 0; i <= (data_len - sig_len); i++) {
-               for (j = 0; j < sig_len; j++) {
-                       if (data[i + j] == (unsigned char) virus_signature[j])
-                               continue;
-                       else
+       for (size_t i = 0; i < data.size() - needle.length(); i++) {
+               bool isMatched = true;
+               for (size_t j = 0; j < needle.length(); j++) {
+                       if (data[i + j] != static_cast<unsigned char>(needle[j])) {
+                               isMatched = false;
                                break;
+                       }
                }
 
-               if (j == sig_len)
+               if (isMatched)
                        return 0; // matched
        }
 
        return -1;
 }
 
-int csret_cs_detect_malware(csret_cs_context_s *context, const unsigned char *data, unsigned int length,
+int csret_cs_detect_malware(csret_cs_context_s *context, const RawBuffer &data,
                                                 csret_cs_detected_s **pdetected)
 {
-       char *virus_signature = nullptr;
-       csret_cs_detected_s *detected = nullptr;
-       int ret = CSRE_ERROR_NONE;
-
        if (context == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       if (data == nullptr || pdetected == nullptr)
+       if (data.empty())
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       if (virus_sig == nullptr)
+       if (g_virus_sig.empty())
                return CSRE_ERROR_ENGINE_NOT_ACTIVATED;
 
-       // comare data with virus signature
-       csret_cs_malware_list_s *curr_sig = virus_sig;
-
-       while (curr_sig != nullptr) {
-               virus_signature = curr_sig->malware.signature;
-
-               if (csret_cs_compare_data(data, length, virus_signature, strlen(virus_signature)) == 0) { // detected
-                       //printf("..csret_cs_detect_malware: detected signature=%s\n", virus_signature);
-                       // create new detected
-                       detected = (csret_cs_detected_s *)calloc(sizeof(csret_cs_detected_s), 1);
-
-                       if (detected == nullptr)
-                               return CSRE_ERROR_OUT_OF_MEMORY;
-
-                       // set detected into context
-                       csret_cs_detected_list_s *last = (csret_cs_detected_list_s *) calloc(sizeof(csret_cs_detected_list_s), 1);
-
-                       if (last == nullptr) {
-                               free(detected);
-                               return CSRE_ERROR_OUT_OF_MEMORY;
-                       }
-
-                       last->detected = detected;
-                       csret_cs_detected_list_s *curr = context->detected_list;
+       for (auto &item : g_virus_sig) {
+               if (csret_cs_compare_data(data, item.signature) != 0)
+                       continue;
 
-                       while (curr != nullptr && curr->next != nullptr) curr = curr->next; // move to the last
+               csret_cs_detected_s detected;
+               detected.malware.severity = item.severity;
+               detected.malware.threat_type = item.threat_type;
+               detected.malware.name = item.name;
+               detected.malware.detailed_url = item.detailed_url;
+               detected.timestamp = csret_cs_get_timestamp();
 
-                       curr = last;
-                       // set values into detected
-                       detected->malware.severity = curr_sig->malware.severity;
-                       detected->malware.threat_type = curr_sig->malware.threat_type;
-                       snprintf(detected->malware.malware_name, MAX_NAME_LEN, "%s", curr_sig->malware.malware_name);
-                       snprintf(detected->malware.detailed_url, MAX_URL_LEN, "%s", curr_sig->malware.detailed_url);
-                       detected->timestamp = csret_cs_get_timestamp();
-                       break; // return the first malware in test engine.
-               }
+               context->detected_list.push_back(detected);
+               *pdetected = &context->detected_list.back();
 
-               curr_sig = curr_sig->next;
+               return CSRE_ERROR_NONE;
        }
 
-       // set detected into context
-       csret_cs_detected_list_s *last = (csret_cs_detected_list_s *) calloc(sizeof(csret_cs_detected_list_s), 1);
+       *pdetected = nullptr;
 
-       if (last == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
-
-       last->detected = detected;
-       csret_cs_detected_list_s *curr = context->detected_list;
-
-       while (curr != nullptr && curr->next != nullptr) curr = curr->next; // move to the last
-
-       curr = last;
-       *pdetected = detected;
-
-       return ret;
+       return CSRE_ERROR_NONE;
 }
 
 //==============================================================================
 // Main function related
 //==============================================================================
-
 API
-int csre_cs_context_create(const char *engine_root_dir, csre_cs_context_h *phandle)
+int csre_cs_global_initialize(const char *ro_res_dir, const char *rw_working_dir)
 {
-       int ret = CSRE_ERROR_NONE;
-       char sig_file[MAX_FILE_PATH_LEN] = {0, };
-
-       if (phandle == nullptr || engine_root_dir == nullptr)
+       if (ro_res_dir == nullptr || rw_working_dir == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       csret_cs_context_s *context = (csret_cs_context_s *)calloc(sizeof(csret_cs_context_s), 1);
+       g_resdir = ro_res_dir;
+       g_workingdir = rw_working_dir;
 
-       if (context == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
+       g_virus_sig.clear();
 
-       if (engine_info == nullptr) {
-               ret = csret_cs_init_engine(engine_root_dir);
+       return csret_cs_read_virus_signatures(g_workingdir + "/" PRIVATE_DB_NAME);
+}
 
-               if (ret != CSRE_ERROR_NONE)
-                       return ret;
-       }
+API
+int csre_cs_global_deinitialize()
+{
+       return CSRE_ERROR_NONE;
+}
 
-       if (virus_sig == nullptr) {
-               snprintf(sig_file, sizeof(sig_file), "%s/%s", engine_root_dir, PRIVATE_DB_NAME);
-               ret = csret_cs_read_virus_signatures(sig_file);
+API
+int csre_cs_context_create(csre_cs_context_h *phandle)
+{
+       if (phandle == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
 
-               if (ret != CSRE_ERROR_NONE && ret != CSRET_CS_ERROR_NO_SIGNATURE_FILE)
-                       return ret;
-       }
+       if (g_virus_sig.empty())
+               return CSRE_ERROR_INVALID_HANDLE; // not yet initialized
+
+       auto context = new csret_cs_context_s;
+
+       *phandle = reinterpret_cast<csre_cs_context_h>(context);
 
-       *phandle = (csre_cs_context_h) context;
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_context_destroy(csre_cs_context_h handle)
 {
-       if (handle == nullptr)
-               return CSRE_ERROR_INVALID_HANDLE;
+       auto context = reinterpret_cast<csret_cs_context_s *>(handle);
 
-       csret_cs_context_s *context = (csret_cs_context_s *)handle;
-       csret_cs_detected_list_s *curr = nullptr;
-       csret_cs_detected_list_s *prev = nullptr;
-       curr = context->detected_list;
-
-       while (curr != nullptr) {
-               if (curr->detected != nullptr)
-                       free(curr->detected);
+       if (context == nullptr)
+               return CSRE_ERROR_INVALID_HANDLE;
 
-               prev = curr;
-               curr = curr->next;
-               free(prev);
-       }
+       delete context;
 
-       free(context);
        return CSRE_ERROR_NONE;
 }
 
@@ -469,20 +351,19 @@ int csre_cs_scan_data(csre_cs_context_h handle,
                                          size_t length,
                                          csre_cs_detected_h *pdetected)
 {
-       int ret = CSRE_ERROR_NONE;
-       csret_cs_detected_s *detected = nullptr;
+       auto context = reinterpret_cast<csret_cs_context_s *>(handle);
 
-       if (handle == nullptr)
+       if (context == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       csret_cs_context_s *context = (csret_cs_context_s *)handle;
-
-       ret = csret_cs_detect_malware(context, data, length, &detected);
-
+       RawBuffer vec(data, data + length);
+       csret_cs_detected_s *detected = nullptr;
+       int ret = csret_cs_detect_malware(context, vec, &detected);
        if (ret != CSRE_ERROR_NONE)
                return ret;
 
-       *pdetected = (csre_cs_detected_h) detected;
+       *pdetected = reinterpret_cast<csre_cs_detected_h>(detected);
+
        return ret;
 }
 
@@ -491,11 +372,6 @@ int csre_cs_scan_file(csre_cs_context_h handle,
                                          const char *file_path,
                                          csre_cs_detected_h *pdetected)
 {
-       csret_cs_detected_s *detected = nullptr;
-       unsigned char *data;
-       unsigned int data_len;
-       int ret = CSRE_ERROR_NONE;
-
        if (file_path == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
@@ -506,21 +382,19 @@ int csre_cs_scan_file(csre_cs_context_h handle,
        if (handle == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       csret_cs_context_s *context = (csret_cs_context_s *)handle;
+       auto context = reinterpret_cast<csret_cs_context_s *>(handle);
 
-       ret = csret_cs_read_binary(file_path, &data, &data_len);
+       RawBuffer vec;
+       int ret = csret_cs_read_binary(file_path, vec);
        if (ret != CSRE_ERROR_NONE)
                return ret;
 
-       ret = csret_cs_detect_malware(context, data, data_len, &detected);
-
-       if (ret != CSRE_ERROR_NONE) {
-               if (data)
-                       free(data);
+       csret_cs_detected_s *detected = nullptr;
+       ret = csret_cs_detect_malware(context, vec, &detected);
+       if (ret != CSRE_ERROR_NONE)
                return ret;
-       }
 
-       *pdetected = (csre_cs_detected_h) detected;
+       *pdetected = reinterpret_cast<csre_cs_detected_h>(detected);
 
        return CSRE_ERROR_NONE;
 }
@@ -530,54 +404,54 @@ int csre_cs_scan_app_on_cloud(csre_cs_context_h handle,
                                                          const char *app_root_dir,
                                                          csre_cs_detected_h *pdetected)
 {
+       if (handle == nullptr || app_root_dir == nullptr || pdetected == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
+
        int ret;
-       DIR *dir;
-       struct dirent entry;
-       struct dirent *result;
-       csret_cs_detected_s *detected = nullptr;
-       csret_cs_detected_s *most_severe= nullptr;
-       int path_length;
-       char path[MAX_FILE_PATH_LEN] = {0 };
+       csre_cs_detected_h detected = nullptr;
+       csre_cs_detected_h most_detected = nullptr;
+       csre_cs_severity_level_e most = CSRE_CS_SEVERITY_LOW;
 
-       dir = opendir(app_root_dir);
-       if(!dir)
+       std::unique_ptr<DIR, std::function<int(DIR *)>> dirp(opendir(app_root_dir), closedir);
+       if (!dirp)
                return CSRE_ERROR_FILE_NOT_FOUND;
 
-       while ((!readdir_r(dir, &entry, &result))) {
-               if(result == nullptr) // when the end of the directory stread is reached
-                       break;
 
-               path_length = snprintf(path, MAX_FILE_PATH_LEN, "%s/%s", app_root_dir, entry.d_name);
-               if(path_length >= MAX_FILE_PATH_LEN) {
-                       ret = CSRE_ERROR_UNKNOWN;
-                       goto error;
-               }
+       struct dirent entry;
+       struct dirent *result;
+       while (readdir_r(dirp.get(), &entry, &result) == 0 && result != nullptr) {
+               std::string fullpath(app_root_dir);
+               std::string filename(entry.d_name);
+               fullpath += "/";
+               fullpath += filename;
+
+               if (entry.d_type & (DT_REG | DT_LNK))
+                       ret = csre_cs_scan_file(handle, fullpath.c_str(), &detected);
+               else if ((entry.d_type & DT_DIR)
+                               && filename.compare("..") != 0
+                               && filename.compare(".") != 0)
+                       ret = csre_cs_scan_app_on_cloud(handle, fullpath.c_str(), &detected);
+               else
+                       continue;
 
-               if( (entry.d_type & DT_REG) || (entry.d_type & DT_LNK) ) {
-                       ret = csre_cs_scan_file(handle, path, (csre_cs_detected_h *)(&detected) );
-               } else if( (entry.d_type & DT_DIR)
-                               && (strcmp(entry.d_name,"..") != 0)
-                               && (strcmp(entry.d_name,".") != 0) ) {
-                       ret = csre_cs_scan_app_on_cloud(handle, path, (csre_cs_detected_h *)(&detected) );
-               } else {
+               if (ret != CSRE_ERROR_NONE)
+                       return ret;
+
+               if (detected == nullptr)
                        continue;
-               }
 
-               if(ret != CSRE_ERROR_NONE)
-                       goto error;
-               if(detected != nullptr) { // detected
-                       if(most_severe == nullptr || detected->malware.severity > most_severe->malware.severity)
-                               most_severe = detected;
-                       else
-                               detected = nullptr;
+               csre_cs_severity_level_e s = CSRE_CS_SEVERITY_LOW;
+               ret = csre_cs_detected_get_severity(detected, &s);
+               if (ret != CSRE_ERROR_NONE)
+                       return ret;
+
+               if (most_detected == nullptr || s > most) {
+                       most_detected = detected;
+                       most = s;
                }
        }
 
-error:
-       if(dir != nullptr)
-               closedir(dir);
-
-       *pdetected = (csre_cs_detected_h) detected;
+       *pdetected = reinterpret_cast<csre_cs_detected_h>(detected);
 
        return ret;
 }
@@ -588,15 +462,13 @@ error:
 API
 int csre_cs_detected_get_severity(csre_cs_detected_h detected, csre_cs_severity_level_e *pseverity)
 {
-       csret_cs_detected_s *pdetected = nullptr;
-
        if (detected == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (pseverity == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       pdetected = (csret_cs_detected_s *) detected;
+       auto pdetected = reinterpret_cast<csret_cs_detected_s *>(detected);
        *pseverity = pdetected->malware.severity;
        return CSRE_ERROR_NONE;
 }
@@ -605,15 +477,13 @@ API
 int csre_cs_detected_get_threat_type(csre_cs_detected_h detected,
                                                                         csre_cs_threat_type_e *pthreat_type)
 {
-       csret_cs_detected_s *pdetected = nullptr;
-
        if (detected == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (pthreat_type == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       pdetected = (csret_cs_detected_s *) detected;
+       auto pdetected = reinterpret_cast<csret_cs_detected_s *>(detected);
        *pthreat_type = pdetected->malware.threat_type;
        return CSRE_ERROR_NONE;
 }
@@ -621,47 +491,41 @@ int csre_cs_detected_get_threat_type(csre_cs_detected_h detected,
 API
 int csre_cs_detected_get_malware_name(csre_cs_detected_h detected, const char **malware_name)
 {
-       csret_cs_detected_s *pdetected = nullptr;
-
        if (detected == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (malware_name == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       pdetected = (csret_cs_detected_s *) detected;
-       *malware_name = pdetected->malware.malware_name;
+       auto pdetected = reinterpret_cast<csret_cs_detected_s *>(detected);
+       *malware_name = pdetected->malware.name.c_str();
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_detected_get_detailed_url(csre_cs_detected_h detected, const char **detailed_url)
 {
-       csret_cs_detected_s *pdetected = nullptr;
-
        if (detected == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (detailed_url == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       pdetected = (csret_cs_detected_s *) detected;
-       *detailed_url = pdetected->malware.detailed_url;
+       auto pdetected = reinterpret_cast<csret_cs_detected_s *>(detected);
+       *detailed_url = pdetected->malware.detailed_url.c_str();
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_detected_get_timestamp(csre_cs_detected_h detected, time_t *timestamp)
 {
-       csret_cs_detected_s *pdetected = nullptr;
-
        if (detected == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (timestamp == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       pdetected = (csret_cs_detected_s *) detected;
+       auto pdetected = reinterpret_cast<csret_cs_detected_s *>(detected);
        *timestamp = pdetected->timestamp;
        return CSRE_ERROR_NONE;
 }
@@ -675,14 +539,27 @@ int csre_cs_engine_get_info(csre_cs_engine_h *pengine)
        if (pengine == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *pengine = (csre_cs_engine_h)engine_info;
+       auto ptr = csret_cs_init_engine();
+       *pengine = reinterpret_cast<csre_cs_engine_h>(ptr);
+
+       return CSRE_ERROR_NONE;
+}
+
+API
+int csre_cs_engine_destroy(csre_cs_engine_h engine)
+{
+       if (engine == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
+
+       delete reinterpret_cast<csret_cs_engine_s *>(engine);
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_engine_get_vendor(csre_cs_engine_h engine, const char **vendor)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -690,46 +567,49 @@ int csre_cs_engine_get_vendor(csre_cs_engine_h engine, const char **vendor)
        if (vendor == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *vendor = eng->vendor_name;
+       *vendor = eng->vendorName.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
-int csre_cs_engine_get_name(csre_cs_engine_h engine, const char **engine_name)
+int csre_cs_engine_get_name(csre_cs_engine_h engine, const char **name)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       if (engine_name == nullptr)
+       if (name == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *engine_name = eng->engine_name;
+       *name = eng->engineName.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
-int csre_cs_engine_get_vendor_logo(csre_cs_engine_h engine, unsigned char **vendor_logo_image,
+int csre_cs_engine_get_vendor_logo(csre_cs_engine_h engine, unsigned char **logo_image,
                                                                   unsigned int *image_size)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       if (vendor_logo_image == nullptr || image_size == nullptr)
+       if (logo_image == nullptr || image_size == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *vendor_logo_image = eng->vendor_logo_image;
-       *image_size = eng->image_size;
+       *logo_image = eng->logoImage.data();
+       *image_size = eng->logoImage.size();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_engine_get_version(csre_cs_engine_h engine, const char **version)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -737,14 +617,15 @@ int csre_cs_engine_get_version(csre_cs_engine_h engine, const char **version)
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = eng->engine_version;
+       *version = eng->engineVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_engine_get_data_version(csre_cs_engine_h engine, const char **version)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -752,14 +633,15 @@ int csre_cs_engine_get_data_version(csre_cs_engine_h engine, const char **versio
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = eng->data_version;
+       *version = eng->dataVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_engine_get_latest_update_time(csre_cs_engine_h engine, time_t *time)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -767,14 +649,15 @@ int csre_cs_engine_get_latest_update_time(csre_cs_engine_h engine, time_t *time)
        if (time == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *time = eng->latest_update;
+       *time = eng->latestUpdate;
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_cs_engine_get_activated(csre_cs_engine_h engine, csre_cs_activated_e *pactivated)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -782,7 +665,7 @@ int csre_cs_engine_get_activated(csre_cs_engine_h engine, csre_cs_activated_e *p
        if (pactivated == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       if (virus_sig == nullptr)
+       if (g_virus_sig.empty())
                *pactivated = CSRE_CS_NOT_ACTIVATED;
        else
                *pactivated = CSRE_CS_ACTIVATED;
@@ -793,7 +676,7 @@ int csre_cs_engine_get_activated(csre_cs_engine_h engine, csre_cs_activated_e *p
 API
 int csre_cs_engine_get_api_version(csre_cs_engine_h engine, const char **version)
 {
-       csret_cs_engine_s *eng = (csret_cs_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_cs_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -801,7 +684,8 @@ int csre_cs_engine_get_api_version(csre_cs_engine_h engine, const char **version
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = CSRE_CS_API_VERSION;
+       *version = eng->apiVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
@@ -820,8 +704,8 @@ int csre_cs_get_error_string(int error_code, const char **string)
                *string = "CSRET_CS_ERROR_NO_SIGNATURE_FILE";
                break;
 
-       case CSRET_CS_ERROR_SIGNARUE_FILE_FORMAT:
-               *string = "CSRET_CS_ERROR_SIGNARUE_FILE_FORMAT";
+       case CSRET_CS_ERROR_SIGNATURE_FILE_FORMAT:
+               *string = "CSRET_CS_ERROR_SIGNATURE_FILE_FORMAT";
                break;
 
        case CSRET_CS_ERROR_FILE_IO:
@@ -835,7 +719,3 @@ int csre_cs_get_error_string(int error_code, const char **string)
 
        return CSRE_ERROR_NONE;
 }
-
-#ifdef __cplusplus
-}
-#endif
index 1383766..19561f6 100644 (file)
@@ -42,4 +42,4 @@ TARGET_LINK_LIBRARIES(${TARGET_CSR_WP_ENGINE_SAMPLE}
 )
 
 INSTALL(TARGETS ${TARGET_CSR_WP_ENGINE_SAMPLE} DESTINATION ${SAMPLE_ENGINE_DIR})
-INSTALL(DIRECTORY resources/ DESTINATION ${SAMPLE_ENGINE_WORKING_DIR})
+INSTALL(DIRECTORY resources/ DESTINATION ${SAMPLE_ENGINE_RW_WORKING_DIR})
index f53f685..269ac40 100644 (file)
 #include "csre/web-protection.h"
 #include "csre/web-protection-engine-info.h"
 
-#include <cstdlib>
-#include <cstdio>
-#include <cstring>
+#include <string>
+#include <vector>
+#include <memory>
+#include <functional>
+#include <list>
+#include <fstream>
+#include <iostream>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #define PRIVATE_DB_NAME   "csret_wp_risky_urls"
 #define PRIVATE_LOGO_FILE "vendor_logo.bmp"
-#define MAX_FILE_PATH_LEN 256
-#define MAX_NAME_LEN      64
-#define MAX_VERSION_LEN   32
-#define MAX_URL_LEN       256
 
 #define VENDOR_NAME       "TEST_VENDOR"
 #define ENGINE_NAME       "TEST_LOCAL_TWP_ENGINE"
 #define ENGINE_VERSION    "0.0.1"
 
-typedef struct __csret_wp_risky_url {
-       char url[MAX_URL_LEN];
+using RawBuffer = std::vector<unsigned char>;
+
+// TODO(k.tak): make all engine functions exception-safe
+
+struct csret_wp_risky_url_s {
+       std::string url;
        csre_wp_risk_level_e risk_level;
-       char detailed_url[MAX_URL_LEN];
-} csret_wp_risky_url_s;
-
-typedef struct __csret_wp_risky_url_list {
-       struct __csret_wp_risky_url_list   *next;
-       csret_wp_risky_url_s               *risky_url;
-} csret_wp_risky_url_list_s;
-
-typedef struct __csret_wp_context {
-       csret_wp_risky_url_list_s *detected_list;
-} csret_wp_context_s;
-
-typedef struct __csret_wp_engine {
-       char           vendor_name[MAX_NAME_LEN];
-       char           engine_name[MAX_NAME_LEN];
-       unsigned char *vendor_logo_image;
-       unsigned int   image_size;
-       char           engine_version[MAX_VERSION_LEN];
-       char           data_version[MAX_VERSION_LEN];
-       time_t         latest_update;
-} csret_wp_engine_s;
-
-typedef enum __csret_wp_internal_error {
-       CSRET_WP_ERROR_NO_RISKY_URL_FILE    = -0x0101,
-       CSRET_WP_ERROR_FILE_IO              = -0x0103
-} csret_wp_internal_error_e;
+       std::string detailed_url;
+
+       csret_wp_risky_url_s() : risk_level(CSRE_WP_RISK_UNVERIFIED) {}
+};
+
+struct csret_wp_context_s {
+       std::list<csret_wp_risky_url_s> detected_list;
+};
+
+struct csret_wp_engine_s {
+       std::string vendorName;
+       std::string engineName;
+       std::string apiVersion;
+       std::string engineVersion;
+       std::string dataVersion;
+       RawBuffer logoImage;
+       time_t latestUpdate;
+};
+
+enum csret_wp_internal_error_e {
+       CSRET_WP_ERROR_NO_RISKY_URL_FILE     = -0x0101,
+       CSRET_WP_ERROR_SIGNATURE_FILE_FORMAT = -0x0102,
+       CSRET_WP_ERROR_FILE_IO               = -0x0103,
+};
 
 //==============================================================================
 // static variables
 //==============================================================================
-static csret_wp_engine_s         *engine_info = nullptr;
-static csret_wp_risky_url_list_s *risky_urls  = nullptr;
+static std::string g_resdir;
+static std::string g_workingdir;
+static std::list<csret_wp_risky_url_s> g_risky_urls;
 
 //==============================================================================
 // Utilities functions
 //==============================================================================
-char *csret_wp_extract_value(char *line, const char *key)
+std::string csret_wp_extract_value(const std::string &line, const std::string &key)
 {
-       if (line == nullptr || key == nullptr)
-               return nullptr;
+       if (line.empty() || key.empty())
+               return std::string();
 
-       auto found = strstr(line, key);
-       if (found != line)
-               return nullptr;
-
-       auto value = found + strlen(key);
+       auto pos = line.find(key);
+       if (pos == std::string::npos || pos != 0)
+               return std::string();
 
-       // remove end line char
-       for (auto current = found; current && *current; current++) {
-               if (*current == '\n') {
-                       *current = '\0';
-                       break;
-               }
-       }
-
-       return value;
+       return line.substr(key.length());
 }
 
-int csret_wp_read_risky_urls(const char *path)
+int csret_wp_read_risky_urls(const std::string &path)
 {
        // csret_wp_risky_urls file format
        // data_version=1.0.0 // it should be in the first line.
@@ -114,144 +107,100 @@ int csret_wp_read_risky_urls(const char *path)
        // risk_level=HIGH  // LOW/MEDIUM/HIGH
        // detailed_url=http://high.risky.com
        //
-       // url=midiumrisky.test.com
+       // url=mediumrisky.test.com
        // risk_level=MEDIUM
        // detailed_url=http://medium.risky.com
-       FILE *fp;
-       char *line = nullptr;
-       size_t len = 0;
-       ssize_t read;
-       char *value;
-       csret_wp_risky_url_list_s *curr_url = nullptr;
-       csret_wp_risky_url_list_s *next_url = nullptr;
-       fp = fopen(path, "r");
-
-       if (fp == nullptr)
+       std::ifstream f(path.c_str());
+       if (!f.is_open())
                return CSRET_WP_ERROR_NO_RISKY_URL_FILE;
 
-       while ((read = getline(&line, &len, fp)) != -1) {
-               if (line == nullptr || strlen(line) == 0)
+       std::string line;
+       csret_wp_risky_url_s node;
+       while (std::getline(f, line)) {
+               if (line.empty() || line[0] == '#')
                        continue;
 
-               value = csret_wp_extract_value(line, "data_version=");
-
-               if (value != nullptr && engine_info != nullptr)
-                       snprintf(engine_info->data_version, MAX_VERSION_LEN, "%s", value);
-
-               value = csret_wp_extract_value(line, "url=");
-
-               if (value != nullptr) {
-                       next_url = (csret_wp_risky_url_list_s *) calloc(sizeof(csret_wp_risky_url_list_s), 1);
-                       next_url->risky_url = (csret_wp_risky_url_s *) calloc(sizeof(csret_wp_risky_url_s), 1);
+               auto value = csret_wp_extract_value(line, "url=");
+               if (!value.empty()) {
+                       if (!node.url.empty()) {
+                               g_risky_urls.push_back(node);
+                               node = csret_wp_risky_url_s();
+                       }
 
-                       if (curr_url != nullptr) curr_url->next = next_url;
-                       else                 risky_urls = next_url;
-
-                       curr_url = next_url;
-                       snprintf(curr_url->risky_url->url, MAX_URL_LEN, "%s", value);
+                       node.url = std::move(value);
+                       continue;
                }
 
                value = csret_wp_extract_value(line, "risk_level=");
-
-               if (value != nullptr) {
-                       if (strcmp(value, "LOW") == 0)
-                               curr_url->risky_url->risk_level = CSRE_WP_RISK_LOW;
-                       else if (strcmp(value, "MEDIUM") == 0)
-                               curr_url->risky_url->risk_level = CSRE_WP_RISK_MEDIUM;
-                       else if (strcmp(value, "HIGH") == 0)
-                               curr_url->risky_url->risk_level = CSRE_WP_RISK_HIGH;
+               if (!value.empty()) {
+                       if (value.compare("LOW") == 0)
+                               node.risk_level = CSRE_WP_RISK_LOW;
+                       else if (value.compare("MEDIUM") == 0)
+                               node.risk_level = CSRE_WP_RISK_MEDIUM;
+                       else if (value.compare("HIGH") == 0)
+                               node.risk_level = CSRE_WP_RISK_HIGH;
                        else
-                               curr_url->risky_url->risk_level = CSRE_WP_RISK_UNVERIFIED;
+                               node.risk_level = CSRE_WP_RISK_UNVERIFIED;
+
+                       continue;
                }
 
                value = csret_wp_extract_value(line, "detailed_url=");
-
-               if (value != nullptr)
-                       strncpy(curr_url->risky_url->detailed_url, value, sizeof(curr_url->risky_url->detailed_url) - 1);
+               if (!value.empty())
+                       node.detailed_url = std::move(value);
        }
 
-       free(line);
-       fclose(fp);
+       if (!node.url.empty())
+               g_risky_urls.push_back(node);
+
        return CSRE_ERROR_NONE;
 }
 
-
-int csret_wp_csret_wp_read_binary_by_file(FILE *file, unsigned char **data, unsigned int *len)
+int csret_wp_read_binary(const std::string &path, RawBuffer &buffer)
 {
-       unsigned char *buffer;
-       long int fileLen;
-       int read;
-       int index = 0;
-
-       if (!file)
-               return CSRET_WP_ERROR_FILE_IO;
-
-       //Get file length
-       fseek(file, 0, SEEK_END);
-       fileLen = ftell(file);
+       std::ifstream f(path.c_str(), std::ios::binary);
 
-       if (fileLen <= 0)
-               return CSRET_WP_ERROR_FILE_IO;
-
-       fseek(file, 0, SEEK_SET);
-       //Allocate memory
-       buffer = (unsigned char *)calloc(fileLen + 1, 1);
-
-       if (!buffer)
-               return CSRE_ERROR_OUT_OF_MEMORY;
+       if (!f.is_open()) {
+               buffer.clear();
+               return CSRE_ERROR_NONE;
+       }
 
-       //Read file contents into buffer
-       while ((read = fread(buffer + index, 1, fileLen, file)) > 0)
-               index += read;
+       f.seekg(0, f.end);
+       auto len = f.tellg();
+       f.seekg(0, f.beg);
 
-       fclose(file);
+       buffer.resize(len, 0);
+       f.read(reinterpret_cast<char *>(buffer.data()), buffer.size());
 
-       if (index != fileLen) {
-               free(buffer);
+       if (!f) {
+               buffer.clear();
                return CSRET_WP_ERROR_FILE_IO;
        }
 
-       *data = buffer;
-       *len = fileLen;
        return CSRE_ERROR_NONE;
 }
 
-int csret_wp_read_binary(const char *path, unsigned char **data, unsigned int *len)
+csret_wp_engine_s *csret_wp_init_engine()
 {
-       FILE *file = fopen(path, "rb");
-       return csret_wp_csret_wp_read_binary_by_file(file, data, len);
-}
+       auto ptr = new csret_wp_engine_s;
 
+       ptr->vendorName = VENDOR_NAME;
+       ptr->engineName = ENGINE_NAME;
+       ptr->apiVersion = CSRE_WP_API_VERSION;
+       ptr->engineVersion = ENGINE_VERSION;
+       ptr->dataVersion = ENGINE_VERSION;
 
-int csret_wp_init_engine(const char *root_dir)
-{
-       int ret = CSRE_ERROR_NONE;
-       char db_file_name[MAX_FILE_PATH_LEN] = {0, };
-       char logo_file_name[MAX_FILE_PATH_LEN] = {0, };
-       struct stat attrib;
-       engine_info = (csret_wp_engine_s *) calloc(sizeof(csret_wp_engine_s), 1);
-
-       if (engine_info == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
-
-       snprintf(engine_info->vendor_name, MAX_NAME_LEN, "%s", VENDOR_NAME);
-       snprintf(engine_info->engine_name, MAX_NAME_LEN, "%s", ENGINE_NAME);
-       snprintf(engine_info->engine_version, MAX_VERSION_LEN, "%s", ENGINE_VERSION);
-       snprintf(db_file_name, MAX_FILE_PATH_LEN, "%s/%s", root_dir, PRIVATE_DB_NAME);
-       snprintf(logo_file_name, MAX_FILE_PATH_LEN, "%s/%s", root_dir, PRIVATE_LOGO_FILE);
-       ret = csret_wp_read_binary(logo_file_name, &(engine_info->vendor_logo_image),
-                                                       &(engine_info->image_size));
-
-       if (ret == CSRET_WP_ERROR_FILE_IO) {
-               engine_info->vendor_logo_image = nullptr;
-               engine_info->image_size = 0;
-               ret = CSRE_ERROR_NONE;
+       int ret = csret_wp_read_binary(g_resdir + "/" PRIVATE_LOGO_FILE, ptr->logoImage);
+       if (ret != CSRE_ERROR_NONE) {
+               delete ptr;
+               return nullptr;
        }
 
-       stat(db_file_name, &attrib);
-       engine_info->latest_update = attrib.st_mtime;
+       struct stat attrib;
+       stat(PRIVATE_DB_NAME, &attrib);
+       ptr->latestUpdate = attrib.st_mtime;
 
-       return ret;
+       return ptr;
 }
 
 
@@ -259,120 +208,89 @@ int csret_wp_init_engine(const char *root_dir)
 // Main function related
 //==============================================================================
 API
-int csre_wp_context_create(const char *engine_root_dir, csre_wp_context_h *phandle)
+int csre_wp_global_initialize(const char *ro_res_dir, const char *rw_working_dir)
 {
-       int ret = CSRE_ERROR_NONE;
-       char url_file[MAX_FILE_PATH_LEN] = {0, };
-
-       if (phandle == nullptr || engine_root_dir == nullptr)
+       if (ro_res_dir == nullptr || rw_working_dir == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       csret_wp_context_s *context = (csret_wp_context_s *)calloc(sizeof(csret_wp_context_s), 1);
-
-       if (context == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
+       g_resdir = ro_res_dir;
+       g_workingdir = rw_working_dir;
 
-       if (engine_info == nullptr) {
-               ret = csret_wp_init_engine(engine_root_dir);
+       g_risky_urls.clear();
 
-               if (ret != CSRE_ERROR_NONE)
-                       return ret;
-       }
+       int ret = csret_wp_read_risky_urls(g_workingdir + "/" PRIVATE_DB_NAME);
+       if (ret != CSRE_ERROR_NONE && ret != CSRET_WP_ERROR_NO_RISKY_URL_FILE)
+               return ret;
 
-       if (risky_urls == nullptr) {
-               snprintf(url_file, MAX_FILE_PATH_LEN, "%s/%s", engine_root_dir, PRIVATE_DB_NAME);
-               ret = csret_wp_read_risky_urls(url_file);
 
-               if (ret != CSRE_ERROR_NONE && ret != CSRET_WP_ERROR_NO_RISKY_URL_FILE)
-                       return ret;
-       }
+       return CSRE_ERROR_NONE;
+}
 
-       *phandle = (csre_wp_context_h) context;
+API
+int csre_wp_global_deinitialize()
+{
        return CSRE_ERROR_NONE;
 }
 
 API
-int csre_wp_context_destroy(csre_wp_context_h handle)
+int csre_wp_context_create(csre_wp_context_h *phandle)
 {
-       if (handle == nullptr)
-               return CSRE_ERROR_INVALID_HANDLE;
+       if (phandle == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
 
-       csret_wp_context_s *context = (csret_wp_context_s *)handle;
-       csret_wp_risky_url_list_s *curr = nullptr;
-       csret_wp_risky_url_list_s *prev = nullptr;
-       curr = context->detected_list;
+       if (g_risky_urls.empty())
+               return CSRE_ERROR_INVALID_HANDLE; // not yet initialized
 
-       while (curr != nullptr) {
-               if (curr->risky_url != nullptr)
-                       free(curr->risky_url);
+       auto context = new csret_wp_context_s;
 
-               prev = curr;
-               curr = curr->next;
-               free(prev);
-       }
+       *phandle = reinterpret_cast<csre_wp_context_h>(context);
 
-       free(context);
        return CSRE_ERROR_NONE;
 }
 
 API
-int csre_wp_check_url(csre_wp_context_h handle, const char *url, csre_wp_check_result_h *presult)
+int csre_wp_context_destroy(csre_wp_context_h handle)
 {
-       int ret = CSRE_ERROR_NONE;
-       char *risky_url = nullptr;
-       csret_wp_risky_url_s *detected = nullptr;
+       auto context = reinterpret_cast<csret_wp_context_s *>(handle);
 
-       if (handle == nullptr)
+       if (context == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
-       if (url == nullptr || presult == nullptr)
-               return CSRE_ERROR_INVALID_PARAMETER;
-
-       if (risky_urls == nullptr)
-               return CSRE_ERROR_ENGINE_NOT_ACTIVATED;
+       delete context;
 
-       csret_wp_context_s *context = (csret_wp_context_s *)handle;
-       // create new detected
-       detected = (csret_wp_risky_url_s *)calloc(sizeof(csret_wp_risky_url_s), 1);
-
-       if (detected == nullptr)
-               return CSRE_ERROR_OUT_OF_MEMORY;
-
-       snprintf(detected->url, MAX_URL_LEN, "%s", url);
-       // set detected into context
-       csret_wp_risky_url_list_s *last = (csret_wp_risky_url_list_s *) calloc(sizeof(csret_wp_risky_url_list_s), 1);
+       return CSRE_ERROR_NONE;
+}
 
-       if (last == nullptr) {
-               free(detected);
-               return CSRE_ERROR_OUT_OF_MEMORY;
-       }
+API
+int csre_wp_check_url(csre_wp_context_h handle, const char *url, csre_wp_check_result_h *presult)
+{
+       auto context = reinterpret_cast<csret_wp_context_s *>(handle);
 
-       last->risky_url = detected;
-       csret_wp_risky_url_list_s *curr = context->detected_list;
+       if (context == nullptr)
+               return CSRE_ERROR_INVALID_HANDLE;
 
-       while (curr != nullptr && curr->next != nullptr) curr = curr->next; // move to the last
+       if (url == nullptr || presult == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
 
-       curr = last;
-       // check url
-       csret_wp_risky_url_list_s *curr_url = risky_urls;
+       std::string in_url(url);
 
-       while (curr_url != nullptr) {
-               risky_url = curr_url->risky_url->url;
+       csret_wp_risky_url_s detected;
+       detected.url = in_url;
+       // check url from static risky urls list
+       for (auto &item : g_risky_urls) {
+               if (in_url.find(item.url) == std::string::npos)
+                       continue;
 
-               if (strstr(url, risky_url) != nullptr) { // found
-                       detected->risk_level = curr_url->risky_url->risk_level;
-                       snprintf(detected->detailed_url, MAX_URL_LEN, "%s", curr_url->risky_url->detailed_url);
-                       break; // return the first risky url info in test engine
-               }
+               detected.risk_level = item.risk_level;
+               detected.detailed_url = item.detailed_url;
 
-               curr_url = curr_url->next;
+               break;
        }
 
-       if (detected->risk_level == 0)
-               detected->risk_level = CSRE_WP_RISK_UNVERIFIED;
+       context->detected_list.push_back(detected);
+       *presult = reinterpret_cast<csre_wp_check_result_h>(&context->detected_list.back());
 
-       *presult = (csre_wp_check_result_h) detected;
-       return ret;
+       return CSRE_ERROR_NONE;
 }
 
 
@@ -382,32 +300,32 @@ int csre_wp_check_url(csre_wp_context_h handle, const char *url, csre_wp_check_r
 API
 int csre_wp_result_get_risk_level(csre_wp_check_result_h result, csre_wp_risk_level_e *plevel)
 {
-       csret_wp_risky_url_s *detected = nullptr;
-
        if (result == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (plevel == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       detected = (csret_wp_risky_url_s *) result;
+       auto detected = reinterpret_cast<csret_wp_risky_url_s *>(result);
+
        *plevel = detected->risk_level;
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_wp_result_get_detailed_url(csre_wp_check_result_h result, const char** detailed_url)
 {
-       csret_wp_risky_url_s *detected = nullptr;
-
        if (result == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
 
        if (detailed_url == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       detected = (csret_wp_risky_url_s *) result;
-       *detailed_url = detected->detailed_url;
+       auto detected = reinterpret_cast<csret_wp_risky_url_s *>(result);
+
+       *detailed_url = detected->detailed_url.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
@@ -420,14 +338,27 @@ int csre_wp_engine_get_info(csre_wp_engine_h *pengine)
        if (pengine == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *pengine = (csre_wp_engine_h)engine_info;
+       auto ptr = csret_wp_init_engine();
+       *pengine = reinterpret_cast<csre_wp_engine_h>(ptr);
+
+       return CSRE_ERROR_NONE;
+}
+
+API
+int csre_wp_engine_destroy(csre_wp_engine_h engine)
+{
+       if (engine == nullptr)
+               return CSRE_ERROR_INVALID_PARAMETER;
+
+       delete reinterpret_cast<csret_wp_engine_s *>(engine);
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_wp_engine_get_vendor(csre_wp_engine_h engine, const char **vendor)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -435,14 +366,15 @@ int csre_wp_engine_get_vendor(csre_wp_engine_h engine, const char **vendor)
        if (vendor == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *vendor = eng->vendor_name;
+       *vendor = eng->vendorName.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_wp_engine_get_name(csre_wp_engine_h engine, const char **name)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -450,7 +382,8 @@ int csre_wp_engine_get_name(csre_wp_engine_h engine, const char **name)
        if (name == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *name = eng->engine_name;
+       *name = eng->engineName.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
@@ -458,7 +391,7 @@ API
 int csre_wp_engine_get_vendor_logo(csre_wp_engine_h engine, unsigned char **vendor_logo_image,
                                                                   unsigned int *image_size)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -466,15 +399,16 @@ int csre_wp_engine_get_vendor_logo(csre_wp_engine_h engine, unsigned char **vend
        if (vendor_logo_image == nullptr || image_size == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *vendor_logo_image = eng->vendor_logo_image;
-       *image_size = eng->image_size;
+       *vendor_logo_image = eng->logoImage.data();
+       *image_size = eng->logoImage.size();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_wp_engine_get_version(csre_wp_engine_h engine, const char **version)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -482,14 +416,15 @@ int csre_wp_engine_get_version(csre_wp_engine_h engine, const char **version)
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = eng->engine_version;
+       *version = eng->engineVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
 API
 int csre_wp_engine_get_data_version(csre_wp_engine_h engine, const char **version)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -497,7 +432,8 @@ int csre_wp_engine_get_data_version(csre_wp_engine_h engine, const char **versio
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = eng->data_version;
+       *version = eng->dataVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
@@ -505,7 +441,7 @@ int csre_wp_engine_get_data_version(csre_wp_engine_h engine, const char **versio
 API
 int csre_wp_engine_get_latest_update_time(csre_wp_engine_h engine, time_t *time)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -513,7 +449,7 @@ int csre_wp_engine_get_latest_update_time(csre_wp_engine_h engine, time_t *time)
        if (time == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *time = eng->latest_update;
+       *time = eng->latestUpdate;
        return CSRE_ERROR_NONE;
 }
 
@@ -521,7 +457,7 @@ int csre_wp_engine_get_latest_update_time(csre_wp_engine_h engine, time_t *time)
 API
 int csre_wp_engine_get_activated(csre_wp_engine_h engine, csre_wp_activated_e *pactivated)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -529,7 +465,7 @@ int csre_wp_engine_get_activated(csre_wp_engine_h engine, csre_wp_activated_e *p
        if (pactivated == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       if (risky_urls == nullptr)
+       if (g_risky_urls.empty())
                *pactivated = CSRE_WP_NOT_ACTIVATED;
        else
                *pactivated = CSRE_WP_ACTIVATED;
@@ -540,7 +476,7 @@ int csre_wp_engine_get_activated(csre_wp_engine_h engine, csre_wp_activated_e *p
 API
 int csre_wp_engine_get_api_version(csre_wp_engine_h engine, const char **version)
 {
-       csret_wp_engine_s *eng = (csret_wp_engine_s *) engine;
+       auto eng = reinterpret_cast<csret_wp_engine_s *>(engine);
 
        if (eng == nullptr)
                return CSRE_ERROR_INVALID_HANDLE;
@@ -548,7 +484,8 @@ int csre_wp_engine_get_api_version(csre_wp_engine_h engine, const char **version
        if (version == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       *version = CSRE_WP_API_VERSION;
+       *version = eng->apiVersion.c_str();
+
        return CSRE_ERROR_NONE;
 }
 
@@ -566,6 +503,9 @@ int csre_wp_get_error_string(int error_code, const char **string)
                *string = "CSRET_WP_ERROR_NO_RISKY_URL_FILE";
                break;
 
+       case CSRET_WP_ERROR_SIGNATURE_FILE_FORMAT:
+               *string = "CSRET_WP_ERROR_SIGNATURE_FILE_FORMAT";
+
        case CSRET_WP_ERROR_FILE_IO:
                *string = "CSRET_WP_ERROR_FILE_IO";
                break;
index 41865a2..a6e2a3c 100644 (file)
@@ -19,15 +19,16 @@ Requires:      lib%{name}-common = %{version}-%{release}
 General purpose content screening and reputation solution. Can scan
 file contents and checking url to prevent malicious items.
 
-%global service_name              csr
-%global bin_dir                   %{_bindir}
-%global sbin_dir                  /sbin
-%global ro_data_dir               %{_datadir}
-%global ro_db_dir                 %{_datadir}/%{service_name}/dbspace
-%global rw_db_dir                 /opt/share//%{service_name}/dbspace
-%global sample_engine_working_dir /opt/share/%{service_name}/engine
-%global sample_engine_dir         %{_libdir}
-%global test_dir                  /opt/share/%{service_name}-test
+%global service_name                 csr
+%global bin_dir                      %{_bindir}
+%global sbin_dir                     /sbin
+%global ro_data_dir                  %{_datadir}
+%global ro_db_dir                    %{_datadir}/%{service_name}/dbspace
+%global rw_db_dir                    /opt/share/%{service_name}/dbspace
+%global sample_engine_ro_res_dir     %{ro_data_dir}/%{service_name}/engine
+%global sample_engine_rw_working_dir /opt/share/%{service_name}/engine
+%global sample_engine_dir            %{_libdir}
+%global test_dir                     /opt/share/%{service_name}-test
 
 %package -n lib%{name}-common
 Summary: Common library package for %{name}
@@ -87,7 +88,8 @@ test program of csr-framework
     -DSYSTEMD_UNIT_USER_DIR=%{_unitdir_user} \
     -DRO_DBSPACE:PATH=%{ro_db_dir} \
     -DRW_DBSPACE:PATH=%{rw_db_dir} \
-    -DSAMPLE_ENGINE_WORKING_DIR:PATH=%{sample_engine_working_dir} \
+    -DSAMPLE_ENGINE_RO_RES_DIR:PATH=%{sample_engine_ro_res_dir} \
+    -DSAMPLE_ENGINE_RW_WORKING_DIR:PATH=%{sample_engine_rw_working_dir} \
     -DSAMPLE_ENGINE_DIR:PATH=%{sample_engine_dir} \
     -DTEST_DIR:PATH=%{test_dir}
 
@@ -115,6 +117,7 @@ cp LICENSE.BSL-1.0 %{buildroot}%{ro_data_dir}/license/%{name}-test.BSL-1.0
 
 mkdir -p %{buildroot}%{ro_db_dir}
 mkdir -p %{buildroot}%{rw_db_dir}
+mkdir -p %{buildroot}%{sample_engine_ro_res_dir}
 cp data/scripts/*.sql %{buildroot}%{ro_db_dir}
 
 %post
@@ -169,7 +172,8 @@ fi
 # sample engine related files
 %{sample_engine_dir}/lib%{service_name}-cs-engine.so
 %{sample_engine_dir}/lib%{service_name}-wp-engine.so
-%{sample_engine_working_dir}
+%dir %{sample_engine_ro_res_dir}
+%attr(775, system, system) %{sample_engine_rw_working_dir}
 
 %files -n lib%{name}-common
 %defattr(-,root,root,-)
index e6394c5..6c4dd75 100644 (file)
@@ -58,6 +58,22 @@ typedef enum {
 int csre_cs_engine_get_info(csre_cs_engine_h *engine);
 
 /**
+ * @brief Releases all system resources associated with a engine information handle.
+ *
+ * @param[in]  engine      The engine information handle.
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE                  Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE        Invalid handle
+ * @retval #CSRE_ERROR_SOCKET                Socket error between client and server
+ * @retval #CSRE_ERROR_SERVER                Server has been failed for some reason
+ * @retval #CSRE_ERROR_ENGINE_INTERNAL       Engine Internal error
+ * @retval #CSRE_ERROR_UNKNOWN               Error with unknown reason
+ */
+int csre_cs_engine_destroy(csre_cs_engine_h engine);
+
+/**
  * @brief returns the engine API version.
  *
  * @param[in]  engine   The engine information handle.
index 57fcff3..20f9ea1 100644 (file)
@@ -36,44 +36,78 @@ extern "C" {
 // Main function related
 //==============================================================================
 /**
- * @brief Initializes and returns a Tizen Malware Screening engine API handle.
+ * @brief Initializes content screening engine. This will be called only once after
+ *        load engine library.
  *
- * @details A Malware Screening engine interface handle (or CSR CS engine handle) is
- *          obtained using the csre_cs_context_create() function. The engine handle is
- *          required for subsequent CSR CS engine API calls. The csre_cs_context_destroy()
- *          function releases/closes the engine handle. Multiple handles can be obtained
- *          using csre_cs_context_create().
+ * @remarks Directory paths in parameters are absolute path so not ended with '/'.
+ *          Pre-provided resources by engine exist in @a ro_res_dir and those resources
+ *          is only readable in runtime. Engine can read/write/create resources in
+ *          @a rw_working_dir.
+ *
+ * @param[in] ro_res_dir      Read-only resources which are pre-provided by engine exists
+ *                            in here.
+ * @param[in] rw_working_dir  Engine can read/write/create resources in runtime in here.
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE               Successful
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY      Not enough memory
+ * @retval #CSRE_ERROR_UNKNOWN            Error with unknown reason
+ * @retval -0x0100~-0xFF00                Engine defined error
+ */
+int csre_cs_global_initialize(const char *ro_res_dir, const char *rw_working_dir);
+
+/**
+ * @brief Deinitializes content screening engine. This will be called only once before
+ *        unload engine library.
+ *
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE               Successful
+ * @retval #CSRE_ERROR_UNKNOWN            Error with unknown reason
+ * @retval -0x0100~-0xFF00                Engine defined error
+ */
+int csre_cs_global_deinitialize();
+
+/**
+ * @brief Creates context handle. The handle contains related resources to do operations.
+ *
+ * @details The handle is required for subsequent CSR CS engine API calls.
+ *          csre_cs_context_destroy() function releases/closes the engine handle. Multiple
+ *          handles can be obtained and multiple requests can be dispatched concurrently.
+ *          Returned resources after operations with the handle will be contained by the
+ *          handle and have same life cycle with it. Context handle may have some options
+ *          to handle request. Options are context-handle-scoped.
  *
- * @param[in]  engine_root_dir  A root directory where an engine exists. It is a absolute
- *                              path and it doesn't end with '/'.
  * @param[out] phandle          A pointer of CSR CS Engine context handle.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE               Successful
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER  phandle is invalid
- * @retval #CSRE_CS_ERROR_OUT_OF_MEMORY      Not enough memory
- * @retval #CSRE_CS_ERROR_UNKNOWN            Error with unknown reason
- * @retval -0x0100~-0xFF00                   Engine defined error
+ * @retval #CSRE_ERROR_NONE               Successful
+ * @retval #CSRE_ERROR_INVALID_PARAMETER  phandle is invalid
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY      Not enough memory
+ * @retval #CSRE_ERROR_UNKNOWN            Error with unknown reason
+ * @retval -0x0100~-0xFF00                Engine defined error
  *
  * @see csre_cs_context_destroy()
  */
-int csre_cs_context_create(const char *engine_root_dir, csre_cs_context_h* phandle);
+int csre_cs_context_create(csre_cs_context_h* phandle);
 
 /**
- * @brief Releases all system resources associated with a Tizen Malware Screening engine
- *        API handle.
+ * @brief Destroys context handle.
  *
- * @details The handle is one returned by the csre_cs_context_create() function.
+ * @details The handle is one returned by the csre_cs_context_create(). Destroy all
+ *          resources associated to the handle.
  *
  * @param[in] handle CSR CS Engine context handle returned by csre_cs_context_create().
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE             Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE   Invalid handle
- * @retval #CSRE_CS_ERROR_UNKNOWN          Error with unknown reason
- * @retval -0x0100~-0xFF00                 Engine defined error
+ * @retval #CSRE_ERROR_NONE             Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE   Invalid handle
+ * @retval #CSRE_ERROR_UNKNOWN          Error with unknown reason
+ * @retval -0x0100~-0xFF00              Engine defined error
  *
  * @see csre_cs_context_create()
  */
@@ -89,15 +123,15 @@ int csre_cs_context_destroy(csre_cs_context_h handle);
  * @param[out] pdetected  A pointer of the detected malware handle. It can be null when
  *                        no malware detected.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid handle
- * @retval #CSRE_CS_ERROR_OUT_OF_MEMORY        Not enough memory
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    data or presult is invalid
- * @retval #CSRE_CS_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid handle
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY        Not enough memory
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    data or presult is invalid
+ * @retval #CSRE_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  *
  * @see csre_cs_context_create()
  * @see csre_cs_scan_file()
@@ -116,17 +150,17 @@ int csre_cs_scan_data(csre_cs_context_h handle,
  * @param[out] pdetected  A pointer of the detected malware handle. It can be null when
  *                        no malware detected.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid handle
- * @retval #CSRE_CS_ERROR_OUT_OF_MEMORY        Not enough memory
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    file_path or presult is invalid
- * @retval #CSRE_CS_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
- * @retval #CSRE_CS_ERROR_PERMISSION_DENIED    Access denied
- * @retval #CSRE_CS_ERROR_FILE_NOT_FOUND       File not found
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid handle
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY        Not enough memory
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    file_path or presult is invalid
+ * @retval #CSRE_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
+ * @retval #CSRE_ERROR_PERMISSION_DENIED    Access denied
+ * @retval #CSRE_ERROR_FILE_NOT_FOUND       File not found
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  *
  * @see csre_cs_context_create()
  * @see csre_cs_scan_data()
@@ -146,17 +180,17 @@ int csre_cs_scan_file(csre_cs_context_h handle,
  * @param[out] pdetected    A pointer of the detected malware handle. It can be null when
  *                          no malware detected.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid handle
- * @retval #CSRE_CS_ERROR_OUT_OF_MEMORY        Not enough memory
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    app_root_dir or presult is invalid
- * @retval #CSRE_CS_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
- * @retval #CSRE_CS_ERROR_PERMISSION_DENIED    Access denied
- * @retval #CSRE_CS_ERROR_FILE_NOT_FOUND       File not found
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid handle
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY        Not enough memory
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    app_root_dir or presult is invalid
+ * @retval #CSRE_ERROR_ENGINE_NOT_ACTIVATED Engine is not activated
+ * @retval #CSRE_ERROR_PERMISSION_DENIED    Access denied
+ * @retval #CSRE_ERROR_FILE_NOT_FOUND       File not found
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  *
  * @see csre_cs_context_create()
  * @see csre_cs_scan_data()
@@ -174,13 +208,13 @@ int csre_cs_scan_app_on_cloud(csre_cs_context_h handle, const char* app_root_dir
  * @param[in]  detected    A detected malware handle.
  * @param[out] pseverity  A pointer of the severity of a detected malware.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid detected malware handle
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    pseverity is invalid.
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid detected malware handle
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    pseverity is invalid.
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  *
  * @see csre_cs_result_get_detected_by_idx()
  * @see csre_cs_result_get_detected_most_severe()
@@ -194,12 +228,12 @@ int csre_cs_detected_get_severity(csre_cs_detected_h detected, csre_cs_severity_
  * @param[in]  detected      A detected malware handle.
  * @param[out] pthreat_type  A pointer of the threat type of a detected malware.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid detected malware handle
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    pharmful_type is invalid.
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid detected malware handle
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    pharmful_type is invalid.
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
  */
 int csre_cs_detected_get_threat_type(csre_cs_detected_h detected, csre_cs_threat_type_e* pthreat_type);
 
@@ -210,13 +244,13 @@ int csre_cs_detected_get_threat_type(csre_cs_detected_h detected, csre_cs_threat
  * @param[out] name      A pointer of the name of a detected malware. A caller should not
  *                       free it.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid detected malware handle
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    malware_name is invalid.
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid detected malware handle
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    malware_name is invalid.
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  *
  * @see csre_cs_result_get_detected_by_idx()
  * @see csre_cs_result_get_detected_most_severe()
@@ -231,13 +265,13 @@ int csre_cs_detected_get_malware_name(csre_cs_detected_h detected, const char**
  * @param[out] detailed_url  A pointer of an url that contains detailed information on
  *                           vendor's web site. A caller should not free this string.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid detected malware handle
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    malware_name is invalid.
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid detected malware handle
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    malware_name is invalid.
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  */
 int csre_cs_detected_get_detailed_url(csre_cs_detected_h detected, const char** detailed_url);
 
@@ -249,13 +283,13 @@ int csre_cs_detected_get_detailed_url(csre_cs_detected_h detected, const char**
  * @param[out] timestamp  A pointer of the time stamp in millisecond when a malware is
  *                        detected. A caller should not free it.
  *
- * @return #CSRE_CS_ERROR_NONE on success, otherwise a negative error value
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
  *
- * @retval #CSRE_CS_ERROR_NONE                 Successful
- * @retval #CSRE_CS_ERROR_INVALID_HANDLE       Invalid detected malware handle
- * @retval #CSRE_CS_ERROR_INVALID_PARAMETER    timestamp is invalid
- * @retval #CSRE_CS_ERROR_UNKNOWN              Error with unknown reason
- * @retval -0x0100~-0xFF00                     Engine defined error
+ * @retval #CSRE_ERROR_NONE                 Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE       Invalid detected malware handle
+ * @retval #CSRE_ERROR_INVALID_PARAMETER    timestamp is invalid
+ * @retval #CSRE_ERROR_UNKNOWN              Error with unknown reason
+ * @retval -0x0100~-0xFF00                  Engine defined error
  */
 int csre_cs_detected_get_timestamp(csre_cs_detected_h detected, time_t* timestamp);
 
index 2343462..a26b3b7 100644 (file)
@@ -58,6 +58,22 @@ typedef enum {
 int csre_wp_engine_get_info(csre_wp_engine_h *engine);
 
 /**
+ * @brief Releases all system resources associated with a engine information handle.
+ *
+ * @param[in]  engine      The engine information handle.
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE                  Successful
+ * @retval #CSRE_ERROR_INVALID_HANDLE        Invalid handle
+ * @retval #CSRE_ERROR_SOCKET                Socket error between client and server
+ * @retval #CSRE_ERROR_SERVER                Server has been failed for some reason
+ * @retval #CSRE_ERROR_ENGINE_INTERNAL       Engine Internal error
+ * @retval #CSRE_ERROR_UNKNOWN               Error with unknown reason
+ */
+int csre_wp_engine_destroy(csre_wp_engine_h engine);
+
+/**
  * @brief returns the engine API version.
  *
  * @param[in]  engine   The engine information handle.
index 9db4c08..30cd3e0 100644 (file)
@@ -33,16 +33,50 @@ extern "C" {
 // Main function related
 //==============================================================================
 /**
- * @brief Initializes and returns a Tizen Web Screening engine API handle.
+ * @brief Initializes web protection engine. This will be called only once after
+ *        load engine library.
  *
- * @details A Web Screening engine interface handle (or CSR WP engine handle) is obtained
- *          using the csre_wp_context_create() function. The engine handle is required
- *          for subsequent CSR WP engine API calls. The csre_wp_context_destroy() function
- *          releases/closes the engine handle. Multiple handles can be obtained using
- *          csre_wp_context_create().
+ * @remarks Directory paths in parameters are absolute path so not ended with '/'.
+ *          Pre-provided resources by engine exist in @a ro_res_dir and those resources
+ *          is only readable in runtime. Engine can read/write/create resources in
+ *          @a rw_working_dir.
+ *
+ * @param[in] ro_res_dir      Read-only resources which are pre-provided by engine exists
+ *                            in here.
+ * @param[in] rw_working_dir  Engine can read/write/create resources in runtime in here.
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE               Successful
+ * @retval #CSRE_ERROR_OUT_OF_MEMORY      Not enough memory
+ * @retval #CSRE_ERROR_UNKNOWN            Error with unknown reason
+ * @retval -0x0100~-0xFF00                Engine defined error
+ */
+int csre_wp_global_initialize(const char *ro_res_dir, const char *rw_working_dir);
+
+/**
+ * @brief Deinitializes web protection engine. This will be called only once before
+ *        unload engine library.
+ *
+ *
+ * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSRE_ERROR_NONE               Successful
+ * @retval #CSRE_ERROR_UNKNOWN            Error with unknown reason
+ * @retval -0x0100~-0xFF00                Engine defined error
+ */
+int csre_wp_global_deinitialize();
+
+/**
+ * @brief Creates context handle. The handle contains related resources to do operations.
+ *
+ * @details The handle is required for subsequent CSR WP engine API calls.
+ *          csre_wp_context_destroy() function releases/closes the engine handle. Multiple
+ *          handles can be obtained and multiple requests can be dispatched concurrently.
+ *          Returned resources after operations with the handle will be contained by the
+ *          handle and have same life cycle with it. Context handle may have some options
+ *          to handle request. Options are context-handle-scoped.
  *
- * @param[in]  engine_root_dir  A root directory where an engine exists. It is a absolute
- *                              path and it doesn't end with '/'.
  * @param[out] phandle          A pointer of CSR WP Engine context handle.
  *
  * @return #CSRE_ERROR_NONE on success, otherwise a negative error value
@@ -55,7 +89,7 @@ extern "C" {
  *
  * @see csre_wp_context_destroy()
  */
-int csre_wp_context_create(const char *engine_root_dir, csre_wp_context_h* phandle);
+int csre_wp_context_create(csre_wp_context_h* phandle);
 
 /**
  * @brief Releases all system resources associated with a CSR WP engine API handle.
index 68e2d0f..c0044bc 100644 (file)
@@ -161,27 +161,25 @@ inline csre_cs_engine_h getEngineHandle(void)
        int ret = CSRE_ERROR_UNKNOWN;
 
        BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_info(&engine));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       BOOST_REQUIRE_MESSAGE(ret == CSRE_ERROR_NONE,
+               "Failed to create engine handle. ret: " << ret);
        CHECK_IS_NOT_NULL(engine);
 
        return engine;
 }
 
-inline ScopedContext getContextHandleWithDir(const char *dir)
+inline ScopedContext getContextHandle(void)
 {
        csre_cs_context_h context;
        int ret = CSRE_ERROR_UNKNOWN;
 
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_context_create(dir, &context));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       BOOST_REQUIRE_NO_THROW(ret = csre_cs_context_create(&context));
+       BOOST_REQUIRE_MESSAGE(ret == CSRE_ERROR_NONE,
+               "Failed to create context handle. ret: " << ret);
        CHECK_IS_NOT_NULL(context);
 
        return makeScopedContext(context);
-}
 
-inline ScopedContext getContextHandle(void)
-{
-       return getContextHandleWithDir(SAMPLE_ENGINE_WORKING_DIR);
 }
 
 }
@@ -420,7 +418,9 @@ BOOST_AUTO_TEST_CASE(get_latest_update_time)
        time_t time = 0;
        BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_latest_update_time(handle, &time));
        BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       BOOST_REQUIRE(time > 0);
+
+       struct tm t;
+       BOOST_MESSAGE(asctime(gmtime_r(&time, &t)));
 }
 
 BOOST_AUTO_TEST_CASE(get_engine_activated)
index 73d204a..e757cbc 100644 (file)
@@ -46,7 +46,7 @@ std::unordered_map<std::string, Result> ExpectedResult = {
        {"http://lowrisky.test.com",    Result(CSRE_WP_RISK_LOW, "")}
 };
 
-inline void checkResult(csre_wp_check_result_h &result, const Result &expected)
+inline void checkResult(const std::string &url, csre_wp_check_result_h &result, const Result &expected)
 {
        int ret = CSRE_ERROR_UNKNOWN;
        CHECK_IS_NOT_NULL(result);
@@ -56,7 +56,7 @@ inline void checkResult(csre_wp_check_result_h &result, const Result &expected)
 
        BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(risk_level == expected.risk_level,
-               "risk level isn't expected value. "
+               "url[" << url << "] risk level isn't expected value. "
                        "val: " << risk_level << " expected: " << expected.risk_level);
 
        const char *detailed_url = nullptr;
@@ -64,7 +64,7 @@ inline void checkResult(csre_wp_check_result_h &result, const Result &expected)
 
        BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(expected.detailed_url.compare(detailed_url) == 0,
-               "detailed url isn't expected value. "
+               "url[" << url << "] detailed url isn't expected value. "
                        "val: " << detailed_url <<" expected: " << expected.detailed_url);
 }
 
@@ -106,23 +106,18 @@ inline csre_wp_engine_h getEngineHandle(void)
        return engine;
 }
 
-inline ScopedContext getContextHandleWithDir(const char *dir)
+inline ScopedContext getContextHandle(void)
 {
        csre_wp_context_h context;
        int ret = CSRE_ERROR_UNKNOWN;
 
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_context_create(dir, &context));
+       BOOST_REQUIRE_NO_THROW(ret = csre_wp_context_create(&context));
        BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(context);
 
        return makeScopedContext(context);
 }
 
-inline ScopedContext getContextHandle(void)
-{
-       return getContextHandleWithDir(SAMPLE_ENGINE_WORKING_DIR);
-}
-
 }
 
 BOOST_AUTO_TEST_SUITE(API_ENGINE_WEB_PROTECTION)
@@ -144,7 +139,7 @@ BOOST_AUTO_TEST_CASE(check_url)
                BOOST_REQUIRE_NO_THROW(ret = csre_wp_check_url(context, pair.first.c_str(), &result));
 
                BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-               checkResult(result, pair.second);
+               checkResult(pair.first, result, pair.second);
        }
 }
 
@@ -227,7 +222,9 @@ BOOST_AUTO_TEST_CASE(get_latest_update_time)
        time_t time = 0;
        BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_latest_update_time(handle, &time));
        BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       BOOST_REQUIRE(time > 0);
+
+       struct tm t;
+       BOOST_MESSAGE(asctime(gmtime_r(&time, &t)));
 }
 
 BOOST_AUTO_TEST_CASE(get_engine_activated)
index 581e3ed..533f0e5 100644 (file)
@@ -24,6 +24,9 @@
 #include <boost/test/results_reporter.hpp>
 #include <colour_log_formatter.h>
 
+#include "csre/content-screening.h"
+#include "csre/web-protection.h"
+
 struct TestConfig {
        TestConfig()
        {
@@ -33,4 +36,45 @@ struct TestConfig {
        }
 };
 
+bool isEngineInitialized = false;
+struct Initializer {
+       Initializer()
+       {
+               if (!isEngineInitialized) {
+                       int ret = csre_cs_global_initialize(
+                               SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
+
+                       if (ret != CSRE_ERROR_NONE)
+                               throw std::logic_error("Failed to init content screening engine.");
+
+                       ret = csre_wp_global_initialize(
+                               SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
+
+                       if (ret != CSRE_ERROR_NONE)
+                               throw std::logic_error("Failed to init web protection engine.");
+
+                       isEngineInitialized = true;
+
+                       BOOST_MESSAGE("Initialize engines");
+               }
+       }
+
+       ~Initializer()
+       {
+               if (!isEngineInitialized)
+                       return;
+
+               int ret = csre_cs_global_deinitialize();
+               if (ret != CSRE_ERROR_NONE)
+                       throw std::logic_error("Failed to deinit content screening engine.");
+
+               ret = csre_wp_global_deinitialize();
+               if (ret != CSRE_ERROR_NONE)
+                       throw std::logic_error("Failed to deinit web protection engine.");
+
+               BOOST_MESSAGE("Deinitialize engines");
+       }
+};
+
 BOOST_GLOBAL_FIXTURE(TestConfig)
+BOOST_GLOBAL_FIXTURE(Initializer)