Add warn_if on test
[platform/upstream/csr-framework.git] / test / test-common.h
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file        test-common.h
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       Common utilities for test
21  */
22 #pragma once
23
24 #include <sstream>
25 #include <iostream>
26 #include <ios>
27 #include <functional>
28 #include <typeinfo>
29 #include <string>
30 #include <cstring>
31 #include <cerrno>
32 #include <system_error>
33 #include <unistd.h>
34
35 #include <boost/test/unit_test.hpp>
36
37 #include <csr-content-screening.h>
38 #include <csr-web-protection.h>
39
40 #include <csre-content-screening.h>
41 #include <csre-web-protection.h>
42
43 #ifndef __FILENAME__
44 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
45 #endif
46
47 #define TOSTRING(ITEMS)                                                         \
48         (dynamic_cast<std::ostringstream &>(std::ostringstream().seekp(             \
49                                                                                 0, std::ios_base::cur) << ITEMS)).str()
50
51 #define ASSERT_IF_MSG(value, expected, msg) \
52         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, TOSTRING(msg))
53
54 #define WARN_IF_MSG(value, expected, msg) \
55         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, TOSTRING(msg))
56
57 #define ASSERT_IF(value, expected) \
58         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, "")
59
60 #define WARN_IF(value, expected) \
61         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, "")
62
63 #define ASSERT_SUCCESS(value) \
64         Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, true, "")
65
66 #define WARN_SUCCESS(value) \
67         Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, false, "")
68
69 #define ASSERT_INSTALL_APP(path, type)                       \
70         BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
71                                                   "Failed to install app: " << path)
72
73 #define ASSERT_UNINSTALL_APP(path)                             \
74         BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path),           \
75                                                   "Failed to uninstall app: " << path)
76
77 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
78 #define EXCEPTION_GUARD_END   });
79
80 #define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
81 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
82
83 namespace Test {
84
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)
89 {
90         if (isAssert)
91                 BOOST_REQUIRE_MESSAGE(value == expected,
92                                                           "[" << filename << " > " << funcname << " : " << line <<
93                                                           "]" << " returned[" << value << "] expected[" << expected <<
94                                                           "] " << msg);
95         else
96                 BOOST_WARN_MESSAGE(value == expected,
97                                                           "[" << filename << " > " << funcname << " : " << line <<
98                                                           "]" << " returned[" << value << "] expected[" << expected <<
99                                                           "] " << msg);
100 }
101
102 template <>
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,
107                                                                            unsigned int line,
108                                                                            bool isAssert,
109                                                                            const std::string &msg);
110
111 template <>
112 void _assert<csr_error_e, int>(const csr_error_e &value,
113                                                            const int &expected,
114                                                            const std::string &filename,
115                                                            const std::string &funcname,
116                                                            unsigned int line,
117                                                            bool isAssert,
118                                                            const std::string &msg);
119
120 template <>
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,
125                                                            unsigned int line,
126                                                            bool isAssert,
127                                                            const std::string &msg);
128
129 template <>
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,
134                                                                                  unsigned int line,
135                                                                                  bool isAssert,
136                                                                                  const std::string &msg);
137
138 template <>
139 void _assert<char *, const char *>(char * const &value,
140                                                                    const char * const &expected,
141                                                                    const std::string &filename,
142                                                                    const std::string &funcname,
143                                                                    unsigned int line,
144                                                                    bool isAssert,
145                                                                    const std::string &msg);
146
147 template <>
148 void _assert<const char *, char *>(const char * const &value,
149                                                                    char * const &expected,
150                                                                    const std::string &filename,
151                                                                    const std::string &funcname,
152                                                                    unsigned int line,
153                                                                    bool isAssert,
154                                                                    const std::string &msg);
155
156 template <>
157 void _assert<char *, char *>(char * const &value,
158                                                          char * const &expected,
159                                                          const std::string &filename,
160                                                          const std::string &funcname,
161                                                          unsigned int line,
162                                                          bool isAssert,
163                                                          const std::string &msg);
164
165 template <>
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,
170                                                                                 unsigned int line,
171                                                                                 bool isAssert,
172                                                                                 const std::string &msg);
173
174 template <>
175 void _assert<char *, std::string>(char * const &value,
176                                                                   const std::string &expected,
177                                                                   const std::string &filename,
178                                                                   const std::string &funcname,
179                                                                   unsigned int line,
180                                                                   bool isAssert,
181                                                                   const std::string &msg);
182
183 void exceptionGuard(const std::function<void()> &);
184
185 void make_dir(const char *dir);
186 void copy_file(const char *src_file, const char *dest_file);
187 void touch_file(const char *file);
188 void remove_file(const char *file);
189 bool is_file_exist(const char *file);
190 bool install_app(const char *app_path, const char *app_type);
191 bool uninstall_app(const char *pkg_id);
192 void initialize_db();
193
194 struct ScopedCstr {
195         char *ptr;
196
197         ScopedCstr() : ptr(nullptr) {}
198         ~ScopedCstr()
199         {
200                 if (ptr)
201                         free(ptr);
202         }
203 };
204
205 class ScopedChDir {
206 public:
207         ScopedChDir(const std::string &dirpath)
208         {
209                 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
210                         throw std::system_error(errno, std::system_category(), "getcwd failed");
211
212                 if (::chdir(dirpath.c_str()) == -1)
213                         throw std::system_error(errno, std::system_category(),
214                                                                         dirpath + " chdir failed");
215         }
216
217         ~ScopedChDir()
218         {
219                 if (::chdir(cdbuf) == -1)
220                         throw std::system_error(errno, std::system_category(),
221                                                                         std::string(cdbuf) + " chdir failed");
222         }
223
224 private:
225         char cdbuf[PATH_MAX + 1];
226 };
227
228 template <typename T>
229 class Context {
230 public:
231         Context() : m_context(nullptr)
232         {
233                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
234                                                           << "] isn't specialized for context template");
235         }
236
237         virtual ~Context()
238         {
239                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
240                                                           << "] isn't specialized for context template");
241         }
242
243         T get(void) const
244         {
245                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
246                                                           << "] isn't specialized for context template");
247
248                 return nullptr;
249         }
250
251 private:
252         T m_context;
253 };
254
255 template <>
256 class Context<csr_cs_context_h> {
257 public:
258         Context() : m_context(nullptr)
259         {
260                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
261                 BOOST_REQUIRE(m_context != nullptr);
262         }
263
264         virtual ~Context()
265         {
266                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
267         }
268
269         csr_cs_context_h get(void) const
270         {
271                 return m_context;
272         }
273
274 private:
275         csr_cs_context_h m_context;
276 };
277
278 template <>
279 class Context<csr_wp_context_h> {
280 public:
281         Context() : m_context(nullptr)
282         {
283                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
284                 BOOST_REQUIRE(m_context != nullptr);
285         }
286
287         virtual ~Context()
288         {
289                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
290         }
291
292         csr_wp_context_h get(void) const
293         {
294                 return m_context;
295         }
296
297 private:
298         csr_wp_context_h m_context;
299 };
300
301 template <>
302 class Context<csre_cs_context_h> {
303 public:
304         Context() : m_context(nullptr)
305         {
306                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
307                 BOOST_REQUIRE(m_context != nullptr);
308         }
309
310         virtual ~Context()
311         {
312                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
313         }
314
315         csre_cs_context_h get(void) const
316         {
317                 return m_context;
318         }
319
320 private:
321         csre_cs_context_h m_context;
322 };
323
324 template <>
325 class Context<csre_wp_context_h> {
326 public:
327         Context() : m_context(nullptr)
328         {
329                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
330                 BOOST_REQUIRE(m_context != nullptr);
331         }
332
333         virtual ~Context()
334         {
335                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
336         }
337
338         csre_wp_context_h get(void) const
339         {
340                 return m_context;
341         }
342
343 private:
344         csre_wp_context_h m_context;
345 };
346
347 } // namespace Test