Restore data_version usage from db schema
[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 *dst_file)
317 {
318         try {
319                 std::ifstream src;
320                 std::ofstream dst;
321
322                 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
323                 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
324
325                 src.open(src_file, std::ios::binary);
326                 dst.open(dst_file, std::ios::binary);
327
328                 dst << src.rdbuf();
329         } catch (const std::exception &e) {
330                 BOOST_REQUIRE_MESSAGE(false,
331                         "Failed to copy file from src[" << src_file <<
332                         "] to dst[" << dst_file << "]: " << e.what());
333         }
334 }
335
336 void copy_file(const char *src_file, const char *dst_file)
337 {
338         try {
339                 std::ifstream src;
340                 std::ofstream dst;
341
342                 src.exceptions(std::ifstream::failbit | std::ifstream::badbit);
343                 dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
344
345                 src.open(src_file, std::ios::binary);
346                 dst.open(dst_file, std::ios::binary);
347
348                 dst << src.rdbuf();
349         } catch (const std::exception &e) {
350                 BOOST_WARN_MESSAGE(false,
351                         "Failed to copy file from src[" << src_file <<
352                         "] to dst[" << dst_file << "]: " << e.what());
353         }
354 }
355
356 void make_dir_assert(const char *dir)
357 {
358         if (!std::ifstream(dir))
359                 BOOST_REQUIRE_MESSAGE(
360                         ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
361                         "Failed to mkdir[" << dir << "] with errno: " << errno);
362 }
363
364 void make_dir(const char *dir)
365 {
366         if (!std::ifstream(dir))
367                 BOOST_WARN_MESSAGE(
368                         ::mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
369                         "Failed to mkdir[" << dir << "] with errno: " << errno);
370 }
371
372 void touch_file_assert(const char *file)
373 {
374         struct utimbuf new_times;
375         time_t now = ::time(nullptr);
376
377         new_times.actime = now;
378         new_times.modtime = now;
379
380         BOOST_REQUIRE_MESSAGE(
381                 ::utime(file, &new_times) == 0,
382                 "utime() to touch file[" << file << "] failed with errno: " << errno);
383 }
384
385 void touch_file(const char *file)
386 {
387         struct utimbuf new_times;
388         time_t now = ::time(nullptr);
389
390         new_times.actime = now;
391         new_times.modtime = now;
392
393         BOOST_WARN_MESSAGE(
394                 ::utime(file, &new_times) == 0,
395                 "utime() to touch file[" << file << "] failed with errno: " << errno);
396 }
397
398 void remove_file_assert(const char *file)
399 {
400         BOOST_REQUIRE_MESSAGE(
401                 ::unlink(file) == 0,
402                 "unlink file[" << file << " failed with errno: " << errno);
403 }
404
405 void remove_file(const char *file)
406 {
407         BOOST_WARN_MESSAGE(
408                 ::unlink(file) == 0,
409                 "unlink file[" << file << " failed with errno: " << errno);
410 }
411
412 bool is_file_exist(const char *file)
413 {
414         return ::access(file, F_OK) != -1;
415 }
416
417 bool uninstall_app(const char *pkg_id)
418 {
419         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
420 #ifdef PLATFORM_VERSION_3
421                 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
422                                                                                  __quit_loop_on_end_cb, data, ::getuid());
423 #else
424                 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
425                                                                                  __quit_loop_on_end_cb, data);
426 #endif
427         });
428 }
429
430 bool install_app(const char *app_path, const char *pkg_type)
431 {
432         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
433 #ifdef PLATFORM_VERSION_3
434                 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
435                                                                                    PM_QUIET, __quit_loop_on_end_cb, data,
436                                                                                    ::getuid());
437 #else
438                 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
439                                                                            __quit_loop_on_end_cb, data);
440 #endif
441         });
442 }
443
444 void initialize_db()
445 {
446 #if 0
447 #ifdef PLATFORM_VERSION_3
448         remove_file(RW_DBSPACE ".csr.db");
449         remove_file(RW_DBSPACE ".csr.db-journal");
450
451         int ret = system("systemctl restart csr.service");
452         BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);
453 #endif
454 #endif
455 }
456
457 } // namespace Test