update for beta universally
[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
32 static char** gargv;
33 static int gargc;
34 static char* cmd;
35 static int apn_pid;
36
37
38 typedef struct _test_func_t{
39         char* name;
40         int(*func)();
41         char* desc;
42         char* usage;
43 }test_func_t;
44
45 __set_bundle_from_args(bundle * kb)
46 {
47         int opt;
48         char *op = NULL;
49         char *mime = NULL;
50         char *uri = NULL;
51         char *package = NULL;
52         char* key = NULL;
53         char* val = NULL;
54         char* val_array[128];
55         
56         while( (opt = getopt(gargc,gargv,"d:o:m:u:p:")) != -1){
57                 switch(opt) {
58                         case 'o':
59                                 if(optarg)
60                                         op = strdup(optarg);
61                                 break;
62                         case 'm':
63                                 if(optarg)
64                                         mime = strdup(optarg);
65                                 break;
66                         case 'u':
67                                 if(optarg)
68                                         uri = strdup(optarg);
69                                 break;
70                         case 'p':
71                                 if(optarg)
72                                         package = strdup(optarg);
73                                 break;
74                         case 'd':
75                                 if(optarg){
76                                         int i = 0;
77                                         key = strtok(optarg,",");
78                                         while(val_array[i] = strtok(NULL,","))
79                                         {
80                                                 i++;
81                                         }
82                                         if(i==1) 
83                                                 appsvc_add_data(kb, key, val_array[0]);
84                                         else if(i>1) 
85                                                 appsvc_add_data_array(kb, key, val_array, i);   
86                                 }
87                                 break;
88                 }
89         }               
90
91         if(op) {
92                 appsvc_set_operation(kb,op);
93                 free(op);
94         }
95         if(mime) {
96                 appsvc_set_mime(kb,mime);
97                 free(mime);
98         }
99         if(uri) {
100                 appsvc_set_uri(kb,uri);
101                 free(uri);
102         }
103         if(package) {
104                 appsvc_set_pkgname(kb,package);
105                 free(package);
106         }
107 }
108
109 int run_svc()
110 {
111         static int num=0;
112         int ret;
113         bundle *kb=NULL;
114         kb = bundle_create();
115         if(kb == NULL)
116         {
117                 printf("bundle creation fail\n");
118                 return -1;
119         }
120         printf("[run_svc test]\n");
121
122         __set_bundle_from_args(kb);
123         
124         ret = appsvc_run_service(kb,0,NULL, NULL);
125         
126         if(ret >= 0){
127                 printf("open service success\n");
128                 if(kb)
129                 {
130                         bundle_free(kb); 
131                         kb=NULL;
132                 }
133                 return 0;
134         }
135         else{
136                 printf("open service fail\n");
137                 if(kb)
138                 {
139                         bundle_free(kb); 
140                         kb=NULL;
141                 }
142                 return -1;              
143         }
144 }
145
146 static void prt_recvd_bundle(const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
147 {
148         char **array_val;
149         int array_len;
150         size_t *array_item_size;
151
152         char *val;
153         size_t *size;
154         int i;
155         
156         if(bundle_keyval_type_is_array(kv) > 0) {
157                 bundle_keyval_get_array_val(kv, &array_val, &array_len, &array_item_size);
158                 
159                 for (i=0;i<array_len;i++)
160                 {
161                         printf("recvd - key: %s[%d], value: %s\n", key, i, array_val[i]);
162                 }
163                 
164         } else {
165                 bundle_keyval_get_basic_val(kv, &val, &size);
166                 printf("recvd - key: %s, value: %s\n",key,val);
167         }       
168 }
169
170 static void cb_func(bundle *kb, int request_code, appsvc_result_val result, void *data)
171 {
172         int num;
173         num = (int)data;
174         
175         if(result == APPSVC_RES_CANCEL){
176                 printf("==== %d : canceled(preemptted) my request ===\n",num);
177         }
178         else{
179                 printf("==== %d : result packet === result %d\n",num, (int)result);
180                 //bundle_iterate(kb, prt_recvd_bundle, NULL);
181
182                 bundle_foreach(kb, prt_recvd_bundle, NULL);
183         }       
184
185
186         if(strcmp(cmd,"run_svc_res")==0){
187                 printf("==== end of appsvc_run() ====\n");
188                 ecore_main_loop_quit();
189         }
190 }
191
192 int run_svc_res()
193 {
194         static int num=0;
195         int ret;
196         int opt;
197         char *op = NULL;
198         char *mime = NULL;
199         char *uri = NULL;
200         char* key = NULL;
201         char* val = NULL;
202
203         bundle *kb=NULL;
204         kb = bundle_create();
205         if(kb == NULL)
206         {
207                 printf("bundle creation fail\n");
208                 return -1;
209         }
210         
211         printf("[run_svc_res test]\n");
212         
213         __set_bundle_from_args(kb);     
214         
215         ret = appsvc_run_service(kb, 0, cb_func, (void*)num);
216         
217         if(ret >= 0){
218                 printf("open service success\n");
219                 if(kb)
220                 {
221                         bundle_free(kb); 
222                         kb=NULL;
223                 }
224                 return 0;
225         }
226         else{
227                 printf("open service fail\n");
228                 if(kb)
229                 {
230                         bundle_free(kb); 
231                         kb=NULL;
232                 }
233                 return -1;              
234         }
235 }
236
237
238 static test_func_t test_func[] = {
239
240
241         {"run_svc", run_svc, "run_svc test",
242                 "[usage] run_svc -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
243         {"run_svc_res", run_svc_res, "run_svc_res test",
244                 "[usage] run_svc_res -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
245                 
246 };
247
248 int callfunc(char* testname)
249 {
250         test_func_t *tmp;
251         int res;
252         int i;
253
254         for(i=0; i<sizeof(test_func)/sizeof(test_func_t); i++)
255         {
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
271 void print_usage(char* progname)
272 {
273         test_func_t *tmp;
274         int i;
275         
276         printf("[usage] %s <cmd> ...\n",progname);
277         printf(" - available cmd list\n");
278
279         for(i=0; i<sizeof(test_func)/sizeof(test_func_t); i++)
280         {
281                 tmp = &test_func[i];
282                 printf("\t%s : %s\n",tmp->name, tmp->desc);
283                 printf("\t\t%s\n",tmp->usage);
284         }
285
286 }
287
288
289 static Eina_Bool run_func(void *data)
290 {
291         callfunc(cmd);
292
293         if(strcmp(cmd,"run_svc_res") == 0 )
294                 return 0;
295         else
296                 ecore_main_loop_quit();
297
298         return 0;
299 }
300
301
302 int main(int argc, char** argv)
303 {
304         if(argc < 3){
305                 print_usage(argv[0]);
306                 exit(0);
307         }
308         
309         ecore_init();
310
311         cmd = argv[1];
312         gargc = argc;
313         gargv = argv;
314         apn_pid = atoi(argv[2]);
315         
316         aul_launch_init(NULL,NULL); 
317
318         //aul_listen_app_dead_signal(dead_tracker,NULL);
319         //aul_listen_app_dead_signal(NULL,NULL);
320
321         ecore_idler_add(run_func, NULL);
322         
323         ecore_main_loop_begin();
324                                         
325         return 0;
326 }
327
328