Apply tizen coding style, fixed by astyle tool
[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, file, f, l) \
45         Test::_assert(value, expected, file, f, l)
46
47 #define ASSERT_IF(value, expected) \
48         ASSERT_IF_(value, expected, __FILENAME__, __func__, __LINE__)
49
50 #define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
51 #define EXCEPTION_GUARD_END   });
52
53 #define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
54 #define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)
55
56 namespace Test {
57
58 template <typename T, typename U>
59 void _assert(const T &value, const U &expected, const std::string &filename,
60                          const std::string &funcname, unsigned int line)
61 {
62         BOOST_REQUIRE_MESSAGE(value == expected,
63                                                   "[" << filename << " > " << funcname << " : " << line << "]"
64                                                   << " returned code: " << value
65                                                   << " expected: " << expected);
66 }
67
68 void exceptionGuard(const std::function<void()> &);
69
70 template <typename T>
71 class Context {
72 public:
73         Context() : m_context(nullptr)
74         {
75                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
76                                                           << "] isn't specialized for context template");
77         }
78
79         virtual ~Context()
80         {
81                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
82                                                           << "] isn't specialized for context template");
83         }
84
85         T get(void) const
86         {
87                 BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
88                                                           << "] isn't specialized for context template");
89
90                 return nullptr;
91         }
92
93 private:
94         T m_context;
95 };
96
97 template <>
98 class Context<csr_cs_context_h> {
99 public:
100         Context() : m_context(nullptr)
101         {
102                 ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
103                 BOOST_REQUIRE(m_context != nullptr);
104         }
105
106         virtual ~Context()
107         {
108                 ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
109         }
110
111         csr_cs_context_h get(void) const
112         {
113                 return m_context;
114         }
115
116 private:
117         csr_cs_context_h m_context;
118 };
119
120 template <>
121 class Context<csr_wp_context_h> {
122 public:
123         Context() : m_context(nullptr)
124         {
125                 ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
126                 BOOST_REQUIRE(m_context != nullptr);
127         }
128
129         virtual ~Context()
130         {
131                 ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
132         }
133
134         csr_wp_context_h get(void) const
135         {
136                 return m_context;
137         }
138
139 private:
140         csr_wp_context_h m_context;
141 };
142
143 template <>
144 class Context<csre_cs_context_h> {
145 public:
146         Context() : m_context(nullptr)
147         {
148                 ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
149                 BOOST_REQUIRE(m_context != nullptr);
150         }
151
152         virtual ~Context()
153         {
154                 ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
155         }
156
157         csre_cs_context_h get(void) const
158         {
159                 return m_context;
160         }
161
162 private:
163         csre_cs_context_h m_context;
164 };
165
166 template <>
167 class Context<csre_wp_context_h> {
168 public:
169         Context() : m_context(nullptr)
170         {
171                 ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
172                 BOOST_REQUIRE(m_context != nullptr);
173         }
174
175         virtual ~Context()
176         {
177                 ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
178         }
179
180         csre_wp_context_h get(void) const
181         {
182                 return m_context;
183         }
184
185 private:
186         csre_wp_context_h m_context;
187 };
188
189 template <>
190 class Context<csre_cs_engine_h> {
191 public:
192         Context() : m_context(nullptr)
193         {
194                 ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
195                 BOOST_REQUIRE(m_context != nullptr);
196         }
197
198         virtual ~Context()
199         {
200                 ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
201         }
202
203         csre_cs_engine_h get(void) const
204         {
205                 return m_context;
206         }
207
208 private:
209         csre_cs_engine_h m_context;
210 };
211
212 template <>
213 class Context<csre_wp_engine_h> {
214 public:
215         Context() : m_context(nullptr)
216         {
217                 ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
218                 BOOST_REQUIRE(m_context != nullptr);
219         }
220
221         virtual ~Context()
222         {
223                 ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
224         }
225
226         csre_wp_engine_h get(void) const
227         {
228                 return m_context;
229         }
230
231 private:
232         csre_wp_engine_h m_context;
233 };
234
235 } // namespace Test