belle_sip_register_tester.c 20.2 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 19
/*
	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
    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/>.
*/

#include <stdio.h>
jehan's avatar
jehan committed
20
#include "CUnit/Basic.h"
Simon Morlat's avatar
Simon Morlat committed
21
#include "belle-sip/belle-sip.h"
22
#include "belle_sip_internal.h"
Ghislain MARY's avatar
Ghislain MARY committed
23
#include "belle_sip_tester.h"
24

Simon Morlat's avatar
Simon Morlat committed
25 26
#include "register_tester.h"

Ghislain MARY's avatar
Ghislain MARY committed
27

Simon Morlat's avatar
Simon Morlat committed
28
const char *test_domain="test.linphone.org";
29
const char *auth_domain="sip.linphone.org";
jehan's avatar
jehan committed
30 31 32
const char *client_auth_domain="client.example.org";
const char *client_auth_outband_proxy="sips:sip2.linphone.org:5063";

jehan's avatar
jehan committed
33
static int is_register_ok;
34
static int number_of_challenge;
35
static int using_transaction;
jehan's avatar
jehan committed
36 37 38
belle_sip_stack_t * stack;
belle_sip_provider_t *prov;
static belle_sip_listener_t* l;
Ghislain MARY's avatar
Ghislain MARY committed
39 40 41 42
belle_sip_request_t* authorized_request;
belle_sip_listener_callbacks_t listener_callbacks;
belle_sip_listener_t *listener;

jehan's avatar
jehan committed
43

44
static void process_dialog_terminated(void *user_ctx, const belle_sip_dialog_terminated_event_t *event){
45 46
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
47 48
	belle_sip_message("process_dialog_terminated called");
}
Ghislain MARY's avatar
Ghislain MARY committed
49

50
static void process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){
51 52
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
53
	belle_sip_message("process_io_error");
54 55
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
	/*CU_ASSERT(CU_FALSE);*/
56
}
Ghislain MARY's avatar
Ghislain MARY committed
57

58
static void process_request_event(void *user_ctx, const belle_sip_request_event_t *event){
59 60
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
61 62
	belle_sip_message("process_request_event");
}
Ghislain MARY's avatar
Ghislain MARY committed
63

64
static void process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
65 66
	int status;
	belle_sip_request_t* request;
67
	BELLESIP_UNUSED(user_ctx);
jehan's avatar
jehan committed
68
	CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_response_event_get_response(event));
69 70 71 72
	belle_sip_message("process_response_event [%i] [%s]"
					,status=belle_sip_response_get_status_code(belle_sip_response_event_get_response(event))
					,belle_sip_response_get_reason_phrase(belle_sip_response_event_get_response(event)));
	if (status==401) {
73 74
		belle_sip_header_cseq_t* cseq;
		belle_sip_client_transaction_t *t;
75
		CU_ASSERT_NOT_EQUAL_FATAL(number_of_challenge,2);
76 77
		CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_response_event_get_client_transaction(event)); /*require transaction mode*/
		request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(belle_sip_response_event_get_client_transaction(event)));
78
		cseq=(belle_sip_header_cseq_t*)belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_CSEQ);
79
		belle_sip_header_cseq_set_seq_number(cseq,belle_sip_header_cseq_get_seq_number(cseq)+1);
80
		CU_ASSERT_TRUE_FATAL(belle_sip_provider_add_authorization(prov,request,belle_sip_response_event_get_response(event),NULL));
81
		t=belle_sip_provider_create_client_transaction(prov,request);
82
		belle_sip_client_transaction_send_request(t);
83
		number_of_challenge++;
84
		authorized_request=request;
85
		belle_sip_object_ref(authorized_request);
86 87 88 89 90 91
	}  else {
		CU_ASSERT_EQUAL(status,200);
		is_register_ok=1;
		using_transaction=belle_sip_response_event_get_client_transaction(event)!=NULL;
		belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
	}
92
}
Ghislain MARY's avatar
Ghislain MARY committed
93

94
static void process_timeout(void *user_ctx, const belle_sip_timeout_event_t *event){
95 96
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
97 98
	belle_sip_message("process_timeout");
}
Ghislain MARY's avatar
Ghislain MARY committed
99

100
static void process_transaction_terminated(void *user_ctx, const belle_sip_transaction_terminated_event_t *event){
101 102
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
103 104
	belle_sip_message("process_transaction_terminated");
}
Ghislain MARY's avatar
Ghislain MARY committed
105

jehan's avatar
jehan committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static const char* client_cert = /*for URI:sip:tester@client.example.org*/
		"-----BEGIN CERTIFICATE-----\n"
		"MIIDYzCCAsygAwIBAgIBCDANBgkqhkiG9w0BAQUFADCBuzELMAkGA1UEBhMCRlIx\n"
		"EzARBgNVBAgMClNvbWUtU3RhdGUxETAPBgNVBAcMCEdyZW5vYmxlMSIwIAYDVQQK\n"
		"DBlCZWxsZWRvbm5lIENvbW11bmljYXRpb25zMQwwCgYDVQQLDANMQUIxFjAUBgNV\n"
		"BAMMDUplaGFuIE1vbm5pZXIxOjA4BgkqhkiG9w0BCQEWK2plaGFuLm1vbm5pZXJA\n"
		"YmVsbGVkb25uZS1jb21tdW5pY2F0aW9ucy5jb20wHhcNMTMxMDAzMTQ0MTEwWhcN\n"
		"MjMxMDAxMTQ0MTEwWjCBtTELMAkGA1UEBhMCRlIxDzANBgNVBAgMBkZyYW5jZTER\n"
		"MA8GA1UEBwwIR3Jlbm9ibGUxIjAgBgNVBAoMGUJlbGxlZG9ubmUgQ29tbXVuaWNh\n"
		"dGlvbnMxDDAKBgNVBAsMA0xBQjEUMBIGA1UEAwwLY2xpZW50IGNlcnQxOjA4Bgkq\n"
		"hkiG9w0BCQEWK2plaGFuLm1vbm5pZXJAYmVsbGVkb25uZS1jb21tdW5pY2F0aW9u\n"
		"cy5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZxC/qBi/zB/4lgI7V7\n"
		"I5TsmMmOp0+R/TCyVnYvKQuaJXh9i+CobVM7wj/pQg8RgsY1x+4mVwH1QbhOdIN0\n"
		"ExYHKgLTPlo9FaN6oHPOcHxU/wt552aZhCHC+ushwUUyjy8+T09UOP+xK9V7y5uD\n"
		"ZY+vIOvi6QNwc5cqyy8TREwNAgMBAAGjezB5MAkGA1UdEwQCMAAwLAYJYIZIAYb4\n"
		"QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRlMB0GA1UdDgQWBBTL\n"
		"eWEg7jewRbQbKXSdBvsyygGIFzAfBgNVHSMEGDAWgBQGX13HFq9i+C1ucQOIoNYd\n"
		"KwR/ujANBgkqhkiG9w0BAQUFAAOBgQBTbEoi94pVfevbK22Oj8PJFsuy+el1pJG+\n"
		"h0XGM9SQGfbcS7PsV/MhFXtmpnmj3vQB3u5QtMGtWcLA2uxXi79i82gw+oEpBKHR\n"
		"sLqsNCzWNCL9n1pjpNSdqqBFGUdB9pSpnYalujAbuzkqq1ZLyzsElvK7pCaLQcSs\n"
		"oEncRDdPOA==\n"
		"-----END CERTIFICATE-----";


static const char* private_key =
		"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
		"MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIbEHnQwhgRwoCAggA\n"
		"MBQGCCqGSIb3DQMHBAgmrtCEBCP9kASCAoCq9EKInROalaBSLWY44U4RVAC+CKdx\n"
		"Q8ooT7Bz/grgZuCiaGf0UKINJeV4LYHoP+AWjCH8EeebIA8dldNy5rGcBTt7sXd1\n"
		"QOGmnkBplXTW/NTsb9maYRK56kNJhLE4DR5X5keziV1Tdy2KBmTlpllsCXWsSOBq\n"
		"iI63PTaakIvZxA0TEmie5QQWpH777e/LmW3vVHdH8hhp2zeDDjfSW2E290+ce4Yj\n"
		"SDW9oFXvauzhzhSYRkUdfoJSbpu5MYwyzhjAXQpmBJDauu7+jAU/rQw6TLmYjDNZ\n"
		"3PYHzyD4N7tCG9u4mPBo33dhUirP+8E1BftHB+i/VIn6pI3ypMyiFZ1ZCHqi4vhW\n"
		"z7aChRrUY/8XWCpln3azcfj4SW+Mz62sAChY8rn+yyxFgIno8d9rrx67jyAnYJ6Q\n"
		"sfIMwKp3Sz5oI7IDk8If5SuBVkpqlRV+eZFT6zRRFk65beYpq70BN2mYaKzSV8A7\n"
		"rnciho/dfa9wvyWmkqXciBgWh18UTACOM9HPLmQef3FGaUDLiTAGS1osyypGUEPt\n"
		"Ox3u51qpYkibwyQZo1+ujQkh9PiKfevIAXmty0nTFWMEED15G2SJKjunw5N1rEAh\n"
		"M9jlYpLnATcfigPfGo19QrIPQ1c0LB4BqdwAWN3ZLe0QqYdgwzdcwIoLQRp9iDcw\n"
		"Omc31+38cTc2yGQ2Y2XHZkL8GY/rkqkbhVt9Rnh+VJxFeB6FlsL66EycApe07ngx\n"
		"QimGP57yp4aBzpJyW+6GPf8A/Ogsv3ay1QBLUiGEJtUglRHnl9F6nm5Nxm7wubVx\n"
		"WEuSefVM4xgB+mfQauAJu2N9yKhzXOytslZflpa06qJedlLYFk9njvcv\n"
		"-----END ENCRYPTED PRIVATE KEY-----\n";

static const char* private_key_passwd="secret";


152
static void process_auth_requested(void *user_ctx, belle_sip_auth_event_t *event){
153
	BELLESIP_UNUSED(user_ctx);
154
	if (belle_sip_auth_event_get_mode(event) == BELLE_SIP_AUTH_MODE_HTTP_DIGEST) {
jehan's avatar
jehan committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		belle_sip_message("process_auth_requested requested for [%s@%s]"
				,belle_sip_auth_event_get_username(event)
				,belle_sip_auth_event_get_realm(event));
		belle_sip_auth_event_set_passwd(event,"secret");
	} else if (belle_sip_auth_event_get_mode(event) == BELLE_SIP_AUTH_MODE_TLS) {
		belle_sip_certificates_chain_t* cert = belle_sip_certificates_chain_parse(client_cert,strlen(client_cert),BELLE_SIP_CERTIFICATE_RAW_FORMAT_PEM);
		belle_sip_signing_key_t* key = belle_sip_signing_key_parse(private_key,strlen(private_key),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));

	} else {
		belle_sip_error("Unexpected auth mode");
	}
170
}
171

jehan's avatar
jehan committed
172
int register_init(void) {
173
	belle_sip_listening_point_t *lp;
jehan's avatar
jehan committed
174
	stack=belle_sip_stack_new(NULL);
175
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
176
	prov=belle_sip_stack_create_provider(stack,lp);
177
	
178 179
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"TCP");
	belle_sip_provider_add_listening_point(prov,lp);
180
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7061,"TLS");
jehan's avatar
jehan committed
181
	if (lp) {
182 183
		/* since test.linphone.org does not have proper certificates, don't verify anything*/
		belle_sip_tls_listening_point_set_verify_exceptions(BELLE_SIP_TLS_LISTENING_POINT(lp),BELLE_SIP_TLS_LISTENING_POINT_BADCERT_ANY_REASON);
jehan's avatar
jehan committed
184 185
		belle_sip_provider_add_listening_point(prov,lp);
	}
jehan's avatar
jehan committed
186

187 188 189 190 191 192 193 194 195
	listener_callbacks.process_dialog_terminated=process_dialog_terminated;
	listener_callbacks.process_io_error=process_io_error;
	listener_callbacks.process_request_event=process_request_event;
	listener_callbacks.process_response_event=process_response_event;
	listener_callbacks.process_timeout=process_timeout;
	listener_callbacks.process_transaction_terminated=process_transaction_terminated;
	listener_callbacks.process_auth_requested=process_auth_requested;
	listener_callbacks.listener_destroyed=NULL;
	listener=belle_sip_listener_create_from_callbacks(&listener_callbacks,NULL);
jehan's avatar
jehan committed
196 197
	return 0;
}
Ghislain MARY's avatar
Ghislain MARY committed
198

jehan's avatar
jehan committed
199
int register_uninit(void) {
200
	belle_sip_object_unref(prov);
jehan's avatar
jehan committed
201
	belle_sip_object_unref(stack);
202
	belle_sip_object_unref(listener);
jehan's avatar
jehan committed
203 204
	return 0;
}
jehan's avatar
jehan committed
205 206 207 208 209 210

void unregister_user(belle_sip_stack_t * stack
					,belle_sip_provider_t *prov
					,belle_sip_request_t* initial_request
					,int use_transaction) {
	belle_sip_request_t *req;
211 212 213
	belle_sip_header_cseq_t* cseq;
	belle_sip_header_expires_t* expires_header;
	int i;
jehan's avatar
jehan committed
214 215 216 217
	belle_sip_provider_add_sip_listener(prov,l);
	is_register_ok=0;
	using_transaction=0;
	req=(belle_sip_request_t*)belle_sip_object_clone((belle_sip_object_t*)initial_request);
218
	cseq=(belle_sip_header_cseq_t*)belle_sip_message_get_header((belle_sip_message_t*)req,BELLE_SIP_CSEQ);
jehan's avatar
jehan committed
219
	belle_sip_header_cseq_set_seq_number(cseq,belle_sip_header_cseq_get_seq_number(cseq)+2); /*+2 if initial reg was challenged*/
220
	expires_header=(belle_sip_header_expires_t*)belle_sip_message_get_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_EXPIRES);
jehan's avatar
jehan committed
221 222
	belle_sip_header_expires_set_expires(expires_header,0);
	if (use_transaction){
223
		belle_sip_client_transaction_t *t;
224 225
		belle_sip_message_remove_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_AUTHORIZATION);
		belle_sip_message_remove_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_PROXY_AUTHORIZATION);
226
		belle_sip_provider_add_authorization(prov,req,NULL,NULL); /*just in case*/
227
		t=belle_sip_provider_create_client_transaction(prov,req);
jehan's avatar
jehan committed
228 229
		belle_sip_client_transaction_send_request(t);
	}else belle_sip_provider_send_request(prov,req);
jehan's avatar
jehan committed
230 231
	for(i=0;!is_register_ok && i<2 ;i++)
		belle_sip_stack_sleep(stack,5000);
jehan's avatar
jehan committed
232 233 234 235
	CU_ASSERT_EQUAL(is_register_ok,1);
	CU_ASSERT_EQUAL(using_transaction,use_transaction);
	belle_sip_provider_remove_sip_listener(prov,l);
}
Ghislain MARY's avatar
Ghislain MARY committed
236

jehan's avatar
jehan committed
237
belle_sip_request_t* try_register_user_at_domain(belle_sip_stack_t * stack
jehan's avatar
jehan committed
238 239 240
					,belle_sip_provider_t *prov
					,const char *transport
					,int use_transaction
241
					,const char* username
jehan's avatar
jehan committed
242
					,const char* domain
Simon Morlat's avatar
Simon Morlat committed
243
					,const char* outbound_proxy
jehan's avatar
jehan committed
244
					,int success_expected) {
245
	belle_sip_request_t *req,*copy;
246 247
	char identity[256];
	char uri[256];
248
	int i;
Simon Morlat's avatar
Simon Morlat committed
249 250
	char *outbound=NULL;
	
251
	number_of_challenge=0;
252
	if (transport)
253 254
		snprintf(uri,sizeof(uri),"sip:%s;transport=%s",domain,transport);
	else snprintf(uri,sizeof(uri),"sip:%s",domain);
jehan's avatar
jehan committed
255

Simon Morlat's avatar
Simon Morlat committed
256 257
	if (transport && strcasecmp("tls",transport)==0 && belle_sip_provider_get_listening_point(prov,"tls")==NULL){
		belle_sip_error("No TLS support, test skipped.");
jehan's avatar
jehan committed
258
		return NULL;
Simon Morlat's avatar
Simon Morlat committed
259
	}
Simon Morlat's avatar
Simon Morlat committed
260 261
	
	if (outbound_proxy){
jehan's avatar
jehan committed
262
		if (strstr(outbound_proxy,"sip:")==NULL && strstr(outbound_proxy,"sips:")==NULL){
Simon Morlat's avatar
Simon Morlat committed
263 264 265
			outbound=belle_sip_strdup_printf("sip:%s",outbound_proxy);
		}else outbound=belle_sip_strdup(outbound_proxy);
	}
Simon Morlat's avatar
Simon Morlat committed
266

267
	snprintf(identity,sizeof(identity),"Tester <sip:%s@%s>",username,domain);
Simon Morlat's avatar
Simon Morlat committed
268
	req=belle_sip_request_create(
269
	                    belle_sip_uri_parse(uri),
Simon Morlat's avatar
Simon Morlat committed
270 271 272
	                    "REGISTER",
	                    belle_sip_provider_create_call_id(prov),
	                    belle_sip_header_cseq_create(20,"REGISTER"),
273
	                    belle_sip_header_from_create2(identity,BELLE_SIP_RANDOM_TAG),
jehan's avatar
jehan committed
274
	                    belle_sip_header_to_create2(identity,NULL),
275
	                    belle_sip_header_via_new(),
Simon Morlat's avatar
Simon Morlat committed
276
	                    70);
jehan's avatar
jehan committed
277
	is_register_ok=0;
278
	using_transaction=0;
jehan's avatar
jehan committed
279
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_expires_create(600)));
280
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_contact_new()));
281
	copy=(belle_sip_request_t*)belle_sip_object_ref(belle_sip_object_clone((belle_sip_object_t*)req));
jehan's avatar
jehan committed
282
	belle_sip_provider_add_sip_listener(prov,l=BELLE_SIP_LISTENER(listener));
283 284
	if (use_transaction){
		belle_sip_client_transaction_t *t=belle_sip_provider_create_client_transaction(prov,req);
Simon Morlat's avatar
Simon Morlat committed
285
		belle_sip_client_transaction_send_request_to(t,outbound?belle_sip_uri_parse(outbound):NULL);
286
	}else belle_sip_provider_send_request(prov,req);
jehan's avatar
jehan committed
287 288
	for(i=0;!is_register_ok && i<2 ;i++)
		belle_sip_stack_sleep(stack,5000);
jehan's avatar
jehan committed
289
	CU_ASSERT_EQUAL(is_register_ok,success_expected);
290
	if (success_expected) CU_ASSERT_EQUAL(using_transaction,use_transaction);
291

jehan's avatar
jehan committed
292
	belle_sip_provider_remove_sip_listener(prov,l);
Simon Morlat's avatar
Simon Morlat committed
293
	if (outbound) belle_sip_free(outbound);
294
	return copy;
Simon Morlat's avatar
Simon Morlat committed
295
}
Ghislain MARY's avatar
Ghislain MARY committed
296

jehan's avatar
jehan committed
297 298 299 300 301
belle_sip_request_t* register_user_at_domain(belle_sip_stack_t * stack
					,belle_sip_provider_t *prov
					,const char *transport
					,int use_transaction
					,const char* username
302 303 304
					,const char* domain
					,const char* outband) {
	return try_register_user_at_domain(stack,prov,transport,use_transaction,username,domain,outband,1);
305

jehan's avatar
jehan committed
306
}
Ghislain MARY's avatar
Ghislain MARY committed
307

308 309 310 311
belle_sip_request_t* register_user(belle_sip_stack_t * stack
					,belle_sip_provider_t *prov
					,const char *transport
					,int use_transaction
312
					,const char* username
Simon Morlat's avatar
Simon Morlat committed
313 314
					,const char* outbound) {
	return register_user_at_domain(stack,prov,transport,use_transaction,username,test_domain,outbound);
315
}
316

Simon Morlat's avatar
Simon Morlat committed
317
static void register_with_outbound(const char *transport, int use_transaction,const char* outbound ) {
jehan's avatar
jehan committed
318
	belle_sip_request_t *req;
Simon Morlat's avatar
Simon Morlat committed
319 320
	
	req=register_user(stack, prov, transport,use_transaction,"tester",outbound);
321 322 323 324
	if (req) {
		unregister_user(stack,prov,req,use_transaction);
		belle_sip_object_unref(req);
	}
jehan's avatar
jehan committed
325
}
Ghislain MARY's avatar
Ghislain MARY committed
326

327
static void register_test(const char *transport, int use_transaction) {
Simon Morlat's avatar
Simon Morlat committed
328
	register_with_outbound(transport,use_transaction,NULL);
329
}
Ghislain MARY's avatar
Ghislain MARY committed
330

331 332
static void stateless_register_udp(void){
	register_test(NULL,0);
jehan's avatar
jehan committed
333
}
334

jehan's avatar
jehan committed
335 336 337 338
static void stateless_register_tls(void){
	register_test("tls",0);
}

339 340 341 342 343 344 345
static void stateless_register_tcp(void){
	register_test("tcp",0);
}

static void stateful_register_udp(void){
	register_test(NULL,1);
}
Ghislain MARY's avatar
Ghislain MARY committed
346

347
static void stateful_register_udp_with_keep_alive(void) {
jehan's avatar
jehan committed
348 349 350 351 352
	belle_sip_listening_point_set_keep_alive(belle_sip_provider_get_listening_point(prov,"udp"),200);
	register_test(NULL,1);
	belle_sip_main_loop_sleep(belle_sip_stack_get_main_loop(stack),500);
	belle_sip_listening_point_set_keep_alive(belle_sip_provider_get_listening_point(prov,"udp"),-1);
}
Ghislain MARY's avatar
Ghislain MARY committed
353

Simon Morlat's avatar
Simon Morlat committed
354 355
static void stateful_register_udp_with_outbound_proxy(void){
	register_with_outbound("udp",1,test_domain);
356 357
}

Simon Morlat's avatar
Simon Morlat committed
358
static void stateful_register_udp_delayed(void){
359
	belle_sip_stack_set_tx_delay(stack,3000);
Simon Morlat's avatar
Simon Morlat committed
360 361 362 363
	register_test(NULL,1);
	belle_sip_stack_set_tx_delay(stack,0);
}

364 365
static void stateful_register_udp_with_send_error(void){
	belle_sip_stack_set_send_error(stack,-1);
366
	try_register_user_at_domain(stack, prov, NULL,1,"tester",test_domain,NULL,0);
367 368 369
	belle_sip_stack_set_send_error(stack,0);
}

370 371
static void stateful_register_tcp(void){
	register_test("tcp",1);
jehan's avatar
jehan committed
372
}
Simon Morlat's avatar
Simon Morlat committed
373

374 375 376
static void stateful_register_tls(void){
	register_test("tls",1);
}
jehan's avatar
jehan committed
377

jehan's avatar
jehan committed
378
static void bad_req_process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){
379 380
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
jehan's avatar
jehan committed
381 382
	belle_sip_message("bad_req_process_io_error not implemented yet");
}
Ghislain MARY's avatar
Ghislain MARY committed
383

jehan's avatar
jehan committed
384
static void bad_req_process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
385 386
	BELLESIP_UNUSED(user_ctx);
	BELLESIP_UNUSED(event);
jehan's avatar
jehan committed
387 388 389
	belle_sip_message("bad_req_process_response_event not implemented yet");
}

390
static void test_bad_request(void) {
jehan's avatar
jehan committed
391
	belle_sip_request_t *req;
392 393
	belle_sip_listener_t *bad_req_listener;
	belle_sip_client_transaction_t *t;
jehan's avatar
jehan committed
394 395 396 397 398 399
	belle_sip_header_address_t* route_address=belle_sip_header_address_create(NULL,belle_sip_uri_create(NULL,test_domain));
	belle_sip_header_route_t* route;
	belle_sip_header_to_t* to = belle_sip_header_to_create2("sip:toto@titi.com",NULL);
	belle_sip_listener_callbacks_t cbs;
	memset(&cbs,0,sizeof(cbs));

400
	bad_req_listener = belle_sip_listener_create_from_callbacks(&cbs,NULL);
jehan's avatar
jehan committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	cbs.process_io_error=bad_req_process_io_error;
	cbs.process_response_event=bad_req_process_response_event;

	req=belle_sip_request_create(
	                    BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(belle_sip_header_address_get_uri(route_address)))),
	                    "REGISTER",
	                    belle_sip_provider_create_call_id(prov),
	                    belle_sip_header_cseq_create(20,"REGISTER"),
	                    belle_sip_header_from_create2("sip:toto@titi.com",BELLE_SIP_RANDOM_TAG),
	                    to,
	                    belle_sip_header_via_new(),
	                    70);

	belle_sip_uri_set_transport_param(belle_sip_header_address_get_uri(route_address),"tcp");
	route = belle_sip_header_route_create(route_address);
	belle_sip_header_set_name(BELLE_SIP_HEADER(to),"BrokenHeader");

	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_expires_create(600)));
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(route));
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_contact_new()));
	belle_sip_provider_add_sip_listener(prov,bad_req_listener);
422
	t=belle_sip_provider_create_client_transaction(prov,req);
jehan's avatar
jehan committed
423 424 425
	belle_sip_client_transaction_send_request(t);
	belle_sip_stack_sleep(stack,100);
	belle_sip_provider_remove_sip_listener(prov,bad_req_listener);
426
	belle_sip_object_unref(bad_req_listener);
jehan's avatar
jehan committed
427
}
Simon Morlat's avatar
Simon Morlat committed
428

429
static void test_register_authenticate(void) {
430
	belle_sip_request_t *reg;
431
	number_of_challenge=0;
432
	authorized_request=NULL;
433
	reg=register_user_at_domain(stack, prov, "udp",1,"bellesip",auth_domain,NULL);
434 435 436 437 438
	if (authorized_request) {
		unregister_user(stack,prov,authorized_request,1);
		belle_sip_object_unref(authorized_request);
	}
	belle_sip_object_unref(reg);
439
}
jehan's avatar
jehan committed
440

441 442 443 444 445 446 447 448 449 450 451 452
static void test_register_channel_inactive(void){
	belle_sip_listening_point_t *lp=belle_sip_provider_get_listening_point(prov,"TCP");
	CU_ASSERT_PTR_NOT_NULL_FATAL(lp);
	belle_sip_stack_set_inactive_transport_timeout(stack,5);
	belle_sip_listening_point_clean_channels(lp);
	CU_ASSERT_EQUAL(belle_sip_listening_point_get_channel_count(lp),0);
	register_test("tcp",1);
	CU_ASSERT_EQUAL(belle_sip_listening_point_get_channel_count(lp),1);
	belle_sip_stack_sleep(stack,5000);
	CU_ASSERT_EQUAL(belle_sip_listening_point_get_channel_count(lp),0);
	belle_sip_stack_set_inactive_transport_timeout(stack,3600);
}
jehan's avatar
jehan committed
453

jehan's avatar
jehan committed
454 455 456 457 458 459 460 461 462 463 464 465 466


static void test_register_client_authenticated(void) {
	belle_sip_request_t *reg;
	authorized_request=NULL;
	/*we don't care to check sercer cert*/
	belle_sip_tls_listening_point_set_verify_exceptions(	(belle_sip_tls_listening_point_t*)belle_sip_provider_get_listening_point(prov,"tls")
															,BELLE_SIP_TLS_LISTENING_POINT_BADCERT_ANY_REASON);
	reg=register_user_at_domain(stack, prov, "tls",1,"tester",client_auth_domain,client_auth_outband_proxy);
	if (authorized_request) {
		unregister_user(stack,prov,authorized_request,1);
		belle_sip_object_unref(authorized_request);
	}
467
	if (reg) belle_sip_object_unref(reg);
jehan's avatar
jehan committed
468 469 470
}


Ghislain MARY's avatar
Ghislain MARY committed
471 472 473 474 475 476 477 478 479 480 481 482
test_t register_tests[] = {
	{ "Stateful UDP", stateful_register_udp },
	{ "Stateful UDP with keep-alive", stateful_register_udp_with_keep_alive },
	{ "Stateful UDP with network delay", stateful_register_udp_delayed },
	{ "Stateful UDP with send error", stateful_register_udp_with_send_error },
	{ "Stateful UDP with outbound proxy", stateful_register_udp_with_outbound_proxy },
	{ "Stateful TCP", stateful_register_tcp },
	{ "Stateful TLS", stateful_register_tls },
	{ "Stateless UDP", stateless_register_udp },
	{ "Stateless TCP", stateless_register_tcp },
	{ "Stateless TLS", stateless_register_tls },
	{ "Bad TCP request", test_bad_request },
483
	{ "Authenticate", test_register_authenticate },
jehan's avatar
jehan committed
484
	{ "TLS client cert authentication", test_register_client_authenticated },
485
	{ "Channel inactive", test_register_channel_inactive }
Ghislain MARY's avatar
Ghislain MARY committed
486 487 488 489 490 491 492 493 494
};

test_suite_t register_test_suite = {
	"REGISTER",
	register_init,
	register_uninit,
	sizeof(register_tests) / sizeof(register_tests[0]),
	register_tests
};
jehan's avatar
jehan committed
495