Release version 0.1.57
[platform/core/appfw/app-svc.git] / test / appsvc_test.c
1 /*
2  *  app-svc
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <poll.h>
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <Ecore.h>
27 #include <aul.h>
28
29 #include "appsvc.h"
30
31 static char **gargv;
32 static int gargc;
33 static char *cmd;
34 static int apn_pid;
35
36 typedef struct _test_func_t {
37         char *name;
38         int (*func) ();
39         char *desc;
40         char *usage;
41 } test_func_t;
42
43 void __set_bundle_from_args(bundle *kb)
44 {
45         int opt;
46         char *op = NULL;
47         char *mime = NULL;
48         char *uri = NULL;
49         char *package = NULL;
50         char *category = NULL;
51         char *key = NULL;
52         char *val_array[128];
53
54         while ((opt = getopt(gargc, gargv, "d:o:m:u:p:c:")) != -1) {
55                 switch (opt) {
56                 case 'o':
57                         if (optarg) {
58                                 if (op)
59                                         free(op);
60                                 op = strdup(optarg);
61                         }
62                         break;
63                 case 'm':
64                         if (optarg) {
65                                 if (mime)
66                                         free(mime);
67                                 mime = strdup(optarg);
68                         }
69                         break;
70                 case 'u':
71                         if (optarg) {
72                                 if (uri)
73                                         free(uri);
74                                 uri = strdup(optarg);
75                         }
76                         break;
77                 case 'p':
78                         if (optarg) {
79                                 if (package)
80                                         free(package);
81                                 package = strdup(optarg);
82                         }
83                         break;
84                 case 'c':
85                         if (optarg) {
86                                 if (category)
87                                         free(category);
88                                 category = strdup(optarg);
89                         }
90                         break;
91                 case 'd':
92                         if (optarg) {
93                                 int i = 0;
94                                 key = strtok(optarg, ",");
95                                 while (1) {
96                                         val_array[i] = strtok(NULL, ",");
97                                         if (val_array[i] == NULL)
98                                                 break;
99                                         i++;
100                                 }
101                                 if (i == 1)
102                                         appsvc_add_data(kb, key, val_array[0]);
103                                 else if (i > 1)
104                                         appsvc_add_data_array(kb, key, (const char **)val_array, i);
105                         }
106                         break;
107                 }
108         }
109
110         if (op) {
111                 appsvc_set_operation(kb, op);
112                 free(op);
113         }
114         if (mime) {
115                 appsvc_set_mime(kb, mime);
116                 free(mime);
117         }
118         if (uri) {
119                 appsvc_set_uri(kb, uri);
120                 free(uri);
121         }
122         if (package) {
123                 appsvc_set_appid(kb, package);
124                 free(package);
125         }
126         if (category) {
127                 appsvc_set_category(kb, category);
128                 free(category);
129         }
130 }
131
132 int run_svc()
133 {
134         int ret;
135         bundle *kb = NULL;
136         kb = bundle_create();
137         if (kb == NULL) {
138                 printf("bundle creation fail\n");
139                 return -1;
140         }
141         printf("[run_svc test]\n");
142
143         __set_bundle_from_args(kb);
144
145         ret = appsvc_usr_run_service(kb, 0, NULL, NULL, getuid());
146
147         if (ret >= 0) {
148                 printf("open service success\n");
149                 if (kb) {
150                         bundle_free(kb);
151                         kb = NULL;
152                 }
153                 return 0;
154         } else {
155                 printf("open service fail\n");
156                 if (kb) {
157                         bundle_free(kb);
158                         kb = NULL;
159                 }
160                 return -1;
161         }
162 }
163
164 static void prt_recvd_bundle(const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
165 {
166         char **array_val;
167         int array_len;
168         size_t *array_item_size;
169
170         char *val;
171         size_t size;
172         int i;
173
174         if (bundle_keyval_type_is_array((bundle_keyval_t *)kv) > 0) {
175                 bundle_keyval_get_array_val((bundle_keyval_t *)kv, (void ***)&array_val, (unsigned int *)&array_len, &array_item_size);
176
177                 for (i = 0; i < array_len; i++)
178                         printf("recvd - key: %s[%d], value: %s\n", key, i, array_val[i]);
179         } else {
180                 bundle_keyval_get_basic_val((bundle_keyval_t *)kv, (void **)&val, &size);
181                 printf("recvd - key: %s, value: %s\n", key, val);
182         }
183 }
184
185 static void cb_func(bundle *kb, int request_code, appsvc_result_val result, void *data)
186 {
187         int num;
188         num = (int)data;
189
190         if (result == APPSVC_RES_CANCEL) {
191                 printf("==== %d : canceled(preemptted) my request ===\n", num);
192         } else {
193                 printf("==== %d : result packet === result %d\n", num, (int)result);
194                 //bundle_iterate(kb, prt_recvd_bundle, NULL);
195
196                 bundle_foreach(kb, prt_recvd_bundle, NULL);
197         }
198
199         if (strcmp(cmd, "run_svc_res") == 0) {
200                 printf("==== end of appsvc_run() ====\n");
201                 ecore_main_loop_quit();
202         }
203 }
204
205 int run_svc_res()
206 {
207         static int num = 0;
208         int ret;
209
210         bundle *kb = NULL;
211         kb = bundle_create();
212         if (kb == NULL) {
213                 printf("bundle creation fail\n");
214                 return -1;
215         }
216
217         printf("[run_svc_res test]\n");
218
219         __set_bundle_from_args(kb);
220
221         ret = appsvc_usr_run_service(kb, 0, cb_func, (void *)num, getuid());
222
223         if (ret >= 0) {
224                 printf("open service success\n");
225                 if (kb) {
226                         bundle_free(kb);
227                         kb = NULL;
228                 }
229                 return 0;
230         } else {
231                 printf("open service fail\n");
232                 if (kb) {
233                         bundle_free(kb);
234                         kb = NULL;
235                 }
236                 return -1;
237         }
238 }
239
240 static test_func_t test_func[] = {
241
242         {"run_svc", run_svc, "run_svc test",
243          "[usage] run_svc -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
244         {"run_svc_res", run_svc_res, "run_svc_res test",
245          "[usage] run_svc_res -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
246
247 };
248
249 int callfunc(char *testname)
250 {
251         test_func_t *tmp;
252         int res;
253         int i;
254
255         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
256                 tmp = &test_func[i];
257                 if (strcmp(testname, tmp->name) == 0) {
258                         res = tmp->func();
259                         if (strcmp(testname, "all")) {
260                                 if (res < 0)
261                                         printf("... test failed\n");
262                                 else
263                                         printf("... test successs ret = %d\n", res);
264                         }
265                 }
266         }
267         return 0;
268 }
269
270 void print_usage(char *progname)
271 {
272         test_func_t *tmp;
273         int i;
274
275         printf("[usage] %s <cmd> ...\n", progname);
276         printf(" - available cmd list\n");
277
278         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
279                 tmp = &test_func[i];
280                 printf("\t%s : %s\n", tmp->name, tmp->desc);
281                 printf("\t\t%s\n", tmp->usage);
282         }
283
284 }
285
286 static Eina_Bool run_func(void *data)
287 {
288         callfunc(cmd);
289
290         if (strcmp(cmd, "run_svc_res") == 0)
291                 return 0;
292         else
293                 ecore_main_loop_quit();
294
295         return 0;
296 }
297
298 int main(int argc, char **argv)
299 {
300         if (argc < 3) {
301                 print_usage(argv[0]);
302                 exit(0);
303         }
304
305         ecore_init();
306
307         cmd = argv[1];
308         gargc = argc;
309         gargv = argv;
310         apn_pid = atoi(argv[2]);
311
312         aul_launch_init(NULL, NULL);
313
314         //aul_listen_app_dead_signal(dead_tracker,NULL);
315         //aul_listen_app_dead_signal(NULL,NULL);
316
317         ecore_idler_add(run_func, NULL);
318
319         ecore_main_loop_begin();
320
321         return 0;
322 }