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