Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/ioq_tcp.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21 /**
22 * \page page_pjlib_ioqueue_tcp_test Test: I/O Queue (TCP)
23 *
24 * This file provides implementation to test the
25 * functionality of the I/O queue when TCP socket is used.
26 *
27 *
28 * This file is <b>pjlib-test/ioq_tcp.c</b>
29 *
30 * \include pjlib-test/ioq_tcp.c
31 */
32
33
34 #if INCLUDE_TCP_IOQUEUE_TEST
35
36 #include <pjlib.h>
37
38 #if PJ_HAS_TCP
39
40 #define THIS_FILE "test_tcp"
41 #define NON_EXISTANT_PORT 50123
42 #define LOOP 100
43 #define BUF_MIN_SIZE 32
44 #define BUF_MAX_SIZE 2048
45 #define SOCK_INACTIVE_MIN (4-2)
46 #define SOCK_INACTIVE_MAX (PJ_IOQUEUE_MAX_HANDLES - 2)
47 #define POOL_SIZE (2*BUF_MAX_SIZE + SOCK_INACTIVE_MAX*128 + 2048)
48
49 static pj_ssize_t callback_read_size,
50 callback_write_size,
51 callback_accept_status,
52 callback_connect_status;
53 static unsigned callback_call_count;
54 static pj_ioqueue_key_t *callback_read_key,
55 *callback_write_key,
56 *callback_accept_key,
57 *callback_connect_key;
58 static pj_ioqueue_op_key_t *callback_read_op,
59 *callback_write_op,
60 *callback_accept_op;
61
62 static void on_ioqueue_read(pj_ioqueue_key_t *key,
63 pj_ioqueue_op_key_t *op_key,
64 pj_ssize_t bytes_read)
65 {
66 callback_read_key = key;
67 callback_read_op = op_key;
68 callback_read_size = bytes_read;
69 callback_call_count++;
70 }
71
72 static void on_ioqueue_write(pj_ioqueue_key_t *key,
73 pj_ioqueue_op_key_t *op_key,
74 pj_ssize_t bytes_written)
75 {
76 callback_write_key = key;
77 callback_write_op = op_key;
78 callback_write_size = bytes_written;
79 callback_call_count++;
80 }
81
82 static void on_ioqueue_accept(pj_ioqueue_key_t *key,
83 pj_ioqueue_op_key_t *op_key,
84 pj_sock_t sock,
85 int status)
86 {
87 if (sock == PJ_INVALID_SOCKET) {
88
89 if (status != PJ_SUCCESS) {
90 /* Ignore. Could be blocking error */
91 app_perror(".....warning: received error in on_ioqueue_accept() callback",
92 status);
93 } else {
94 callback_accept_status = -61;
95 PJ_LOG(3,("", "..... on_ioqueue_accept() callback was given "
96 "invalid socket and status is %d", status));
97 }
98 } else {
99 pj_sockaddr addr;
100 int client_addr_len;
101
102 client_addr_len = sizeof(addr);
103 status = pj_sock_getsockname(sock, &addr, &client_addr_len);
104 if (status != PJ_SUCCESS) {
105 app_perror("...ERROR in pj_sock_getsockname()", status);
106 }
107
108 callback_accept_key = key;
109 callback_accept_op = op_key;
110 callback_accept_status = status;
111 callback_call_count++;
112 }
113 }
114
115 static void on_ioqueue_connect(pj_ioqueue_key_t *key, int status)
116 {
117 callback_connect_key = key;
118 callback_connect_status = status;
119 callback_call_count++;
120 }
121
122 static pj_ioqueue_callback test_cb =
123 {
124 &on_ioqueue_read,
125 &on_ioqueue_write,
126 &on_ioqueue_accept,
127 &on_ioqueue_connect,
128 };
129
130 static int send_recv_test(pj_ioqueue_t *ioque,
131 pj_ioqueue_key_t *skey,
132 pj_ioqueue_key_t *ckey,
133 void *send_buf,
134 void *recv_buf,
135 pj_ssize_t bufsize,
136 pj_timestamp *t_elapsed)
137 {
138 pj_status_t status;
139 pj_ssize_t bytes;
140 pj_time_val timeout;
141 pj_timestamp t1, t2;
142 int i, pending_op = 0;
143 pj_ioqueue_op_key_t read_op, write_op;
144
145 // Init operation keys.
146 pj_ioqueue_op_key_init(&read_op, sizeof(read_op));
147 pj_ioqueue_op_key_init(&write_op, sizeof(write_op));
148
149 // Start reading on the server side.
150 bytes = bufsize;
151 status = pj_ioqueue_recv(skey, &read_op, recv_buf, &bytes, 0);
152 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
153 app_perror("...pj_ioqueue_recv error", status);
154 return -100;
155 }
156
157 if (status == PJ_EPENDING)
158 ++pending_op;
159 else {
160 /* Does not expect to return error or immediate data. */
161 return -115;
162 }
163
164 // Randomize send buffer.
165 pj_create_random_string((char*)send_buf, bufsize);
166
167 // Starts send on the client side.
168 bytes = bufsize;
169 status = pj_ioqueue_send(ckey, &write_op, send_buf, &bytes, 0);
170 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
171 return -120;
172 }
173 if (status == PJ_EPENDING) {
174 ++pending_op;
175 }
176
177 // Begin time.
178 pj_get_timestamp(&t1);
179
180 // Reset indicators
181 callback_read_size = callback_write_size = 0;
182 callback_read_key = callback_write_key = NULL;
183 callback_read_op = callback_write_op = NULL;
184
185 // Poll the queue until we've got completion event in the server side.
186 status = 0;
187 while (pending_op > 0) {
188 timeout.sec = 1; timeout.msec = 0;
189 #ifdef PJ_SYMBIAN
190 PJ_UNUSED_ARG(ioque);
191 status = pj_symbianos_poll(-1, 1000);
192 #else
193 status = pj_ioqueue_poll(ioque, &timeout);
194 #endif
195 if (status > 0) {
196 if (callback_read_size) {
197 if (callback_read_size != bufsize)
198 return -160;
199 if (callback_read_key != skey)
200 return -161;
201 if (callback_read_op != &read_op)
202 return -162;
203 }
204 if (callback_write_size) {
205 if (callback_write_key != ckey)
206 return -163;
207 if (callback_write_op != &write_op)
208 return -164;
209 }
210 pending_op -= status;
211 }
212 if (status == 0) {
213 PJ_LOG(3,("", "...error: timed out"));
214 }
215 if (status < 0) {
216 return -170;
217 }
218 }
219
220 // Pending op is zero.
221 // Subsequent poll should yield zero too.
222 for (i=0; i<10; ++i) {
223 timeout.sec = 0;
224 timeout.msec = 50;
225 #ifdef PJ_SYMBIAN
226 status = pj_symbianos_poll(-1, 1);
227 #else
228 status = pj_ioqueue_poll(ioque, &timeout);
229 #endif
230 if (status != 0)
231 return -173;
232 }
233
234 // End time.
235 pj_get_timestamp(&t2);
236 t_elapsed->u32.lo += (t2.u32.lo - t1.u32.lo);
237
238 // Compare recv buffer with send buffer.
239 if (pj_memcmp(send_buf, recv_buf, bufsize) != 0) {
240 return -180;
241 }
242
243 // Success
244 return 0;
245 }
246
247
248 /*
249 * Compliance test for success scenario.
250 */
251 static int compliance_test_0(const pj_ioqueue_cfg *cfg)
252 {
253 pj_sock_t ssock=-1, csock0=-1, csock1=-1;
254 pj_sockaddr_in addr, client_addr, rmt_addr;
255 int client_addr_len;
256 pj_pool_t *pool = NULL;
257 char *send_buf, *recv_buf;
258 pj_ioqueue_t *ioque = NULL;
259 pj_ioqueue_key_t *skey=NULL, *ckey0=NULL, *ckey1=NULL;
260 pj_ioqueue_op_key_t accept_op;
261 int bufsize = BUF_MIN_SIZE;
262 int status = -1;
263 int pending_op = 0;
264 pj_timestamp t_elapsed;
265 pj_str_t s;
266 pj_status_t rc;
267
268 // Create pool.
269 pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
270
271 // Allocate buffers for send and receive.
272 send_buf = (char*)pj_pool_alloc(pool, bufsize);
273 recv_buf = (char*)pj_pool_alloc(pool, bufsize);
274
275 // Create server socket and client socket for connecting
276 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &ssock);
277 if (rc != PJ_SUCCESS) {
278 app_perror("...error creating socket", rc);
279 status=-1; goto on_error;
280 }
281
282 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &csock1);
283 if (rc != PJ_SUCCESS) {
284 app_perror("...error creating socket", rc);
285 status=-1; goto on_error;
286 }
287
288 // Bind server socket.
289 pj_sockaddr_in_init(&addr, 0, 0);
290 if ((rc=pj_sock_bind(ssock, &addr, sizeof(addr))) != 0 ) {
291 app_perror("...bind error", rc);
292 status=-10; goto on_error;
293 }
294
295 // Get server address.
296 client_addr_len = sizeof(addr);
297 rc = pj_sock_getsockname(ssock, &addr, &client_addr_len);
298 if (rc != PJ_SUCCESS) {
299 app_perror("...ERROR in pj_sock_getsockname()", rc);
300 status=-15; goto on_error;
301 }
302 addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
303
304 // Create I/O Queue.
305 rc = pj_ioqueue_create2(pool, PJ_IOQUEUE_MAX_HANDLES, cfg, &ioque);
306 if (rc != PJ_SUCCESS) {
307 app_perror("...ERROR in pj_ioqueue_create()", rc);
308 status=-20; goto on_error;
309 }
310
311 // Init operation key.
312 pj_ioqueue_op_key_init(&accept_op, sizeof(accept_op));
313
314 // Register server socket and client socket.
315 rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, &test_cb, &skey);
316 if (rc == PJ_SUCCESS)
317 rc = pj_ioqueue_register_sock(pool, ioque, csock1, NULL, &test_cb,
318 &ckey1);
319 else
320 ckey1 = NULL;
321 if (rc != PJ_SUCCESS) {
322 app_perror("...ERROR in pj_ioqueue_register_sock()", rc);
323 status=-23; goto on_error;
324 }
325
326 // Server socket listen().
327 if (pj_sock_listen(ssock, 5)) {
328 app_perror("...ERROR in pj_sock_listen()", rc);
329 status=-25; goto on_error;
330 }
331
332 // Server socket accept()
333 client_addr_len = sizeof(pj_sockaddr_in);
334 status = pj_ioqueue_accept(skey, &accept_op, &csock0,
335 &client_addr, &rmt_addr, &client_addr_len);
336 if (status != PJ_EPENDING) {
337 app_perror("...ERROR in pj_ioqueue_accept()", rc);
338 status=-30; goto on_error;
339 }
340 if (status==PJ_EPENDING) {
341 ++pending_op;
342 }
343
344 // Client socket connect()
345 status = pj_ioqueue_connect(ckey1, &addr, sizeof(addr));
346 if (status!=PJ_SUCCESS && status != PJ_EPENDING) {
347 app_perror("...ERROR in pj_ioqueue_connect()", rc);
348 status=-40; goto on_error;
349 }
350 if (status==PJ_EPENDING) {
351 ++pending_op;
352 }
353
354 // Poll until connected
355 callback_read_size = callback_write_size = 0;
356 callback_accept_status = callback_connect_status = -2;
357 callback_call_count = 0;
358
359 callback_read_key = callback_write_key =
360 callback_accept_key = callback_connect_key = NULL;
361 callback_accept_op = callback_read_op = callback_write_op = NULL;
362
363 while (pending_op) {
364 pj_time_val timeout = {1, 0};
365
366 #ifdef PJ_SYMBIAN
367 callback_call_count = 0;
368 pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
369 status = callback_call_count;
370 #else
371 status = pj_ioqueue_poll(ioque, &timeout);
372 #endif
373 if (status > 0) {
374 if (callback_accept_status != -2) {
375 if (callback_accept_status != 0) {
376 status=-41; goto on_error;
377 }
378 if (callback_accept_key != skey) {
379 status=-42; goto on_error;
380 }
381 if (callback_accept_op != &accept_op) {
382 status=-43; goto on_error;
383 }
384 callback_accept_status = -2;
385 }
386
387 if (callback_connect_status != -2) {
388 if (callback_connect_status != 0) {
389 status=-50; goto on_error;
390 }
391 if (callback_connect_key != ckey1) {
392 status=-51; goto on_error;
393 }
394 callback_connect_status = -2;
395 }
396
397 if (status > pending_op) {
398 PJ_LOG(3,(THIS_FILE,
399 "...error: pj_ioqueue_poll() returned %d "
400 "(only expecting %d)",
401 status, pending_op));
402 return -52;
403 }
404 pending_op -= status;
405 }
406 }
407
408 // There's no pending operation.
409 // When we poll the ioqueue, there must not be events.
410 if (pending_op == 0) {
411 unsigned i;
412
413 for (i=0; i<10; ++i) {
414 pj_time_val timeout = {0, 50};
415 #ifdef PJ_SYMBIAN
416 status = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
417 #else
418 status = pj_ioqueue_poll(ioque, &timeout);
419 #endif
420 if (status != 0) {
421 status=-60; goto on_error;
422 }
423 }
424 }
425
426 // Check accepted socket.
427 if (csock0 == PJ_INVALID_SOCKET) {
428 status = -69;
429 app_perror("...accept() error", pj_get_os_error());
430 goto on_error;
431 }
432
433 // Register newly accepted socket.
434 rc = pj_ioqueue_register_sock(pool, ioque, csock0, NULL,
435 &test_cb, &ckey0);
436 if (rc != PJ_SUCCESS) {
437 app_perror("...ERROR in pj_ioqueue_register_sock", rc);
438 status = -70;
439 goto on_error;
440 }
441
442 // Test send and receive.
443 t_elapsed.u32.lo = 0;
444 status = send_recv_test(ioque, ckey0, ckey1, send_buf,
445 recv_buf, bufsize, &t_elapsed);
446 if (status != 0) {
447 goto on_error;
448 }
449
450 // Success
451 status = 0;
452
453 on_error:
454 if (skey != NULL)
455 pj_ioqueue_unregister(skey);
456 else if (ssock != PJ_INVALID_SOCKET)
457 pj_sock_close(ssock);
458
459 if (ckey1 != NULL)
460 pj_ioqueue_unregister(ckey1);
461 else if (csock1 != PJ_INVALID_SOCKET)
462 pj_sock_close(csock1);
463
464 if (ckey0 != NULL)
465 pj_ioqueue_unregister(ckey0);
466 else if (csock0 != PJ_INVALID_SOCKET)
467 pj_sock_close(csock0);
468
469 if (ioque != NULL)
470 pj_ioqueue_destroy(ioque);
471 pj_pool_release(pool);
472 return status;
473
474 }
475
476 /*
477 * Compliance test for failed scenario.
478 * In this case, the client connects to a non-existant service.
479 */
480 static int compliance_test_1(const pj_ioqueue_cfg *cfg)
481 {
482 pj_sock_t csock1=PJ_INVALID_SOCKET;
483 pj_sockaddr_in addr;
484 pj_pool_t *pool = NULL;
485 pj_ioqueue_t *ioque = NULL;
486 pj_ioqueue_key_t *ckey1 = NULL;
487 int status = -1;
488 int pending_op = 0;
489 pj_str_t s;
490 pj_status_t rc;
491
492 // Create pool.
493 pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
494
495 // Create I/O Queue.
496 rc = pj_ioqueue_create2(pool, PJ_IOQUEUE_MAX_HANDLES, cfg, &ioque);
497 if (!ioque) {
498 status=-20; goto on_error;
499 }
500
501 // Create client socket
502 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &csock1);
503 if (rc != PJ_SUCCESS) {
504 app_perror("...ERROR in pj_sock_socket()", rc);
505 status=-1; goto on_error;
506 }
507
508 // Register client socket.
509 rc = pj_ioqueue_register_sock(pool, ioque, csock1, NULL,
510 &test_cb, &ckey1);
511 if (rc != PJ_SUCCESS) {
512 app_perror("...ERROR in pj_ioqueue_register_sock()", rc);
513 status=-23; goto on_error;
514 }
515
516 // Initialize remote address.
517 pj_sockaddr_in_init(&addr, pj_cstr(&s, "127.0.0.1"), NON_EXISTANT_PORT);
518
519 // Client socket connect()
520 status = pj_ioqueue_connect(ckey1, &addr, sizeof(addr));
521 if (status==PJ_SUCCESS) {
522 // unexpectedly success!
523 status = -30;
524 goto on_error;
525 }
526 if (status != PJ_EPENDING) {
527 // success
528 } else {
529 ++pending_op;
530 }
531
532 callback_connect_status = -2;
533 callback_connect_key = NULL;
534
535 // Poll until we've got result
536 while (pending_op) {
537 pj_time_val timeout = {1, 0};
538
539 #ifdef PJ_SYMBIAN
540 callback_call_count = 0;
541 pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
542 status = callback_call_count;
543 #else
544 status = pj_ioqueue_poll(ioque, &timeout);
545 #endif
546 if (status > 0) {
547 if (callback_connect_key==ckey1) {
548 if (callback_connect_status == 0) {
549 // unexpectedly connected!
550 status = -50;
551 goto on_error;
552 }
553 }
554
555 if (status > pending_op) {
556 PJ_LOG(3,(THIS_FILE,
557 "...error: pj_ioqueue_poll() returned %d "
558 "(only expecting %d)",
559 status, pending_op));
560 return -552;
561 }
562
563 pending_op -= status;
564 if (pending_op == 0) {
565 status = 0;
566 }
567 }
568 }
569
570 // There's no pending operation.
571 // When we poll the ioqueue, there must not be events.
572 if (pending_op == 0) {
573 unsigned i;
574
575 for (i=0; i<10; ++i) {
576 pj_time_val timeout = {0, 50};
577 #ifdef PJ_SYMBIAN
578 status = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
579 #else
580 status = pj_ioqueue_poll(ioque, &timeout);
581 #endif
582 if (status != 0) {
583 status=-60; goto on_error;
584 }
585 }
586 }
587
588 // Success
589 status = 0;
590
591 on_error:
592 if (ckey1 != NULL)
593 pj_ioqueue_unregister(ckey1);
594 else if (csock1 != PJ_INVALID_SOCKET)
595 pj_sock_close(csock1);
596
597 if (ioque != NULL)
598 pj_ioqueue_destroy(ioque);
599 pj_pool_release(pool);
600 return status;
601 }
602
603
604 /*
605 * Repeated connect/accept on the same listener socket.
606 */
607 static int compliance_test_2(const pj_ioqueue_cfg *cfg)
608 {
609 #if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
610 enum { MAX_PAIR = 1, TEST_LOOP = 2 };
611 #else
612 enum { MAX_PAIR = 4, TEST_LOOP = 2 };
613 #endif
614
615 struct listener
616 {
617 pj_sock_t sock;
618 pj_ioqueue_key_t *key;
619 pj_sockaddr_in addr;
620 int addr_len;
621 } listener;
622
623 struct server
624 {
625 pj_sock_t sock;
626 pj_ioqueue_key_t *key;
627 pj_sockaddr_in local_addr;
628 pj_sockaddr_in rem_addr;
629 int rem_addr_len;
630 pj_ioqueue_op_key_t accept_op;
631 } server[MAX_PAIR];
632
633 struct client
634 {
635 pj_sock_t sock;
636 pj_ioqueue_key_t *key;
637 } client[MAX_PAIR];
638
639 pj_pool_t *pool = NULL;
640 char *send_buf, *recv_buf;
641 pj_ioqueue_t *ioque = NULL;
642 unsigned i, bufsize = BUF_MIN_SIZE;
643 int status;
644 int test_loop, pending_op = 0;
645 pj_timestamp t_elapsed;
646 pj_str_t s;
647 pj_status_t rc;
648
649 listener.sock = PJ_INVALID_SOCKET;
650 listener.key = NULL;
651
652 for (i=0; i<MAX_PAIR; ++i) {
653 server[i].sock = PJ_INVALID_SOCKET;
654 server[i].key = NULL;
655 }
656
657 for (i=0; i<MAX_PAIR; ++i) {
658 client[i].sock = PJ_INVALID_SOCKET;
659 client[i].key = NULL;
660 }
661
662 // Create pool.
663 pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
664
665
666 // Create I/O Queue.
667 rc = pj_ioqueue_create2(pool, PJ_IOQUEUE_MAX_HANDLES, cfg, &ioque);
668 if (rc != PJ_SUCCESS) {
669 app_perror("...ERROR in pj_ioqueue_create()", rc);
670 return -10;
671 }
672
673
674 // Allocate buffers for send and receive.
675 send_buf = (char*)pj_pool_alloc(pool, bufsize);
676 recv_buf = (char*)pj_pool_alloc(pool, bufsize);
677
678 // Create listener socket
679 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &listener.sock);
680 if (rc != PJ_SUCCESS) {
681 app_perror("...error creating socket", rc);
682 status=-20; goto on_error;
683 }
684
685 // Bind listener socket.
686 pj_sockaddr_in_init(&listener.addr, 0, 0);
687 if ((rc=pj_sock_bind(listener.sock, &listener.addr, sizeof(listener.addr))) != 0 ) {
688 app_perror("...bind error", rc);
689 status=-30; goto on_error;
690 }
691
692 // Get listener address.
693 listener.addr_len = sizeof(listener.addr);
694 rc = pj_sock_getsockname(listener.sock, &listener.addr, &listener.addr_len);
695 if (rc != PJ_SUCCESS) {
696 app_perror("...ERROR in pj_sock_getsockname()", rc);
697 status=-40; goto on_error;
698 }
699 listener.addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
700
701
702 // Register listener socket.
703 rc = pj_ioqueue_register_sock(pool, ioque, listener.sock, NULL, &test_cb,
704 &listener.key);
705 if (rc != PJ_SUCCESS) {
706 app_perror("...ERROR", rc);
707 status=-50; goto on_error;
708 }
709
710
711 // Listener socket listen().
712 if (pj_sock_listen(listener.sock, 5)) {
713 app_perror("...ERROR in pj_sock_listen()", rc);
714 status=-60; goto on_error;
715 }
716
717
718 for (test_loop=0; test_loop < TEST_LOOP; ++test_loop) {
719 // Client connect and server accept.
720 for (i=0; i<MAX_PAIR; ++i) {
721 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &client[i].sock);
722 if (rc != PJ_SUCCESS) {
723 app_perror("...error creating socket", rc);
724 status=-70; goto on_error;
725 }
726
727 rc = pj_ioqueue_register_sock(pool, ioque, client[i].sock, NULL,
728 &test_cb, &client[i].key);
729 if (rc != PJ_SUCCESS) {
730 app_perror("...error ", rc);
731 status=-80; goto on_error;
732 }
733
734 // Server socket accept()
735 pj_ioqueue_op_key_init(&server[i].accept_op,
736 sizeof(server[i].accept_op));
737 server[i].rem_addr_len = sizeof(pj_sockaddr_in);
738 status = pj_ioqueue_accept(listener.key, &server[i].accept_op,
739 &server[i].sock, &server[i].local_addr,
740 &server[i].rem_addr,
741 &server[i].rem_addr_len);
742 if (status!=PJ_SUCCESS && status != PJ_EPENDING) {
743 app_perror("...ERROR in pj_ioqueue_accept()", rc);
744 status=-90; goto on_error;
745 }
746 if (status==PJ_EPENDING) {
747 ++pending_op;
748 }
749
750
751 // Client socket connect()
752 status = pj_ioqueue_connect(client[i].key, &listener.addr,
753 sizeof(listener.addr));
754 if (status!=PJ_SUCCESS && status != PJ_EPENDING) {
755 app_perror("...ERROR in pj_ioqueue_connect()", rc);
756 status=-100; goto on_error;
757 }
758 if (status==PJ_EPENDING) {
759 ++pending_op;
760 }
761
762 // Poll until connection of this pair established
763 while (pending_op) {
764 pj_time_val timeout = {1, 0};
765
766 #ifdef PJ_SYMBIAN
767 status = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
768 #else
769 status = pj_ioqueue_poll(ioque, &timeout);
770 #endif
771 if (status > 0) {
772 if (status > pending_op) {
773 PJ_LOG(3,(THIS_FILE,
774 "...error: pj_ioqueue_poll() returned %d "
775 "(only expecting %d)",
776 status, pending_op));
777 return -110;
778 }
779 pending_op -= status;
780
781 if (pending_op == 0) {
782 status = 0;
783 }
784 }
785 }
786 }
787
788 // There's no pending operation.
789 // When we poll the ioqueue, there must not be events.
790 if (pending_op == 0) {
791 for (i=0; i<10; ++i) {
792 pj_time_val timeout = {0, 50};
793 #ifdef PJ_SYMBIAN
794 status = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
795 #else
796 status = pj_ioqueue_poll(ioque, &timeout);
797 #endif
798 if (status != 0) {
799 status=-120; goto on_error;
800 }
801 }
802 }
803
804 for (i=0; i<MAX_PAIR; ++i) {
805 // Check server socket.
806 if (server[i].sock == PJ_INVALID_SOCKET) {
807 status = -130;
808 app_perror("...accept() error", pj_get_os_error());
809 goto on_error;
810 }
811
812 // Check addresses
813 if (server[i].local_addr.sin_family != pj_AF_INET() ||
814 server[i].local_addr.sin_addr.s_addr == 0 ||
815 server[i].local_addr.sin_port == 0)
816 {
817 app_perror("...ERROR address not set", rc);
818 status = -140;
819 goto on_error;
820 }
821
822 if (server[i].rem_addr.sin_family != pj_AF_INET() ||
823 server[i].rem_addr.sin_addr.s_addr == 0 ||
824 server[i].rem_addr.sin_port == 0)
825 {
826 app_perror("...ERROR address not set", rc);
827 status = -150;
828 goto on_error;
829 }
830
831
832 // Register newly accepted socket.
833 rc = pj_ioqueue_register_sock(pool, ioque, server[i].sock, NULL,
834 &test_cb, &server[i].key);
835 if (rc != PJ_SUCCESS) {
836 app_perror("...ERROR in pj_ioqueue_register_sock", rc);
837 status = -160;
838 goto on_error;
839 }
840
841 // Test send and receive.
842 t_elapsed.u32.lo = 0;
843 status = send_recv_test(ioque, server[i].key, client[i].key,
844 send_buf, recv_buf, bufsize, &t_elapsed);
845 if (status != 0) {
846 goto on_error;
847 }
848 }
849
850 // Success
851 status = 0;
852
853 for (i=0; i<MAX_PAIR; ++i) {
854 if (server[i].key != NULL) {
855 pj_ioqueue_unregister(server[i].key);
856 server[i].key = NULL;
857 server[i].sock = PJ_INVALID_SOCKET;
858 } else if (server[i].sock != PJ_INVALID_SOCKET) {
859 pj_sock_close(server[i].sock);
860 server[i].sock = PJ_INVALID_SOCKET;
861 }
862
863 if (client[i].key != NULL) {
864 pj_ioqueue_unregister(client[i].key);
865 client[i].key = NULL;
866 client[i].sock = PJ_INVALID_SOCKET;
867 } else if (client[i].sock != PJ_INVALID_SOCKET) {
868 pj_sock_close(client[i].sock);
869 client[i].sock = PJ_INVALID_SOCKET;
870 }
871 }
872 }
873
874 status = 0;
875
876 on_error:
877 for (i=0; i<MAX_PAIR; ++i) {
878 if (server[i].key != NULL) {
879 pj_ioqueue_unregister(server[i].key);
880 server[i].key = NULL;
881 server[i].sock = PJ_INVALID_SOCKET;
882 } else if (server[i].sock != PJ_INVALID_SOCKET) {
883 pj_sock_close(server[i].sock);
884 server[i].sock = PJ_INVALID_SOCKET;
885 }
886
887 if (client[i].key != NULL) {
888 pj_ioqueue_unregister(client[i].key);
889 client[i].key = NULL;
890 client[i].sock = PJ_INVALID_SOCKET;
891 } else if (client[i].sock != PJ_INVALID_SOCKET) {
892 pj_sock_close(client[i].sock);
893 client[i].sock = PJ_INVALID_SOCKET;
894 }
895 }
896
897 if (listener.key) {
898 pj_ioqueue_unregister(listener.key);
899 listener.key = NULL;
900 } else if (listener.sock != PJ_INVALID_SOCKET) {
901 pj_sock_close(listener.sock);
902 listener.sock = PJ_INVALID_SOCKET;
903 }
904
905 if (ioque != NULL)
906 pj_ioqueue_destroy(ioque);
907 pj_pool_release(pool);
908 return status;
909
910 }
911
912
913 static int tcp_ioqueue_test_impl(const pj_ioqueue_cfg *cfg)
914 {
915 int status;
916 char title[64];
917
918 pj_ansi_snprintf(title, sizeof(title), "%s (concur:%d, epoll_flags:0x%x)",
919 pj_ioqueue_name(), cfg->default_concurrency,
920 cfg->epoll_flags);
921
922 PJ_LOG(3, (THIS_FILE, "..%s compliance test 0 (success scenario)",
923 title));
924 if ((status=compliance_test_0(cfg)) != 0) {
925 PJ_LOG(1, (THIS_FILE, "....FAILED (status=%d)\n", status));
926 return status;
927 }
928 PJ_LOG(3, (THIS_FILE, "..%s compliance test 1 (failed scenario)",
929 title));
930 if ((status=compliance_test_1(cfg)) != 0) {
931 PJ_LOG(1, (THIS_FILE, "....FAILED (status=%d)\n", status));
932 return status;
933 }
934
935 PJ_LOG(3, (THIS_FILE, "..%s compliance test 2 (repeated accept)",
936 title));
937 if ((status=compliance_test_2(cfg)) != 0) {
938 PJ_LOG(1, (THIS_FILE, "....FAILED (status=%d)\n", status));
939 return status;
940 }
941
942 return 0;
943 }
944
945 int tcp_ioqueue_test()
946 {
947 pj_ioqueue_epoll_flag epoll_flags[] = {
948 PJ_IOQUEUE_EPOLL_AUTO,
949 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
950 PJ_IOQUEUE_EPOLL_EXCLUSIVE,
951 PJ_IOQUEUE_EPOLL_ONESHOT,
952 0
953 #endif
954 };
955 pj_bool_t concurs[] = { PJ_TRUE, PJ_FALSE };
956 int i, rc;
957
958 for (i=0; i<(int)PJ_ARRAY_SIZE(epoll_flags); ++i) {
959 pj_ioqueue_cfg cfg;
960
961 pj_ioqueue_cfg_default(&cfg);
962 cfg.epoll_flags = epoll_flags[i];
963
964 PJ_LOG(3, (THIS_FILE, "..%s TCP compliance test, epoll_flags=0x%x",
965 pj_ioqueue_name(), cfg.epoll_flags));
966
967 rc = tcp_ioqueue_test_impl(&cfg);
968 if (rc != 0)
969 return rc;
970 }
971
972 for (i=0; i<(int)PJ_ARRAY_SIZE(concurs); ++i) {
973 pj_ioqueue_cfg cfg;
974
975 pj_ioqueue_cfg_default(&cfg);
976 cfg.default_concurrency = concurs[i];
977
978 PJ_LOG(3, (THIS_FILE, "..%s TCP compliance test, concurrency=%d",
979 pj_ioqueue_name(), cfg.default_concurrency));
980
981 rc = tcp_ioqueue_test_impl(&cfg);
982 if (rc != 0)
983 return rc;
984 }
985
986 return 0;
987 }
988
989 #endif /* PJ_HAS_TCP */
990
991
992 #else
993 /* To prevent warning about "translation unit is empty"
994 * when this test is disabled.
995 */
996 int dummy_uiq_tcp;
997 #endif /* INCLUDE_TCP_IOQUEUE_TEST */
998
999