1
/* $Id: tsx_uac_test.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#define THIS_FILE "tsx_uac_test.c"
28
/*****************************************************************************
32
** This file performs various tests for UAC transactions. Each test will have
33
** a different Via branch param so that message receiver module and
34
** transaction user module can identify which test is being carried out.
37
** Perform basic retransmission and timeout test. Message receiver will
38
** verify that retransmission is received at correct time.
39
** This test verifies the following requirements:
40
** - retransmit timer doubles for INVITE
41
** - retransmit timer doubles and caps off for non-INVITE
42
** - retransmit timer timer is precise
43
** - correct timeout and retransmission count
44
** Requirements not tested:
45
** - retransmit timer only starts after resolving has completed.
48
** Test scenario where resolver is unable to resolve destination host.
51
** Test scenario where transaction is terminated while resolver is still
55
** Test scenario where transport failed after several retransmissions.
58
** Test scenario where transaction is terminated by user after several
62
** Test successfull non-INVITE transaction.
63
** It tests the following requirements:
64
** - transaction correctly moves to COMPLETED state.
65
** - retransmission must cease.
66
** - tx_data must be maintained until state is terminated.
69
** Test successfull non-INVITE transaction, with provisional response.
72
** Test failed INVITE transaction (e.g. ACK must be received)
75
** Test failed INVITE transaction with provisional response.
78
*****************************************************************************
81
static char *TEST1_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test1";
82
static char *TEST2_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test2";
83
static char *TEST3_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test3";
84
static char *TEST4_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test4";
85
static char *TEST5_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test5";
86
static char *TEST6_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test6";
87
static char *TEST7_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test7";
88
static char *TEST8_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test8";
89
static char *TEST9_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAC-Test9";
91
#define TEST1_ALLOWED_DIFF (150)
92
#define TEST4_RETRANSMIT_CNT 3
93
#define TEST5_RETRANSMIT_CNT 3
95
static char TARGET_URI[128];
96
static char FROM_URI[128];
97
static unsigned tp_flag;
98
static struct tsx_test_param *test_param;
100
static void tsx_user_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e);
101
static pj_bool_t msg_receiver_on_rx_request(pjsip_rx_data *rdata);
103
/* UAC transaction user module. */
104
static pjsip_module tsx_user =
106
NULL, NULL, /* prev and next */
107
{ "Tsx-UAC-User", 12}, /* Name. */
109
PJSIP_MOD_PRIORITY_APPLICATION-1, /* Priority */
114
NULL, /* on_rx_request() */
115
NULL, /* on_rx_response() */
116
NULL, /* on_tx_request() */
117
NULL, /* on_tx_response() */
118
&tsx_user_on_tsx_state, /* on_tsx_state() */
121
/* Module to receive the loop-backed request. */
122
static pjsip_module msg_receiver =
124
NULL, NULL, /* prev and next */
125
{ "Msg-Receiver", 12}, /* Name. */
127
PJSIP_MOD_PRIORITY_APPLICATION-1, /* Priority */
132
&msg_receiver_on_rx_request, /* on_rx_request() */
133
NULL, /* on_rx_response() */
134
NULL, /* on_tx_request() */
135
NULL, /* on_tx_response() */
136
NULL, /* on_tsx_state() */
139
/* Static vars, which will be reset on each test. */
140
static int recv_count;
141
static pj_time_val recv_last;
142
static pj_bool_t test_complete;
144
/* Loop transport instance. */
145
static pjsip_transport *loop;
147
/* General timer entry to be used by tests. */
148
static struct my_timer
150
pj_timer_entry entry;
156
* This is the handler to receive state changed notification from the
157
* transaction. It is used to verify that the transaction behaves according
158
* to the test scenario.
160
static void tsx_user_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
162
if (pj_strcmp2(&tsx->branch, TEST1_BRANCH_ID)==0) {
164
* Transaction with TEST1_BRANCH_ID should terminate with transaction
167
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
169
if (test_complete == 0)
172
/* Test the status code. */
173
if (tsx->status_code != PJSIP_SC_TSX_TIMEOUT) {
175
" error: status code is %d instead of %d",
176
tsx->status_code, PJSIP_SC_TSX_TIMEOUT));
177
test_complete = -710;
181
/* If transport is reliable, then there must not be any
184
if (tp_flag & PJSIP_TRANSPORT_RELIABLE) {
185
if (recv_count != 1) {
187
" error: there were %d (re)transmissions",
189
test_complete = -715;
192
/* Check the number of transmissions, which must be
193
* 6 for INVITE and 10 for non-INVITE
195
if (tsx->method.id==PJSIP_INVITE_METHOD && recv_count != 7) {
197
" error: there were %d (re)transmissions",
199
test_complete = -716;
201
if (tsx->method.id==PJSIP_OPTIONS_METHOD && recv_count != 11) {
203
" error: there were %d (re)transmissions",
205
test_complete = -717;
207
if (tsx->method.id!=PJSIP_INVITE_METHOD &&
208
tsx->method.id!=PJSIP_OPTIONS_METHOD)
210
PJ_LOG(3,(THIS_FILE, " error: unexpected method"));
211
test_complete = -718;
216
} else if (pj_strcmp2(&tsx->branch, TEST2_BRANCH_ID)==0) {
218
* Transaction with TEST2_BRANCH_ID should terminate with transport error.
220
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
222
/* Test the status code. */
223
if (tsx->status_code != PJSIP_SC_TSX_TRANSPORT_ERROR) {
225
" error: status code is %d instead of %d",
226
tsx->status_code, PJSIP_SC_TSX_TRANSPORT_ERROR));
227
test_complete = -720;
230
if (test_complete == 0)
234
} else if (pj_strcmp2(&tsx->branch, TEST3_BRANCH_ID)==0) {
236
* This test terminates the transaction while resolver is still
239
if (tsx->state == PJSIP_TSX_STATE_CALLING) {
241
/* Terminate the transaction. */
242
pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
244
} else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
246
/* Check if status code is correct. */
247
if (tsx->status_code != PJSIP_SC_REQUEST_TERMINATED) {
249
" error: status code is %d instead of %d",
250
tsx->status_code, PJSIP_SC_REQUEST_TERMINATED));
251
test_complete = -730;
254
if (test_complete == 0)
259
} else if (pj_strcmp2(&tsx->branch, TEST4_BRANCH_ID)==0) {
261
* This test simulates transport failure after several
264
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
266
/* Status code must be transport error. */
267
if (tsx->status_code != PJSIP_SC_TSX_TRANSPORT_ERROR) {
269
" error: status code is %d instead of %d",
270
tsx->status_code, PJSIP_SC_TSX_TRANSPORT_ERROR));
271
test_complete = -730;
274
/* Must have correct retransmission count. */
275
if (tsx->retransmit_count != TEST4_RETRANSMIT_CNT) {
277
" error: retransmit cnt is %d instead of %d",
278
tsx->retransmit_count, TEST4_RETRANSMIT_CNT));
279
test_complete = -731;
282
if (test_complete == 0)
287
} else if (pj_strcmp2(&tsx->branch, TEST5_BRANCH_ID)==0) {
289
* This test simulates transport failure after several
292
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
294
/* Status code must be PJSIP_SC_REQUEST_TERMINATED. */
295
if (tsx->status_code != PJSIP_SC_REQUEST_TERMINATED) {
297
" error: status code is %d instead of %d",
298
tsx->status_code, PJSIP_SC_REQUEST_TERMINATED));
299
test_complete = -733;
302
/* Must have correct retransmission count. */
303
if (tsx->retransmit_count != TEST5_RETRANSMIT_CNT) {
305
" error: retransmit cnt is %d instead of %d",
306
tsx->retransmit_count, TEST5_RETRANSMIT_CNT));
307
test_complete = -734;
310
if (test_complete == 0)
315
} else if (pj_strcmp2(&tsx->branch, TEST6_BRANCH_ID)==0) {
317
* Successfull non-INVITE transaction.
319
if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
321
/* Status code must be 202. */
322
if (tsx->status_code != 202) {
324
" error: status code is %d instead of %d",
325
tsx->status_code, 202));
326
test_complete = -736;
329
/* Must have correct retransmission count. */
330
if (tsx->retransmit_count != 0) {
332
" error: retransmit cnt is %d instead of %d",
333
tsx->retransmit_count, 0));
334
test_complete = -737;
337
/* Must still keep last_tx */
338
if (tsx->last_tx == NULL) {
340
" error: transaction lost last_tx"));
341
test_complete = -738;
344
if (test_complete == 0) {
346
pjsip_tsx_terminate(tsx, 202);
349
} else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
351
/* Previous state must be COMPLETED. */
352
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
353
test_complete = -7381;
358
} else if (pj_strcmp2(&tsx->branch, TEST7_BRANCH_ID)==0) {
360
* Successfull non-INVITE transaction.
362
if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
364
/* Check prev state. */
365
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_PROCEEDING) {
367
" error: prev state is %s instead of %s",
368
pjsip_tsx_state_str((pjsip_tsx_state_e)e->body.tsx_state.prev_state),
369
pjsip_tsx_state_str(PJSIP_TSX_STATE_PROCEEDING)));
370
test_complete = -739;
373
/* Status code must be 202. */
374
if (tsx->status_code != 202) {
376
" error: status code is %d instead of %d",
377
tsx->status_code, 202));
378
test_complete = -740;
381
/* Must have correct retransmission count. */
382
if (tsx->retransmit_count != 0) {
384
" error: retransmit cnt is %d instead of %d",
385
tsx->retransmit_count, 0));
386
test_complete = -741;
389
/* Must still keep last_tx */
390
if (tsx->last_tx == NULL) {
392
" error: transaction lost last_tx"));
393
test_complete = -741;
396
if (test_complete == 0) {
398
pjsip_tsx_terminate(tsx, 202);
401
} else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
403
/* Previous state must be COMPLETED. */
404
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
405
test_complete = -742;
411
} else if (pj_strcmp2(&tsx->branch, TEST8_BRANCH_ID)==0) {
413
* Failed INVITE transaction.
415
if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
417
/* Status code must be 301. */
418
if (tsx->status_code != 301) {
420
" error: status code is %d instead of %d",
421
tsx->status_code, 301));
422
test_complete = -745;
425
/* Must have correct retransmission count. */
426
if (tsx->retransmit_count != 0) {
428
" error: retransmit cnt is %d instead of %d",
429
tsx->retransmit_count, 0));
430
test_complete = -746;
433
/* Must still keep last_tx */
434
if (tsx->last_tx == NULL) {
436
" error: transaction lost last_tx"));
437
test_complete = -747;
440
/* last_tx MUST be the INVITE request
441
* (authorization depends on this behavior)
443
if (tsx->last_tx && tsx->last_tx->msg->line.req.method.id !=
447
" error: last_tx is not INVITE"));
448
test_complete = -748;
451
else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
455
/* Previous state must be COMPLETED. */
456
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
457
test_complete = -750;
460
/* Status code must be 301. */
461
if (tsx->status_code != 301) {
463
" error: status code is %d instead of %d",
464
tsx->status_code, 301));
465
test_complete = -751;
471
} else if (pj_strcmp2(&tsx->branch, TEST9_BRANCH_ID)==0) {
473
* Failed INVITE transaction with provisional response.
475
if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
477
/* Previous state must be PJSIP_TSX_STATE_PROCEEDING. */
478
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_PROCEEDING) {
479
test_complete = -760;
482
/* Status code must be 302. */
483
if (tsx->status_code != 302) {
485
" error: status code is %d instead of %d",
486
tsx->status_code, 302));
487
test_complete = -761;
490
/* Must have correct retransmission count. */
491
if (tsx->retransmit_count != 0) {
493
" error: retransmit cnt is %d instead of %d",
494
tsx->retransmit_count, 0));
495
test_complete = -762;
498
/* Must still keep last_tx */
499
if (tsx->last_tx == NULL) {
501
" error: transaction lost last_tx"));
502
test_complete = -763;
505
/* last_tx MUST be INVITE.
506
* (authorization depends on this behavior)
508
if (tsx->last_tx && tsx->last_tx->msg->line.req.method.id !=
512
" error: last_tx is not INVITE"));
513
test_complete = -764;
517
else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
521
/* Previous state must be COMPLETED. */
522
if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
523
test_complete = -767;
526
/* Status code must be 302. */
527
if (tsx->status_code != 302) {
529
" error: status code is %d instead of %d",
530
tsx->status_code, 302));
531
test_complete = -768;
540
* This timer callback is called to send delayed response.
544
pjsip_response_addr res_addr;
545
pjsip_tx_data *tdata;
548
static void send_response_callback( pj_timer_heap_t *timer_heap,
549
struct pj_timer_entry *entry)
551
struct response *r = (struct response*) entry->user_data;
552
pjsip_transport *tp = r->res_addr.transport;
554
PJ_UNUSED_ARG(timer_heap);
556
pjsip_endpt_send_response(endpt, &r->res_addr, r->tdata, NULL, NULL);
558
pjsip_transport_dec_ref(tp);
561
/* Timer callback to terminate a transaction. */
562
static void terminate_tsx_callback( pj_timer_heap_t *timer_heap,
563
struct pj_timer_entry *entry)
565
struct my_timer *m = (struct my_timer *)entry;
566
pjsip_transaction *tsx = pjsip_tsx_layer_find_tsx(&m->tsx_key, PJ_FALSE);
567
int status_code = entry->id;
569
PJ_UNUSED_ARG(timer_heap);
572
pjsip_tsx_terminate(tsx, status_code);
577
#define DIFF(a,b) ((a<b) ? (b-a) : (a-b))
580
* This is the handler to receive message for this test. It is used to
581
* control and verify the behavior of the message transmitted by the
584
static pj_bool_t msg_receiver_on_rx_request(pjsip_rx_data *rdata)
586
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST1_BRANCH_ID) == 0) {
588
* The TEST1_BRANCH_ID test performs the verifications for transaction
589
* retransmission mechanism. It will not answer the incoming request
592
pjsip_msg *msg = rdata->msg_info.msg;
594
PJ_LOG(4,(THIS_FILE, " received request"));
596
/* Only wants to take INVITE or OPTIONS method. */
597
if (msg->line.req.method.id != PJSIP_INVITE_METHOD &&
598
msg->line.req.method.id != PJSIP_OPTIONS_METHOD)
600
PJ_LOG(3,(THIS_FILE, " error: received unexpected method %.*s",
601
msg->line.req.method.name.slen,
602
msg->line.req.method.name.ptr));
603
test_complete = -600;
607
if (recv_count == 0) {
609
//pj_gettimeofday(&recv_last);
610
recv_last = rdata->pkt_info.timestamp;
613
unsigned msec_expected, msec_elapsed;
616
//pj_gettimeofday(&now);
617
now = rdata->pkt_info.timestamp;
618
PJ_TIME_VAL_SUB(now, recv_last);
619
msec_elapsed = now.sec*1000 + now.msec;
622
msec_expected = (1<<(recv_count-2))*pjsip_cfg()->tsx.t1;
624
if (msg->line.req.method.id != PJSIP_INVITE_METHOD) {
625
if (msec_expected > pjsip_cfg()->tsx.t2)
626
msec_expected = pjsip_cfg()->tsx.t2;
632
if (DIFF(msec_expected, msec_elapsed) > TEST1_ALLOWED_DIFF) {
634
" error: expecting retransmission no. %d in %d "
635
"ms, received in %d ms",
636
recv_count-1, msec_expected, msec_elapsed));
637
test_complete = -610;
641
if (recv_count > max_received) {
643
" error: too many messages (%d) received",
645
test_complete = -620;
648
//pj_gettimeofday(&recv_last);
649
recv_last = rdata->pkt_info.timestamp;
654
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST4_BRANCH_ID) == 0) {
656
* The TEST4_BRANCH_ID test simulates transport failure after several
661
if (recv_count == TEST4_RETRANSMIT_CNT) {
662
/* Simulate transport failure. */
663
pjsip_loop_set_failure(loop, 2, NULL);
665
} else if (recv_count > TEST4_RETRANSMIT_CNT) {
666
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
668
test_complete = -631;
675
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST5_BRANCH_ID) == 0) {
677
* The TEST5_BRANCH_ID test simulates user terminating the transaction
678
* after several retransmissions.
682
if (recv_count == TEST5_RETRANSMIT_CNT+1) {
684
pjsip_transaction *tsx;
686
pjsip_tsx_create_key( rdata->tp_info.pool, &key, PJSIP_ROLE_UAC,
687
&rdata->msg_info.msg->line.req.method, rdata);
688
tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE);
690
pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
691
pj_mutex_unlock(tsx->mutex);
693
PJ_LOG(3,(THIS_FILE, " error: uac transaction not found!"));
694
test_complete = -633;
697
} else if (recv_count > TEST5_RETRANSMIT_CNT+1) {
698
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
700
test_complete = -634;
706
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST6_BRANCH_ID) == 0) {
708
* The TEST6_BRANCH_ID test successfull non-INVITE transaction.
714
if (recv_count > 1) {
715
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
717
test_complete = -635;
720
status = pjsip_endpt_respond_stateless(endpt, rdata, 202, NULL,
722
if (status != PJ_SUCCESS) {
723
app_perror(" error: unable to send response", status);
724
test_complete = -636;
731
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST7_BRANCH_ID) == 0) {
733
* The TEST7_BRANCH_ID test successfull non-INVITE transaction
734
* with provisional response.
737
pjsip_response_addr res_addr;
739
pjsip_tx_data *tdata;
740
pj_time_val delay = { 2, 0 };
744
if (recv_count > 1) {
745
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
747
test_complete = -640;
751
/* Respond with provisional response */
752
status = pjsip_endpt_create_response(endpt, rdata, 100, NULL, &tdata);
753
pj_assert(status == PJ_SUCCESS);
755
status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
756
pj_assert(status == PJ_SUCCESS);
758
status = pjsip_endpt_send_response(endpt, &res_addr, tdata,
760
pj_assert(status == PJ_SUCCESS);
762
/* Create the final response. */
763
status = pjsip_endpt_create_response(endpt, rdata, 202, NULL, &tdata);
764
pj_assert(status == PJ_SUCCESS);
766
/* Schedule sending final response in couple of of secs. */
767
r = PJ_POOL_ALLOC_T(tdata->pool, struct response);
768
r->res_addr = res_addr;
770
if (r->res_addr.transport)
771
pjsip_transport_add_ref(r->res_addr.transport);
773
timer.entry.cb = &send_response_callback;
774
timer.entry.user_data = r;
775
pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);
781
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST8_BRANCH_ID) == 0) {
783
* The TEST8_BRANCH_ID test failed INVITE transaction.
785
pjsip_method *method;
788
method = &rdata->msg_info.msg->line.req.method;
792
if (method->id == PJSIP_INVITE_METHOD) {
794
if (recv_count > 1) {
795
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
797
test_complete = -635;
800
status = pjsip_endpt_respond_stateless(endpt, rdata, 301, NULL,
802
if (status != PJ_SUCCESS) {
803
app_perror(" error: unable to send response", status);
804
test_complete = -636;
807
} else if (method->id == PJSIP_ACK_METHOD) {
809
if (recv_count == 2) {
811
pj_time_val delay = { 5, 0 };
813
/* Schedule timer to destroy transaction after 5 seconds.
814
* This is to make sure that transaction does not
817
pjsip_tsx_create_key(rdata->tp_info.pool, &key,
818
PJSIP_ROLE_UAC, &pjsip_invite_method,
821
pj_strcpy(&timer.tsx_key, &key);
822
timer.entry.id = 301;
823
timer.entry.cb = &terminate_tsx_callback;
825
pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);
828
if (recv_count > 2) {
829
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
831
test_complete = -638;
836
PJ_LOG(3,(THIS_FILE," error: not expecting %s",
837
pjsip_rx_data_get_info(rdata)));
838
test_complete = -639;
844
if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST9_BRANCH_ID) == 0) {
846
* The TEST9_BRANCH_ID test failed INVITE transaction with
847
* provisional response.
849
pjsip_method *method;
852
method = &rdata->msg_info.msg->line.req.method;
856
if (method->id == PJSIP_INVITE_METHOD) {
858
pjsip_response_addr res_addr;
860
pjsip_tx_data *tdata;
861
pj_time_val delay = { 2, 0 };
863
if (recv_count > 1) {
864
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
866
test_complete = -650;
870
/* Respond with provisional response */
871
status = pjsip_endpt_create_response(endpt, rdata, 100, NULL,
873
pj_assert(status == PJ_SUCCESS);
875
status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
876
pj_assert(status == PJ_SUCCESS);
878
status = pjsip_endpt_send_response(endpt, &res_addr, tdata,
880
pj_assert(status == PJ_SUCCESS);
882
/* Create the final response. */
883
status = pjsip_endpt_create_response(endpt, rdata, 302, NULL,
885
pj_assert(status == PJ_SUCCESS);
887
/* Schedule sending final response in couple of of secs. */
888
r = PJ_POOL_ALLOC_T(tdata->pool, struct response);
889
r->res_addr = res_addr;
891
if (r->res_addr.transport)
892
pjsip_transport_add_ref(r->res_addr.transport);
894
timer.entry.cb = &send_response_callback;
895
timer.entry.user_data = r;
896
pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);
898
} else if (method->id == PJSIP_ACK_METHOD) {
900
if (recv_count == 2) {
902
pj_time_val delay = { 5, 0 };
904
/* Schedule timer to destroy transaction after 5 seconds.
905
* This is to make sure that transaction does not
908
pjsip_tsx_create_key(rdata->tp_info.pool, &key,
909
PJSIP_ROLE_UAC, &pjsip_invite_method,
912
pj_strcpy(&timer.tsx_key, &key);
913
timer.entry.id = 302;
914
timer.entry.cb = &terminate_tsx_callback;
916
pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);
919
if (recv_count > 2) {
920
PJ_LOG(3,(THIS_FILE," error: not expecting %d-th packet!",
922
test_complete = -638;
927
PJ_LOG(3,(THIS_FILE," error: not expecting %s",
928
pjsip_rx_data_get_info(rdata)));
929
test_complete = -639;
941
* The generic test framework, used by most of the tests.
943
static int perform_tsx_test(int dummy, char *target_uri, char *from_uri,
944
char *branch_param, int test_time,
945
const pjsip_method *method)
947
pjsip_tx_data *tdata;
948
pjsip_transaction *tsx;
949
pj_str_t target, from, tsx_key;
954
PJ_UNUSED_ARG(dummy);
957
" please standby, this will take at most %d seconds..",
965
target = pj_str(target_uri);
966
from = pj_str(from_uri);
968
/* Create request. */
969
status = pjsip_endpt_create_request( endpt, method, &target,
970
&from, &target, NULL, NULL, -1,
972
if (status != PJ_SUCCESS) {
973
app_perror(" Error: unable to create request", status);
977
/* Set the branch param for test 1. */
978
via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
979
via->branch_param = pj_str(branch_param);
981
/* Add additional reference to tdata to prevent transaction from
984
pjsip_tx_data_add_ref(tdata);
986
/* Create transaction. */
987
status = pjsip_tsx_create_uac( &tsx_user, tdata, &tsx);
988
if (status != PJ_SUCCESS) {
989
app_perror(" Error: unable to create UAC transaction", status);
990
pjsip_tx_data_dec_ref(tdata);
994
/* Get transaction key. */
995
pj_strdup(tdata->pool, &tsx_key, &tsx->transaction_key);
997
/* Send the message. */
998
status = pjsip_tsx_send_msg(tsx, NULL);
999
// Ignore send result. Some tests do deliberately triggers error
1000
// when sending message.
1001
if (status != PJ_SUCCESS) {
1002
// app_perror(" Error: unable to send request", status);
1003
pjsip_tx_data_dec_ref(tdata);
1008
/* Set test completion time. */
1009
pj_gettimeofday(&timeout);
1010
timeout.sec += test_time;
1012
/* Wait until test complete. */
1013
while (!test_complete) {
1014
pj_time_val now, poll_delay = {0, 10};
1016
pjsip_endpt_handle_events(endpt, &poll_delay);
1018
pj_gettimeofday(&now);
1019
if (now.sec > timeout.sec) {
1020
PJ_LOG(3,(THIS_FILE, " Error: test has timed out"));
1021
pjsip_tx_data_dec_ref(tdata);
1026
if (test_complete < 0) {
1027
tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
1029
pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
1030
pj_mutex_unlock(tsx->mutex);
1033
pjsip_tx_data_dec_ref(tdata);
1034
return test_complete;
1039
/* Allow transaction to destroy itself */
1042
/* Wait until test completes */
1043
pj_gettimeofday(&now);
1045
if (PJ_TIME_VAL_LT(now, timeout)) {
1046
pj_time_val interval;
1048
PJ_TIME_VAL_SUB(interval, now);
1049
flush_events(PJ_TIME_VAL_MSEC(interval));
1053
/* Make sure transaction has been destroyed. */
1054
if (pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE) != NULL) {
1055
PJ_LOG(3,(THIS_FILE, " Error: transaction has not been destroyed"));
1056
pjsip_tx_data_dec_ref(tdata);
1060
/* Check tdata reference counter. */
1061
if (pj_atomic_get(tdata->ref_cnt) != 1) {
1062
PJ_LOG(3,(THIS_FILE, " Error: tdata reference counter is %d",
1063
pj_atomic_get(tdata->ref_cnt)));
1064
pjsip_tx_data_dec_ref(tdata);
1068
/* Destroy txdata */
1069
pjsip_tx_data_dec_ref(tdata);
1074
/*****************************************************************************
1076
** TEST1_BRANCH_ID: UAC basic retransmission and timeout test.
1078
** This will test the retransmission of the UAC transaction. Remote will not
1079
** answer the transaction, so the transaction should fail. The Via branch prm
1080
** TEST1_BRANCH_ID will be used for this test.
1082
*****************************************************************************
1084
static int tsx_uac_retransmit_test(void)
1086
int status = 0, enabled;
1089
const pjsip_method *method;
1093
{ &pjsip_invite_method, 0},
1094
{ &pjsip_invite_method, TEST1_ALLOWED_DIFF*2},
1095
{ &pjsip_options_method, 0},
1096
{ &pjsip_options_method, TEST1_ALLOWED_DIFF*2}
1099
PJ_LOG(3,(THIS_FILE, " test1: basic uac retransmit and timeout test"));
1102
/* For this test. message printing shound be disabled because it makes
1105
enabled = msg_logger_set_enabled(0);
1107
for (i=0; i<(int)PJ_ARRAY_SIZE(sub_test); ++i) {
1109
PJ_LOG(3,(THIS_FILE,
1110
" variant %c: %s with %d ms network delay",
1112
sub_test[i].method->name.ptr,
1113
sub_test[i].delay));
1115
/* Configure transport */
1116
pjsip_loop_set_failure(loop, 0, NULL);
1117
pjsip_loop_set_recv_delay(loop, sub_test[i].delay, NULL);
1120
status = perform_tsx_test(-500, TARGET_URI, FROM_URI,
1122
35, sub_test[i].method);
1127
/* Restore transport. */
1128
pjsip_loop_set_recv_delay(loop, 0, NULL);
1130
/* Restore msg logger. */
1131
msg_logger_set_enabled(enabled);
1137
/*****************************************************************************
1139
** TEST2_BRANCH_ID: UAC resolve error test.
1141
** Test the scenario where destination host is unresolvable. There are
1143
** (a) resolver returns immediate error
1144
** (b) resolver returns error via the callback.
1146
*****************************************************************************
1148
static int tsx_resolve_error_test(void)
1152
PJ_LOG(3,(THIS_FILE, " test2: resolve error test"));
1155
* Variant (a): immediate resolve error.
1157
PJ_LOG(3,(THIS_FILE, " variant a: immediate resolving error"));
1159
status = perform_tsx_test(-800,
1160
"sip:bob@unresolved-host",
1161
FROM_URI, TEST2_BRANCH_ID, 20,
1162
&pjsip_options_method);
1167
* Variant (b): error via callback.
1169
PJ_LOG(3,(THIS_FILE, " variant b: error via callback"));
1171
/* This only applies to "loop-dgram" transport */
1172
if (test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1173
/* Set loop transport to return delayed error. */
1174
pjsip_loop_set_failure(loop, 2, NULL);
1175
pjsip_loop_set_send_callback_delay(loop, 10, NULL);
1177
status = perform_tsx_test(-800, TARGET_URI, FROM_URI,
1179
&pjsip_options_method);
1183
/* Restore loop transport settings. */
1184
pjsip_loop_set_failure(loop, 0, NULL);
1185
pjsip_loop_set_send_callback_delay(loop, 0, NULL);
1192
/*****************************************************************************
1194
** TEST3_BRANCH_ID: UAC terminate while resolving test.
1196
** Terminate the transaction while resolver is still running.
1198
*****************************************************************************
1200
static int tsx_terminate_resolving_test(void)
1202
unsigned prev_delay;
1205
PJ_LOG(3,(THIS_FILE, " test3: terminate while resolving test"));
1207
/* Configure transport delay. */
1208
pjsip_loop_set_send_callback_delay(loop, 100, &prev_delay);
1210
/* Start the test. */
1211
status = perform_tsx_test(-900, TARGET_URI, FROM_URI,
1212
TEST3_BRANCH_ID, 2, &pjsip_options_method);
1214
/* Restore delay. */
1215
pjsip_loop_set_send_callback_delay(loop, prev_delay, NULL);
1221
/*****************************************************************************
1223
** TEST4_BRANCH_ID: Transport failed after several retransmissions
1225
** There are two variants of this test: (a) failure occurs immediately when
1226
** transaction calls pjsip_transport_send() or (b) failure is reported via
1227
** transport callback.
1229
*****************************************************************************
1231
static int tsx_retransmit_fail_test(void)
1234
unsigned delay[] = {0, 10};
1235
pj_status_t status = PJ_SUCCESS;
1237
PJ_LOG(3,(THIS_FILE,
1238
" test4: transport fails after several retransmissions test"));
1241
for (i=0; i<(int)PJ_ARRAY_SIZE(delay); ++i) {
1243
PJ_LOG(3,(THIS_FILE,
1244
" variant %c: transport delay %d ms", ('a'+i), delay[i]));
1246
/* Configure transport delay. */
1247
pjsip_loop_set_send_callback_delay(loop, delay[i], NULL);
1249
/* Restore transport failure mode. */
1250
pjsip_loop_set_failure(loop, 0, 0);
1252
/* Start the test. */
1253
status = perform_tsx_test(-1000, TARGET_URI, FROM_URI,
1254
TEST4_BRANCH_ID, 6, &pjsip_options_method);
1261
/* Restore delay. */
1262
pjsip_loop_set_send_callback_delay(loop, 0, NULL);
1264
/* Restore transport failure mode. */
1265
pjsip_loop_set_failure(loop, 0, 0);
1271
/*****************************************************************************
1273
** TEST5_BRANCH_ID: Terminate transaction after several retransmissions
1275
*****************************************************************************
1277
static int tsx_terminate_after_retransmit_test(void)
1281
PJ_LOG(3,(THIS_FILE, " test5: terminate after retransmissions"));
1284
status = perform_tsx_test(-1100, TARGET_URI, FROM_URI,
1286
6, &pjsip_options_method);
1293
/*****************************************************************************
1295
** TEST6_BRANCH_ID: Successfull non-invite transaction
1296
** TEST7_BRANCH_ID: Successfull non-invite transaction with provisional
1297
** TEST8_BRANCH_ID: Failed invite transaction
1298
** TEST9_BRANCH_ID: Failed invite transaction with provisional
1300
*****************************************************************************
1302
static int perform_generic_test( const char *title,
1304
const pjsip_method *method)
1307
unsigned delay[] = { 1, 200 };
1309
PJ_LOG(3,(THIS_FILE, " %s", title));
1312
for (i=0; i<(int)PJ_ARRAY_SIZE(delay); ++i) {
1314
if (test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1315
PJ_LOG(3,(THIS_FILE, " variant %c: with %d ms transport delay",
1316
('a'+i), delay[i]));
1318
pjsip_loop_set_delay(loop, delay[i]);
1321
status = perform_tsx_test(-1200, TARGET_URI, FROM_URI,
1322
branch_id, 10, method);
1326
if (test_param->type != PJSIP_TRANSPORT_LOOP_DGRAM)
1330
pjsip_loop_set_delay(loop, 0);
1337
/*****************************************************************************
1339
** UAC Transaction Test.
1341
*****************************************************************************
1343
int tsx_uac_test(struct tsx_test_param *param)
1345
pj_sockaddr_in addr;
1348
timer.tsx_key.ptr = timer.key_buf;
1352
/* Get transport flag */
1353
tp_flag = pjsip_transport_get_flag_from_type((pjsip_transport_type_e)test_param->type);
1355
pj_ansi_sprintf(TARGET_URI, "sip:bob@127.0.0.1:%d;transport=%s",
1356
param->port, param->tp_type);
1357
pj_ansi_sprintf(FROM_URI, "sip:alice@127.0.0.1:%d;transport=%s",
1358
param->port, param->tp_type);
1360
/* Check if loop transport is configured. */
1361
status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_LOOP_DGRAM,
1362
&addr, sizeof(addr), NULL, &loop);
1363
if (status != PJ_SUCCESS) {
1364
PJ_LOG(3,(THIS_FILE, " Error: loop transport is not configured!"));
1368
/* Register modules. */
1369
status = pjsip_endpt_register_module(endpt, &tsx_user);
1370
if (status != PJ_SUCCESS) {
1371
app_perror(" Error: unable to register module", status);
1374
status = pjsip_endpt_register_module(endpt, &msg_receiver);
1375
if (status != PJ_SUCCESS) {
1376
app_perror(" Error: unable to register module", status);
1380
/* TEST1_BRANCH_ID: Basic retransmit and timeout test. */
1381
status = tsx_uac_retransmit_test();
1385
/* TEST2_BRANCH_ID: Resolve error test. */
1386
status = tsx_resolve_error_test();
1390
/* TEST3_BRANCH_ID: UAC terminate while resolving test. */
1391
status = tsx_terminate_resolving_test();
1395
/* TEST4_BRANCH_ID: Transport failed after several retransmissions.
1396
* Only applies to loop transport.
1398
if (test_param->type == PJSIP_TRANSPORT_LOOP_DGRAM) {
1399
status = tsx_retransmit_fail_test();
1404
/* TEST5_BRANCH_ID: Terminate transaction after several retransmissions
1405
* Only applicable to non-reliable transports.
1407
if ((tp_flag & PJSIP_TRANSPORT_RELIABLE) == 0) {
1408
status = tsx_terminate_after_retransmit_test();
1413
/* TEST6_BRANCH_ID: Successfull non-invite transaction */
1414
status = perform_generic_test("test6: successfull non-invite transaction",
1415
TEST6_BRANCH_ID, &pjsip_options_method);
1419
/* TEST7_BRANCH_ID: Successfull non-invite transaction */
1420
status = perform_generic_test("test7: successfull non-invite transaction "
1421
"with provisional response",
1422
TEST7_BRANCH_ID, &pjsip_options_method);
1426
/* TEST8_BRANCH_ID: Failed invite transaction */
1427
status = perform_generic_test("test8: failed invite transaction",
1428
TEST8_BRANCH_ID, &pjsip_invite_method);
1432
/* TEST9_BRANCH_ID: Failed invite transaction with provisional response */
1433
status = perform_generic_test("test9: failed invite transaction with "
1434
"provisional response",
1435
TEST9_BRANCH_ID, &pjsip_invite_method);
1439
pjsip_transport_dec_ref(loop);
1442
/* Unregister modules. */
1443
status = pjsip_endpt_unregister_module(endpt, &tsx_user);
1444
if (status != PJ_SUCCESS) {
1445
app_perror(" Error: unable to unregister module", status);
1448
status = pjsip_endpt_unregister_module(endpt, &msg_receiver);
1449
if (status != PJ_SUCCESS) {
1450
app_perror(" Error: unable to unregister module", status);