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__, TOSTRING(msg))
54 #define ASSERT_IF(value, expected) \
55 Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, "")
57 #define ASSERT_SUCCESS(value) \
58 Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, "")
60 #define ASSERT_INSTALL_APP(path, type) \
61 BOOST_REQUIRE_MESSAGE(Test::install_app(path, type), \
62 "Failed to install app: " << path)
64 #define ASSERT_UNINSTALL_APP(path) \
65 BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path), \
66 "Failed to uninstall app: " << path)
68 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
69 #define EXCEPTION_GUARD_END });
71 #define CHECK_IS_NULL(ptr) BOOST_REQUIRE(ptr == nullptr)
72 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
76 template <typename T, typename U>
77 void _assert(const T &value, const U &expected, const std::string &filename,
78 const std::string &funcname, unsigned int line, const std::string &msg)
80 BOOST_REQUIRE_MESSAGE(value == expected,
81 "[" << filename << " > " << funcname << " : " << line << "]" <<
82 " returned[" << value << "] expected[" << expected <<
87 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
88 const csr_error_e &expected,
89 const std::string &filename,
90 const std::string &funcname,
92 const std::string &msg);
95 void _assert<csr_error_e, int>(const csr_error_e &value,
97 const std::string &filename,
98 const std::string &funcname,
100 const std::string &msg);
103 void _assert<int, csr_error_e>(const int &value,
104 const csr_error_e &expected,
105 const std::string &filename,
106 const std::string &funcname,
108 const std::string &msg);
111 void _assert<const char *, const char *>(const char * const &value,
112 const char * const &expected,
113 const std::string &filename,
114 const std::string &funcname,
116 const std::string &msg);
119 void _assert<char *, const char *>(char * const &value,
120 const char * const &expected,
121 const std::string &filename,
122 const std::string &funcname,
124 const std::string &msg);
127 void _assert<const char *, char *>(const char * const &value,
128 char * const &expected,
129 const std::string &filename,
130 const std::string &funcname,
132 const std::string &msg);
135 void _assert<char *, char *>(char * const &value,
136 char * const &expected,
137 const std::string &filename,
138 const std::string &funcname,
140 const std::string &msg);
143 void _assert<const char *, std::string>(const char * const &value,
144 const std::string &expected,
145 const std::string &filename,
146 const std::string &funcname,
148 const std::string &msg);
151 void _assert<char *, std::string>(char * const &value,
152 const std::string &expected,
153 const std::string &filename,
154 const std::string &funcname,
156 const std::string &msg);
158 void exceptionGuard(const std::function<void()> &);
160 void make_dir(const char *dir);
161 void copy_file(const char *src_file, const char *dest_file);
162 void touch_file(const char *file);
163 void remove_file(const char *file);
164 bool is_file_exist(const char *file);
165 bool install_app(const char *app_path, const char *app_type);
166 bool uninstall_app(const char *pkg_id);
167 void initialize_db();
172 ScopedCstr() : ptr(nullptr) {}
182 ScopedChDir(const std::string &dirpath)
184 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
185 throw std::system_error(errno, std::system_category(), "getcwd failed");
187 if (::chdir(dirpath.c_str()) == -1)
188 throw std::system_error(errno, std::system_category(),
189 dirpath + " chdir failed");
194 if (::chdir(cdbuf) == -1)
195 throw std::system_error(errno, std::system_category(),
196 std::string(cdbuf) + " chdir failed");
200 char cdbuf[PATH_MAX + 1];
203 template <typename T>
206 Context() : m_context(nullptr)
208 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
209 << "] isn't specialized for context template");
214 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
215 << "] isn't specialized for context template");
220 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
221 << "] isn't specialized for context template");
231 class Context<csr_cs_context_h> {
233 Context() : m_context(nullptr)
235 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
236 BOOST_REQUIRE(m_context != nullptr);
241 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
244 csr_cs_context_h get(void) const
250 csr_cs_context_h m_context;
254 class Context<csr_wp_context_h> {
256 Context() : m_context(nullptr)
258 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
259 BOOST_REQUIRE(m_context != nullptr);
264 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
267 csr_wp_context_h get(void) const
273 csr_wp_context_h m_context;
277 class Context<csre_cs_context_h> {
279 Context() : m_context(nullptr)
281 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
282 BOOST_REQUIRE(m_context != nullptr);
287 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
290 csre_cs_context_h get(void) const
296 csre_cs_context_h m_context;
300 class Context<csre_wp_context_h> {
302 Context() : m_context(nullptr)
304 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
305 BOOST_REQUIRE(m_context != nullptr);
310 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
313 csre_wp_context_h get(void) const
319 csre_wp_context_h m_context;