Support test app management on tizen v 3.0
[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 <unistd.h>
28 #include <utime.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>
32 #include <glib.h>
33
34 #include <package-manager.h>
35
36 namespace Test {
37
38 namespace {
39
40 struct PkgEventData {
41         bool isSuccess;
42         GMainLoop *loop;
43
44         PkgEventData() : isSuccess(false), loop(nullptr) {}
45 };
46
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)
50 #else
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)
53 #endif
54 {
55         (void) req_id;
56         (void) pkg_type;
57         (void) pkgid;
58         (void) pmsg;
59
60         auto eventData = reinterpret_cast<PkgEventData *>(data);
61
62         if (key && strncmp(key, "end", strlen("end")) == 0) {
63                 eventData->isSuccess = (strncmp(val, "ok", strlen("ok")) == 0);
64
65                 g_main_loop_quit(eventData->loop);
66         }
67
68         return 0;
69 }
70
71 gboolean __quit_loop_on_timeout_cb(gpointer data)
72 {
73         auto eventData = reinterpret_cast<PkgEventData *>(data);
74
75         eventData->isSuccess = false;
76         g_main_loop_quit(eventData->loop);
77
78         return FALSE;
79 }
80
81 bool pkgmgr_request(const std::function<int(pkgmgr_client *, PkgEventData *)> &request)
82 {
83         auto pkgmgr = pkgmgr_client_new(PC_REQUEST);
84         CHECK_IS_NOT_NULL(pkgmgr);
85
86         PkgEventData data;
87         auto ret = request(pkgmgr, &data);
88         if (ret <= PKGMGR_R_OK) {
89                 BOOST_MESSAGE("pkgmgr request failed with ret: " << ret);
90                 return false;
91         }
92
93         auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
94         data.loop = g_main_loop_new(nullptr, false);
95         g_main_loop_run(data.loop);
96         BOOST_WARN_MESSAGE(g_source_remove(id),
97                         "Failed to remove timeout event source from main loop.");
98         g_main_loop_unref(data.loop);
99         pkgmgr_client_free(pkgmgr);
100
101         return data.isSuccess;
102 }
103
104 #define ERRORDESCRIBE(name) case name: return #name
105 std::string capi_ec_to_string(csr_error_e ec)
106 {
107         switch (ec) {
108         ERRORDESCRIBE(CSR_ERROR_NONE);
109         ERRORDESCRIBE(CSR_ERROR_INVALID_PARAMETER);
110         ERRORDESCRIBE(CSR_ERROR_OUT_OF_MEMORY);
111         ERRORDESCRIBE(CSR_ERROR_PERMISSION_DENIED);
112         ERRORDESCRIBE(CSR_ERROR_NOT_SUPPORTED);
113         ERRORDESCRIBE(CSR_ERROR_BUSY);
114         ERRORDESCRIBE(CSR_ERROR_SOCKET);
115         ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
116         ERRORDESCRIBE(CSR_ERROR_SERVER);
117         ERRORDESCRIBE(CSR_ERROR_NO_TASK);
118         ERRORDESCRIBE(CSR_ERROR_DB);
119         ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
120         ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
121         ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
122         ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
123         ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
124         ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
125         ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
126         ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
127         ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
128         ERRORDESCRIBE(CSR_ERROR_SYSTEM);
129         default: return std::string("Undefined capi error code: ")
130                                                 + std::to_string(static_cast<int>(ec));
131         }
132 }
133 #undef ERRORDESCRIBE
134
135 } // namespace anonymous
136
137 template <>
138 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
139                                                                            const csr_error_e &expected,
140                                                                            const std::string &filename,
141                                                                            const std::string &funcname,
142                                                                            unsigned int line,
143                                                                            const std::string &msg)
144 {
145         BOOST_REQUIRE_MESSAGE(value == expected,
146                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
147                                                   " returned[" << capi_ec_to_string(value) <<
148                                                   "] expected[" << capi_ec_to_string(expected) << "] " << msg);
149 }
150
151 template <>
152 void _assert<csr_error_e, int>(const csr_error_e &value,
153                                                            const int &expected,
154                                                            const std::string &filename,
155                                                            const std::string &funcname,
156                                                            unsigned int line,
157                                                            const std::string &msg)
158 {
159         BOOST_REQUIRE_MESSAGE(value == expected,
160                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
161                                                   " returned[" << capi_ec_to_string(value) << "] expected[" <<
162                                                   capi_ec_to_string(static_cast<csr_error_e>(expected)) << "]" <<
163                                                   " " << msg);
164 }
165
166 template <>
167 void _assert<int, csr_error_e>(const int &value,
168                                                            const csr_error_e &expected,
169                                                            const std::string &filename,
170                                                            const std::string &funcname,
171                                                            unsigned int line,
172                                                            const std::string &msg)
173 {
174         BOOST_REQUIRE_MESSAGE(value == expected,
175                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
176                                                   " returned[" <<
177                                                   capi_ec_to_string(static_cast<csr_error_e>(value)) <<
178                                                   "] expected[" << capi_ec_to_string(expected) << "] " << msg);
179 }
180
181 template <>
182 void _assert<const char *, const char *>(const char * const &value,
183                                                                                  const char * const &expected,
184                                                                                  const std::string &filename,
185                                                                                  const std::string &funcname,
186                                                                                  unsigned int line,
187                                                                                  const std::string &msg)
188 {
189         if (value == nullptr && expected == nullptr)
190                 BOOST_REQUIRE(true);
191         else if (value != nullptr && expected != nullptr)
192                 _assert<std::string, const char *>(std::string(value), expected, filename,
193                                                                                    funcname, line, msg);
194         else if (value == nullptr && expected != nullptr)
195                 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
196                                                           "[" << filename << " > " << funcname << " : " << line <<
197                                                           "] returned[nullptr] expected[" << expected << "] " << msg);
198         else
199                 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
200                                                           "[" << filename << " > " << funcname << " : " << line <<
201                                                           "] returned[" << value << "] expected[nullptr] " << msg);
202 }
203
204 template <>
205 void _assert<char *, const char *>(char * const &value,
206                                                                    const char * const &expected,
207                                                                    const std::string &filename,
208                                                                    const std::string &funcname,
209                                                                    unsigned int line,
210                                                                    const std::string &msg)
211 {
212         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
213 }
214
215 template <>
216 void _assert<const char *, char *>(const char * const &value,
217                                                                    char * const &expected,
218                                                                    const std::string &filename,
219                                                                    const std::string &funcname,
220                                                                    unsigned int line,
221                                                                    const std::string &msg)
222 {
223         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
224 }
225
226 template <>
227 void _assert<char *, char *>(char * const &value,
228                                                          char * const &expected,
229                                                          const std::string &filename,
230                                                          const std::string &funcname,
231                                                          unsigned int line,
232                                                          const std::string &msg)
233 {
234         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
235 }
236
237 template <>
238 void _assert<const char *, std::string>(const char * const &value,
239                                                                                 const std::string &expected,
240                                                                                 const std::string &filename,
241                                                                                 const std::string &funcname,
242                                                                                 unsigned int line,
243                                                                                 const std::string &msg)
244 {
245         _assert<std::string, std::string>(
246                         (value == nullptr) ? std::string() : std::string(value),
247                         expected, filename, funcname, line, msg);
248 }
249
250 template <>
251 void _assert<char *, std::string>(char * const &value,
252                                                                   const std::string &expected,
253                                                                   const std::string &filename,
254                                                                   const std::string &funcname,
255                                                                   unsigned int line,
256                                                                   const std::string &msg)
257 {
258         _assert<const char *, std::string>(value, expected, filename, funcname, line, msg);
259 }
260
261 void exceptionGuard(const std::function<void()> &f)
262 {
263         try {
264                 f();
265         } catch (const std::exception &e) {
266                 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
267         } catch (...) {
268                 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
269         }
270 }
271
272 void copy_file(const char *src_file, const char *dest_file)
273 {
274         std::ifstream srce(src_file, std::ios::binary);
275         std::ofstream dest(dest_file, std::ios::binary);
276         dest << srce.rdbuf();
277 }
278
279 void make_dir(const char *dir)
280 {
281         mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
282 }
283
284 void touch_file(const char *file)
285 {
286         struct utimbuf new_times;
287         time_t now = time(nullptr);
288
289         new_times.actime = now;
290         new_times.modtime = now;
291
292         utime(file, &new_times);
293 }
294
295 void remove_file(const char *file)
296 {
297         unlink(file);
298 }
299
300 bool is_file_exist(const char *file)
301 {
302         return (access(file, F_OK) != -1);
303 }
304
305 bool uninstall_app(const char *pkg_id)
306 {
307         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
308 #ifdef PLATFORM_VERSION_3
309                 return ::pkgmgr_client_usr_uninstall(pc, nullptr, pkg_id, PM_QUIET,
310                                                                                  __quit_loop_on_end_cb, data, ::getuid());
311 #else
312                 return ::pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
313                                                                                  __quit_loop_on_end_cb, data);
314 #endif
315         });
316 }
317
318 bool install_app(const char *app_path, const char *pkg_type)
319 {
320         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
321 #ifdef PLATFORM_VERSION_3
322                 return ::pkgmgr_client_usr_install(pc, pkg_type, nullptr, app_path, nullptr,
323                                                                                    PM_QUIET, __quit_loop_on_end_cb, data,
324                                                                                    ::getuid());
325 #else
326                 return ::pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
327                                                                            __quit_loop_on_end_cb, data);
328 #endif
329         });
330 }
331
332 void initialize_db()
333 {
334 #ifdef PLATFORM_VERSION_3
335         remove_file(RW_DBSPACE ".csr.db");
336         remove_file(RW_DBSPACE ".csr.db-journal");
337
338         int ret = system("systemctl restart csr.service");
339         BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);
340 #endif
341 }
342
343 } // namespace Test