liblinphone_tester.c 8.03 KB
Newer Older
jehan's avatar
jehan committed
1
/*
2 3
 liblinphone_tester - liblinphone test suite
 Copyright (C) 2013  Belledonne Communications SARL
jehan's avatar
jehan committed
4

5 6 7 8
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 2 of the License, or
 (at your option) any later version.
jehan's avatar
jehan committed
9

10 11 12 13
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
jehan's avatar
jehan committed
14

15 16 17
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Simon Morlat's avatar
Simon Morlat committed
18

19

20
#include "linphone/core.h"
jehan's avatar
jehan committed
21
#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
22
#include "tester_utils.h"
23

24 25
static FILE * log_file = NULL;

26
#ifdef __ANDROID__
27

28
#include <android/log.h>
29 30
#include <jni.h>
#define CALLBACK_BUFFER_SIZE  1024
31

32 33
static JNIEnv *current_env = NULL;
static jobject current_obj = 0;
34 35
static const char* LogDomain = "liblinphone_tester";

36 37
int main(int argc, char** argv);

38
void liblinphone_android_log_handler(int prio, const char *fmt, va_list args) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	char str[4096];
	char *current;
	char *next;

	vsnprintf(str, sizeof(str) - 1, fmt, args);
	str[sizeof(str) - 1] = '\0';
	if (strlen(str) < 512) {
		__android_log_write(prio, LogDomain, str);
	} else {
		current = str;
		while ((next = strchr(current, '\n')) != NULL) {
			*next = '\0';
			__android_log_write(prio, LogDomain, current);
			current = next + 1;
		}
		__android_log_write(prio, LogDomain, current);
	}
}

58
static void liblinphone_android_ortp_log_handler(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args) {
59 60
	int prio;
	switch(lev){
61 62 63 64 65 66
		case ORTP_DEBUG:	prio = ANDROID_LOG_DEBUG;	break;
		case ORTP_MESSAGE:	prio = ANDROID_LOG_INFO;	break;
		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;
67
	}
68
	liblinphone_android_log_handler(prio, fmt, args);
69
}
70

71 72 73 74 75 76 77 78 79 80 81 82 83
static void liblinphone_android_bctbx_log_handler(const char *domain, BctbxLogLevel lev, const char *fmt, va_list args) {
	int prio;
	switch(lev){
		case BCTBX_LOG_DEBUG:	prio = ANDROID_LOG_DEBUG;	break;
		case BCTBX_LOG_MESSAGE:	prio = ANDROID_LOG_INFO;	break;
		case BCTBX_LOG_WARNING:	prio = ANDROID_LOG_WARN;	break;
		case BCTBX_LOG_ERROR:	prio = ANDROID_LOG_ERROR;	break;
		case BCTBX_LOG_FATAL:	prio = ANDROID_LOG_FATAL;	break;
		default:			prio = ANDROID_LOG_DEFAULT;	break;
	}
	liblinphone_android_log_handler(prio, fmt, args);
}

84
void bcunit_android_trace_handler(int level, const char *fmt, va_list args) {
85
	char buffer[CALLBACK_BUFFER_SIZE];
86 87 88 89
	jstring javaString;
	jclass cls;
	jmethodID method;
	jint javaLevel = level;
90 91 92
	JNIEnv *env = current_env;
	if(env == NULL) return;
	vsnprintf(buffer, CALLBACK_BUFFER_SIZE, fmt, args);
93 94 95
	javaString = (*env)->NewStringUTF(env, buffer);
	cls = (*env)->GetObjectClass(env, current_obj);
	method = (*env)->GetMethodID(env, cls, "printLog", "(ILjava/lang/String;)V");
96 97
	(*env)->CallVoidMethod(env, current_obj, method, javaLevel, javaString);
	(*env)->DeleteLocalRef(env,javaString);
98
	(*env)->DeleteLocalRef(env,cls);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

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; i<argc; i++) {
		jstring string = (jstring) (*env)->GetObjectArrayElement(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;
114
	bc_set_trace_handler(bcunit_android_trace_handler);
115 116
	ret = main(argc, argv);
	current_env = NULL;
117
	bc_set_trace_handler(NULL);
118 119 120 121 122 123 124
	for (i=0; i<argc; i++) {
		free(argv[i]);
	}
	free(argv);
	return ret;
}

125 126 127 128 129 130 131
JNIEXPORT void JNICALL Java_org_linphone_tester_Tester_keepAccounts(JNIEnv *env, jclass c, jboolean keep) {
	liblinphone_tester_keep_accounts((int)keep);
}

JNIEXPORT void JNICALL Java_org_linphone_tester_Tester_clearAccounts(JNIEnv *env, jclass c) {
	liblinphone_tester_clear_accounts();
}
132
#endif /* __ANDROID__ */
133

134
static void log_handler(int lev, const char *fmt, va_list args) {
135
#ifdef _WIN32
136 137 138 139 140
	vfprintf(lev == ORTP_ERROR ? stderr : stdout, fmt, args);
	fprintf(lev == ORTP_ERROR ? stderr : stdout, "\n");
#else
	va_list cap;
	va_copy(cap,args);
141
#ifdef __ANDROID__
142
	/* IMPORTANT: needed by liblinphone tester to retrieve suite list...*/
143
	bcunit_android_trace_handler(lev == ORTP_ERROR, fmt, cap);
144
#else
145
	/* Otherwise, we must use stdio to avoid log formatting (for autocompletion etc.) */
146 147 148 149
	vfprintf(lev == ORTP_ERROR ? stderr : stdout, fmt, cap);
	fprintf(lev == ORTP_ERROR ? stderr : stdout, "\n");
#endif
	va_end(cap);
150
#endif
151
	bctbx_logv(ORTP_LOG_DOMAIN, lev, fmt, args);
152 153
}

154
void liblinphone_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
155
	bctbx_init_logger(FALSE);
156
	if (! log_file) {
157
#if defined(__ANDROID__)
158
		linphone_core_set_log_handler(liblinphone_android_ortp_log_handler);
159
		bctbx_set_log_handler(liblinphone_android_bctbx_log_handler);
160
#endif
161
	}
162

163
	if (ftester_printf == NULL) ftester_printf = log_handler;
164
	bc_tester_init(ftester_printf, ORTP_MESSAGE, ORTP_ERROR, "rcfiles");
165
	liblinphone_tester_add_suites();
166 167
}

168
int liblinphone_tester_set_log_file(const char *filename) {
169 170 171
	if (log_file) {
		fclose(log_file);
	}
172 173 174 175 176 177
	log_file = fopen(filename, "w");
	if (!log_file) {
		ms_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
		return -1;
	}
	ms_message("Redirecting traces to file [%s]", filename);
Benjamin REIS's avatar
Benjamin REIS committed
178
	linphone_core_set_log_file(log_file);
179 180 181
	return 0;
}

182 183 184 185 186 187 188 189 190 191 192

#if !TARGET_OS_IPHONE && !(defined(LINPHONE_WINDOWS_PHONE) || defined(LINPHONE_WINDOWS_UNIVERSAL))

static const char* liblinphone_helper =
		"\t\t\t--verbose\n"
		"\t\t\t--silent\n"
		"\t\t\t--log-file <output log file path>\n"
		"\t\t\t--domain <test sip domain>\n"
		"\t\t\t--auth-domain <test auth domain>\n"
		"\t\t\t--dns-hosts </etc/hosts -like file to used to override DNS names (default: tester_hosts)>\n"
		"\t\t\t--keep-recorded-files\n"
193
		"\t\t\t--disable-leak-detector\n"
194
		"\t\t\t--disable-tls-support\n"
195 196
		"\t\t\t--no-ipv6 (turn off IPv6 in LinphoneCore, tests requiring IPv6 will be skipped)\n"
		"\t\t\t--show-account-manager-logs (show temporary test account creation logs)\n"
197
		;
198 199 200 201 202 203 204

int main (int argc, char *argv[])
{
	int i;
	int ret;

	liblinphone_tester_init(NULL);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
205
	linphone_core_set_log_level(ORTP_ERROR);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	for(i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--verbose") == 0) {
			linphone_core_set_log_level(ORTP_MESSAGE);
		} else if (strcmp(argv[i], "--silent") == 0) {
			linphone_core_set_log_level(ORTP_FATAL);
		} else if (strcmp(argv[i],"--log-file")==0){
			CHECK_ARG("--log-file", ++i, argc);
			if (liblinphone_tester_set_log_file(argv[i]) < 0) return -2;
		} else if (strcmp(argv[i],"--domain")==0){
			CHECK_ARG("--domain", ++i, argc);
			test_domain=argv[i];
		} else if (strcmp(argv[i],"--auth-domain")==0){
			CHECK_ARG("--auth-domain", ++i, argc);
			auth_domain=argv[i];
		}else if (strcmp(argv[i],"--dns-hosts")==0){
			CHECK_ARG("--dns-hosts", ++i, argc);
			userhostsfile=argv[i];
		} else if (strcmp(argv[i],"--keep-recorded-files")==0){
			liblinphone_tester_keep_recorded_files(TRUE);
		} else if (strcmp(argv[i],"--disable-leak-detector")==0){
			liblinphone_tester_disable_leak_detector(TRUE);
228 229
		} else if (strcmp(argv[i],"--disable-tls-support")==0){
			liblinphone_tester_tls_support_disabled = TRUE;
230 231 232 233
		} else if (strcmp(argv[i],"--no-ipv6")==0){
			liblinphonetester_ipv6 = FALSE;
		} else if (strcmp(argv[i],"--show-account-manager-logs")==0){
			liblinphonetester_show_account_manager_logs=TRUE;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		} else {
			int bret = bc_tester_parse_args(argc, argv, i);
			if (bret>0) {
				i += bret - 1;
				continue;
			} else if (bret<0) {
				bc_tester_helper(argv[0], liblinphone_helper);
			}
			return bret;
		}
	}

	ret = bc_tester_start(argv[0]);
	liblinphone_tester_uninit();
	return ret;
}


252
#endif