blob: f030ea06483a87f373c35a9662ee5e0c3d093e9a [file] [log] [blame]
Benny Prijono85598d92006-01-07 18:44:25 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono85598d92006-01-07 18:44:25 +00005 *
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.
10 *
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.
15 *
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
19 */
20
21#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000022#include <pjsip.h>
Benny Prijono85598d92006-01-07 18:44:25 +000023#include <pjlib.h>
24
25#define THIS_FILE "tsx_basic_test.c"
26
Benny Prijonoe85bc412006-07-29 20:29:24 +000027static char TARGET_URI[PJSIP_MAX_URL_SIZE];
28static char FROM_URI[PJSIP_MAX_URL_SIZE];
Benny Prijonoe93e2872006-06-28 16:46:49 +000029
30
Benny Prijono85598d92006-01-07 18:44:25 +000031/* Test transaction layer. */
32static int tsx_layer_test(void)
33{
34 pj_str_t target, from, tsx_key;
35 pjsip_tx_data *tdata;
36 pjsip_transaction *tsx, *found;
37 pj_status_t status;
38
39 PJ_LOG(3,(THIS_FILE, " transaction layer test"));
40
Benny Prijonoe93e2872006-06-28 16:46:49 +000041 target = pj_str(TARGET_URI);
42 from = pj_str(FROM_URI);
Benny Prijono85598d92006-01-07 18:44:25 +000043
44 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
45 &from, &target, NULL, NULL, -1, NULL,
46 &tdata);
47 if (status != PJ_SUCCESS) {
48 app_perror(" error: unable to create request", status);
49 return -110;
50 }
51
52 status = pjsip_tsx_create_uac(NULL, tdata, &tsx);
53 if (status != PJ_SUCCESS) {
54 app_perror(" error: unable to create transaction", status);
55 return -120;
56 }
57
58 pj_strdup(tdata->pool, &tsx_key, &tsx->transaction_key);
59
60 found = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE);
61 if (found != tsx) {
62 return -130;
63 }
64
65 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
66 flush_events(500);
67
68 if (pjsip_tx_data_dec_ref(tdata) != PJSIP_EBUFDESTROYED) {
69 return -140;
70 }
71
72 return 0;
73}
74
75/* Double terminate test. */
76static int double_terminate(void)
77{
78 pj_str_t target, from, tsx_key;
79 pjsip_tx_data *tdata;
80 pjsip_transaction *tsx;
81 pj_status_t status;
82
83 PJ_LOG(3,(THIS_FILE, " double terminate test"));
84
Benny Prijonoe93e2872006-06-28 16:46:49 +000085 target = pj_str(TARGET_URI);
86 from = pj_str(FROM_URI);
Benny Prijono85598d92006-01-07 18:44:25 +000087
88 /* Create request. */
89 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
90 &from, &target, NULL, NULL, -1, NULL,
91 &tdata);
92 if (status != PJ_SUCCESS) {
93 app_perror(" error: unable to create request", status);
94 return -10;
95 }
96
97 /* Create transaction. */
98 status = pjsip_tsx_create_uac(NULL, tdata, &tsx);
99 if (status != PJ_SUCCESS) {
100 app_perror(" error: unable to create transaction", status);
101 return -20;
102 }
103
104 /* Save transaction key for later. */
105 pj_strdup_with_null(tdata->pool, &tsx_key, &tsx->transaction_key);
106
107 /* Add reference to transmit buffer (tsx_send_msg() will dec txdata). */
108 pjsip_tx_data_add_ref(tdata);
109
110 /* Send message to start timeout timer. */
111 status = pjsip_tsx_send_msg(tsx, NULL);
112
113 /* Terminate transaction. */
114 status = pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
115 if (status != PJ_SUCCESS) {
116 app_perror(" error: unable to terminate transaction", status);
117 return -30;
118 }
119
120 tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
121 if (tsx) {
122 /* Terminate transaction again. */
123 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);
124 if (status != PJ_SUCCESS) {
125 app_perror(" error: unable to terminate transaction", status);
126 return -40;
127 }
128 pj_mutex_unlock(tsx->mutex);
129 }
130
131 flush_events(500);
132 if (pjsip_tx_data_dec_ref(tdata) != PJSIP_EBUFDESTROYED) {
133 return -50;
134 }
135
136 return PJ_SUCCESS;
137}
138
Benny Prijonoe93e2872006-06-28 16:46:49 +0000139int tsx_basic_test(struct tsx_test_param *param)
Benny Prijono85598d92006-01-07 18:44:25 +0000140{
141 int status;
142
Benny Prijonoe93e2872006-06-28 16:46:49 +0000143 pj_ansi_sprintf(TARGET_URI, "sip:bob@127.0.0.1:%d;transport=%s",
144 param->port, param->tp_type);
145 pj_ansi_sprintf(FROM_URI, "sip:alice@127.0.0.1:%d;transport=%s",
146 param->port, param->tp_type);
147
Benny Prijono85598d92006-01-07 18:44:25 +0000148 status = tsx_layer_test();
149 if (status != 0)
150 return status;
151
152 status = double_terminate();
153 if (status != 0)
154 return status;
155
156 return 0;
157}