Disable gcov
[platform/upstream/csr-framework.git] / test / test-common.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file        test-common.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       Common utilities for test
21  */
22 #include "test-common.h"
23
24 #include <iostream>
25 #include <fstream>
26 #include <functional>
27 #include <cerrno>
28 #include <unistd.h>
29 #include <utime.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <stdlib.h>
33 #include <glib.h>
34
35 #include <package-manager.h>
36
37 namespace Test {
38
39 namespace {
40
41 struct PkgEventData {
42         bool isSuccess;
43         GMainLoop *loop;
44
45         PkgEventData() : isSuccess(false), loop(nullptr) {}
46 };
47
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)
50 {
51         (void) req_id;
52         (void) pkg_type;
53         (void) pkgid;
54         (void) pmsg;
55
56         auto eventData = reinterpret_cast<PkgEventData *>(data);
57
58         if (key && strncmp(key, "end", strlen("end")) == 0) {
59                 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
60
61                 g_main_loop_quit(eventData->loop);
62         }
63
64         return 0;
65 }
66
67 gboolean __quit_loop_on_timeout_cb(gpointer data)
68 {
69         auto eventData = reinterpret_cast<PkgEventData *>(data);
70
71         eventData->isSuccess = false;
72         g_main_loop_quit(eventData->loop);
73
74         return FALSE;
75 }
76
77 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
78 {
79         auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
80         CHECK_IS_NOT_NULL(pkgmgr);
81
82         PkgEventData data;
83         auto ret = request(pkgmgr, &data);
84         if (ret <= PKGMGR_R_OK) {
85                 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
86                 return false;
87         }
88
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);
96
97         return data.isSuccess;
98 }
99
100 } // namespace anonymous
101
102 #define ERRORDESCRIBE(name) case name: return #name
103 std::string capi_ec_to_string(csr_error_e ec)
104 {
105         switch (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));
129         }
130 }
131 #undef ERRORDESCRIBE
132
133 std::string capi_ec_to_string(int ec)
134 {
135         return capi_ec_to_string(static_cast<csr_error_e>(ec));
136 }
137
138 template <>
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,
143                                                                            unsigned int line,
144                                                                            bool isAssert,
145                                                                            const std::string &msg)
146 {
147         if (isAssert)
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);
152         else
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);
157 }
158
159 template <>
160 void _assert<csr_error_e, int>(const csr_error_e &value,
161                                                            const int &expected,
162                                                            const std::string &filename,
163                                                            const std::string &funcname,
164                                                            unsigned int line,
165                                                            bool isAssert,
166                                                            const std::string &msg)
167 {
168         if (isAssert)
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);
173         else
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);
178 }
179
180 template <>
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,
185                                                            unsigned int line,
186                                                            bool isAssert,
187                                                            const std::string &msg)
188 {
189         if (isAssert)
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);
198 }
199
200 template <>
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,
205                                                                                  unsigned int line,
206                                                                                  bool isAssert,
207                                                                                  const std::string &msg)
208 {
209         if (value == nullptr && expected == nullptr) {
210                 if (isAssert)
211                         BOOST_REQUIRE(true);
212                 else
213                         BOOST_WARN(true);
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) {
218                 if (isAssert)
219                         BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
220                                                                   "[" << filename << " > " << funcname << " : " << line <<
221                                                                   "] returned[nullptr] expected[" << expected << "] " << msg);
222                 else
223                         BOOST_WARN_MESSAGE(std::string(expected).empty(),
224                                                            "[" << filename << " > " << funcname << " : " << line <<
225                                                            "] returned[nullptr] expected[" << expected << "] " << msg);
226         } else {
227                 if (isAssert)
228                         BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
229                                                                   "[" << filename << " > " << funcname << " : " << line <<
230                                                                   "] returned[" << value << "] expected[nullptr] " << msg);
231                 else
232                         BOOST_WARN_MESSAGE(std::string(value).empty(),
233                                                            "[" << filename << " > " << funcname << " : " << line <<
234                                                            "] returned[" << value << "] expected[nullptr] " << msg);
235         }
236 }
237
238 template <>
239 void _assert<char *, const char *>(char * const &value,
240                                                                    const char * const &expected,
241                                                                    const std::string &filename,
242                                                                    const std::string &funcname,
243                                                                    unsigned int line,
244                                                                    bool isAssert,
245                                                                    const std::string &msg)
246 {
247         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
248 }
249
250 template <>
251 void _assert<const char *, char *>(const char * const &value,
252                                                                    char * const &expected,
253                                                                    const std::string &filename,
254                                                                    const std::string &funcname,
255                                                                    unsigned int line,
256                                                                    bool isAssert,
257                                                                    const std::string &msg)
258 {
259         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
260 }
261
262 template <>
263 void _assert<char *, char *>(char * const &value,
264                                                          char * const &expected,
265                                                          const std::string &filename,
266                                                          const std::string &funcname,
267                                                          unsigned int line,
268                                                          bool isAssert,
269                                                          const std::string &msg)
270 {
271         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
272 }
273
274 template <>
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,
279                                                                                 unsigned int line,
280                                                                                 bool isAssert,
281                                                                                 const std::string &msg)
282 {
283         _assert<std::string, std::string>(
284                         (value == nullptr) ? std::string() : std::string(value),
285                         expected, filename, funcname, line, isAssert, msg);
286 }
287
288 template <>
289 void _assert<char *, std::string>(char * const &value,
290                                                                   const std::string &expected,
291                                                                   const std::string &filename,
292                                                                   const std::string &funcname,
293                                                                   unsigned int line,
294                                                                   bool isAssert,
295                                                                   const std::string &msg)
296 {
297         _assert<const char *, std::string>(value, expected, filename, funcname, line, isAssert, msg);
298 }
299
300 void exceptionGuard(const std::function<void()> &f)
301 {
302         try {
303                 f();
304         } catch (const std::exception &e) {
305                 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
306         } catch (...) {
307                 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
308         }
309 }
310
311 void copy_file_assert(const char *src_file, const char *dst_file)
312 {
313         try {
314                 std::ifstream src;
315                 std::ofstream dst;
316
317                 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
318                 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
319
320                 src.open(src_file, std::ios::binary);
321                 dst.open(dst_file, std::ios::binary);
322
323                 dst << src.rdbuf();
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());
328         }
329 }
330
331 void copy_file(const char *src_file, const char *dst_file)
332 {
333         try {
334                 std::ifstream src;
335                 std::ofstream dst;
336
337                 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
338                 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
339
340                 src.open(src_file, std::ios::binary);
341                 dst.open(dst_file, std::ios::binary);
342
343                 dst << src.rdbuf();
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());
348         }
349 }
350
351 void make_dir_assert(const char *dir)
352 {
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);
357 }
358
359 void make_dir(const char *dir)
360 {
361         if (!std::ifstream(dir))
362                 BOOST_WARN_MESSAGE(
363                         ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
364                         "Failed to mkdir[" << dir << "] with errno: " << errno);
365 }
366
367 void touch_file_assert(const char *file)
368 {
369         struct utimbuf new_times;
370         time_t now = ::time(nullptr);
371
372         new_times.actime = now;
373         new_times.modtime = now;
374
375         BOOST_REQUIRE_MESSAGE(
376                 ::utime(file, &new_times) == 0,
377                 "utime() to touch file[" << file << "] failed with errno: " << errno);
378 }
379
380 void touch_file(const char *file)
381 {
382         struct utimbuf new_times;
383         time_t now = ::time(nullptr);
384
385         new_times.actime = now;
386         new_times.modtime = now;
387
388         BOOST_WARN_MESSAGE(
389                 ::utime(file, &new_times) == 0,
390                 "utime() to touch file[" << file << "] failed with errno: " << errno);
391 }
392
393 void remove_file_assert(const char *file)
394 {
395         BOOST_REQUIRE_MESSAGE(
396                 ::unlink(file) == 0,
397                 "unlink file[" << file << " failed with errno: " << errno);
398 }
399
400 void remove_file(const char *file)
401 {
402         BOOST_WARN_MESSAGE(
403                 ::unlink(file) == 0,
404                 "unlink file[" << file << " failed with errno: " << errno);
405 }
406
407 bool is_file_exist(const char *file)
408 {
409         return ::access(file, F_OK) != -1;
410 }
411
412 bool uninstall_app(const char *pkg_id)
413 {
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());
417         });
418 }
419
420 bool install_app(const char *app_path, const char *pkg_type)
421 {
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,
425                                                                                    ::getuid());
426         });
427 }
428
429 void initialize_db()
430 {
431 #if 0
432         remove_file(RW_DBSPACE ".csr.db");
433         remove_file(RW_DBSPACE ".csr.db-journal");
434
435         int ret = system("systemctl restart csr.service");
436         BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);
437 #endif
438 }
439
440 } // namespace Test