belle_sip_dialog_tester.c 15.2 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
	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>
#include "CUnit/Basic.h"
#include "belle-sip/belle-sip.h"
#include "pthread.h"

extern belle_sip_stack_t * stack;
extern belle_sip_provider_t *prov;
extern const char *test_domain;
jehan's avatar
jehan committed
27
int call_endeed;
jehan's avatar
jehan committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
extern int register_init(void);
extern int register_uninit(void);
extern belle_sip_request_t* register_user(belle_sip_stack_t * stack
		,belle_sip_provider_t *prov
		,const char *transport
		,int use_transaction
		,const char* username) ;
extern void unregister_user(belle_sip_stack_t * stack
					,belle_sip_provider_t *prov
					,belle_sip_request_t* initial_request
					,int use_transaction);



jehan's avatar
jehan committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static const char* sdp = 		"v=0\r\n"\
								"o=jehan-mac 1239 1239 IN IP4 192.168.0.18\r\n"\
								"s=Talk\r\n"\
								"c=IN IP4 192.168.0.18\r\n"\
								"t=0 0\r\n"\
								"m=audio 7078 RTP/AVP 111 110 3 0 8 101\r\n"\
								"a=rtpmap:111 speex/16000\r\n"\
								"a=fmtp:111 vbr=on\r\n"\
								"a=rtpmap:110 speex/8000\r\n"\
								"a=fmtp:110 vbr=on\r\n"\
								"a=rtpmap:101 telephone-event/8000\r\n"\
								"a=fmtp:101 0-11\r\n"\
								"m=video 8078 RTP/AVP 99 97 98\r\n"\
								"c=IN IP4 192.168.0.18\r\n"\
								"b=AS:380\r\n"\
								"a=rtpmap:99 MP4V-ES/90000\r\n"\
								"a=fmtp:99 profile-level-id=3\r\n"\
								"a=rtpmap:97 theora/90000\r\n"\
								"a=rtpmap:98 H263-1998/90000\r\n"\
								"a=fmtp:98 CIF=1;QCIF=1\r\n";

jehan's avatar
jehan committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static int init(void) {
	register_init();
	return 0;
}
static int uninit(void) {
	register_uninit();

	return 0;
}

belle_sip_request_t* build_request(belle_sip_stack_t * stack
									, belle_sip_provider_t *prov
									,belle_sip_header_address_t* from
									,belle_sip_header_address_t* to
									,belle_sip_header_address_t* route
									,const char* method) {
	belle_sip_header_from_t* from_header;
	belle_sip_header_to_t* to_header;
	belle_sip_request_t *req;
	belle_sip_uri_t* req_uri;
	belle_sip_header_contact_t* contact_header;


	from_header = belle_sip_header_from_create(from,BELLE_SIP_RANDOM_TAG);
	to_header = belle_sip_header_to_create(to,NULL);
	req_uri = (belle_sip_uri_t*)belle_sip_object_clone((belle_sip_object_t*)belle_sip_header_address_get_uri((belle_sip_header_address_t*)to_header));

	contact_header= belle_sip_header_contact_new();
	belle_sip_header_address_set_uri((belle_sip_header_address_t*)contact_header,belle_sip_uri_new());
	belle_sip_uri_set_user(belle_sip_header_address_get_uri((belle_sip_header_address_t*)contact_header),belle_sip_uri_get_user(req_uri));
	req=belle_sip_request_create(
							req_uri,
							method,
		                    belle_sip_provider_create_call_id(prov),
		                    belle_sip_header_cseq_create(20,method),
		                    from_header,
		                    to_header,
		                    belle_sip_header_via_new(),
		                    70);
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(contact_header));
	if (route) {
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_route_create(route)));
	}
	return req;
}

jehan's avatar
jehan committed
109 110 111 112 113
static belle_sip_dialog_t* caller_dialog;
static belle_sip_dialog_t* callee_dialog;
static belle_sip_response_t* ok_response;
static belle_sip_server_transaction_t* inserv_transaction;

jehan's avatar
jehan committed
114 115 116 117 118 119
static void process_dialog_terminated(void *user_ctx, const belle_sip_dialog_terminated_event_t *event){
	belle_sip_message("process_dialog_terminated not implemented yet");
}
static void process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){
	belle_sip_message("process_io_error not implemented yet");
}
jehan's avatar
jehan committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static void caller_process_request_event(void *user_ctx, const belle_sip_request_event_t *event) {
	belle_sip_header_to_t* to=belle_sip_message_get_header_by_type(belle_sip_request_event_get_request(event),belle_sip_header_to_t);
	if (!belle_sip_uri_equals(BELLE_SIP_URI(user_ctx),belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(to)))) {
		return; /*not for the caller*/
	}
	belle_sip_message("caller_process_request_event received [%s] message",belle_sip_request_get_method(belle_sip_request_event_get_request(event)));
	belle_sip_server_transaction_t* server_transaction =belle_sip_provider_create_server_transaction(prov,belle_sip_request_event_get_request(event));
	belle_sip_response_t* resp;
	CU_ASSERT_STRING_EQUAL_FATAL("BYE",belle_sip_request_get_method(belle_sip_request_event_get_request(event)));
	belle_sip_dialog_t* dialog =  belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(server_transaction));
	CU_ASSERT_PTR_NOT_NULL_FATAL(dialog);
	CU_ASSERT_EQUAL(belle_sip_dialog_get_state(dialog) , BELLE_SIP_DIALOG_CONFIRMED);
	resp=belle_sip_response_create_from_request(belle_sip_request_event_get_request(event),200);
	belle_sip_server_transaction_send_response(server_transaction,resp);

}
136
static void callee_process_request_event(void *user_ctx, const belle_sip_request_event_t *event) {
jehan's avatar
jehan committed
137
	belle_sip_server_transaction_t* server_transaction = belle_sip_request_event_get_server_transaction(event);
138 139 140 141 142 143
	belle_sip_header_to_t* to=belle_sip_message_get_header_by_type(belle_sip_request_event_get_request(event),belle_sip_header_to_t);
	const char* method;
	if (!belle_sip_uri_equals(BELLE_SIP_URI(user_ctx),belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(to)))) {
		return; /*not for the callee*/
	}

jehan's avatar
jehan committed
144 145 146
	if (!server_transaction) {
		server_transaction= belle_sip_provider_create_server_transaction(prov,belle_sip_request_event_get_request(event));
	}
147 148
	method = belle_sip_request_get_method(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(server_transaction)));
	belle_sip_message("callee_process_request_event received [%s] message",method);
jehan's avatar
jehan committed
149 150
	belle_sip_dialog_t* dialog =  belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(server_transaction));
	belle_sip_response_t* ringing_response;
jehan's avatar
jehan committed
151 152
	belle_sip_header_content_type_t* content_type ;
	belle_sip_header_content_length_t* content_length;
jehan's avatar
jehan committed
153
	if (!dialog ) {
154
		CU_ASSERT_STRING_EQUAL_FATAL("INVITE",method);
jehan's avatar
jehan committed
155
		dialog=belle_sip_provider_create_dialog(prov,BELLE_SIP_TRANSACTION(server_transaction));
jehan's avatar
jehan committed
156
		callee_dialog=dialog;
jehan's avatar
jehan committed
157
		inserv_transaction=server_transaction;
jehan's avatar
jehan committed
158 159 160
	}
	if (belle_sip_dialog_get_state(dialog) == BELLE_SIP_DIALOG_NULL) {
		ringing_response = belle_sip_response_create_from_request(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(server_transaction)),180);
jehan's avatar
jehan committed
161 162 163 164 165 166 167 168
		/*prepare 200ok*/
		ok_response = belle_sip_response_create_from_request(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(server_transaction)),200);
		content_length= belle_sip_header_content_length_create(strlen(sdp));
		content_type = belle_sip_header_content_type_create("application","sdp");
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(ok_response),BELLE_SIP_HEADER(content_type));
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(ok_response),BELLE_SIP_HEADER(content_length));
		belle_sip_message_set_body(BELLE_SIP_MESSAGE(ok_response),sdp,strlen(sdp));
		/*only send ringing*/
jehan's avatar
jehan committed
169
		belle_sip_server_transaction_send_response(server_transaction,ringing_response);
jehan's avatar
jehan committed
170 171 172 173
	} else if (belle_sip_dialog_get_state(dialog) == BELLE_SIP_DIALOG_CONFIRMED) {
		/*time to send bye*/
		belle_sip_client_transaction_t* client_transaction = belle_sip_provider_create_client_transaction(prov,belle_sip_dialog_create_request(dialog,"BYE"));
		belle_sip_client_transaction_send_request(client_transaction);
jehan's avatar
jehan committed
174
	}
175

jehan's avatar
jehan committed
176 177
}

jehan's avatar
jehan committed
178 179
static void caller_process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
	belle_sip_client_transaction_t* client_transaction = belle_sip_response_event_get_client_transaction(event);
180 181 182 183 184 185 186
	belle_sip_header_from_t* from=belle_sip_message_get_header_by_type(belle_sip_response_event_get_response(event),belle_sip_header_from_t);
	belle_sip_header_cseq_t* invite_cseq=belle_sip_message_get_header_by_type(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction)),belle_sip_header_cseq_t);
	belle_sip_request_t* ack;
	if (!belle_sip_uri_equals(BELLE_SIP_URI(user_ctx),belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(from)))) {
		return; /*not for the caller*/
	}

jehan's avatar
jehan committed
187 188 189
	int status = belle_sip_response_get_status_code(belle_sip_response_event_get_response(event));
	belle_sip_message("caller_process_response_event [%i]",status);
	CU_ASSERT_PTR_NOT_NULL_FATAL(client_transaction);
jehan's avatar
jehan committed
190 191
	belle_sip_dialog_t* dialog =  belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(client_transaction));
	CU_ASSERT_PTR_NOT_NULL_FATAL(dialog);
jehan's avatar
jehan committed
192
	CU_ASSERT_PTR_EQUAL(caller_dialog,dialog);
jehan's avatar
jehan committed
193 194 195 196 197 198
	if (belle_sip_dialog_get_state(dialog) == BELLE_SIP_DIALOG_NULL) {
		CU_ASSERT_EQUAL(status,100);
	} else if (belle_sip_dialog_get_state(dialog) == BELLE_SIP_DIALOG_EARLY){
		CU_ASSERT_EQUAL(status,180);
		/*send 200ok from callee*/
		belle_sip_server_transaction_send_response(inserv_transaction,ok_response);
199 200 201
	} else if (belle_sip_dialog_get_state(dialog) == BELLE_SIP_DIALOG_CONFIRMED) {
		ack=belle_sip_dialog_create_ack(dialog,belle_sip_header_cseq_get_seq_number(invite_cseq));
		belle_sip_dialog_send_ack(dialog,ack);
jehan's avatar
jehan committed
202 203
	}

jehan's avatar
jehan committed
204 205 206 207 208


}
static void callee_process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
	/*belle_sip_client_transaction_t* server_transaction = belle_sip_response_event_get_client_transaction(event);*/
209 210 211 212 213 214 215
	belle_sip_header_from_t* from=belle_sip_message_get_header_by_type(belle_sip_response_event_get_response(event),belle_sip_header_from_t);
	int status = belle_sip_response_get_status_code(belle_sip_response_event_get_response(event));
	if (!belle_sip_uri_equals(BELLE_SIP_URI(user_ctx),belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(from)))) {
		return; /*not for the callee*/
	}

	belle_sip_message("callee_process_response_event [%i]",status);
jehan's avatar
jehan committed
216

jehan's avatar
jehan committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}
static void process_timeout(void *user_ctx, const belle_sip_timeout_event_t *event) {
/*	belle_sip_client_transaction_t* client_transaction = belle_sip_timeout_event_get_client_transaction(event);
	SalOp* op = (SalOp*)belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction));
	if (op->callbacks.process_timeout) {
		op->callbacks.process_timeout(op,event);
	} else*/ {
		belle_sip_message("Unhandled event timeout [%p]",event);
	}
}
static void process_transaction_terminated(void *user_ctx, const belle_sip_transaction_terminated_event_t *event) {
/*	belle_sip_client_transaction_t* client_transaction = belle_sip_transaction_terminated_event_get_client_transaction(event);
	SalOp* op = (SalOp*)belle_sip_transaction_get_application_data(client_transaction);
	if (op->calbacks.process_transaction_terminated) {
		op->calbacks.process_transaction_terminated(op,event);
	} else */{
		belle_sip_message("Unhandled transaction terminated [%p]",event);
	}
}

jehan's avatar
jehan committed
237

jehan's avatar
jehan committed
238 239 240 241 242 243 244

static void simple_call(void) {
#define CALLER "marie"
#define CALLEE "pauline"
	belle_sip_request_t *pauline_register_req;
	belle_sip_request_t *marie_register_req;
	belle_sip_listener_callbacks_t caller_listener_callbacks;
jehan's avatar
jehan committed
245
	belle_sip_listener_t* caller_listener;
jehan's avatar
jehan committed
246
	belle_sip_listener_callbacks_t callee_listener_callbacks;
jehan's avatar
jehan committed
247
	belle_sip_listener_t* callee_listener;
jehan's avatar
jehan committed
248 249 250 251 252 253 254 255 256 257 258
	belle_sip_request_t* req;
	belle_sip_header_address_t* from;
	belle_sip_header_address_t* to;
	belle_sip_header_address_t* route;
	belle_sip_header_allow_t* header_allow;
	belle_sip_header_content_type_t* content_type ;
	belle_sip_header_content_length_t* content_length;
	belle_sip_client_transaction_t* client_transaction;

	caller_listener_callbacks.process_dialog_terminated=process_dialog_terminated;
	caller_listener_callbacks.process_io_error=process_io_error;
jehan's avatar
jehan committed
259
	caller_listener_callbacks.process_request_event=caller_process_request_event;
jehan's avatar
jehan committed
260
	caller_listener_callbacks.process_response_event=caller_process_response_event;
jehan's avatar
jehan committed
261 262 263 264 265
	caller_listener_callbacks.process_timeout=process_timeout;
	caller_listener_callbacks.process_transaction_terminated=process_transaction_terminated;

	callee_listener_callbacks.process_dialog_terminated=process_dialog_terminated;
	callee_listener_callbacks.process_io_error=process_io_error;
266
	callee_listener_callbacks.process_request_event=callee_process_request_event;
jehan's avatar
jehan committed
267
	callee_listener_callbacks.process_response_event=callee_process_response_event;
jehan's avatar
jehan committed
268 269 270 271 272 273 274 275
	callee_listener_callbacks.process_timeout=process_timeout;
	callee_listener_callbacks.process_transaction_terminated=process_transaction_terminated;

	pauline_register_req=register_user(stack, prov, "TCP" ,1 ,CALLER);
	marie_register_req=register_user(stack, prov, "TLS" ,1 ,CALLEE);

	from=belle_sip_header_address_create(NULL,belle_sip_uri_create(CALLER,test_domain));
	to=belle_sip_header_address_create(NULL,belle_sip_uri_create(CALLEE,test_domain));
276 277 278 279 280

	belle_sip_provider_add_sip_listener(prov,caller_listener=belle_sip_listener_create_from_callbacks(&caller_listener_callbacks,belle_sip_object_ref(belle_sip_header_address_get_uri((belle_sip_header_address_t*)from))));
	belle_sip_provider_add_sip_listener(prov,callee_listener=belle_sip_listener_create_from_callbacks(&callee_listener_callbacks,belle_sip_object_ref(belle_sip_header_address_get_uri((belle_sip_header_address_t*)to))));


jehan's avatar
jehan committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	route = belle_sip_header_address_create(NULL,belle_sip_uri_create(NULL,test_domain));
	belle_sip_uri_set_transport_param(belle_sip_header_address_get_uri(route),"tcp");

	req=build_request(stack,prov,from,to,route,"INVITE");
	header_allow = belle_sip_header_allow_create("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(header_allow));

	content_length= belle_sip_header_content_length_create(strlen(sdp));
	content_type = belle_sip_header_content_type_create("application","sdp");
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(content_type));
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(content_length));
	belle_sip_message_set_body(BELLE_SIP_MESSAGE(req),sdp,strlen(sdp));


	client_transaction = belle_sip_provider_create_client_transaction(prov,req);
jehan's avatar
jehan committed
296
	caller_dialog=belle_sip_provider_create_dialog(prov,BELLE_SIP_TRANSACTION(client_transaction));
jehan's avatar
jehan committed
297
	CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(client_transaction)));
jehan's avatar
jehan committed
298
	//belle_sip_transaction_set_application_data(BELLE_SIP_TRANSACTION(client_transaction),op);
jehan's avatar
jehan committed
299
	call_endeed=0;
jehan's avatar
jehan committed
300
	belle_sip_client_transaction_send_request(client_transaction);
jehan's avatar
jehan committed
301 302 303
	int i=0;
	for(i=0;i<10 &&!call_endeed;i++)
		belle_sip_stack_sleep(stack,3000);
jehan's avatar
jehan committed
304

jehan's avatar
jehan committed
305 306 307
	belle_sip_provider_remove_sip_listener(prov,caller_listener);
	belle_sip_provider_remove_sip_listener(prov,callee_listener);

jehan's avatar
jehan committed
308 309 310 311 312 313 314 315 316 317 318 319
	unregister_user(stack, prov, pauline_register_req ,1);
	unregister_user(stack, prov, marie_register_req ,1);
}
int belle_sip_dialog_test_suite(){
	CU_pSuite pSuite = CU_add_suite("Dialog", init, uninit);

	if (NULL == CU_add_test(pSuite, "simple call", simple_call)) {
		return CU_get_error();
	}
	return 0;
}