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 BOOST_REQUIRE_MESSAGE(
359 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
360 "Failed to mkdir[" << dir << "] with errno: " << errno);
363 void make_dir(const char *dir)
366 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
367 "Failed to mkdir[" << dir << "] with errno: " << errno);
370 void touch_file_assert(const char *file)
372 struct utimbuf new_times;
373 time_t now = ::time(nullptr);
375 new_times.actime = now;
376 new_times.modtime = now;
378 BOOST_REQUIRE_MESSAGE(
379 ::utime(file, &new_times) == 0,
380 "utime() to touch file[" << file << "] failed with errno: " << errno);
383 void touch_file(const char *file)
385 struct utimbuf new_times;
386 time_t now = ::time(nullptr);
388 new_times.actime = now;
389 new_times.modtime = now;
392 ::utime(file, &new_times) == 0,
393 "utime() to touch file[" << file << "] failed with errno: " << errno);
396 void remove_file_assert(const char *file)
398 BOOST_REQUIRE_MESSAGE(
400 "unlink file[" << file << " failed with errno: " << errno);
403 void remove_file(const char *file)
407 "unlink file[" << file << " failed with errno: " << errno);
410 bool is_file_exist(const char *file)
412 return ::access(file, F_OK) != -1;
415 bool uninstall_app(const char *pkg_id)
417 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
418 #ifdef PLATFORM_VERSION_3
419 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
420 __quit_loop_on_end_cb, data, ::getuid());
422 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
423 __quit_loop_on_end_cb, data);
428 bool install_app(const char *app_path, const char *pkg_type)
430 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
431 #ifdef PLATFORM_VERSION_3
432 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
433 PM_QUIET, __quit_loop_on_end_cb, data,
436 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
437 __quit_loop_on_end_cb, data);
445 #ifdef PLATFORM_VERSION_3
446 remove_file(RW_DBSPACE ".csr.db");
447 remove_file(RW_DBSPACE ".csr.db-journal");
449 int ret = system("systemctl restart csr.service");
450 BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);