Declare key-value pair options to context / result 55/66355/1
authorKyungwook Tak <k.tak@samsung.com>
Tue, 12 Apr 2016 08:14:11 +0000 (17:14 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 18 Apr 2016 11:12:00 +0000 (20:12 +0900)
Change-Id: I01cb343025647bdcdb5210a07a352d723182f59a
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
29 files changed:
src/framework/CMakeLists.txt
src/framework/client/async-logic.cpp
src/framework/client/async-logic.h
src/framework/client/content-screening.cpp
src/framework/client/handle-ext.cpp
src/framework/client/handle-ext.h
src/framework/client/handle.cpp
src/framework/client/handle.h
src/framework/client/web-protection.cpp
src/framework/common/cs-context.cpp [new file with mode: 0644]
src/framework/common/cs-context.h [new file with mode: 0644]
src/framework/common/cs-detected.cpp [new file with mode: 0644]
src/framework/common/cs-detected.h [new file with mode: 0644]
src/framework/common/kvp-container.cpp [new file with mode: 0644]
src/framework/common/kvp-container.h [new file with mode: 0644]
src/framework/common/serialization.h
src/framework/common/types.h
src/framework/service/logic.cpp
src/framework/service/logic.h
src/include/csr/content-screening.h
src/include/csr/error.h
test/CMakeLists.txt
test/test-api-content-screening-async.cpp [new file with mode: 0644]
test/test-api-content-screening.cpp
test/test-api-engine-content-screening.cpp
test/test-api-engine-web-protection.cpp
test/test-api-web-protection.cpp
test/test-common.cpp [new file with mode: 0644]
test/test-common.h [new file with mode: 0644]

index 3c9662d..b5b904a 100644 (file)
@@ -28,6 +28,9 @@ SET(${TARGET_CSR_COMMON}_SRCS
        common/audit/logger.cpp
        common/binary-queue.cpp
        common/connection.cpp
+       common/cs-context.cpp
+       common/cs-detected.cpp
+       common/kvp-container.cpp
        common/types.cpp
        common/dispatcher.cpp
        common/mainloop.cpp
index 3002106..8ea4338 100644 (file)
 
 #include <utility>
 
+#include "common/cs-detected.h"
 #include "common/audit/logger.h"
 
 namespace Csr {
 namespace Client {
 
-AsyncLogic::AsyncLogic(Context &context, const Callback &cb, void *userdata,
-                                          const std::function<bool()> &isStopped) :
+AsyncLogic::AsyncLogic(std::shared_ptr<Context> &context, const Callback &cb,
+                                          void *userdata, const std::function<bool()> &isStopped) :
        m_origCtx(context),
-       m_ctx(context),
+       m_ctx(new CsContext),
        m_cb(cb),
        m_userdata(userdata),
        m_isStopped(isStopped),
        m_dispatcher(new Dispatcher("/tmp/." SERVICE_NAME ".socket"))
 {
+       copyKvp<int>(CsContext::Key::AskUser);
+       copyKvp<int>(CsContext::Key::CoreUsage);
+       copyKvp<std::string>(CsContext::Key::PopupMessage);
+       copyKvp<bool>(CsContext::Key::ScanOnCloud);
 }
 
 AsyncLogic::~AsyncLogic()
 {
        DEBUG("AsyncLogic dtor. Results num in "
-                 "mother ctx[" << m_origCtx.size() << "] "
-                 "and here[" << m_ctx.size() << "]");
+                 "mother ctx[" << m_origCtx->size() << "] "
+                 "and here[" << m_ctx->size() << "]");
 
-       for (auto &resultPtr : m_ctx.m_results)
-               m_origCtx.add(std::move(resultPtr));
+       for (auto &resultPtr : m_ctx->m_results)
+               m_origCtx->add(std::move(resultPtr));
 
-       DEBUG("Integrated mother ctx results num: " << m_origCtx.size());
+       DEBUG("Integrated mother ctx results num: " << m_origCtx->size());
 }
 
 std::pair<Callback::Id, Task> AsyncLogic::scanDirs(const std::shared_ptr<StrSet>
@@ -75,7 +80,7 @@ std::pair<Callback::Id, Task> AsyncLogic::scanDir(const std::string &dir)
 {
        // For in case of there's already detected malware for dir
        auto retResults =
-               m_dispatcher->methodCall<std::pair<int, std::vector<Result *>>>(
+               m_dispatcher->methodCall<std::pair<int, std::vector<CsDetected *>>>(
                        CommandId::DIR_GET_RESULTS, m_ctx, dir);
 
        if (retResults.first != CSR_ERROR_NONE) {
@@ -175,7 +180,7 @@ std::pair<Callback::Id, Task> AsyncLogic::scanFiles(const
 
 void AsyncLogic::add(Result *r)
 {
-       m_ctx.add(r);
+       m_ctx->add(r);
 }
 
 }
index 714c4f5..5aaae20 100644 (file)
@@ -25,6 +25,7 @@
 #include <atomic>
 
 #include "common/types.h"
+#include "common/cs-context.h"
 #include "common/dispatcher.h"
 #include "client/callback.h"
 
@@ -33,7 +34,7 @@ namespace Client {
 
 class AsyncLogic {
 public:
-       AsyncLogic(Context &context, const Callback &cb, void *userdata,
+       AsyncLogic(std::shared_ptr<Context> &context, const Callback &cb, void *userdata,
                           const std::function<bool()> &isStopped);
        virtual ~AsyncLogic();
 
@@ -44,10 +45,13 @@ public:
        void stop(void);
 
 private:
+       template<typename T>
+       void copyKvp(CsContext::Key);
+
        void add(Result *);
 
-       Context &m_origCtx; // for registering results for auto-release
-       Context m_ctx; // TODO: append it to handle context when destroyed
+       std::shared_ptr<Context> &m_origCtx; // for registering results for auto-release
+       std::unique_ptr<Context> m_ctx; // TODO: append it to handle context when destroyed
        Callback m_cb;
        void *m_userdata;
        std::function<bool()> m_isStopped;
@@ -56,5 +60,14 @@ private:
 
 };
 
+template<typename T>
+void AsyncLogic::copyKvp(CsContext::Key key)
+{
+       T value;
+
+       m_origCtx->get(static_cast<int>(key), value);
+       m_ctx->set(static_cast<int>(key), value);
+}
+
 }
 }
index 802c82b..64dd6c8 100644 (file)
 #include "client/utils.h"
 #include "client/handle-ext.h"
 #include "client/async-logic.h"
+#include "common/cs-context.h"
+#include "common/cs-detected.h"
 #include "common/command-id.h"
 #include "common/audit/logger.h"
 
 using namespace Csr;
 
+namespace {
+
+bool _isValid(const csr_cs_core_usage_e &value)
+{
+       switch (value) {
+       case CSR_CS_USE_CORE_DEFAULT:
+       case CSR_CS_USE_CORE_ALL:
+       case CSR_CS_USE_CORE_HALF:
+       case CSR_CS_USE_CORE_SINGLE:
+               return true;
+       default:
+               return false;
+       }
+}
+
+bool _isValid(const csr_cs_ask_user_e &value)
+{
+       switch (value) {
+       case CSR_CS_NOT_ASK_USER:
+       case CSR_CS_ASK_USER:
+               return true;
+       default:
+               return false;
+       }
+}
+
+}
+
 API
 int csr_cs_context_create(csr_cs_context_h* phandle)
 {
@@ -39,7 +69,8 @@ int csr_cs_context_create(csr_cs_context_h* phandle)
        if (phandle == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
-       *phandle = reinterpret_cast<csr_cs_context_h>(new Client::HandleExt());
+       *phandle = reinterpret_cast<csr_cs_context_h>(
+               new Client::HandleExt(std::shared_ptr<Context>(new CsContext())));
 
        return CSR_ERROR_NONE;
 
@@ -64,40 +95,66 @@ int csr_cs_context_destroy(csr_cs_context_h handle)
 API
 int csr_cs_set_ask_user(csr_cs_context_h handle, csr_cs_ask_user_e ask_user)
 {
-       (void) handle;
-       (void) ask_user;
+       EXCEPTION_SAFE_START
+
+       if (handle == nullptr || !_isValid(ask_user))
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Client::HandleExt *>(handle)->getContext()->set(
+               static_cast<int>(CsContext::Key::AskUser), static_cast<int>(ask_user));
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
 int csr_cs_set_popup_message(csr_cs_context_h handle, const char* message)
 {
-       (void) handle;
-       (void) message;
+       EXCEPTION_SAFE_START
+
+       if (handle == nullptr || message == nullptr || message[0] == '\0')
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Client::HandleExt *>(handle)->getContext()->set(
+               static_cast<int>(CsContext::Key::PopupMessage), message);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
 int csr_cs_set_core_usage(csr_cs_context_h handle, csr_cs_core_usage_e usage)
 {
-       (void) handle;
-       (void) usage;
+       EXCEPTION_SAFE_START
+
+       if (handle == nullptr || !_isValid(usage))
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Client::HandleExt *>(handle)->getContext()->set(
+               static_cast<int>(CsContext::Key::CoreUsage), static_cast<int>(usage));
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
+// TODO: API which unset this option isn't needed?
 API
 int csr_cs_set_scan_on_cloud(csr_cs_context_h handle)
 {
-       (void) handle;
+       EXCEPTION_SAFE_START
+
+       if (handle == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Client::HandleExt *>(handle)->getContext()->set(
+               static_cast<int>(CsContext::Key::ScanOnCloud), true);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
@@ -132,8 +189,15 @@ int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, csr_cs_dete
                return ret.first;
        }
 
-       hExt->add(ret.second);
-       *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
+       if (ret.second == nullptr)
+               return CSR_ERROR_UNKNOWN; // deserialization logic error
+
+       if (ret.second->hasValue()) {
+               hExt->add(ret.second);
+               *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
+       } else {
+               *pdetected = nullptr;
+       }
 
        return CSR_ERROR_NONE;
 
@@ -147,7 +211,7 @@ int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, csr_cs_on_file_
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
-       if (hExt == nullptr)
+       if (hExt == nullptr || callback == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
        hExt->m_cb.onScanned = callback;
@@ -164,7 +228,7 @@ int csr_cs_set_callback_on_detected(csr_cs_context_h handle, csr_cs_on_detected_
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
-       if (hExt == nullptr)
+       if (hExt == nullptr || callback == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
        hExt->m_cb.onDetected = callback;
@@ -181,7 +245,7 @@ int csr_cs_set_callback_on_completed(csr_cs_context_h handle, csr_cs_on_complete
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
-       if (hExt == nullptr)
+       if (hExt == nullptr || callback == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
        hExt->m_cb.onCompleted = callback;
@@ -198,7 +262,7 @@ int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle, csr_cs_on_cancelle
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
-       if (hExt == nullptr)
+       if (hExt == nullptr || callback == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
        hExt->m_cb.onCancelled = callback;
@@ -215,7 +279,7 @@ int csr_cs_set_callback_on_error(csr_cs_context_h handle, csr_cs_on_error_cb cal
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
-       if (hExt == nullptr)
+       if (hExt == nullptr || callback == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
        hExt->m_cb.onError = callback;
@@ -330,72 +394,119 @@ int csr_cs_scan_cancel(csr_cs_context_h handle)
 API
 int csr_cs_detected_get_severity(csr_cs_detected_h detected, csr_cs_severity_level_e* pseverity)
 {
-       (void) detected;
-       (void) pseverity;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || pseverity == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       int intSeverity;
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::Severity), intSeverity);
+       *pseverity = static_cast<csr_cs_severity_level_e>(intSeverity);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
 int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, csr_cs_threat_type_e* pthreat_type)
 {
-       (void) detected;
-       (void) pthreat_type;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || pthreat_type == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       int intThreat;
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::Threat), intThreat);
+       *pthreat_type = static_cast<csr_cs_threat_type_e>(intThreat);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
-int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, char** malware_name)
+int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, const char** pmalware_name)
 {
-       (void) detected;
-       (void) malware_name;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || pmalware_name == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::MalwareName), pmalware_name);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
-int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, char** detailed_url)
+int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, const char** pdetailed_url)
 {
-       (void) detected;
-       (void) detailed_url;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || pdetailed_url == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::MalwareName), pdetailed_url);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
-int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* timestamp)
+int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* ptimestamp)
 {
-       (void) detected;
-       (void) timestamp;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || ptimestamp == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::TimeStamp), *ptimestamp);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
-int csr_cs_detected_get_file_name(csr_cs_detected_h detected, char** file_name)
+int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile_name)
 {
-       (void) detected;
-       (void) file_name;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || pfile_name == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::TargetName), pfile_name);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
 int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_response_e* presponse)
 {
-       (void) detected;
-       (void) detected;
-       (void) presponse;
+       EXCEPTION_SAFE_START
+
+       if (detected == nullptr || presponse == nullptr)
+               return CSR_ERROR_INVALID_PARAMETER;
+
+       int intResponse;
+       reinterpret_cast<Result *>(detected)->get(
+               static_cast<int>(CsDetected::Key::UserResponse), intResponse);
+       *presponse = static_cast<csr_cs_user_response_e>(intResponse);
 
-       DEBUG("start!");
        return CSR_ERROR_NONE;
+
+       EXCEPTION_SAFE_END
 }
 
 API
index c32d173..83cc706 100644 (file)
@@ -30,7 +30,9 @@
 namespace Csr {
 namespace Client {
 
-HandleExt::HandleExt() : m_stop(false)
+HandleExt::HandleExt(std::shared_ptr<Context> &&context) :
+       Handle(std::move(context)),
+       m_stop(false)
 {
 }
 
index fc154c6..73b8b43 100644 (file)
@@ -40,7 +40,7 @@ namespace Client {
 
 class HandleExt : public Handle {
 public:
-       HandleExt();
+       explicit HandleExt(std::shared_ptr<Context> &&);
        virtual ~HandleExt();
 
        void dispatchAsync(const Task &task);
index a8d3c17..bfd800c 100644 (file)
 namespace Csr {
 namespace Client {
 
-Context &Handle::getContext() noexcept
+Handle::Handle(std::shared_ptr<Context> &&context) :
+       m_ctx(std::move(context))
+{
+       if (!m_ctx)
+               throw std::logic_error("context shouldn't be null");
+}
+
+Handle::~Handle()
+{
+}
+
+std::shared_ptr<Context> &Handle::getContext() noexcept
 {
        return m_ctx;
 }
@@ -36,7 +47,7 @@ void Handle::add(Result *result)
        if (result == nullptr)
                throw std::logic_error("result shouldn't be null");
 
-       m_ctx.add(result);
+       m_ctx->add(result);
 }
 
 } // namespace Client
index a0705ea..e3b8c70 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <utility>
+#include <memory>
 
 #include "common/types.h"
 #include "common/dispatcher.h"
@@ -31,16 +32,19 @@ namespace Client {
 
 class Handle {
 public:
+       explicit Handle(std::shared_ptr<Context> &&);
+       virtual ~Handle();
+
        template<typename Type, typename ...Args>
        Type dispatch(Args &&...);
 
        void add(Result *);
 
-       Context &getContext(void) noexcept;
+       std::shared_ptr<Context> &getContext(void) noexcept;
 
 private:
        std::unique_ptr<Dispatcher> m_dispatcher;
-       Context m_ctx;
+       std::shared_ptr<Context> m_ctx;
 };
 
 template<typename Type, typename ...Args>
index e533168..d441fcb 100644 (file)
@@ -39,7 +39,8 @@ int csr_wp_context_create(csr_wp_context_h* phandle)
        if (phandle == nullptr)
                return CSR_ERROR_INVALID_PARAMETER;
 
-       *phandle = reinterpret_cast<csr_wp_context_h>(new Client::Handle());
+       *phandle = reinterpret_cast<csr_wp_context_h>(
+               new Client::Handle(std::make_shared<Context>()));
 
        return CSR_ERROR_NONE;
 
diff --git a/src/framework/common/cs-context.cpp b/src/framework/common/cs-context.cpp
new file mode 100644 (file)
index 0000000..6da289f
--- /dev/null
@@ -0,0 +1,219 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        cs-context.cpp
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Content screening context with dependent options
+ */
+#include "common/cs-context.h"
+
+#include <stdexcept>
+
+#include "common/audit/logger.h"
+
+namespace Csr {
+
+CsContext::CsContext() :
+       m_popupMessage(),
+       m_askUser(CSR_CS_NOT_ASK_USER),
+       m_coreUsage(CSR_CS_USE_CORE_DEFAULT),
+       m_isScanOnCloud(false)
+{
+}
+
+CsContext::~CsContext()
+{
+}
+
+CsContext::CsContext(IStream &stream) : Context(stream)
+{
+       int intAskUser;
+       int intCoreUsage;
+       Deserializer<std::string, int, int, bool>::Deserialize(stream,
+               m_popupMessage, intAskUser, intCoreUsage, m_isScanOnCloud);
+
+       m_askUser = static_cast<csr_cs_ask_user_e>(intAskUser);
+       m_coreUsage = static_cast<csr_cs_core_usage_e>(intCoreUsage);
+}
+
+void CsContext::Serialize(IStream &stream) const
+{
+       Serializer<std::string, int, int, bool>::Serialize(stream,
+               m_popupMessage, static_cast<int>(m_askUser), static_cast<int>(m_coreUsage), m_isScanOnCloud);
+}
+
+CsContext::CsContext(CsContext &&other) :
+       Context(std::move(other)),
+       m_popupMessage(std::move(other.m_popupMessage)),
+       m_askUser(other.m_askUser),
+       m_coreUsage(other.m_coreUsage),
+       m_isScanOnCloud(other.m_isScanOnCloud)
+{
+}
+
+CsContext &CsContext::operator=(CsContext &&other)
+{
+       if (this == &other)
+               return *this;
+
+       Context::operator=(std::move(other));
+
+       m_popupMessage = std::move(other.m_popupMessage);
+       m_askUser = other.m_askUser;
+       m_coreUsage = other.m_coreUsage;
+       m_isScanOnCloud = other.m_isScanOnCloud;
+
+       return *this;
+}
+
+CsContext::CsContext(const CsContext &other) :
+       Context(other),
+       m_popupMessage(other.m_popupMessage),
+       m_askUser(other.m_askUser),
+       m_coreUsage(other.m_coreUsage),
+       m_isScanOnCloud(other.m_isScanOnCloud)
+{
+}
+
+CsContext &CsContext::operator=(const CsContext &other)
+{
+       Context::operator=(other);
+
+       m_popupMessage = other.m_popupMessage;
+       m_askUser = other.m_askUser;
+       m_coreUsage = other.m_coreUsage;
+       m_isScanOnCloud = other.m_isScanOnCloud;
+
+       return *this;
+}
+
+void CsContext::set(int key, int value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::AskUser:
+               m_askUser = static_cast<csr_cs_ask_user_e>(value);
+               break;
+
+       case Key::CoreUsage:
+               m_coreUsage = static_cast<csr_cs_core_usage_e>(value);
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as int."));
+       }
+}
+
+void CsContext::set(int key, const std::string &value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::PopupMessage:
+               m_popupMessage = value;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as string."));
+       }
+}
+
+void CsContext::set(int key, const char *value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::PopupMessage:
+               m_popupMessage = value;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as string."));
+       }
+}
+
+void CsContext::set(int key, bool value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::ScanOnCloud:
+               m_isScanOnCloud = value;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as bool."));
+       }
+}
+
+void CsContext::get(int key, int &value) const
+{
+       switch (static_cast<Key>(key)) {
+       case Key::AskUser:
+               value = static_cast<int>(m_askUser);
+               break;
+
+       case Key::CoreUsage:
+               value = static_cast<int>(m_coreUsage);
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as int."));
+       }
+}
+
+void CsContext::get(int key, std::string &value) const
+{
+       switch (static_cast<Key>(key)) {
+       case Key::PopupMessage:
+               value = m_popupMessage;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as string."));
+       }
+}
+
+void CsContext::get(int key, const char **value) const
+{
+       if (value == nullptr)
+               throw std::logic_error("invalud argument. output storage pointer is null.");
+
+       switch (static_cast<Key>(key)) {
+       case Key::PopupMessage:
+               *value = m_popupMessage.c_str();
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as string."));
+       }
+}
+
+void CsContext::get(int key, bool &value) const
+{
+       switch (static_cast<Key>(key)) {
+       case Key::ScanOnCloud:
+               value = m_isScanOnCloud;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as bool."));
+       }
+}
+
+}
diff --git a/src/framework/common/cs-context.h b/src/framework/common/cs-context.h
new file mode 100644 (file)
index 0000000..7927c13
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        cs-context.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Content screening context with dependent options
+ */
+#pragma once
+
+#include <string>
+
+#include "common/types.h"
+#include "csr/content-screening-types.h"
+
+namespace Csr {
+
+class CsContext : public Context {
+public:
+       enum class Key : int {
+               PopupMessage = 0x01, // string
+
+               AskUser      = 0x10, // int
+               CoreUsage    = 0x11, // int
+
+               ScanOnCloud  = 0x20, // bool
+       };
+
+       CsContext();
+       virtual ~CsContext();
+
+       CsContext(IStream &);
+       virtual void Serialize(IStream &) const;
+
+       CsContext(CsContext &&);
+       CsContext &operator=(CsContext &&);
+
+       CsContext(const CsContext &);
+       CsContext &operator=(const CsContext &);
+
+       virtual void set(int, int) override;
+       virtual void set(int, const std::string &) override;
+       virtual void set(int, const char *) override;
+       virtual void set(int, bool) override;
+
+       virtual void get(int, int &) const override;
+       virtual void get(int, std::string &) const override;
+       virtual void get(int, const char **) const override;
+       virtual void get(int, bool &) const override;
+
+private:
+       std::string m_popupMessage;
+       csr_cs_ask_user_e m_askUser;
+       csr_cs_core_usage_e m_coreUsage;
+       bool m_isScanOnCloud;
+};
+
+}
diff --git a/src/framework/common/cs-detected.cpp b/src/framework/common/cs-detected.cpp
new file mode 100644 (file)
index 0000000..b620507
--- /dev/null
@@ -0,0 +1,230 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        cs-detected.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Content screening detected result
+ */
+#include "common/cs-detected.h"
+
+#include <stdexcept>
+
+#include "common/audit/logger.h"
+
+namespace Csr {
+
+CsDetected::CsDetected() :
+       m_targetName(),
+       m_malwareName(),
+       m_detailedUrl(),
+       m_severity(CSR_CS_SEVERITY_LOW),
+       m_threat(CSR_CS_THREAT_GENERIC),
+       m_response(CSR_CS_NO_ASK_USER),
+       m_ts(0)
+{
+}
+
+CsDetected::~CsDetected()
+{
+}
+
+CsDetected::CsDetected(IStream &stream)
+{
+       int intSeverity;
+       int intThreat;
+       int intResponse;
+       Deserializer<std::string, std::string, std::string, int, int, int, time_t>
+               ::Deserialize(stream,
+                       m_targetName, m_malwareName, m_detailedUrl,
+                       intSeverity, intThreat, intResponse, m_ts);
+
+       m_severity = static_cast<csr_cs_severity_level_e>(intSeverity);
+       m_threat = static_cast<csr_cs_threat_type_e>(intThreat);
+       m_response = static_cast<csr_cs_user_response_e>(intResponse);
+}
+
+void CsDetected::Serialize(IStream &stream) const
+{
+       Serializer<std::string, std::string, std::string, int, int, int, time_t>
+               ::Serialize(stream,
+                       m_targetName, m_malwareName, m_detailedUrl,
+                       static_cast<int>(m_severity), static_cast<int>(m_threat),
+                       static_cast<int>(m_response), m_ts);
+}
+
+CsDetected::CsDetected(CsDetected &&other) :
+       Result(std::move(other)),
+       m_targetName(std::move(other.m_targetName)),
+       m_malwareName(std::move(other.m_malwareName)),
+       m_detailedUrl(std::move(other.m_detailedUrl)),
+       m_severity(other.m_severity),
+       m_threat(other.m_threat),
+       m_response(other.m_response),
+       m_ts(other.m_ts)
+{
+}
+
+CsDetected &CsDetected::operator=(CsDetected &&other)
+{
+       if (this == &other)
+               return *this;
+
+       Result::operator=(std::move(other));
+
+       m_targetName = std::move(other.m_targetName);
+       m_malwareName = std::move(other.m_malwareName);
+       m_detailedUrl = std::move(other.m_detailedUrl);
+       m_severity = other.m_severity;
+       m_threat = other.m_threat;
+       m_response = other.m_response;
+       m_ts = other.m_ts;
+
+       return *this;
+}
+
+void CsDetected::set(int key, int value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::Severity:
+               m_severity = static_cast<csr_cs_severity_level_e>(value);
+               break;
+
+       case Key::Threat:
+               m_threat = static_cast<csr_cs_threat_type_e>(value);
+               break;
+
+       case Key::UserResponse:
+               m_response = static_cast<csr_cs_user_response_e>(value);
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as int."));
+       }
+}
+
+void CsDetected::set(int key, const std::string &value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::TargetName:
+               m_targetName = value;
+               break;
+
+       case Key::MalwareName:
+               m_malwareName = value;
+               break;
+
+       case Key::DetailedUrl:
+               m_detailedUrl = value;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as string."));
+       }
+}
+
+void CsDetected::set(int key, const char *value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::TargetName:
+               m_targetName = value;
+               break;
+
+       case Key::MalwareName:
+               m_malwareName = value;
+               break;
+
+       case Key::DetailedUrl:
+               m_detailedUrl = value;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as string."));
+       }
+}
+
+void CsDetected::set(int key, time_t value)
+{
+       switch (static_cast<Key>(key)) {
+       case Key::TimeStamp:
+               m_ts = value;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to set as time_t."));
+       }
+}
+
+void CsDetected::get(int key, int &value) const
+{
+       switch (static_cast<Key>(key)) {
+       case Key::Severity:
+               value = static_cast<int>(m_severity);
+               break;
+
+       case Key::Threat:
+               value = static_cast<int>(m_threat);
+               break;
+
+       case Key::UserResponse:
+               value = static_cast<int>(m_response);
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as int."));
+       }
+}
+
+void CsDetected::get(int key, const char **value) const
+{
+       if (value == nullptr)
+               throw std::logic_error("invalud argument. output storage pointer is null.");
+
+       switch (static_cast<Key>(key)) {
+       case Key::TargetName:
+               *value = m_targetName.c_str();
+               break;
+
+       case Key::MalwareName:
+               *value = m_malwareName.c_str();
+               break;
+
+       case Key::DetailedUrl:
+               *value = m_detailedUrl.c_str();
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as string."));
+       }
+}
+
+void CsDetected::get(int key, time_t &value) const
+{
+       switch (static_cast<Key>(key)) {
+       case Key::TimeStamp:
+               value = m_ts;
+               break;
+
+       default:
+               throw std::logic_error(FORMAT("Invalid key[" << key
+                       << "] comes in to get as time_t."));
+       }
+}
+
+}
diff --git a/src/framework/common/cs-detected.h b/src/framework/common/cs-detected.h
new file mode 100644 (file)
index 0000000..322e452
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        cs-detected.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Content screening detected result
+ */
+#pragma once
+
+#include <string>
+
+#include "common/types.h"
+#include "csr/content-screening-types.h"
+
+namespace Csr {
+
+class CsDetected : public Result {
+public:
+       // key for set/get
+       enum class Key : int {
+               TargetName   = 0x01, // string
+               MalwareName  = 0x02, // string
+               DetailedUrl  = 0x03, // string
+
+               Severity     = 0x10, // int
+               Threat       = 0x11, // int
+               UserResponse = 0x12, // int
+
+               TimeStamp    = 0x21  // time_t
+       };
+
+       CsDetected();
+       virtual ~CsDetected();
+
+       CsDetected(IStream &);
+       virtual void Serialize(IStream &) const override;
+
+       CsDetected(CsDetected &&);
+       CsDetected &operator=(CsDetected &&);
+
+       virtual void set(int, int) override;
+       virtual void set(int, const std::string &) override;
+       virtual void set(int, const char *) override;
+       virtual void set(int, time_t) override;
+
+       virtual void get(int, int &) const override;
+       virtual void get(int, const char **) const override;
+       virtual void get(int, time_t &) const override;
+
+private:
+       std::string m_targetName; // file path or app id which contains malware
+
+       std::string m_malwareName;
+       std::string m_detailedUrl;
+       csr_cs_severity_level_e m_severity;
+       csr_cs_threat_type_e m_threat;
+       csr_cs_user_response_e m_response;
+       time_t m_ts;
+};
+
+}
diff --git a/src/framework/common/kvp-container.cpp b/src/framework/common/kvp-container.cpp
new file mode 100644 (file)
index 0000000..f93a3cf
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        kvp-container.cpp
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Key-value pair container with set/get interface
+ */
+#include "common/kvp-container.h"
+
+#include <stdexcept>
+
+namespace Csr {
+
+KvpContainer::~KvpContainer()
+{
+}
+
+void KvpContainer::set(int, int)
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::set(int, bool)
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::set(int, const std::string &)
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::set(int, const char *)
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::set(int, time_t)
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::get(int, int &) const
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::get(int, bool &) const
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::get(int, std::string &) const
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::get(int, const char **) const
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+void KvpContainer::get(int, time_t &) const
+{
+       throw std::logic_error("Not implemented key-value pair interface used!");
+}
+
+
+}
diff --git a/src/framework/common/kvp-container.h b/src/framework/common/kvp-container.h
new file mode 100644 (file)
index 0000000..fa85dca
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        kvp-container.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Key-value pair container with set/get interface
+ */
+#pragma once
+
+#include <string>
+#include <ctime>
+
+namespace Csr {
+
+// set/get key-value pairs. Key should be defined in each derived classes
+class KvpContainer {
+public:
+       virtual ~KvpContainer();
+
+       virtual void set(int, int);
+       virtual void set(int, bool);
+       virtual void set(int, const std::string &);
+       virtual void set(int, const char *);
+       virtual void set(int, time_t);
+
+       virtual void get(int, int &) const;
+       virtual void get(int, bool &) const;
+       virtual void get(int, std::string &) const;
+       virtual void get(int, const char **) const; // For not copy to string lifecycle within itself.
+       virtual void get(int, time_t &) const;
+};
+
+}
index 22c66ae..22b3cce 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <stdint.h>
+#include <time.h>
 
 #include <string>
 #include <vector>
@@ -128,6 +129,15 @@ struct Serialization {
                stream.write(sizeof(*value), value);
        }
 
+       static void Serialize(IStream& stream, const time_t value)
+       {
+               stream.write(sizeof(value), &value);
+       }
+       static void Serialize(IStream& stream, const time_t* const value)
+       {
+               stream.write(sizeof(*value), value);
+       }
+
        // bool
        static void Serialize(IStream& stream, const bool value)
        {
@@ -279,6 +289,14 @@ struct Serialization {
        {
                Serialize(stream, *p);
        }
+
+       // std::shared_ptr
+       template <typename T>
+       static void Serialize(IStream& stream, const std::shared_ptr<T>& p)
+       {
+               Serialize(stream, *p);
+       }
+
 }; // struct Serialization
 
 struct Deserialization {
@@ -364,6 +382,16 @@ struct Deserialization {
                stream.read(sizeof(*value), value);
        }
 
+       static void Deserialize(IStream& stream, time_t& value)
+       {
+               stream.read(sizeof(value), &value);
+       }
+       static void Deserialize(IStream& stream, time_t*& value)
+       {
+               value = new time_t;
+               stream.read(sizeof(*value), value);
+       }
+
        // bool
        static void Deserialize(IStream& stream, bool& value)
        {
index d6a48b1..c6f8c65 100644 (file)
 
 #include "common/dispatcher.h"
 #include "common/serialization.h"
+#include "common/kvp-container.h"
 
 namespace Csr {
 
 using Task = std::function<void()>;
 using StrSet = std::set<std::string>;
 
-class Result : public ISerializable {
+class Result : public ISerializable, public KvpContainer {
 public:
        Result();
        virtual ~Result();
@@ -49,7 +50,7 @@ private:
        bool m_hasVal;
 };
 
-class Context : public ISerializable {
+class Context : public ISerializable, public KvpContainer {
 public:
        Context();
        virtual ~Context();
index 6073f58..fbd0dff 100644 (file)
 #include <stdexcept>
 
 #include "common/audit/logger.h"
+#include "common/cs-detected.h"
 #include "csr/error.h"
 
 namespace Csr {
 
+namespace {
+
+// temporal function for debugging until modules integrated to logic.
+void printCsContext(const CsContext &context)
+{
+       std::string popupMessage;
+       int askUser;
+       int coreUsage;
+       bool scanOnCloud;
+
+       context.get(static_cast<int>(CsContext::Key::PopupMessage), popupMessage);
+       context.get(static_cast<int>(CsContext::Key::AskUser), askUser);
+       context.get(static_cast<int>(CsContext::Key::CoreUsage), coreUsage);
+       context.get(static_cast<int>(CsContext::Key::ScanOnCloud), scanOnCloud);
+
+       INFO("Context info:"
+               " PopupMessage: " << popupMessage <<
+               " AskUser: " << askUser <<
+               " CoreUsage: " << coreUsage <<
+               " ScanOnCloud: " << scanOnCloud);
+}
+
+}
+
 Logic::Logic()
 {
 }
@@ -46,7 +71,7 @@ RawBuffer Logic::dispatch(const RawBuffer &in)
 
        switch (info.first) {
        case CommandId::SCAN_FILE: {
-               Context context;
+               CsContext context;
                std::string filepath;
                info.second.Deserialize(context, filepath);
                return scanFile(context, filepath);
@@ -61,14 +86,14 @@ RawBuffer Logic::dispatch(const RawBuffer &in)
        }
 
        case CommandId::DIR_GET_RESULTS: {
-               Context context;
+               CsContext context;
                std::string dir;
                info.second.Deserialize(context, dir);
                return dirGetResults(context, dir);
        }
 
        case CommandId::DIR_GET_FILES: {
-               Context context;
+               CsContext context;
                std::string dir;
                info.second.Deserialize(context, dir);
                return dirGetFiles(context, dir);
@@ -91,36 +116,39 @@ std::pair<CommandId, BinaryQueue> Logic::getRequestInfo(const RawBuffer &data)
        return std::make_pair(id, std::move(q));
 }
 
-RawBuffer Logic::scanFile(const Context &context, const std::string &filepath)
+RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath)
 {
        INFO("Scan file[" << filepath << "] by engine");
-       (void) context;
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, Result()).pop();
+       printCsContext(context);
+
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, CsDetected()).pop();
 }
 
-RawBuffer Logic::checkUrl(const Context &context, const std::string &url)
+RawBuffer Logic::dirGetResults(const CsContext &context, const std::string &dir)
 {
-       INFO("Check url[" << url << "] by engine");
-       (void) context;
+       INFO("Dir[" << dir << "] get results");
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, Result()).pop();
+       printCsContext(context);
+
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, std::vector<CsDetected>()).pop();
 }
 
-RawBuffer Logic::dirGetResults(const Context &context, const std::string &dir)
+RawBuffer Logic::dirGetFiles(const CsContext &context, const std::string &dir)
 {
-       INFO("Dir[" << dir << "] get results");
-       (void) context;
+       INFO("Dir[" << dir << "] get files");
+
+       printCsContext(context);
 
        return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop();
 }
 
-RawBuffer Logic::dirGetFiles(const Context &context, const std::string &dir)
+RawBuffer Logic::checkUrl(const Context &context, const std::string &url)
 {
-       INFO("Dir[" << dir << "] get files");
+       INFO("Check url[" << url << "] by engine");
        (void) context;
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, std::vector<Result>()).pop();
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, Result()).pop();
 }
 
 }
index 3575b9d..1c84e3c 100644 (file)
@@ -25,6 +25,7 @@
 #include <utility>
 
 #include "common/types.h"
+#include "common/cs-context.h"
 #include "common/command-id.h"
 #include "common/raw-buffer.h"
 #include "common/binary-queue.h"
@@ -41,10 +42,11 @@ public:
 private:
        std::pair<CommandId, BinaryQueue> getRequestInfo(const RawBuffer &);
 
-       RawBuffer scanFile(const Context &context, const std::string &filepath);
+       RawBuffer scanFile(const CsContext &context, const std::string &filepath);
+       RawBuffer dirGetResults(const CsContext &context, const std::string &dir);
+       RawBuffer dirGetFiles(const CsContext &context, const std::string &dir);
+
        RawBuffer checkUrl(const Context &context, const std::string &url);
-       RawBuffer dirGetResults(const Context &context, const std::string &dir);
-       RawBuffer dirGetFiles(const Context &context, const std::string &dir);
 };
 
 }
index 619fe1a..417570d 100644 (file)
@@ -462,7 +462,7 @@ int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, csr_cs_threat_ty
  * @retval #CSR_ERROR_INVALID_PARAMETER    malware_name is invalid
  * @retval #CSR_ERROR_UNKNOWN              Error with unknown reason
  */
-int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, char** malware_name);
+int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, const char** pmalware_name);
 
 /**
  * @brief extracts an url that contains detailed information on vendor's web site from the detected malware handle.
@@ -479,7 +479,7 @@ int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, char** malware_
  * @retval #CSR_ERROR_INVALID_PARAMETER    malware_name is invalid.
  * @retval #CSR_ERROR_UNKNOWN              Error with unknown reason
  */
-int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, char** detailed_url);
+int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, const char** pdetailed_url);
 
 /**
  * @brief extracts the time stamp when a malware is detected from the detected malware handle.
@@ -494,7 +494,7 @@ int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, char** detailed
  * @retval #CSR_ERROR_INVALID_PARAMETER    timestamp is invalid
  * @retval #CSR_ERROR_UNKNOWN              Error with unknown reason
  */
-int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* timestamp);
+int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* ptimestamp);
 
 /**
  * @brief extracts the file name where a malware is detected from the detected malware handle.
@@ -509,7 +509,7 @@ int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* timestamp)
  * @retval #CSR_ERROR_INVALID_PARAMETER    file_name is invalid
  * @retval #CSR_ERROR_UNKNOWN              Error with unknown reason
  */
-int csr_cs_detected_get_file_name(csr_cs_detected_h detected, char** file_name);
+int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile_name);
 
 /**
  * @brief extracts a user reponse of a popup from the detected malware handle.
index ccad0e1..0009a60 100644 (file)
@@ -30,7 +30,7 @@ extern "C" {
 
 /* define it temporary until this code goes into capi-base-common package */
 #ifndef TIZEN_ERROR_CSR
-#define TIZEN_ERROR_CSR 0x60 // 0110 0000
+#define TIZEN_ERROR_CSR 0x6000 // 0110 0000 0000 0000
 #endif
 
 
index 8540443..31be3a9 100644 (file)
@@ -35,6 +35,7 @@ SET(${TARGET_CSR_TEST}_SRCS
        ${CSR_FW_SRC_PATH}/service/core-usage.cpp
        colour_log_formatter.cpp
        test-api-content-screening.cpp
+       test-api-content-screening-async.cpp
        test-api-engine-manager.cpp
        test-api-web-protection.cpp
        test-api-engine-content-screening.cpp
@@ -42,6 +43,7 @@ SET(${TARGET_CSR_TEST}_SRCS
        test-internal-database.cpp
        test-internal-cpucore.cpp
        test-main.cpp
+       test-common.cpp
 )
 
 INCLUDE_DIRECTORIES(
diff --git a/test/test-api-content-screening-async.cpp b/test/test-api-content-screening-async.cpp
new file mode 100644 (file)
index 0000000..5623d11
--- /dev/null
@@ -0,0 +1,243 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        test-api-content-screening.cpp
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       CSR Content screening API test
+ */
+#include <csr/content-screening.h>
+
+#include <condition_variable>
+#include <thread>
+#include <mutex>
+#include <boost/test/unit_test.hpp>
+
+#include "test-common.h"
+
+namespace {
+
+struct AsyncTestContext {
+       std::mutex m;
+       std::condition_variable cv;
+       int scannedCnt;
+       int detectedCnt;
+       int completedCnt;
+       int cancelledCnt;
+       int errorCnt;
+
+       AsyncTestContext() :
+               scannedCnt(0),
+               detectedCnt(0),
+               completedCnt(0),
+               cancelledCnt(0),
+               errorCnt(0) {}
+};
+
+void on_scanned(void *userdata, const char *file)
+{
+       BOOST_MESSAGE("on_scanned. file[" << file << "] scanned!");
+       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
+       ctx->scannedCnt++;
+}
+
+void on_error(void *userdata, int ec)
+{
+       BOOST_MESSAGE("on_error. async request done with error code[" << ec << "]");
+       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
+       ctx->errorCnt++;
+}
+
+void on_detected(void *userdata, csr_cs_detected_h detected)
+{
+       (void) detected;
+       BOOST_MESSAGE("on_detected.");
+       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
+       ctx->detectedCnt++;
+}
+
+void on_completed(void *userdata)
+{
+       BOOST_MESSAGE("on_completed. async request completed succesfully.");
+       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
+       ctx->completedCnt++;
+       ctx->cv.notify_one();
+}
+
+void on_cancelled(void *userdata)
+{
+       BOOST_MESSAGE("on_cancelled. async request canceled!");
+       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
+       ctx->cancelledCnt++;
+}
+
+}
+
+BOOST_AUTO_TEST_SUITE(API_CONTENT_SCREENING_ASYNC)
+
+BOOST_AUTO_TEST_CASE(set_callbacks_positive)
+{
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
+                         CSR_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(set_callbacks_negative)
+{
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+       ASSERT_IF(csr_cs_set_callback_on_error(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(scan_files_async_positive)
+{
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
+                         CSR_ERROR_NONE);
+
+       const char *files[3] = {
+               TEST_DIR "/test_malware_file",
+               TEST_DIR "/test_normal_file",
+               TEST_DIR "/test_risky_file"
+       };
+
+       AsyncTestContext testCtx;
+
+       ASSERT_IF(
+               csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *),
+                                                               &testCtx),
+               CSR_ERROR_NONE);
+
+       std::unique_lock<std::mutex> l(testCtx.m);
+       testCtx.cv.wait(l);
+       l.unlock();
+       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 3 &&
+                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
+                                                 "Async request result isn't expected.");
+
+       EXCEPTION_GUARD_END
+}
+
+// TODO: directory scanning integarted testing will be meaningful
+//       after delta management by logic(+ db) is integrated.
+BOOST_AUTO_TEST_CASE(scan_dir_positive)
+{
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
+                         CSR_ERROR_NONE);
+
+       AsyncTestContext testCtx;
+
+       ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR, &testCtx), CSR_ERROR_NONE);
+
+       std::unique_lock<std::mutex> l(testCtx.m);
+       testCtx.cv.wait(l);
+       l.unlock();
+       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 0 &&
+                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
+                                                 "Async request result isn't expected.");
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(scan_dirs_positive)
+{
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
+                         CSR_ERROR_NONE);
+
+       AsyncTestContext testCtx;
+
+       const char *dirs[1] = {
+               TEST_DIR
+       };
+
+       ASSERT_IF(
+               csr_cs_scan_dirs_async(context, dirs, sizeof(dirs) / sizeof(const char *),
+                                                          &testCtx),
+               CSR_ERROR_NONE);
+
+       std::unique_lock<std::mutex> l(testCtx.m);
+       testCtx.cv.wait(l);
+       l.unlock();
+       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 0 &&
+                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
+                                                 "Async request result isn't expected.");
+
+       EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_SUITE_END()
index 5c89b09..41e7703 100644 (file)
 #include <memory>
 #include <new>
 #include <iostream>
-#include <condition_variable>
-#include <thread>
-#include <mutex>
 #include <boost/test/unit_test.hpp>
 
-namespace {
+#include "test-common.h"
 
-class ContextPtr {
-public:
-       ContextPtr() : m_context(nullptr) {}
-       ContextPtr(csr_cs_context_h context) : m_context(context) {}
-       virtual ~ContextPtr()
-       {
-               BOOST_REQUIRE(csr_cs_context_destroy(m_context) == CSR_ERROR_NONE);
-       }
-
-       inline csr_cs_context_h get(void)
-       {
-               return m_context;
-       }
+BOOST_AUTO_TEST_SUITE(API_CONTENT_SCREENING)
 
-private:
-       csr_cs_context_h m_context;
-};
+BOOST_AUTO_TEST_CASE(context_create_destroy)
+{
+       EXCEPTION_GUARD_START
 
-using ScopedContext = std::unique_ptr<ContextPtr>;
+       auto c = Test::Context<csr_cs_context_h>();
+       (void) c;
 
-inline ScopedContext makeScopedContext(csr_cs_context_h context)
-{
-       return ScopedContext(new ContextPtr(context));
+       EXCEPTION_GUARD_END
 }
 
-inline ScopedContext getContextHandle(void)
+BOOST_AUTO_TEST_CASE(set_values_to_context_positive)
 {
-       csr_cs_context_h context;
-       int ret = CSR_ERROR_UNKNOWN;
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_context_create(&context));
-       BOOST_REQUIRE_MESSAGE(ret == CSR_ERROR_NONE,
-                                                 "Failed to create context handle. ret: " << ret);
-       BOOST_REQUIRE(context != nullptr);
-       return makeScopedContext(context);
-}
+       EXCEPTION_GUARD_START
 
-}
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
 
-BOOST_AUTO_TEST_SUITE(API_CONTENT_SCREENING)
+       ASSERT_IF(csr_cs_set_ask_user(context, CSR_CS_NOT_ASK_USER), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_ask_user(context, CSR_CS_ASK_USER), CSR_ERROR_NONE);
 
-BOOST_AUTO_TEST_CASE(context_create_destroy)
-{
-       auto contextPtr = getContextHandle();
-       (void) contextPtr;
-}
+       ASSERT_IF(csr_cs_set_popup_message(context, "Test popup message"),
+                         CSR_ERROR_NONE);
 
-BOOST_AUTO_TEST_CASE(scan_file)
-{
-       int ret = CSR_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
-       csr_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_scan_file(context, "dummy_file_path",
-                                                                &detected));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-}
+       ASSERT_IF(csr_cs_set_core_usage(context, CSR_CS_USE_CORE_DEFAULT),
+                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_core_usage(context, CSR_CS_USE_CORE_ALL), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_core_usage(context, CSR_CS_USE_CORE_HALF), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_core_usage(context, CSR_CS_USE_CORE_SINGLE),
+                         CSR_ERROR_NONE);
 
-struct AsyncTestContext {
-       std::mutex m;
-       std::condition_variable cv;
-       int scannedCnt;
-       int detectedCnt;
-       int completedCnt;
-       int cancelledCnt;
-       int errorCnt;
-
-       AsyncTestContext() :
-               scannedCnt(0),
-               detectedCnt(0),
-               completedCnt(0),
-               cancelledCnt(0),
-               errorCnt(0) {}
-};
-
-void on_scanned(void *userdata, const char *file)
-{
-       BOOST_MESSAGE("on_scanned called. file[" << file << "] scanned!");
-       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
-       ctx->scannedCnt++;
-}
+       ASSERT_IF(csr_cs_set_scan_on_cloud(context), CSR_ERROR_NONE);
 
-void on_error(void *userdata, int ec)
-{
-       BOOST_MESSAGE("on_error called. async request done with error code[" << ec <<
-                                 "]");
-       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
-       ctx->errorCnt++;
+       EXCEPTION_GUARD_END
 }
 
-void on_detected(void *userdata, csr_cs_detected_h detected)
+BOOST_AUTO_TEST_CASE(set_values_to_context_negative)
 {
-       (void) detected;
-       BOOST_MESSAGE("on_detected called.");
-       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
-       ctx->detectedCnt++;
-}
+       EXCEPTION_GUARD_START
 
-void on_completed(void *userdata)
-{
-       BOOST_MESSAGE("on_completed called. async request completed succesfully.");
-       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
-       ctx->completedCnt++;
-       ctx->cv.notify_one();
-}
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
 
-void on_cancelled(void *userdata)
-{
-       BOOST_MESSAGE("on_cancelled called. async request canceled!");
-       auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
-       ctx->cancelledCnt++;
+       ASSERT_IF(csr_cs_set_ask_user(context, static_cast<csr_cs_ask_user_e>(0x926ce)),
+                         CSR_ERROR_INVALID_PARAMETER);
+
+       ASSERT_IF(csr_cs_set_popup_message(context, nullptr),
+                         CSR_ERROR_INVALID_PARAMETER);
+       ASSERT_IF(csr_cs_set_popup_message(context, ""), CSR_ERROR_INVALID_PARAMETER);
+
+       ASSERT_IF(csr_cs_set_core_usage(context,
+                                                                       static_cast<csr_cs_core_usage_e>(0x882a2)),
+                         CSR_ERROR_INVALID_PARAMETER);
+
+       EXCEPTION_GUARD_END
 }
 
-BOOST_AUTO_TEST_CASE(scan_files_async)
+BOOST_AUTO_TEST_CASE(scan_file)
 {
-       int ret = CSR_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_set_callback_on_completed(context,
-                                                                on_completed));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_set_callback_on_error(context, on_error));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_set_callback_on_cancelled(context,
-                                                                on_cancelled));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_set_callback_on_detected(context,
-                                                                on_detected));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       BOOST_REQUIRE_NO_THROW(ret = csr_cs_set_callback_on_file_scanned(context,
-                                                                on_scanned));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       const char *files[3] = {
-               TEST_DIR "/test_malware_file",
-               TEST_DIR "/test_normal_file",
-               TEST_DIR "/test_risky_file"
-       };
-       AsyncTestContext testCtx;
-       BOOST_REQUIRE_NO_THROW(ret =
-                                                          csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *),
-                                                                          &testCtx));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
-       std::unique_lock<std::mutex> l(testCtx.m);
-       testCtx.cv.wait(l);
-       l.unlock();
-       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 3 &&
-                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
-                                                 "Async request result isn't expected.");
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csr_cs_context_h>();
+       auto context = c.get();
+       csr_cs_detected_h detected;
+
+       ASSERT_IF(csr_cs_scan_file(context, "dummy_file_path", &detected),
+                         CSR_ERROR_NONE);
+
+       // no malware detected
+       CHECK_IS_NULL(detected);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
index c0044bc..f96f4f8 100644 (file)
@@ -31,8 +31,7 @@
 
 #include <boost/test/unit_test.hpp>
 
-#define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
-#define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
+#include "test-common.h"
 
 #define TEST_FILE_NORMAL   TEST_DIR "/test_normal_file"
 #define TEST_FILE_MALWARE  TEST_DIR "/test_malware_file"
@@ -48,28 +47,24 @@ inline void checkDetected(csre_cs_detected_h detected,
                const char *expected_detailed_url,
                long expected_timestamp)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
+       EXCEPTION_GUARD_START
+
        CHECK_IS_NOT_NULL(detected);
 
        csre_cs_severity_level_e severity;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_severity(detected, &severity));
-
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_detected_get_severity(detected, &severity), CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(severity == expected_severity,
                "severity isn't expected value. "
                "val: " << severity << " expected: " << expected_severity);
 
        csre_cs_threat_type_e threat_type;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_threat_type(detected, &threat_type));
-
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_detected_get_threat_type(detected, &threat_type), CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(threat_type == expected_threat_type,
                "threat type isn't expected value. "
                "val: " << threat_type << " expected: " << expected_threat_type);
 
        const char *malware_name = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_malware_name(detected, &malware_name));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_detected_get_malware_name(detected, &malware_name), CSRE_ERROR_NONE);
 
        if (expected_malware_name != nullptr) {
                CHECK_IS_NOT_NULL(malware_name);
@@ -81,8 +76,7 @@ inline void checkDetected(csre_cs_detected_h detected,
        }
 
        const char *detailed_url = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_detailed_url(detected, &detailed_url));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_detected_get_detailed_url(detected, &detailed_url), CSRE_ERROR_NONE);
 
        if (expected_detailed_url != nullptr) {
                CHECK_IS_NOT_NULL(detailed_url);
@@ -95,33 +89,15 @@ inline void checkDetected(csre_cs_detected_h detected,
        }
 
        long timestamp;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_timestamp(detected, &timestamp));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_detected_get_timestamp(detected, &timestamp), CSRE_ERROR_NONE);
 
        if (expected_timestamp != 0)
                BOOST_REQUIRE_MESSAGE(timestamp == expected_timestamp,
                        "timestamp isn't expected value. "
                                "val: " << timestamp << " expected: " << expected_timestamp);
-}
-
-class ContextPtr {
-public:
-       ContextPtr() : m_context(nullptr) {}
-       ContextPtr(csre_cs_context_h context) : m_context(context) {}
-       virtual ~ContextPtr()
-       {
-               int ret = csre_cs_context_destroy(m_context);
-               BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       }
 
-       csre_cs_context_h get(void)
-       {
-               return m_context;
-       }
-
-private:
-       csre_cs_context_h m_context;
-};
+       EXCEPTION_GUARD_END
+}
 
 class ScopedFile {
 public:
@@ -148,87 +124,59 @@ private:
        int m_fd;
 };
 
-using ScopedContext = std::unique_ptr<ContextPtr>;
-
-inline ScopedContext makeScopedContext(csre_cs_context_h context)
-{
-       return ScopedContext(new ContextPtr(context));
-}
-
-inline csre_cs_engine_h getEngineHandle(void)
-{
-       csre_cs_engine_h engine;
-       int ret = CSRE_ERROR_UNKNOWN;
-
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_info(&engine));
-       BOOST_REQUIRE_MESSAGE(ret == CSRE_ERROR_NONE,
-               "Failed to create engine handle. ret: " << ret);
-       CHECK_IS_NOT_NULL(engine);
-
-       return engine;
-}
-
-inline ScopedContext getContextHandle(void)
-{
-       csre_cs_context_h context;
-       int ret = CSRE_ERROR_UNKNOWN;
-
-       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);
-
-}
-
 }
 
 BOOST_AUTO_TEST_SUITE(API_ENGINE_CONTENT_SCREENING)
 
 BOOST_AUTO_TEST_CASE(context_create_destroy)
 {
-       auto context = getContextHandle();
-       (void)context;
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       (void) c;
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_data_clear)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        const char *data = "abcd1234dfdfdf334dfdi8ffndsfdfdsfdasfagdfvdfdfafadfasdfsdfe";
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_data(
+       ASSERT_IF(csre_cs_scan_data(
                context,
                reinterpret_cast<const unsigned char *>(data),
                strlen(data),
-               &detected));
+               &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NULL(detected);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_data_high)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        const char *data =
                "aabbccX5O!P%@AP[4\\PZX54(P^)7CC)7}$"
                "EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*112233";
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_data(
+       ASSERT_IF(csre_cs_scan_data(
                context,
                reinterpret_cast<const unsigned char *>(data),
                strlen(data),
-               &detected));
+               &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        checkDetected(detected,
@@ -237,24 +185,26 @@ BOOST_AUTO_TEST_CASE(scan_data_high)
                "test_malware",
                "http://high.malware.com",
                0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_data_medium)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        const char *data = "aabbccRISKY_MALWARE112233";
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_data(
+       ASSERT_IF(csre_cs_scan_data(
                context,
                reinterpret_cast<const unsigned char *>(data),
                strlen(data),
-               &detected));
+               &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        checkDetected(detected,
@@ -263,31 +213,35 @@ BOOST_AUTO_TEST_CASE(scan_data_medium)
                "test_risk",
                nullptr,
                0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_file_normal)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_file(context, TEST_FILE_NORMAL, &detected));
+       ASSERT_IF(csre_cs_scan_file(context, TEST_FILE_NORMAL, &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NULL(detected);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_file_malware)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_file(context, TEST_FILE_MALWARE, &detected));
+       ASSERT_IF(csre_cs_scan_file(context, TEST_FILE_MALWARE, &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        checkDetected(detected,
@@ -296,18 +250,20 @@ BOOST_AUTO_TEST_CASE(scan_file_malware)
                "test_malware",
                "http://high.malware.com",
                0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_file_risky)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_file(context, TEST_FILE_RISKY, &detected));
+       ASSERT_IF(csre_cs_scan_file(context, TEST_FILE_RISKY, &detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        checkDetected(detected,
@@ -316,18 +272,20 @@ BOOST_AUTO_TEST_CASE(scan_file_risky)
                "test_risk",
                "http://medium.malware.com",
                0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(scan_app_on_cloud)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_context_h>();
+       auto context = c.get();
 
        csre_cs_detected_h detected;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_scan_app_on_cloud(context, TEST_APP_ROOT,&detected));
+       ASSERT_IF(csre_cs_scan_app_on_cloud(context, TEST_APP_ROOT,&detected), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        checkDetected(detected,
@@ -336,8 +294,9 @@ BOOST_AUTO_TEST_CASE(scan_app_on_cloud)
                "test_malware",
                "http://high.malware.com",
                0);
-}
 
+       EXCEPTION_GUARD_END
+}
 
 BOOST_AUTO_TEST_SUITE_END()
 
@@ -346,14 +305,15 @@ BOOST_AUTO_TEST_SUITE(API_ENGINE_CONTENT_SCREENING_ERR_STRING)
 
 BOOST_AUTO_TEST_CASE(positive)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
+       EXCEPTION_GUARD_START
 
        const char *string = nullptr;
 
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_get_error_string(ret, &string));
+       ASSERT_IF(csre_cs_get_error_string(CSRE_ERROR_UNKNOWN, &string), CSRE_ERROR_NONE);
 
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(string);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
@@ -363,85 +323,110 @@ BOOST_AUTO_TEST_SUITE(API_ENGINE_CONTENT_SCREENING_ENGINE_INFO)
 
 BOOST_AUTO_TEST_CASE(get_engine_info)
 {
-       auto handle = getEngineHandle();
-       (void)handle;
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       (void) c;
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_vendor)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        const char *vendor = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_vendor(handle, &vendor));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_vendor(handle, &vendor), CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_vendor_logo)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        unsigned char *vendor_logo_image = nullptr;
        unsigned int size = 0;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_vendor_logo(handle, &vendor_logo_image, &size));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_vendor_logo(handle, &vendor_logo_image, &size), CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_version(handle, &version), CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(version);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_data_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_data_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_data_version(handle, &version), CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(version);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_latest_update_time)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        time_t time = 0;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_latest_update_time(handle, &time));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_latest_update_time(handle, &time), CSRE_ERROR_NONE);
 
        struct tm t;
        BOOST_MESSAGE(asctime(gmtime_r(&time, &t)));
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_engine_activated)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        csre_cs_activated_e activated;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_activated(handle, &activated));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_cs_engine_get_activated(handle, &activated), CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_api_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_cs_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_cs_engine_get_api_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       BOOST_REQUIRE(memcmp(version, CSRE_CS_API_VERSION, strlen(version)) == 0);
+       ASSERT_IF(csre_cs_engine_get_api_version(handle, &version), CSRE_ERROR_NONE);
+       ASSERT_IF(memcmp(version, CSRE_CS_API_VERSION, strlen(version)), 0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
index e757cbc..76a16e5 100644 (file)
@@ -27,8 +27,7 @@
 
 #include <boost/test/unit_test.hpp>
 
-#define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
-#define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
+#include "test-common.h"
 
 namespace {
 
@@ -48,74 +47,23 @@ std::unordered_map<std::string, Result> ExpectedResult = {
 
 inline void checkResult(const std::string &url, csre_wp_check_result_h &result, const Result &expected)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
+       EXCEPTION_GUARD_START
+
        CHECK_IS_NOT_NULL(result);
 
        csre_wp_risk_level_e risk_level;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_result_get_risk_level(result, &risk_level));
-
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_result_get_risk_level(result, &risk_level), CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(risk_level == expected.risk_level,
                "url[" << url << "] risk level isn't expected value. "
                        "val: " << risk_level << " expected: " << expected.risk_level);
 
        const char *detailed_url = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_result_get_detailed_url(result, &detailed_url));
-
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_result_get_detailed_url(result, &detailed_url), CSRE_ERROR_NONE);
        BOOST_REQUIRE_MESSAGE(expected.detailed_url.compare(detailed_url) == 0,
                "url[" << url << "] detailed url isn't expected value. "
                        "val: " << detailed_url <<" expected: " << expected.detailed_url);
-}
-
-class ContextPtr {
-public:
-       ContextPtr() : m_context(nullptr) {}
-       ContextPtr(csre_wp_context_h context) : m_context(context) {}
-       virtual ~ContextPtr()
-       {
-               int ret = csre_wp_context_destroy(m_context);
-               BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       }
 
-       csre_wp_context_h get(void)
-       {
-               return m_context;
-       }
-
-private:
-       csre_wp_context_h m_context;
-};
-
-using ScopedContext = std::unique_ptr<ContextPtr>;
-
-inline ScopedContext makeScopedContext(csre_wp_context_h context)
-{
-       return ScopedContext(new ContextPtr(context));
-}
-
-inline csre_wp_engine_h getEngineHandle(void)
-{
-       csre_wp_engine_h engine;
-       int ret = CSRE_ERROR_UNKNOWN;
-
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_info(&engine));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       CHECK_IS_NOT_NULL(engine);
-
-       return engine;
-}
-
-inline ScopedContext getContextHandle(void)
-{
-       csre_wp_context_h context;
-       int ret = CSRE_ERROR_UNKNOWN;
-
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_context_create(&context));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       CHECK_IS_NOT_NULL(context);
-
-       return makeScopedContext(context);
+       EXCEPTION_GUARD_END
 }
 
 }
@@ -124,23 +72,28 @@ BOOST_AUTO_TEST_SUITE(API_ENGINE_WEB_PROTECTION)
 
 BOOST_AUTO_TEST_CASE(context_create_destroy)
 {
-       auto handle = getContextHandle();
-       (void)handle;
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_context_h>();
+       (void) c;
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(check_url)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto contextPtr = getContextHandle();
-       auto context = contextPtr->get();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_context_h>();
+       auto context = c.get();
 
        for (const auto &pair : ExpectedResult) {
                csre_wp_check_result_h result;
-               BOOST_REQUIRE_NO_THROW(ret = csre_wp_check_url(context, pair.first.c_str(), &result));
-
-               BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+               ASSERT_IF(csre_wp_check_url(context, pair.first.c_str(), &result), CSRE_ERROR_NONE);
                checkResult(pair.first, result, pair.second);
        }
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
@@ -150,14 +103,16 @@ BOOST_AUTO_TEST_SUITE(API_ENGINE_WEB_PROTECTION_ERR_STRING)
 
 BOOST_AUTO_TEST_CASE(positive)
 {
+       EXCEPTION_GUARD_START
+
        int ret = CSRE_ERROR_UNKNOWN;
 
        const char *string = nullptr;
 
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_get_error_string(ret, &string));
-
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_get_error_string(ret, &string), CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(string);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
@@ -167,85 +122,111 @@ BOOST_AUTO_TEST_SUITE(API_ENGINE_WEB_PROTECTION_ENGINE_INFO)
 
 BOOST_AUTO_TEST_CASE(get_engine_info)
 {
-       auto handle = getEngineHandle();
-       (void)handle;
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       (void) c;
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_vendor)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        const char *vendor = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_vendor(handle, &vendor));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_vendor(handle, &vendor), CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_vendor_logo)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        unsigned char *vendor_logo_image = nullptr;
        unsigned int size = 0;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_vendor_logo(handle, &vendor_logo_image, &size));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_vendor_logo(handle, &vendor_logo_image, &size),
+                       CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_version(handle, &version), CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(version);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_data_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_data_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_data_version(handle, &version), CSRE_ERROR_NONE);
        CHECK_IS_NOT_NULL(version);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_latest_update_time)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        time_t time = 0;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_latest_update_time(handle, &time));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_latest_update_time(handle, &time), CSRE_ERROR_NONE);
 
        struct tm t;
        BOOST_MESSAGE(asctime(gmtime_r(&time, &t)));
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_engine_activated)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        csre_wp_activated_e activated;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_activated(handle, &activated));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+       ASSERT_IF(csre_wp_engine_get_activated(handle, &activated), CSRE_ERROR_NONE);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(get_api_version)
 {
-       int ret = CSRE_ERROR_UNKNOWN;
-       auto handle = getEngineHandle();
+       EXCEPTION_GUARD_START
+
+       auto c = Test::Context<csre_wp_engine_h>();
+       auto handle = c.get();
 
        const char *version = nullptr;
-       BOOST_REQUIRE_NO_THROW(ret = csre_wp_engine_get_api_version(handle, &version));
-       BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
-       BOOST_REQUIRE(memcmp(version, CSRE_WP_API_VERSION, strlen(version)) == 0);
+       ASSERT_IF(csre_wp_engine_get_api_version(handle, &version), CSRE_ERROR_NONE);
+       ASSERT_IF(memcmp(version, CSRE_WP_API_VERSION, strlen(version)), 0);
+
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
index 073aa71..1a6aef9 100644 (file)
 #include <iostream>
 #include <boost/test/unit_test.hpp>
 
+#include "test-common.h"
+
 BOOST_AUTO_TEST_SUITE(API_WEB_PROTECTION)
 
 BOOST_AUTO_TEST_CASE(context_create_destroy)
 {
-       csr_wp_context_h handle;
-       int ret = CSR_ERROR_UNKNOWN;
+       EXCEPTION_GUARD_START
 
-       BOOST_REQUIRE_NO_THROW(ret = csr_wp_context_create(&handle));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
+       auto c = Test::Context<csr_wp_context_h>();
+       (void) c;
 
-       BOOST_REQUIRE_NO_THROW(ret = csr_wp_context_destroy(handle));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_CASE(check_url)
 {
-       csr_wp_context_h handle;
-       int ret = CSR_ERROR_UNKNOWN;
+       EXCEPTION_GUARD_START
 
-       BOOST_REQUIRE_NO_THROW(ret = csr_wp_context_create(&handle));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
+       auto c = Test::Context<csr_wp_context_h>();
+       auto context = c.get();
 
        csr_wp_check_result_h result;
-       BOOST_REQUIRE_NO_THROW(ret = csr_wp_check_url(handle, "dummy/url/test", &result));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
+       ASSERT_IF(csr_wp_check_url(context, "dummy/url/test", &result), CSR_ERROR_NONE);
 
-       BOOST_REQUIRE_NO_THROW(ret = csr_wp_context_destroy(handle));
-       BOOST_REQUIRE(ret == CSR_ERROR_NONE);
+       EXCEPTION_GUARD_END
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/test-common.cpp b/test/test-common.cpp
new file mode 100644 (file)
index 0000000..08d4c6d
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        test-common.cpp
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Common utilities for test
+ */
+#include "test-common.h"
+
+namespace Test {
+
+void exceptionGuard(const std::function<void()> &f)
+{
+       try {
+               f();
+       } catch (const std::exception &e) {
+               BOOST_REQUIRE_MESSAGE(0, "std::exception catched: " << e.what());
+       } catch (...) {
+               BOOST_REQUIRE_MESSAGE(0, "Unknown exception catched");
+       }
+}
+
+} // namespace Test
diff --git a/test/test-common.h b/test/test-common.h
new file mode 100644 (file)
index 0000000..896e75f
--- /dev/null
@@ -0,0 +1,234 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        test-common.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Common utilities for test
+ */
+#pragma once
+
+#include <functional>
+#include <typeinfo>
+#include <string>
+#include <cstring>
+
+#include <boost/test/unit_test.hpp>
+
+#include <csr/content-screening.h>
+#include <csr/web-protection.h>
+
+#include <csre/content-screening.h>
+#include <csre/web-protection.h>
+
+#include <csre/content-screening-engine-info.h>
+#include <csre/web-protection-engine-info.h>
+
+#ifndef __FILENAME__
+#define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
+#endif
+
+#define ASSERT_IF_(value, expected, file, f, l) \
+       Test::_assert(value, expected, file, f, l)
+
+#define ASSERT_IF(value, expected) \
+       ASSERT_IF_(value, expected, __FILENAME__, __func__, __LINE__)
+
+#define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
+#define EXCEPTION_GUARD_END   });
+
+#define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
+#define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
+
+namespace Test {
+
+template <typename T, typename U>
+void _assert(T value, U expected, const std::string &filename, const std::string &funcname, unsigned int line)
+{
+       BOOST_REQUIRE_MESSAGE(value == expected,
+               "[" << filename << " > " << funcname << " : " << line << "]"
+                       << " returned code: " << value
+                       << " expected: " << expected);
+}
+
+void exceptionGuard(const std::function<void()> &);
+
+template <typename T>
+class Context {
+public:
+       Context() : m_context(nullptr)
+       {
+               BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
+                               << "] isn't specialized for context template");
+       }
+
+       virtual ~Context()
+       {
+               BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
+                               << "] isn't specialized for context template");
+       }
+
+       T get(void) const
+       {
+               BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
+                               << "] isn't specialized for context template");
+
+               return nullptr;
+       }
+
+private:
+       T m_context;
+};
+
+template <>
+class Context<csr_cs_context_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
+       }
+
+       csr_cs_context_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csr_cs_context_h m_context;
+};
+
+template <>
+class Context<csr_wp_context_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
+       }
+
+       csr_wp_context_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csr_wp_context_h m_context;
+};
+
+template <>
+class Context<csre_cs_context_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
+       }
+
+       csre_cs_context_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csre_cs_context_h m_context;
+};
+
+template <>
+class Context<csre_wp_context_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
+       }
+
+       csre_wp_context_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csre_wp_context_h m_context;
+};
+
+template <>
+class Context<csre_cs_engine_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
+       }
+
+       csre_cs_engine_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csre_cs_engine_h m_context;
+};
+
+template <>
+class Context<csre_wp_engine_h> {
+public:
+       Context() : m_context(nullptr)
+       {
+               ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
+               BOOST_REQUIRE(m_context != nullptr);
+       }
+
+       virtual ~Context()
+       {
+               ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
+       }
+
+       csre_wp_engine_h get(void) const
+       {
+               return m_context;
+       }
+
+private:
+       csre_wp_engine_h m_context;
+};
+
+} // namespace Test