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