belle_sip_tester.c 8.18 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 38
static int leaked_objects_count;

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
	if (log_file){
85
		belle_sip_logv(BELLE_SIP_LOG_DOMAIN,lev, fmt, args);
86 87 88
	}
}

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

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

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

122
	bc_tester_uninit();
123 124
}

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

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

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

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

159
int belle_sip_tester_set_log_file(const char *filename) {
Ghislain MARY's avatar
Ghislain MARY committed
160
	bctbx_log_handler_t *filehandler;
Benjamin REIS's avatar
Benjamin REIS committed
161 162
	char* dir;
	char* base;
163 164 165 166 167 168 169 170
	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
171 172
	dir = bctbx_dirname(filename);
	base = bctbx_basename(filename);
173
	belle_sip_message("Redirecting traces to file [%s]", filename);
Ghislain MARY's avatar
Ghislain MARY committed
174
	filehandler = bctbx_create_file_log_handler(0, dir, base, log_file);
Benjamin REIS's avatar
Benjamin REIS committed
175
	bctbx_add_log_handler(filehandler);
Ghislain MARY's avatar
Ghislain MARY 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
		"\t\t\t--root-ca <root ca file path>\n";
192

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

199
	belle_sip_tester_init(NULL);
200

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

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

251
	ret = bc_tester_start(argv[0]);
252
	belle_sip_tester_uninit();
jehan's avatar
jehan committed
253
	bctbx_uninit_logger();
254
	return ret;
jehan's avatar
jehan committed
255
}
256

257
#endif