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
32 #include <system_error>
35 #include <boost/test/unit_test.hpp>
37 #include <csr-content-screening.h>
38 #include <csr-web-protection.h>
40 #include <csre-content-screening.h>
41 #include <csre-web-protection.h>
43 #include <csre-content-screening-engine-info.h>
44 #include <csre-web-protection-engine-info.h>
47 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
50 #define TOSTRING(ITEMS) \
51 (dynamic_cast<std::ostringstream &>(std::ostringstream().seekp( \
52 0, std::ios_base::cur) << ITEMS)).str()
54 #define ASSERT_IF_MSG(value, expected, msg) \
55 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, TOSTRING(msg))
57 #define ASSERT_IF(value, expected) \
58 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, "")
60 #define ASSERT_SUCCESS(value) \
61 Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, "")
63 #define ASSERT_INSTALL_APP(path, type) \
64 BOOST_REQUIRE_MESSAGE(Test::install_app(path, type), \
65 "Failed to install app: " << path)
67 #define ASSERT_UNINSTALL_APP(path) \
68 BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path), \
69 "Failed to uninstall app: " << path)
71 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
72 #define EXCEPTION_GUARD_END });
74 #define CHECK_IS_NULL(ptr) BOOST_REQUIRE(ptr == nullptr)
75 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
79 template <typename T, typename U>
80 void _assert(const T &value, const U &expected, const std::string &filename,
81 const std::string &funcname, unsigned int line, const std::string &msg)
83 BOOST_REQUIRE_MESSAGE(value == expected,
84 "[" << filename << " > " << funcname << " : " << line << "]" <<
85 " returned[" << value << "] expected[" << expected <<
90 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
91 const csr_error_e &expected,
92 const std::string &filename,
93 const std::string &funcname,
95 const std::string &msg);
98 void _assert<csr_error_e, int>(const csr_error_e &value,
100 const std::string &filename,
101 const std::string &funcname,
103 const std::string &msg);
106 void _assert<int, csr_error_e>(const int &value,
107 const csr_error_e &expected,
108 const std::string &filename,
109 const std::string &funcname,
111 const std::string &msg);
114 void _assert<const char *, const char *>(const char * const &value,
115 const char * const &expected,
116 const std::string &filename,
117 const std::string &funcname,
119 const std::string &msg);
122 void _assert<char *, const char *>(char * const &value,
123 const char * const &expected,
124 const std::string &filename,
125 const std::string &funcname,
127 const std::string &msg);
130 void _assert<const char *, char *>(const char * const &value,
131 char * const &expected,
132 const std::string &filename,
133 const std::string &funcname,
135 const std::string &msg);
138 void _assert<char *, char *>(char * const &value,
139 char * const &expected,
140 const std::string &filename,
141 const std::string &funcname,
143 const std::string &msg);
146 void _assert<const char *, std::string>(const char * const &value,
147 const std::string &expected,
148 const std::string &filename,
149 const std::string &funcname,
151 const std::string &msg);
154 void _assert<char *, std::string>(char * const &value,
155 const std::string &expected,
156 const std::string &filename,
157 const std::string &funcname,
159 const std::string &msg);
161 void exceptionGuard(const std::function<void()> &);
163 void make_dir(const char *dir);
164 void copy_file(const char *src_file, const char *dest_file);
165 void touch_file(const char *file);
166 void remove_file(const char *file);
167 bool is_file_exist(const char *file);
168 bool install_app(const char *app_path, const char *app_type);
169 bool uninstall_app(const char *pkg_id);
170 void initialize_db();
175 ScopedCstr() : ptr(nullptr) {}
185 ScopedChDir(const std::string &dirpath)
187 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
188 throw std::system_error(errno, std::system_category(), "getcwd failed");
190 if (::chdir(dirpath.c_str()) == -1)
191 throw std::system_error(errno, std::system_category(),
192 dirpath + " chdir failed");
197 if (::chdir(cdbuf) == -1)
198 throw std::system_error(errno, std::system_category(),
199 std::string(cdbuf) + " chdir failed");
203 char cdbuf[PATH_MAX + 1];
206 template <typename T>
209 Context() : m_context(nullptr)
211 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
212 << "] isn't specialized for context template");
217 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
218 << "] isn't specialized for context template");
223 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
224 << "] isn't specialized for context template");
234 class Context<csr_cs_context_h> {
236 Context() : m_context(nullptr)
238 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
239 BOOST_REQUIRE(m_context != nullptr);
244 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
247 csr_cs_context_h get(void) const
253 csr_cs_context_h m_context;
257 class Context<csr_wp_context_h> {
259 Context() : m_context(nullptr)
261 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
262 BOOST_REQUIRE(m_context != nullptr);
267 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
270 csr_wp_context_h get(void) const
276 csr_wp_context_h m_context;
280 class Context<csre_cs_context_h> {
282 Context() : m_context(nullptr)
284 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
285 BOOST_REQUIRE(m_context != nullptr);
290 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
293 csre_cs_context_h get(void) const
299 csre_cs_context_h m_context;
303 class Context<csre_wp_context_h> {
305 Context() : m_context(nullptr)
307 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
308 BOOST_REQUIRE(m_context != nullptr);
313 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
316 csre_wp_context_h get(void) const
322 csre_wp_context_h m_context;
326 class Context<csre_cs_engine_h> {
328 Context() : m_context(nullptr)
330 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
331 BOOST_REQUIRE(m_context != nullptr);
336 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
339 csre_cs_engine_h get(void) const
345 csre_cs_engine_h m_context;
349 class Context<csre_wp_engine_h> {
351 Context() : m_context(nullptr)
353 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
354 BOOST_REQUIRE(m_context != nullptr);
359 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
362 csre_wp_engine_h get(void) const
368 csre_wp_engine_h m_context;