2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file test-common.cpp
18 * @author Kyungwook Tak (k.tak@samsung.com)
20 * @brief Common utilities for test
22 #include "test-common.h"
30 #include <sys/types.h>
35 #include <package-manager.h>
45 PkgEventData() : isSuccess(false), loop(nullptr) {}
48 #ifdef PLATFORM_VERSION_3
49 int __quit_loop_on_end_cb(uid_t, int req_id, const char *pkg_type, const char *pkgid,
50 const char *key, const char *val, const void *pmsg, void *data)
52 int __quit_loop_on_end_cb(int req_id, const char *pkg_type, const char *pkgid,
53 const char *key, const char *val, const void *pmsg, void *data)
61 auto eventData = reinterpret_cast<PkgEventData *>(data);
63 if (key && strncmp(key, "end", strlen("end")) == 0) {
64 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
66 g_main_loop_quit(eventData->loop);
72 gboolean __quit_loop_on_timeout_cb(gpointer data)
74 auto eventData = reinterpret_cast<PkgEventData *>(data);
76 eventData->isSuccess = false;
77 g_main_loop_quit(eventData->loop);
82 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
84 auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
85 CHECK_IS_NOT_NULL(pkgmgr);
88 auto ret = request(pkgmgr, &data);
89 if (ret <= PKGMGR_R_OK) {
90 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
94 auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
95 data.loop = g_main_loop_new(nullptr, false);
96 g_main_loop_run(data.loop);
97 BOOST_WARN_MESSAGE(g_source_remove(id),
98 "Failed to remove timeout event source from main loop.");
99 g_main_loop_unref(data.loop);
100 pkgmgr_client_free(pkgmgr);
102 return data.isSuccess;
105 } // namespace anonymous
107 #define ERRORDESCRIBE(name) case name: return #name
108 std::string capi_ec_to_string(csr_error_e ec)
111 ERRORDESCRIBE(CSR_ERROR_NONE);
112 ERRORDESCRIBE(CSR_ERROR_INVALID_PARAMETER);
113 ERRORDESCRIBE(CSR_ERROR_OUT_OF_MEMORY);
114 ERRORDESCRIBE(CSR_ERROR_PERMISSION_DENIED);
115 ERRORDESCRIBE(CSR_ERROR_NOT_SUPPORTED);
116 ERRORDESCRIBE(CSR_ERROR_BUSY);
117 ERRORDESCRIBE(CSR_ERROR_SOCKET);
118 ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
119 ERRORDESCRIBE(CSR_ERROR_SERVER);
120 ERRORDESCRIBE(CSR_ERROR_NO_TASK);
121 ERRORDESCRIBE(CSR_ERROR_DB);
122 ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
123 ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
124 ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
125 ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
126 ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
127 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
128 ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
129 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
130 ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
131 ERRORDESCRIBE(CSR_ERROR_SYSTEM);
132 default: return std::string("Undefined capi error code: ")
133 + std::to_string(static_cast<int>(ec));
138 std::string capi_ec_to_string(int ec)
140 return capi_ec_to_string(static_cast<csr_error_e>(ec));
144 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
145 const csr_error_e &expected,
146 const std::string &filename,
147 const std::string &funcname,
150 const std::string &msg)
153 BOOST_REQUIRE_MESSAGE(value == expected,
154 "[" << filename << " > " << funcname << " : " << line <<
155 "]" << " returned[" << capi_ec_to_string(value) <<
156 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
158 BOOST_WARN_MESSAGE(value == expected,
159 "[" << filename << " > " << funcname << " : " << line <<
160 "] returned[" << capi_ec_to_string(value) <<
161 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
165 void _assert<csr_error_e, int>(const csr_error_e &value,
167 const std::string &filename,
168 const std::string &funcname,
171 const std::string &msg)
174 BOOST_REQUIRE_MESSAGE(value == expected,
175 "[" << filename << " > " << funcname << " : " << line <<
176 "] returned[" << capi_ec_to_string(value) << "] expected[" <<
177 capi_ec_to_string(expected) << "] " << msg);
179 BOOST_WARN_MESSAGE(value == expected,
180 "[" << filename << " > " << funcname << " : " << line <<
181 "] returned[" << capi_ec_to_string(value) << "] expected[" <<
182 capi_ec_to_string(expected) << "] " << msg);
186 void _assert<int, csr_error_e>(const int &value,
187 const csr_error_e &expected,
188 const std::string &filename,
189 const std::string &funcname,
192 const std::string &msg)
195 BOOST_REQUIRE_MESSAGE(value == expected,
196 "[" << filename << " > " << funcname << " : " << line <<
197 "] returned[" << capi_ec_to_string(value) <<
198 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
199 BOOST_WARN_MESSAGE(value == expected,
200 "[" << filename << " > " << funcname << " : " << line <<
201 "] returned[" << capi_ec_to_string(value) <<
202 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
206 void _assert<const char *, const char *>(const char * const &value,
207 const char * const &expected,
208 const std::string &filename,
209 const std::string &funcname,
212 const std::string &msg)
214 if (value == nullptr && expected == nullptr) {
219 } else if (value != nullptr && expected != nullptr) {
220 _assert<std::string, const char *>(std::string(value), expected, filename,
221 funcname, line, isAssert, msg);
222 } else if (value == nullptr && expected != nullptr) {
224 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
225 "[" << filename << " > " << funcname << " : " << line <<
226 "] returned[nullptr] expected[" << expected << "] " << msg);
228 BOOST_WARN_MESSAGE(std::string(expected).empty(),
229 "[" << filename << " > " << funcname << " : " << line <<
230 "] returned[nullptr] expected[" << expected << "] " << msg);
233 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
234 "[" << filename << " > " << funcname << " : " << line <<
235 "] returned[" << value << "] expected[nullptr] " << msg);
237 BOOST_WARN_MESSAGE(std::string(value).empty(),
238 "[" << filename << " > " << funcname << " : " << line <<
239 "] returned[" << value << "] expected[nullptr] " << msg);
244 void _assert<char *, const char *>(char * const &value,
245 const char * const &expected,
246 const std::string &filename,
247 const std::string &funcname,
250 const std::string &msg)
252 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
256 void _assert<const char *, char *>(const char * const &value,
257 char * const &expected,
258 const std::string &filename,
259 const std::string &funcname,
262 const std::string &msg)
264 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
268 void _assert<char *, char *>(char * const &value,
269 char * const &expected,
270 const std::string &filename,
271 const std::string &funcname,
274 const std::string &msg)
276 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
280 void _assert<const char *, std::string>(const char * const &value,
281 const std::string &expected,
282 const std::string &filename,
283 const std::string &funcname,
286 const std::string &msg)
288 _assert<std::string, std::string>(
289 (value == nullptr) ? std::string() : std::string(value),
290 expected, filename, funcname, line, isAssert, msg);
294 void _assert<char *, std::string>(char * const &value,
295 const std::string &expected,
296 const std::string &filename,
297 const std::string &funcname,
300 const std::string &msg)
302 _assert<const char *, std::string>(value, expected, filename, funcname, line, isAssert, msg);
305 void exceptionGuard(const std::function<void()> &f)
309 } catch (const std::exception &e) {
310 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
312 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
316 void copy_file_assert(const char *src_file, const char *dst_file)
322 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
323 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
325 src.open(src_file, std::ios::binary);
326 dst.open(dst_file, std::ios::binary);
329 } catch (const std::exception &e) {
330 BOOST_REQUIRE_MESSAGE(false,
331 "Failed to copy file from src[" << src_file <<
332 "] to dst[" << dst_file << "]: " << e.what());
336 void copy_file(const char *src_file, const char *dst_file)
342 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
343 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
345 src.open(src_file, std::ios::binary);
346 dst.open(dst_file, std::ios::binary);
349 } catch (const std::exception &e) {
350 BOOST_WARN_MESSAGE(false,
351 "Failed to copy file from src[" << src_file <<
352 "] to dst[" << dst_file << "]: " << e.what());
356 void make_dir_assert(const char *dir)
358 if (!std::ifstream(dir))
359 BOOST_REQUIRE_MESSAGE(
360 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
361 "Failed to mkdir[" << dir << "] with errno: " << errno);
364 void make_dir(const char *dir)
366 if (!std::ifstream(dir))
368 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
369 "Failed to mkdir[" << dir << "] with errno: " << errno);
372 void touch_file_assert(const char *file)
374 struct utimbuf new_times;
375 time_t now = ::time(nullptr);
377 new_times.actime = now;
378 new_times.modtime = now;
380 BOOST_REQUIRE_MESSAGE(
381 ::utime(file, &new_times) == 0,
382 "utime() to touch file[" << file << "] failed with errno: " << errno);
385 void touch_file(const char *file)
387 struct utimbuf new_times;
388 time_t now = ::time(nullptr);
390 new_times.actime = now;
391 new_times.modtime = now;
394 ::utime(file, &new_times) == 0,
395 "utime() to touch file[" << file << "] failed with errno: " << errno);
398 void remove_file_assert(const char *file)
400 BOOST_REQUIRE_MESSAGE(
402 "unlink file[" << file << " failed with errno: " << errno);
405 void remove_file(const char *file)
409 "unlink file[" << file << " failed with errno: " << errno);
412 bool is_file_exist(const char *file)
414 return ::access(file, F_OK) != -1;
417 bool uninstall_app(const char *pkg_id)
419 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
420 #ifdef PLATFORM_VERSION_3
421 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
422 __quit_loop_on_end_cb, data, ::getuid());
424 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
425 __quit_loop_on_end_cb, data);
430 bool install_app(const char *app_path, const char *pkg_type)
432 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
433 #ifdef PLATFORM_VERSION_3
434 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
435 PM_QUIET, __quit_loop_on_end_cb, data,
438 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
439 __quit_loop_on_end_cb, data);
447 #ifdef PLATFORM_VERSION_3
448 remove_file(RW_DBSPACE ".csr.db");
449 remove_file(RW_DBSPACE ".csr.db-journal");
451 int ret = system("systemctl restart csr.service");
452 BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);