| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "test.h" |
| 22 |
#include <pjlib.h> |
| 23 |
#include <pjlib-util.h> |
| 24 |
#include <pjsip.h> |
| 25 |
|
| 26 |
#define THIS_FILE "test.c" |
| 27 |
|
| 28 |
pjsip_endpoint *endpt; |
| 29 |
pj_caching_pool caching_pool; |
| 30 |
int log_level = 3; |
| 31 |
int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME | PJ_LOG_HAS_SENDER | |
| 32 |
PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_INDENT; |
| 33 |
|
| 34 |
static pj_oshandle_t fd_report; |
| 35 |
const char *system_name = "Unknown"; |
| 36 |
static char buf[1024]; |
| 37 |
struct test_app_t test_app; |
| 38 |
struct tsx_test_param tsx_test[MAX_TSX_TESTS]; |
| 39 |
|
| 40 |
void app_perror(const char *msg, pj_status_t rc) |
| 41 |
{ |
| 42 |
char errbuf[256]; |
| 43 |
|
| 44 |
PJ_CHECK_STACK(); |
| 45 |
|
| 46 |
pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 47 |
PJ_LOG(3,(THIS_FILE, "%s: [pj_status_t=%d] %s", msg, rc, errbuf)); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
void flush_events(unsigned duration) |
| 52 |
{ |
| 53 |
pj_time_val stop_time; |
| 54 |
|
| 55 |
pj_gettimeofday(&stop_time); |
| 56 |
stop_time.msec += duration; |
| 57 |
pj_time_val_normalize(&stop_time); |
| 58 |
|
| 59 |
|
| 60 |
for (;;) { |
| 61 |
pj_time_val timeout = {0, 1}, now; |
| 62 |
|
| 63 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 64 |
|
| 65 |
pj_gettimeofday(&now); |
| 66 |
if (PJ_TIME_VAL_GTE(now, stop_time)) |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
pjsip_endpt_stop_handle_events(endpt); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
pjsip_transport *wait_loop_transport_clear(int secs) |
| 75 |
{ |
| 76 |
pj_time_val timeout; |
| 77 |
|
| 78 |
pj_gettimeofday(&timeout); |
| 79 |
timeout.sec += secs; |
| 80 |
|
| 81 |
for (;;) { |
| 82 |
pj_sockaddr_in addr; |
| 83 |
pjsip_transport *loop = NULL; |
| 84 |
pj_time_val now; |
| 85 |
pj_status_t status; |
| 86 |
|
| 87 |
loop = NULL; |
| 88 |
pj_bzero(&addr, sizeof(addr)); |
| 89 |
status = pjsip_endpt_acquire_transport(endpt, |
| 90 |
PJSIP_TRANSPORT_LOOP_DGRAM, |
| 91 |
&addr, sizeof(addr), NULL, |
| 92 |
&loop); |
| 93 |
if (status!=PJ_SUCCESS) |
| 94 |
return NULL; |
| 95 |
|
| 96 |
pj_gettimeofday(&now); |
| 97 |
if (PJ_TIME_VAL_GTE(now, timeout)) { |
| 98 |
return loop; |
| 99 |
} |
| 100 |
|
| 101 |
pjsip_transport_dec_ref(loop); |
| 102 |
loop = NULL; |
| 103 |
flush_events(500); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
pj_status_t register_static_modules(pj_size_t *count, pjsip_module **modules) |
| 108 |
{ |
| 109 |
PJ_UNUSED_ARG(modules); |
| 110 |
|
| 111 |
*count = 0; |
| 112 |
return PJ_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
static pj_status_t init_report(void) |
| 116 |
{ |
| 117 |
char tmp[80]; |
| 118 |
pj_time_val timestamp; |
| 119 |
pj_parsed_time date_time; |
| 120 |
pj_ssize_t len; |
| 121 |
pj_status_t status; |
| 122 |
|
| 123 |
pj_ansi_snprintf(tmp, sizeof(tmp), |
| 124 |
"pjsip-static-bench-%s-%s.htm", PJ_OS_NAME, PJ_CC_NAME); |
| 125 |
|
| 126 |
status = pj_file_open(NULL, tmp, PJ_O_WRONLY, &fd_report); |
| 127 |
if (status != PJ_SUCCESS) |
| 128 |
return status; |
| 129 |
|
| 130 |
|
| 131 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 132 |
"<HTML>\n" |
| 133 |
" <HEAD>\n" |
| 134 |
" <TITLE>PJSIP %s (%s) - Static Benchmark</TITLE>\n" |
| 135 |
" </HEAD>\n" |
| 136 |
"<BODY>\n" |
| 137 |
"\n", |
| 138 |
PJ_VERSION, |
| 139 |
(PJ_DEBUG ? "Debug" : "Release")); |
| 140 |
pj_file_write(fd_report, buf, &len); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 145 |
"<H1>PJSIP %s (%s) - Static Benchmark</H1>\n", |
| 146 |
PJ_VERSION, |
| 147 |
(PJ_DEBUG ? "Debug" : "Release")); |
| 148 |
pj_file_write(fd_report, buf, &len); |
| 149 |
|
| 150 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 151 |
"<P>Below is the benchmark result generated " |
| 152 |
"by <b>test-pjsip</b> program. The program " |
| 153 |
"is single-threaded only.</P>\n"); |
| 154 |
pj_file_write(fd_report, buf, &len); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 159 |
"<TABLE border=\"1\" cellpadding=\"4\">\n" |
| 160 |
" <TR><TD bgColor=\"aqua\" align=\"center\">Variable</TD>\n" |
| 161 |
" <TD bgColor=\"aqua\" align=\"center\">Value</TD>\n" |
| 162 |
" <TD bgColor=\"aqua\" align=\"center\">Description</TD>\n" |
| 163 |
" </TR>\n"); |
| 164 |
pj_file_write(fd_report, buf, &len); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
report_sval("version", PJ_VERSION, "", "PJLIB/PJSIP version"); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
report_sval("build-type", (PJ_DEBUG ? "Debug" : "Release"), "", "Build type"); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pj_gettimeofday(×tamp); |
| 177 |
report_ival("timestamp", timestamp.sec, "", "System timestamp of the test"); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
pj_time_decode(×tamp, &date_time); |
| 182 |
len = pj_ansi_snprintf(tmp, sizeof(tmp), "%04d-%02d-%02d %02d:%02d:%02d", |
| 183 |
date_time.year, date_time.mon+1, date_time.day, |
| 184 |
date_time.hour, date_time.min, date_time.sec); |
| 185 |
report_sval("date-time", tmp, "", "Date/time of the test"); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
report_sval("system", system_name, "", "System description"); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
report_sval("os-family", PJ_OS_NAME, "", "Operating system family"); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
len = pj_ansi_snprintf(tmp, sizeof(tmp), "%s-%d.%d.%d", PJ_CC_NAME, |
| 198 |
PJ_CC_VER_1, PJ_CC_VER_2, PJ_CC_VER_2); |
| 199 |
report_sval("cc-name", tmp, "", "Compiler name and version"); |
| 200 |
|
| 201 |
|
| 202 |
return PJ_SUCCESS; |
| 203 |
} |
| 204 |
|
| 205 |
void report_sval(const char *name, const char* value, const char *valname, |
| 206 |
const char *desc) |
| 207 |
{ |
| 208 |
pj_ssize_t len; |
| 209 |
|
| 210 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 211 |
" <TR><TD><TT>%s</TT></TD>\n" |
| 212 |
" <TD align=\"right\"><B>%s %s</B></TD>\n" |
| 213 |
" <TD>%s</TD>\n" |
| 214 |
" </TR>\n", |
| 215 |
name, value, valname, desc); |
| 216 |
pj_file_write(fd_report, buf, &len); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
void report_ival(const char *name, int value, const char *valname, |
| 221 |
const char *desc) |
| 222 |
{ |
| 223 |
pj_ssize_t len; |
| 224 |
|
| 225 |
len = pj_ansi_snprintf(buf, sizeof(buf), |
| 226 |
" <TR><TD><TT>%s</TT></TD>\n" |
| 227 |
" <TD align=\"right\"><B>%d %s</B></TD>\n" |
| 228 |
" <TD>%s</TD>\n" |
| 229 |
" </TR>\n", |
| 230 |
name, value, valname, desc); |
| 231 |
pj_file_write(fd_report, buf, &len); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
static void close_report(void) |
| 236 |
{ |
| 237 |
pj_ssize_t len; |
| 238 |
|
| 239 |
if (fd_report) { |
| 240 |
len = pj_ansi_snprintf(buf, sizeof(buf), "</TABLE>\n</BODY>\n</HTML>\n"); |
| 241 |
pj_file_write(fd_report, buf, &len); |
| 242 |
|
| 243 |
pj_file_close(fd_report); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
int test_main(int argc, char *argv[]) |
| 249 |
{ |
| 250 |
pj_status_t rc; |
| 251 |
const char *filename; |
| 252 |
unsigned tsx_test_cnt=0; |
| 253 |
#if INCLUDE_TSX_TEST |
| 254 |
unsigned i; |
| 255 |
pjsip_transport *tp; |
| 256 |
#if PJ_HAS_TCP |
| 257 |
pjsip_tpfactory *tpfactory; |
| 258 |
#endif |
| 259 |
#endif |
| 260 |
int line; |
| 261 |
|
| 262 |
pj_log_set_level(log_level); |
| 263 |
pj_log_set_decor(param_log_decor); |
| 264 |
|
| 265 |
PJ_TEST_SUCCESS(pj_init(), NULL, return 10); |
| 266 |
PJ_TEST_SUCCESS(pjlib_util_init(), NULL, return 20); |
| 267 |
PJ_TEST_SUCCESS(init_report(), NULL, return 30); |
| 268 |
|
| 269 |
if (test_app.ut_app.prm_config) { |
| 270 |
pj_dump_config(); |
| 271 |
} |
| 272 |
|
| 273 |
pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, |
| 274 |
PJSIP_TEST_MEM_SIZE ); |
| 275 |
|
| 276 |
PJ_TEST_SUCCESS(pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt), |
| 277 |
NULL, { |
| 278 |
pj_caching_pool_destroy(&caching_pool); |
| 279 |
return 40; |
| 280 |
}); |
| 281 |
|
| 282 |
|
| 283 |
init_msg_logger(); |
| 284 |
msg_logger_set_enabled(1); |
| 285 |
|
| 286 |
|
| 287 |
PJ_TEST_SUCCESS(rc = pjsip_tsx_layer_init_module(endpt), |
| 288 |
NULL, goto on_return); |
| 289 |
|
| 290 |
tsx_test[tsx_test_cnt].port = 5060; |
| 291 |
tsx_test[tsx_test_cnt].tp_type = "loop-dgram"; |
| 292 |
tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_LOOP_DGRAM; |
| 293 |
++tsx_test_cnt; |
| 294 |
|
| 295 |
PJ_TEST_SUCCESS(rc=ut_app_init1(&test_app.ut_app, &caching_pool.factory), |
| 296 |
NULL, goto on_return); |
| 297 |
|
| 298 |
#if INCLUDE_URI_TEST |
| 299 |
UT_ADD_TEST(&test_app.ut_app, uri_test, 0); |
| 300 |
#endif |
| 301 |
|
| 302 |
#if INCLUDE_MSG_TEST |
| 303 |
UT_ADD_TEST(&test_app.ut_app, msg_test, 0); |
| 304 |
UT_ADD_TEST(&test_app.ut_app, msg_err_test, 0); |
| 305 |
#endif |
| 306 |
|
| 307 |
#if INCLUDE_MULTIPART_TEST |
| 308 |
UT_ADD_TEST(&test_app.ut_app, multipart_test, 0); |
| 309 |
#endif |
| 310 |
|
| 311 |
#if INCLUDE_TXDATA_TEST |
| 312 |
UT_ADD_TEST(&test_app.ut_app, txdata_test, 0); |
| 313 |
#endif |
| 314 |
|
| 315 |
#if INCLUDE_TSX_BENCH |
| 316 |
UT_ADD_TEST(&test_app.ut_app, tsx_bench, 0); |
| 317 |
#endif |
| 318 |
|
| 319 |
#if INCLUDE_LOOP_TEST |
| 320 |
UT_ADD_TEST(&test_app.ut_app, transport_loop_multi_test, 0); |
| 321 |
#endif |
| 322 |
|
| 323 |
#if INCLUDE_RESOLVE_TEST |
| 324 |
UT_ADD_TEST(&test_app.ut_app, resolve_test, 0); |
| 325 |
#endif |
| 326 |
|
| 327 |
#if INCLUDE_INV_OA_TEST |
| 328 |
UT_ADD_TEST(&test_app.ut_app, inv_offer_answer_test, 0); |
| 329 |
#endif |
| 330 |
|
| 331 |
#if INCLUDE_TSX_TEST |
| 332 |
PJ_TEST_SUCCESS(rc=pjsip_udp_transport_start(endpt, NULL, NULL, 1, &tp), |
| 333 |
NULL, goto on_return); |
| 334 |
tsx_test[tsx_test_cnt].port = tp->local_name.port; |
| 335 |
tsx_test[tsx_test_cnt].tp_type = "udp"; |
| 336 |
tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_UDP; |
| 337 |
++tsx_test_cnt; |
| 338 |
|
| 339 |
#if PJ_HAS_TCP |
| 340 |
PJ_TEST_SUCCESS(rc=pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory), |
| 341 |
NULL, goto on_return); |
| 342 |
tsx_test[tsx_test_cnt].port = tpfactory->addr_name.port; |
| 343 |
tsx_test[tsx_test_cnt].tp_type = "tcp"; |
| 344 |
tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_TCP; |
| 345 |
++tsx_test_cnt; |
| 346 |
#endif |
| 347 |
|
| 348 |
pj_assert(tsx_test[0].type == PJSIP_TRANSPORT_LOOP_DGRAM); |
| 349 |
|
| 350 |
for (i = 0; i < tsx_test_cnt; ++i) { |
| 351 |
UT_ADD_TEST1(&test_app.ut_app, tsx_basic_test, (void*)(uintptr_t)i, 0); |
| 352 |
UT_ADD_TEST1(&test_app.ut_app, tsx_uac_test, (void*)(uintptr_t)i, 0); |
| 353 |
UT_ADD_TEST1(&test_app.ut_app, tsx_uas_test, (void*)(uintptr_t)i, 0); |
| 354 |
} |
| 355 |
#endif |
| 356 |
|
| 357 |
#if INCLUDE_LOOP_TEST |
| 358 |
UT_ADD_TEST(&test_app.ut_app, transport_loop_test, 0); |
| 359 |
#endif |
| 360 |
|
| 361 |
#if INCLUDE_UDP_TEST |
| 362 |
|
| 363 |
UT_ADD_TEST(&test_app.ut_app, transport_udp_test, 0); |
| 364 |
#endif |
| 365 |
|
| 366 |
#if INCLUDE_TCP_TEST |
| 367 |
UT_ADD_TEST(&test_app.ut_app, transport_tcp_test, 0); |
| 368 |
#endif |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
#if INCLUDE_REGC_TEST |
| 376 |
UT_ADD_TEST(&test_app.ut_app, regc_test, PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST); |
| 377 |
#endif |
| 378 |
|
| 379 |
#if INCLUDE_AUTH_ASYNC_TEST |
| 380 |
UT_ADD_TEST(&test_app.ut_app, auth_async_test, |
| 381 |
PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST); |
| 382 |
#endif |
| 383 |
|
| 384 |
#if INCLUDE_PJSUA_AUTH_TEST |
| 385 |
UT_ADD_TEST(&test_app.ut_app, pjsua_auth_test, |
| 386 |
PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST); |
| 387 |
#endif |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
#if INCLUDE_LOOP_TEST |
| 394 |
UT_ADD_TEST(&test_app.ut_app, transport_loop_resolve_error_test, |
| 395 |
PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST); |
| 396 |
#endif |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
#if INCLUDE_TSX_DESTROY_TEST |
| 402 |
UT_ADD_TEST(&test_app.ut_app, tsx_destroy_test, |
| 403 |
PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST); |
| 404 |
#endif |
| 405 |
|
| 406 |
if (ut_run_tests(&test_app.ut_app, "pjsip tests", argc, argv)) { |
| 407 |
rc = 99; |
| 408 |
} else { |
| 409 |
rc = 0; |
| 410 |
} |
| 411 |
|
| 412 |
ut_app_destroy(&test_app.ut_app); |
| 413 |
|
| 414 |
on_return: |
| 415 |
flush_events(500); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
pjsip_endpt_destroy(endpt); |
| 428 |
pj_caching_pool_destroy(&caching_pool); |
| 429 |
|
| 430 |
PJ_LOG(3,(THIS_FILE, " ")); |
| 431 |
|
| 432 |
pj_thread_get_stack_info(pj_thread_this(), &filename, &line); |
| 433 |
PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u", |
| 434 |
pj_thread_get_stack_max_usage(pj_thread_this()), |
| 435 |
filename, line)); |
| 436 |
if (rc == 0) |
| 437 |
PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!..")); |
| 438 |
else |
| 439 |
PJ_LOG(3,(THIS_FILE, "Test completed with error(s)")); |
| 440 |
|
| 441 |
report_ival("test-status", rc, "", "Overall test status/result (0==success)"); |
| 442 |
close_report(); |
| 443 |
return rc; |
| 444 |
} |
| 445 |
|