Do not wait for complete event when uninstall app
[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 <functional>
25 #include <typeinfo>
26 #include <string>
27 #include <cstring>
28
29 #include <boost/test/unit_test.hpp>
30
31 #include <csr/content-screening.h>
32 #include <csr/web-protection.h>
33
34 #include <csre/content-screening.h>
35 #include <csre/web-protection.h>
36
37 #include <csre/content-screening-engine-info.h>
38 #include <csre/web-protection-engine-info.h>
39
40 #ifndef __FILENAME__
41 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
42 #endif
43
44 #define ASSERT_IF(value, expected) \
45         Test::_assert(value, expected, __FILENAME__, __func__, __LINE__)
46
47 #define ASSERT_SUCCESS(value) \
48         Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__)
49
50 #define ASSERT_INSTALL_APP(path, type)                       \
51         BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
52                                                   "Failed to install app: " << path)
53
54 #define ASSERT_UNINSTALL_APP(path)                             \
55         BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path),           \
56                                                   "Failed to uninstall app: " << path)
57
58 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
59 #define EXCEPTION_GUARD_END   });
60
61 #define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
62 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
63
64 namespace Test {
65
66 template <typename T, typename U>
67 void _assert(const T &value, const U &expected, const std::string &filename,
68                          const std::string &funcname, unsigned int line)
69 {
70         BOOST_REQUIRE_MESSAGE(value == expected,
71                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
72                                                   " returned[" << value << "] expected[" << expected << "]");
73 }
74
75 template <>
76 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
77                                                                            const csr_error_e &expected,
78                                                                            const std::string &filename,
79                                                                            const std::string &funcname,
80                                                                            unsigned int line);
81
82 template <>
83 void _assert<csr_error_e, int>(const csr_error_e &value,
84                                                            const int &expected,
85                                                            const std::string &filename,
86                                                            const std::string &funcname,
87                                                            unsigned int line);
88
89 template <>
90 void _assert<int, csr_error_e>(const int &value,
91                                                            const csr_error_e &expected,
92                                                            const std::string &filename,
93                                                            const std::string &funcname,
94                                                            unsigned int line);
95
96 void _assert(const char *value, const char *expected, const std::string &filename,
97                          const std::string &funcname, unsigned int line);
98 void _assert(const char *value, const std::string &expected, const std::string &filename,
99                          const std::string &funcname, unsigned int line);
100
101 void exceptionGuard(const std::function<void()> &);
102
103 void copy_file(const char *src_file, const char *dest_file);
104 void touch_file(const char *file);
105 bool is_file_exist(const char *file);
106 bool install_app(const char *app_path, const char *app_type);
107 bool uninstall_app(const char *pkg_id);
108
109 template <typename T>
110 class Context {
111 public:
112         Context() : m_context(nullptr)
113         {
114                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
115                                                           << "] isn't specialized for context template");
116         }
117
118         virtual ~Context()
119         {
120                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
121                                                           << "] isn't specialized for context template");
122         }
123
124         T get(void) const
125         {
126                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
127                                                           << "] isn't specialized for context template");
128
129                 return nullptr;
130         }
131
132 private:
133         T m_context;
134 };
135
136 template <>
137 class Context<csr_cs_context_h> {
138 public:
139         Context() : m_context(nullptr)
140         {
141                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
142                 BOOST_REQUIRE(m_context != nullptr);
143         }
144
145         virtual ~Context()
146         {
147                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
148         }
149
150         csr_cs_context_h get(void) const
151         {
152                 return m_context;
153         }
154
155 private:
156         csr_cs_context_h m_context;
157 };
158
159 template <>
160 class Context<csr_wp_context_h> {
161 public:
162         Context() : m_context(nullptr)
163         {
164                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
165                 BOOST_REQUIRE(m_context != nullptr);
166         }
167
168         virtual ~Context()
169         {
170                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
171         }
172
173         csr_wp_context_h get(void) const
174         {
175                 return m_context;
176         }
177
178 private:
179         csr_wp_context_h m_context;
180 };
181
182 template <>
183 class Context<csre_cs_context_h> {
184 public:
185         Context() : m_context(nullptr)
186         {
187                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
188                 BOOST_REQUIRE(m_context != nullptr);
189         }
190
191         virtual ~Context()
192         {
193                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
194         }
195
196         csre_cs_context_h get(void) const
197         {
198                 return m_context;
199         }
200
201 private:
202         csre_cs_context_h m_context;
203 };
204
205 template <>
206 class Context<csre_wp_context_h> {
207 public:
208         Context() : m_context(nullptr)
209         {
210                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
211                 BOOST_REQUIRE(m_context != nullptr);
212         }
213
214         virtual ~Context()
215         {
216                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
217         }
218
219         csre_wp_context_h get(void) const
220         {
221                 return m_context;
222         }
223
224 private:
225         csre_wp_context_h m_context;
226 };
227
228 template <>
229 class Context<csre_cs_engine_h> {
230 public:
231         Context() : m_context(nullptr)
232         {
233                 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
234                 BOOST_REQUIRE(m_context != nullptr);
235         }
236
237         virtual ~Context()
238         {
239                 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
240         }
241
242         csre_cs_engine_h get(void) const
243         {
244                 return m_context;
245         }
246
247 private:
248         csre_cs_engine_h m_context;
249 };
250
251 template <>
252 class Context<csre_wp_engine_h> {
253 public:
254         Context() : m_context(nullptr)
255         {
256                 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
257                 BOOST_REQUIRE(m_context != nullptr);
258         }
259
260         virtual ~Context()
261         {
262                 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
263         }
264
265         csre_wp_engine_h get(void) const
266         {
267                 return m_context;
268         }
269
270 private:
271         csre_wp_engine_h m_context;
272 };
273
274 } // namespace Test