belle_sip_tester.c 8.73 KB
Newer Older
jehan's avatar
jehan committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (c) 2012-2019 Belledonne Communications SARL.
 *
 * This file is part of belle-sip.
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * 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
19 20 21
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Ghislain MARY's avatar
Ghislain MARY committed
22
#include "belle_sip_tester.h"
Simon Morlat's avatar
Simon Morlat committed
23

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

27 28
#include "port.h"

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

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

35
static belle_sip_object_pool_t *pool;
36

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

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

static int ipv6_available=0;

int belle_sip_tester_ipv6_available(void){
	return ipv6_available;
}

64 65 66 67 68 69 70 71 72
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;
}


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

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

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

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

Nicolas Michon's avatar
Nicolas Michon committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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;
}

125
void belle_sip_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
126 127 128
	bc_tester_set_silent_func(silent_arg_func);
	bc_tester_set_verbose_func(verbose_arg_func);
	bc_tester_set_logfile_func(logfile_arg_func);
129
	if (ftester_printf == NULL) ftester_printf = log_handler;
130
	bc_tester_init(ftester_printf, BELLE_SIP_LOG_MESSAGE, BELLE_SIP_LOG_ERROR, "tester_hosts");
131
	belle_sip_init_sockets();
132
	belle_sip_object_enable_marshal_check(TRUE);
Simon Morlat's avatar
Simon Morlat committed
133
	ipv6_available=_belle_sip_tester_ipv6_available();
134 135
	bc_tester_add_suite(&cast_test_suite);
	bc_tester_add_suite(&sip_uri_test_suite);
jehan's avatar
jehan committed
136
	bc_tester_add_suite(&fast_sip_uri_test_suite);
jehan's avatar
jehan committed
137
	bc_tester_add_suite(&perf_sip_uri_test_suite);
138 139 140 141 142 143 144 145 146 147 148
	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
149
	bc_tester_add_suite(&object_test_suite);
Ghislain MARY's avatar
Ghislain MARY committed
150
}
151

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

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

162
	bc_tester_uninit();
163 164
}

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

170
void belle_sip_tester_after_each(void) {
171 172
	int leaked_objects = belle_sip_object_get_object_count() - leaked_objects_count;
	if (leaked_objects > 0) {
173 174 175
		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());
176
		belle_sip_object_dump_active_objects();
177
		belle_sip_object_flush_active_objects();
178
		bc_tester_printf(BELLE_SIP_LOG_MESSAGE, format);
179 180
		belle_sip_error("%s", format);

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

184 185 186 187 188 189 190 191 192 193 194
	// 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!");
195
		}
196 197
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
198

199 200 201 202
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{
203
		char *default_hosts = bc_tester_res("tester_hosts");
204 205 206 207 208 209 210
		if (default_hosts){
			belle_sip_stack_set_dns_user_hosts_file(stack, default_hosts);
			bc_free(default_hosts);
		}
	}
}

211

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

214
static const char* belle_sip_helper =
215 216
		"\t\t\t--domain <test sip domain>\n"
		"\t\t\t--auth-domain <test auth domain>\n"
217 218
		"\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";
219

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

227
	belle_sip_tester_init(NULL);
228

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

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

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

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

280
#endif