Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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" |
| 22 | #include <pjsip.h> |
| 23 | #include <pjlib.h> |
| 24 | |
| 25 | #define THIS_FILE "tsx_basic_test.c" |
| 26 | |
| 27 | static char TARGET_URI[PJSIP_MAX_URL_SIZE]; |
| 28 | static char FROM_URI[PJSIP_MAX_URL_SIZE]; |
| 29 | |
| 30 | |
| 31 | /* Test transaction layer. */ |
| 32 | static 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 | |
| 41 | target = pj_str(TARGET_URI); |
| 42 | from = pj_str(FROM_URI); |
| 43 | |
| 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. */ |
| 76 | static 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 | |
| 85 | target = pj_str(TARGET_URI); |
| 86 | from = pj_str(FROM_URI); |
| 87 | |
| 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_grp_lock_release(tsx->grp_lock); |
| 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 | |
| 139 | int tsx_basic_test(struct tsx_test_param *param) |
| 140 | { |
| 141 | int status; |
| 142 | |
| 143 | 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 | |
| 148 | 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 | } |
| 158 | |
| 159 | /**************************************************************************/ |
| 160 | |
| 161 | struct tsx_test_state |
| 162 | { |
| 163 | pj_size_t pool_cnt; |
| 164 | }; |
| 165 | |
| 166 | static void save_tsx_test_state(struct tsx_test_state *st) |
| 167 | { |
| 168 | st->pool_cnt = caching_pool.used_count; |
| 169 | } |
| 170 | |
| 171 | static pj_status_t check_tsx_test_state(struct tsx_test_state *st) |
| 172 | { |
| 173 | if (caching_pool.used_count > st->pool_cnt) |
| 174 | return -1; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void destroy_endpt() |
| 180 | { |
| 181 | pjsip_endpt_destroy(endpt); |
| 182 | endpt = NULL; |
| 183 | } |
| 184 | |
| 185 | static pj_status_t init_endpt() |
| 186 | { |
| 187 | pj_str_t ns = { "10.187.27.172", 13}; /* just a random, unreachable IP */ |
| 188 | pj_dns_resolver *resolver; |
| 189 | pj_status_t rc; |
| 190 | |
| 191 | rc = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt); |
| 192 | if (rc != PJ_SUCCESS) { |
| 193 | app_perror("pjsip_endpt_create", rc); |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /* Start transaction layer module. */ |
| 198 | rc = pjsip_tsx_layer_init_module(endpt); |
| 199 | if (rc != PJ_SUCCESS) { |
| 200 | app_perror("tsx_layer_init", rc); |
| 201 | return rc; |
| 202 | } |
| 203 | |
| 204 | rc = pjsip_udp_transport_start(endpt, NULL, NULL, 1, NULL); |
| 205 | if (rc != PJ_SUCCESS) { |
| 206 | app_perror("udp init", rc); |
| 207 | return rc; |
| 208 | } |
| 209 | |
| 210 | rc = pjsip_tcp_transport_start(endpt, NULL, 1, NULL); |
| 211 | if (rc != PJ_SUCCESS) { |
| 212 | app_perror("tcp init", rc); |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | rc = pjsip_endpt_create_resolver(endpt, &resolver); |
| 217 | if (rc != PJ_SUCCESS) { |
| 218 | app_perror("create resolver", rc); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | pj_dns_resolver_set_ns(resolver, 1, &ns, NULL); |
| 223 | |
| 224 | rc = pjsip_endpt_set_resolver(endpt, resolver); |
| 225 | if (rc != PJ_SUCCESS) { |
| 226 | app_perror("set resolver", rc); |
| 227 | return rc; |
| 228 | } |
| 229 | |
| 230 | return PJ_SUCCESS; |
| 231 | } |
| 232 | |
| 233 | static int tsx_create_and_send_req(void *arg) |
| 234 | { |
| 235 | pj_str_t dst_uri = pj_str((char*)arg); |
| 236 | pj_str_t from_uri = pj_str((char*)"<sip:user@host>"); |
| 237 | pjsip_tx_data *tdata; |
| 238 | pj_status_t status; |
| 239 | |
| 240 | status = pjsip_endpt_create_request(endpt, &pjsip_options_method, |
| 241 | &dst_uri, &from_uri, &dst_uri, |
| 242 | NULL, NULL, -1, NULL, |
| 243 | &tdata); |
| 244 | if (status != PJ_SUCCESS) |
| 245 | return status; |
| 246 | |
| 247 | status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL); |
| 248 | if (status != PJ_SUCCESS) |
| 249 | return status; |
| 250 | |
| 251 | return PJ_SUCCESS; |
| 252 | } |
| 253 | |
| 254 | int tsx_destroy_test() |
| 255 | { |
| 256 | struct tsx_test_state state; |
| 257 | struct test_desc |
| 258 | { |
| 259 | const char *title; |
| 260 | int (*func)(void*); |
| 261 | void *arg; |
| 262 | int sleep_before_unload; |
| 263 | int sleep_after_unload; |
| 264 | } test_entries[] = |
| 265 | { |
| 266 | { |
| 267 | "normal unable to resolve", |
| 268 | &tsx_create_and_send_req, |
| 269 | "sip:user@somehost", |
| 270 | 10000, |
| 271 | 1 |
| 272 | }, |
| 273 | { |
| 274 | "resolve and destroy, wait", |
| 275 | &tsx_create_and_send_req, |
| 276 | "sip:user@somehost", |
| 277 | 1, |
| 278 | 10000 |
| 279 | }, |
| 280 | { |
| 281 | "tcp connect and destroy", |
| 282 | &tsx_create_and_send_req, |
| 283 | "sip:user@10.125.36.63:58517;transport=tcp", |
| 284 | 60000, |
| 285 | 1000 |
| 286 | }, |
| 287 | { |
| 288 | "tcp connect and destroy", |
| 289 | &tsx_create_and_send_req, |
| 290 | "sip:user@10.125.36.63:58517;transport=tcp", |
| 291 | 1, |
| 292 | 60000 |
| 293 | }, |
| 294 | |
| 295 | }; |
| 296 | int rc; |
| 297 | unsigned i; |
| 298 | const int INDENT = 2; |
| 299 | |
| 300 | pj_log_add_indent(INDENT); |
| 301 | destroy_endpt(); |
| 302 | |
| 303 | for (i=0; i<PJ_ARRAY_SIZE(test_entries); ++i) { |
| 304 | struct test_desc *td = &test_entries[i]; |
| 305 | |
| 306 | PJ_LOG(3,(THIS_FILE, "%s", td->title)); |
| 307 | |
| 308 | pj_log_add_indent(INDENT); |
| 309 | save_tsx_test_state(&state); |
| 310 | |
| 311 | rc = init_endpt(); |
| 312 | if (rc != PJ_SUCCESS) { |
| 313 | pj_log_add_indent(-INDENT*2); |
| 314 | return -10; |
| 315 | } |
| 316 | |
| 317 | rc = td->func(td->arg); |
| 318 | if (rc != PJ_SUCCESS) { |
| 319 | pj_log_add_indent(-INDENT*2); |
| 320 | return -20; |
| 321 | } |
| 322 | |
| 323 | flush_events(td->sleep_before_unload); |
| 324 | pjsip_tsx_layer_destroy(); |
| 325 | flush_events(td->sleep_after_unload); |
| 326 | destroy_endpt(); |
| 327 | |
| 328 | rc = check_tsx_test_state(&state); |
| 329 | if (rc != PJ_SUCCESS) { |
| 330 | init_endpt(); |
| 331 | pj_log_add_indent(-INDENT*2); |
| 332 | return -30; |
| 333 | } |
| 334 | |
| 335 | pj_log_add_indent(-INDENT); |
| 336 | } |
| 337 | |
| 338 | init_endpt(); |
| 339 | |
| 340 | pj_log_add_indent(-INDENT); |
| 341 | return 0; |
| 342 | } |
| 343 | |