1
/* $Id: test.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
23
void app_perror(const char *msg, pj_status_t rc)
29
pj_strerror(rc, errbuf, sizeof(errbuf));
30
PJ_LOG(1,("test", "%s: [pj_status_t=%d] %s", msg, rc, errbuf));
33
pj_status_t create_stun_config(pj_pool_t *pool, pj_stun_config *stun_cfg)
35
pj_ioqueue_t *ioqueue;
36
pj_timer_heap_t *timer_heap;
39
status = pj_ioqueue_create(pool, 64, &ioqueue);
40
if (status != PJ_SUCCESS) {
41
app_perror(" pj_ioqueue_create()", status);
45
status = pj_timer_heap_create(pool, 256, &timer_heap);
46
if (status != PJ_SUCCESS) {
47
app_perror(" pj_timer_heap_create()", status);
48
pj_ioqueue_destroy(ioqueue);
52
pj_stun_config_init(stun_cfg, mem, 0, ioqueue, timer_heap);
57
void destroy_stun_config(pj_stun_config *stun_cfg)
59
if (stun_cfg->timer_heap) {
60
pj_timer_heap_destroy(stun_cfg->timer_heap);
61
stun_cfg->timer_heap = NULL;
63
if (stun_cfg->ioqueue) {
64
pj_ioqueue_destroy(stun_cfg->ioqueue);
65
stun_cfg->ioqueue = NULL;
69
void poll_events(pj_stun_config *stun_cfg, unsigned msec,
70
pj_bool_t first_event_only)
72
pj_time_val stop_time;
75
pj_gettimeofday(&stop_time);
76
stop_time.msec += msec;
77
pj_time_val_normalize(&stop_time);
79
/* Process all events for the specified duration. */
81
pj_time_val timeout = {0, 1}, now;
84
c = pj_timer_heap_poll( stun_cfg->timer_heap, NULL );
88
//timeout.sec = timeout.msec = 0;
89
c = pj_ioqueue_poll( stun_cfg->ioqueue, &timeout);
93
pj_gettimeofday(&now);
94
if (PJ_TIME_VAL_GTE(now, stop_time))
97
if (first_event_only && count >= 0)
102
void capture_pjlib_state(pj_stun_config *cfg, struct pjlib_state *st)
106
st->timer_cnt = pj_timer_heap_count(cfg->timer_heap);
108
cp = (pj_caching_pool*)mem;
109
st->pool_used_cnt = cp->used_count;
112
int check_pjlib_state(pj_stun_config *cfg,
113
const struct pjlib_state *initial_st)
115
struct pjlib_state current_state;
118
capture_pjlib_state(cfg, ¤t_state);
120
if (current_state.timer_cnt > initial_st->timer_cnt) {
121
PJ_LOG(3,("", " error: possibly leaking timer"));
122
rc |= ERR_TIMER_LEAK;
125
if (current_state.pool_used_cnt > initial_st->pool_used_cnt) {
126
PJ_LOG(3,("", " error: possibly leaking memory"));
127
PJ_LOG(3,("", " dumping memory pool:"));
128
pj_pool_factory_dump(mem, PJ_TRUE);
129
rc |= ERR_MEMORY_LEAK;
136
#define DO_TEST(test) do { \
137
PJ_LOG(3, ("test", "Running %s...", #test)); \
141
(char*)(rc ? "..ERROR" : "..success"), rc)); \
142
if (rc!=0) goto on_return; \
146
pj_pool_factory *mem;
148
int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
149
PJ_LOG_HAS_MICRO_SEC;
151
static int test_inner(void)
153
pj_caching_pool caching_pool;
156
mem = &caching_pool.factory;
160
pj_log_set_decor(param_log_decor);
165
app_perror("pj_init() error!!", rc);
170
pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 );
175
#if INCLUDE_STUN_TEST
176
DO_TEST(stun_test());
177
DO_TEST(sess_auth_test());
184
#if INCLUDE_STUN_SOCK_TEST
185
DO_TEST(stun_sock_test());
188
#if INCLUDE_TURN_SOCK_TEST
189
DO_TEST(turn_sock_test());
204
int id = PJ_GET_EXCEPTION();
205
PJ_LOG(3,("test", "FATAL: unhandled exception id %d (%s)",
206
id, pj_exception_id_name(id)));