[CAPI Changed] Client should free returned cstring
[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
32 #include <boost/test/unit_test.hpp>
33
34 #include <csr-content-screening.h>
35 #include <csr-web-protection.h>
36
37 #include <csre-content-screening.h>
38 #include <csre-web-protection.h>
39
40 #include <csre-content-screening-engine-info.h>
41 #include <csre-web-protection-engine-info.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__, TOSTRING(msg))
53
54 #define ASSERT_IF(value, expected) \
55         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, "")
56
57 #define ASSERT_SUCCESS(value) \
58         Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, "")
59
60 #define ASSERT_INSTALL_APP(path, type)                       \
61         BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
62                                                   "Failed to install app: " << path)
63
64 #define ASSERT_UNINSTALL_APP(path)                             \
65         BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path),           \
66                                                   "Failed to uninstall app: " << path)
67
68 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
69 #define EXCEPTION_GUARD_END   });
70
71 #define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
72 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
73
74 namespace Test {
75
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)
79 {
80         BOOST_REQUIRE_MESSAGE(value == expected,
81                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
82                                                   " returned[" << value << "] expected[" << expected <<
83                                                   "] " << msg);
84 }
85
86 template <>
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,
91                                                                            unsigned int line,
92                                                                            const std::string &msg);
93
94 template <>
95 void _assert<csr_error_e, int>(const csr_error_e &value,
96                                                            const int &expected,
97                                                            const std::string &filename,
98                                                            const std::string &funcname,
99                                                            unsigned int line,
100                                                            const std::string &msg);
101
102 template <>
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,
107                                                            unsigned int line,
108                                                            const std::string &msg);
109
110 template <>
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,
115                                                                                  unsigned int line,
116                                                                                  const std::string &msg);
117
118 template <>
119 void _assert<char *, const char *>(char * const &value,
120                                                                    const char * const &expected,
121                                                                    const std::string &filename,
122                                                                    const std::string &funcname,
123                                                                    unsigned int line,
124                                                                    const std::string &msg);
125
126 template <>
127 void _assert<const char *, char *>(const char * const &value,
128                                                                    char * const &expected,
129                                                                    const std::string &filename,
130                                                                    const std::string &funcname,
131                                                                    unsigned int line,
132                                                                    const std::string &msg);
133
134 template <>
135 void _assert<char *, char *>(char * const &value,
136                                                          char * const &expected,
137                                                          const std::string &filename,
138                                                          const std::string &funcname,
139                                                          unsigned int line,
140                                                          const std::string &msg);
141
142 template <>
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,
147                                                                                 unsigned int line,
148                                                                                 const std::string &msg);
149
150 template <>
151 void _assert<char *, std::string>(char * const &value,
152                                                                   const std::string &expected,
153                                                                   const std::string &filename,
154                                                                   const std::string &funcname,
155                                                                   unsigned int line,
156                                                                   const std::string &msg);
157
158 void exceptionGuard(const std::function<void()> &);
159
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();
168
169 struct ScopedCstr {
170         char *ptr;
171
172         ScopedCstr() : ptr(nullptr) {}
173         ~ScopedCstr()
174         {
175                 if (ptr)
176                         free(ptr);
177         }
178 };
179
180 template <typename T>
181 class Context {
182 public:
183         Context() : m_context(nullptr)
184         {
185                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
186                                                           << "] isn't specialized for context template");
187         }
188
189         virtual ~Context()
190         {
191                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
192                                                           << "] isn't specialized for context template");
193         }
194
195         T get(void) const
196         {
197                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
198                                                           << "] isn't specialized for context template");
199
200                 return nullptr;
201         }
202
203 private:
204         T m_context;
205 };
206
207 template <>
208 class Context<csr_cs_context_h> {
209 public:
210         Context() : m_context(nullptr)
211         {
212                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
213                 BOOST_REQUIRE(m_context != nullptr);
214         }
215
216         virtual ~Context()
217         {
218                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
219         }
220
221         csr_cs_context_h get(void) const
222         {
223                 return m_context;
224         }
225
226 private:
227         csr_cs_context_h m_context;
228 };
229
230 template <>
231 class Context<csr_wp_context_h> {
232 public:
233         Context() : m_context(nullptr)
234         {
235                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
236                 BOOST_REQUIRE(m_context != nullptr);
237         }
238
239         virtual ~Context()
240         {
241                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
242         }
243
244         csr_wp_context_h get(void) const
245         {
246                 return m_context;
247         }
248
249 private:
250         csr_wp_context_h m_context;
251 };
252
253 template <>
254 class Context<csre_cs_context_h> {
255 public:
256         Context() : m_context(nullptr)
257         {
258                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
259                 BOOST_REQUIRE(m_context != nullptr);
260         }
261
262         virtual ~Context()
263         {
264                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
265         }
266
267         csre_cs_context_h get(void) const
268         {
269                 return m_context;
270         }
271
272 private:
273         csre_cs_context_h m_context;
274 };
275
276 template <>
277 class Context<csre_wp_context_h> {
278 public:
279         Context() : m_context(nullptr)
280         {
281                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
282                 BOOST_REQUIRE(m_context != nullptr);
283         }
284
285         virtual ~Context()
286         {
287                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
288         }
289
290         csre_wp_context_h get(void) const
291         {
292                 return m_context;
293         }
294
295 private:
296         csre_wp_context_h m_context;
297 };
298
299 template <>
300 class Context<csre_cs_engine_h> {
301 public:
302         Context() : m_context(nullptr)
303         {
304                 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
305                 BOOST_REQUIRE(m_context != nullptr);
306         }
307
308         virtual ~Context()
309         {
310                 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
311         }
312
313         csre_cs_engine_h get(void) const
314         {
315                 return m_context;
316         }
317
318 private:
319         csre_cs_engine_h m_context;
320 };
321
322 template <>
323 class Context<csre_wp_engine_h> {
324 public:
325         Context() : m_context(nullptr)
326         {
327                 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
328                 BOOST_REQUIRE(m_context != nullptr);
329         }
330
331         virtual ~Context()
332         {
333                 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
334         }
335
336         csre_wp_engine_h get(void) const
337         {
338                 return m_context;
339         }
340
341 private:
342         csre_wp_engine_h m_context;
343 };
344
345 } // namespace Test