blob: a8f95b95fc7c488a4a858af2793b3fe82c8b00c1 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id: test.c 4537 2013-06-19 06:47:43Z riza $ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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#include "test.h"
21#include <pjlib.h>
22
23void app_perror(const char *msg, pj_status_t rc)
24{
25 char errbuf[256];
26
27 PJ_CHECK_STACK();
28
29 pj_strerror(rc, errbuf, sizeof(errbuf));
30 PJ_LOG(1,("test", "%s: [pj_status_t=%d] %s", msg, rc, errbuf));
31}
32
33pj_status_t create_stun_config(pj_pool_t *pool, pj_stun_config *stun_cfg)
34{
35 pj_ioqueue_t *ioqueue;
36 pj_timer_heap_t *timer_heap;
37 pj_lock_t *lock;
38 pj_status_t status;
39
40 status = pj_ioqueue_create(pool, 64, &ioqueue);
41 if (status != PJ_SUCCESS) {
42 app_perror(" pj_ioqueue_create()", status);
43 return status;
44 }
45
46 status = pj_timer_heap_create(pool, 256, &timer_heap);
47 if (status != PJ_SUCCESS) {
48 app_perror(" pj_timer_heap_create()", status);
49 pj_ioqueue_destroy(ioqueue);
50 return status;
51 }
52
53 pj_lock_create_recursive_mutex(pool, NULL, &lock);
54 pj_timer_heap_set_lock(timer_heap, lock, PJ_TRUE);
55
56 pj_stun_config_init(stun_cfg, mem, 0, ioqueue, timer_heap);
57
58 return PJ_SUCCESS;
59}
60
61void destroy_stun_config(pj_stun_config *stun_cfg)
62{
63 if (stun_cfg->timer_heap) {
64 pj_timer_heap_destroy(stun_cfg->timer_heap);
65 stun_cfg->timer_heap = NULL;
66 }
67 if (stun_cfg->ioqueue) {
68 pj_ioqueue_destroy(stun_cfg->ioqueue);
69 stun_cfg->ioqueue = NULL;
70 }
71}
72
73void poll_events(pj_stun_config *stun_cfg, unsigned msec,
74 pj_bool_t first_event_only)
75{
76 pj_time_val stop_time;
77 int count = 0;
78
79 pj_gettimeofday(&stop_time);
80 stop_time.msec += msec;
81 pj_time_val_normalize(&stop_time);
82
83 /* Process all events for the specified duration. */
84 for (;;) {
85 pj_time_val timeout = {0, 1}, now;
86 int c;
87
88 c = pj_timer_heap_poll( stun_cfg->timer_heap, NULL );
89 if (c > 0)
90 count += c;
91
92 //timeout.sec = timeout.msec = 0;
93 c = pj_ioqueue_poll( stun_cfg->ioqueue, &timeout);
94 if (c > 0)
95 count += c;
96
97 pj_gettimeofday(&now);
98 if (PJ_TIME_VAL_GTE(now, stop_time))
99 break;
100
101 if (first_event_only && count >= 0)
102 break;
103 }
104}
105
106void capture_pjlib_state(pj_stun_config *cfg, struct pjlib_state *st)
107{
108 pj_caching_pool *cp;
109
110 st->timer_cnt = (unsigned)pj_timer_heap_count(cfg->timer_heap);
111
112 cp = (pj_caching_pool*)cfg->pf;
113 st->pool_used_cnt = (unsigned)cp->used_count;
114}
115
116int check_pjlib_state(pj_stun_config *cfg,
117 const struct pjlib_state *initial_st)
118{
119 struct pjlib_state current_state;
120 int rc = 0;
121
122 capture_pjlib_state(cfg, &current_state);
123
124 if (current_state.timer_cnt > initial_st->timer_cnt) {
125 PJ_LOG(3,("", " error: possibly leaking timer"));
126 rc |= ERR_TIMER_LEAK;
127
128#if PJ_TIMER_DEBUG
129 pj_timer_heap_dump(cfg->timer_heap);
130#endif
131 }
132
133 if (current_state.pool_used_cnt > initial_st->pool_used_cnt) {
134 PJ_LOG(3,("", " error: possibly leaking memory"));
135 PJ_LOG(3,("", " dumping memory pool:"));
136 pj_pool_factory_dump(mem, PJ_TRUE);
137 rc |= ERR_MEMORY_LEAK;
138 }
139
140 return rc;
141}
142
143
144#define DO_TEST(test) do { \
145 PJ_LOG(3, ("test", "Running %s...", #test)); \
146 rc = test; \
147 PJ_LOG(3, ("test", \
148 "%s(%d)", \
149 (char*)(rc ? "..ERROR" : "..success"), rc)); \
150 if (rc!=0) goto on_return; \
151 } while (0)
152
153
154pj_pool_factory *mem;
155
156int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
157 PJ_LOG_HAS_MICRO_SEC;
158
159pj_log_func *orig_log_func;
160FILE *log_file;
161
162static void test_log_func(int level, const char *data, int len)
163{
164 if (log_file) {
165 fwrite(data, len, 1, log_file);
166 }
167 if (level <= 3)
168 orig_log_func(level, data, len);
169}
170
171static int test_inner(void)
172{
173 pj_caching_pool caching_pool;
174 int rc = 0;
175
176 mem = &caching_pool.factory;
177
178#if 1
179 pj_log_set_level(3);
180 pj_log_set_decor(param_log_decor);
181#elif 1
182 log_file = fopen("pjnath-test.log", "wt");
183 pj_log_set_level(5);
184 orig_log_func = pj_log_get_log_func();
185 pj_log_set_log_func(&test_log_func);
186#endif
187
188 rc = pj_init();
189 if (rc != 0) {
190 app_perror("pj_init() error!!", rc);
191 return rc;
192 }
193
194 pj_dump_config();
195 pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 );
196
197 pjlib_util_init();
198 pjnath_init();
199
200#if INCLUDE_STUN_TEST
201 DO_TEST(stun_test());
202 DO_TEST(sess_auth_test());
203#endif
204
205#if INCLUDE_ICE_TEST
206 DO_TEST(ice_test());
207#endif
208
209#if INCLUDE_STUN_SOCK_TEST
210 DO_TEST(stun_sock_test());
211#endif
212
213#if INCLUDE_TURN_SOCK_TEST
214 DO_TEST(turn_sock_test());
215#endif
216
217#if INCLUDE_CONCUR_TEST
218 DO_TEST(concur_test());
219#endif
220
221on_return:
222 if (log_file)
223 fclose(log_file);
224 return rc;
225}
226
227int test_main(void)
228{
229 PJ_USE_EXCEPTION;
230
231 PJ_TRY {
232 return test_inner();
233 }
234 PJ_CATCH_ANY {
235 int id = PJ_GET_EXCEPTION();
236 PJ_LOG(3,("test", "FATAL: unhandled exception id %d (%s)",
237 id, pj_exception_id_name(id)));
238 }
239 PJ_END;
240
241 return -1;
242}
243