belle_http_tester.c 13.7 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    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
7
    the Free Software Foundation, either version 2 of the License, or
Simon Morlat's avatar
Simon Morlat committed
8 9 10 11 12 13 14 15 16 17 18 19 20
    (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/>.
*/

#include "belle-sip/belle-sip.h"
#include "belle_sip_tester.h"
21 22 23
#include <belle_sip_internal.h>

typedef struct http_counters{
24
	int response_headers_count;
25 26
	int response_count;
	int io_error_count;
jehan's avatar
jehan committed
27 28 29
	int two_hundred;
	int three_hundred;
	int four_hundred;
30 31
}http_counters_t;

Simon Morlat's avatar
Simon Morlat committed
32 33 34 35 36 37 38 39 40 41
static int wait_for(belle_sip_stack_t*s1,int* counter,int value,int timeout) {
	int retry=0;
#define SLEEP_TIME 100
	while (*counter!=value && retry++ <(timeout/SLEEP_TIME)) {
		if (s1) belle_sip_stack_sleep(s1,SLEEP_TIME);
	}
	if (*counter!=value) return FALSE;
	else return TRUE;
}

42

43 44 45
static void process_response(void *data, const belle_http_response_event_t *event){
	http_counters_t *counters=(http_counters_t*)data;
	counters->response_count++;
46
	BC_ASSERT_PTR_NOT_NULL(event->response);
47 48
	if (event->response){
		int code=belle_http_response_get_status_code(event->response);
49
		belle_sip_body_handler_t *body=belle_sip_message_get_body_handler(BELLE_SIP_MESSAGE(event->response));
Guillaume Beraudo's avatar
Guillaume Beraudo committed
50
		if (code>=200 && code <300)
jehan's avatar
jehan committed
51
			counters->two_hundred++;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
52
		else if (code>=300 && code <400)
jehan's avatar
jehan committed
53
			counters->three_hundred++;
jehan's avatar
jehan committed
54
		else if (code>=400 && code <500)
jehan's avatar
jehan committed
55
			counters->four_hundred++;
56
		BC_ASSERT_PTR_NOT_NULL(body);
57 58 59
	}
}

60
static void process_io_error(void *data, const belle_sip_io_error_event_t *event){
61 62 63 64
	http_counters_t *counters=(http_counters_t*)data;
	counters->io_error_count++;
}

65
static void process_auth_requested(void *data, belle_sip_auth_event_t *event){
Simon Morlat's avatar
Simon Morlat committed
66 67 68 69 70 71 72 73 74 75 76 77 78
	if (belle_sip_auth_event_get_mode(event)==BELLE_SIP_AUTH_MODE_TLS){
		belle_sip_certificates_chain_t* cert = belle_sip_certificates_chain_parse(belle_sip_tester_client_cert,strlen(belle_sip_tester_client_cert),BELLE_SIP_CERTIFICATE_RAW_FORMAT_PEM);
		belle_sip_signing_key_t* key = belle_sip_signing_key_parse(belle_sip_tester_private_key,strlen(belle_sip_tester_private_key),belle_sip_tester_private_key_passwd);
		belle_sip_auth_event_set_client_certificates_chain(event,cert);
		belle_sip_auth_event_set_signing_key(event,key);
		belle_sip_message("process_auth_requested requested for DN [%s]"
							,belle_sip_auth_event_get_distinguished_name(event));
	}
}

static belle_sip_stack_t *stack=NULL;
static belle_http_provider_t *prov=NULL;

79
static int http_before_all(void) {
Simon Morlat's avatar
Simon Morlat committed
80
	stack=belle_sip_stack_new(NULL);
81
	belle_sip_tester_set_dns_host_file(stack);
82

Simon Morlat's avatar
Simon Morlat committed
83
	prov=belle_sip_stack_create_http_provider(stack,"0.0.0.0");
84
	if (belle_sip_tester_get_root_ca_path() != NULL) {
85 86 87
		belle_tls_crypto_config_t *crypto_config=belle_tls_crypto_config_new();
		belle_tls_crypto_config_set_root_ca(crypto_config,belle_sip_tester_get_root_ca_path());
		belle_http_provider_set_tls_crypto_config(prov,crypto_config);
jehan's avatar
jehan committed
88
		belle_sip_object_unref(crypto_config);
89

90
	}
Simon Morlat's avatar
Simon Morlat committed
91 92 93
	return 0;
}

94
static int http_after_all(void) {
Simon Morlat's avatar
Simon Morlat committed
95 96 97
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
	return 0;
98 99
}

100
static int url_supported(const char *url) {
101
	if (url && strstr(url,"https://")==url && !belle_sip_stack_tls_available(stack)) {
102 103 104 105 106 107 108 109 110 111 112 113 114
		belle_sip_error("No TLS support, test skipped.");
		return -1;
	}
	return 0;
}
static int one_get(const char *url,http_counters_t* counters, int *counter){
	if (url_supported(url)==-1) {
		return -1;
	} else {
		belle_http_request_listener_callbacks_t cbs={0};
		belle_http_request_listener_t *l;
		belle_generic_uri_t *uri;
		belle_http_request_t *req;
115

116
		uri=belle_generic_uri_parse(url);
117

118 119 120 121 122 123 124 125 126 127
		req=belle_http_request_create("GET",
										uri,
										belle_sip_header_create("User-Agent","belle-sip/"PACKAGE_VERSION),
										NULL);
		cbs.process_response=process_response;
		cbs.process_io_error=process_io_error;
		cbs.process_auth_requested=process_auth_requested;
		l=belle_http_request_listener_create_from_callbacks(&cbs,counters);
		belle_http_provider_send_request(prov,req,l);
		wait_for(stack,counter,1,10000);
128

129 130 131
		belle_sip_object_unref(l);
		return 0;
	}
132 133
}

134
static void one_http_get(void){
jehan's avatar
jehan committed
135
	http_counters_t counters={0};
136
	if (one_get("http://smtp.linphone.org",&counters,&counters.response_count) == 0) {
137 138
		BC_ASSERT_EQUAL(counters.response_count,1,int,"%d");
		BC_ASSERT_EQUAL(counters.io_error_count,0,int,"%d");
139 140
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
141 142
}

143 144
static void http_get_empty_body(void){
	http_counters_t counters={0};
145
	if (one_get("http://smtp.linphone.org/marie_invalid",&counters,&counters.response_count) == 0) {
146 147
		BC_ASSERT_EQUAL(counters.response_count,1,int,"%d");
		BC_ASSERT_EQUAL(counters.io_error_count,0,int,"%d");
148 149
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
150 151
}

152 153
static void http_get_io_error(void){
	http_counters_t counters={0};
154 155 156 157 158
	if (one_get("http://blablabla.fail",&counters,&counters.io_error_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count,0,int,"%d");
		BC_ASSERT_EQUAL(counters.io_error_count,1,int,"%d");
	}
	}
159

160
static void one_https_get(void){
jehan's avatar
jehan committed
161
	http_counters_t counters={0};
162 163 164 165 166
	if (one_get("https://smtp.linphone.org",&counters,&counters.response_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count, 1, int, "%d");
		BC_ASSERT_EQUAL(counters.io_error_count, 0, int, "%d");
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
Simon Morlat's avatar
Simon Morlat committed
167 168
}

jehan's avatar
jehan committed
169 170 171 172 173 174 175 176 177
static void one_https_get_with_maddr(void){
	http_counters_t counters={0};
	if (one_get("https://blabla.linphone.org;maddr=94.23.19.176/",&counters,&counters.response_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count, 1, int, "%d");
		BC_ASSERT_EQUAL(counters.io_error_count, 0, int, "%d");
		BC_ASSERT_EQUAL(counters.four_hundred,1,int,"%d");
	}
}

178 179
static void https_get_long_body(void){
	http_counters_t counters={0};
180 181 182 183 184
	if (one_get("https://smtp.linphone.org/linphone.html",&counters, &counters.response_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count, 1, int, "%d");
		BC_ASSERT_EQUAL(counters.io_error_count, 0, int, "%d");
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
185 186
}

Simon Morlat's avatar
Simon Morlat committed
187
static void https_digest_get(void){
jehan's avatar
jehan committed
188
	http_counters_t counters={0};
189 190 191 192 193
	if (one_get("https://pauline:pouet@smtp.linphone.org/restricted",&counters,&counters.response_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count, 1, int, "%d");
		BC_ASSERT_EQUAL(counters.io_error_count, 0, int, "%d");
		BC_ASSERT_EQUAL(counters.three_hundred,1,int,"%d");
	}
Simon Morlat's avatar
Simon Morlat committed
194
}
jehan's avatar
jehan committed
195
#if 0
Simon Morlat's avatar
Simon Morlat committed
196 197
static void https_client_cert_connection(void){
	belle_tls_verify_policy_t *policy=belle_tls_verify_policy_new();
jehan's avatar
jehan committed
198
	http_counters_t counters={0};
Simon Morlat's avatar
Simon Morlat committed
199 200
	belle_tls_verify_policy_set_exceptions(policy,BELLE_TLS_VERIFY_ANY_REASON);/*ignore the server verification because we don't have a true certificate*/
	belle_http_provider_set_tls_verify_policy(prov,policy);
201 202 203
	if (one_get("https://sip2.linphone.org:5063",&counters) == 0) {
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
Simon Morlat's avatar
Simon Morlat committed
204 205
	belle_tls_verify_policy_set_exceptions(policy,0);
	belle_sip_object_unref(policy);
206
}
jehan's avatar
jehan committed
207
#endif
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

static void on_progress(belle_sip_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, size_t total){
	if (total!=0){
		double frac=100.0*(double)offset/(double)total;
		belle_sip_message("transfer %g %% done",frac);
	}else belle_sip_message("%i bytes transfered",(int)offset);
}

#define MULTIPART_BEGIN "somehash.jpg\r\n" \
			"--" MULTIPART_BOUNDARY "\r\n" \
			"Content-Disposition: form-data; name=\"userfile\"; filename=\"belle_http_sip_tester.jpg\"\r\n" \
			"Content-Type: application/octet-stream\r\n\r\n"
#define MULTIPART_END "\r\n--" MULTIPART_BOUNDARY "--\r\n"
const char *multipart_boudary=MULTIPART_BOUNDARY;

const int image_size=250000;

225
static int on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, uint8_t *buffer, size_t *size){
226 227
	size_t end_of_img=sizeof(MULTIPART_BEGIN)+image_size;
	if (offset==0){
228
		size_t partlen=sizeof(MULTIPART_BEGIN);
229
		BC_ASSERT_LOWER_STRICT((unsigned int)partlen,(unsigned int)*size,unsigned int,"%u");
230 231 232 233 234 235 236 237 238 239 240
		memcpy(buffer,MULTIPART_BEGIN,partlen);
		*size=partlen;
	}else if (offset<end_of_img){
		size_t i;
		size_t end=MIN(offset+*size, end_of_img);
		for(i=offset;i<end;++i){
			((char*)buffer)[i-offset]='a'+(i%26);
		}
		*size=i-offset;
	}else{
		*size=sizeof(MULTIPART_END);
241
		strncpy((char*)buffer,MULTIPART_END,*size);
242 243 244 245 246 247 248 249 250 251
	}
	return BELLE_SIP_CONTINUE;
}

static void https_post_long_body(void){
	belle_http_request_listener_callbacks_t cbs={0};
	belle_http_request_listener_t *l;
	belle_generic_uri_t *uri;
	belle_http_request_t *req;
	http_counters_t counters={0};
252 253
	belle_sip_user_body_handler_t *bh;
	char *content_type;
254
	const char *url="https://www.linphone.org:444/lft.php";
255 256 257
	if (url_supported(url)==-1) {
		return;
	}
258
	bh=belle_sip_user_body_handler_new(image_size+sizeof(MULTIPART_BEGIN)+sizeof(MULTIPART_END), on_progress, NULL, NULL, on_send_body, NULL, NULL);
259
	content_type=belle_sip_strdup_printf("multipart/form-data; boundary=%s",multipart_boudary);
260

261
	uri=belle_generic_uri_parse(url);
262

263 264 265 266 267 268 269 270 271 272 273 274
	req=belle_http_request_create("POST",
				uri,
				belle_sip_header_create("User-Agent","belle-sip/" PACKAGE_VERSION),
				belle_sip_header_create("Content-type",content_type),
				NULL);
	belle_sip_free(content_type);
	belle_sip_message_set_body_handler(BELLE_SIP_MESSAGE(req),BELLE_SIP_BODY_HANDLER(bh));
	cbs.process_response=process_response;
	cbs.process_io_error=process_io_error;
	cbs.process_auth_requested=process_auth_requested;
	l=belle_http_request_listener_create_from_callbacks(&cbs,&counters);
	belle_http_provider_send_request(prov,req,l);
275 276
	BC_ASSERT_TRUE(wait_for(stack,&counters.two_hundred,1,20000));

277 278 279
	belle_sip_object_unref(l);
}

280
static void on_recv_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, uint8_t *buffer, size_t size){
281 282 283 284 285 286 287 288
	FILE *file=(FILE*)data;
	if (file)
		fwrite(buffer,1,size,file);
}

static void process_response_headers(void *data, const belle_http_response_event_t *event){
	http_counters_t *counters=(http_counters_t*)data;
	counters->response_headers_count++;
289
	BC_ASSERT_PTR_NOT_NULL(event->response);
290 291 292 293 294 295
	if (event->response){
		/*we are receiving a response, set a specific body handler to acquire the response.
		 * if not done, belle-sip will create a memory body handler, the default*/
		FILE *file=belle_sip_object_data_get(BELLE_SIP_OBJECT(event->request),"file");
		belle_sip_message_set_body_handler(
			(belle_sip_message_t*)event->response,
296
			(belle_sip_body_handler_t*)belle_sip_user_body_handler_new(0,on_progress, NULL,on_recv_body,NULL, NULL,file)
297 298 299 300
		);
	}
}

301
static void http_get_long_user_body(void){
302 303 304 305 306 307 308 309 310
	belle_http_request_listener_callbacks_t cbs={0};
	belle_http_request_listener_t *l;
	belle_generic_uri_t *uri;
	belle_http_request_t *req;
	http_counters_t counters={0};
	const char *url="http://download-mirror.savannah.gnu.org/releases/linphone/belle-sip/belle-sip-1.3.0.tar.gz";
	belle_sip_body_handler_t *bh;
	belle_http_response_t *resp;
	FILE *outfile=fopen("download.tar.gz","w");
311

312
	uri=belle_generic_uri_parse(url);
313

314 315 316 317 318 319 320 321 322 323 324 325
	req=belle_http_request_create("GET",
				uri,
				belle_sip_header_create("User-Agent","belle-sip/" PACKAGE_VERSION),
				NULL);
	cbs.process_response_headers=process_response_headers;
	cbs.process_response=process_response;
	cbs.process_io_error=process_io_error;
	cbs.process_auth_requested=process_auth_requested;
	l=belle_http_request_listener_create_from_callbacks(&cbs,&counters);
	belle_sip_object_ref(req);
	belle_sip_object_data_set(BELLE_SIP_OBJECT(req),"file",outfile,NULL);
	belle_http_provider_send_request(prov,req,l);
326
	BC_ASSERT_TRUE(wait_for(stack,&counters.two_hundred,1,20000));
327
	BC_ASSERT_EQUAL(counters.response_headers_count,1,int,"%d");
328
	resp=belle_http_request_get_response(req);
329
	BC_ASSERT_PTR_NOT_NULL(resp);
330 331
	if (resp){
		bh=belle_sip_message_get_body_handler((belle_sip_message_t*)resp);
332
		BC_ASSERT_GREATER_STRICT((unsigned int)belle_sip_body_handler_get_size(bh),0,unsigned int,"%u");
333 334 335 336 337 338
	}
	belle_sip_object_unref(req);
	belle_sip_object_unref(l);
	if (outfile) fclose(outfile);
}

339 340 341 342 343 344 345
extern const char *test_http_proxy_addr;
extern int test_http_proxy_port;

static void one_https_get_with_proxy(void){
	http_counters_t counters={0};
	belle_sip_stack_set_http_proxy_host(stack, test_http_proxy_addr);
	belle_sip_stack_set_http_proxy_port(stack, test_http_proxy_port);
346

347 348 349 350 351 352 353 354 355 356 357
	if (one_get("https://smtp.linphone.org",&counters,&counters.response_count) == 0) {
		BC_ASSERT_EQUAL(counters.response_count, 1, int, "%d");
		BC_ASSERT_EQUAL(counters.io_error_count, 0, int, "%d");
		BC_ASSERT_EQUAL(counters.two_hundred,1,int,"%d");
	}
	belle_sip_stack_set_http_proxy_host(stack, NULL);
	belle_sip_stack_set_http_proxy_port(stack, 0);

}


358
test_t http_tests[] = {
359 360 361
	TEST_NO_TAG("One http GET", one_http_get),
	TEST_NO_TAG("http GET of empty body", http_get_empty_body),
	TEST_NO_TAG("One https GET", one_https_get),
jehan's avatar
jehan committed
362
	TEST_NO_TAG("One https GET with maddr", one_https_get_with_maddr),
363 364 365 366 367 368 369
	TEST_NO_TAG("One https GET with http proxy", one_https_get_with_proxy),
	TEST_NO_TAG("http request with io error", http_get_io_error),
	TEST_NO_TAG("https GET with long body", https_get_long_body),
	TEST_NO_TAG("https digest GET", https_digest_get),/*, FIXME, need a server for testing
	TEST_NO_TAG("https with client certificate", https_client_cert_connection),*/
	TEST_NO_TAG("https POST with long body", https_post_long_body),
	TEST_NO_TAG("http GET with long user body", http_get_long_user_body)
370 371
};

372
test_suite_t http_test_suite = {"HTTP stack", http_before_all, http_after_all, NULL,
Simon Morlat's avatar
Simon Morlat committed
373
								NULL, sizeof(http_tests) / sizeof(http_tests[0]), http_tests};