[CAPI Changed] Enum value names and scan cloud
[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                 return false;
90
91         auto id = g_timeout_add_seconds(10, __quit_loop_on_timeout_cb, &data);
92         data.loop = g_main_loop_new(nullptr, false);
93         g_main_loop_run(data.loop);
94         BOOST_REQUIRE_MESSAGE(g_source_remove(id),
95                         "Failed to remove timeout event source from main loop.");
96         g_main_loop_unref(data.loop);
97         pkgmgr_client_free(pkgmgr);
98
99         return data.isSuccess;
100 }
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_SOCKET);
111         ERRORDESCRIBE(CSR_ERROR_INVALID_HANDLE);
112         ERRORDESCRIBE(CSR_ERROR_SERVER);
113         ERRORDESCRIBE(CSR_ERROR_NO_TASK);
114         ERRORDESCRIBE(CSR_ERROR_DB);
115         ERRORDESCRIBE(CSR_ERROR_REMOVE_FAILED);
116         ERRORDESCRIBE(CSR_ERROR_FILE_DO_NOT_EXIST);
117         ERRORDESCRIBE(CSR_ERROR_FILE_CHANGED);
118         ERRORDESCRIBE(CSR_ERROR_FILE_SYSTEM);
119         ERRORDESCRIBE(CSR_ERROR_ENGINE_PERMISSION);
120         ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_EXIST);
121         ERRORDESCRIBE(CSR_ERROR_ENGINE_DISABLED);
122         ERRORDESCRIBE(CSR_ERROR_ENGINE_NOT_ACTIVATED);
123         ERRORDESCRIBE(CSR_ERROR_ENGINE_INTERNAL);
124         ERRORDESCRIBE(CSR_ERROR_SYSTEM);
125         default: return std::string("Undefined capi error code: ")
126                                                 + std::to_string(static_cast<int>(ec));
127         }
128 }
129 #undef ERRORDESCRIBE
130
131 } // namespace anonymous
132
133 template <>
134 void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
135                                                                            const csr_error_e &expected,
136                                                                            const std::string &filename,
137                                                                            const std::string &funcname,
138                                                                            unsigned int line,
139                                                                            const std::string &msg)
140 {
141         BOOST_REQUIRE_MESSAGE(value == expected,
142                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
143                                                   " returned[" << capi_ec_to_string(value) <<
144                                                   "] expected[" << capi_ec_to_string(expected) << "] " << msg);
145 }
146
147 template <>
148 void _assert<csr_error_e, int>(const csr_error_e &value,
149                                                            const int &expected,
150                                                            const std::string &filename,
151                                                            const std::string &funcname,
152                                                            unsigned int line,
153                                                            const std::string &msg)
154 {
155         BOOST_REQUIRE_MESSAGE(value == expected,
156                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
157                                                   " returned[" << capi_ec_to_string(value) << "] expected[" <<
158                                                   capi_ec_to_string(static_cast<csr_error_e>(expected)) << "]" <<
159                                                   " " << msg);
160 }
161
162 template <>
163 void _assert<int, csr_error_e>(const int &value,
164                                                            const csr_error_e &expected,
165                                                            const std::string &filename,
166                                                            const std::string &funcname,
167                                                            unsigned int line,
168                                                            const std::string &msg)
169 {
170         BOOST_REQUIRE_MESSAGE(value == expected,
171                                                   "[" << filename << " > " << funcname << " : " << line << "]" <<
172                                                   " returned[" <<
173                                                   capi_ec_to_string(static_cast<csr_error_e>(value)) <<
174                                                   "] expected[" << capi_ec_to_string(expected) << "] " << msg);
175 }
176
177 template <>
178 void _assert<const char *, const char *>(const char * const &value,
179                                                                                  const char * const &expected,
180                                                                                  const std::string &filename,
181                                                                                  const std::string &funcname,
182                                                                                  unsigned int line,
183                                                                                  const std::string &msg)
184 {
185         if (value == nullptr && expected == nullptr)
186                 BOOST_REQUIRE(true);
187         else if (value != nullptr && expected != nullptr)
188                 _assert<std::string, const char *>(std::string(value), expected, filename,
189                                                                                    funcname, line, msg);
190         else if (value == nullptr && expected != nullptr)
191                 BOOST_REQUIRE_MESSAGE(std::string(expected).empty(),
192                                                           "[" << filename << " > " << funcname << " : " << line <<
193                                                           "] returned[nullptr] expected[" << expected << "] " << msg);
194         else
195                 BOOST_REQUIRE_MESSAGE(std::string(value).empty(),
196                                                           "[" << filename << " > " << funcname << " : " << line <<
197                                                           "] returned[" << value << "] expected[nullptr] " << msg);
198 }
199
200 template <>
201 void _assert<char *, const char *>(char * const &value,
202                                                                    const char * const &expected,
203                                                                    const std::string &filename,
204                                                                    const std::string &funcname,
205                                                                    unsigned int line,
206                                                                    const std::string &msg)
207 {
208         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
209 }
210
211 template <>
212 void _assert<const char *, char *>(const char * const &value,
213                                                                    char * const &expected,
214                                                                    const std::string &filename,
215                                                                    const std::string &funcname,
216                                                                    unsigned int line,
217                                                                    const std::string &msg)
218 {
219         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
220 }
221
222 template <>
223 void _assert<char *, char *>(char * const &value,
224                                                          char * const &expected,
225                                                          const std::string &filename,
226                                                          const std::string &funcname,
227                                                          unsigned int line,
228                                                          const std::string &msg)
229 {
230         _assert<const char *, const char *>(value, expected, filename, funcname, line, msg);
231 }
232
233 template <>
234 void _assert<const char *, std::string>(const char * const &value,
235                                                                                 const std::string &expected,
236                                                                                 const std::string &filename,
237                                                                                 const std::string &funcname,
238                                                                                 unsigned int line,
239                                                                                 const std::string &msg)
240 {
241         _assert<std::string, std::string>(
242                         (value == nullptr) ? std::string() : std::string(value),
243                         expected, filename, funcname, line, msg);
244 }
245
246 template <>
247 void _assert<char *, std::string>(char * const &value,
248                                                                   const std::string &expected,
249                                                                   const std::string &filename,
250                                                                   const std::string &funcname,
251                                                                   unsigned int line,
252                                                                   const std::string &msg)
253 {
254         _assert<const char *, std::string>(value, expected, filename, funcname, line, msg);
255 }
256
257 void exceptionGuard(const std::function<void()> &f)
258 {
259         try {
260                 f();
261         } catch (const std::exception &e) {
262                 BOOST_REQUIRE_MESSAGE(0, "std::exception caught: " << e.what());
263         } catch (...) {
264                 BOOST_REQUIRE_MESSAGE(0, "Unknown exception caught");
265         }
266 }
267
268 void copy_file(const char *src_file, const char *dest_file)
269 {
270         std::ifstream srce(src_file, std::ios::binary);
271         std::ofstream dest(dest_file, std::ios::binary);
272         dest << srce.rdbuf();
273 }
274
275 void make_dir(const char *dir)
276 {
277         mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
278 }
279
280 void touch_file(const char *file)
281 {
282         struct utimbuf new_times;
283         time_t now = time(nullptr);
284
285         new_times.actime = now;
286         new_times.modtime = now;
287
288         utime(file, &new_times);
289 }
290
291 void remove_file(const char *file)
292 {
293         unlink(file);
294 }
295
296 bool is_file_exist(const char *file)
297 {
298         return (access(file, F_OK) != -1);
299 }
300
301 bool uninstall_app(const char *pkg_id)
302 {
303         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
304                 return pkgmgr_client_uninstall(pc, nullptr, pkg_id, PM_QUIET,
305                                                                            __quit_loop_on_end_cb, data);
306         });
307 }
308
309 bool install_app(const char *app_path, const char *pkg_type)
310 {
311         return pkgmgr_request([&](pkgmgr_client *pc, PkgEventData *data) {
312                 return pkgmgr_client_install(pc, pkg_type, nullptr, app_path, nullptr, PM_QUIET,
313                                                                          __quit_loop_on_end_cb, data);
314         });
315 }
316
317 void initialize_db()
318 {
319 #ifdef PLATFORM_VERSION3
320         remove_file(RW_DBSPACE ".csr.db");
321         remove_file(RW_DBSPACE ".csr.db-journal");
322
323         int ret = system("systemctl restart csr.service");
324         BOOST_MESSAGE("CSR DB Initalization & Daemon Restart. Result=" << ret);
325 #endif
326 }
327
328 } // namespace Test