2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Kyungwook Tak (k.tak@samsung.com)
20 * @brief Common utilities for test
29 #include <boost/test/unit_test.hpp>
31 #include <csr/content-screening.h>
32 #include <csr/web-protection.h>
34 #include <csre/content-screening.h>
35 #include <csre/web-protection.h>
37 #include <csre/content-screening-engine-info.h>
38 #include <csre/web-protection-engine-info.h>
41 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
44 #define ASSERT_IF(value, expected) \
45 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__)
47 #define ASSERT_SUCCESS(value) \
48 Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__)
50 #define ASSERT_INSTALL_APP(path, type) \
51 BOOST_REQUIRE_MESSAGE(Test::install_app(path, type), \
52 "Failed to install app: " << path)
54 #define ASSERT_UNINSTALL_APP(path) \
55 BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path), \
56 "Failed to uninstall app: " << path)
58 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
59 #define EXCEPTION_GUARD_END });
61 #define CHECK_IS_NULL(ptr) BOOST_REQUIRE(ptr == nullptr)
62 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
66 template <typename T, typename U>
67 void _assert(const T &value, const U &expected, const std::string &filename,
68 const std::string &funcname, unsigned int line)
70 BOOST_REQUIRE_MESSAGE(value == expected,
71 "[" << filename << " > " << funcname << " : " << line << "]" <<
72 " returned[" << value << "] expected[" << expected << "]");
76 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
77 const csr_error_e &expected,
78 const std::string &filename,
79 const std::string &funcname,
83 void _assert<csr_error_e, int>(const csr_error_e &value,
85 const std::string &filename,
86 const std::string &funcname,
90 void _assert<int, csr_error_e>(const int &value,
91 const csr_error_e &expected,
92 const std::string &filename,
93 const std::string &funcname,
96 void _assert(const char *value, const char *expected, const std::string &filename,
97 const std::string &funcname, unsigned int line);
98 void _assert(const char *value, const std::string &expected, const std::string &filename,
99 const std::string &funcname, unsigned int line);
101 void exceptionGuard(const std::function<void()> &);
103 void copy_file(const char *src_file, const char *dest_file);
104 void touch_file(const char *file);
105 bool is_file_exist(const char *file);
106 bool install_app(const char *app_path, const char *app_type);
107 bool uninstall_app(const char *pkg_id);
108 bool is_app_exist(const char *pkg_id);
110 template <typename T>
113 Context() : m_context(nullptr)
115 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
116 << "] isn't specialized for context template");
121 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
122 << "] isn't specialized for context template");
127 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
128 << "] isn't specialized for context template");
138 class Context<csr_cs_context_h> {
140 Context() : m_context(nullptr)
142 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
143 BOOST_REQUIRE(m_context != nullptr);
148 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
151 csr_cs_context_h get(void) const
157 csr_cs_context_h m_context;
161 class Context<csr_wp_context_h> {
163 Context() : m_context(nullptr)
165 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
166 BOOST_REQUIRE(m_context != nullptr);
171 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
174 csr_wp_context_h get(void) const
180 csr_wp_context_h m_context;
184 class Context<csre_cs_context_h> {
186 Context() : m_context(nullptr)
188 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
189 BOOST_REQUIRE(m_context != nullptr);
194 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
197 csre_cs_context_h get(void) const
203 csre_cs_context_h m_context;
207 class Context<csre_wp_context_h> {
209 Context() : m_context(nullptr)
211 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
212 BOOST_REQUIRE(m_context != nullptr);
217 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
220 csre_wp_context_h get(void) const
226 csre_wp_context_h m_context;
230 class Context<csre_cs_engine_h> {
232 Context() : m_context(nullptr)
234 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
235 BOOST_REQUIRE(m_context != nullptr);
240 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
243 csre_cs_engine_h get(void) const
249 csre_cs_engine_h m_context;
253 class Context<csre_wp_engine_h> {
255 Context() : m_context(nullptr)
257 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
258 BOOST_REQUIRE(m_context != nullptr);
263 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
266 csre_wp_engine_h get(void) const
272 csre_wp_engine_h m_context;