Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +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 | |
| 21 | #include "test.h" |
| 22 | #include <pjlib.h> |
| 23 | #include <pjsip_core.h> |
| 24 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 25 | #define THIS_FILE "test.c" |
| 26 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 27 | #define DO_TEST(test) do { \ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 28 | PJ_LOG(3, (THIS_FILE, "Running %s...", #test)); \ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 29 | rc = test; \ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 30 | PJ_LOG(3, (THIS_FILE, \ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 31 | "%s(%d)", \ |
| 32 | (rc ? "..ERROR" : "..success"), rc)); \ |
| 33 | if (rc!=0) goto on_return; \ |
| 34 | } while (0) |
| 35 | |
| 36 | |
| 37 | |
| 38 | pjsip_endpoint *endpt; |
Benny Prijono | 9c1d9f5 | 2006-01-07 23:01:56 +0000 | [diff] [blame] | 39 | int log_level = 5; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 40 | |
| 41 | void app_perror(const char *msg, pj_status_t rc) |
| 42 | { |
| 43 | char errbuf[256]; |
| 44 | |
| 45 | PJ_CHECK_STACK(); |
| 46 | |
| 47 | pjsip_strerror(rc, errbuf, sizeof(errbuf)); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 48 | PJ_LOG(3,(THIS_FILE, "%s: [pj_status_t=%d] %s", msg, rc, errbuf)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 49 | |
| 50 | } |
| 51 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 52 | void flush_events(unsigned duration) |
| 53 | { |
| 54 | pj_time_val stop_time; |
| 55 | |
| 56 | pj_gettimeofday(&stop_time); |
| 57 | stop_time.msec += duration; |
| 58 | pj_time_val_normalize(&stop_time); |
| 59 | |
| 60 | /* Process all events for the specified duration. */ |
| 61 | for (;;) { |
| 62 | pj_time_val timeout = {0, 1}, now; |
| 63 | |
| 64 | pjsip_endpt_handle_events(endpt, &timeout); |
| 65 | |
| 66 | pj_gettimeofday(&now); |
| 67 | if (PJ_TIME_VAL_GTE(now, stop_time)) |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 72 | pj_status_t register_static_modules(pj_size_t *count, pjsip_module **modules) |
| 73 | { |
| 74 | *count = 0; |
| 75 | return PJ_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | int test_main(void) |
| 79 | { |
| 80 | pj_status_t rc; |
| 81 | pj_caching_pool caching_pool; |
| 82 | const char *filename; |
| 83 | int line; |
| 84 | |
Benny Prijono | 9c1d9f5 | 2006-01-07 23:01:56 +0000 | [diff] [blame] | 85 | pj_log_set_level(log_level); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 86 | /* |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 87 | pj_log_set_decor(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME | |
| 88 | PJ_LOG_HAS_MICRO_SEC); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 89 | */ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 90 | |
| 91 | if ((rc=pj_init()) != PJ_SUCCESS) { |
| 92 | app_perror("pj_init", rc); |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | pj_dump_config(); |
| 97 | |
| 98 | pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 ); |
| 99 | |
| 100 | rc = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt); |
| 101 | if (rc != PJ_SUCCESS) { |
| 102 | app_perror("pjsip_endpt_create", rc); |
| 103 | pj_caching_pool_destroy(&caching_pool); |
| 104 | return rc; |
| 105 | } |
| 106 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 107 | PJ_LOG(3,(THIS_FILE,"")); |
| 108 | |
| 109 | /* Init logger module. */ |
| 110 | init_msg_logger(); |
| 111 | msg_logger_set_enabled(1); |
| 112 | |
| 113 | /* Start transaction layer module. */ |
| 114 | rc = pjsip_tsx_layer_init(endpt); |
| 115 | if (rc != PJ_SUCCESS) { |
| 116 | app_perror(" Error initializing transaction module", rc); |
| 117 | goto on_return; |
| 118 | } |
| 119 | |
| 120 | /* Create loop transport. */ |
| 121 | rc = pjsip_loop_start(endpt, NULL); |
| 122 | if (rc != PJ_SUCCESS) { |
| 123 | app_perror(" error: unable to create datagram loop transport", |
| 124 | rc); |
| 125 | goto on_return; |
| 126 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 127 | |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 128 | //DO_TEST(uri_test()); |
| 129 | //DO_TEST(msg_test()); |
| 130 | //DO_TEST(txdata_test()); |
| 131 | //DO_TEST(transport_udp_test()); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 132 | //DO_TEST(transport_loop_test()); |
| 133 | //DO_TEST(tsx_basic_test()); |
Benny Prijono | dbe337a | 2006-01-08 23:57:52 +0000 | [diff] [blame] | 134 | //DO_TEST(tsx_uac_test()); |
| 135 | DO_TEST(tsx_uas_test()); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 136 | |
| 137 | on_return: |
| 138 | |
| 139 | pjsip_endpt_destroy(endpt); |
| 140 | pj_caching_pool_destroy(&caching_pool); |
| 141 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 142 | PJ_LOG(3,(THIS_FILE, "")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 143 | |
| 144 | pj_thread_get_stack_info(pj_thread_this(), &filename, &line); |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 145 | PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u", |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 146 | pj_thread_get_stack_max_usage(pj_thread_this()), |
| 147 | filename, line)); |
| 148 | if (rc == 0) |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 149 | PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!..")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 150 | else |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 151 | PJ_LOG(3,(THIS_FILE, "Test completed with error(s)")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |