Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/activesock.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 #include <pjlib.h>
21
22 /**
23 * \page page_pjlib_activesock_test Test: Active Socket
24 *
25 * This file is <b>pjlib-test/activesock.c</b>
26 *
27 * \include pjlib-test/activesock.c
28 */
29
30 #if INCLUDE_ACTIVESOCK_TEST
31
32 #define THIS_FILE "activesock.c"
33
34 #define ERR(r__) { ret=r__; goto on_return; }
35
36 /*******************************************************************
37 * Simple UDP echo server.
38 */
39 struct udp_echo_srv
40 {
41 pj_activesock_t *asock;
42 pj_bool_t echo_enabled;
43 pj_uint16_t port;
44 pj_ioqueue_op_key_t send_key;
45 pj_status_t status;
46 unsigned rx_cnt;
47 unsigned rx_err_cnt, tx_err_cnt;
48 };
49
50 static void udp_echo_err(const char *title, pj_status_t status)
51 {
52 char errmsg[PJ_ERR_MSG_SIZE];
53
54 pj_strerror(status, errmsg, sizeof(errmsg));
55 PJ_LOG(3,(THIS_FILE, " error: %s: %s", title, errmsg));
56 }
57
58 static pj_bool_t udp_echo_srv_on_data_recvfrom(pj_activesock_t *asock,
59 void *data,
60 pj_size_t size,
61 const pj_sockaddr_t *src_addr,
62 int addr_len,
63 pj_status_t status)
64 {
65 struct udp_echo_srv *srv;
66 pj_ssize_t sent;
67
68
69 srv = (struct udp_echo_srv*) pj_activesock_get_user_data(asock);
70
71 if (status != PJ_SUCCESS) {
72 srv->status = status;
73 srv->rx_err_cnt++;
74 udp_echo_err("recvfrom() callback", status);
75 return PJ_TRUE;
76 }
77
78 srv->rx_cnt++;
79
80 /* Send back if echo is enabled */
81 if (srv->echo_enabled) {
82 sent = size;
83 srv->status = pj_activesock_sendto(asock, &srv->send_key, data,
84 &sent, 0,
85 src_addr, addr_len);
86 if (srv->status != PJ_SUCCESS && srv->status != PJ_EPENDING) {
87 srv->tx_err_cnt++;
88 udp_echo_err("sendto()", srv->status);
89 }
90 }
91
92 return PJ_TRUE;
93 }
94
95
96 static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
97 pj_ioqueue_t *ioqueue,
98 pj_bool_t enable_echo,
99 struct udp_echo_srv **p_srv)
100 {
101 struct udp_echo_srv *srv;
102 pj_sockaddr addr;
103 pj_activesock_cb activesock_cb;
104 pj_status_t status;
105
106 srv = PJ_POOL_ZALLOC_T(pool, struct udp_echo_srv);
107 srv->echo_enabled = enable_echo;
108
109 pj_sockaddr_in_init(&addr.ipv4, NULL, 0);
110
111 pj_bzero(&activesock_cb, sizeof(activesock_cb));
112 activesock_cb.on_data_recvfrom = &udp_echo_srv_on_data_recvfrom;
113
114 status = pj_activesock_create_udp(pool, &addr, NULL, ioqueue, &activesock_cb,
115 srv, &srv->asock, &addr);
116 if (status != PJ_SUCCESS) {
117 udp_echo_err("pj_activesock_create()", status);
118 return status;
119 }
120
121 srv->port = pj_ntohs(addr.ipv4.sin_port);
122
123 pj_ioqueue_op_key_init(&srv->send_key, sizeof(srv->send_key));
124
125 status = pj_activesock_start_recvfrom(srv->asock, pool, 32, 0);
126 if (status != PJ_SUCCESS) {
127 pj_activesock_close(srv->asock);
128 udp_echo_err("pj_activesock_start_recvfrom()", status);
129 return status;
130 }
131
132
133 *p_srv = srv;
134 return PJ_SUCCESS;
135 }
136
137 static void udp_echo_srv_destroy(struct udp_echo_srv *srv)
138 {
139 pj_activesock_close(srv->asock);
140 }
141
142 /*******************************************************************
143 * UDP ping pong test (send packet back and forth between two UDP echo
144 * servers.
145 */
146 /* was udp_ping_pong_test(void) */
147 static int activesock_test0(void)
148 {
149 pj_ioqueue_t *ioqueue = NULL;
150 pj_pool_t *pool = NULL;
151 struct udp_echo_srv *srv1=NULL, *srv2=NULL;
152 pj_bool_t need_send = PJ_TRUE;
153 unsigned data = 0;
154 int count, ret;
155 pj_status_t status;
156
157 pool = pj_pool_create(mem, "pingpong", 512, 512, NULL);
158 PJ_TEST_NOT_NULL(pool, NULL, return -10);
159
160 PJ_TEST_SUCCESS(pj_ioqueue_create(pool, 4, &ioqueue), NULL, ERR(-20))
161 PJ_TEST_SUCCESS(udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv1),
162 NULL, ERR(-30));
163 PJ_TEST_SUCCESS(udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv2),
164 NULL, ERR(-40));
165
166 /* initiate the first send */
167 for (count=0; count<1000; ++count) {
168 unsigned last_rx1, last_rx2;
169 unsigned i;
170
171 if (need_send) {
172 pj_str_t loopback;
173 pj_sockaddr_in addr;
174 pj_ssize_t sent;
175
176 ++data;
177
178 sent = sizeof(data);
179 loopback = pj_str("127.0.0.1");
180 pj_sockaddr_in_init(&addr, &loopback, srv2->port);
181 status = pj_activesock_sendto(srv1->asock, &srv1->send_key,
182 &data, &sent, 0,
183 &addr, sizeof(addr));
184 PJ_TEST_TRUE(status==PJ_SUCCESS || status==PJ_EPENDING,
185 "pj_activesock_sendto()", ERR(-50));
186
187 need_send = PJ_FALSE;
188 }
189
190 last_rx1 = srv1->rx_cnt;
191 last_rx2 = srv2->rx_cnt;
192
193 for (i=0; i<10 && last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt; ++i) {
194 pj_time_val delay = {0, 10};
195 #ifdef PJ_SYMBIAN
196 PJ_UNUSED_ARG(delay);
197 pj_symbianos_poll(-1, 100);
198 #else
199 pj_ioqueue_poll(ioqueue, &delay);
200 #endif
201 }
202
203 PJ_TEST_EQ(srv1->rx_err_cnt+srv1->tx_err_cnt, 0, NULL, ERR(-60));
204 PJ_TEST_EQ(srv2->rx_err_cnt+srv2->tx_err_cnt, 0, NULL, ERR(-62));
205 PJ_TEST_TRUE(last_rx1 != srv1->rx_cnt || last_rx2 != srv2->rx_cnt,
206 "timeout/packets have been lost", ERR(-70));
207 }
208
209 ret = 0;
210
211 on_return:
212 if (srv2)
213 udp_echo_srv_destroy(srv2);
214 if (srv1)
215 udp_echo_srv_destroy(srv1);
216 if (ioqueue)
217 pj_ioqueue_destroy(ioqueue);
218 if (pool)
219 pj_pool_release(pool);
220
221 return ret;
222 }
223
224
225
226 #define SIGNATURE 0xdeadbeef
227 struct tcp_pkt
228 {
229 pj_uint32_t signature;
230 pj_uint32_t seq;
231 char fill[513];
232 };
233
234 struct tcp_state
235 {
236 pj_bool_t err;
237 pj_bool_t sent;
238 pj_uint32_t next_recv_seq;
239 pj_uint8_t pkt[600];
240 };
241
242 struct send_key
243 {
244 pj_ioqueue_op_key_t op_key;
245 };
246
247
248 static pj_bool_t tcp_on_data_read(pj_activesock_t *asock,
249 void *data,
250 pj_size_t size,
251 pj_status_t status,
252 pj_size_t *remainder)
253 {
254 struct tcp_state *st = (struct tcp_state*) pj_activesock_get_user_data(asock);
255 char *next = (char*) data;
256
257 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
258 PJ_LOG(1,("", " err: status=%d", status));
259 st->err = PJ_TRUE;
260 return PJ_FALSE;
261 }
262
263 while (size >= sizeof(struct tcp_pkt)) {
264 struct tcp_pkt *tcp_pkt = (struct tcp_pkt*) next;
265
266 if (tcp_pkt->signature != SIGNATURE) {
267 PJ_LOG(1,("", " err: invalid signature at seq=%d",
268 st->next_recv_seq));
269 st->err = PJ_TRUE;
270 return PJ_FALSE;
271 }
272 if (tcp_pkt->seq != st->next_recv_seq) {
273 PJ_LOG(1,("", " err: wrong sequence"));
274 st->err = PJ_TRUE;
275 return PJ_FALSE;
276 }
277
278 st->next_recv_seq++;
279 next += sizeof(struct tcp_pkt);
280 size -= sizeof(struct tcp_pkt);
281 }
282
283 if (size) {
284 pj_memmove(data, next, size);
285 *remainder = size;
286 }
287
288 return PJ_TRUE;
289 }
290
291 static pj_bool_t tcp_on_data_sent(pj_activesock_t *asock,
292 pj_ioqueue_op_key_t *op_key,
293 pj_ssize_t sent)
294 {
295 struct tcp_state *st=(struct tcp_state*)pj_activesock_get_user_data(asock);
296
297 PJ_UNUSED_ARG(op_key);
298
299 st->sent = 1;
300
301 if (sent < 1) {
302 st->err = PJ_TRUE;
303 return PJ_FALSE;
304 }
305
306 return PJ_TRUE;
307 }
308
309 /* was tcp_perf_test() */
310 static int activesock_test1(void)
311 {
312 enum { COUNT=10000 };
313 pj_pool_t *pool = NULL;
314 pj_ioqueue_t *ioqueue = NULL;
315 pj_sock_t sock1=PJ_INVALID_SOCKET, sock2=PJ_INVALID_SOCKET;
316 pj_activesock_t *asock1 = NULL, *asock2 = NULL;
317 pj_activesock_cb cb;
318 struct tcp_state *state1, *state2;
319 unsigned i;
320 int ret = 0;
321 pj_status_t status = PJ_SUCCESS;
322
323 pool = pj_pool_create(mem, "activesock_test1", 256, 256, NULL);
324 PJ_TEST_NOT_NULL(pool, NULL, return -10);
325
326 PJ_TEST_SUCCESS( app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock1,
327 &sock2),
328 NULL, ERR(-100));
329 PJ_TEST_SUCCESS(pj_ioqueue_create(pool, 4, &ioqueue),
330 NULL, ERR(-110));
331
332 pj_bzero(&cb, sizeof(cb));
333 cb.on_data_read = &tcp_on_data_read;
334 cb.on_data_sent = &tcp_on_data_sent;
335
336 state1 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
337 PJ_TEST_SUCCESS(pj_activesock_create(pool, sock1, pj_SOCK_STREAM(),
338 NULL, ioqueue, &cb, state1, &asock1),
339 NULL, ERR(-120));
340
341 state2 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
342 PJ_TEST_SUCCESS(pj_activesock_create(pool, sock2, pj_SOCK_STREAM(), NULL,
343 ioqueue, &cb, state2, &asock2),
344 NULL, ERR(-130));
345 PJ_TEST_SUCCESS(pj_activesock_start_read(asock1, pool, 1000, 0),
346 NULL, ERR(-140));
347
348 /* Send packet as quickly as possible */
349 for (i=0; i<COUNT && !state1->err && !state2->err; ++i) {
350 struct tcp_pkt *pkt;
351 struct send_key send_key[2], *op_key;
352 pj_ssize_t len;
353
354 pkt = (struct tcp_pkt*)state2->pkt;
355 pkt->signature = SIGNATURE;
356 pkt->seq = i;
357 pj_memset(pkt->fill, 'a', sizeof(pkt->fill));
358
359 op_key = &send_key[i%2];
360 pj_ioqueue_op_key_init(&op_key->op_key, sizeof(*op_key));
361
362 state2->sent = PJ_FALSE;
363 len = sizeof(*pkt);
364 status = pj_activesock_send(asock2, &op_key->op_key, pkt, &len, 0);
365 if (status == PJ_EPENDING) {
366 do {
367 #if PJ_SYMBIAN
368 pj_symbianos_poll(-1, -1);
369 #else
370 pj_ioqueue_poll(ioqueue, NULL);
371 #endif
372 } while (!state2->sent);
373 } else {
374 #if PJ_SYMBIAN
375 /* The Symbian socket always returns PJ_SUCCESS for TCP send,
376 * eventhough the remote end hasn't received the data yet.
377 * If we continue sending, eventually send() will block,
378 * possibly because the send buffer is full. So we need to
379 * poll the ioqueue periodically, to let receiver gets the
380 * data.
381 */
382 pj_symbianos_poll(-1, 0);
383 #endif
384 PJ_TEST_SUCCESS(status, "send error", ERR(-180));
385 if (status == PJ_SUCCESS) {
386 PJ_TEST_EQ(len, sizeof(*pkt),
387 "shouldn't report partial sent", ERR(-190));
388 }
389 }
390
391 #ifndef PJ_SYMBIAN
392 for (;;) {
393 pj_time_val timeout = {0, 10};
394 if (pj_ioqueue_poll(ioqueue, &timeout) < 1)
395 break;
396 }
397 #endif
398
399 }
400
401 /* Wait until everything has been sent/received */
402 if (state1->next_recv_seq < COUNT) {
403 #ifdef PJ_SYMBIAN
404 while (pj_symbianos_poll(-1, 1000) == PJ_TRUE)
405 ;
406 #else
407 pj_time_val delay = {0, 100};
408 while (pj_ioqueue_poll(ioqueue, &delay) > 0)
409 ;
410 #endif
411 }
412
413 if (status == PJ_EPENDING)
414 status = PJ_SUCCESS;
415
416 /* this should not happen. non-success should have been dealt with */
417 PJ_TEST_SUCCESS(status, NULL, ERR(-182));
418
419 PJ_TEST_EQ(state1->err, 0, NULL, ERR(-183));
420 PJ_TEST_EQ(state2->err, 0, NULL, ERR(-186));
421 PJ_TEST_EQ(state1->next_recv_seq, COUNT,
422 "not all packets are received", ERR(-195));
423
424 on_return:
425 if (asock2)
426 pj_activesock_close(asock2);
427 if (asock1)
428 pj_activesock_close(asock1);
429 if (ioqueue)
430 pj_ioqueue_destroy(ioqueue);
431 if (pool)
432 pj_pool_release(pool);
433
434 return ret;
435 }
436
437 /*
438 * Test that send callbacks are invoked even after activesock is closed.
439 * This verifies the fix for #4864/#4878 where pending write callbacks
440 * were silently dropped, leaking tdata references in upper layers.
441 */
442 struct close_test_state
443 {
444 int cb_count;
445 };
446
447 static pj_bool_t close_test_on_data_sent(pj_activesock_t *asock,
448 pj_ioqueue_op_key_t *op_key,
449 pj_ssize_t sent)
450 {
451 struct close_test_state *st;
452
453 PJ_UNUSED_ARG(op_key);
454 PJ_UNUSED_ARG(sent);
455
456 st = (struct close_test_state*)pj_activesock_get_user_data(asock);
457 if (st)
458 st->cb_count++;
459
460 return PJ_TRUE;
461 }
462
463 static int activesock_close_send_cb_test(void)
464 {
465 enum { COUNT = 100 };
466 pj_pool_t *pool = NULL;
467 pj_ioqueue_t *ioqueue = NULL;
468 pj_sock_t sock1 = PJ_INVALID_SOCKET, sock2 = PJ_INVALID_SOCKET;
469 pj_activesock_t *asock1 = NULL, *asock2 = NULL;
470 pj_activesock_cb cb;
471 struct close_test_state state;
472 struct send_key op_keys[COUNT];
473 pj_time_val delay = {0, 100};
474 int pending_count = 0;
475 unsigned i;
476 int ret = 0;
477 pj_status_t status;
478
479 PJ_LOG(3, (THIS_FILE, "..activesock close with pending send CB test"));
480
481 pool = pj_pool_create(mem, "close_send_cb", 256, 256, NULL);
482 PJ_TEST_NOT_NULL(pool, NULL, return -500);
483
484 PJ_TEST_SUCCESS(app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0,
485 &sock1, &sock2),
486 NULL, ERR(-510));
487 PJ_TEST_SUCCESS(pj_ioqueue_create(pool, 4, &ioqueue),
488 NULL, ERR(-520));
489
490 pj_bzero(&cb, sizeof(cb));
491 cb.on_data_sent = &close_test_on_data_sent;
492
493 pj_bzero(&state, sizeof(state));
494 PJ_TEST_SUCCESS(pj_activesock_create(pool, sock2, pj_SOCK_STREAM(),
495 NULL, ioqueue, &cb, &state,
496 &asock2),
497 NULL, ERR(-530));
498
499 /* Create asock1 just to keep sock1 alive (peer side) */
500 PJ_TEST_SUCCESS(pj_activesock_create(pool, sock1, pj_SOCK_STREAM(),
501 NULL, ioqueue, &cb, NULL, &asock1),
502 NULL, ERR(-535));
503
504 /* Shrink send buffer so fast-track fails quickly, forcing sends
505 * through the async ioqueue path.
506 */
507 {
508 int sndbuf = 1024;
509 pj_sock_setsockopt(sock2, pj_SOL_SOCKET(), pj_SO_SNDBUF(),
510 &sndbuf, sizeof(sndbuf));
511 }
512
513 /* Send multiple packets without polling, they should queue up */
514 for (i = 0; i < COUNT; ++i) {
515 struct tcp_pkt pkt;
516 pj_ssize_t len;
517
518 pkt.signature = SIGNATURE;
519 pkt.seq = i;
520 pj_memset(pkt.fill, 'a', sizeof(pkt.fill));
521
522 pj_ioqueue_op_key_init(&op_keys[i].op_key,
523 sizeof(op_keys[i]));
524
525 len = sizeof(pkt);
526 status = pj_activesock_send(asock2, &op_keys[i].op_key,
527 &pkt, &len, 0);
528 if (status == PJ_EPENDING) {
529 pending_count++;
530 } else if (status != PJ_SUCCESS) {
531 break;
532 }
533 }
534
535 /* Close the activesock while sends are pending. The send callbacks
536 * MUST still be invoked so upper layers can free resources.
537 */
538 pj_activesock_close(asock2);
539 asock2 = NULL;
540
541 /* Poll to process any remaining ioqueue events. On IOCP, each
542 * cancelled overlapped op needs a separate poll to dequeue from
543 * the completion port. Poll until all callbacks received or
544 * timeout (~20s worst case, but early-break when done).
545 */
546 for (i = 0; i < 200; ++i) {
547 pj_ioqueue_poll(ioqueue, &delay);
548 if (state.cb_count >= pending_count && pending_count > 0)
549 break;
550 }
551
552 PJ_LOG(3, (THIS_FILE, "...pending=%d, callbacks=%d",
553 pending_count, state.cb_count));
554
555 if (pending_count == 0) {
556 /* All sends completed synchronously (fast-track succeeded).
557 * The async drain path is not exercised in this run. To force
558 * async path, set PJ_IOQUEUE_FAST_TRACK=0 in config_site.h.
559 */
560 PJ_LOG(3, (THIS_FILE, "...async path not exercised, skipping"));
561 } else {
562 /* All pending sends must have received a callback */
563 PJ_TEST_GTE(state.cb_count, pending_count,
564 "not all pending send callbacks were invoked",
565 ERR(-540));
566 }
567
568 on_return:
569 if (asock2)
570 pj_activesock_close(asock2);
571 if (asock1)
572 pj_activesock_close(asock1);
573 if (ioqueue)
574 pj_ioqueue_destroy(ioqueue);
575 if (pool)
576 pj_pool_release(pool);
577
578 return ret;
579 }
580
581 int activesock_test(void)
582 {
583 int rc;
584 if ((rc=activesock_test0()) != 0)
585 return rc;
586
587 if ((rc=activesock_test1()) != 0)
588 return rc;
589
590 if ((rc=activesock_close_send_cb_test()) != 0)
591 return rc;
592
593 return 0;
594 }
595
596 #else /* INCLUDE_ACTIVESOCK_TEST */
597 /* To prevent warning about "translation unit is empty"
598 * when this test is disabled.
599 */
600 int dummy_active_sock_test;
601 #endif /* INCLUDE_ACTIVESOCK_TEST */
602