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)
91 auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
92 data.loop = g_main_loop_new(nullptr, false);
93 g_main_loop_run(data.loop);
94 BOOST_REQUIRE_MESSAGE(g_source_remove(id),
95 "Failed to remove timeout event source from main loop.");
96 g_main_loop_unref(data.loop);
97 pkgmgr_client_free(pkgmgr);
99 return data.isSuccess;
102 #define ERRORDESCRIBE(name) case name: return #name
103 std::string capi_ec_to_string(csr_error_e ec)
106 ERRORDESCRIBE(CSR_ERROR_NONE);
107 ERRORDESCRIBE(CSR_ERROR_INVALID_PARAMETER);
108 ERRORDESCRIBE(CSR_ERROR_OUT_OF_MEMORY);
109 ERRORDESCRIBE(CSR_ERROR_PERMISSION_DENIED);
110 ERRORDESCRIBE(CSR_ERROR_SOCKET);
111 ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
112 ERRORDESCRIBE(CSR_ERROR_SERVER);
113 ERRORDESCRIBE(CSR_ERROR_NO_TASK);
114 ERRORDESCRIBE(CSR_ERROR_DB);
115 ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
116 ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
117 ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
118 ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
119 ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
120 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
121 ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
122 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
123 ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
124 ERRORDESCRIBE(CSR_ERROR_SYSTEM);
125 default: return std::string("Undefined capi error code: ")
126 + std::to_string(static_cast<int>(ec));
131 } // namespace anonymous
134 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
135 const csr_error_e &expected,
136 const std::string &filename,
137 const std::string &funcname,
139 const std::string &msg)
141 BOOST_REQUIRE_MESSAGE(value == expected,
142 "[" << filename << " > " << funcname << " : " << line << "]" <<
143 " returned[" << capi_ec_to_string(value) <<
144 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
148 void _assert<csr_error_e, int>(const csr_error_e &value,
150 const std::string &filename,
151 const std::string &funcname,
153 const std::string &msg)
155 BOOST_REQUIRE_MESSAGE(value == expected,
156 "[" << filename << " > " << funcname << " : " << line << "]" <<
157 " returned[" << capi_ec_to_string(value) << "] expected[" <<
158 capi_ec_to_string(static_cast<csr_error_e>(expected)) << "]" <<
163 void _assert<int, csr_error_e>(const int &value,
164 const csr_error_e &expected,
165 const std::string &filename,
166 const std::string &funcname,
168 const std::string &msg)
170 BOOST_REQUIRE_MESSAGE(value == expected,
171 "[" << filename << " > " << funcname << " : " << line << "]" <<
173 capi_ec_to_string(static_cast<csr_error_e>(value)) <<
174 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
178 void _assert<const char *, const char *>(const char * const &value,
179 const char * const &expected,
180 const std::string &filename,
181 const std::string &funcname,
183 const std::string &msg)
185 if (value == nullptr && expected == nullptr)
187 else if (value != nullptr && expected != nullptr)
188 _assert<std::string, const char *>(std::string(value), expected, filename,
189 funcname, line, msg);
190 else if (value == nullptr && expected != nullptr)
191 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
192 "[" << filename << " > " << funcname << " : " << line <<
193 "] returned[nullptr] expected[" << expected << "] " << msg);
195 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
196 "[" << filename << " > " << funcname << " : " << line <<
197 "] returned[" << value << "] expected[nullptr] " << msg);
201 void _assert<char *, const char *>(char * const &value,
202 const char * const &expected,
203 const std::string &filename,
204 const std::string &funcname,
206 const std::string &msg)
208 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
212 void _assert<const char *, char *>(const char * const &value,
213 char * const &expected,
214 const std::string &filename,
215 const std::string &funcname,
217 const std::string &msg)
219 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
223 void _assert<char *, char *>(char * const &value,
224 char * const &expected,
225 const std::string &filename,
226 const std::string &funcname,
228 const std::string &msg)
230 _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
234 void _assert<const char *, std::string>(const char * const &value,
235 const std::string &expected,
236 const std::string &filename,
237 const std::string &funcname,
239 const std::string &msg)
241 _assert<std::string, std::string>(
242 (value == nullptr) ? std::string() : std::string(value),
243 expected, filename, funcname, line, msg);
247 void _assert<char *, std::string>(char * const &value,
248 const std::string &expected,
249 const std::string &filename,
250 const std::string &funcname,
252 const std::string &msg)
254 _assert<const char *, std::string>(value, expected, filename, funcname, line, msg);
257 void exceptionGuard(const std::function<void()> &f)
261 } catch (const std::exception &e) {
262 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
264 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
268 void copy_file(const char *src_file, const char *dest_file)
270 std::ifstream srce(src_file, std::ios::binary);
271 std::ofstream dest(dest_file, std::ios::binary);
272 dest << srce.rdbuf();
275 void make_dir(const char *dir)
277 mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
280 void touch_file(const char *file)
282 struct utimbuf new_times;
283 time_t now = time(nullptr);
285 new_times.actime = now;
286 new_times.modtime = now;
288 utime(file, &new_times);
291 void remove_file(const char *file)
296 bool is_file_exist(const char *file)
298 return (access(file, F_OK) != -1);
301 bool uninstall_app(const char *pkg_id)
303 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
304 return pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
305 __quit_loop_on_end_cb, data);
309 bool install_app(const char *app_path, const char *pkg_type)
311 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
312 return pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
313 __quit_loop_on_end_cb, data);
319 #ifdef PLATFORM_VERSION3
320 remove_file(RW_DBSPACE ".csr.db");
321 remove_file(RW_DBSPACE ".csr.db-journal");
323 int ret = system("systemctl restart csr.service");
324 BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);