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>
44 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
47 #define TOSTRING(ITEMS) \
48 (dynamic_cast<std::ostringstream &>(std::ostringstream().seekp( \
49 0, std::ios_base::cur) << ITEMS)).str()
51 #define ASSERT_IF_MSG(value, expected, msg) \
52 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, TOSTRING(msg))
54 #define WARN_IF_MSG(value, expected, msg) \
55 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, TOSTRING(msg))
57 #define ASSERT_IF(value, expected) \
58 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, "")
60 #define WARN_IF(value, expected) \
61 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, "")
63 #define ASSERT_SUCCESS(value) \
64 Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, true, "")
66 #define WARN_SUCCESS(value) \
67 Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, false, "")
69 #define ASSERT_INSTALL_APP(path, type) \
70 BOOST_REQUIRE_MESSAGE(Test::install_app(path, type), \
71 "Failed to install app: " << path)
73 #define ASSERT_UNINSTALL_APP(path) \
74 BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path), \
75 "Failed to uninstall app: " << path)
77 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
78 #define EXCEPTION_GUARD_END });
80 #define CHECK_IS_NULL(ptr) BOOST_REQUIRE(ptr == nullptr)
81 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
85 template <typename T, typename U>
86 void _assert(const T &value, const U &expected, const std::string &filename,
87 const std::string &funcname, unsigned int line, bool isAssert,
88 const std::string &msg)
91 BOOST_REQUIRE_MESSAGE(value == expected,
92 "[" << filename << " > " << funcname << " : " << line <<
93 "]" << " returned[" << value << "] expected[" << expected <<
96 BOOST_WARN_MESSAGE(value == expected,
97 "[" << filename << " > " << funcname << " : " << line <<
98 "]" << " returned[" << value << "] expected[" << expected <<
103 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
104 const csr_error_e &expected,
105 const std::string &filename,
106 const std::string &funcname,
109 const std::string &msg);
112 void _assert<csr_error_e, int>(const csr_error_e &value,
114 const std::string &filename,
115 const std::string &funcname,
118 const std::string &msg);
121 void _assert<int, csr_error_e>(const int &value,
122 const csr_error_e &expected,
123 const std::string &filename,
124 const std::string &funcname,
127 const std::string &msg);
130 void _assert<const char *, const char *>(const char * const &value,
131 const char * const &expected,
132 const std::string &filename,
133 const std::string &funcname,
136 const std::string &msg);
139 void _assert<char *, const char *>(char * const &value,
140 const char * const &expected,
141 const std::string &filename,
142 const std::string &funcname,
145 const std::string &msg);
148 void _assert<const char *, char *>(const char * const &value,
149 char * const &expected,
150 const std::string &filename,
151 const std::string &funcname,
154 const std::string &msg);
157 void _assert<char *, char *>(char * const &value,
158 char * const &expected,
159 const std::string &filename,
160 const std::string &funcname,
163 const std::string &msg);
166 void _assert<const char *, std::string>(const char * const &value,
167 const std::string &expected,
168 const std::string &filename,
169 const std::string &funcname,
172 const std::string &msg);
175 void _assert<char *, std::string>(char * const &value,
176 const std::string &expected,
177 const std::string &filename,
178 const std::string &funcname,
181 const std::string &msg);
183 void exceptionGuard(const std::function<void()> &);
185 std::string capi_ec_to_string(csr_error_e ec);
186 std::string capi_ec_to_string(int ec);
188 void make_dir(const char *dir);
189 void make_dir_assert(const char *dir);
190 void copy_file(const char *src_file, const char *dest_file);
191 void copy_file_assert(const char *src_file, const char *dest_file);
192 void touch_file(const char *file);
193 void touch_file_assert(const char *file);
194 void remove_file(const char *file);
195 void remove_file_assert(const char *file);
196 bool is_file_exist(const char *file);
197 bool install_app(const char *app_path, const char *app_type);
198 bool uninstall_app(const char *pkg_id);
199 void initialize_db();
204 ScopedCstr() : ptr(nullptr) {}
214 ScopedChDir(const std::string &dirpath)
216 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
217 throw std::system_error(errno, std::system_category(), "getcwd failed");
219 if (::chdir(dirpath.c_str()) == -1)
220 throw std::system_error(errno, std::system_category(),
221 dirpath + " chdir failed");
226 if (::chdir(cdbuf) == -1)
227 throw std::system_error(errno, std::system_category(),
228 std::string(cdbuf) + " chdir failed");
232 char cdbuf[PATH_MAX + 1];
235 template <typename T>
238 Context() : m_context(nullptr)
240 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
241 << "] isn't specialized for context template");
246 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
247 << "] isn't specialized for context template");
252 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
253 << "] isn't specialized for context template");
263 class Context<csr_cs_context_h> {
265 Context() : m_context(nullptr)
267 ASSERT_SUCCESS(csr_cs_context_create(&m_context));
268 BOOST_REQUIRE(m_context != nullptr);
273 ASSERT_SUCCESS(csr_cs_context_destroy(m_context));
276 csr_cs_context_h get(void) const
282 csr_cs_context_h m_context;
286 class Context<csr_wp_context_h> {
288 Context() : m_context(nullptr)
290 ASSERT_SUCCESS(csr_wp_context_create(&m_context));
291 BOOST_REQUIRE(m_context != nullptr);
296 ASSERT_SUCCESS(csr_wp_context_destroy(m_context));
299 csr_wp_context_h get(void) const
305 csr_wp_context_h m_context;
309 class Context<csre_cs_context_h> {
311 Context() : m_context(nullptr)
313 ASSERT_SUCCESS(csre_cs_context_create(&m_context));
314 BOOST_REQUIRE(m_context != nullptr);
319 ASSERT_SUCCESS(csre_cs_context_destroy(m_context));
322 csre_cs_context_h get(void) const
328 csre_cs_context_h m_context;
332 class Context<csre_wp_context_h> {
334 Context() : m_context(nullptr)
336 ASSERT_SUCCESS(csre_wp_context_create(&m_context));
337 BOOST_REQUIRE(m_context != nullptr);
342 ASSERT_SUCCESS(csre_wp_context_destroy(m_context));
345 csre_wp_context_h get(void) const
351 csre_wp_context_h m_context;