diff --git a/tester/Makefile.am b/tester/Makefile.am index 92023f78c..95beaf139 100644 --- a/tester/Makefile.am +++ b/tester/Makefile.am @@ -10,7 +10,7 @@ liblinphone_HEADERS = liblinphone_tester.h lib_LTLIBRARIES = liblinphonetester.la -liblinphonetester_la_SOURCES = \ +liblinphonetester_la_SOURCES = tester.c \ setup_tester.c \ register_tester.c \ message_tester.c \ @@ -25,14 +25,13 @@ liblinphonetester_la_SOURCES = \ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/coreapi AM_CFLAGS = $(STRICT_OPTIONS) -DIN_LINPHONE $(ORTP_CFLAGS) $(MEDIASTREAMER_CFLAGS) $(CUNIT_CFLAGS) $(BELLESIP_CFLAGS) $(LIBXML2_CFLAGS) -AM_OBJCFLAGS = $(AM_CFLAGS) - if BUILD_IOS #we build the liblinphonetester as a library, not as an executable, so that it is usable in XCode -liblinphonetester_la_SOURCES += liblinphonetester_ios.m liblinphonetester_ios.h -liblinphonetester_la_LDFLAGS = -framework CoreFoundation -framework AudioToolbox -framework CoreAudio -framework Foundation -framework QuartzCore -framework OpenGLES -framework UIKit -framework AVFoundation $(CUNIT_LIBS) +liblinphonetester_la_OBJCFLAGS = $(AM_CFLAGS) +liblinphonetester_la_SOURCES += liblinphonetester_ios.m liblinphonetester_ios.h +liblinphonetester_la_LDFLAGS = -framework CoreFoundation -framework AudioToolbox -framework CoreAudio -framework Foundation -framework QuartzCore -framework OpenGLES -framework UIKit -framework AVFoundation $(CUNIT_LIBS) else diff --git a/tester/liblinphone_tester.c b/tester/liblinphone_tester.c index 0f55ea131..f47a556b9 100644 --- a/tester/liblinphone_tester.c +++ b/tester/liblinphone_tester.c @@ -25,370 +25,18 @@ #include "CUnit/CUCurses.h" #endif -static LinphoneCore* configure_lc_from(LinphoneCoreVTable* v_table, const char* path, const char* file, void* user_data); - -static test_suite_t **test_suite = NULL; -static int nb_test_suites = 0; - - -#if HAVE_CU_CURSES -static unsigned char curses = 0; -#endif - -#if TARGET_OS_IPHONE -#include "liblinphonetester_ios.h" -#endif - - -const char* test_domain="sipopen.example.org"; -const char* auth_domain="sip.example.org"; -const char* test_username="liblinphone_tester"; -const char* test_password="secret"; -const char* test_route="sip2.linphone.org"; - -#if WINAPI_FAMILY_PHONE_APP -const char *liblinphone_tester_file_prefix="Assets"; -#elif defined(__QNX__) -const char *liblinphone_tester_file_prefix="./app/native/assets/"; -#else -const char *liblinphone_tester_file_prefix="."; -#endif - -const char *userhostsfile = "tester_hosts"; - #ifdef ANDROID + extern void AndroidPrintf(FILE *stream, const char *fmt, ...); #define fprintf(file, fmt, ...) AndroidPrintf(file, fmt, ##__VA_ARGS__) -#endif - -LinphoneAddress * create_linphone_address(const char * domain) { - LinphoneAddress *addr = linphone_address_new(NULL); - CU_ASSERT_PTR_NOT_NULL_FATAL(addr); - linphone_address_set_username(addr,test_username); - CU_ASSERT_STRING_EQUAL(test_username,linphone_address_get_username(addr)); - if (!domain) domain= test_route; - linphone_address_set_domain(addr,domain); - CU_ASSERT_STRING_EQUAL(domain,linphone_address_get_domain(addr)); - linphone_address_set_display_name(addr, NULL); - linphone_address_set_display_name(addr, "Mr Tester"); - CU_ASSERT_STRING_EQUAL("Mr Tester",linphone_address_get_display_name(addr)); - return addr; -} - -static void auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) { - stats* counters; - ms_message("Auth info requested for user id [%s] at realm [%s]\n" - ,username - ,realm); - counters = get_stats(lc); - counters->number_of_auth_info_requested++; -} - - - -void reset_counters( stats* counters) { - memset(counters,0,sizeof(stats)); -} - -static LinphoneCore* configure_lc_from(LinphoneCoreVTable* v_table, const char* path, const char* file, void* user_data) { - LinphoneCore* lc; - char filepath[256]={0}; - char ringpath[256]={0}; - char ringbackpath[256]={0}; - char rootcapath[256]={0}; - char dnsuserhostspath[256]={0}; - char nowebcampath[256]={0}; - - if (path==NULL) path="."; - - if (file){ - sprintf(filepath, "%s/%s", path, file); - CU_ASSERT_TRUE_FATAL(ortp_file_exist(filepath)==0); - } - - lc = linphone_core_new(v_table,NULL,*filepath!='\0' ? filepath : NULL, user_data); - - sal_enable_test_features(lc->sal,TRUE); - snprintf(rootcapath, sizeof(rootcapath), "%s/certificates/cn/cafile.pem", path); - linphone_core_set_root_ca(lc,rootcapath); - - sprintf(dnsuserhostspath, "%s/%s", path, userhostsfile); - sal_set_dns_user_hosts_file(lc->sal, dnsuserhostspath); - - snprintf(ringpath,sizeof(ringpath), "%s/sounds/oldphone.wav",path); - snprintf(ringbackpath,sizeof(ringbackpath), "%s/sounds/ringback.wav", path); - linphone_core_set_ring(lc, ringpath); - linphone_core_set_ringback(lc, ringbackpath); - - snprintf(nowebcampath, sizeof(nowebcampath), "%s/images/nowebcamCIF.jpg", path); - linphone_core_set_static_picture(lc,nowebcampath); - return lc; -} - - -bool_t wait_for_until(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value,int timout) { - MSList* lcs=NULL; - bool_t result; - if (lc_1) - lcs=ms_list_append(lcs,lc_1); - if (lc_2) - lcs=ms_list_append(lcs,lc_2); - result=wait_for_list(lcs,counter,value,timout); - ms_list_free(lcs); - return result; -} - -bool_t wait_for(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value) { - return wait_for_until(lc_1, lc_2,counter,value,3000); -} - -bool_t wait_for_list(MSList* lcs,int* counter,int value,int timeout_ms) { - int retry=0; - MSList* iterator; - while ((counter==NULL || *counternext) { - linphone_core_iterate((LinphoneCore*)(iterator->data)); - } - ms_usleep(100000); - } - if(counter && *counternext) { - linphone_core_enable_payload_type(lc,(PayloadType*)codecs_it->data,0); - } - if((pt = linphone_core_find_payload_type(lc,type,rate,1))) { - linphone_core_enable_payload_type(lc,pt, enable); - } - ms_list_free(codecs); -} - -static void enable_codec(LinphoneCore* lc,const char* type,int rate) { - set_codec_enable(lc,type,rate,TRUE); -} -stats * get_stats(LinphoneCore *lc){ - LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc); - return &manager->stat; -} - -LinphoneCoreManager *get_manager(LinphoneCore *lc){ - LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc); - return manager; -} - -LinphoneCoreManager* linphone_core_manager_new2(const char* rc_file, int check_for_proxies) { - LinphoneCoreManager* mgr= ms_new0(LinphoneCoreManager,1); - LinphoneProxyConfig* proxy; - char *rc_path = NULL; - int proxy_count; - int retry=0; - - mgr->v_table.registration_state_changed=registration_state_changed; - mgr->v_table.auth_info_requested=auth_info_requested; - mgr->v_table.call_state_changed=call_state_changed; - mgr->v_table.text_received=text_message_received; - mgr->v_table.message_received=message_received; - mgr->v_table.is_composing_received=is_composing_received; - mgr->v_table.new_subscription_requested=new_subscription_requested; - mgr->v_table.notify_presence_received=notify_presence_received; - mgr->v_table.transfer_state_changed=linphone_transfer_state_changed; - mgr->v_table.info_received=info_message_received; - mgr->v_table.subscription_state_changed=linphone_subscription_state_change; - mgr->v_table.notify_received=linphone_notify_received; - mgr->v_table.publish_state_changed=linphone_publish_state_changed; - mgr->v_table.configuring_status=linphone_configuration_status; - - reset_counters(&mgr->stat); - if (rc_file) rc_path = ms_strdup_printf("rcfiles/%s", rc_file); - mgr->lc=configure_lc_from(&mgr->v_table, liblinphone_tester_file_prefix, rc_path, mgr); - /*CU_ASSERT_EQUAL(ms_list_size(linphone_core_get_proxy_config_list(lc)),proxy_count);*/ - if (check_for_proxies && rc_file) /**/ - proxy_count=ms_list_size(linphone_core_get_proxy_config_list(mgr->lc)); - else - proxy_count=0; - - while (mgr->stat.number_of_LinphoneRegistrationOk2?(proxy_count-2)*10:0))) { - linphone_core_iterate(mgr->lc); - ms_usleep(100000); - } - CU_ASSERT_EQUAL(mgr->stat.number_of_LinphoneRegistrationOk,proxy_count); - enable_codec(mgr->lc,"PCMU",8000); - - linphone_core_get_default_proxy(mgr->lc,&proxy); - if (proxy) { - mgr->identity = linphone_address_new(linphone_proxy_config_get_identity(proxy)); - linphone_address_clean(mgr->identity); - } - if (rc_path) ms_free(rc_path); - return mgr; -} - -LinphoneCoreManager* linphone_core_manager_new( const char* rc_file) { - return linphone_core_manager_new2(rc_file, TRUE); -} - -void linphone_core_manager_stop(LinphoneCoreManager *mgr){ - if (mgr->lc) { - linphone_core_destroy(mgr->lc); - mgr->lc=NULL; - } -} - -void linphone_core_manager_destroy(LinphoneCoreManager* mgr) { - if (mgr->lc) linphone_core_destroy(mgr->lc); - if (mgr->identity) linphone_address_destroy(mgr->identity); - ms_free(mgr); -} - - -static void add_test_suite(test_suite_t *suite) { - if (test_suite == NULL) { - test_suite = (test_suite_t **)malloc(10 * sizeof(test_suite_t *)); - } - test_suite[nb_test_suites] = suite; - nb_test_suites++; - if ((nb_test_suites % 10) == 0) { - test_suite = (test_suite_t **)realloc(test_suite, (nb_test_suites + 10) * sizeof(test_suite_t *)); - } -} - -static int run_test_suite(test_suite_t *suite) { - int i; - - CU_pSuite pSuite = CU_add_suite(suite->name, suite->init_func, suite->cleanup_func); - - for (i = 0; i < suite->nb_tests; i++) { - if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) { - return CU_get_error(); - } - } - - return 0; -} - -static int test_suite_index(const char *suite_name) { - int i; - - for (i = 0; i < liblinphone_tester_nb_test_suites(); i++) { - if ((strcmp(suite_name, test_suite[i]->name) == 0) && (strlen(suite_name) == strlen(test_suite[i]->name))) { - return i; - } - } - - return -1; -} - -static int test_index(const char *suite_name, const char *test_name) { - int j,i; - - j = test_suite_index(suite_name); - if(j != -1) { - for (i = 0; i < test_suite[j]->nb_tests; i++) { - if ((strcmp(test_name, test_suite[j]->tests[i].name) == 0) && (strlen(test_name) == strlen(test_suite[j]->tests[i].name))) { - return i; - } - } - } - - return -1; -} - -int liblinphone_tester_nb_test_suites(void) { - return nb_test_suites; -} - -int liblinphone_tester_nb_tests(const char *suite_name) { - int i = test_suite_index(suite_name); - if (i < 0) return 0; - return test_suite[i]->nb_tests; -} - -const char * liblinphone_tester_test_suite_name(int suite_index) { - if (suite_index >= liblinphone_tester_nb_test_suites()) return NULL; - return test_suite[suite_index]->name; -} - -const char * liblinphone_tester_test_name(const char *suite_name, int test_index) { - int suite_index = test_suite_index(suite_name); - if ((suite_index < 0) || (suite_index >= liblinphone_tester_nb_test_suites())) return NULL; - if (test_index >= test_suite[suite_index]->nb_tests) return NULL; - return test_suite[suite_index]->tests[test_index].name; -} - -void liblinphone_tester_init(void) { - add_test_suite(&setup_test_suite); - add_test_suite(®ister_test_suite); - add_test_suite(&call_test_suite); - add_test_suite(&message_test_suite); - add_test_suite(&presence_test_suite); -#ifdef UPNP - add_test_suite(&upnp_test_suite); -#endif - add_test_suite(&stun_test_suite); - add_test_suite(&event_test_suite); - add_test_suite(&flexisip_test_suite); - add_test_suite(&remote_provisioning_test_suite); -} - -void liblinphone_tester_uninit(void) { - if (test_suite != NULL) { - free(test_suite); - test_suite = NULL; - nb_test_suites = 0; - } -} - -int liblinphone_tester_run_tests(const char *suite_name, const char *test_name) { - int i; - int ret; - /* initialize the CUnit test registry */ - if (CUE_SUCCESS != CU_initialize_registry()) - return CU_get_error(); - - for (i = 0; i < liblinphone_tester_nb_test_suites(); i++) { - run_test_suite(test_suite[i]); - } - - if (suite_name){ - CU_pSuite suite; - CU_basic_set_mode(CU_BRM_VERBOSE); - suite=CU_get_suite_by_name(suite_name, CU_get_registry()); - if (test_name) { - CU_pTest test=CU_get_test_by_name(test_name, suite); - CU_ErrorCode err= CU_basic_run_test(suite, test); - if (err != CUE_SUCCESS) ms_error("CU_basic_run_test error %d", err); - } else - CU_basic_run_suite(suite); - } else - { -#if HAVE_CU_CURSES - if (curses) { - /* Run tests using the CUnit curses interface */ - CU_curses_run_tests(); - } - else -#endif - { - /* Run all tests using the CUnit Basic interface */ - CU_basic_set_mode(CU_BRM_VERBOSE); - CU_basic_run_tests(); - } - } - - ret=CU_get_number_of_tests_failed()!=0; - CU_cleanup_registry(); - return ret; -} - -#ifdef ANDROID #include +#include +#include +#define CALLBACK_BUFFER_SIZE 1024 +static JNIEnv *current_env = NULL; +static jobject current_obj = 0; static const char* LogDomain = "liblinphone_tester"; void linphone_android_log_handler(int prio, const char *fmt, va_list args) { @@ -419,11 +67,49 @@ static void linphone_android_ortp_log_handler(OrtpLogLevel lev, const char *fmt, case ORTP_WARNING: prio = ANDROID_LOG_WARN; break; case ORTP_ERROR: prio = ANDROID_LOG_ERROR; break; case ORTP_FATAL: prio = ANDROID_LOG_FATAL; break; - default: prio = ANDROID_LOG_DEFAULT; break; + default: prio = ANDROID_LOG_DEFAULT; break; } linphone_android_log_handler(prio, fmt, args); } -#endif + +void cunit_android_trace_handler(int level, const char *fmt, va_list args) { + char buffer[CALLBACK_BUFFER_SIZE]; + JNIEnv *env = current_env; + if(env == NULL) return; + vsnprintf(buffer, CALLBACK_BUFFER_SIZE, fmt, args); + jstring javaString = (*env)->NewStringUTF(env, buffer); + jint javaLevel = level; + jclass cls = (*env)->GetObjectClass(env, current_obj); + jmethodID method = (*env)->GetMethodID(env, cls, "printLog", "(ILjava/lang/String;)V"); + (*env)->CallVoidMethod(env, current_obj, method, javaLevel, javaString); + (*env)->DeleteLocalRef(env,javaString); +} + +JNIEXPORT jint JNICALL Java_org_linphone_tester_Tester_run(JNIEnv *env, jobject obj, jobjectArray stringArray) { + int i, ret; + int argc = (*env)->GetArrayLength(env, stringArray); + char **argv = (char**) malloc(sizeof(char*) * argc); + + for (i=0; iGetObjectArrayElement(env, stringArray, i); + const char *rawString = (const char *) (*env)->GetStringUTFChars(env, string, 0); + argv[i] = strdup(rawString); + (*env)->ReleaseStringUTFChars(env, string, rawString); + } + current_env = env; + current_obj = obj; + CU_set_trace_handler(cunit_android_trace_handler); + ret = main(argc, argv); + current_env = NULL; + CU_set_trace_handler(NULL); + for (i=0; i= argc) { \ +#define CHECK_ARG(argument, index, argc) \ +if(index >= argc) { \ fprintf(stderr, "Missing argument for \"%s\"\n", argument); \ return -1; \ -} \ +} \ #ifndef WINAPI_FAMILY_PHONE_APP -#if TARGET_OS_IPHONE -int ios_tester_main(int argc, char * argv[]) -#else int main (int argc, char *argv[]) -#endif - { int i,j; int ret; @@ -475,8 +156,6 @@ int main (int argc, char *argv[]) linphone_core_set_log_handler(linphone_android_ortp_log_handler); #elif defined(__QNX__) linphone_core_set_log_handler(liblinphone_tester_qnx_log_handler); -#elif TARGET_OS_IPHONE - linphone_core_set_log_handler(liblinphone_tester_ios_log_handler); #else linphone_core_set_log_file(NULL); #endif @@ -531,12 +210,12 @@ int main (int argc, char *argv[]) // Check arguments if(suite_name != NULL) { - if(test_suite_index(suite_name) == -1) { + if(liblinphone_tester_test_suite_index(suite_name) == -1) { fprintf(stderr, "Suite \"%s\" not found\n", suite_name); return -1; } if(test_name != NULL) { - if(test_index(suite_name, test_name) == -1) { + if(liblinphone_tester_test_index(suite_name, test_name) == -1) { fprintf(stderr, "Test \"%s\" not found\n", test_name); return -1; } @@ -547,52 +226,5 @@ int main (int argc, char *argv[]) liblinphone_tester_uninit(); return ret; } -#endif - -#ifdef ANDROID -#include -#include -#define CALLBACK_BUFFER_SIZE 1024 -static JNIEnv *current_env = NULL; -static jobject current_obj = 0; - -void cunit_android_trace_handler(int level, const char *fmt, va_list args) { - char buffer[CALLBACK_BUFFER_SIZE]; - JNIEnv *env = current_env; - if(env == NULL) return; - vsnprintf(buffer, CALLBACK_BUFFER_SIZE, fmt, args); - jstring javaString = (*env)->NewStringUTF(env, buffer); - jint javaLevel = level; - jclass cls = (*env)->GetObjectClass(env, current_obj); - jmethodID method = (*env)->GetMethodID(env, cls, "printLog", "(ILjava/lang/String;)V"); - (*env)->CallVoidMethod(env, current_obj, method, javaLevel, javaString); - (*env)->DeleteLocalRef(env,javaString); -} - -JNIEXPORT - -JNIEXPORT jint JNICALL Java_org_linphone_tester_Tester_run(JNIEnv *env, jobject obj, jobjectArray stringArray) { - int i, ret; - int argc = (*env)->GetArrayLength(env, stringArray); - char **argv = (char**) malloc(sizeof(char*) * argc); - - for (i=0; iGetObjectArrayElement(env, stringArray, i); - const char *rawString = (const char *) (*env)->GetStringUTFChars(env, string, 0); - argv[i] = strdup(rawString); - (*env)->ReleaseStringUTFChars(env, string, rawString); - } - current_env = env; - current_obj = obj; - CU_set_trace_handler(cunit_android_trace_handler); - ret = main(argc, argv); - current_env = NULL; - CU_set_trace_handler(NULL); - for (i=0; i. + */ + +#include +#include "CUnit/Basic.h" +#include "linphonecore.h" +#include "private.h" +#include "liblinphone_tester.h" +#if HAVE_CU_CURSES +#include "CUnit/CUCurses.h" +#endif + +static LinphoneCore* configure_lc_from(LinphoneCoreVTable* v_table, const char* path, const char* file, void* user_data); + +static test_suite_t **test_suite = NULL; +static int nb_test_suites = 0; + + +#if HAVE_CU_CURSES +static unsigned char curses = 0; +#endif + +#if TARGET_OS_IPHONE +#include "liblinphonetester_ios.h" +#endif + + +const char* test_domain="sipopen.example.org"; +const char* auth_domain="sip.example.org"; +const char* test_username="liblinphone_tester"; +const char* test_password="secret"; +const char* test_route="sip2.linphone.org"; + +#if WINAPI_FAMILY_PHONE_APP +const char *liblinphone_tester_file_prefix="Assets"; +#elif defined(__QNX__) +const char *liblinphone_tester_file_prefix="./app/native/assets/"; +#else +const char *liblinphone_tester_file_prefix="."; +#endif + +const char *userhostsfile = "tester_hosts"; + +LinphoneAddress * create_linphone_address(const char * domain) { + LinphoneAddress *addr = linphone_address_new(NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(addr); + linphone_address_set_username(addr,test_username); + CU_ASSERT_STRING_EQUAL(test_username,linphone_address_get_username(addr)); + if (!domain) domain= test_route; + linphone_address_set_domain(addr,domain); + CU_ASSERT_STRING_EQUAL(domain,linphone_address_get_domain(addr)); + linphone_address_set_display_name(addr, NULL); + linphone_address_set_display_name(addr, "Mr Tester"); + CU_ASSERT_STRING_EQUAL("Mr Tester",linphone_address_get_display_name(addr)); + return addr; +} + +static void auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) { + stats* counters; + ms_message("Auth info requested for user id [%s] at realm [%s]\n" + ,username + ,realm); + counters = get_stats(lc); + counters->number_of_auth_info_requested++; +} + + + +void reset_counters( stats* counters) { + memset(counters,0,sizeof(stats)); +} + +static LinphoneCore* configure_lc_from(LinphoneCoreVTable* v_table, const char* path, const char* file, void* user_data) { + LinphoneCore* lc; + char filepath[256]={0}; + char ringpath[256]={0}; + char ringbackpath[256]={0}; + char rootcapath[256]={0}; + char dnsuserhostspath[256]={0}; + char nowebcampath[256]={0}; + + if (path==NULL) path="."; + + if (file){ + sprintf(filepath, "%s/%s", path, file); + CU_ASSERT_TRUE_FATAL(ortp_file_exist(filepath)==0); + } + + lc = linphone_core_new(v_table,NULL,*filepath!='\0' ? filepath : NULL, user_data); + + sal_enable_test_features(lc->sal,TRUE); + snprintf(rootcapath, sizeof(rootcapath), "%s/certificates/cn/cafile.pem", path); + linphone_core_set_root_ca(lc,rootcapath); + + sprintf(dnsuserhostspath, "%s/%s", path, userhostsfile); + sal_set_dns_user_hosts_file(lc->sal, dnsuserhostspath); + + snprintf(ringpath,sizeof(ringpath), "%s/sounds/oldphone.wav",path); + snprintf(ringbackpath,sizeof(ringbackpath), "%s/sounds/ringback.wav", path); + linphone_core_set_ring(lc, ringpath); + linphone_core_set_ringback(lc, ringbackpath); + + snprintf(nowebcampath, sizeof(nowebcampath), "%s/images/nowebcamCIF.jpg", path); + linphone_core_set_static_picture(lc,nowebcampath); + return lc; +} + + +bool_t wait_for_until(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value,int timout) { + MSList* lcs=NULL; + bool_t result; + if (lc_1) + lcs=ms_list_append(lcs,lc_1); + if (lc_2) + lcs=ms_list_append(lcs,lc_2); + result=wait_for_list(lcs,counter,value,timout); + ms_list_free(lcs); + return result; +} + +bool_t wait_for(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value) { + return wait_for_until(lc_1, lc_2,counter,value,3000); +} + +bool_t wait_for_list(MSList* lcs,int* counter,int value,int timeout_ms) { + int retry=0; + MSList* iterator; + while ((counter==NULL || *counternext) { + linphone_core_iterate((LinphoneCore*)(iterator->data)); + } + ms_usleep(100000); + } + if(counter && *counternext) { + linphone_core_enable_payload_type(lc,(PayloadType*)codecs_it->data,0); + } + if((pt = linphone_core_find_payload_type(lc,type,rate,1))) { + linphone_core_enable_payload_type(lc,pt, enable); + } + ms_list_free(codecs); +} + +static void enable_codec(LinphoneCore* lc,const char* type,int rate) { + set_codec_enable(lc,type,rate,TRUE); +} +stats * get_stats(LinphoneCore *lc){ + LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc); + return &manager->stat; +} + +LinphoneCoreManager *get_manager(LinphoneCore *lc){ + LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc); + return manager; +} + +LinphoneCoreManager* linphone_core_manager_new2(const char* rc_file, int check_for_proxies) { + LinphoneCoreManager* mgr= ms_new0(LinphoneCoreManager,1); + LinphoneProxyConfig* proxy; + char *rc_path = NULL; + int proxy_count; + int retry=0; + + mgr->v_table.registration_state_changed=registration_state_changed; + mgr->v_table.auth_info_requested=auth_info_requested; + mgr->v_table.call_state_changed=call_state_changed; + mgr->v_table.text_received=text_message_received; + mgr->v_table.message_received=message_received; + mgr->v_table.is_composing_received=is_composing_received; + mgr->v_table.new_subscription_requested=new_subscription_requested; + mgr->v_table.notify_presence_received=notify_presence_received; + mgr->v_table.transfer_state_changed=linphone_transfer_state_changed; + mgr->v_table.info_received=info_message_received; + mgr->v_table.subscription_state_changed=linphone_subscription_state_change; + mgr->v_table.notify_received=linphone_notify_received; + mgr->v_table.publish_state_changed=linphone_publish_state_changed; + mgr->v_table.configuring_status=linphone_configuration_status; + + reset_counters(&mgr->stat); + if (rc_file) rc_path = ms_strdup_printf("rcfiles/%s", rc_file); + mgr->lc=configure_lc_from(&mgr->v_table, liblinphone_tester_file_prefix, rc_path, mgr); + /*CU_ASSERT_EQUAL(ms_list_size(linphone_core_get_proxy_config_list(lc)),proxy_count);*/ + if (check_for_proxies && rc_file) /**/ + proxy_count=ms_list_size(linphone_core_get_proxy_config_list(mgr->lc)); + else + proxy_count=0; + + while (mgr->stat.number_of_LinphoneRegistrationOk2?(proxy_count-2)*10:0))) { + linphone_core_iterate(mgr->lc); + ms_usleep(100000); + } + CU_ASSERT_EQUAL(mgr->stat.number_of_LinphoneRegistrationOk,proxy_count); + enable_codec(mgr->lc,"PCMU",8000); + + linphone_core_get_default_proxy(mgr->lc,&proxy); + if (proxy) { + mgr->identity = linphone_address_new(linphone_proxy_config_get_identity(proxy)); + linphone_address_clean(mgr->identity); + } + if (rc_path) ms_free(rc_path); + return mgr; +} + +LinphoneCoreManager* linphone_core_manager_new( const char* rc_file) { + return linphone_core_manager_new2(rc_file, TRUE); +} + +void linphone_core_manager_stop(LinphoneCoreManager *mgr){ + if (mgr->lc) { + linphone_core_destroy(mgr->lc); + mgr->lc=NULL; + } +} + +void linphone_core_manager_destroy(LinphoneCoreManager* mgr) { + if (mgr->lc) linphone_core_destroy(mgr->lc); + if (mgr->identity) linphone_address_destroy(mgr->identity); + ms_free(mgr); +} + + +static void add_test_suite(test_suite_t *suite) { + if (test_suite == NULL) { + test_suite = (test_suite_t **)malloc(10 * sizeof(test_suite_t *)); + } + test_suite[nb_test_suites] = suite; + nb_test_suites++; + if ((nb_test_suites % 10) == 0) { + test_suite = (test_suite_t **)realloc(test_suite, (nb_test_suites + 10) * sizeof(test_suite_t *)); + } +} + +static int run_test_suite(test_suite_t *suite) { + int i; + + CU_pSuite pSuite = CU_add_suite(suite->name, suite->init_func, suite->cleanup_func); + + for (i = 0; i < suite->nb_tests; i++) { + if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) { + return CU_get_error(); + } + } + + return 0; +} + +int liblinphone_tester_test_suite_index(const char *suite_name) { + int i; + + for (i = 0; i < liblinphone_tester_nb_test_suites(); i++) { + if ((strcmp(suite_name, test_suite[i]->name) == 0) && (strlen(suite_name) == strlen(test_suite[i]->name))) { + return i; + } + } + + return -1; +} + +int liblinphone_tester_test_index(const char *suite_name, const char *test_name) { + int j,i; + + j = liblinphone_tester_test_suite_index(suite_name); + if(j != -1) { + for (i = 0; i < test_suite[j]->nb_tests; i++) { + if ((strcmp(test_name, test_suite[j]->tests[i].name) == 0) && (strlen(test_name) == strlen(test_suite[j]->tests[i].name))) { + return i; + } + } + } + + return -1; +} + +int liblinphone_tester_nb_test_suites(void) { + return nb_test_suites; +} + +int liblinphone_tester_nb_tests(const char *suite_name) { + int i = liblinphone_tester_test_suite_index(suite_name); + if (i < 0) return 0; + return test_suite[i]->nb_tests; +} + +const char * liblinphone_tester_test_suite_name(int suite_index) { + if (suite_index >= liblinphone_tester_nb_test_suites()) return NULL; + return test_suite[suite_index]->name; +} + +const char * liblinphone_tester_test_name(const char *suite_name, int test_index) { + int suite_index = liblinphone_tester_test_suite_index(suite_name); + if ((suite_index < 0) || (suite_index >= liblinphone_tester_nb_test_suites())) return NULL; + if (test_index >= test_suite[suite_index]->nb_tests) return NULL; + return test_suite[suite_index]->tests[test_index].name; +} + +void liblinphone_tester_init(void) { + add_test_suite(&setup_test_suite); + add_test_suite(®ister_test_suite); + add_test_suite(&call_test_suite); + add_test_suite(&message_test_suite); + add_test_suite(&presence_test_suite); +#ifdef UPNP + add_test_suite(&upnp_test_suite); +#endif + add_test_suite(&stun_test_suite); + add_test_suite(&event_test_suite); + add_test_suite(&flexisip_test_suite); + add_test_suite(&remote_provisioning_test_suite); +} + +void liblinphone_tester_uninit(void) { + if (test_suite != NULL) { + free(test_suite); + test_suite = NULL; + nb_test_suites = 0; + } +} + +int liblinphone_tester_run_tests(const char *suite_name, const char *test_name) { + int i; + int ret; + /* initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) + return CU_get_error(); + + for (i = 0; i < liblinphone_tester_nb_test_suites(); i++) { + run_test_suite(test_suite[i]); + } + + if (suite_name){ + CU_pSuite suite; + CU_basic_set_mode(CU_BRM_VERBOSE); + suite=CU_get_suite_by_name(suite_name, CU_get_registry()); + if (test_name) { + CU_pTest test=CU_get_test_by_name(test_name, suite); + CU_ErrorCode err= CU_basic_run_test(suite, test); + if (err != CUE_SUCCESS) ms_error("CU_basic_run_test error %d", err); + } else + CU_basic_run_suite(suite); + } else + { +#if HAVE_CU_CURSES + if (curses) { + /* Run tests using the CUnit curses interface */ + CU_curses_run_tests(); + } + else +#endif + { + /* Run all tests using the CUnit Basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + } + } + + ret=CU_get_number_of_tests_failed()!=0; + CU_cleanup_registry(); + return ret; +} +