summaryrefslogtreecommitdiff
path: root/test/tst_ico_uws_server.c
blob: 28d5c403bf866e5a4654d90b4f5b1958b423994e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
/**
 * @brief   test server (socket library for communicate)
 *
 * @date    June-7-2013
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#include "ico_uws.h"

#include "tst_ico_uws.h"

/* ----------------------------------------------- */
/* Variable                                        */
/* ----------------------------------------------- */
#define SLEEP_TIME  2

/* context */
static struct ico_uws_context *context;

/* close event check */
static int close_flag = UNSET_FLAG;
static int num_connect = 0;

/* callback function is setting or not setting */
static int set_cb_flag = UNSET_FLAG;
static int num_call_cb = 0;

/* ----------------------------------------------- */
/* Define of static function                       */
/* ----------------------------------------------- */
static void tst_uws_callback(const struct ico_uws_context *context,
                             const ico_uws_evt_e event,
                             const void *id,
                             const ico_uws_detail *detail,
                             void *user_data);
static void tst_create_context(char *uri);
static void tst_get_ready_state(int state, char *str_state);
static void tst_get_uri(char *set_uri);
static void tst_service(void);
static void tst_close(void);
static void tst_set_evt_callback(void);
static void tst_unset_evt_callback(void);
static int ico_uws_server_test(char *uri);

/* ----------------------------------------------- */
/* Public API Test                                 */
/* ----------------------------------------------- */
/* event callback */
static void
tst_uws_callback(const struct ico_uws_context *context,
                 const ico_uws_evt_e event,
                 const void *id,
                 const ico_uws_detail *detail,
                 void *user_data)
{
    char str[256];
    char *ret_str;

    num_call_cb++;
    if (set_cb_flag == SET_FLAG) {
        ret_str = TEST_OK;
    }
    else {
        ret_str = TEST_NG;
    }

    switch (event) {
    case ICO_UWS_EVT_OPEN:
        sprintf(str, "open");
        num_connect++;
        if (context != NULL) {
            tst_get_ready_state(ICO_UWS_STATE_OPEN, "open");
        }
        break;
    case ICO_UWS_EVT_ERROR:
        sprintf(str, "error");
        break;
    case ICO_UWS_EVT_CLOSE:
        sprintf(str, "close");
        num_connect--;
        if (num_connect == 0) {
            close_flag = SET_FLAG;
        }
        if (context != NULL) {
            tst_get_ready_state(ICO_UWS_STATE_CLOSED, "close");
        }
        break;
    case ICO_UWS_EVT_RECEIVE:
        sprintf(str, "receive");
        ico_uws_send((struct ico_uws_context *)context,
                     (void *)id,
                     (unsigned char *)detail->_ico_uws_message.recv_data,
                     detail->_ico_uws_message.recv_len);
        sprintf(str, "%s '%s'",
                str, (unsigned char *)detail->_ico_uws_message.recv_data);
        break;
    case ICO_UWS_EVT_ADD_FD:
        sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
        break;
    case ICO_UWS_EVT_DEL_FD:
        sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
        break;
    default:
        /* other event is not test */
        break;
    }
    dbg_print("ico_uws_evt_cb [%d (%s)] (server) : %s\n",
              event, str, ret_str);

    return;
}

/* create context */
static void
tst_create_context(char *uri)
{
    char *ret_str = TEST_OK;

    context = ico_uws_create_context(uri, PROTOCOL_NAME);
    if (context == NULL) {
        ret_str = TEST_NG;
    }
    num_connect = 0;
    dbg_print("ico_uws_create_context (server) : %s\n", ret_str);

    return;
}

/* get ready state */
static void
tst_get_ready_state(int state, char *str_state)
{
    char *ret_str = TEST_OK;

    ico_uws_state_e cur_state = ico_uws_get_ready_state(context);
    if (cur_state != state) {
        ret_str = TEST_NG;
    }
    dbg_print("ico_uws_get_ready_state [%s] (server) : %s\n",
              str_state, ret_str);

    return;
}

/* get uri */
static void
tst_get_uri(char *set_uri)
{
    char *ret_str = TEST_OK;

    char *uri = ico_uws_get_uri(context);
    if (strcmp(uri, set_uri) != 0) {
        ret_str = TEST_NG;
    }
    dbg_print("ico_uws_get_uri [%s] (server) : %s\n",
              uri, ret_str);

    return;
}

/* service loop */
static void
tst_service()
{
    char *ret_str = TEST_OK;

    close_flag = UNSET_FLAG;
    /* wait to close the connection */
    while (close_flag == UNSET_FLAG) {
        ico_uws_service(context);
        usleep(50);
    }
    dbg_print("ico_uws_service (server) : %s\n", ret_str);

    return;
}

/* close */
static void
tst_close()
{
    char *ret_str = TEST_OK;

    ico_uws_close(context);
    dbg_print("ico_uws_close (server) : %s\n", ret_str);

    return;
}

/* set callback */
static void
tst_set_evt_callback()
{
    int ret;
    char *ret_str = TEST_OK;
    
    /* set callback */
    set_cb_flag = SET_FLAG;
    ret = ico_uws_set_event_cb(context, tst_uws_callback, NULL);
    if (ret != ICO_UWS_ERR_NONE) {
        ret_str = TEST_NG;
        dbg_print("ico_uws_set_event_cb (server) : %s (%d)\n",
                  ret_str, ret);
        return;
    }

    dbg_print("ico_uws_set_event_cb (server) : %s\n", ret_str);

    return;
}

/* unset callback */
static void
tst_unset_evt_callback()
{
    char *ret_str = TEST_OK;
    
    /* unset callback */
    ico_uws_unset_event_cb(context);
    set_cb_flag = UNSET_FLAG;
    num_call_cb = 0;

    /* occurs the error event */
    (void)ico_uws_get_uri(NULL);
    sleep(SLEEP_TIME);
    if (num_call_cb > 0) {
        ret_str = TEST_NG;
    }

    dbg_print("ico_uws_unset_event_cb (server) : %s\n", ret_str);

    return;
}

/* test main (server) */
static int
ico_uws_server_test(char *uri)
{
    /* create context */
    tst_create_context(uri);

    if (context) {
        /* set callback */
        tst_set_evt_callback();

        /* client does not connect */
        tst_get_ready_state(ICO_UWS_STATE_CONNECTING, "connecting");

        /* get uri */
        tst_get_uri(uri);

        /* service (loop) */
        tst_service();

        /* interval */
        sleep(SLEEP_TIME);

        /* unset callback */
        tst_unset_evt_callback();

        /* close */
        tst_close();
    }

    return 1;
}

/* ----------------------------------------------- */
/* Main                                            */
/* ----------------------------------------------- */
static GMainLoop *g_mainloop = NULL;

static gboolean
exit_program(gpointer data)
{
    g_main_loop_quit(g_mainloop);

    return FALSE;
}

/* main */
int
main(int argc, char **argv)
{
    char uri[256];
    int id;
    int set_uri_flag = UNSET_FLAG;

    for (id = 0; id < argc; id++) {
        if (strcmp(argv[id], "-p") == 0) {
            id++;
            sprintf(uri, ":%s", argv[id]);
            set_uri_flag = SET_FLAG;
        }
    }

    /* set default uri */
    if (set_uri_flag == UNSET_FLAG) {
        sprintf(uri, ":%s", SRV_PORT);
    }

    g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_server", 1);
    g_mainloop = g_main_loop_new(NULL, 0);

    printf("\n");
    printf("##### ico_uws API (server) Test Start #####\n");
    ico_uws_server_test(uri);
    printf("##### ico_uws API (server) Test End #####\n");
    printf("\n");

    g_timeout_add_seconds(2, exit_program, NULL);
    g_main_loop_run(g_mainloop);

    return 0;
}