44e49d91ebb529bb160d8c9e059fd991a0d970e9
[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 #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)
51 #else
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)
54 #endif
55 {
56         (void) req_id;
57         (void) pkg_type;
58         (void) pkgid;
59         (void) pmsg;
60
61         auto eventData = reinterpret_cast<PkgEventData *>(data);
62
63         if (key && strncmp(key, "end", strlen("end")) == 0) {
64                 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
65
66                 g_main_loop_quit(eventData->loop);
67         }
68
69         return 0;
70 }
71
72 gboolean __quit_loop_on_timeout_cb(gpointer data)
73 {
74         auto eventData = reinterpret_cast<PkgEventData *>(data);
75
76         eventData->isSuccess = false;
77         g_main_loop_quit(eventData->loop);
78
79         return FALSE;
80 }
81
82 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
83 {
84         auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
85         CHECK_IS_NOT_NULL(pkgmgr);
86
87         PkgEventData data;
88         auto ret = request(pkgmgr, &data);
89         if (ret <= PKGMGR_R_OK) {
90                 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
91                 return false;
92         }
93
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);
101
102         return data.isSuccess;
103 }
104
105 } // namespace anonymous
106
107 #define ERRORDESCRIBE(name) case name: return #name
108 std::string capi_ec_to_string(csr_error_e ec)
109 {
110         switch (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));
134         }
135 }
136 #undef ERRORDESCRIBE
137
138 std::string capi_ec_to_string(int ec)
139 {
140         return capi_ec_to_string(static_cast<csr_error_e>(ec));
141 }
142
143 template <>
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,
148                                                                            unsigned int line,
149                                                                            bool isAssert,
150                                                                            const std::string &msg)
151 {
152         if (isAssert)
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);
157         else
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);
162 }
163
164 template <>
165 void _assert<csr_error_e, int>(const csr_error_e &value,
166                                                            const int &expected,
167                                                            const std::string &filename,
168                                                            const std::string &funcname,
169                                                            unsigned int line,
170                                                            bool isAssert,
171                                                            const std::string &msg)
172 {
173         if (isAssert)
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);
178         else
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);
183 }
184
185 template <>
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,
190                                                            unsigned int line,
191                                                            bool isAssert,
192                                                            const std::string &msg)
193 {
194         if (isAssert)
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);
203 }
204
205 template <>
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,
210                                                                                  unsigned int line,
211                                                                                  bool isAssert,
212                                                                                  const std::string &msg)
213 {
214         if (value == nullptr && expected == nullptr) {
215                 if (isAssert)
216                         BOOST_REQUIRE(true);
217                 else
218                         BOOST_WARN(true);
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) {
223                 if (isAssert)
224                         BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
225                                                                   "[" << filename << " > " << funcname << " : " << line <<
226                                                                   "] returned[nullptr] expected[" << expected << "] " << msg);
227                 else
228                         BOOST_WARN_MESSAGE(std::string(expected).empty(),
229                                                            "[" << filename << " > " << funcname << " : " << line <<
230                                                            "] returned[nullptr] expected[" << expected << "] " << msg);
231         } else {
232                 if (isAssert)
233                         BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
234                                                                   "[" << filename << " > " << funcname << " : " << line <<
235                                                                   "] returned[" << value << "] expected[nullptr] " << msg);
236                 else
237                         BOOST_WARN_MESSAGE(std::string(value).empty(),
238                                                            "[" << filename << " > " << funcname << " : " << line <<
239                                                            "] returned[" << value << "] expected[nullptr] " << msg);
240         }
241 }
242
243 template <>
244 void _assert<char *, const char *>(char * const &value,
245                                                                    const char * const &expected,
246                                                                    const std::string &filename,
247                                                                    const std::string &funcname,
248                                                                    unsigned int line,
249                                                                    bool isAssert,
250                                                                    const std::string &msg)
251 {
252         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
253 }
254
255 template <>
256 void _assert<const char *, char *>(const char * const &value,
257                                                                    char * const &expected,
258                                                                    const std::string &filename,
259                                                                    const std::string &funcname,
260                                                                    unsigned int line,
261                                                                    bool isAssert,
262                                                                    const std::string &msg)
263 {
264         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
265 }
266
267 template <>
268 void _assert<char *, char *>(char * const &value,
269                                                          char * const &expected,
270                                                          const std::string &filename,
271                                                          const std::string &funcname,
272                                                          unsigned int line,
273                                                          bool isAssert,
274                                                          const std::string &msg)
275 {
276         _assert<const char *, const char *>(value, expected, filename, funcname, line, isAssert, msg);
277 }
278
279 template <>
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,
284                                                                                 unsigned int line,
285                                                                                 bool isAssert,
286                                                                                 const std::string &msg)
287 {
288         _assert<std::string, std::string>(
289                         (value == nullptr) ? std::string() : std::string(value),
290                         expected, filename, funcname, line, isAssert, msg);
291 }
292
293 template <>
294 void _assert<char *, std::string>(char * const &value,
295                                                                   const std::string &expected,
296                                                                   const std::string &filename,
297                                                                   const std::string &funcname,
298                                                                   unsigned int line,
299                                                                   bool isAssert,
300                                                                   const std::string &msg)
301 {
302         _assert<const char *, std::string>(value, expected, filename, funcname, line, isAssert, msg);
303 }
304
305 void exceptionGuard(const std::function<void()> &f)
306 {
307         try {
308                 f();
309         } catch (const std::exception &e) {
310                 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
311         } catch (...) {
312                 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
313         }
314 }
315
316 void copy_file_assert(const char *src_file, const char *dest_file)
317 {
318         try {
319                 std::ifstream srce(src_file, std::ios::binary);
320                 std::ofstream dest(dest_file, std::ios::binary);
321                 dest << srce.rdbuf();
322         } catch (const std::exception &e) {
323                 BOOST_REQUIRE_MESSAGE(false,
324                         "Failed to copy file from src[" << src_file <<
325                         "] to dst[" << dest_file << "]: " << e.what());
326         }
327 }
328
329 void copy_file(const char *src_file, const char *dest_file)
330 {
331         try {
332                 std::ifstream srce(src_file, std::ios::binary);
333                 std::ofstream dest(dest_file, std::ios::binary);
334                 dest << srce.rdbuf();
335         } catch (const std::exception &e) {
336                 BOOST_WARN_MESSAGE(false,
337                         "Failed to copy file from src[" << src_file <<
338                         "] to dst[" << dest_file << "]: " << e.what());
339         }
340 }
341
342 void make_dir_assert(const char *dir)
343 {
344         BOOST_REQUIRE_MESSAGE(
345                 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
346                 "Failed to mkdir[" << dir << "] with errno: " << errno);
347 }
348
349 void make_dir(const char *dir)
350 {
351         BOOST_WARN_MESSAGE(
352                 ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
353                 "Failed to mkdir[" << dir << "] with errno: " << errno);
354 }
355
356 void touch_file_assert(const char *file)
357 {
358         struct utimbuf new_times;
359         time_t now = ::time(nullptr);
360
361         new_times.actime = now;
362         new_times.modtime = now;
363
364         BOOST_REQUIRE_MESSAGE(
365                 ::utime(file, &new_times) == 0,
366                 "utime() to touch file[" << file << "] failed with errno: " << errno);
367 }
368
369 void touch_file(const char *file)
370 {
371         struct utimbuf new_times;
372         time_t now = ::time(nullptr);
373
374         new_times.actime = now;
375         new_times.modtime = now;
376
377         BOOST_WARN_MESSAGE(
378                 ::utime(file, &new_times) == 0,
379                 "utime() to touch file[" << file << "] failed with errno: " << errno);
380 }
381
382 void remove_file_assert(const char *file)
383 {
384         BOOST_REQUIRE_MESSAGE(
385                 ::unlink(file) == 0,
386                 "unlink file[" << file << " failed with errno: " << errno);
387 }
388
389 void remove_file(const char *file)
390 {
391         BOOST_WARN_MESSAGE(
392                 ::unlink(file) == 0,
393                 "unlink file[" << file << " failed with errno: " << errno);
394 }
395
396 bool is_file_exist(const char *file)
397 {
398         return ::access(file, F_OK) != -1;
399 }
400
401 bool uninstall_app(const char *pkg_id)
402 {
403         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
404 #ifdef PLATFORM_VERSION_3
405                 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
406                                                                                  __quit_loop_on_end_cb, data, ::getuid());
407 #else
408                 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
409                                                                                  __quit_loop_on_end_cb, data);
410 #endif
411         });
412 }
413
414 bool install_app(const char *app_path, const char *pkg_type)
415 {
416         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
417 #ifdef PLATFORM_VERSION_3
418                 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
419                                                                                    PM_QUIET, __quit_loop_on_end_cb, data,
420                                                                                    ::getuid());
421 #else
422                 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
423                                                                            __quit_loop_on_end_cb, data);
424 #endif
425         });
426 }
427
428 void initialize_db()
429 {
430 #if 0
431 #ifdef PLATFORM_VERSION_3
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 #endif
439 }
440
441 } // namespace Test