Debian Codegraph @

Xpjproject/pjsip/src/test/transport_udp_test.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
20 #include "test.h"
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #define THIS_FILE "transport_udp_test.c"
25
26 static pj_status_t multi_transport_test(pjsip_transport *tp[], unsigned max_tp)
27 {
28 #define ERR(rc__) { rc=rc__; goto on_return; }
29 int rc;
30 unsigned i, num_tp=0;
31 pj_str_t s;
32 pjsip_transport *other_udp_tp = NULL, *udp_tp = NULL;
33 pj_sockaddr_in rem_addr;
34 pjsip_tpselector tp_sel;
35
36 /* Check if an UDP transport has been registered by other part of
37 * pjsip-test */
38 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
39 pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
40 &rem_addr, sizeof(rem_addr),
41 NULL, &other_udp_tp);
42
43 for (i=num_tp; i<max_tp; ++i) {
44 pj_sockaddr_in addr;
45
46 pj_sockaddr_in_init(&addr, NULL, (pj_uint16_t)(TEST_UDP_PORT+i));
47
48 /* Start UDP transport. */
49 PJ_TEST_SUCCESS(pjsip_udp_transport_start( endpt, &addr, NULL, 1, &udp_tp),
50 NULL, ERR(-110));
51
52 /* UDP transport must have initial reference counter set to 1. */
53 PJ_TEST_EQ(pj_atomic_get(udp_tp->ref_cnt), 1, NULL, ERR(-120));
54
55 tp[num_tp++] = udp_tp;
56 }
57
58 for (i = 0; i < num_tp; ++i) {
59 PJ_TEST_EQ(pj_atomic_get(tp[i]->ref_cnt), 1, NULL, ERR(-130));
60
61 /* Test basic transport attributes */
62 rc = generic_transport_test(udp_tp);
63 if (rc != 0)
64 goto on_return;
65 }
66
67 /* Acquire transport test without selector. */
68 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
69 PJ_TEST_SUCCESS(pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
70 &rem_addr, sizeof(rem_addr),
71 NULL, &udp_tp),
72 NULL, ERR(-140));
73
74 for (i = 0; i < num_tp; ++i) {
75 if (udp_tp == tp[i]) {
76 break;
77 }
78 }
79 PJ_TEST_TRUE(i<num_tp || udp_tp==other_udp_tp, NULL, ERR(-150));
80
81 pjsip_transport_dec_ref(udp_tp);
82
83 if (udp_tp == other_udp_tp) {
84 PJ_TEST_GT(pj_atomic_get(udp_tp->ref_cnt), 1, NULL, ERR(-160));
85 } else {
86 PJ_TEST_EQ(pj_atomic_get(udp_tp->ref_cnt), 1, NULL, ERR(-165));
87 }
88
89 /* Acquire transport test with selector. */
90 pj_bzero(&tp_sel, sizeof(tp_sel));
91 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
92 tp_sel.u.transport = tp[num_tp-1];
93 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
94 PJ_TEST_SUCCESS( pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
95 &rem_addr, sizeof(rem_addr),
96 &tp_sel, &udp_tp),
97 NULL, ERR(-170));
98
99 PJ_TEST_EQ(udp_tp, tp[num_tp-1], NULL, ERR(-180));
100
101 pjsip_transport_dec_ref(udp_tp);
102
103 PJ_TEST_EQ(pj_atomic_get(udp_tp->ref_cnt), 1, NULL, ERR(-190));
104
105 rc = 0;
106
107 on_return:
108 if (other_udp_tp) {
109 pjsip_transport_dec_ref(other_udp_tp);
110 }
111 if (rc != 0) {
112 for (i=0; i<num_tp; ++i) {
113 pjsip_transport_dec_ref(tp[i]);
114 }
115 }
116
117 return rc;
118 #undef ERR
119 }
120
121 /*
122 * UDP transport test.
123 */
124 int transport_udp_test(void)
125 {
126 enum { SEND_RECV_LOOP = 8 };
127 enum { NUM_TP = 4 };
128 pjsip_transport *tp[NUM_TP], *udp_tp;
129 pj_sockaddr_in rem_addr;
130 pj_str_t s;
131 pj_status_t status;
132 int rtt[SEND_RECV_LOOP], min_rtt;
133 int i, pkt_lost;
134
135 status = multi_transport_test(&tp[0], NUM_TP);
136 if (status != PJ_SUCCESS)
137 return status;
138
139 /* Basic transport's send/receive loopback test. */
140 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "127.0.0.1"), TEST_UDP_PORT);
141 for (i=0; i<SEND_RECV_LOOP; ++i) {
142 status = transport_send_recv_test(PJSIP_TRANSPORT_UDP, tp[0],
143 "127.0.0.1:"TEST_UDP_PORT_STR,
144 &rtt[i]);
145 if (status != 0)
146 return status;
147 }
148
149 min_rtt = 0xFFFFFFF;
150 for (i=0; i<SEND_RECV_LOOP; ++i)
151 if (rtt[i] < min_rtt) min_rtt = rtt[i];
152
153 report_ival("udp-rtt-usec", min_rtt, "usec",
154 "Best UDP transport round trip time, in microseconds "
155 "(time from sending request until response is received. "
156 "Tests were performed on local machine only)");
157
158
159 /* Multi-threaded round-trip test. */
160 status = transport_rt_test(PJSIP_TRANSPORT_UDP, tp[0],
161 "127.0.0.1:"TEST_UDP_PORT_STR,
162 &pkt_lost);
163 if (status != 0)
164 return status;
165
166 if (pkt_lost != 0)
167 PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost));
168
169 for (i = 0; i < NUM_TP; ++i) {
170 udp_tp = tp[i];
171
172 /* Check again that reference counter is 1. */
173 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
174 return -80;
175
176 /* Destroy this transport. */
177 pjsip_transport_dec_ref(udp_tp);
178 status = pjsip_transport_destroy(udp_tp);
179 if (status != PJ_SUCCESS)
180 return -90;
181 }
182
183 /* Flush events. */
184 PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second..."));
185 flush_events(1000);
186
187 /* Done */
188 return 0;
189 }