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