liblinphone_tester.c 7.57 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

jehan's avatar
jehan committed
20
#include "linphonecore.h"
21
#include "private.h"
jehan's avatar
jehan committed
22
#include "liblinphone_tester.h"
23 24 25 26

#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
27
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
28

29 30 31
#ifdef HAVE_GTK
#include <gtk/gtk.h>
#endif
32

33 34 35 36 37
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif


Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
38 39
static FILE * log_file = NULL;

Yann Diorcet's avatar
Yann Diorcet committed
40
#ifdef ANDROID
41

42
#include <android/log.h>
43 44
#include <jni.h>
#define CALLBACK_BUFFER_SIZE  1024
45

46 47
static JNIEnv *current_env = NULL;
static jobject current_obj = 0;
48 49
static const char* LogDomain = "liblinphone_tester";

50 51
int main(int argc, char** argv);

Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
52
void liblinphone_android_log_handler(int prio, const char *fmt, va_list args) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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);
	}
}

Simon Morlat's avatar
Simon Morlat committed
72
static void liblinphone_android_ortp_log_handler(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args) {
73 74
	int prio;
	switch(lev){
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
75 76 77 78 79 80
		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;
81
	}
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
82
	liblinphone_android_log_handler(prio, fmt, args);
83
}
84 85 86

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

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

126 127 128 129 130 131 132
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();
}
133
#endif /* ANDROID */
Ghislain MARY's avatar
Ghislain MARY committed
134

Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
135
static void log_handler(int lev, const char *fmt, va_list args) {
Ghislain MARY's avatar
Ghislain MARY committed
136
#ifdef _WIN32
137 138 139 140 141
	vfprintf(lev == ORTP_ERROR ? stderr : stdout, fmt, args);
	fprintf(lev == ORTP_ERROR ? stderr : stdout, "\n");
#else
	va_list cap;
	va_copy(cap,args);
142 143
#ifdef ANDROID
	/* IMPORTANT: needed by liblinphone tester to retrieve suite list...*/
144
	cunit_android_trace_handler(lev == ORTP_ERROR, fmt, cap);
145
#else
146
	/* Otherwise, we must use stdio to avoid log formatting (for autocompletion etc.) */
147 148 149 150
	vfprintf(lev == ORTP_ERROR ? stderr : stdout, fmt, cap);
	fprintf(lev == ORTP_ERROR ? stderr : stdout, "\n");
#endif
	va_end(cap);
151
#endif
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
152
	if (log_file){
Simon Morlat's avatar
Simon Morlat committed
153
		ortp_logv_out(ORTP_LOG_DOMAIN, lev, fmt, args);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
154 155 156
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
157
void liblinphone_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
158
	if (! log_file) {
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
159
#if defined(ANDROID)
160
		linphone_core_set_log_handler(liblinphone_android_ortp_log_handler);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
161
#endif
162
	}
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
163

Ghislain MARY's avatar
Ghislain MARY committed
164
	if (ftester_printf == NULL) ftester_printf = log_handler;
165
	bc_tester_init(ftester_printf, ORTP_MESSAGE, ORTP_ERROR, "rcfiles");
166
	liblinphone_tester_add_suites();
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
167 168
}

169
int liblinphone_tester_set_log_file(const char *filename) {
170 171 172
	if (log_file) {
		fclose(log_file);
	}
173 174 175 176 177 178
	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);
179
	bctbx_set_log_file(log_file);
180 181 182 183
	ortp_set_log_file(log_file);
	return 0;
}

184 185 186 187 188 189 190 191 192 193 194

#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"
195 196 197
		"\t\t\t--disable-leak-detector\n"
		"\t\t\t--6\n"
		;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

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

#ifdef HAVE_GTK
	gtk_init(&argc, &argv);
#if !GLIB_CHECK_VERSION(2,32,0) // backward compatibility with Debian 6 and CentOS 6
	g_thread_init(NULL);
#endif
	gdk_threads_init();
#endif

	liblinphone_tester_init(NULL);

	for(i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--verbose") == 0) {
216
			bctbx_set_log_level(NULL, BCTBX_LOG_MESSAGE);
217 218
			linphone_core_set_log_level(ORTP_MESSAGE);
		} else if (strcmp(argv[i], "--silent") == 0) {
219
			bctbx_set_log_level(NULL, BCTBX_LOG_FATAL);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
			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);
237 238
		} else if (strcmp(argv[i],"--6")==0){
			liblinphonetester_ipv6=TRUE;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		} 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;
}


257
#endif