~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjnath/src/pjnath-test/test.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier, Francois Marier, Mark Purcell
  • Date: 2014-10-18 15:08:50 UTC
  • mfrom: (1.1.12)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20141018150850-2exfk34ckb15pcwi
Tags: 1.4.1-0.1
[ Francois Marier ]
* Non-maintainer upload
* New upstream release (closes: #759576, #741130)
  - debian/rules +PJPROJECT_VERSION := 2.2.1
  - add upstream patch to fix broken TLS support
  - add patch to fix pjproject regression

[ Mark Purcell ]
* Build-Depends:
  - sflphone-daemon + libavformat-dev, libavcodec-dev, libswscale-dev,
  libavdevice-dev, libavutil-dev
  - sflphone-gnome + libclutter-gtk-1.0-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: test.c 4728 2014-02-04 10:13:56Z bennylp $ */
 
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
 
 
23
void 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
 
 
33
pj_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
 
 
61
void 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
 
 
73
void 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
 
 
106
void 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
 
 
116
int 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
 
 
154
pj_pool_factory *mem;
 
155
 
 
156
int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
 
157
                      PJ_LOG_HAS_MICRO_SEC;
 
158
 
 
159
pj_log_func *orig_log_func;
 
160
FILE *log_file;
 
161
 
 
162
static 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
 
 
171
static 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
    PJ_UNUSED_ARG(test_log_func);
 
182
#elif 1
 
183
    log_file = fopen("pjnath-test.log", "wt");
 
184
    pj_log_set_level(5);
 
185
    orig_log_func = pj_log_get_log_func();
 
186
    pj_log_set_log_func(&test_log_func);
 
187
#endif
 
188
 
 
189
    rc = pj_init();
 
190
    if (rc != 0) {
 
191
        app_perror("pj_init() error!!", rc);
 
192
        return rc;
 
193
    }
 
194
 
 
195
    pj_dump_config();
 
196
    pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 );
 
197
 
 
198
    pjlib_util_init();
 
199
    pjnath_init();
 
200
 
 
201
#if INCLUDE_STUN_TEST
 
202
    DO_TEST(stun_test());
 
203
    DO_TEST(sess_auth_test());
 
204
#endif
 
205
 
 
206
#if INCLUDE_ICE_TEST
 
207
    DO_TEST(ice_test());
 
208
#endif
 
209
 
 
210
#if INCLUDE_STUN_SOCK_TEST
 
211
    DO_TEST(stun_sock_test());
 
212
#endif
 
213
 
 
214
#if INCLUDE_TURN_SOCK_TEST
 
215
    DO_TEST(turn_sock_test());
 
216
#endif
 
217
 
 
218
#if INCLUDE_CONCUR_TEST
 
219
    DO_TEST(concur_test());
 
220
#endif
 
221
 
 
222
on_return:
 
223
    if (log_file)
 
224
        fclose(log_file);
 
225
    return rc;
 
226
}
 
227
 
 
228
int test_main(void)
 
229
{
 
230
    PJ_USE_EXCEPTION;
 
231
 
 
232
    PJ_TRY {
 
233
        return test_inner();
 
234
    }
 
235
    PJ_CATCH_ANY {
 
236
        int id = PJ_GET_EXCEPTION();
 
237
        PJ_LOG(3,("test", "FATAL: unhandled exception id %d (%s)",
 
238
                  id, pj_exception_id_name(id)));
 
239
    }
 
240
    PJ_END;
 
241
 
 
242
    return -1;
 
243
}
 
244