belle_sip_tester.c 7.63 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>
Simon Morlat's avatar
Simon Morlat committed
24
#include <belle-sip/belle-sip.h>
jehan's avatar
jehan committed
25

26 27
#include "port.h"

Simon Morlat's avatar
Simon Morlat committed
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;
Simon Morlat's avatar
Simon Morlat committed
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 40 41 42 43 44 45 46 47 48 49 50
static int _belle_sip_tester_ipv6_available(void){
	struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_INET6,"2a01:e00::2",53);
	if (ai){
		struct sockaddr_storage ss;
		struct addrinfo src;
		socklen_t slen=sizeof(ss);
		char localip[128];
		int port=0;
		belle_sip_get_src_addr_for(ai->ai_addr,ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
		src.ai_addr=(struct sockaddr*) &ss;
		src.ai_addrlen=slen;
		belle_sip_addrinfo_to_ip(&src,localip, sizeof(localip),&port);
51
		belle_sip_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);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
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(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, "certificates");
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 97 98 99 100 101 102 103 104 105 106 107
	bc_tester_add_suite(&cast_test_suite);
	bc_tester_add_suite(&sip_uri_test_suite);
	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
108
}
109

Ghislain MARY's avatar
Ghislain MARY committed
110
void belle_sip_tester_uninit(void) {
Simon Morlat's avatar
Simon Morlat committed
111
	belle_sip_object_unref(pool);
112
	belle_sip_uninit_sockets();
113 114 115

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

120
	bc_tester_uninit();
121 122
}

123 124 125 126 127
void belle_sip_tester_before_each() {
	belle_sip_object_enable_leak_detector(TRUE);
	leaked_objects_count = belle_sip_object_get_object_count();
}

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

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

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
int belle_sip_tester_set_log_file(const char *filename) {
	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;
	}
	belle_sip_message("Redirecting traces to file [%s]", filename);
	belle_sip_set_log_file(log_file);
	return 0;
}


Ghislain MARY's avatar
Ghislain MARY committed
174 175
#if !defined(ANDROID) && !defined(TARGET_OS_IPHONE) && !(defined(BELLE_SIP_WINDOWS_PHONE) || defined(BELLE_SIP_WINDOWS_UNIVERSAL))

176
static const char* belle_sip_helper =
Ghislain MARY's avatar
Ghislain MARY committed
177
		"\t\t\t--verbose\n"
178
		"\t\t\t--silent\n"
179
		"\t\t\t--log-file <output log file path>\n"
Ghislain MARY's avatar
Ghislain MARY committed
180 181
		"\t\t\t--domain <test sip domain>\n"
		"\t\t\t--auth-domain <test auth domain>\n"
182
		"\t\t\t--root-ca <root ca file path>\n";
Ghislain MARY's avatar
Ghislain MARY committed
183

jehan's avatar
jehan committed
184
int main (int argc, char *argv[]) {
Simon Morlat's avatar
Simon Morlat committed
185
	int i;
186
	int ret;
187
	const char *root_ca_path = NULL;
Simon Morlat's avatar
Simon Morlat committed
188
	const char *env_domain=getenv("TEST_DOMAIN");
jehan's avatar
jehan committed
189

190
	belle_sip_tester_init(NULL);
191

192
#ifndef _WIN32   /*this hack doesn't work for argv[0]="c:\blablab\"*/
193
	// this allows to launch liblinphone_tester from outside of tester directory
194
	if (strstr(argv[0], ".libs")) {
195 196
		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]);
197
		// printf("Resource prefix set to %s\n", prefix);
198 199 200
		bc_tester_set_resource_dir_prefix(prefix);
		bc_tester_set_writable_dir_prefix(prefix);
		belle_sip_free(prefix);
201
	}
202
#endif
203

204
	if (env_domain) {
Simon Morlat's avatar
Simon Morlat committed
205
		test_domain=env_domain;
206
	}
207

Simon Morlat's avatar
Simon Morlat committed
208
	for(i=1;i<argc;++i){
209
		if (strcmp(argv[i],"--verbose")==0){
Simon Morlat's avatar
Simon Morlat committed
210
			belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
211 212
		} else if (strcmp(argv[i],"--silent")==0){
			belle_sip_set_log_level(BELLE_SIP_LOG_FATAL);
213 214
		} else if (strcmp(argv[i],"--log-file")==0){
			CHECK_ARG("--log-file", ++i, argc);
215
			if (belle_sip_tester_set_log_file(argv[i]) < 0) return -2;
216
		} else if (strcmp(argv[i],"--domain")==0){
Ghislain MARY's avatar
Ghislain MARY committed
217
			CHECK_ARG("--domain", ++i, argc);
Simon Morlat's avatar
Simon Morlat committed
218
			test_domain=argv[i];
219
		}else if (strcmp(argv[i],"--auth-domain")==0){
Ghislain MARY's avatar
Ghislain MARY committed
220 221
			CHECK_ARG("--auth-domain", ++i, argc);
			auth_domain=argv[i];
222 223 224
		} else if (strcmp(argv[i], "--root-ca") == 0) {
			CHECK_ARG("--root-ca", ++i, argc);
			root_ca_path = argv[i];
225
		}else {
226
			int ret = bc_tester_parse_args(argc, argv, i);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
227
			if (ret>0) {
228 229 230
				i += ret - 1;
				continue;
			} else if (ret<0) {
231
				bc_tester_helper(argv[0], belle_sip_helper);
232
			}
233
			return ret;
Ghislain MARY's avatar
Ghislain MARY committed
234
		}
Simon Morlat's avatar
Simon Morlat committed
235
	}
236
	belle_sip_tester_set_root_ca_path(root_ca_path);
237
	pool=belle_sip_object_pool_push();
238

239
	ret = bc_tester_start(argv[0]);
240 241
	belle_sip_tester_uninit();
	return ret;
jehan's avatar
jehan committed
242
}
243

244
#endif