Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 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" |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 22 | #include <pjsip.h> |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 23 | #include <pjlib.h> |
| 24 | |
| 25 | #define THIS_FILE "tsx_basic_test.c" |
| 26 | |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 27 | static char TARGET_URI[PJSIP_MAX_URL_SIZE]; |
| 28 | static char FROM_URI[PJSIP_MAX_URL_SIZE]; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 29 | |
| 30 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 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 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 41 | target = pj_str(TARGET_URI); |
| 42 | from = pj_str(FROM_URI); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 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 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 85 | target = pj_str(TARGET_URI); |
| 86 | from = pj_str(FROM_URI); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 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_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 Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 139 | int tsx_basic_test(struct tsx_test_param *param) |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 140 | { |
| 141 | int status; |
| 142 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 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 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 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 | } |