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