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"
29 #include <sys/types.h>
34 #include <package-manager.h>
44 PkgEventData() : isSuccess(false), loop(nullptr) {}
47 #ifdef PLATFORM_VERSION_3
48 int __quit_loop_on_end_cb(uid_t, int req_id, const char *pkg_type, const char *pkgid,
49 const char *key, const char *val, const void *pmsg, void *data)
51 int __quit_loop_on_end_cb(int req_id, const char *pkg_type, const char *pkgid,
52 const char *key, const char *val, const void *pmsg, void *data)
60 auto eventData = reinterpret_cast<PkgEventData *>(data);
62 if (key && strncmp(key, "end", strlen("end")) == 0) {
63 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
65 g_main_loop_quit(eventData->loop);
71 gboolean __quit_loop_on_timeout_cb(gpointer data)
73 auto eventData = reinterpret_cast<PkgEventData *>(data);
75 eventData->isSuccess = false;
76 g_main_loop_quit(eventData->loop);
81 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
83 auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
84 CHECK_IS_NOT_NULL(pkgmgr);
87 auto ret = request(pkgmgr, &data);
88 if (ret <= PKGMGR_R_OK) {
89 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
93 auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
94 data.loop = g_main_loop_new(nullptr, false);
95 g_main_loop_run(data.loop);
96 BOOST_WARN_MESSAGE(g_source_remove(id),
97 "Failed to remove timeout event source from main loop.");
98 g_main_loop_unref(data.loop);
99 pkgmgr_client_free(pkgmgr);
101 return data.isSuccess;
104 #define ERRORDESCRIBE(name) case name: return #name
105 std::string capi_ec_to_string(csr_error_e ec)
108 ERRORDESCRIBE(CSR_ERROR_NONE);
109 ERRORDESCRIBE(CSR_ERROR_INVALID_PARAMETER);
110 ERRORDESCRIBE(CSR_ERROR_OUT_OF_MEMORY);
111 ERRORDESCRIBE(CSR_ERROR_PERMISSION_DENIED);
112 ERRORDESCRIBE(CSR_ERROR_NOT_SUPPORTED);
113 ERRORDESCRIBE(CSR_ERROR_BUSY);
114 ERRORDESCRIBE(CSR_ERROR_SOCKET);
115 ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
116 ERRORDESCRIBE(CSR_ERROR_SERVER);
117 ERRORDESCRIBE(CSR_ERROR_NO_TASK);
118 ERRORDESCRIBE(CSR_ERROR_DB);
119 ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
120 ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
121 ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
122 ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
123 ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
124 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
125 ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
126 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
127 ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
128 ERRORDESCRIBE(CSR_ERROR_SYSTEM);
129 default: return std::string("Undefined capi error code: ")
130 + std::to_string(static_cast<int>(ec));
135 } // namespace anonymous
138 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
139 const csr_error_e &expected,
140 const std::string &filename,
141 const std::string &funcname,
143 const std::string &msg)
145 BOOST_REQUIRE_MESSAGE(value == expected,
146 "[" << filename << " > " << funcname << " : " << line << "]" <<
147 " returned[" << capi_ec_to_string(value) <<
148 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
152 void _assert<csr_error_e, int>(const csr_error_e &value,
154 const std::string &filename,
155 const std::string &funcname,
157 const std::string &msg)
159 BOOST_REQUIRE_MESSAGE(value == expected,
160 "[" << filename << " > " << funcname << " : " << line << "]" <<
161 " returned[" << capi_ec_to_string(value) << "] expected[" <<
162 capi_ec_to_string(static_cast<csr_error_e>(expected)) << "]" <<
167 void _assert<int, csr_error_e>(const int &value,
168 const csr_error_e &expected,
169 const std::string &filename,
170 const std::string &funcname,
172 const std::string &msg)
174 BOOST_REQUIRE_MESSAGE(value == expected,
175 "[" << filename << " > " << funcname << " : " << line << "]" <<
177 capi_ec_to_string(static_cast<csr_error_e>(value)) <<
178 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
182 void _assert<const char *, const char *>(const char * const &value,
183 const char * const &expected,
184 const std::string &filename,
185 const std::string &funcname,
187 const std::string &msg)
189 if (value == nullptr && expected == nullptr)
191 else if (value != nullptr && expected != nullptr)
192 _assert<std::string, const char *>(std::string(value), expected, filename,
193 funcname, line, msg);
194 else if (value == nullptr && expected != nullptr)
195 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
196 "[" << filename << " > " << funcname << " : " << line <<
197 "] returned[nullptr] expected[" << expected << "] " << msg);
199 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
200 "[" << filename << " > " << funcname << " : " << line <<
201 "] returned[" << value << "] expected[nullptr] " << msg);
205 void _assert<char *, const char *>(char * const &value,
206 const char * const &expected,
207 const std::string &filename,
208 const std::string &funcname,
210 const std::string &msg)
212 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
216 void _assert<const char *, char *>(const char * const &value,
217 char * const &expected,
218 const std::string &filename,
219 const std::string &funcname,
221 const std::string &msg)
223 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
227 void _assert<char *, char *>(char * const &value,
228 char * const &expected,
229 const std::string &filename,
230 const std::string &funcname,
232 const std::string &msg)
234 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
238 void _assert<const char *, std::string>(const char * const &value,
239 const std::string &expected,
240 const std::string &filename,
241 const std::string &funcname,
243 const std::string &msg)
245 _assert<std::string, std::string>(
246 (value == nullptr) ? std::string() : std::string(value),
247 expected, filename, funcname, line, msg);
251 void _assert<char *, std::string>(char * const &value,
252 const std::string &expected,
253 const std::string &filename,
254 const std::string &funcname,
256 const std::string &msg)
258 _assert<const char *, std::string>(value, expected, filename, funcname, line, msg);
261 void exceptionGuard(const std::function<void()> &f)
265 } catch (const std::exception &e) {
266 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
268 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
272 void copy_file(const char *src_file, const char *dest_file)
274 std::ifstream srce(src_file, std::ios::binary);
275 std::ofstream dest(dest_file, std::ios::binary);
276 dest << srce.rdbuf();
279 void make_dir(const char *dir)
281 mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
284 void touch_file(const char *file)
286 struct utimbuf new_times;
287 time_t now = time(nullptr);
289 new_times.actime = now;
290 new_times.modtime = now;
292 utime(file, &new_times);
295 void remove_file(const char *file)
300 bool is_file_exist(const char *file)
302 return (access(file, F_OK) != -1);
305 bool uninstall_app(const char *pkg_id)
307 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
308 #ifdef PLATFORM_VERSION_3
309 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
310 __quit_loop_on_end_cb, data, ::getuid());
312 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
313 __quit_loop_on_end_cb, data);
318 bool install_app(const char *app_path, const char *pkg_type)
320 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
321 #ifdef PLATFORM_VERSION_3
322 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
323 PM_QUIET, __quit_loop_on_end_cb, data,
326 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
327 __quit_loop_on_end_cb, data);
334 #ifdef PLATFORM_VERSION_3
335 remove_file(RW_DBSPACE ".csr.db");
336 remove_file(RW_DBSPACE ".csr.db-journal");
338 int ret = system("systemctl restart csr.service");
339 BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);