Fix failed test cases and make test common lib
[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 bool is_app_exist(const char *pkg_id);
109
110 template <typename T>
111 class Context {
112 public:
113         Context() : m_context(nullptr)
114         {
115                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
116                                                           << "] isn't specialized for context template");
117         }
118
119         virtual ~Context()
120         {
121                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
122                                                           << "] isn't specialized for context template");
123         }
124
125         T get(void) const
126         {
127                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
128                                                           << "] isn't specialized for context template");
129
130                 return nullptr;
131         }
132
133 private:
134         T m_context;
135 };
136
137 template <>
138 class Context<csr_cs_context_h> {
139 public:
140         Context() : m_context(nullptr)
141         {
142                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
143                 BOOST_REQUIRE(m_context != nullptr);
144         }
145
146         virtual ~Context()
147         {
148                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
149         }
150
151         csr_cs_context_h get(void) const
152         {
153                 return m_context;
154         }
155
156 private:
157         csr_cs_context_h m_context;
158 };
159
160 template <>
161 class Context<csr_wp_context_h> {
162 public:
163         Context() : m_context(nullptr)
164         {
165                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
166                 BOOST_REQUIRE(m_context != nullptr);
167         }
168
169         virtual ~Context()
170         {
171                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
172         }
173
174         csr_wp_context_h get(void) const
175         {
176                 return m_context;
177         }
178
179 private:
180         csr_wp_context_h m_context;
181 };
182
183 template <>
184 class Context<csre_cs_context_h> {
185 public:
186         Context() : m_context(nullptr)
187         {
188                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
189                 BOOST_REQUIRE(m_context != nullptr);
190         }
191
192         virtual ~Context()
193         {
194                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
195         }
196
197         csre_cs_context_h get(void) const
198         {
199                 return m_context;
200         }
201
202 private:
203         csre_cs_context_h m_context;
204 };
205
206 template <>
207 class Context<csre_wp_context_h> {
208 public:
209         Context() : m_context(nullptr)
210         {
211                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
212                 BOOST_REQUIRE(m_context != nullptr);
213         }
214
215         virtual ~Context()
216         {
217                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
218         }
219
220         csre_wp_context_h get(void) const
221         {
222                 return m_context;
223         }
224
225 private:
226         csre_wp_context_h m_context;
227 };
228
229 template <>
230 class Context<csre_cs_engine_h> {
231 public:
232         Context() : m_context(nullptr)
233         {
234                 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
235                 BOOST_REQUIRE(m_context != nullptr);
236         }
237
238         virtual ~Context()
239         {
240                 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
241         }
242
243         csre_cs_engine_h get(void) const
244         {
245                 return m_context;
246         }
247
248 private:
249         csre_cs_engine_h m_context;
250 };
251
252 template <>
253 class Context<csre_wp_engine_h> {
254 public:
255         Context() : m_context(nullptr)
256         {
257                 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
258                 BOOST_REQUIRE(m_context != nullptr);
259         }
260
261         virtual ~Context()
262         {
263                 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
264         }
265
266         csre_wp_engine_h get(void) const
267         {
268                 return m_context;
269         }
270
271 private:
272         csre_wp_engine_h m_context;
273 };
274
275 } // namespace Test