summaryrefslogtreecommitdiff
path: root/test/test-common.h
blob: 1a99416d451fbf0f48caee3d212e618cb6e724fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        test-common.h
 * @author      Kyungwook Tak (k.tak@samsung.com)
 * @version     1.0
 * @brief       Common utilities for test
 */
#pragma once

#include <sstream>
#include <iostream>
#include <ios>
#include <functional>
#include <typeinfo>
#include <string>
#include <cstring>
#include <cerrno>
#include <system_error>
#include <unistd.h>

#include <boost/test/unit_test.hpp>

#include <csr-content-screening.h>
#include <csr-web-protection.h>

#include <csre-content-screening.h>
#include <csre-web-protection.h>

#ifndef __FILENAME__
#define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
#endif

#define TOSTRING(ITEMS)                                                         \
	(dynamic_cast<std::ostringstream &>(std::ostringstream().seekp(             \
										0, std::ios_base::cur) << ITEMS)).str()

#define ASSERT_IF_MSG(value, expected, msg) \
	Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, TOSTRING(msg))

#define WARN_IF_MSG(value, expected, msg) \
	Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, TOSTRING(msg))

#define ASSERT_IF(value, expected) \
	Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, "")

#define WARN_IF(value, expected) \
	Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, "")

#define ASSERT_SUCCESS(value) \
	Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, true, "")

#define WARN_SUCCESS(value) \
	Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, false, "")

#define ASSERT_INSTALL_APP(path, type)                       \
	BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
						  "Failed to install app: " << path)

#define ASSERT_UNINSTALL_APP(path)                             \
	BOOST_REQUIRE_MESSAGE(Test::uninstall_app(path),           \
						  "Failed to uninstall app: " << path)

#define EXCEPTION_GUARD_START Test::exceptionGuard([&] {
#define EXCEPTION_GUARD_END   });

#define CHECK_IS_NULL(ptr)     BOOST_REQUIRE(ptr == nullptr)
#define CHECK_IS_NOT_NULL(ptr) BOOST_REQUIRE(ptr != nullptr)

namespace Test {

template <typename T, typename U>
void _assert(const T &value, const U &expected, const std::string &filename,
			 const std::string &funcname, unsigned int line, bool isAssert,
			 const std::string &msg)
{
	if (isAssert)
		BOOST_REQUIRE_MESSAGE(value == expected,
							  "[" << filename << " > " << funcname << " : " << line <<
							  "]" << " returned[" << value << "] expected[" << expected <<
							  "] " << msg);
	else
		BOOST_WARN_MESSAGE(value == expected,
							  "[" << filename << " > " << funcname << " : " << line <<
							  "]" << " returned[" << value << "] expected[" << expected <<
							  "] " << msg);
}

template <>
void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
									   const csr_error_e &expected,
									   const std::string &filename,
									   const std::string &funcname,
									   unsigned int line,
									   bool isAssert,
									   const std::string &msg);

template <>
void _assert<csr_error_e, int>(const csr_error_e &value,
							   const int &expected,
							   const std::string &filename,
							   const std::string &funcname,
							   unsigned int line,
							   bool isAssert,
							   const std::string &msg);

template <>
void _assert<int, csr_error_e>(const int &value,
							   const csr_error_e &expected,
							   const std::string &filename,
							   const std::string &funcname,
							   unsigned int line,
							   bool isAssert,
							   const std::string &msg);

template <>
void _assert<const char *, const char *>(const char * const &value,
										 const char * const &expected,
										 const std::string &filename,
										 const std::string &funcname,
										 unsigned int line,
										 bool isAssert,
										 const std::string &msg);

template <>
void _assert<char *, const char *>(char * const &value,
								   const char * const &expected,
								   const std::string &filename,
								   const std::string &funcname,
								   unsigned int line,
								   bool isAssert,
								   const std::string &msg);

template <>
void _assert<const char *, char *>(const char * const &value,
								   char * const &expected,
								   const std::string &filename,
								   const std::string &funcname,
								   unsigned int line,
								   bool isAssert,
								   const std::string &msg);

template <>
void _assert<char *, char *>(char * const &value,
							 char * const &expected,
							 const std::string &filename,
							 const std::string &funcname,
							 unsigned int line,
							 bool isAssert,
							 const std::string &msg);

template <>
void _assert<const char *, std::string>(const char * const &value,
										const std::string &expected,
										const std::string &filename,
										const std::string &funcname,
										unsigned int line,
										bool isAssert,
										const std::string &msg);

template <>
void _assert<char *, std::string>(char * const &value,
								  const std::string &expected,
								  const std::string &filename,
								  const std::string &funcname,
								  unsigned int line,
								  bool isAssert,
								  const std::string &msg);

void exceptionGuard(const std::function<void()> &);

std::string capi_ec_to_string(csr_error_e ec);
std::string capi_ec_to_string(int ec);

void make_dir(const char *dir);
void make_dir_assert(const char *dir);
void copy_file(const char *src_file, const char *dst_file);
void copy_file_assert(const char *src_file, const char *dst_file);
void touch_file(const char *file);
void touch_file_assert(const char *file);
void remove_file(const char *file);
void remove_file_assert(const char *file);
bool is_file_exist(const char *file);
bool install_app(const char *app_path, const char *app_type);
bool uninstall_app(const char *pkg_id);
void initialize_db();

struct ScopedCstr {
	char *ptr;

	ScopedCstr() : ptr(nullptr) {}
	~ScopedCstr()
	{
		if (ptr)
			free(ptr);
	}
};

class ScopedChDir {
public:
	ScopedChDir(const std::string &dirpath)
	{
		if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
			throw std::system_error(errno, std::system_category(), "getcwd failed");

		if (::chdir(dirpath.c_str()) == -1)
			throw std::system_error(errno, std::system_category(),
									dirpath + " chdir failed");
	}

	~ScopedChDir()
	{
		if (::chdir(cdbuf) == -1)
			BOOST_MESSAGE("chdir failed but ignore");
	}

private:
	char cdbuf[PATH_MAX + 1];
};

template <typename T>
class Context {
public:
	Context() : m_context(nullptr)
	{
		BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
							  << "] isn't specialized for context template");
	}

	virtual ~Context()
	{
		BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
							  << "] isn't specialized for context template");
	}

	T get(void) const
	{
		BOOST_REQUIRE_MESSAGE(0, "Type[" << typeid(T).name()
							  << "] isn't specialized for context template");

		return nullptr;
	}

private:
	T m_context;
};

template <>
class Context<csr_cs_context_h> {
public:
	Context() : m_context(nullptr)
	{
		ASSERT_SUCCESS(csr_cs_context_create(&m_context));
		BOOST_REQUIRE(m_context != nullptr);
	}

	virtual ~Context()
	{
		ASSERT_SUCCESS(csr_cs_context_destroy(m_context));
	}

	csr_cs_context_h get(void) const
	{
		return m_context;
	}

private:
	csr_cs_context_h m_context;
};

template <>
class Context<csr_wp_context_h> {
public:
	Context() : m_context(nullptr)
	{
		ASSERT_SUCCESS(csr_wp_context_create(&m_context));
		BOOST_REQUIRE(m_context != nullptr);
	}

	virtual ~Context()
	{
		ASSERT_SUCCESS(csr_wp_context_destroy(m_context));
	}

	csr_wp_context_h get(void) const
	{
		return m_context;
	}

private:
	csr_wp_context_h m_context;
};

template <>
class Context<csre_cs_context_h> {
public:
	Context() : m_context(nullptr)
	{
		ASSERT_SUCCESS(csre_cs_context_create(&m_context));
		BOOST_REQUIRE(m_context != nullptr);
	}

	virtual ~Context()
	{
		ASSERT_SUCCESS(csre_cs_context_destroy(m_context));
	}

	csre_cs_context_h get(void) const
	{
		return m_context;
	}

private:
	csre_cs_context_h m_context;
};

template <>
class Context<csre_wp_context_h> {
public:
	Context() : m_context(nullptr)
	{
		ASSERT_SUCCESS(csre_wp_context_create(&m_context));
		BOOST_REQUIRE(m_context != nullptr);
	}

	virtual ~Context()
	{
		ASSERT_SUCCESS(csre_wp_context_destroy(m_context));
	}

	csre_wp_context_h get(void) const
	{
		return m_context;
	}

private:
	csre_wp_context_h m_context;
};

} // namespace Test