Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include "test.h" |
| 22 | #include <pjsip.h> |
| 23 | #include <pjlib.h> |
| 24 | |
| 25 | #define THIS_FILE "transport_udp_test.c" |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * UDP transport test. |
| 30 | */ |
| 31 | int transport_udp_test(void) |
| 32 | { |
| 33 | enum { SEND_RECV_LOOP = 8 }; |
| 34 | pjsip_transport *udp_tp, *tp; |
| 35 | pj_sockaddr_in addr, rem_addr; |
| 36 | pj_str_t s; |
| 37 | pj_status_t status; |
| 38 | int rtt[SEND_RECV_LOOP], min_rtt; |
| 39 | int i, pkt_lost; |
| 40 | |
| 41 | pj_sockaddr_in_init(&addr, NULL, TEST_UDP_PORT); |
| 42 | |
| 43 | /* Start UDP transport. */ |
| 44 | status = pjsip_udp_transport_start( endpt, &addr, NULL, 1, &udp_tp); |
| 45 | if (status != PJ_SUCCESS) { |
| 46 | app_perror(" Error: unable to start UDP transport", status); |
| 47 | return -10; |
| 48 | } |
| 49 | |
| 50 | /* UDP transport must have initial reference counter set to 1. */ |
| 51 | if (pj_atomic_get(udp_tp->ref_cnt) != 1) |
| 52 | return -20; |
| 53 | |
| 54 | /* Test basic transport attributes */ |
| 55 | status = generic_transport_test(udp_tp); |
| 56 | if (status != PJ_SUCCESS) |
| 57 | return status; |
| 58 | |
| 59 | /* Test that transport manager is returning the correct |
| 60 | * transport. |
| 61 | */ |
| 62 | pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80); |
| 63 | status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP, |
| 64 | &rem_addr, sizeof(rem_addr), |
| 65 | NULL, &tp); |
| 66 | if (status != PJ_SUCCESS) |
| 67 | return -50; |
| 68 | if (tp != udp_tp) |
| 69 | return -60; |
| 70 | |
| 71 | /* pjsip_endpt_acquire_transport() adds reference, so we need |
| 72 | * to decrement it. |
| 73 | */ |
| 74 | pjsip_transport_dec_ref(tp); |
| 75 | |
| 76 | /* Check again that reference counter is 1. */ |
| 77 | if (pj_atomic_get(udp_tp->ref_cnt) != 1) |
| 78 | return -70; |
| 79 | |
| 80 | /* Basic transport's send/receive loopback test. */ |
| 81 | pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "127.0.0.1"), TEST_UDP_PORT); |
| 82 | for (i=0; i<SEND_RECV_LOOP; ++i) { |
| 83 | status = transport_send_recv_test(PJSIP_TRANSPORT_UDP, tp, |
| 84 | "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR, |
| 85 | &rtt[i]); |
| 86 | if (status != 0) |
| 87 | return status; |
| 88 | } |
| 89 | |
| 90 | min_rtt = 0xFFFFFFF; |
| 91 | for (i=0; i<SEND_RECV_LOOP; ++i) |
| 92 | if (rtt[i] < min_rtt) min_rtt = rtt[i]; |
| 93 | |
| 94 | report_ival("udp-rtt-usec", min_rtt, "usec", |
| 95 | "Best UDP transport round trip time, in microseconds " |
| 96 | "(time from sending request until response is received. " |
| 97 | "Tests were performed on local machine only)"); |
| 98 | |
| 99 | |
| 100 | /* Multi-threaded round-trip test. */ |
| 101 | status = transport_rt_test(PJSIP_TRANSPORT_UDP, tp, |
| 102 | "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR, |
| 103 | &pkt_lost); |
| 104 | if (status != 0) |
| 105 | return status; |
| 106 | |
| 107 | if (pkt_lost != 0) |
| 108 | PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost)); |
| 109 | |
| 110 | /* Check again that reference counter is 1. */ |
| 111 | if (pj_atomic_get(udp_tp->ref_cnt) != 1) |
| 112 | return -80; |
| 113 | |
| 114 | /* Destroy this transport. */ |
| 115 | pjsip_transport_dec_ref(udp_tp); |
| 116 | |
| 117 | /* Force destroy this transport. */ |
| 118 | status = pjsip_transport_destroy(udp_tp); |
| 119 | if (status != PJ_SUCCESS) |
| 120 | return -90; |
| 121 | |
| 122 | /* Flush events. */ |
| 123 | PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second...")); |
| 124 | flush_events(1000); |
| 125 | |
| 126 | /* Done */ |
| 127 | return 0; |
| 128 | } |