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
#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>
{
// 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) {
void AsyncLogic::add(Result *r)
{
- m_ctx.add(r);
+ m_ctx->add(r);
}
}
#include <atomic>
#include "common/types.h"
+#include "common/cs-context.h"
#include "common/dispatcher.h"
#include "client/callback.h"
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();
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;
};
+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);
+}
+
}
}
#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)
{
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;
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
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;
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;
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;
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;
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;
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;
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
namespace Csr {
namespace Client {
-HandleExt::HandleExt() : m_stop(false)
+HandleExt::HandleExt(std::shared_ptr<Context> &&context) :
+ Handle(std::move(context)),
+ m_stop(false)
{
}
class HandleExt : public Handle {
public:
- HandleExt();
+ explicit HandleExt(std::shared_ptr<Context> &&);
virtual ~HandleExt();
void dispatchAsync(const Task &task);
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;
}
if (result == nullptr)
throw std::logic_error("result shouldn't be null");
- m_ctx.add(result);
+ m_ctx->add(result);
}
} // namespace Client
#pragma once
#include <utility>
+#include <memory>
#include "common/types.h"
#include "common/dispatcher.h"
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>
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;
--- /dev/null
+/*
+ * 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."));
+ }
+}
+
+}
--- /dev/null
+/*
+ * 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;
+};
+
+}
--- /dev/null
+/*
+ * 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."));
+ }
+}
+
+}
--- /dev/null
+/*
+ * 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;
+};
+
+}
--- /dev/null
+/*
+ * 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!");
+}
+
+
+}
--- /dev/null
+/*
+ * 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;
+};
+
+}
#pragma once
#include <stdint.h>
+#include <time.h>
#include <string>
#include <vector>
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)
{
{
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 {
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)
{
#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();
bool m_hasVal;
};
-class Context : public ISerializable {
+class Context : public ISerializable, public KvpContainer {
public:
Context();
virtual ~Context();
#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()
{
}
switch (info.first) {
case CommandId::SCAN_FILE: {
- Context context;
+ CsContext context;
std::string filepath;
info.second.Deserialize(context, filepath);
return scanFile(context, filepath);
}
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);
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();
}
}
#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"
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);
};
}
* @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.
* @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.
* @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.
* @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.
/* 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
${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
test-internal-database.cpp
test-internal-cpucore.cpp
test-main.cpp
+ test-common.cpp
)
INCLUDE_DIRECTORIES(
--- /dev/null
+/*
+ * 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()
#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()
#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"
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);
}
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);
}
long timestamp;
- BOOST_REQUIRE_NO_THROW(ret = csre_cs_detected_get_timestamp(detected, ×tamp));
- BOOST_REQUIRE(ret == CSRE_ERROR_NONE);
+ ASSERT_IF(csre_cs_detected_get_timestamp(detected, ×tamp), CSRE_ERROR_NONE);
if (expected_timestamp != 0)
BOOST_REQUIRE_MESSAGE(timestamp == expected_timestamp,
"timestamp isn't expected value. "
"val: " << timestamp << " expected: " << expected_timestamp);
-}
-
-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:
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,
"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,
"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,
"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,
"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,
"test_malware",
"http://high.malware.com",
0);
-}
+ EXCEPTION_GUARD_END
+}
BOOST_AUTO_TEST_SUITE_END()
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()
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()
#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 {
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
}
}
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()
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()
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()
#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()
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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