0.9.7 release -- bugfix: a package test stops on the way
[profile/ivi/ico-uxf-utilities.git] / test / tst_ico_uws_client.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   test client (socket library for communicate)
11  *
12  * @date    June-7-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <glib.h>
21 #include "ico_uws.h"
22
23 #include "tst_ico_uws.h"
24
25 /* ----------------------------------------------- */
26 /* Variable                                        */
27 /* ----------------------------------------------- */
28 #define SLEEP_TIME  2
29 #define RETRY_NUM   10
30
31 /* context */
32 static struct ico_uws_context *clt_context;
33 static void *clt_id;
34
35 /* receive event check */
36 static int receive_flag = UNSET_FLAG;
37
38 /* callback function is setting or not setting */
39 static int set_cb_flag = UNSET_FLAG;
40 static int num_call_cb = 0;
41
42 /* ----------------------------------------------- */
43 /* Define of static function                       */
44 /* ----------------------------------------------- */
45 static void tst_uws_callback(const struct ico_uws_context *context,
46                              const ico_uws_evt_e event,
47                              const void *id,
48                              const ico_uws_detail *detail,
49                              void *user_data);
50 static void tst_create_context(char *uri);
51 static void tst_get_ready_state(int state, char *str_state);
52 static void tst_get_uri(char *set_uri);
53 static void tst_send(unsigned char *data);
54 static void tst_service(void);
55 static void tst_close(void);
56 static void tst_set_evt_callback(unsigned char *send_data);
57 static void tst_unset_evt_callback(void);
58 static int ico_uws_client_test(char *uri, unsigned char *data);
59
60 /* ----------------------------------------------- */
61 /* Public API Test                                 */
62 /* ----------------------------------------------- */
63 /* event callback */
64 static void
65 tst_uws_callback(const struct ico_uws_context *context,
66                  const ico_uws_evt_e event,
67                  const void *id,
68                  const ico_uws_detail *detail,
69                  void *user_data)
70 {
71     char str[256];
72     char *ret_str;
73
74     num_call_cb++;
75     if (set_cb_flag == SET_FLAG) {
76         ret_str = TEST_OK;
77     }
78     else {
79         ret_str = TEST_NG;
80     }
81
82     /* set id */
83     clt_id = (void *)id;
84
85     switch (event) {
86     case ICO_UWS_EVT_OPEN:
87         sprintf(str, "open");
88         if (clt_context != NULL) {
89             tst_get_ready_state(ICO_UWS_STATE_OPEN, "open");
90         }
91         break;
92     case ICO_UWS_EVT_ERROR:
93         sprintf(str, "error");
94         if (detail->_ico_uws_error.code == ICO_UWS_ERR_SEND) {
95             dbg_print("ico_uws_service (client) : %s\n", TEST_NG);
96             dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
97             receive_flag = SET_FLAG;
98         }
99         break;
100     case ICO_UWS_EVT_CLOSE:
101         sprintf(str, "close");
102         if (clt_context != NULL) {
103             tst_get_ready_state(ICO_UWS_STATE_CLOSED, "close");
104         }
105         break;
106     case ICO_UWS_EVT_RECEIVE:
107         sprintf(str, "receive");
108         char *data = (char *)detail->_ico_uws_message.recv_data;
109         if (strcmp((char *)user_data, data) != 0) {
110             dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
111         } else {
112             dbg_print("ico_uws_send (client) : %s\n", TEST_OK);
113         }
114         sprintf(str, "%s '%s'", str, data);
115         receive_flag = SET_FLAG;
116         break;
117     case ICO_UWS_EVT_ADD_FD:
118         sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
119         break;
120     case ICO_UWS_EVT_DEL_FD:
121         sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
122         break;
123     default:
124         /* other event is not test */
125         break;
126     }
127     dbg_print("ico_uws_evt_cb [%d (%s)] (client) : %s\n",
128               event, str, ret_str);
129
130     return;
131 }
132
133 /* create context */
134 static void
135 tst_create_context(char *uri)
136 {
137     char *ret_str = TEST_OK;
138     int id;
139
140     clt_context = ico_uws_create_context(uri, PROTOCOL_NAME);
141     if (clt_context == NULL) {
142         ret_str = TEST_NG;
143         for (id = 0; id < RETRY_NUM; id++) {
144             clt_context = ico_uws_create_context(uri, PROTOCOL_NAME);
145             if (clt_context != NULL) {
146                 ret_str = TEST_OK;
147                 break;
148             }
149             sleep(0.01);
150         }
151     }
152     dbg_print("ico_uws_create_context (client) : %s\n", ret_str);
153
154     return;
155 }
156
157 /* get ready state */
158 static void
159 tst_get_ready_state(int state, char *str_state)
160 {
161     char *ret_str = TEST_OK;
162
163     ico_uws_state_e cur_state = ico_uws_get_ready_state(clt_context);
164     if (cur_state != state) {
165         ret_str = TEST_NG;
166     }
167     dbg_print("ico_uws_get_ready_state [%s] (client) : %s\n",
168               str_state, ret_str);
169
170     return;
171 }
172
173 /* get uri */
174 static void
175 tst_get_uri(char *set_uri)
176 {
177     char *ret_str = TEST_OK;
178
179     char *uri = ico_uws_get_uri(clt_context);
180     if (strcmp(uri, set_uri) != 0) {
181         ret_str = TEST_NG;
182     }
183     dbg_print("ico_uws_get_uri [%s] (client) : %s\n",
184               uri, ret_str);
185
186     return;
187 }
188
189 /* send data */
190 static void
191 tst_send(unsigned char *data)
192 {
193     int i;
194     size_t len = strlen((char *)data) + 1;
195
196     for (i = 0; i < 10; i++) {
197         ico_uws_service(clt_context);
198         usleep(100);
199     }
200     ico_uws_send(clt_context, clt_id, data, len);
201
202     return;
203 }
204
205 /* service loop (wait to receive data) */
206 static void
207 tst_service()
208 {
209     char *ret_str = TEST_OK;
210
211     /* wait to close the connection */
212     while (receive_flag == UNSET_FLAG) {
213         ico_uws_service(clt_context);
214         usleep(50);
215     }
216     receive_flag = UNSET_FLAG;
217     dbg_print("ico_uws_service (client) : %s\n", ret_str);
218
219     return;
220 }
221
222 /* close */
223 static void
224 tst_close()
225 {
226     char *ret_str = TEST_OK;
227
228     ico_uws_close(clt_context);
229     dbg_print("ico_uws_close (client) : %s\n", ret_str);
230
231     return;
232 }
233
234 /* set callback */
235 static void
236 tst_set_evt_callback(unsigned char *send_data)
237 {
238     int ret;
239     char *ret_str = TEST_OK;
240
241     /* set callback */
242     set_cb_flag = SET_FLAG;
243     ret = ico_uws_set_event_cb(clt_context, tst_uws_callback,
244                                (void *)send_data);
245     if (ret != ICO_UWS_ERR_NONE) {
246         ret_str = TEST_NG;
247         dbg_print("ico_uws_set_event_cb (client) : %s (%d)\n",
248                   ret_str, ret);
249         return;
250     }
251
252     dbg_print("ico_uws_set_event_cb (client) : %s\n", ret_str);
253
254     return;
255 }
256
257 /* unset callback */
258 static void
259 tst_unset_evt_callback()
260 {
261     char *ret_str = TEST_OK;
262     char *uri;
263
264     /* unset callback */
265     ico_uws_unset_event_cb(clt_context);
266     set_cb_flag = UNSET_FLAG;
267     num_call_cb = 0;
268
269     /* occurs the error event */
270     printf("-- Occurs the error event to test unset_event_cb\n");
271     uri = ico_uws_get_uri(NULL);
272     if (uri == NULL) {
273         printf("-- Error event happened. (ico_uws_get_uri return Errror)\n");
274     }
275
276     sleep(SLEEP_TIME);
277     if (num_call_cb > 0) {
278         ret_str = TEST_NG;
279     }
280
281     dbg_print("ico_uws_unset_event_cb (client) : %s\n", ret_str);
282
283     return;
284 }
285
286 /* test main (to connect to single server) */
287 static int
288 ico_uws_client_test(char *uri, unsigned char *data)
289 {
290     /* create context */
291     tst_create_context(uri);
292
293     /* set callback */
294     tst_set_evt_callback(data);
295
296     /* interval */
297     sleep(SLEEP_TIME);
298
299     if (clt_context) {
300         /* get uri */
301         tst_get_uri(uri);
302
303         /* send data */
304         tst_send(data);
305
306         /* wait to receive data */
307         tst_service();
308
309         /* interval */
310         sleep(SLEEP_TIME);
311
312         /* unset callback */
313         tst_unset_evt_callback();
314
315         /* close */
316         tst_close();
317     }
318
319     return 1;
320 }
321
322 /* ----------------------------------------------- */
323 /* Main                                            */
324 /* ----------------------------------------------- */
325 static GMainLoop *g_mainloop = NULL;
326
327 static gboolean
328 exit_program(gpointer data)
329 {
330     g_main_loop_quit(g_mainloop);
331
332     return FALSE;
333 }
334
335 /* main */
336 int
337 main(int argc, char **argv)
338 {
339     char uri[128];
340     unsigned char data[256];
341     int id;
342     int set_uri_flag = UNSET_FLAG;
343     int set_data_flag = UNSET_FLAG;
344
345     for (id = 0; id < argc; id++) {
346         if (strcmp(argv[id], "-p") == 0) {
347             /* set uri to connect */
348             id++;
349             sprintf(uri, "%s:%s", SRV_URI, argv[id]);
350             set_uri_flag = SET_FLAG;
351         }
352         else if (strcmp(argv[id], "-d") == 0) {
353             /* set data to send */
354             id++;
355             sprintf((char *)data, "%s", argv[id]);
356             data[strlen(argv[id]) + 1] = '\0';
357             set_data_flag = SET_FLAG;
358         }
359     }
360
361     /* set default uri to connect */
362     if (set_uri_flag == UNSET_FLAG) {
363         sprintf(uri, "%s:%s", SRV_URI, SRV_PORT);
364     }
365
366     /* set default data to send */
367     if (set_data_flag == UNSET_FLAG) {
368         sprintf((char *)data, "%s", CLT_DATA);
369     }
370
371     g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_client", 1);
372     g_mainloop = g_main_loop_new(NULL, 0);
373
374     printf("\n");
375     printf("##### ico_uws API (client) Test Start #####\n");
376     ico_uws_client_test(uri, data);
377     printf("##### ico_uws API (client) Test End #####\n");
378     printf("\n");
379
380     g_timeout_add_seconds(2, exit_program, NULL);
381     g_main_loop_run(g_mainloop);
382
383     return 0;
384 }