blob: a89f83d75c781ae9c222913a795c898f57bcfc89 [file] [log] [blame]
Benny Prijono0ca04b62005-12-30 23:50:15 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono0ca04b62005-12-30 23:50:15 +00004 *
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
20#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000021#include <pjsip.h>
Benny Prijono0ca04b62005-12-30 23:50:15 +000022#include <pjlib.h>
23
Benny Prijono85598d92006-01-07 18:44:25 +000024#define THIS_FILE "transport_udp_test.c"
25
Benny Prijono0ca04b62005-12-30 23:50:15 +000026
27/*
28 * UDP transport test.
29 */
30int transport_udp_test(void)
31{
Benny Prijonoe93e2872006-06-28 16:46:49 +000032 enum { SEND_RECV_LOOP = 8 };
Benny Prijono0ca04b62005-12-30 23:50:15 +000033 pjsip_transport *udp_tp, *tp;
34 pj_sockaddr_in addr, rem_addr;
35 pj_str_t s;
36 pj_status_t status;
Benny Prijono7db431e2006-07-23 14:38:49 +000037 int rtt[SEND_RECV_LOOP], min_rtt;
Benny Prijono85598d92006-01-07 18:44:25 +000038 int i, pkt_lost;
Benny Prijono0ca04b62005-12-30 23:50:15 +000039
40 pj_sockaddr_in_init(&addr, NULL, TEST_UDP_PORT);
41
42 /* Start UDP transport. */
43 status = pjsip_udp_transport_start( endpt, &addr, NULL, 1, &udp_tp);
44 if (status != PJ_SUCCESS) {
45 app_perror(" Error: unable to start UDP transport", status);
46 return -10;
47 }
48
49 /* UDP transport must have initial reference counter set to 1. */
50 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
51 return -20;
52
53 /* Test basic transport attributes */
54 status = generic_transport_test(udp_tp);
55 if (status != PJ_SUCCESS)
56 return status;
57
58 /* Test that transport manager is returning the correct
59 * transport.
60 */
61 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
62 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
63 &rem_addr, sizeof(rem_addr),
Benny Prijonodf2b71e2007-01-20 19:17:47 +000064 NULL, &tp);
Benny Prijono0ca04b62005-12-30 23:50:15 +000065 if (status != PJ_SUCCESS)
66 return -50;
67 if (tp != udp_tp)
68 return -60;
69
70 /* pjsip_endpt_acquire_transport() adds reference, so we need
71 * to decrement it.
72 */
73 pjsip_transport_dec_ref(tp);
74
75 /* Check again that reference counter is 1. */
76 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
77 return -70;
78
79 /* Basic transport's send/receive loopback test. */
80 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "127.0.0.1"), TEST_UDP_PORT);
81 for (i=0; i<SEND_RECV_LOOP; ++i) {
Benny Prijonofa73e3e2006-01-05 23:35:46 +000082 status = transport_send_recv_test(PJSIP_TRANSPORT_UDP, tp,
Benny Prijonoe93e2872006-06-28 16:46:49 +000083 "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR,
84 &rtt[i]);
Benny Prijono0ca04b62005-12-30 23:50:15 +000085 if (status != 0)
86 return status;
87 }
88
Benny Prijonoe93e2872006-06-28 16:46:49 +000089 min_rtt = 0xFFFFFFF;
90 for (i=0; i<SEND_RECV_LOOP; ++i)
91 if (rtt[i] < min_rtt) min_rtt = rtt[i];
92
93 report_ival("udp-rtt-usec", min_rtt, "usec",
94 "Best UDP transport round trip time, in microseconds "
95 "(time from sending request until response is received. "
96 "Tests were performed on local machine only)");
97
98
Benny Prijono0ca04b62005-12-30 23:50:15 +000099 /* Multi-threaded round-trip test. */
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000100 status = transport_rt_test(PJSIP_TRANSPORT_UDP, tp,
Benny Prijono85598d92006-01-07 18:44:25 +0000101 "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR,
102 &pkt_lost);
Benny Prijono0ca04b62005-12-30 23:50:15 +0000103 if (status != 0)
104 return status;
105
Benny Prijono85598d92006-01-07 18:44:25 +0000106 if (pkt_lost != 0)
107 PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost));
108
Benny Prijono0ca04b62005-12-30 23:50:15 +0000109 /* Check again that reference counter is 1. */
110 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
111 return -80;
112
113 /* Destroy this transport. */
114 pjsip_transport_dec_ref(udp_tp);
115
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000116 /* Force destroy this transport. */
Benny Prijono478d4312006-06-23 15:04:54 +0000117 status = pjsip_transport_destroy(udp_tp);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000118 if (status != PJ_SUCCESS)
119 return -90;
120
Benny Prijono85598d92006-01-07 18:44:25 +0000121 /* Flush events. */
122 PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second..."));
123 flush_events(1000);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000124
Benny Prijono0ca04b62005-12-30 23:50:15 +0000125 /* Done */
126 return 0;
127}