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