Debian Codegraph @

Xpjproject/pjsip/src/test/tsx_uac_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_uac_test.c"
25
26
27 /*****************************************************************************
28 **
29 ** UAC tests.
30 **
31 ** This file performs various tests for UAC transactions. Each test will have
32 ** a different Via branch param so that message receiver module and
33 ** transaction user module can identify which test is being carried out.
34 **
35 ** TEST1_BRANCH_ID
36 ** Perform basic retransmission and timeout test. Message receiver will
37 ** verify that retransmission is received at correct time.
38 ** This test verifies the following requirements:
39 ** - retransmit timer doubles for INVITE
40 ** - retransmit timer doubles and caps off for non-INVITE
41 ** - retransmit timer timer is precise
42 ** - correct timeout and retransmission count
43 ** Requirements not tested:
44 ** - retransmit timer only starts after resolving has completed.
45 **
46 ** TEST2_BRANCH_ID
47 ** Test scenario where resolver is unable to resolve destination host.
48 **
49 ** TEST3_BRANCH_ID
50 ** Test scenario where transaction is terminated while resolver is still
51 ** running.
52 **
53 ** TEST4_BRANCH_ID
54 ** Test scenario where transport failed after several retransmissions.
55 **
56 ** TEST5_BRANCH_ID
57 ** Test scenario where transaction is terminated by user after several
58 ** retransmissions.
59 **
60 ** TEST6_BRANCH_ID
61 ** Test successfull non-INVITE transaction.
62 ** It tests the following requirements:
63 ** - transaction correctly moves to COMPLETED state.
64 ** - retransmission must cease.
65 ** - tx_data must be maintained until state is terminated.
66 **
67 ** TEST7_BRANCH_ID
68 ** Test successfull non-INVITE transaction, with provisional response.
69 **
70 ** TEST8_BRANCH_ID
71 ** Test failed INVITE transaction (e.g. ACK must be received)
72 **
73 ** TEST9_BRANCH_ID
74 ** Test failed INVITE transaction with provisional response.
75 **
76 **
77 *****************************************************************************
78 */
79
80 static char *TEST1_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test01";
81 static char *TEST2_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test02";
82 static char *TEST3_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test03";
83 static char *TEST4_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test04";
84 static char *TEST5_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test05";
85 static char *TEST6_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test06";
86 static char *TEST7_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test07";
87 static char *TEST8_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test08";
88 static char *TEST9_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test09";
89
90 #define BRANCH_LEN (7+11)
91
92 /* Widen timing tolerance in CI mode — shared runners can be 5-10x slower. */
93 #define TEST1_ALLOWED_DIFF (test_app.ut_app.prm_ci_mode ? 2500 : 500)
94 #define TEST4_RETRANSMIT_CNT 3
95 #define TEST5_RETRANSMIT_CNT 3
96
97 struct my_timer
98 {
99 pj_timer_entry entry;
100 char key_buf[1024];
101 pj_str_t tsx_key;
102 };
103
104
105 static struct tsx_uac_test_global_t
106 {
107 char TARGET_URI[128];
108 char FROM_URI[128];
109 unsigned tp_flag;
110 struct tsx_test_param *test_param;
111
112 /* Static vars, which will be reset on each test. */
113 int recv_count;
114 pj_time_val recv_last;
115 pj_bool_t test_complete;
116
117 /* Loop transport instance, only valid when test_param->tp_type is loop. */
118 pjsip_transport *loop;
119
120 struct my_timer timer;
121
122 } g[MAX_TSX_TESTS];
123
124 static void tsx_user_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e);
125 static pj_bool_t msg_receiver_on_rx_request(pjsip_rx_data *rdata);
126
127 /* UAC transaction user module. */
128 static pjsip_module tsx_user =
129 {
130 NULL, NULL, /* prev and next */
131 { "Tsx-UAC-User", 12}, /* Name. */
132 -1, /* Id */
133 PJSIP_MOD_PRIORITY_APPLICATION-1, /* Priority */
134 NULL, /* load() */
135 NULL, /* start() */
136 NULL, /* stop() */
137 NULL, /* unload() */
138 NULL, /* on_rx_request() */
139 NULL, /* on_rx_response() */
140 NULL, /* on_tx_request() */
141 NULL, /* on_tx_response() */
142 &tsx_user_on_tsx_state, /* on_tsx_state() */
143 };
144
145 /* Module to receive the loop-backed request and also process tx msgs. */
146 static pjsip_module msg_receiver =
147 {
148 NULL, NULL, /* prev and next */
149 { "Msg-Receiver", 12}, /* Name. */
150 -1, /* Id */
151 /* Note:
152 * Priority needs to be more important than UA layer, because UA layer
153 * silently absorbs ACK with To tag.
154 */
155 PJSIP_MOD_PRIORITY_UA_PROXY_LAYER-1,/* Priority */
156 NULL, /* load() */
157 NULL, /* start() */
158 NULL, /* stop() */
159 NULL, /* unload() */
160 &msg_receiver_on_rx_request, /* on_rx_request() */
161 NULL, /* on_rx_response() */
162 NULL, /* on_tx_request() */
163 NULL, /* on_tx_response() */
164 NULL, /* on_tsx_state() */
165 };
166
167 /* Init uac test */
168 static int init_test(unsigned tid)
169 {
170 pj_enter_critical_section();
171 if (tsx_user.id == -1) {
172 PJ_TEST_SUCCESS(pjsip_endpt_register_module(endpt, &tsx_user), NULL,
173 { pj_leave_critical_section(); return -30; });
174 }
175
176 if (msg_receiver.id == -1) {
177 PJ_TEST_SUCCESS(pjsip_endpt_register_module(endpt, &msg_receiver), NULL,
178 { pj_leave_critical_section(); return -40; });
179 }
180 pj_leave_critical_section();
181
182 pj_assert(g[tid].loop==NULL);
183
184 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
185 PJ_TEST_SUCCESS(pjsip_loop_start(endpt, &g[tid].loop),
186 NULL, return -50);
187 pjsip_transport_add_ref(g[tid].loop);
188 }
189 return PJ_SUCCESS;
190 }
191
192 /* Finish test */
193 static void finish_test(unsigned tid)
194 {
195 /* Note: don't unregister modules on_tsx_state() may be called when
196 * transaction layer is shutdown later, which will cause
197 * get_tsx_tid() to be called and it needs the module id.
198 */
199 if (g[tid].loop) {
200 /* Order must be shutdown then dec_ref so it gets destroyed */
201 pjsip_transport_shutdown(g[tid].loop);
202 pjsip_transport_dec_ref(g[tid].loop);
203 g[tid].loop = 0;
204 }
205 }
206
207 /* Get test ID from transaction instance */
208 static unsigned get_tsx_tid(const pjsip_transaction *tsx)
209 {
210 pj_assert(tsx_user.id >= 0);
211 return (unsigned)(uintptr_t)tsx->mod_data[tsx_user.id];
212 }
213
214 /* Set test ID to transaction instance */
215 static void set_tsx_tid(pjsip_transaction *tsx, unsigned tid)
216 {
217 pj_assert(tsx_user.id >= 0);
218 tsx->mod_data[tsx_user.id] = (void*)(uintptr_t)tid;
219 }
220
221 /*
222 * This is the handler to receive state changed notification from the
223 * transaction. It is used to verify that the transaction behaves according
224 * to the test scenario.
225 */
226 static void tsx_user_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
227 {
228 unsigned tid = get_tsx_tid(tsx);
229
230 /*
231 PJ_LOG(3,(THIS_FILE,
232 " on_tsx_state state: %s, event: %s (%s)",
233 pjsip_tsx_state_str(tsx->state),
234 pjsip_event_str(e->type),
235 pjsip_event_str(e->body.tsx_state.type)
236 ));
237 */
238 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM &&
239 e->type==PJSIP_EVENT_TSX_STATE)
240 {
241 if (e->body.tsx_state.type==PJSIP_EVENT_RX_MSG) {
242 PJ_TEST_EQ( e->body.tsx_state.src.rdata->tp_info.transport,
243 g[tid].loop, NULL, {
244 g[tid].test_complete = -704;
245 return;
246 });
247 } else if (e->body.tsx_state.type==PJSIP_EVENT_TX_MSG &&
248 e->body.tsx_state.src.tdata->dest_info.addr.count) {
249 PJ_TEST_EQ( e->body.tsx_state.src.tdata->tp_info.transport,
250 g[tid].loop, NULL, {
251 g[tid].test_complete = -706;
252 return;
253 });
254 }
255 }
256
257 if (pj_strnicmp2(&tsx->branch, TEST1_BRANCH_ID, BRANCH_LEN)==0) {
258 /*
259 * Transaction with TEST1_BRANCH_ID should terminate with transaction
260 * timeout status.
261 */
262 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
263
264 if (g[tid].test_complete == 0)
265 g[tid].test_complete = 1;
266
267 /* Test the status code. */
268 if (tsx->status_code != PJSIP_SC_TSX_TIMEOUT) {
269 PJ_LOG(3,(THIS_FILE,
270 " error: status code is %d instead of %d",
271 tsx->status_code, PJSIP_SC_TSX_TIMEOUT));
272 g[tid].test_complete = -710;
273 }
274
275
276 /* If transport is reliable, then there must not be any
277 * retransmissions.
278 */
279 if (g[tid].tp_flag & PJSIP_TRANSPORT_RELIABLE) {
280 if (g[tid].recv_count != 1) {
281 PJ_LOG(3,(THIS_FILE,
282 " error: there were %d (re)transmissions",
283 g[tid].recv_count));
284 g[tid].test_complete = -715;
285 }
286 } else {
287 /* Check the number of (re)transmissions, which must be
288 * 6 or 7 for INVITE and 10 or 11 for non-INVITE.
289 * Theoretically the total (re)transmission time is 31,500ms
290 * (plus 300ms for delayed transport), and tsx timeout is 32s.
291 * In some test machines (e.g: MacOS), sometime the tsx timeout
292 * fires first which causes recv_count fall short (by one).
293 */
294 //if (tsx->method.id==PJSIP_INVITE_METHOD && recv_count != 7) {
295 if (tsx->method.id==PJSIP_INVITE_METHOD && g[tid].recv_count < 6) {
296 PJ_LOG(3,(THIS_FILE,
297 " error: there were %d (re)transmissions",
298 g[tid].recv_count));
299 g[tid].test_complete = -716;
300 } else
301 //if (tsx->method.id==PJSIP_OPTIONS_METHOD && recv_count != 11) {
302 if (tsx->method.id==PJSIP_OPTIONS_METHOD && g[tid].recv_count < 10) {
303 PJ_LOG(3,(THIS_FILE,
304 " error: there were %d (re)transmissions",
305 g[tid].recv_count));
306 g[tid].test_complete = -717;
307 } else
308 if (tsx->method.id!=PJSIP_INVITE_METHOD &&
309 tsx->method.id!=PJSIP_OPTIONS_METHOD)
310 {
311 PJ_LOG(3,(THIS_FILE, " error: unexpected method"));
312 g[tid].test_complete = -718;
313 }
314 }
315 }
316
317 } else if (pj_strnicmp2(&tsx->branch, TEST2_BRANCH_ID, BRANCH_LEN)==0) {
318 /*
319 * Transaction with TEST2_BRANCH_ID should terminate with transport error.
320 */
321 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
322
323 /* Test the status code. */
324 if (tsx->status_code != PJSIP_SC_TSX_TRANSPORT_ERROR &&
325 tsx->status_code != PJSIP_SC_BAD_GATEWAY)
326 {
327 PJ_LOG(3,(THIS_FILE,
328 " error: status code is %d instead of %d or %d",
329 tsx->status_code, PJSIP_SC_TSX_TRANSPORT_ERROR,
330 PJSIP_SC_BAD_GATEWAY));
331 g[tid].test_complete = -720;
332 }
333
334 if (g[tid].test_complete == 0)
335 g[tid].test_complete = 1;
336 }
337
338 } else if (pj_strnicmp2(&tsx->branch, TEST3_BRANCH_ID, BRANCH_LEN)==0) {
339 /*
340 * This test terminates the transaction while resolver is still
341 * running.
342 */
343 if (tsx->state == PJSIP_TSX_STATE_CALLING) {
344
345 /* Terminate the transaction. */
346 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
347
348 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
349
350 /* Check if status code is correct. */
351 if (tsx->status_code != PJSIP_SC_REQUEST_TERMINATED) {
352 PJ_LOG(3,(THIS_FILE,
353 " error: status code is %d instead of %d",
354 tsx->status_code, PJSIP_SC_REQUEST_TERMINATED));
355 g[tid].test_complete = -730;
356 }
357
358 if (g[tid].test_complete == 0)
359 g[tid].test_complete = 1;
360
361 }
362
363 } else if (pj_strnicmp2(&tsx->branch, TEST4_BRANCH_ID, BRANCH_LEN)==0) {
364 /*
365 * This test simulates transport failure after several
366 * retransmissions.
367 */
368 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
369
370 /* Status code must be transport error. */
371 if (tsx->status_code != PJSIP_SC_TSX_TRANSPORT_ERROR) {
372 PJ_LOG(3,(THIS_FILE,
373 " error: status code is %d instead of %d",
374 tsx->status_code, PJSIP_SC_TSX_TRANSPORT_ERROR));
375 g[tid].test_complete = -730;
376 }
377
378 /* Must have correct retransmission count. */
379 if (tsx->retransmit_count != TEST4_RETRANSMIT_CNT) {
380 PJ_LOG(3,(THIS_FILE,
381 " error: retransmit cnt is %d instead of %d",
382 tsx->retransmit_count, TEST4_RETRANSMIT_CNT));
383 g[tid].test_complete = -731;
384 }
385
386 if (g[tid].test_complete == 0)
387 g[tid].test_complete = 1;
388 }
389
390
391 } else if (pj_strnicmp2(&tsx->branch, TEST5_BRANCH_ID, BRANCH_LEN)==0) {
392 /*
393 * This test simulates transport failure after several
394 * retransmissions.
395 */
396 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
397
398 /* Status code must be PJSIP_SC_REQUEST_TERMINATED. */
399 if (tsx->status_code != PJSIP_SC_REQUEST_TERMINATED) {
400 PJ_LOG(3,(THIS_FILE,
401 " error: status code is %d instead of %d",
402 tsx->status_code, PJSIP_SC_REQUEST_TERMINATED));
403 g[tid].test_complete = -733;
404 }
405
406 /* Must have correct retransmission count. */
407 if (tsx->retransmit_count != TEST5_RETRANSMIT_CNT) {
408 PJ_LOG(3,(THIS_FILE,
409 " error: retransmit cnt is %d instead of %d",
410 tsx->retransmit_count, TEST5_RETRANSMIT_CNT));
411 g[tid].test_complete = -734;
412 }
413
414 if (g[tid].test_complete == 0)
415 g[tid].test_complete = 1;
416 }
417
418
419 } else if (pj_strnicmp2(&tsx->branch, TEST6_BRANCH_ID, BRANCH_LEN)==0) {
420 /*
421 * Successfull non-INVITE transaction.
422 */
423 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
424
425 /* Status code must be 202. */
426 if (tsx->status_code != 202) {
427 PJ_LOG(3,(THIS_FILE,
428 " error: status code is %d instead of %d",
429 tsx->status_code, 202));
430 g[tid].test_complete = -736;
431 }
432
433 /* Must have correct retransmission count. */
434 if (tsx->retransmit_count != 0) {
435 PJ_LOG(3,(THIS_FILE,
436 " error: retransmit cnt is %d instead of %d",
437 tsx->retransmit_count, 0));
438 g[tid].test_complete = -737;
439 }
440
441 /* Must still keep last_tx */
442 if (tsx->last_tx == NULL) {
443 PJ_LOG(3,(THIS_FILE,
444 " error: transaction lost last_tx"));
445 g[tid].test_complete = -738;
446 }
447
448 if (g[tid].test_complete == 0) {
449 g[tid].test_complete = 1;
450 pjsip_tsx_terminate(tsx, 202);
451 }
452
453 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
454
455 /* Previous state must be COMPLETED. */
456 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
457 g[tid].test_complete = -7381;
458 }
459
460 }
461
462 } else if (pj_strnicmp2(&tsx->branch, TEST7_BRANCH_ID, BRANCH_LEN)==0) {
463 /*
464 * Successfull non-INVITE transaction.
465 */
466 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
467
468 /* Check prev state. */
469 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_PROCEEDING) {
470 PJ_LOG(3,(THIS_FILE,
471 " error: prev state is %s instead of %s",
472 pjsip_tsx_state_str((pjsip_tsx_state_e)e->body.tsx_state.prev_state),
473 pjsip_tsx_state_str(PJSIP_TSX_STATE_PROCEEDING)));
474 g[tid].test_complete = -739;
475 }
476
477 /* Status code must be 202. */
478 if (tsx->status_code != 202) {
479 PJ_LOG(3,(THIS_FILE,
480 " error: status code is %d instead of %d",
481 tsx->status_code, 202));
482 g[tid].test_complete = -740;
483 }
484
485 /* Must have correct retransmission count. */
486 if (tsx->retransmit_count != 0) {
487 PJ_LOG(3,(THIS_FILE,
488 " error: retransmit cnt is %d instead of %d",
489 tsx->retransmit_count, 0));
490 g[tid].test_complete = -741;
491 }
492
493 /* Must still keep last_tx */
494 if (tsx->last_tx == NULL) {
495 PJ_LOG(3,(THIS_FILE,
496 " error: transaction lost last_tx"));
497 g[tid].test_complete = -741;
498 }
499
500 if (g[tid].test_complete == 0) {
501 g[tid].test_complete = 1;
502 pjsip_tsx_terminate(tsx, 202);
503 }
504
505 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
506
507 /* Previous state must be COMPLETED. */
508 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
509 g[tid].test_complete = -742;
510 }
511
512 }
513
514
515 } else if (pj_strnicmp2(&tsx->branch, TEST8_BRANCH_ID, BRANCH_LEN)==0) {
516 /*
517 * Failed INVITE transaction.
518 */
519 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
520
521 /* Status code must be 301. */
522 if (tsx->status_code != 301) {
523 PJ_LOG(3,(THIS_FILE,
524 " error: status code is %d instead of %d",
525 tsx->status_code, 301));
526 g[tid].test_complete = -745;
527 }
528
529 /* Must have correct retransmission count. */
530 if (tsx->retransmit_count != 0) {
531 PJ_LOG(3,(THIS_FILE,
532 " error: retransmit cnt is %d instead of %d",
533 tsx->retransmit_count, 0));
534 g[tid].test_complete = -746;
535 }
536
537 /* Must still keep last_tx */
538 if (tsx->last_tx == NULL) {
539 PJ_LOG(3,(THIS_FILE,
540 " error: transaction lost last_tx"));
541 g[tid].test_complete = -747;
542 }
543
544 /* last_tx MUST be the INVITE request
545 * (authorization depends on this behavior)
546 */
547 if (tsx->last_tx && tsx->last_tx->msg->line.req.method.id !=
548 PJSIP_INVITE_METHOD)
549 {
550 PJ_LOG(3,(THIS_FILE,
551 " error: last_tx is not INVITE"));
552 g[tid].test_complete = -748;
553 }
554 }
555 else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
556
557 g[tid].test_complete = 1;
558
559 /* Previous state must be COMPLETED. */
560 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
561 PJ_LOG(3,(THIS_FILE,
562 " error: expecting last state=COMLETED instead of %d",
563 e->body.tsx_state.prev_state));
564 g[tid].test_complete = -750;
565 }
566
567 /* Status code must be 301. */
568 if (tsx->status_code != 301) {
569 PJ_LOG(3,(THIS_FILE,
570 " error: status code is %d instead of %d",
571 tsx->status_code, 301));
572 g[tid].test_complete = -751;
573 }
574
575 }
576
577
578 } else if (pj_strnicmp2(&tsx->branch, TEST9_BRANCH_ID, BRANCH_LEN)==0) {
579 /*
580 * Failed INVITE transaction with provisional response.
581 */
582 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
583
584 /* Previous state must be PJSIP_TSX_STATE_PROCEEDING. */
585 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_PROCEEDING) {
586 g[tid].test_complete = -760;
587 }
588
589 /* Status code must be 302. */
590 if (tsx->status_code != 302) {
591 PJ_LOG(3,(THIS_FILE,
592 " error: status code is %d instead of %d",
593 tsx->status_code, 302));
594 g[tid].test_complete = -761;
595 }
596
597 /* Must have correct retransmission count. */
598 if (tsx->retransmit_count != 0) {
599 PJ_LOG(3,(THIS_FILE,
600 " error: retransmit cnt is %d instead of %d",
601 tsx->retransmit_count, 0));
602 g[tid].test_complete = -762;
603 }
604
605 /* Must still keep last_tx */
606 if (tsx->last_tx == NULL) {
607 PJ_LOG(3,(THIS_FILE,
608 " error: transaction lost last_tx"));
609 g[tid].test_complete = -763;
610 }
611
612 /* last_tx MUST be INVITE.
613 * (authorization depends on this behavior)
614 */
615 if (tsx->last_tx && tsx->last_tx->msg->line.req.method.id !=
616 PJSIP_INVITE_METHOD)
617 {
618 PJ_LOG(3,(THIS_FILE,
619 " error: last_tx is not INVITE"));
620 g[tid].test_complete = -764;
621 }
622
623 }
624 else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
625
626 g[tid].test_complete = 1;
627
628 /* Previous state must be COMPLETED. */
629 if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
630 g[tid].test_complete = -767;
631 }
632
633 /* Status code must be 302. */
634 if (tsx->status_code != 302) {
635 PJ_LOG(3,(THIS_FILE,
636 " error: status code is %d instead of %d",
637 tsx->status_code, 302));
638 g[tid].test_complete = -768;
639 }
640
641 }
642
643 }
644 }
645
646 /*
647 * This timer callback is called to send delayed response.
648 */
649 struct response
650 {
651 pjsip_response_addr res_addr;
652 pjsip_tx_data *tdata;
653 };
654
655 static void send_response_callback( pj_timer_heap_t *timer_heap,
656 struct pj_timer_entry *entry)
657 {
658 struct response *r = (struct response*) entry->user_data;
659 pjsip_transport *tp = r->res_addr.transport;
660 pj_status_t status;
661
662 PJ_UNUSED_ARG(timer_heap);
663
664 status = pjsip_endpt_send_response(endpt, &r->res_addr, r->tdata, NULL, NULL);
665 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(r->tdata);
666 if (tp)
667 pjsip_transport_dec_ref(tp);
668 }
669
670 /* Timer callback to terminate a transaction. */
671 static void terminate_tsx_callback( pj_timer_heap_t *timer_heap,
672 struct pj_timer_entry *entry)
673 {
674 struct my_timer *m = (struct my_timer *)entry;
675 pjsip_transaction *tsx = pjsip_tsx_layer_find_tsx(&m->tsx_key, PJ_FALSE);
676 int status_code = entry->id;
677
678 PJ_UNUSED_ARG(timer_heap);
679
680 if (tsx) {
681 pjsip_tsx_terminate(tsx, status_code);
682 }
683 }
684
685
686 #define DIFF(a,b) ((a<b) ? (b-a) : (a-b))
687
688 /*
689 * This is the handler to receive message for this test. It is used to
690 * control and verify the behavior of the message transmitted by the
691 * transaction.
692 */
693 static pj_bool_t msg_receiver_on_rx_request(pjsip_rx_data *rdata)
694 {
695 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
696 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
697 pjsip_to_hdr *from_hdr = rdata->msg_info.from;
698 pjsip_sip_uri *from_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(from_hdr->uri);
699 unsigned tid;
700
701 if (pj_strcmp2(&from_uri->user, "tsx_uac_test")) {
702 /* Not our message */
703 return PJ_FALSE;
704 }
705
706 tid = (unsigned)pj_strtoul(&target->user);
707
708 /*
709 PJ_LOG(3,(THIS_FILE, " on_rx_request %s (recv_count: %d) on %s, branch: %.*s",
710 pjsip_rx_data_get_info(rdata),
711 g[tid].recv_count, rdata->tp_info.transport->info,
712 (int)rdata->msg_info.via->branch_param.slen,
713 rdata->msg_info.via->branch_param.ptr));
714 */
715
716 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
717 PJ_TEST_EQ(rdata->tp_info.transport, g[tid].loop, NULL,
718 {g[tid].test_complete = -602; return PJ_TRUE;});
719 }
720
721 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST1_BRANCH_ID,
722 BRANCH_LEN) == 0) {
723 /*
724 * The TEST1_BRANCH_ID test performs the verifications for transaction
725 * retransmission mechanism. It will not answer the incoming request
726 * with any response.
727 */
728 pjsip_msg *msg = rdata->msg_info.msg;
729
730 PJ_LOG(4,(THIS_FILE, " received request"));
731
732 /* Only wants to take INVITE or OPTIONS method. */
733 if (msg->line.req.method.id != PJSIP_INVITE_METHOD &&
734 msg->line.req.method.id != PJSIP_OPTIONS_METHOD)
735 {
736 PJ_LOG(3,(THIS_FILE, " error: received unexpected method %.*s",
737 (int)msg->line.req.method.name.slen,
738 msg->line.req.method.name.ptr));
739 g[tid].test_complete = -600;
740 return PJ_TRUE;
741 }
742
743 if (g[tid].recv_count == 0) {
744 g[tid].recv_count++;
745 //pj_gettimeofday(&recv_last);
746 g[tid].recv_last = rdata->pkt_info.timestamp;
747 } else {
748 pj_time_val now;
749 unsigned msec_expected, msec_elapsed;
750 int max_received;
751
752 //pj_gettimeofday(&now);
753 now = rdata->pkt_info.timestamp;
754 PJ_TIME_VAL_SUB(now, g[tid].recv_last);
755 msec_elapsed = now.sec*1000 + now.msec;
756
757 ++g[tid].recv_count;
758 msec_expected = (1<<(g[tid].recv_count-2))*pjsip_cfg()->tsx.t1;
759
760 if (msg->line.req.method.id != PJSIP_INVITE_METHOD) {
761 if (msec_expected > pjsip_cfg()->tsx.t2)
762 msec_expected = pjsip_cfg()->tsx.t2;
763 max_received = 11;
764 } else {
765 max_received = 7;
766 }
767
768 if (DIFF(msec_expected, msec_elapsed) > TEST1_ALLOWED_DIFF) {
769 PJ_LOG(3,(THIS_FILE,
770 " error: expecting retransmission no. %d in %d "
771 "ms, received in %d ms",
772 g[tid].recv_count-1, msec_expected, msec_elapsed));
773 g[tid].test_complete = -610;
774 }
775
776
777 if (g[tid].recv_count > max_received) {
778 PJ_LOG(3,(THIS_FILE,
779 " error: too many messages (%d) received",
780 g[tid].recv_count));
781 g[tid].test_complete = -620;
782 }
783
784 //pj_gettimeofday(&recv_last);
785 g[tid].recv_last = rdata->pkt_info.timestamp;
786 }
787 return PJ_TRUE;
788
789 } else
790 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST4_BRANCH_ID,
791 BRANCH_LEN) == 0) {
792 /*
793 * The TEST4_BRANCH_ID test simulates transport failure after several
794 * retransmissions.
795 */
796 g[tid].recv_count++;
797
798 if (g[tid].recv_count == TEST4_RETRANSMIT_CNT) {
799 /* Simulate transport failure. */
800 pjsip_loop_set_failure(g[tid].loop, 2, NULL);
801
802 } else if (g[tid].recv_count > TEST4_RETRANSMIT_CNT) {
803 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
804 g[tid].recv_count));
805 g[tid].test_complete = -631;
806 }
807
808 return PJ_TRUE;
809
810
811 } else
812 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST5_BRANCH_ID,
813 BRANCH_LEN) == 0) {
814 /*
815 * The TEST5_BRANCH_ID test simulates user terminating the transaction
816 * after several retransmissions.
817 */
818 g[tid].recv_count++;
819
820 if (g[tid].recv_count == TEST5_RETRANSMIT_CNT+1) {
821 pj_str_t key;
822 pjsip_transaction *tsx;
823
824 pjsip_tsx_create_key( rdata->tp_info.pool, &key, PJSIP_ROLE_UAC,
825 &rdata->msg_info.msg->line.req.method, rdata);
826 tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE);
827 if (tsx) {
828 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
829 pj_grp_lock_release(tsx->grp_lock);
830 } else {
831 PJ_LOG(3,(THIS_FILE, " error: uac transaction not found!"));
832 g[tid].test_complete = -633;
833 }
834
835 } else if (g[tid].recv_count > TEST5_RETRANSMIT_CNT+1) {
836 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
837 g[tid].recv_count));
838 g[tid].test_complete = -634;
839 }
840
841 return PJ_TRUE;
842
843 } else
844 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST6_BRANCH_ID,
845 BRANCH_LEN) == 0) {
846 /*
847 * The TEST6_BRANCH_ID test successfull non-INVITE transaction.
848 */
849 pj_status_t status;
850
851 g[tid].recv_count++;
852
853 if (g[tid].recv_count > 1) {
854 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
855 g[tid].recv_count));
856 g[tid].test_complete = -635;
857 }
858
859 status = pjsip_endpt_respond_stateless(endpt, rdata, 202, NULL,
860 NULL, NULL);
861 if (status != PJ_SUCCESS) {
862 app_perror(" error: unable to send response", status);
863 g[tid].test_complete = -636;
864 }
865
866 return PJ_TRUE;
867
868
869 } else
870 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST7_BRANCH_ID,
871 BRANCH_LEN) == 0) {
872 /*
873 * The TEST7_BRANCH_ID test successfull non-INVITE transaction
874 * with provisional response.
875 */
876 pj_status_t status;
877 pjsip_response_addr res_addr;
878 struct response *r;
879 pjsip_tx_data *tdata;
880 pj_time_val delay = { 2, 0 };
881
882 g[tid].recv_count++;
883
884 if (g[tid].recv_count > 1) {
885 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
886 g[tid].recv_count));
887 g[tid].test_complete = -640;
888 return PJ_TRUE;
889 }
890
891 /* Respond with provisional response */
892 status = pjsip_endpt_create_response(endpt, rdata, 100, NULL, &tdata);
893 pj_assert(status == PJ_SUCCESS);
894
895 status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
896 pj_assert(status == PJ_SUCCESS);
897
898 status = pjsip_endpt_send_response(endpt, &res_addr, tdata,
899 NULL, NULL);
900 pj_assert(status == PJ_SUCCESS);
901
902 /* Create the final response. */
903 status = pjsip_endpt_create_response(endpt, rdata, 202, NULL, &tdata);
904 pj_assert(status == PJ_SUCCESS);
905
906 /* Schedule sending final response in couple of of secs. */
907 r = PJ_POOL_ALLOC_T(tdata->pool, struct response);
908 r->res_addr = res_addr;
909 r->tdata = tdata;
910 if (r->res_addr.transport)
911 pjsip_transport_add_ref(r->res_addr.transport);
912
913 g[tid].timer.entry.cb = &send_response_callback;
914 g[tid].timer.entry.user_data = r;
915 pjsip_endpt_schedule_timer(endpt, &g[tid].timer.entry, &delay);
916
917 return (status == PJ_SUCCESS);
918
919 } else
920 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST8_BRANCH_ID,
921 BRANCH_LEN) == 0) {
922 /*
923 * The TEST8_BRANCH_ID test failed INVITE transaction.
924 */
925 pjsip_method *method;
926 pj_status_t status;
927
928
929 method = &rdata->msg_info.msg->line.req.method;
930 g[tid].recv_count++;
931
932 if (method->id == PJSIP_INVITE_METHOD) {
933
934 if (g[tid].recv_count > 1) {
935 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
936 g[tid].recv_count));
937 g[tid].test_complete = -635;
938 }
939
940 status = pjsip_endpt_respond_stateless(endpt, rdata, 301, NULL,
941 NULL, NULL);
942 if (status != PJ_SUCCESS) {
943 app_perror(" error: unable to send response", status);
944 g[tid].test_complete = -636;
945 }
946
947 } else if (method->id == PJSIP_ACK_METHOD) {
948
949 if (g[tid].recv_count == 2) {
950 pj_str_t key;
951 pj_time_val delay = { 5, 0 };
952
953 /* Schedule timer to destroy transaction after 5 seconds.
954 * This is to make sure that transaction does not
955 * retransmit ACK.
956 */
957 pjsip_tsx_create_key(rdata->tp_info.pool, &key,
958 PJSIP_ROLE_UAC, &pjsip_invite_method,
959 rdata);
960
961 pj_strcpy(&g[tid].timer.tsx_key, &key);
962 g[tid].timer.entry.id = 301;
963 g[tid].timer.entry.cb = &terminate_tsx_callback;
964
965 pjsip_endpt_schedule_timer(endpt, &g[tid].timer.entry, &delay);
966 }
967
968 if (g[tid].recv_count > 2) {
969 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
970 g[tid].recv_count));
971 g[tid].test_complete = -638;
972 }
973
974
975 } else {
976 PJ_LOG(3,(THIS_FILE," error: not expecting %s",
977 pjsip_rx_data_get_info(rdata)));
978 g[tid].test_complete = -639;
979
980 }
981
982
983 } else
984 if (pj_strnicmp2(&rdata->msg_info.via->branch_param, TEST9_BRANCH_ID,
985 BRANCH_LEN) == 0) {
986 /*
987 * The TEST9_BRANCH_ID test failed INVITE transaction with
988 * provisional response.
989 */
990 pjsip_method *method;
991 pj_status_t status = PJ_SUCCESS;
992
993 method = &rdata->msg_info.msg->line.req.method;
994
995 g[tid].recv_count++;
996
997 if (method->id == PJSIP_INVITE_METHOD) {
998
999 pjsip_response_addr res_addr;
1000 struct response *r;
1001 pjsip_tx_data *tdata;
1002 pj_time_val delay = { 2, 0 };
1003
1004 if (g[tid].recv_count > 1) {
1005 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
1006 g[tid].recv_count));
1007 g[tid].test_complete = -650;
1008 return PJ_TRUE;
1009 }
1010
1011 /* Respond with provisional response */
1012 status = pjsip_endpt_create_response(endpt, rdata, 100, NULL,
1013 &tdata);
1014 pj_assert(status == PJ_SUCCESS);
1015
1016 status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
1017 pj_assert(status == PJ_SUCCESS);
1018
1019 status = pjsip_endpt_send_response(endpt, &res_addr, tdata,
1020 NULL, NULL);
1021 pj_assert(status == PJ_SUCCESS);
1022
1023 /* Create the final response. */
1024 status = pjsip_endpt_create_response(endpt, rdata, 302, NULL,
1025 &tdata);
1026 pj_assert(status == PJ_SUCCESS);
1027
1028 /* Schedule sending final response in couple of of secs. */
1029 r = PJ_POOL_ALLOC_T(tdata->pool, struct response);
1030 r->res_addr = res_addr;
1031 r->tdata = tdata;
1032 if (r->res_addr.transport)
1033 pjsip_transport_add_ref(r->res_addr.transport);
1034
1035 g[tid].timer.entry.cb = &send_response_callback;
1036 g[tid].timer.entry.user_data = r;
1037 pjsip_endpt_schedule_timer(endpt, &g[tid].timer.entry, &delay);
1038
1039 } else if (method->id == PJSIP_ACK_METHOD) {
1040
1041 if (g[tid].recv_count == 2) {
1042 pj_str_t key;
1043 pj_time_val delay = { 5, 0 };
1044
1045 /* Schedule timer to destroy transaction after 5 seconds.
1046 * This is to make sure that transaction does not
1047 * retransmit ACK.
1048 */
1049 pjsip_tsx_create_key(rdata->tp_info.pool, &key,
1050 PJSIP_ROLE_UAC, &pjsip_invite_method,
1051 rdata);
1052
1053 pj_strcpy(&g[tid].timer.tsx_key, &key);
1054 g[tid].timer.entry.id = 302;
1055 g[tid].timer.entry.cb = &terminate_tsx_callback;
1056
1057 pjsip_endpt_schedule_timer(endpt, &g[tid].timer.entry, &delay);
1058 }
1059
1060 if (g[tid].recv_count > 2) {
1061 PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
1062 g[tid].recv_count));
1063 g[tid].test_complete = -638;
1064 }
1065
1066
1067 } else {
1068 PJ_LOG(3,(THIS_FILE," error: not expecting %s",
1069 pjsip_rx_data_get_info(rdata)));
1070 g[tid].test_complete = -639;
1071
1072 }
1073
1074 return (status == PJ_SUCCESS);
1075
1076 }
1077
1078 return PJ_FALSE;
1079 }
1080
1081 /*
1082 * The generic test framework, used by most of the tests.
1083 */
1084 static int perform_tsx_test(unsigned tid, int dummy, char *target_uri,
1085 char *from_uri, char *branch_param, int test_time,
1086 const pjsip_method *method)
1087 {
1088 pjsip_tx_data *tdata;
1089 pjsip_transaction *tsx;
1090 pj_str_t target, from, tsx_key;
1091 pjsip_via_hdr *via;
1092 char branch_buf[BRANCH_LEN+20];
1093 pj_time_val timeout;
1094 pj_status_t status;
1095
1096 PJ_UNUSED_ARG(dummy);
1097 PJ_TEST_EQ(strlen(branch_param), BRANCH_LEN, NULL, return -99);
1098
1099 PJ_LOG(3,(THIS_FILE,
1100 " please standby, this will take at most %d seconds..",
1101 test_time));
1102
1103 /* Reset test. */
1104 g[tid].recv_count = 0;
1105 g[tid].test_complete = 0;
1106
1107 /* Init headers. */
1108 target = pj_str(target_uri);
1109 from = pj_str(from_uri);
1110
1111 /* Create request. */
1112 status = pjsip_endpt_create_request( endpt, method, &target,
1113 &from, &target, NULL, NULL, -1,
1114 NULL, &tdata);
1115 if (status != PJ_SUCCESS) {
1116 app_perror(" Error: unable to create request", status);
1117 return -105;
1118 }
1119
1120 /* Set the branch param. Note that other tsx_uac_test() instances may
1121 * be running simultaneously, thus the branch ID needs to be made unique
1122 * by adding tid */
1123 via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
1124 pj_ansi_snprintf(branch_buf, sizeof(branch_buf),
1125 "%s-%02d", branch_param, tid);
1126 pj_strdup2(tdata->pool, &via->branch_param, branch_buf);
1127
1128 /* Add additional reference to tdata to prevent transaction from
1129 * deleting it.
1130 */
1131 pjsip_tx_data_add_ref(tdata);
1132
1133 /* Create transaction. */
1134 status = pjsip_tsx_create_uac( &tsx_user, tdata, &tsx);
1135 if (status != PJ_SUCCESS) {
1136 app_perror(" Error: unable to create UAC transaction", status);
1137 pjsip_tx_data_dec_ref(tdata);
1138 return -110;
1139 }
1140 set_tsx_tid(tsx, tid);
1141
1142 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1143 pjsip_tpselector tp_sel;
1144
1145 pj_assert(g[tid].loop);
1146 pj_bzero(&tp_sel, sizeof(tp_sel));
1147 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1148 tp_sel.u.transport = g[tid].loop;
1149 pjsip_tsx_set_transport(tsx, &tp_sel);
1150 }
1151
1152 /* Get transaction key. */
1153 pj_strdup(tdata->pool, &tsx_key, &tsx->transaction_key);
1154
1155 /* Send the message. */
1156 status = pjsip_tsx_send_msg(tsx, NULL);
1157 // Ignore send result. Some tests do deliberately triggers error
1158 // when sending message.
1159 if (status != PJ_SUCCESS) {
1160 // app_perror(" Error: unable to send request", status);
1161 pjsip_tx_data_dec_ref(tdata);
1162 // return -120;
1163 }
1164
1165
1166 /* Set test completion time. */
1167 pj_gettimeofday(&timeout);
1168 timeout.sec += test_time;
1169
1170 /* Wait until test complete. */
1171 while (!g[tid].test_complete) {
1172 pj_time_val now, poll_delay = {0, 10};
1173
1174 pjsip_endpt_handle_events(endpt, &poll_delay);
1175
1176 pj_gettimeofday(&now);
1177 if (now.sec > timeout.sec) {
1178 PJ_LOG(3,(THIS_FILE, " Error: test has timed out"));
1179 pjsip_tx_data_dec_ref(tdata);
1180 return -130;
1181 }
1182 }
1183
1184 pjsip_endpt_stop_handle_events(endpt);
1185
1186 if (g[tid].test_complete < 0) {
1187 tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
1188 if (tsx) {
1189 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
1190 pj_grp_lock_release(tsx->grp_lock);
1191 flush_events(1000);
1192 }
1193 pjsip_tx_data_dec_ref(tdata);
1194 return g[tid].test_complete;
1195
1196 } else {
1197 pj_time_val now;
1198
1199 /* Allow transaction to destroy itself */
1200 flush_events(500);
1201
1202 /* Wait until test completes */
1203 pj_gettimeofday(&now);
1204
1205 if (PJ_TIME_VAL_LT(now, timeout)) {
1206 pj_time_val interval;
1207 interval = timeout;
1208 PJ_TIME_VAL_SUB(interval, now);
1209 flush_events(PJ_TIME_VAL_MSEC(interval));
1210 }
1211 }
1212
1213 /* Make sure transaction has been destroyed. */
1214 if (pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE) != NULL) {
1215 PJ_LOG(3,(THIS_FILE, " Error: transaction has not been destroyed"));
1216 pjsip_tx_data_dec_ref(tdata);
1217 return -140;
1218 }
1219
1220 /* Check tdata reference counter. */
1221 if (pj_atomic_get(tdata->ref_cnt) > 1) {
1222 PJ_LOG(3,(THIS_FILE, " Error: tdata reference counter is %ld",
1223 pj_atomic_get(tdata->ref_cnt)));
1224 while (pj_atomic_get(tdata->ref_cnt) > 1)
1225 pjsip_tx_data_dec_ref(tdata);
1226 return -150;
1227 }
1228
1229 /* Destroy txdata */
1230 pjsip_tx_data_dec_ref(tdata);
1231
1232 return PJ_SUCCESS;
1233 }
1234
1235 /*****************************************************************************
1236 **
1237 ** TEST1_BRANCH_ID: UAC basic retransmission and timeout test.
1238 **
1239 ** This will test the retransmission of the UAC transaction. Remote will not
1240 ** answer the transaction, so the transaction should fail. The Via branch prm
1241 ** TEST1_BRANCH_ID will be used for this test.
1242 **
1243 *****************************************************************************
1244 */
1245 static int tsx_uac_retransmit_test(unsigned tid)
1246 {
1247 int status = 0, enabled;
1248 int i;
1249 struct {
1250 const pjsip_method *method;
1251 unsigned delay;
1252 } sub_test[] =
1253 {
1254 { &pjsip_invite_method, 0},
1255 //{ &pjsip_invite_method, TEST1_ALLOWED_DIFF*2},
1256 { &pjsip_invite_method, 300},
1257
1258 { &pjsip_options_method, 0},
1259 //{ &pjsip_options_method, TEST1_ALLOWED_DIFF*2}
1260 { &pjsip_options_method, 300}
1261 };
1262
1263 PJ_LOG(3,(THIS_FILE, " test1: basic uac retransmit and timeout test"));
1264
1265 /* For this test. message printing shound be disabled because it makes
1266 * incorrect timing.
1267 */
1268 enabled = msg_logger_set_enabled(0);
1269
1270 for (i=0; i<(int)PJ_ARRAY_SIZE(sub_test); ++i) {
1271
1272 PJ_LOG(3,(THIS_FILE,
1273 " variant %c: %s with %d ms network delay",
1274 ('a' + i),
1275 sub_test[i].method->name.ptr,
1276 sub_test[i].delay));
1277
1278 /* Configure transport */
1279 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1280 pjsip_loop_set_failure(g[tid].loop, 0, NULL);
1281 pjsip_loop_set_recv_delay(g[tid].loop, sub_test[i].delay, NULL);
1282 }
1283
1284 /* Do the test. */
1285 status = perform_tsx_test(tid, -500, g[tid].TARGET_URI,
1286 g[tid].FROM_URI, TEST1_BRANCH_ID,
1287 35, sub_test[i].method);
1288 if (status != 0)
1289 break;
1290 }
1291
1292 /* Restore transport. */
1293 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1294 pjsip_loop_set_recv_delay(g[tid].loop, 0, NULL);
1295 }
1296
1297 /* Restore msg logger. */
1298 msg_logger_set_enabled(enabled);
1299
1300 /* Done. */
1301 return status;
1302 }
1303
1304 /*****************************************************************************
1305 **
1306 ** TEST2_BRANCH_ID: UAC resolve error test.
1307 **
1308 ** Test the scenario where destination host is unresolvable. There are
1309 ** two variants:
1310 ** (a) resolver returns immediate error
1311 ** (b) resolver returns error via the callback.
1312 **
1313 *****************************************************************************
1314 */
1315 static int tsx_resolve_error_test(unsigned tid)
1316 {
1317 char target[80];
1318 int status = 0;
1319
1320 PJ_LOG(3,(THIS_FILE, " test2: resolve error test"));
1321
1322 pj_ansi_snprintf(target, sizeof(target),
1323 "sip:%d@unresolved-host;transport=%s",
1324 tid, g[tid].test_param->tp_type);
1325
1326 /*
1327 * Variant (a): immediate resolve error.
1328 */
1329 PJ_LOG(3,(THIS_FILE, " variant a: immediate resolving error"));
1330
1331 status = perform_tsx_test(tid, -800, target,
1332 g[tid].FROM_URI, TEST2_BRANCH_ID, 20,
1333 &pjsip_options_method);
1334 if (status != 0)
1335 return status;
1336
1337 /*
1338 * Variant (b): error via callback.
1339 */
1340 PJ_LOG(3,(THIS_FILE, " variant b: error via callback"));
1341
1342 /* This only applies to "loop-dgram" transport */
1343 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1344 int prev_fail = 0;
1345 unsigned prev_delay = 0;
1346
1347 /* Set loop transport to return delayed error. */
1348 pjsip_loop_set_failure(g[tid].loop, 2, &prev_fail);
1349 pjsip_loop_set_send_callback_delay(g[tid].loop, 10, &prev_delay);
1350
1351 status = perform_tsx_test(tid, -800, g[tid].TARGET_URI,
1352 g[tid].FROM_URI, TEST2_BRANCH_ID, 2,
1353 &pjsip_options_method);
1354
1355 /* Restore loop transport settings. */
1356 pjsip_loop_set_failure(g[tid].loop, prev_fail, NULL);
1357 pjsip_loop_set_send_callback_delay(g[tid].loop, prev_delay, NULL);
1358
1359 if (status != 0)
1360 return status;
1361 }
1362
1363 return status;
1364 }
1365
1366
1367 /*****************************************************************************
1368 **
1369 ** TEST3_BRANCH_ID: UAC terminate while resolving test.
1370 **
1371 ** Terminate the transaction while resolver is still running.
1372 **
1373 *****************************************************************************
1374 */
1375 static int tsx_terminate_resolving_test(unsigned tid)
1376 {
1377 unsigned prev_delay = 0;
1378 pj_status_t status;
1379
1380 PJ_LOG(3,(THIS_FILE, " test3: terminate while resolving test"));
1381
1382 /* Configure transport delay. */
1383 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1384 pjsip_loop_set_send_callback_delay(g[tid].loop, 100, &prev_delay);
1385 }
1386
1387 /* Start the test. */
1388 status = perform_tsx_test(tid, -900, g[tid].TARGET_URI, g[tid].FROM_URI,
1389 TEST3_BRANCH_ID, 2, &pjsip_options_method);
1390
1391 /* Restore delay. */
1392 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1393 pjsip_loop_set_send_callback_delay(g[tid].loop, prev_delay, NULL);
1394 }
1395
1396 return status;
1397 }
1398
1399
1400 /*****************************************************************************
1401 **
1402 ** TEST4_BRANCH_ID: Transport failed after several retransmissions
1403 **
1404 ** There are two variants of this test: (a) failure occurs immediately when
1405 ** transaction calls pjsip_transport_send() or (b) failure is reported via
1406 ** transport callback.
1407 **
1408 *****************************************************************************
1409 */
1410 static int tsx_retransmit_fail_test(unsigned tid)
1411 {
1412 int i;
1413 unsigned delay[] = {0, 10};
1414 pj_status_t status = PJ_SUCCESS;
1415
1416 PJ_LOG(3,(THIS_FILE,
1417 " test4: transport fails after several retransmissions test"));
1418
1419
1420 for (i=0; i<(int)PJ_ARRAY_SIZE(delay); ++i) {
1421
1422 PJ_LOG(3,(THIS_FILE,
1423 " variant %c: transport delay %d ms", ('a'+i), delay[i]));
1424
1425 /* Configure transport delay. */
1426 pjsip_loop_set_send_callback_delay(g[tid].loop, delay[i], NULL);
1427
1428 /* Restore transport failure mode. */
1429 pjsip_loop_set_failure(g[tid].loop, 0, 0);
1430
1431 /* Start the test. */
1432 status = perform_tsx_test(tid, -1000, g[tid].TARGET_URI, g[tid].FROM_URI,
1433 TEST4_BRANCH_ID, 6, &pjsip_options_method);
1434
1435 if (status != 0)
1436 break;
1437
1438 }
1439
1440 /* Restore delay. */
1441 pjsip_loop_set_send_callback_delay(g[tid].loop, 0, NULL);
1442
1443 /* Restore transport failure mode. */
1444 pjsip_loop_set_failure(g[tid].loop, 0, 0);
1445
1446 return status;
1447 }
1448
1449
1450 /*****************************************************************************
1451 **
1452 ** TEST5_BRANCH_ID: Terminate transaction after several retransmissions
1453 **
1454 *****************************************************************************
1455 */
1456 static int tsx_terminate_after_retransmit_test(unsigned tid)
1457 {
1458 int status;
1459
1460 PJ_LOG(3,(THIS_FILE, " test5: terminate after retransmissions"));
1461
1462 /* Do the test. */
1463 status = perform_tsx_test(tid, -1100, g[tid].TARGET_URI, g[tid].FROM_URI,
1464 TEST5_BRANCH_ID,
1465 6, &pjsip_options_method);
1466
1467 /* Done. */
1468 return status;
1469 }
1470
1471
1472 /*****************************************************************************
1473 **
1474 ** TEST6_BRANCH_ID: Successfull non-invite transaction
1475 ** TEST7_BRANCH_ID: Successfull non-invite transaction with provisional
1476 ** TEST8_BRANCH_ID: Failed invite transaction
1477 ** TEST9_BRANCH_ID: Failed invite transaction with provisional
1478 **
1479 *****************************************************************************
1480 */
1481 static int perform_generic_test( unsigned tid,
1482 const char *title,
1483 char *branch_id,
1484 const pjsip_method *method)
1485 {
1486 int i, status = 0;
1487 unsigned delay[] = { 1, 200 };
1488
1489 PJ_LOG(3,(THIS_FILE, " %s", title));
1490
1491 /* Do the test. */
1492 for (i=0; i<(int)PJ_ARRAY_SIZE(delay); ++i) {
1493
1494 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1495 PJ_LOG(3,(THIS_FILE, " variant %c: with %d ms transport delay",
1496 ('a'+i), delay[i]));
1497
1498 pjsip_loop_set_failure(g[tid].loop, 0, 0);
1499 pjsip_loop_set_delay(g[tid].loop, delay[i]);
1500 }
1501
1502 status = perform_tsx_test(tid, -1200, g[tid].TARGET_URI, g[tid].FROM_URI,
1503 branch_id, 10, method);
1504 if (status != 0)
1505 return status;
1506
1507 if (g[tid].test_param->type != PJSIP_TRANSPORT_LOOP_DGRAM)
1508 break;
1509 }
1510
1511 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1512 pjsip_loop_set_delay(g[tid].loop, 0);
1513 }
1514
1515 /* Done. */
1516 return status;
1517 }
1518
1519
1520 /*****************************************************************************
1521 **
1522 ** UAC Transaction Test.
1523 **
1524 *****************************************************************************
1525 */
1526 int tsx_uac_test(unsigned tid)
1527 {
1528 #define ERR(rc__) { status=rc__; goto on_return; }
1529 struct tsx_test_param *param = &tsx_test[tid];
1530 int status;
1531
1532 g[tid].timer.tsx_key.ptr = g[tid].timer.key_buf;
1533
1534 g[tid].test_param = param;
1535
1536 /* Get transport flag */
1537 g[tid].tp_flag = pjsip_transport_get_flag_from_type(
1538 (pjsip_transport_type_e)g[tid].test_param->type);
1539
1540 pj_ansi_snprintf(g[tid].TARGET_URI, sizeof(g[tid].TARGET_URI),
1541 "sip:%[email protected]:%d;transport=%s",
1542 tid, param->port, param->tp_type);
1543 pj_ansi_snprintf(g[tid].FROM_URI, sizeof(g[tid].FROM_URI),
1544 "sip:[email protected]:%d;transport=%s",
1545 param->port, param->tp_type);
1546
1547 if ((status=init_test(tid)) != 0)
1548 return status;
1549
1550 status = tsx_uac_retransmit_test(tid);
1551 if (status != 0)
1552 goto on_return;
1553
1554 /* TEST2_BRANCH_ID: Resolve error test. */
1555 status = tsx_resolve_error_test(tid);
1556 if (status != 0)
1557 goto on_return;
1558
1559 /* TEST3_BRANCH_ID: UAC terminate while resolving test. */
1560 status = tsx_terminate_resolving_test(tid);
1561 if (status != 0)
1562 goto on_return;
1563
1564 /* TEST4_BRANCH_ID: Transport failed after several retransmissions.
1565 * Only applies to loop transport.
1566 */
1567 if (g[tid].test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1568 status = tsx_retransmit_fail_test(tid);
1569 if (status != 0)
1570 goto on_return;
1571 }
1572
1573 /* TEST5_BRANCH_ID: Terminate transaction after several retransmissions
1574 * Only applicable to non-reliable transports.
1575 */
1576 if ((g[tid].tp_flag & PJSIP_TRANSPORT_RELIABLE) == 0) {
1577 status = tsx_terminate_after_retransmit_test(tid);
1578 if (status != 0)
1579 goto on_return;
1580 }
1581
1582 /* TEST6_BRANCH_ID: Successfull non-invite transaction */
1583 status = perform_generic_test(tid,
1584 "test6: successfull non-invite transaction",
1585 TEST6_BRANCH_ID, &pjsip_options_method);
1586 if (status != 0)
1587 goto on_return;
1588
1589 /* TEST7_BRANCH_ID: Successfull non-invite transaction */
1590 status = perform_generic_test(tid,
1591 "test7: successfull non-invite transaction "
1592 "with provisional response",
1593 TEST7_BRANCH_ID, &pjsip_options_method);
1594 if (status != 0)
1595 goto on_return;
1596
1597 /* TEST8_BRANCH_ID: Failed invite transaction */
1598 status = perform_generic_test(tid,
1599 "test8: failed invite transaction",
1600 TEST8_BRANCH_ID, &pjsip_invite_method);
1601
1602 if (status != 0)
1603 goto on_return;
1604
1605 /* TEST9_BRANCH_ID: Failed invite transaction with provisional response */
1606 status = perform_generic_test(tid,
1607 "test9: failed invite transaction with "
1608 "provisional response",
1609 TEST9_BRANCH_ID, &pjsip_invite_method);
1610 if (status != 0)
1611 goto on_return;
1612
1613 on_return:
1614 finish_test(tid);
1615 flush_events(500);
1616 return status;
1617 #undef ERR
1618 }
1619