Fix test-resource and permissions on platform v3.0
[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 std::string capi_ec_to_string(csr_error_e ec);
186 std::string capi_ec_to_string(int ec);
187
188 void make_dir(const char *dir);
189 void make_dir_assert(const char *dir);
190 void copy_file(const char *src_file, const char *dst_file);
191 void copy_file_assert(const char *src_file, const char *dst_file);
192 void touch_file(const char *file);
193 void touch_file_assert(const char *file);
194 void remove_file(const char *file);
195 void remove_file_assert(const char *file);
196 bool is_file_exist(const char *file);
197 bool install_app(const char *app_path, const char *app_type);
198 bool uninstall_app(const char *pkg_id);
199 void initialize_db();
200
201 struct ScopedCstr {
202         char *ptr;
203
204         ScopedCstr() : ptr(nullptr) {}
205         ~ScopedCstr()
206         {
207                 if (ptr)
208                         free(ptr);
209         }
210 };
211
212 class ScopedChDir {
213 public:
214         ScopedChDir(const std::string &dirpath)
215         {
216                 if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
217                         throw std::system_error(errno, std::system_category(), "getcwd failed");
218
219                 if (::chdir(dirpath.c_str()) == -1)
220                         throw std::system_error(errno, std::system_category(),
221                                                                         dirpath + " chdir failed");
222         }
223
224         ~ScopedChDir()
225         {
226                 if (::chdir(cdbuf) == -1)
227                         throw std::system_error(errno, std::system_category(),
228                                                                         std::string(cdbuf) + " chdir failed");
229         }
230
231 private:
232         char cdbuf[PATH_MAX + 1];
233 };
234
235 template <typename T>
236 class Context {
237 public:
238         Context() : m_context(nullptr)
239         {
240                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
241                                                           << "] isn't specialized for context template");
242         }
243
244         virtual ~Context()
245         {
246                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
247                                                           << "] isn't specialized for context template");
248         }
249
250         T get(void) const
251         {
252                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
253                                                           << "] isn't specialized for context template");
254
255                 return nullptr;
256         }
257
258 private:
259         T m_context;
260 };
261
262 template <>
263 class Context<csr_cs_context_h> {
264 public:
265         Context() : m_context(nullptr)
266         {
267                 ASSERT_SUCCESS(csr_cs_context_create(&m_context));
268                 BOOST_REQUIRE(m_context != nullptr);
269         }
270
271         virtual ~Context()
272         {
273                 ASSERT_SUCCESS(csr_cs_context_destroy(m_context));
274         }
275
276         csr_cs_context_h get(void) const
277         {
278                 return m_context;
279         }
280
281 private:
282         csr_cs_context_h m_context;
283 };
284
285 template <>
286 class Context<csr_wp_context_h> {
287 public:
288         Context() : m_context(nullptr)
289         {
290                 ASSERT_SUCCESS(csr_wp_context_create(&m_context));
291                 BOOST_REQUIRE(m_context != nullptr);
292         }
293
294         virtual ~Context()
295         {
296                 ASSERT_SUCCESS(csr_wp_context_destroy(m_context));
297         }
298
299         csr_wp_context_h get(void) const
300         {
301                 return m_context;
302         }
303
304 private:
305         csr_wp_context_h m_context;
306 };
307
308 template <>
309 class Context<csre_cs_context_h> {
310 public:
311         Context() : m_context(nullptr)
312         {
313                 ASSERT_SUCCESS(csre_cs_context_create(&m_context));
314                 BOOST_REQUIRE(m_context != nullptr);
315         }
316
317         virtual ~Context()
318         {
319                 ASSERT_SUCCESS(csre_cs_context_destroy(m_context));
320         }
321
322         csre_cs_context_h get(void) const
323         {
324                 return m_context;
325         }
326
327 private:
328         csre_cs_context_h m_context;
329 };
330
331 template <>
332 class Context<csre_wp_context_h> {
333 public:
334         Context() : m_context(nullptr)
335         {
336                 ASSERT_SUCCESS(csre_wp_context_create(&m_context));
337                 BOOST_REQUIRE(m_context != nullptr);
338         }
339
340         virtual ~Context()
341         {
342                 ASSERT_SUCCESS(csre_wp_context_destroy(m_context));
343         }
344
345         csre_wp_context_h get(void) const
346         {
347                 return m_context;
348         }
349
350 private:
351         csre_wp_context_h m_context;
352 };
353
354 } // namespace Test