Debian Codegraph @

Xpjproject/pjnath/src/pjnath-test/test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #include "test.h"
20 #include <pjlib.h>
21 #include <pj/compat/socket.h>
22
23 #define THIS_FILE "test.c"
24
25 void app_perror_dbg(const char *msg, pj_status_t rc,
26 const char *file, int line)
27 {
28 char errbuf[256];
29
30 PJ_CHECK_STACK();
31
32 pj_strerror(rc, errbuf, sizeof(errbuf));
33 PJ_LOG(1,("test", "%s:%d: %s: [pj_status_t=%d] %s", file, line, msg,
34 rc, errbuf));
35 }
36
37 /* Set socket to nonblocking. */
38 void app_set_sock_nb(pj_sock_t sock)
39 {
40 #if defined(PJ_WIN32) && PJ_WIN32!=0 || \
41 defined(PJ_WIN64) && PJ_WIN64 != 0 || \
42 defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0
43 u_long value = 1;
44 ioctlsocket(sock, FIONBIO, &value);
45 #else
46 pj_uint32_t value = 1;
47 ioctl(sock, FIONBIO, &value);
48 #endif
49 }
50
51 pj_status_t create_stun_config(app_sess_t *app_sess)
52 {
53 pj_ioqueue_t *ioqueue = NULL;
54 pj_timer_heap_t *timer_heap = NULL;
55 pj_lock_t *lock = NULL;
56 pj_status_t status;
57
58 pj_bzero(app_sess, sizeof(*app_sess));
59 pj_caching_pool_init(&app_sess->cp,
60 &pj_pool_factory_default_policy, 0 );
61 app_sess->pool = pj_pool_create(&app_sess->cp.factory, NULL,
62 512, 512, NULL);
63 PJ_TEST_NOT_NULL(app_sess->pool, NULL,
64 { status=PJ_ENOMEM; goto on_error;});
65
66 PJ_TEST_SUCCESS(pj_ioqueue_create(app_sess->pool, 64, &ioqueue), NULL,
67 {status = tmp_status_; goto on_error;});
68 PJ_TEST_SUCCESS(pj_timer_heap_create(app_sess->pool, 256, &timer_heap),
69 NULL, {status = tmp_status_; goto on_error;});
70
71 pj_lock_create_recursive_mutex(app_sess->pool, NULL, &lock);
72 pj_timer_heap_set_lock(timer_heap, lock, PJ_TRUE);
73 lock = NULL;
74
75 pj_stun_config_init(&app_sess->stun_cfg, app_sess->pool->factory, 0,
76 ioqueue, timer_heap);
77
78 return PJ_SUCCESS;
79
80 on_error:
81 if (ioqueue)
82 pj_ioqueue_destroy(ioqueue);
83 if (timer_heap)
84 pj_timer_heap_destroy(timer_heap);
85 // Lock should have been destroyed by timer heap.
86 /*
87 if (lock)
88 pj_lock_destroy(lock);
89 */
90 if (app_sess->pool)
91 pj_pool_release(app_sess->pool);
92 pj_caching_pool_destroy(&app_sess->cp);
93 return status;
94 }
95
96 void destroy_stun_config(app_sess_t *app_sess)
97 {
98 if (app_sess->stun_cfg.timer_heap) {
99 pj_timer_heap_destroy(app_sess->stun_cfg.timer_heap);
100 app_sess->stun_cfg.timer_heap = NULL;
101 }
102 if (app_sess->stun_cfg.ioqueue) {
103 pj_ioqueue_destroy(app_sess->stun_cfg.ioqueue);
104 app_sess->stun_cfg.ioqueue = NULL;
105 }
106 if (app_sess->pool) {
107 pj_pool_release(app_sess->pool);
108 app_sess->pool = NULL;
109 }
110 pj_caching_pool_destroy(&app_sess->cp);
111 }
112
113 void poll_events(pj_stun_config *stun_cfg, unsigned msec,
114 pj_bool_t first_event_only)
115 {
116 pj_time_val stop_time;
117 int count = 0;
118
119 pj_gettimeofday(&stop_time);
120 stop_time.msec += msec;
121 pj_time_val_normalize(&stop_time);
122
123 /* Process all events for the specified duration. */
124 for (;;) {
125 pj_time_val timeout = {0, 1}, now;
126 int c;
127
128 c = pj_timer_heap_poll( stun_cfg->timer_heap, NULL );
129 if (c > 0)
130 count += c;
131
132 //timeout.sec = timeout.msec = 0;
133 c = pj_ioqueue_poll( stun_cfg->ioqueue, &timeout);
134 if (c > 0)
135 count += c;
136
137 pj_gettimeofday(&now);
138 if (PJ_TIME_VAL_GTE(now, stop_time))
139 break;
140
141 if (first_event_only && count >= 0)
142 break;
143 }
144 }
145
146 void capture_pjlib_state(pj_stun_config *cfg, struct pjlib_state *st)
147 {
148 pj_caching_pool *cp;
149
150 st->timer_cnt = (unsigned)pj_timer_heap_count(cfg->timer_heap);
151
152 cp = (pj_caching_pool*)cfg->pf;
153 st->pool_used_cnt = (unsigned)cp->used_count;
154 }
155
156 int check_pjlib_state(pj_stun_config *cfg,
157 const struct pjlib_state *initial_st)
158 {
159 struct pjlib_state current_state;
160 int rc = 0;
161
162 capture_pjlib_state(cfg, &current_state);
163
164 if (current_state.timer_cnt > initial_st->timer_cnt) {
165 rc |= ERR_TIMER_LEAK;
166
167 #if PJ_TIMER_DEBUG
168 pj_timer_heap_dump(cfg->timer_heap);
169 #endif
170 PJ_LOG(3,("", " error: possibly leaking timer"));
171 }
172
173 if (current_state.pool_used_cnt > initial_st->pool_used_cnt) {
174 PJ_LOG(3,("", " dumping memory pool:"));
175 pj_pool_factory_dump(cfg->pf, PJ_TRUE);
176 PJ_LOG(3,("", " error: possibly leaking memory"));
177 rc |= ERR_MEMORY_LEAK;
178 }
179
180 return rc;
181 }
182
183
184 pj_pool_factory *mem;
185 struct test_app_t test_app;
186
187 int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME | PJ_LOG_HAS_SENDER |
188 PJ_LOG_HAS_MICRO_SEC;
189
190 pj_log_func *orig_log_func;
191 FILE *log_file;
192
193 static void test_log_func(int level, const char *data, int len)
194 {
195 if (log_file) {
196 fwrite(data, len, 1, log_file);
197 }
198 if (level <= 3)
199 orig_log_func(level, data, len);
200 }
201
202 static int test_inner(int argc, char *argv[])
203 {
204 pj_caching_pool caching_pool;
205 int i, rc = 0;
206
207 mem = &caching_pool.factory;
208
209 #if 1
210 pj_log_set_level(3);
211 pj_log_set_decor(param_log_decor);
212 PJ_UNUSED_ARG(test_log_func);
213 #elif 1
214 log_file = fopen("pjnath-test.log", "wt");
215 pj_log_set_level(5);
216 orig_log_func = pj_log_get_log_func();
217 pj_log_set_log_func(&test_log_func);
218 #endif
219
220 PJ_TEST_SUCCESS(pj_init(), NULL,
221 {if (log_file) fclose(log_file); return 1; });
222
223 if (test_app.ut_app.prm_config)
224 pj_dump_config();
225 pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 );
226
227 PJ_TEST_SUCCESS(pjlib_util_init(), NULL, {rc=2; goto on_return;});
228 PJ_TEST_SUCCESS(pjnath_init(), NULL, {rc=3; goto on_return;});
229 PJ_TEST_SUCCESS(ut_app_init1(&test_app.ut_app, mem),
230 NULL, {rc=4; goto on_return;});
231
232 #if INCLUDE_STUN_TEST
233 UT_ADD_TEST(&test_app.ut_app, stun_test, 0);
234 UT_ADD_TEST(&test_app.ut_app, sess_auth_test, 0);
235 #endif
236
237 #if INCLUDE_STUN_SOCK_TEST
238 UT_ADD_TEST(&test_app.ut_app, stun_sock_test, 0);
239 #endif
240
241 #if INCLUDE_ICE_TEST
242 for (i=0; i<ICE_TEST_START_ARRAY+ICE_TEST_ARRAY_COUNT; ++i)
243 UT_ADD_TEST1(&test_app.ut_app, ice_test, (void*)(intptr_t)i, 0);
244 #endif
245
246 #if INCLUDE_TRICKLE_ICE_TEST
247 UT_ADD_TEST(&test_app.ut_app, trickle_ice_test, 0);
248 #endif
249
250 #if INCLUDE_TURN_SOCK_TEST
251 UT_ADD_TEST1(&test_app.ut_app, turn_sock_test, (void*)(intptr_t)0, 0);
252 UT_ADD_TEST1(&test_app.ut_app, turn_sock_test, (void*)(intptr_t)1, 0);
253 UT_ADD_TEST1(&test_app.ut_app, turn_sock_test, (void*)(intptr_t)2, 0);
254 #endif
255
256 #if INCLUDE_CONCUR_TEST
257 UT_ADD_TEST(&test_app.ut_app, ice_conc_test, 0);
258
259 for (i=0; i<50; ++i) {
260 UT_ADD_TEST(&test_app.ut_app, concur_test, 0);
261 }
262 #else
263 PJ_UNUSED_ARG(i);
264 #endif
265
266 if (ut_run_tests(&test_app.ut_app, "pjnath tests", argc, argv)) {
267 rc = 5;
268 } else {
269 rc = 0;
270 }
271
272 ut_app_destroy(&test_app.ut_app);
273
274 on_return:
275 if (log_file)
276 fclose(log_file);
277 pj_caching_pool_destroy( &caching_pool );
278 return rc;
279 }
280
281 int test_main(int argc, char *argv[])
282 {
283 PJ_USE_EXCEPTION;
284
285 PJ_TRY {
286 return test_inner(argc, argv);
287 }
288 PJ_CATCH_ANY {
289 int id = PJ_GET_EXCEPTION();
290 PJ_LOG(3,("test", "FATAL: unhandled exception id %d (%s)",
291 id, pj_exception_id_name(id)));
292 }
293 PJ_END;
294
295 return -1;
296 }
297