Debian Codegraph @

Xpjproject/pjsip/src/test/tsx_basic_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 "tsx_basic_test.c"
25
26 static struct tsx_basic_test_global_t
27 {
28 char TARGET_URI[PJSIP_MAX_URL_SIZE];
29 char FROM_URI[PJSIP_MAX_URL_SIZE];
30 pjsip_transport *tp;
31 } g[MAX_TSX_TESTS];
32
33
34 /* Test transaction layer. */
35 static int tsx_layer_test(unsigned tid)
36 {
37 pj_str_t target, from, tsx_key;
38 pjsip_tx_data *tdata;
39 pjsip_transaction *tsx, *found;
40 pj_status_t status;
41
42 PJ_LOG(3,(THIS_FILE, " transaction layer test"));
43
44 target = pj_str(g[tid].TARGET_URI);
45 from = pj_str(g[tid].FROM_URI);
46
47 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
48 &from, &target, NULL, NULL, -1, NULL,
49 &tdata);
50 if (status != PJ_SUCCESS) {
51 app_perror(" error: unable to create request", status);
52 return -110;
53 }
54
55 status = pjsip_tsx_create_uac(NULL, tdata, &tsx);
56 if (status != PJ_SUCCESS) {
57 app_perror(" error: unable to create transaction", status);
58 return -120;
59 }
60
61 if (g[tid].tp) {
62 pjsip_tpselector tp_sel;
63
64 pj_bzero(&tp_sel, sizeof(tp_sel));
65 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
66 tp_sel.u.transport = g[tid].tp;
67
68 pjsip_tsx_set_transport(tsx, &tp_sel);
69 }
70
71 pj_strdup(tdata->pool, &tsx_key, &tsx->transaction_key);
72
73 found = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE);
74 if (found != tsx) {
75 return -130;
76 }
77
78 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
79 flush_events(500);
80
81 if (pjsip_tx_data_dec_ref(tdata) != PJSIP_EBUFDESTROYED) {
82 return -140;
83 }
84
85 return 0;
86 }
87
88 /* Double terminate test. */
89 static int double_terminate(unsigned tid)
90 {
91 pj_str_t target, from, tsx_key;
92 pjsip_tx_data *tdata;
93 pjsip_transaction *tsx;
94 pj_status_t status;
95
96 PJ_LOG(3,(THIS_FILE, " double terminate test"));
97
98 target = pj_str(g[tid].TARGET_URI);
99 from = pj_str(g[tid].FROM_URI);
100
101 /* Create request. */
102 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
103 &from, &target, NULL, NULL, -1, NULL,
104 &tdata);
105 if (status != PJ_SUCCESS) {
106 app_perror(" error: unable to create request", status);
107 return -10;
108 }
109
110 /* Create transaction. */
111 status = pjsip_tsx_create_uac(NULL, tdata, &tsx);
112 if (status != PJ_SUCCESS) {
113 app_perror(" error: unable to create transaction", status);
114 return -20;
115 }
116
117 if (g[tid].tp) {
118 pjsip_tpselector tp_sel;
119
120 pj_bzero(&tp_sel, sizeof(tp_sel));
121 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
122 tp_sel.u.transport = g[tid].tp;
123
124 pjsip_tsx_set_transport(tsx, &tp_sel);
125 }
126
127 /* Save transaction key for later. */
128 pj_strdup_with_null(tdata->pool, &tsx_key, &tsx->transaction_key);
129
130 /* Add reference to transmit buffer (tsx_send_msg() will dec txdata). */
131 pjsip_tx_data_add_ref(tdata);
132
133 /* Send message to start timeout timer. */
134 status = pjsip_tsx_send_msg(tsx, NULL);
135
136 /* Terminate transaction. */
137 status = pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
138 if (status != PJ_SUCCESS) {
139 app_perror(" error: unable to terminate transaction", status);
140 return -30;
141 }
142
143 tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
144 if (tsx) {
145 /* Terminate transaction again. */
146 status = pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
147 if (status != PJ_SUCCESS) {
148 app_perror(" error: unable to terminate transaction", status);
149 return -40;
150 }
151 pj_grp_lock_release(tsx->grp_lock);
152 }
153
154 flush_events(500);
155 if (pjsip_tx_data_dec_ref(tdata) != PJSIP_EBUFDESTROYED) {
156 return -50;
157 }
158
159 return PJ_SUCCESS;
160 }
161
162 int tsx_basic_test(unsigned tid)
163 {
164 struct tsx_test_param *param = &tsx_test[tid];
165 pjsip_transport *loop = NULL;
166 int status;
167
168 g[tid].tp = NULL;
169
170 if (param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
171 PJ_TEST_SUCCESS(pjsip_loop_start(endpt, &loop), NULL, return -10);
172 pjsip_transport_add_ref(loop);
173 g[tid].tp = loop;
174 }
175 pj_ansi_snprintf(g[tid].TARGET_URI, sizeof(g[tid].TARGET_URI),
176 "sip:[email protected]:%d;transport=%s",
177 param->port, param->tp_type);
178 pj_ansi_snprintf(g[tid].FROM_URI, sizeof(g[tid].FROM_URI),
179 "sip:[email protected]:%d;transport=%s",
180 param->port, param->tp_type);
181
182 status = tsx_layer_test(tid);
183 if (status != 0)
184 goto on_return;
185
186 status = double_terminate(tid);
187 if (status != 0)
188 goto on_return;
189
190 status = 0;
191
192 on_return:
193 if (loop) {
194 /* Order must be shutdown then dec_ref so it gets destroyed */
195 pjsip_transport_shutdown(loop);
196 pjsip_transport_dec_ref(loop);
197 flush_events(500);
198 }
199 return status;
200 }
201
202 /**************************************************************************/
203
204 struct tsx_test_state
205 {
206 pj_size_t pool_cnt;
207 };
208
209 static void save_tsx_test_state(struct tsx_test_state *st)
210 {
211 st->pool_cnt = caching_pool.used_count;
212 }
213
214 static pj_status_t check_tsx_test_state(struct tsx_test_state *st)
215 {
216 if (caching_pool.used_count > st->pool_cnt)
217 return -1;
218
219 return 0;
220 }
221
222 static void destroy_endpt()
223 {
224 pjsip_endpt_destroy(endpt);
225 endpt = NULL;
226 }
227
228 static pj_status_t init_endpt()
229 {
230 pj_str_t ns = { "10.187.27.172", 13}; /* just a random, unreachable IP */
231 pj_dns_resolver *resolver;
232 pj_status_t rc;
233
234 rc = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt);
235 if (rc != PJ_SUCCESS) {
236 app_perror("pjsip_endpt_create", rc);
237 return rc;
238 }
239
240 /* Start transaction layer module. */
241 rc = pjsip_tsx_layer_init_module(endpt);
242 if (rc != PJ_SUCCESS) {
243 app_perror("tsx_layer_init", rc);
244 return rc;
245 }
246
247 rc = pjsip_udp_transport_start(endpt, NULL, NULL, 1, NULL);
248 if (rc != PJ_SUCCESS) {
249 app_perror("udp init", rc);
250 return rc;
251 }
252
253 rc = pjsip_tcp_transport_start(endpt, NULL, 1, NULL);
254 if (rc != PJ_SUCCESS) {
255 app_perror("tcp init", rc);
256 return rc;
257 }
258
259 rc = pjsip_endpt_create_resolver(endpt, &resolver);
260 if (rc != PJ_SUCCESS) {
261 app_perror("create resolver", rc);
262 return rc;
263 }
264
265 pj_dns_resolver_set_ns(resolver, 1, &ns, NULL);
266
267 rc = pjsip_endpt_set_resolver(endpt, resolver);
268 if (rc != PJ_SUCCESS) {
269 app_perror("set resolver", rc);
270 return rc;
271 }
272
273 return PJ_SUCCESS;
274 }
275
276 static int tsx_create_and_send_req(void *arg)
277 {
278 pj_str_t dst_uri = pj_str((char*)arg);
279 pj_str_t from_uri = pj_str((char*)"<sip:user@host>");
280 pjsip_tx_data *tdata;
281 pj_status_t status;
282
283 status = pjsip_endpt_create_request(endpt, &pjsip_options_method,
284 &dst_uri, &from_uri, &dst_uri,
285 NULL, NULL, -1, NULL,
286 &tdata);
287 if (status != PJ_SUCCESS)
288 return status;
289
290 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
291 if (status != PJ_SUCCESS)
292 return status;
293
294 return PJ_SUCCESS;
295 }
296
297 int tsx_destroy_test()
298 {
299 struct tsx_test_state state;
300 struct test_desc
301 {
302 const char *title;
303 int (*func)(void*);
304 void *arg;
305 int sleep_before_unload;
306 int sleep_after_unload;
307 } test_entries[] =
308 {
309 {
310 "normal unable to resolve",
311 &tsx_create_and_send_req,
312 "sip:user@somehost",
313 20000,
314 1
315 },
316 {
317 "resolve and destroy, wait",
318 &tsx_create_and_send_req,
319 "sip:user@somehost",
320 1,
321 20000
322 },
323 {
324 "tcp connect and destroy",
325 &tsx_create_and_send_req,
326 "sip:[email protected]:58517;transport=tcp",
327 60000,
328 1000
329 },
330 {
331 "tcp connect and destroy 2",
332 &tsx_create_and_send_req,
333 "sip:[email protected]:58517;transport=tcp",
334 1,
335 60000
336 },
337 };
338 int rc;
339 unsigned i;
340 const int INDENT = 2;
341
342 pj_log_add_indent(INDENT);
343 destroy_endpt();
344
345 for (i=0; i<PJ_ARRAY_SIZE(test_entries); ++i) {
346 struct test_desc *td = &test_entries[i];
347
348 PJ_LOG(3,(THIS_FILE, "%s", td->title));
349
350 pj_log_add_indent(INDENT);
351 save_tsx_test_state(&state);
352
353 rc = init_endpt();
354 if (rc != PJ_SUCCESS) {
355 pj_log_add_indent(-INDENT*2);
356 return -10;
357 }
358
359 rc = td->func(td->arg);
360 if (rc != PJ_SUCCESS) {
361 pj_log_add_indent(-INDENT*2);
362 return -20;
363 }
364
365 flush_events(td->sleep_before_unload);
366 pjsip_tsx_layer_destroy();
367 flush_events(td->sleep_after_unload);
368 destroy_endpt();
369
370 rc = check_tsx_test_state(&state);
371 if (rc != PJ_SUCCESS) {
372 init_endpt();
373 pj_log_add_indent(-INDENT*2);
374 return -30;
375 }
376
377 pj_log_add_indent(-INDENT);
378 }
379
380 init_endpt();
381
382 pj_log_add_indent(-INDENT);
383 return 0;
384 }
385