Debian Codegraph @

Xpjproject/pjsip/src/test/transport_tcp_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_tcp_test.c"
25
26
27 /*
28 * TCP transport test.
29 */
30 #if PJ_HAS_TCP
31
32 static pj_status_t multi_listener_test(pjsip_tpfactory *factory[],
33 unsigned num_factory,
34 pjsip_transport *tp[],
35 unsigned *num_tp)
36 {
37 pj_status_t status;
38 unsigned i = 0;
39 pj_str_t s;
40 pjsip_transport *tcp;
41 pjsip_tpfactory *tpfactory = NULL;
42 pj_sockaddr_in rem_addr;
43 pjsip_tpselector tp_sel;
44 unsigned ntp = 0;
45
46 for (;i<num_factory;++i)
47 {
48 /* Start TCP listener on arbitrary port. */
49 status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory);
50 if (status != PJ_SUCCESS) {
51 app_perror(" Error: unable to start TCP transport", status);
52 return -10;
53 }
54
55 factory[i] = tpfactory;
56 }
57
58 /* Get the last listener address */
59 status = pj_sockaddr_in_init(&rem_addr, &tpfactory->addr_name.host,
60 (pj_uint16_t)tpfactory->addr_name.port);
61 if (status != PJ_SUCCESS) {
62 app_perror(" Error: possibly invalid TCP address name", status);
63 return -11;
64 }
65
66 /* Acquire transport without selector. */
67 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP,
68 &rem_addr, sizeof(rem_addr),
69 NULL, &tcp);
70 if (status != PJ_SUCCESS || tcp == NULL) {
71 app_perror(" Error: unable to acquire TCP transport", status);
72 return -12;
73 }
74 tp[ntp++] = tcp;
75
76 /* After pjsip_endpt_acquire_transport, TCP transport must have
77 * reference counter 1.
78 */
79 if (pj_atomic_get(tcp->ref_cnt) != 1)
80 return -13;
81
82 /* Acquire with the same remote address, should return the same tp. */
83 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP,
84 &rem_addr, sizeof(rem_addr),
85 NULL, &tcp);
86 if (status != PJ_SUCCESS || tcp == NULL) {
87 app_perror(" Error: unable to acquire TCP transport", status);
88 return -14;
89 }
90
91 /* Should return existing transport. */
92 if (tp[ntp-1] != tcp) {
93 return -15;
94 }
95
96 /* Using the same TCP transport, it must have reference counter 2.
97 */
98 if (pj_atomic_get(tcp->ref_cnt) != 2)
99 return -16;
100
101 /* Decrease the reference. */
102 pjsip_transport_dec_ref(tcp);
103
104 /* Test basic transport attributes */
105 status = generic_transport_test(tcp);
106 if (status != PJ_SUCCESS)
107 return status;
108
109 /* Check again that reference counter is 1. */
110 if (pj_atomic_get(tcp->ref_cnt) != 1)
111 return -17;
112
113 /* Acquire transport test with selector. */
114 pj_bzero(&tp_sel, sizeof(tp_sel));
115 tp_sel.type = PJSIP_TPSELECTOR_LISTENER;
116 tp_sel.u.listener = factory[num_factory/2];
117 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
118 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP,
119 &rem_addr, sizeof(rem_addr),
120 &tp_sel, &tcp);
121 if (status != PJ_SUCCESS) {
122 app_perror(" Error: unable to acquire TCP transport", status);
123 return -18;
124 }
125
126 /* The transport should have the same factory set on the selector. */
127 if (tcp->factory != factory[num_factory/2])
128 return -19;
129
130 /* The transport should be newly created. */
131 for (i = 0; i < ntp; ++i) {
132 if (tp[i] == tcp) {
133 break;
134 }
135 }
136 if (i != ntp)
137 return -20;
138
139 tp[ntp++] = tcp;
140
141 for (i = 0; i<ntp; ++i) {
142 if (pj_atomic_get(tp[i]->ref_cnt) != 1)
143 return -21;
144 }
145 *num_tp = ntp;
146
147 return PJ_SUCCESS;
148 }
149
150 int transport_tcp_test(void)
151 {
152 enum { SEND_RECV_LOOP = 8 };
153 enum { NUM_LISTENER = 4 };
154 enum { NUM_TP = 8 };
155 pjsip_tpfactory *tpfactory[NUM_LISTENER];
156 pjsip_transport *tcp[NUM_TP];
157 pj_sockaddr_in rem_addr;
158 pj_status_t status;
159 char host_port_param[PJSIP_MAX_URL_SIZE];
160 char addr[PJ_INET_ADDRSTRLEN];
161 int rtt[SEND_RECV_LOOP], min_rtt;
162 int pkt_lost;
163 unsigned i;
164 unsigned num_listener = NUM_LISTENER;
165 unsigned num_tp = NUM_TP;
166
167 status = multi_listener_test(tpfactory, num_listener, tcp, &num_tp);
168 if (status != PJ_SUCCESS)
169 return status;
170
171 /* Get the last listener address */
172 status = pj_sockaddr_in_init(&rem_addr, &tpfactory[0]->addr_name.host,
173 (pj_uint16_t)tpfactory[0]->addr_name.port);
174
175 pj_ansi_snprintf(host_port_param, sizeof(host_port_param),
176 "%s:%d;transport=tcp",
177 pj_inet_ntop2(pj_AF_INET(), &rem_addr.sin_addr, addr,
178 sizeof(addr)),
179 pj_ntohs(rem_addr.sin_port));
180
181 /* Basic transport's send/receive loopback test. */
182 for (i=0; i<SEND_RECV_LOOP; ++i) {
183 status = transport_send_recv_test(PJSIP_TRANSPORT_TCP, tcp[0],
184 host_port_param, &rtt[i]);
185
186 if (status != 0) {
187 for (i = 0; i < num_tp ; ++i) {
188 pjsip_transport_dec_ref(tcp[i]);
189 }
190 flush_events(500);
191 return -72;
192 }
193 }
194
195 min_rtt = 0xFFFFFFF;
196 for (i=0; i<SEND_RECV_LOOP; ++i)
197 if (rtt[i] < min_rtt) min_rtt = rtt[i];
198
199 report_ival("tcp-rtt-usec", min_rtt, "usec",
200 "Best TCP transport round trip time, in microseconds "
201 "(time from sending request until response is received. "
202 "Tests were performed on local machine only, and after "
203 "TCP socket has been established by previous test)");
204
205
206 /* Multi-threaded round-trip test. */
207 status = transport_rt_test(PJSIP_TRANSPORT_TCP, tcp[0], host_port_param,
208 &pkt_lost);
209 if (status != 0) {
210 for (i = 0; i < num_tp ; ++i) {
211 pjsip_transport_dec_ref(tcp[i]);
212 }
213 return status;
214 }
215
216 if (pkt_lost != 0)
217 PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost));
218
219 /* Load test */
220 if ((status=transport_load_test(PJSIP_TRANSPORT_TCP,
221 host_port_param)) != 0)
222 {
223 return status;
224 }
225
226 /* Check again that reference counter is still 1. */
227 for (i = 0; i < num_tp; ++i) {
228 if (pj_atomic_get(tcp[i]->ref_cnt) != 1)
229 return -80;
230 }
231
232 for (i = 0; i < num_tp; ++i) {
233 /* Destroy this transport. */
234 pjsip_transport_dec_ref(tcp[i]);
235
236 /* Force destroy this transport. */
237 status = pjsip_transport_destroy(tcp[i]);
238 if (status != PJ_SUCCESS)
239 return -90;
240 }
241
242 for (i = 0; i < num_listener; ++i) {
243 /* Unregister factory */
244 status = pjsip_tpmgr_unregister_tpfactory(pjsip_endpt_get_tpmgr(endpt),
245 tpfactory[i]);
246 if (status != PJ_SUCCESS)
247 return -95;
248 }
249
250 /* Flush events. */
251 PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second..."));
252 flush_events(1000);
253
254 /* Done */
255 return 0;
256 }
257 #else /* PJ_HAS_TCP */
258 int transport_tcp_test(void)
259 {
260 return 0;
261 }
262 #endif /* PJ_HAS_TCP */