43 char *val = getenv(env);
46 fprintf(stderr,
"Failed to start test: environment variable \"%s\" is not set!\n", env);
57 #define fst_getenv(env, default_value) \ 58 char *env = fst_getenv_default(#env, (char *)default_value, SWITCH_FALSE); 63 #define fst_getenv_required(env) \ 64 char *env = fst_getenv_default(#env, NULL, SWITCH_TRUE); 80 #ifdef SWITCH_TEST_BASE_DIR_OVERRIDE 83 #define SWITCH_TEST_BASE_DIR_OVERRIDE "." 92 #ifdef SWITCH_TEST_BASE_DIR_FOR_CONF 95 if (confdir[0] !=
'/') {
127 fprintf(stderr,
"%s", err);
133 fprintf(stderr,
"%s", err);
142 #define fst_session_park(session) \ 143 switch_ivr_park_session(session); \ 144 switch_channel_wait_for_flag(switch_core_session_get_channel(session), CF_PARK, SWITCH_TRUE, 10000, NULL); 149 #define fst_requires fct_req 154 #define fst_requires_module(modname) fct_req(switch_loadable_module_exists(modname) == SWITCH_STATUS_SUCCESS) 159 #define fst_check fct_chk 164 #define fst_check_int_equals fct_chk_eq_int 169 #define fst_check_string_equals fct_chk_eq_str 174 #define fst_check_string_not_equals fct_chk_neq_str 179 #define fst_check_string_starts_with fct_chk_startswith_str 184 #define fst_check_string_ends_with fct_chk_endswith_str 189 #define fst_check_string_has fct_chk_incl_str 194 #define fst_check_string_does_not_have fct_chk_excl_str 199 #define fst_time_mark() \ 200 fst_time_start = switch_time_now(); 205 #define fst_xcheck(expr, error_msg) \ 206 (fct_xchk(expr, "%s", error_msg)) 211 #define fst_fail(error_msg) \ 212 (fct_xchk(0, "%s", error_msg)) 217 #define fst_check_duration(duration_ms, precision_ms) \ 219 int actual_duration_ms = (int)((switch_time_now() - fst_time_start) / 1000); \ 221 abs((actual_duration_ms - duration_ms)) <= precision_ms, \ 222 "fst_check_duration: %d != %d +/- %d", \ 223 (actual_duration_ms), \ 232 #define fst_check_int_range(actual, expected, precision) \ 234 abs((actual - expected)) <= precision, \ 235 "fst_check_int_range: %d != %d +/- %d", \ 244 #define fst_check_double_range(actual, expected, precision) \ 246 fabs((actual - expected)) <= precision, \ 247 "fst_check_double_range: %f != %f +/- %f", \ 256 #define FST_BEGIN() \ 260 switch_time_t fst_time_start = 0; \ 261 switch_timer_t fst_timer = { 0 }; \ 262 switch_memory_pool_t *fst_pool = NULL; \ 263 int fst_timer_started = 0; \ 264 fst_getenv_default("FST_SUPPRESS_UNUSED_STATIC_WARNING", NULL, SWITCH_FALSE); \ 266 fst_init_core_and_modload(NULL, NULL, 0, 0); \ 273 if (fst_time_start) { \ 275 fst_time_start = 0; \ 284 #define FST_CORE_EX_BEGIN(confdir, flags) \ 288 switch_time_t fst_time_start = 0; \ 289 switch_timer_t fst_timer = { 0 }; \ 290 switch_memory_pool_t *fst_pool = NULL; \ 291 int fst_timer_started = 0; \ 292 fst_getenv_default("FST_SUPPRESS_UNUSED_STATIC_WARNING", NULL, SWITCH_FALSE); \ 293 if (fst_init_core_and_modload(confdir, confdir, 0, flags | SCF_LOG_DISABLE) == SWITCH_STATUS_SUCCESS) { \ 296 fprintf(stderr, "Failed to load FS core\n"); \ 305 #define FST_CORE_END() \ 306 switch_core_destroy(); \ 308 if (fst_time_start) { \ 310 fst_time_start = 0; \ 315 #define FST_CORE_BEGIN(confdir) FST_CORE_EX_BEGIN(confdir, 0) 316 #define FST_CORE_DB_BEGIN(confdir) FST_CORE_EX_BEGIN(confdir, SCF_USE_SQL) 321 #define FST_MINCORE_BEGIN(confdir) \ 325 switch_time_t fst_time_start = 0; \ 326 switch_timer_t fst_timer = { 0 }; \ 327 switch_memory_pool_t *fst_pool = NULL; \ 328 int fst_timer_started = 0; \ 329 fst_getenv_default("FST_SUPPRESS_UNUSED_STATIC_WARNING", NULL, SWITCH_FALSE); \ 330 if (fst_init_core_and_modload(confdir, NULL, 1, 0 | SCF_LOG_DISABLE) == SWITCH_STATUS_SUCCESS) { \ 333 fprintf(stderr, "Failed to load FS core\n"); \ 338 #define FST_MINCORE_END FST_CORE_END 346 #define FST_MODULE_BEGIN(modname,suite) \ 348 const char *fst_test_module = #modname; \ 349 if (fst_core && !zstr(fst_test_module)) { \ 351 switch_loadable_module_load_module((char *)"./mod", (char *)fst_test_module, SWITCH_TRUE, &err); \ 353 FCT_FIXTURE_SUITE_BGN(suite); 355 #define FST_MODULE_BEGIN(modname,suite) \ 357 const char *fst_test_module = #modname; \ 358 if (fst_core && !zstr(fst_test_module)) { \ 361 sprintf(path, "%s%s%s", SWITCH_TEST_BASE_DIR_OVERRIDE, SWITCH_PATH_SEPARATOR, "../.libs/"); \ 362 switch_loadable_module_load_module((char *)path, (char *)fst_test_module, SWITCH_TRUE, &err); \ 364 FCT_FIXTURE_SUITE_BGN(suite); 371 #define FST_MODULE_END() \ 372 FCT_FIXTURE_SUITE_END(); \ 373 if (!zstr(fst_test_module) && switch_loadable_module_exists(fst_test_module) == SWITCH_STATUS_SUCCESS) { \ 375 switch_loadable_module_unload_module((char *)"./mod", (char *)fst_test_module, SWITCH_FALSE, &err); \ 379 #define FST_MODULE_END() \ 380 FCT_FIXTURE_SUITE_END(); \ 381 if (!zstr(fst_test_module) && switch_loadable_module_exists(fst_test_module) == SWITCH_STATUS_SUCCESS) { \ 384 sprintf(path, "%s%s%s", SWITCH_TEST_BASE_DIR_OVERRIDE, SWITCH_PATH_SEPARATOR, "../.libs/"); \ 385 switch_loadable_module_unload_module((char*)path, (char *)fst_test_module, SWITCH_FALSE, &err); \ 394 #define FST_SUITE_BEGIN(suite) \ 395 const char *fst_test_module = NULL; \ 396 FCT_FIXTURE_SUITE_BGN(suite) 401 #define FST_SUITE_END FCT_FIXTURE_SUITE_END 407 #define FST_SETUP_BEGIN() \ 410 switch_core_new_memory_pool(&fst_pool); \ 411 if (fst_core > 1) { \ 412 fst_timer_started = (switch_core_timer_init(&fst_timer, "soft", 20, 160, fst_pool) == SWITCH_STATUS_SUCCESS); \ 419 #define FST_SETUP_END FCT_SETUP_END 425 #define FST_TEARDOWN_BEGIN() \ 428 if (fst_pool) switch_core_destroy_memory_pool(&fst_pool); \ 429 if (fst_core > 1) { \ 430 if (fst_timer_started) switch_core_timer_destroy(&fst_timer); \ 437 #define FST_TEARDOWN_END FCT_TEARDOWN_END 446 #define FST_TEST_BEGIN(name) \ 449 switch_log_level_t level = SWITCH_LOG_DEBUG; \ 450 switch_core_session_ctl(SCSC_LOGLEVEL, &level); \ 451 fst_requires(fst_pool != NULL); \ 452 if (fst_core > 1) { \ 453 fst_requires(fst_timer_started); \ 457 if (fst_test_module) { \ 458 fst_requires_module(fst_test_module); \ 461 #define FST_TEST_END \ 463 switch_log_level_t level = SWITCH_LOG_DISABLE; \ 464 switch_core_session_ctl(SCSC_LOGLEVEL, &level); \ 490 #define FST_SESSION_BEGIN_RATE_VIDEO(name, rate, video_codec) \ 494 switch_log_level_t level = SWITCH_LOG_DEBUG; \ 495 switch_core_session_ctl(SCSC_LOGLEVEL, &level); \ 496 fst_requires(fst_pool != NULL); \ 497 if (fst_core > 1) { \ 498 fst_requires(fst_timer_started); \ 504 switch_core_session_t *fst_session = NULL; \ 505 switch_event_t *fst_originate_vars = NULL; \ 506 switch_call_cause_t fst_cause = SWITCH_CAUSE_NORMAL_CLEARING; \ 507 fst_requires(fst_core); \ 508 if (fst_test_module) { \ 509 fst_requires_module(fst_test_module); \ 511 fst_requires_module("mod_loopback"); \ 512 fst_requires_module("mod_sndfile"); \ 513 fst_requires(switch_core_running()); \ 514 fst_requires(switch_event_create_plain(&fst_originate_vars, SWITCH_EVENT_CHANNEL_DATA) == SWITCH_STATUS_SUCCESS); \ 515 switch_event_add_header_string(fst_originate_vars, SWITCH_STACK_BOTTOM, "origination_caller_id_number", "+15551112222"); \ 516 switch_event_add_header(fst_originate_vars, SWITCH_STACK_BOTTOM, "rate", "%d", rate); \ 517 if (switch_ivr_originate(NULL, &fst_session, &fst_cause, "{null_video_codec=" video_codec "}null/+15553334444", 2, NULL, NULL, NULL, NULL, fst_originate_vars, SOF_NONE, NULL, NULL) == SWITCH_STATUS_SUCCESS && fst_session) { \ 518 switch_memory_pool_t *fst_session_pool = switch_core_session_get_pool(fst_session); \ 519 switch_channel_t *fst_channel = switch_core_session_get_channel(fst_session); \ 520 switch_channel_set_state(fst_channel, CS_SOFT_EXECUTE); \ 521 switch_channel_wait_for_state(fst_channel, NULL, CS_SOFT_EXECUTE); \ 522 switch_channel_set_variable(fst_channel, "send_silence_when_idle", "-1"); \ 523 switch_channel_set_variable(fst_channel, "RECORD_STEREO", "true"); \ 524 switch_ivr_record_session(fst_session, (char *)"/tmp/"#name".wav", 0, NULL); \ 525 switch_channel_set_variable(fst_channel, "RECORD_STEREO", NULL); \ 533 #define FST_SESSION_BEGIN(name) FST_SESSION_BEGIN_RATE(name, 8000) 534 #define FST_SESSION_BEGIN_RATE(name, rate) FST_SESSION_BEGIN_RATE_VIDEO(name, rate, "") 542 #define FST_SESSION_END() \ 545 if (switch_channel_ready(fst_channel)) { \ 546 switch_channel_hangup(fst_channel, SWITCH_CAUSE_NORMAL_CLEARING); \ 548 if (fst_originate_vars) { \ 549 switch_event_destroy(&fst_originate_vars); \ 551 if (fst_session_pool) { \ 552 fst_session_pool = NULL; \ 554 switch_core_session_rwunlock(fst_session); \ 556 switch_log_level_t level = SWITCH_LOG_DISABLE; \ 557 switch_core_session_ctl(SCSC_LOGLEVEL, &level); \ 559 switch_sleep(1000000); \ 581 #define fst_test_core_asr_open(recognizer) \ 583 char *fst_asr_result = NULL; \ 584 switch_asr_handle_t ah = { 0 }; \ 585 switch_asr_flag_t flags = SWITCH_ASR_FLAG_NONE; \ 586 fst_requires(fst_core > 1); \ 587 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Open recognizer: %s\n", recognizer); \ 589 fst_requires(switch_core_asr_open(&ah, recognizer, "L16", 8000, "", &flags, fst_pool) == SWITCH_STATUS_SUCCESS); \ 608 #define fst_test_core_asr(grammar, input_filename) \ 611 switch_status_t result; \ 612 switch_file_handle_t file_handle = { 0 }; \ 615 int got_result = 0; \ 616 fst_asr_result = NULL; \ 617 file_handle.channels = 1; \ 618 file_handle.native_rate = 8000; \ 619 fst_requires(fst_core > 1); \ 620 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Test recognizer: input = %s\n", input_filename); \ 621 fst_requires(switch_core_asr_load_grammar(&ah, grammar, "") == SWITCH_STATUS_SUCCESS); \ 622 fst_requires(switch_core_file_open(&file_handle, input_filename, file_handle.channels, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) == SWITCH_STATUS_SUCCESS); \ 623 buf = (uint8_t *)switch_core_alloc(fst_pool, sizeof(uint8_t) * 160 * sizeof(uint16_t) * file_handle.channels); \ 624 switch_core_timer_sync(&fst_timer); \ 625 while ((result = switch_core_file_read(&file_handle, buf, &len)) == SWITCH_STATUS_SUCCESS) { \ 626 fst_requires(switch_core_asr_feed(&ah, buf, len * sizeof(int16_t), &flags) == SWITCH_STATUS_SUCCESS); \ 627 switch_core_timer_next(&fst_timer); \ 628 if (switch_core_asr_check_results(&ah, &flags) == SWITCH_STATUS_SUCCESS) { \ 629 char *xmlstr = NULL; \ 630 switch_event_t *headers = NULL; \ 631 flags = SWITCH_ASR_FLAG_NONE; \ 633 fst_requires(switch_core_asr_check_results(&ah, &flags) == SWITCH_STATUS_SUCCESS); \ 634 result = switch_core_asr_get_results(&ah, &xmlstr, &flags); \ 635 if (result == SWITCH_STATUS_SUCCESS) { \ 637 switch_core_asr_get_result_headers(&ah, &headers, &flags); \ 639 switch_event_destroy(&headers); \ 641 fst_check(xmlstr != NULL); \ 642 if (xmlstr != NULL) { \ 643 fst_asr_result = switch_core_strdup(fst_pool, xmlstr);\ 645 switch_safe_free(xmlstr); \ 651 fst_check(got_result == 1); \ 652 fst_requires(switch_core_file_close(&file_handle) == SWITCH_STATUS_SUCCESS); \ 661 #define fst_test_core_asr_pause() \ 662 fst_requires(fst_core > 1); \ 663 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Pause recognizer\n"); \ 664 flags = SWITCH_ASR_FLAG_NONE; \ 665 fst_requires(switch_core_asr_pause(&ah) == SWITCH_STATUS_SUCCESS); 673 #define fst_test_core_asr_resume() \ 674 fst_requires(fst_core > 1); \ 675 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Resume recognizer\n"); \ 676 flags = SWITCH_ASR_FLAG_NONE; \ 677 fst_requires(switch_core_asr_resume(&ah) == SWITCH_STATUS_SUCCESS); 685 #define fst_test_core_asr_close() \ 686 fst_requires(fst_core > 1); \ 687 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Close recognizer\n"); \ 688 flags = SWITCH_ASR_FLAG_NONE; \ 689 fst_requires(switch_core_asr_close(&ah, &flags) == SWITCH_STATUS_SUCCESS); \ 702 #define fst_play_and_detect_speech_test_begin() \ 704 const char *fst_asr_result = NULL; \ 705 fst_requires(fst_core > 1); 726 #define fst_play_and_detect_speech_app_test(recognizer, grammar, prompt_filename, input_filename) \ 729 fst_requires(fst_core > 1); \ 730 fst_requires_module("mod_dptools"); \ 731 switch_channel_set_variable(fst_channel, "detect_speech_result", ""); \ 732 fst_requires(switch_ivr_displace_session(fst_session, input_filename, 0, "mrf") == SWITCH_STATUS_SUCCESS); \ 733 args = switch_core_session_sprintf(fst_session, "%s detect:%s %s", prompt_filename, recognizer, grammar); \ 734 fst_requires(switch_core_session_execute_application(fst_session, "play_and_detect_speech", args) == SWITCH_STATUS_SUCCESS); \ 735 fst_asr_result = switch_channel_get_variable(fst_channel, "detect_speech_result"); \ 736 fst_check(fst_asr_result != NULL); \ 757 #define fst_play_and_detect_speech_test(recognizer, grammar, prompt_filename, input_filename, input_args) \ 760 fst_asr_result = NULL; \ 761 fst_requires(fst_core > 1); \ 762 fst_requires(switch_ivr_displace_session(fst_session, input_filename, 0, "mrf") == SWITCH_STATUS_SUCCESS); \ 763 switch_status_t status = switch_ivr_play_and_detect_speech(fst_session, prompt_filename, recognizer, grammar, (char **)&fst_asr_result, 0, input_args); \ 764 fst_check(fst_asr_result != NULL); \ 770 #define fst_play_and_detect_speech_test_end() \ 780 #define fst_check_extension_apps(expected, extension) \ 782 fst_xcheck(extension != NULL, "Missing extension\n"); \ 785 switch_caller_application_t *cur_app = extension->applications; \ 786 for (i = 0; ; i += 2, cur_app = cur_app->next) { \ 787 int cur_app_num = i / 2 + 1; \ 788 if (!expected[i]) { \ 789 if (cur_app != NULL) { \ 790 fst_fail(switch_core_sprintf(fst_pool, "Unexpected application #%d \"%s\"\n", cur_app_num, cur_app->application_name)); \ 794 fst_xcheck(cur_app != NULL, switch_core_sprintf(fst_pool, "Extension application #%d \"%s\" is missing", cur_app_num, expected[i])); \ 798 fst_xcheck(cur_app->application_name && !strcmp(expected[i], cur_app->application_name), switch_core_sprintf(fst_pool, "Expected application #%d name is \"%s\", but is \"%s\"\n", cur_app_num, expected[i], cur_app->application_name)); \ 799 fst_xcheck(cur_app->application_data && !strcmp(expected[i + 1], cur_app->application_data), switch_core_sprintf(fst_pool, "Expected application #%d %s data is \"%s\", but is \"%s\"\n", cur_app_num, expected[i], expected[i + 1], cur_app->application_data)); \ 815 #define fst_sched_recv_dtmf(when, digits) \ 817 switch_status_t api_result; \ 818 switch_stream_handle_t stream = { 0 }; \ 819 SWITCH_STANDARD_STREAM(stream); \ 820 fst_requires(fst_core > 1); \ 821 fst_requires_module("mod_commands"); \ 822 api_result = switch_api_execute("sched_api", switch_core_session_sprintf(fst_session, "%s none uuid_recv_dtmf %s %s", when, switch_core_session_get_uuid(fst_session), digits), NULL, &stream); \ 823 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_INFO, "Injecting DTMF %s at %s\n", digits, when); \ 824 fst_requires(api_result == SWITCH_STATUS_SUCCESS); \ 825 switch_safe_free(stream.data); \ 828 #define fst_xml_start() \ 829 switch_stream_handle_t fst_xml_stream = { 0 }; \ 830 SWITCH_STANDARD_STREAM(fst_xml_stream); 834 #define fst_xml_open_tag(tag_name) \ 835 fst_xml_stream.write_function(&fst_xml_stream, "<%s", #tag_name); \ 838 #define fst_xml_attr(attr) \ 839 if (!zstr(attr)) fst_xml_stream.write_function(&fst_xml_stream, " %s=\"%s\"", #attr, attr); 841 #define fst_xml_close_tag(tag_name) \ 842 --fst_tag_children; \ 843 if (fst_tag_children > 0 || fst_tag_body) { \ 844 fst_xml_stream.write_function(&fst_xml_stream, "</%s>", #tag_name); \ 846 fst_xml_stream.write_function(&fst_xml_stream, "/>"); \ 850 #define fst_xml_body(body) \ 851 if (fst_tag_body) { \ 852 fst_xml_stream.write_function(&fst_xml_stream, "%s", body); \ 855 fst_xml_stream.write_function(&fst_xml_stream, ">%s", body); \ 858 #define fst_xml_end() \ 859 switch_xml_parse_str_dynamic((char *)fst_xml_stream.data, SWITCH_FALSE); 874 #define fst_parse_json_file(varname, file) \ 875 cJSON *varname = NULL; \ 880 int fd = open(file, O_RDONLY); \ 881 fst_requires(fd >= 0); \ 883 switch_zmalloc(buf, s.st_size + 1); \ 885 size = read(fd, buf, s.st_size); \ 886 fst_requires(size == s.st_size); \ 888 varname = cJSON_Parse(buf); \ 890 fst_requires(varname); \
switch_status_t switch_core_init(_In_ switch_core_flag_t flags, _In_ switch_bool_t console, _Out_ const char **err)
Initilize the core.
void switch_sleep(switch_interval_time_t t)
void switch_core_set_variable(_In_z_ const char *varname, _In_opt_z_ const char *value)
Add a global variable to the core.
void switch_core_set_globals(void)
Initiate Globals.
uint32_t switch_core_flag_t
#define SWITCH_TEST_BASE_DIR_OVERRIDE
static switch_status_t fst_init_core_and_modload(const char *confdir, const char *basedir, int minimal, switch_core_flag_t flags)
#define SWITCH_PATH_SEPARATOR
static char * fst_getenv_default(const char *env, char *default_value, switch_bool_t required)
switch_status_t switch_core_init_and_modload(_In_ switch_core_flag_t flags, _In_ switch_bool_t console, _Out_ const char **err)
Initilize the core and load modules.
switch_directories SWITCH_GLOBAL_dirs
switch_status_t
Common return values.
unsigned long switch_getpid(void)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)