[CAPI Changed] Enum value names and scan cloud
[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 #include <csre-content-screening-engine-info.h>
44 #include <csre-web-protection-engine-info.h>
45
46 #ifndef __FILENAME__
47 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
48 #endif
49
50 #define TOSTRING(ITEMS)                                                         \
51         (dynamic_cast<std::ostringstream &>(std::ostringstream().seekp(             \
52                                                                                 0, std::ios_base::cur) << ITEMS)).str()
53
54 #define ASSERT_IF_MSG(value, expected, msg) \
55         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, TOSTRING(msg))
56
57 #define ASSERT_IF(value, expected) \
58         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, "")
59
60 #define ASSERT_SUCCESS(value) \
61         Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, "")
62
63 #define ASSERT_INSTALL_APP(path, type)                       \
64         BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
65                                                   "Failed to install app: " << path)
66
67 #define ASSERT_UNINSTALL_APP(path)                             \
68         BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path),           \
69                                                   "Failed to uninstall app: " << path)
70
71 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
72 #define EXCEPTION_GUARD_END   });
73
74 #define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
75 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
76
77 namespace Test {
78
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)
82 {
83         BOOST_REQUIRE_MESSAGE(value == expected,
84                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
85                                                   " returned[" << value << "] expected[" << expected <<
86                                                   "] " << msg);
87 }
88
89 template <>
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,
94                                                                            unsigned int line,
95                                                                            const std::string &msg);
96
97 template <>
98 void _assert<csr_error_e, int>(const csr_error_e &value,
99                                                            const int &expected,
100                                                            const std::string &filename,
101                                                            const std::string &funcname,
102                                                            unsigned int line,
103                                                            const std::string &msg);
104
105 template <>
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,
110                                                            unsigned int line,
111                                                            const std::string &msg);
112
113 template <>
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,
118                                                                                  unsigned int line,
119                                                                                  const std::string &msg);
120
121 template <>
122 void _assert<char *, const char *>(char * const &value,
123                                                                    const char * const &expected,
124                                                                    const std::string &filename,
125                                                                    const std::string &funcname,
126                                                                    unsigned int line,
127                                                                    const std::string &msg);
128
129 template <>
130 void _assert<const char *, char *>(const char * const &value,
131                                                                    char * const &expected,
132                                                                    const std::string &filename,
133                                                                    const std::string &funcname,
134                                                                    unsigned int line,
135                                                                    const std::string &msg);
136
137 template <>
138 void _assert<char *, char *>(char * const &value,
139                                                          char * const &expected,
140                                                          const std::string &filename,
141                                                          const std::string &funcname,
142                                                          unsigned int line,
143                                                          const std::string &msg);
144
145 template <>
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,
150                                                                                 unsigned int line,
151                                                                                 const std::string &msg);
152
153 template <>
154 void _assert<char *, std::string>(char * const &value,
155                                                                   const std::string &expected,
156                                                                   const std::string &filename,
157                                                                   const std::string &funcname,
158                                                                   unsigned int line,
159                                                                   const std::string &msg);
160
161 void exceptionGuard(const std::function<void()> &);
162
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();
171
172 struct ScopedCstr {
173         char *ptr;
174
175         ScopedCstr() : ptr(nullptr) {}
176         ~ScopedCstr()
177         {
178                 if (ptr)
179                         free(ptr);
180         }
181 };
182
183 class ScopedChDir {
184 public:
185         ScopedChDir(const std::string &dirpath)
186         {
187                 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
188                         throw std::system_error(errno, std::system_category(), "getcwd failed");
189
190                 if (::chdir(dirpath.c_str()) == -1)
191                         throw std::system_error(errno, std::system_category(),
192                                                                         dirpath + " chdir failed");
193         }
194
195         ~ScopedChDir()
196         {
197                 if (::chdir(cdbuf) == -1)
198                         throw std::system_error(errno, std::system_category(),
199                                                                         std::string(cdbuf) + " chdir failed");
200         }
201
202 private:
203         char cdbuf[PATH_MAX + 1];
204 };
205
206 template <typename T>
207 class Context {
208 public:
209         Context() : m_context(nullptr)
210         {
211                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
212                                                           << "] isn't specialized for context template");
213         }
214
215         virtual ~Context()
216         {
217                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
218                                                           << "] isn't specialized for context template");
219         }
220
221         T get(void) const
222         {
223                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
224                                                           << "] isn't specialized for context template");
225
226                 return nullptr;
227         }
228
229 private:
230         T m_context;
231 };
232
233 template <>
234 class Context<csr_cs_context_h> {
235 public:
236         Context() : m_context(nullptr)
237         {
238                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
239                 BOOST_REQUIRE(m_context != nullptr);
240         }
241
242         virtual ~Context()
243         {
244                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
245         }
246
247         csr_cs_context_h get(void) const
248         {
249                 return m_context;
250         }
251
252 private:
253         csr_cs_context_h m_context;
254 };
255
256 template <>
257 class Context<csr_wp_context_h> {
258 public:
259         Context() : m_context(nullptr)
260         {
261                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
262                 BOOST_REQUIRE(m_context != nullptr);
263         }
264
265         virtual ~Context()
266         {
267                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
268         }
269
270         csr_wp_context_h get(void) const
271         {
272                 return m_context;
273         }
274
275 private:
276         csr_wp_context_h m_context;
277 };
278
279 template <>
280 class Context<csre_cs_context_h> {
281 public:
282         Context() : m_context(nullptr)
283         {
284                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
285                 BOOST_REQUIRE(m_context != nullptr);
286         }
287
288         virtual ~Context()
289         {
290                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
291         }
292
293         csre_cs_context_h get(void) const
294         {
295                 return m_context;
296         }
297
298 private:
299         csre_cs_context_h m_context;
300 };
301
302 template <>
303 class Context<csre_wp_context_h> {
304 public:
305         Context() : m_context(nullptr)
306         {
307                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
308                 BOOST_REQUIRE(m_context != nullptr);
309         }
310
311         virtual ~Context()
312         {
313                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
314         }
315
316         csre_wp_context_h get(void) const
317         {
318                 return m_context;
319         }
320
321 private:
322         csre_wp_context_h m_context;
323 };
324
325 template <>
326 class Context<csre_cs_engine_h> {
327 public:
328         Context() : m_context(nullptr)
329         {
330                 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
331                 BOOST_REQUIRE(m_context != nullptr);
332         }
333
334         virtual ~Context()
335         {
336                 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
337         }
338
339         csre_cs_engine_h get(void) const
340         {
341                 return m_context;
342         }
343
344 private:
345         csre_cs_engine_h m_context;
346 };
347
348 template <>
349 class Context<csre_wp_engine_h> {
350 public:
351         Context() : m_context(nullptr)
352         {
353                 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
354                 BOOST_REQUIRE(m_context != nullptr);
355         }
356
357         virtual ~Context()
358         {
359                 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
360         }
361
362         csre_wp_engine_h get(void) const
363         {
364                 return m_context;
365         }
366
367 private:
368         csre_wp_engine_h m_context;
369 };
370
371 } // namespace Test