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 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)
56 auto eventData = reinterpret_cast<PkgEventData *>(data);
58 if (key && strncmp(key, "end", strlen("end")) == 0) {
59 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
61 g_main_loop_quit(eventData->loop);
67 gboolean __quit_loop_on_timeout_cb(gpointer data)
69 auto eventData = reinterpret_cast<PkgEventData *>(data);
71 eventData->isSuccess = false;
72 g_main_loop_quit(eventData->loop);
77 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
79 auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
80 CHECK_IS_NOT_NULL(pkgmgr);
83 auto ret = request(pkgmgr, &data);
84 if (ret <= PKGMGR_R_OK) {
85 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
89 auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
90 data.loop = g_main_loop_new(nullptr, false);
91 g_main_loop_run(data.loop);
92 BOOST_WARN_MESSAGE(g_source_remove(id),
93 "Failed to remove timeout event source from main loop.");
94 g_main_loop_unref(data.loop);
95 pkgmgr_client_free(pkgmgr);
97 return data.isSuccess;
100 } // namespace anonymous
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_NOT_SUPPORTED);
111 ERRORDESCRIBE(CSR_ERROR_BUSY);
112 ERRORDESCRIBE(CSR_ERROR_SOCKET);
113 ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
114 ERRORDESCRIBE(CSR_ERROR_SERVER);
115 ERRORDESCRIBE(CSR_ERROR_NO_TASK);
116 ERRORDESCRIBE(CSR_ERROR_DB);
117 ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
118 ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
119 ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
120 ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
121 ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
122 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
123 ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
124 ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
125 ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
126 ERRORDESCRIBE(CSR_ERROR_SYSTEM);
127 default: return std::string("Undefined capi error code: ")
128 + std::to_string(static_cast<int>(ec));
133 std::string capi_ec_to_string(int ec)
135 return capi_ec_to_string(static_cast<csr_error_e>(ec));
139 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
140 const csr_error_e &expected,
141 const std::string &filename,
142 const std::string &funcname,
145 const std::string &msg)
148 BOOST_REQUIRE_MESSAGE(value == expected,
149 "[" << filename << " > " << funcname << " : " << line <<
150 "]" << " returned[" << capi_ec_to_string(value) <<
151 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
153 BOOST_WARN_MESSAGE(value == expected,
154 "[" << filename << " > " << funcname << " : " << line <<
155 "] returned[" << capi_ec_to_string(value) <<
156 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
160 void _assert<csr_error_e, int>(const csr_error_e &value,
162 const std::string &filename,
163 const std::string &funcname,
166 const std::string &msg)
169 BOOST_REQUIRE_MESSAGE(value == expected,
170 "[" << filename << " > " << funcname << " : " << line <<
171 "] returned[" << capi_ec_to_string(value) << "] expected[" <<
172 capi_ec_to_string(expected) << "] " << msg);
174 BOOST_WARN_MESSAGE(value == expected,
175 "[" << filename << " > " << funcname << " : " << line <<
176 "] returned[" << capi_ec_to_string(value) << "] expected[" <<
177 capi_ec_to_string(expected) << "] " << msg);
181 void _assert<int, csr_error_e>(const int &value,
182 const csr_error_e &expected,
183 const std::string &filename,
184 const std::string &funcname,
187 const std::string &msg)
190 BOOST_REQUIRE_MESSAGE(value == expected,
191 "[" << filename << " > " << funcname << " : " << line <<
192 "] returned[" << capi_ec_to_string(value) <<
193 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
194 BOOST_WARN_MESSAGE(value == expected,
195 "[" << filename << " > " << funcname << " : " << line <<
196 "] returned[" << capi_ec_to_string(value) <<
197 "] expected[" << capi_ec_to_string(expected) << "] " << msg);
201 void _assert<const char *, const char *>(const char * const &value,
202 const char * const &expected,
203 const std::string &filename,
204 const std::string &funcname,
207 const std::string &msg)
209 if (value == nullptr && expected == nullptr) {
214 } else if (value != nullptr && expected != nullptr) {
215 _assert<std::string, const char *>(std::string(value), expected, filename,
216 funcname, line, isAssert, msg);
217 } else if (value == nullptr && expected != nullptr) {
219 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
220 "[" << filename << " > " << funcname << " : " << line <<
221 "] returned[nullptr] expected[" << expected << "] " << msg);
223 BOOST_WARN_MESSAGE(std::string(expected).empty(),
224 "[" << filename << " > " << funcname << " : " << line <<
225 "] returned[nullptr] expected[" << expected << "] " << msg);
228 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
229 "[" << filename << " > " << funcname << " : " << line <<
230 "] returned[" << value << "] expected[nullptr] " << msg);
232 BOOST_WARN_MESSAGE(std::string(value).empty(),
233 "[" << filename << " > " << funcname << " : " << line <<
234 "] returned[" << value << "] expected[nullptr] " << msg);
239 void _assert<char *, const char *>(char * const &value,
240 const char * const &expected,
241 const std::string &filename,
242 const std::string &funcname,
245 const std::string &msg)
247 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
251 void _assert<const char *, char *>(const char * const &value,
252 char * const &expected,
253 const std::string &filename,
254 const std::string &funcname,
257 const std::string &msg)
259 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
263 void _assert<char *, char *>(char * const &value,
264 char * const &expected,
265 const std::string &filename,
266 const std::string &funcname,
269 const std::string &msg)
271 _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
275 void _assert<const char *, std::string>(const char * const &value,
276 const std::string &expected,
277 const std::string &filename,
278 const std::string &funcname,
281 const std::string &msg)
283 _assert<std::string, std::string>(
284 (value == nullptr) ? std::string() : std::string(value),
285 expected, filename, funcname, line, isAssert, msg);
289 void _assert<char *, std::string>(char * const &value,
290 const std::string &expected,
291 const std::string &filename,
292 const std::string &funcname,
295 const std::string &msg)
297 _assert<const char *, std::string>(value, expected, filename, funcname, line, isAssert, msg);
300 void exceptionGuard(const std::function<void()> &f)
304 } catch (const std::exception &e) {
305 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
307 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
311 void copy_file_assert(const char *src_file, const char *dst_file)
317 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
318 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
320 src.open(src_file, std::ios::binary);
321 dst.open(dst_file, std::ios::binary);
324 } catch (const std::exception &e) {
325 BOOST_REQUIRE_MESSAGE(false,
326 "Failed to copy file from src[" << src_file <<
327 "] to dst[" << dst_file << "]: " << e.what());
331 void copy_file(const char *src_file, const char *dst_file)
337 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
338 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
340 src.open(src_file, std::ios::binary);
341 dst.open(dst_file, std::ios::binary);
344 } catch (const std::exception &e) {
345 BOOST_WARN_MESSAGE(false,
346 "Failed to copy file from src[" << src_file <<
347 "] to dst[" << dst_file << "]: " << e.what());
351 void make_dir_assert(const char *dir)
353 if (!std::ifstream(dir))
354 BOOST_REQUIRE_MESSAGE(
355 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
356 "Failed to mkdir[" << dir << "] with errno: " << errno);
359 void make_dir(const char *dir)
361 if (!std::ifstream(dir))
363 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
364 "Failed to mkdir[" << dir << "] with errno: " << errno);
367 void touch_file_assert(const char *file)
369 struct utimbuf new_times;
370 time_t now = ::time(nullptr);
372 new_times.actime = now;
373 new_times.modtime = now;
375 BOOST_REQUIRE_MESSAGE(
376 ::utime(file, &new_times) == 0,
377 "utime() to touch file[" << file << "] failed with errno: " << errno);
380 void touch_file(const char *file)
382 struct utimbuf new_times;
383 time_t now = ::time(nullptr);
385 new_times.actime = now;
386 new_times.modtime = now;
389 ::utime(file, &new_times) == 0,
390 "utime() to touch file[" << file << "] failed with errno: " << errno);
393 void remove_file_assert(const char *file)
395 BOOST_REQUIRE_MESSAGE(
397 "unlink file[" << file << " failed with errno: " << errno);
400 void remove_file(const char *file)
404 "unlink file[" << file << " failed with errno: " << errno);
407 bool is_file_exist(const char *file)
409 return ::access(file, F_OK) != -1;
412 bool uninstall_app(const char *pkg_id)
414 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
415 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
416 __quit_loop_on_end_cb, data, ::getuid());
420 bool install_app(const char *app_path, const char *pkg_type)
422 return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
423 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
424 PM_QUIET, __quit_loop_on_end_cb, data,
432 remove_file(RW_DBSPACE ".csr.db");
433 remove_file(RW_DBSPACE ".csr.db-journal");
435 int ret = system("systemctl restart csr.service");
436 BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);