belle_sip_tester.c 8.7 KB
Newer Older
jehan's avatar
jehan committed
1 2
/*
	belle-sip - SIP (RFC3261) library.
3
	Copyright (C) 2010  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
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
jehan's avatar
jehan committed
17
*/
Simon Morlat's avatar
Simon Morlat committed
18 19 20
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Ghislain MARY's avatar
Ghislain MARY committed
21
#include "belle_sip_tester.h"
Simon Morlat's avatar
Simon Morlat committed
22

23
#include <belle-sip/utils.h>
24
#include <belle-sip/belle-sip.h>
jehan's avatar
jehan committed
25

26 27
#include "port.h"

28
extern const char *test_domain;
29
extern const char *auth_domain;
Ghislain MARY's avatar
Ghislain MARY committed
30

31
static char* all_leaks_buffer = NULL;
32
static const char *belle_sip_tester_root_ca_path = NULL;
33

34
static belle_sip_object_pool_t *pool;
35

36
static int leaked_objects_count;
37
const char *userhostsfile = NULL;
38

Simon Morlat's avatar
Simon Morlat committed
39
static int _belle_sip_tester_ipv6_available(void){
40
	struct addrinfo *ai=bctbx_ip_address_to_addrinfo(AF_INET6,SOCK_STREAM,"2a01:e00::2",53);
Simon Morlat's avatar
Simon Morlat committed
41 42 43 44 45 46
	if (ai){
		struct sockaddr_storage ss;
		struct addrinfo src;
		socklen_t slen=sizeof(ss);
		char localip[128];
		int port=0;
47
		belle_sip_get_src_addr_for(ai->ai_addr,(socklen_t)ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
Simon Morlat's avatar
Simon Morlat committed
48 49
		src.ai_addr=(struct sockaddr*) &ss;
		src.ai_addrlen=slen;
50
		bctbx_addrinfo_to_ip_address(&src,localip, sizeof(localip),&port);
51
		bctbx_freeaddrinfo(ai);
Simon Morlat's avatar
Simon Morlat committed
52 53 54 55 56 57 58 59 60 61 62
		return strcmp(localip,"::1")!=0;
	}
	return FALSE;
}

static int ipv6_available=0;

int belle_sip_tester_ipv6_available(void){
	return ipv6_available;
}

63 64 65 66 67 68 69 70 71
const char * belle_sip_tester_get_root_ca_path(void) {
	return belle_sip_tester_root_ca_path;
}

void belle_sip_tester_set_root_ca_path(const char *root_ca_path) {
	belle_sip_tester_root_ca_path = root_ca_path;
}


72
static void log_handler(int lev, const char *fmt, va_list args) {
73
#ifdef _WIN32
74 75
	/* We must use stdio to avoid log formatting (for autocompletion etc.) */
	vfprintf(lev == BELLE_SIP_LOG_ERROR ? stderr : stdout, fmt, args);
76 77 78 79
	fprintf(lev == BELLE_SIP_LOG_ERROR ? stderr : stdout, "\n");
#else
	va_list cap;
	va_copy(cap,args);
80
	vfprintf(lev == BELLE_SIP_LOG_ERROR ? stderr : stdout, fmt, cap);
81 82 83
	fprintf(lev == BELLE_SIP_LOG_ERROR ? stderr : stdout, "\n");
	va_end(cap);
#endif
84 85

	belle_sip_logv(BELLE_SIP_LOG_DOMAIN,lev, fmt, args);
86 87
}

88
int belle_sip_tester_set_log_file(const char *filename) {
89 90 91
	int res = 0;
	char *dir = bctbx_dirname(filename);
	char *base = bctbx_basename(filename);
92
	belle_sip_message("Redirecting traces to file [%s]", filename);
93
	bctbx_log_handler_t *filehandler = bctbx_create_file_log_handler(0, dir, base);
94
	if (filehandler == NULL) {
95 96 97
		res = -1;
		goto end;
	}
98
	bctbx_add_log_handler(filehandler);
99 100 101 102 103

end:
	bctbx_free(dir);
	bctbx_free(base);
	return res;
104 105
}

Nicolas Michon's avatar
Nicolas Michon committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
int silent_arg_func(const char *arg) {
	belle_sip_set_log_level(BELLE_SIP_LOG_FATAL);
	bctbx_set_log_level(BCTBX_LOG_DOMAIN, BCTBX_LOG_FATAL);
	return 0;
}

int verbose_arg_func(const char *arg) {
	belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
	bctbx_set_log_level(BCTBX_LOG_DOMAIN, BCTBX_LOG_DEBUG);
	return 0;
}

int logfile_arg_func(const char *arg) {
	bctbx_set_log_handler(NULL);/*remove default log handler*/
	if (belle_sip_tester_set_log_file(arg) < 0) return -2;
	return 0;
}

124
void belle_sip_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
125 126 127
	bc_tester_set_silent_func(silent_arg_func);
	bc_tester_set_verbose_func(verbose_arg_func);
	bc_tester_set_logfile_func(logfile_arg_func);
128
	if (ftester_printf == NULL) ftester_printf = log_handler;
129
	bc_tester_init(ftester_printf, BELLE_SIP_LOG_MESSAGE, BELLE_SIP_LOG_ERROR, "tester_hosts");
130
	belle_sip_init_sockets();
131
	belle_sip_object_enable_marshal_check(TRUE);
Simon Morlat's avatar
Simon Morlat committed
132
	ipv6_available=_belle_sip_tester_ipv6_available();
133 134
	bc_tester_add_suite(&cast_test_suite);
	bc_tester_add_suite(&sip_uri_test_suite);
jehan's avatar
jehan committed
135
	bc_tester_add_suite(&fast_sip_uri_test_suite);
jehan's avatar
jehan committed
136
	bc_tester_add_suite(&perf_sip_uri_test_suite);
137 138 139 140 141 142 143 144 145 146 147
	bc_tester_add_suite(&generic_uri_test_suite);
	bc_tester_add_suite(&headers_test_suite);
	bc_tester_add_suite(&core_test_suite);
	bc_tester_add_suite(&sdp_test_suite);
	bc_tester_add_suite(&resolver_test_suite);
	bc_tester_add_suite(&message_test_suite);
	bc_tester_add_suite(&authentication_helper_test_suite);
	bc_tester_add_suite(&register_test_suite);
	bc_tester_add_suite(&dialog_test_suite);
	bc_tester_add_suite(&refresher_test_suite);
	bc_tester_add_suite(&http_test_suite);
Simon Morlat's avatar
Simon Morlat committed
148
	bc_tester_add_suite(&object_test_suite);
Ghislain MARY's avatar
Ghislain MARY committed
149
}
150

Ghislain MARY's avatar
Ghislain MARY committed
151
void belle_sip_tester_uninit(void) {
152
	belle_sip_object_unref(pool);
153
	belle_sip_uninit_sockets();
154 155 156

	// show all leaks that happened during the test
	if (all_leaks_buffer) {
157
		bc_tester_printf(BELLE_SIP_LOG_MESSAGE, all_leaks_buffer);
158 159 160
		belle_sip_free(all_leaks_buffer);
	}

161
	bc_tester_uninit();
162 163
}

164
void belle_sip_tester_before_each(void) {
165 166 167 168
	belle_sip_object_enable_leak_detector(TRUE);
	leaked_objects_count = belle_sip_object_get_object_count();
}

169
void belle_sip_tester_after_each(void) {
170 171
	int leaked_objects = belle_sip_object_get_object_count() - leaked_objects_count;
	if (leaked_objects > 0) {
172 173 174
		char* format = belle_sip_strdup_printf("%d object%s leaked in suite [%s] test [%s], please fix that!",
										leaked_objects, leaked_objects>1?"s were":"was",
										bc_tester_current_suite_name(), bc_tester_current_test_name());
175
		belle_sip_object_dump_active_objects();
176
		belle_sip_object_flush_active_objects();
177
		bc_tester_printf(BELLE_SIP_LOG_MESSAGE, format);
178 179
		belle_sip_error("%s", format);

180
		all_leaks_buffer = all_leaks_buffer ? belle_sip_strcat_printf(all_leaks_buffer, "\n%s", format) : belle_sip_strdup_printf("\n%s", format);
181
	}
182

183 184 185 186 187 188 189 190 191 192 193
	// prevent any future leaks
	{
		const char **tags = bc_tester_current_test_tags();
		int leaks_expected =
			(tags && ((tags[0] && !strcmp(tags[0], "LeaksMemory")) || (tags[1] && !strcmp(tags[1], "LeaksMemory"))));
		// if the test is NOT marked as leaking memory and it actually is, we should make it fail
		if (!leaks_expected && leaked_objects > 0) {
			BC_FAIL("This test is leaking memory!");
			// and reciprocally
		} else if (leaks_expected && leaked_objects == 0) {
			BC_FAIL("This test is not leaking anymore, please remove LeaksMemory tag!");
194
		}
195 196
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
197

198 199 200 201
void belle_sip_tester_set_dns_host_file(belle_sip_stack_t *stack){
	if (userhostsfile){
		belle_sip_stack_set_dns_user_hosts_file(stack, userhostsfile);
	}else{
202
		char *default_hosts = bc_tester_res("tester_hosts");
203 204 205 206 207 208 209
		if (default_hosts){
			belle_sip_stack_set_dns_user_hosts_file(stack, default_hosts);
			bc_free(default_hosts);
		}
	}
}

210

211
#if !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE) && !(defined(BELLE_SIP_WINDOWS_PHONE) || defined(BELLE_SIP_WINDOWS_UNIVERSAL))
Ghislain MARY's avatar
Ghislain MARY committed
212

213
static const char* belle_sip_helper =
214 215
		"\t\t\t--domain <test sip domain>\n"
		"\t\t\t--auth-domain <test auth domain>\n"
216 217
		"\t\t\t--root-ca <root ca file path>\n"
		"\t\t\t--dns-hosts </etc/hosts -like file to used to override DNS names (default: tester_hosts)>\n";
218

jehan's avatar
jehan committed
219
int main (int argc, char *argv[]) {
220
	int i;
221
	int ret;
222
	const char *root_ca_path = NULL;
Simon Morlat's avatar
Simon Morlat committed
223
	const char *env_domain=getenv("TEST_DOMAIN");
224
	char *default_hosts = NULL;
225

226
	belle_sip_tester_init(NULL);
227

228
#ifndef _WIN32   /*this hack doesn't work for argv[0]="c:\blablab\"*/
229
	// this allows to launch liblinphone_tester from outside of tester directory
230
	if (strstr(argv[0], ".libs")) {
231 232
		int prefix_length = strstr(argv[0], ".libs") - argv[0] + 1;
		char *prefix = belle_sip_strdup_printf("%s%.*s", argv[0][0] == '/' ? "" : "./", prefix_length, argv[0]);
233
		// printf("Resource prefix set to %s\n", prefix);
234 235 236
		bc_tester_set_resource_dir_prefix(prefix);
		bc_tester_set_writable_dir_prefix(prefix);
		belle_sip_free(prefix);
237
	}
238
#endif
239

240
	if (env_domain) {
Simon Morlat's avatar
Simon Morlat committed
241
		test_domain=env_domain;
242
	}
Ghislain MARY's avatar
Ghislain MARY committed
243
	bctbx_init_logger(TRUE);
244 245 246

	for(i=1;i<argc;++i) {
		if (strcmp(argv[i],"--domain")==0){
247
			CHECK_ARG("--domain", ++i, argc);
248
			test_domain=argv[i];
249
		} else if (strcmp(argv[i],"--auth-domain")==0){
250 251
			CHECK_ARG("--auth-domain", ++i, argc);
			auth_domain=argv[i];
252 253 254
		} else if (strcmp(argv[i], "--root-ca") == 0) {
			CHECK_ARG("--root-ca", ++i, argc);
			root_ca_path = argv[i];
255
		} else if (strcmp(argv[i],"--dns-hosts")==0){
256 257
			CHECK_ARG("--dns-hosts", ++i, argc);
			userhostsfile=argv[i];
258
		} else {
259
			int ret = bc_tester_parse_args(argc, argv, i);
260
			if (ret>0) {
261 262 263
				i += ret - 1;
				continue;
			} else if (ret<0) {
264
				bc_tester_helper(argv[0], belle_sip_helper);
265
			}
266
			return ret;
267
		}
268
	}
269
	belle_sip_tester_set_root_ca_path(root_ca_path);
270
	pool=belle_sip_object_pool_push();
271

272
	ret = bc_tester_start(argv[0]);
273
	belle_sip_tester_uninit();
jehan's avatar
jehan committed
274
	bctbx_uninit_logger();
275
	if (default_hosts) bc_free(default_hosts);
276
	return ret;
jehan's avatar
jehan committed
277
}
278

279
#endif