belle_sip_tester.c 8.45 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
static FILE * log_file = 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
	if (log_file){
86
		belle_sip_logv(BELLE_SIP_LOG_DOMAIN,lev, fmt, args);
87 88 89
	}
}

90 91
void belle_sip_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
	if (ftester_printf == NULL) ftester_printf = log_handler;
92
	bc_tester_init(ftester_printf, BELLE_SIP_LOG_MESSAGE, BELLE_SIP_LOG_ERROR, NULL);
93
	belle_sip_init_sockets();
94
	belle_sip_object_enable_marshal_check(TRUE);
Simon Morlat's avatar
Simon Morlat committed
95
	ipv6_available=_belle_sip_tester_ipv6_available();
96 97
	bc_tester_add_suite(&cast_test_suite);
	bc_tester_add_suite(&sip_uri_test_suite);
jehan's avatar
jehan committed
98
	bc_tester_add_suite(&fast_sip_uri_test_suite);
jehan's avatar
jehan committed
99
	bc_tester_add_suite(&perf_sip_uri_test_suite);
100 101 102 103 104 105 106 107 108 109 110
	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);
Ghislain MARY's avatar
Ghislain MARY committed
111
}
112

Ghislain MARY's avatar
Ghislain MARY committed
113
void belle_sip_tester_uninit(void) {
114
	belle_sip_object_unref(pool);
115
	belle_sip_uninit_sockets();
116 117 118

	// show all leaks that happened during the test
	if (all_leaks_buffer) {
119
		bc_tester_printf(BELLE_SIP_LOG_MESSAGE, all_leaks_buffer);
120 121 122
		belle_sip_free(all_leaks_buffer);
	}

123
	bc_tester_uninit();
124 125
}

126
void belle_sip_tester_before_each(void) {
127 128 129 130
	belle_sip_object_enable_leak_detector(TRUE);
	leaked_objects_count = belle_sip_object_get_object_count();
}

131
void belle_sip_tester_after_each(void) {
132 133
	int leaked_objects = belle_sip_object_get_object_count() - leaked_objects_count;
	if (leaked_objects > 0) {
134 135 136
		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());
137
		belle_sip_object_dump_active_objects();
138
		belle_sip_object_flush_active_objects();
139
		bc_tester_printf(BELLE_SIP_LOG_MESSAGE, format);
140 141
		belle_sip_error("%s", format);

142
		all_leaks_buffer = all_leaks_buffer ? belle_sip_strcat_printf(all_leaks_buffer, "\n%s", format) : belle_sip_strdup_printf("\n%s", format);
143
	}
144

145 146 147 148 149 150 151 152 153 154 155
	// 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!");
156
		}
157 158
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
159

160
int belle_sip_tester_set_log_file(const char *filename) {
Ghislain MARY's avatar
Ghislain MARY committed
161
	bctbx_log_handler_t *filehandler;
Benjamin REIS's avatar
Benjamin REIS committed
162 163
	char* dir;
	char* base;
164 165 166 167 168 169 170 171
	if (log_file) {
		fclose(log_file);
	}
	log_file = fopen(filename, "w");
	if (!log_file) {
		belle_sip_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
		return -1;
	}
Ghislain MARY's avatar
Ghislain MARY committed
172 173
	dir = bctbx_dirname(filename);
	base = bctbx_basename(filename);
174
	belle_sip_message("Redirecting traces to file [%s]", filename);
Ghislain MARY's avatar
Ghislain MARY committed
175
	filehandler = bctbx_create_file_log_handler(0, dir, base, log_file);
Benjamin REIS's avatar
Benjamin REIS committed
176
	bctbx_add_log_handler(filehandler);
Ghislain MARY's avatar
Ghislain MARY committed
177 178
	if (dir) bctbx_free(dir);
	if (base) bctbx_free(base);
179 180 181 182
	return 0;
}


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

185
static const char* belle_sip_helper =
186
		"\t\t\t--verbose\n"
187
		"\t\t\t--silent\n"
188
		"\t\t\t--log-file <output log file path>\n"
189 190
		"\t\t\t--domain <test sip domain>\n"
		"\t\t\t--auth-domain <test auth domain>\n"
191 192
		"\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";
193

jehan's avatar
jehan committed
194
int main (int argc, char *argv[]) {
195
	int i;
196
	int ret;
197
	const char *root_ca_path = NULL;
Simon Morlat's avatar
Simon Morlat committed
198
	const char *env_domain=getenv("TEST_DOMAIN");
jehan's avatar
jehan committed
199

200
	belle_sip_tester_init(NULL);
201

202
#ifndef _WIN32   /*this hack doesn't work for argv[0]="c:\blablab\"*/
203
	// this allows to launch liblinphone_tester from outside of tester directory
204
	if (strstr(argv[0], ".libs")) {
205 206
		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]);
207
		// printf("Resource prefix set to %s\n", prefix);
208 209 210
		bc_tester_set_resource_dir_prefix(prefix);
		bc_tester_set_writable_dir_prefix(prefix);
		belle_sip_free(prefix);
211
	}
212
#endif
213

214
	if (env_domain) {
Simon Morlat's avatar
Simon Morlat committed
215
		test_domain=env_domain;
216
	}
Ghislain MARY's avatar
Ghislain MARY committed
217
	bctbx_init_logger(TRUE);
jehan's avatar
jehan committed
218
	
219
	for(i=1;i<argc;++i){
220
		if (strcmp(argv[i],"--verbose")==0){
221
			belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
222
			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
223 224
		} else if (strcmp(argv[i],"--silent")==0){
			belle_sip_set_log_level(BELLE_SIP_LOG_FATAL);
225
			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_FATAL);
226 227
		} else if (strcmp(argv[i],"--log-file")==0){
			CHECK_ARG("--log-file", ++i, argc);
228
			bctbx_set_log_handler(NULL);/*remove default log handler*/
229
			if (belle_sip_tester_set_log_file(argv[i]) < 0) return -2;
230
		} else if (strcmp(argv[i],"--domain")==0){
231
			CHECK_ARG("--domain", ++i, argc);
232
			test_domain=argv[i];
233
		}else if (strcmp(argv[i],"--auth-domain")==0){
234 235
			CHECK_ARG("--auth-domain", ++i, argc);
			auth_domain=argv[i];
236 237 238
		} else if (strcmp(argv[i], "--root-ca") == 0) {
			CHECK_ARG("--root-ca", ++i, argc);
			root_ca_path = argv[i];
239 240 241
		}else if (strcmp(argv[i],"--dns-hosts")==0){
			CHECK_ARG("--dns-hosts", ++i, argc);
			userhostsfile=argv[i];
242
		}else {
243
			int ret = bc_tester_parse_args(argc, argv, i);
244
			if (ret>0) {
245 246 247
				i += ret - 1;
				continue;
			} else if (ret<0) {
248
				bc_tester_helper(argv[0], belle_sip_helper);
249
			}
250
			return ret;
251
		}
252
	}
253
	belle_sip_tester_set_root_ca_path(root_ca_path);
254
	pool=belle_sip_object_pool_push();
255

256
	ret = bc_tester_start(argv[0]);
257
	belle_sip_tester_uninit();
jehan's avatar
jehan committed
258
	bctbx_uninit_logger();
259
	return ret;
jehan's avatar
jehan committed
260
}
261

262
#endif