belle_http_tester.c 13.7 KB
Newer Older
Simon Morlat's avatar
Simon Morlat 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

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

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

Simon Morlat's avatar
Simon Morlat committed
33 34 35 36 37 38 39 40 41 42
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;
}

43

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

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

66
static void process_auth_requested(void *data, belle_sip_auth_event_t *event){
Simon Morlat's avatar
Simon Morlat committed
67 68 69 70 71 72 73 74 75 76 77 78 79
	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;

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

Simon Morlat's avatar
Simon Morlat committed
84
	prov=belle_sip_stack_create_http_provider(stack,"0.0.0.0");
85
	if (belle_sip_tester_get_root_ca_path() != NULL) {
86 87 88
		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
89
		belle_sip_object_unref(crypto_config);
90

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

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

101
static int url_supported(const char *url) {
102
	if (url && strstr(url,"https://")==url && !belle_sip_stack_tls_available(stack)) {
103 104 105 106 107 108 109 110 111 112 113 114 115
		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;
116

117
		uri=belle_generic_uri_parse(url);
118

119 120 121 122 123 124 125 126 127 128
		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);
129

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

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

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

153 154
static void http_get_io_error(void){
	http_counters_t counters={0};
155 156 157 158 159
	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");
	}
	}
160

161
static void one_https_get(void){
jehan's avatar
jehan committed
162
	http_counters_t counters={0};
163 164 165 166 167
	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
168 169
}

jehan's avatar
jehan committed
170 171 172 173 174 175 176 177 178
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");
	}
}

179 180
static void https_get_long_body(void){
	http_counters_t counters={0};
181 182 183 184 185
	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");
	}
186 187
}

Simon Morlat's avatar
Simon Morlat committed
188
static void https_digest_get(void){
jehan's avatar
jehan committed
189
	http_counters_t counters={0};
190 191 192 193 194
	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
195
}
jehan's avatar
jehan committed
196
#if 0
Simon Morlat's avatar
Simon Morlat committed
197 198
static void https_client_cert_connection(void){
	belle_tls_verify_policy_t *policy=belle_tls_verify_policy_new();
jehan's avatar
jehan committed
199
	http_counters_t counters={0};
Simon Morlat's avatar
Simon Morlat committed
200 201
	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);
202 203 204
	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
205 206
	belle_tls_verify_policy_set_exceptions(policy,0);
	belle_sip_object_unref(policy);
207
}
jehan's avatar
jehan committed
208
#endif
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

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;

226
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){
227 228
	size_t end_of_img=sizeof(MULTIPART_BEGIN)+image_size;
	if (offset==0){
229
		size_t partlen=sizeof(MULTIPART_BEGIN);
230
		BC_ASSERT_LOWER_STRICT((unsigned int)partlen,(unsigned int)*size,unsigned int,"%u");
231 232 233 234 235 236 237 238 239 240 241
		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);
242
		strncpy((char*)buffer,MULTIPART_END,*size);
243 244 245 246 247 248 249 250 251 252
	}
	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};
253 254
	belle_sip_user_body_handler_t *bh;
	char *content_type;
255
	const char *url="https://www.linphone.org:444/lft.php";
256 257 258
	if (url_supported(url)==-1) {
		return;
	}
259
	bh=belle_sip_user_body_handler_new(image_size+sizeof(MULTIPART_BEGIN)+sizeof(MULTIPART_END), on_progress, NULL, NULL, on_send_body, NULL, NULL);
260
	content_type=belle_sip_strdup_printf("multipart/form-data; boundary=%s",multipart_boudary);
261

262
	uri=belle_generic_uri_parse(url);
263

264 265 266 267 268 269 270 271 272 273 274 275
	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);
276 277
	BC_ASSERT_TRUE(wait_for(stack,&counters.two_hundred,1,20000));

278 279 280
	belle_sip_object_unref(l);
}

281
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){
282 283 284 285 286 287 288 289
	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++;
290
	BC_ASSERT_PTR_NOT_NULL(event->response);
291 292 293 294 295 296
	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,
297
			(belle_sip_body_handler_t*)belle_sip_user_body_handler_new(0,on_progress, NULL,on_recv_body,NULL, NULL,file)
298 299 300 301
		);
	}
}

302
static void http_get_long_user_body(void){
303 304 305 306 307 308 309 310 311
	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");
312

313
	uri=belle_generic_uri_parse(url);
314

315 316 317 318 319 320 321 322 323 324 325 326
	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);
327
	BC_ASSERT_TRUE(wait_for(stack,&counters.two_hundred,1,20000));
328
	BC_ASSERT_EQUAL(counters.response_headers_count,1,int,"%d");
329
	resp=belle_http_request_get_response(req);
330
	BC_ASSERT_PTR_NOT_NULL(resp);
331 332
	if (resp){
		bh=belle_sip_message_get_body_handler((belle_sip_message_t*)resp);
333
		BC_ASSERT_GREATER_STRICT((unsigned int)belle_sip_body_handler_get_size(bh),0,unsigned int,"%u");
334 335 336 337 338 339
	}
	belle_sip_object_unref(req);
	belle_sip_object_unref(l);
	if (outfile) fclose(outfile);
}

340 341 342 343 344 345 346
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);
347

348 349 350 351 352 353 354 355 356 357 358
	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);

}


359
test_t http_tests[] = {
360 361 362
	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
363
	TEST_NO_TAG("One https GET with maddr", one_https_get_with_maddr),
364 365 366 367 368 369 370
	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)
371 372
};

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