belle_sip_register_tester.c 6.73 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"
jehan's avatar
jehan committed
22
#include "pthread.h"
Simon Morlat's avatar
Simon Morlat committed
23

Simon Morlat's avatar
Simon Morlat committed
24
const char *test_domain="test.linphone.org";
jehan's avatar
jehan committed
25
static int is_register_ok;
26
static int using_transaction;
jehan's avatar
jehan committed
27
static belle_sip_stack_t * stack;
Simon Morlat's avatar
Simon Morlat committed
28
static belle_sip_provider_t *prov;
jehan's avatar
jehan committed
29

Simon Morlat's avatar
Simon Morlat committed
30 31 32 33
static void process_dialog_terminated(belle_sip_listener_t *obj, const belle_sip_dialog_terminated_event_t *event){
	belle_sip_message("process_dialog_terminated called");
}
static void process_io_error(belle_sip_listener_t *obj, const belle_sip_io_error_event_t *event){
34 35 36
	belle_sip_warning("process_io_error");
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
	/*CU_ASSERT(CU_FALSE);*/
Simon Morlat's avatar
Simon Morlat committed
37 38 39 40 41 42
}
static void process_request_event(belle_sip_listener_t *obj, const belle_sip_request_event_t *event){
	belle_sip_message("process_request_event");
}
static void process_response_event(belle_sip_listener_t *obj, const belle_sip_response_event_t *event){
	belle_sip_message("process_response_event");
jehan's avatar
jehan committed
43 44 45
	CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_response_event_get_response(event));
	CU_ASSERT_EQUAL(belle_sip_response_get_status_code(belle_sip_response_event_get_response(event)),200);
	is_register_ok=1;
46
	using_transaction=belle_sip_response_event_get_client_transaction(event)!=NULL;
jehan's avatar
jehan committed
47
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
Simon Morlat's avatar
Simon Morlat committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
}
static void process_timeout(belle_sip_listener_t *obj, const belle_sip_timeout_event_t *event){
	belle_sip_message("process_timeout");
}
static void process_transaction_terminated(belle_sip_listener_t *obj, const belle_sip_transaction_terminated_event_t *event){
	belle_sip_message("process_transaction_terminated");
}

/*this would normally go to a .h file*/
struct test_listener{
	belle_sip_object_t base;
	void *some_context;
};

typedef struct test_listener test_listener_t;

BELLE_SIP_DECLARE_TYPES_BEGIN(test,0x1000)
	BELLE_SIP_TYPE_ID(test_listener_t)
BELLE_SIP_DECLARE_TYPES_END

BELLE_SIP_DECLARE_VPTR(test_listener_t);

/*the following would go to .c file */

BELLE_SIP_IMPLEMENT_INTERFACE_BEGIN(test_listener_t,belle_sip_listener_t)
	process_dialog_terminated,
	process_io_error,
	process_request_event,
	process_response_event,
	process_timeout,
	process_transaction_terminated
BELLE_SIP_IMPLEMENT_INTERFACE_END

BELLE_SIP_DECLARE_IMPLEMENTED_INTERFACES_1(test_listener_t,belle_sip_listener_t);

BELLE_SIP_INSTANCIATE_VPTR(test_listener_t,belle_sip_object_t,NULL,NULL,NULL,FALSE);
Simon Morlat's avatar
Simon Morlat committed
84 85 86

static test_listener_t *listener;

jehan's avatar
jehan committed
87 88
static int init(void) {
	stack=belle_sip_stack_new(NULL);
Simon Morlat's avatar
Simon Morlat committed
89 90 91 92 93 94
	belle_sip_listening_point_t *lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
	prov=belle_sip_stack_create_provider(stack,lp);
	belle_sip_object_unref(lp);
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"TCP");
	belle_sip_provider_add_listening_point(prov,lp);
	belle_sip_object_unref(lp);
95
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7061,"TLS");
jehan's avatar
jehan committed
96 97 98 99
	if (lp) {
		belle_sip_provider_add_listening_point(prov,lp);
		belle_sip_object_unref(lp);
	}
Simon Morlat's avatar
Simon Morlat committed
100 101
	listener=belle_sip_object_new(test_listener_t);
	belle_sip_provider_add_sip_listener(prov,BELLE_SIP_LISTENER(listener));
jehan's avatar
jehan committed
102 103 104
	return 0;
}
static int uninit(void) {
Simon Morlat's avatar
Simon Morlat committed
105
	belle_sip_object_unref(prov);
jehan's avatar
jehan committed
106
	belle_sip_object_unref(stack);
Simon Morlat's avatar
Simon Morlat committed
107
	belle_sip_object_unref(listener);
jehan's avatar
jehan committed
108 109
	return 0;
}
Simon Morlat's avatar
Simon Morlat committed
110
static void register_test(const char *transport, int use_transaction) {
Simon Morlat's avatar
Simon Morlat committed
111
	belle_sip_request_t *req;
Simon Morlat's avatar
Simon Morlat committed
112 113 114
	char token[10];
	char identity[256];
	char uri[256];
Simon Morlat's avatar
Simon Morlat committed
115

Simon Morlat's avatar
Simon Morlat committed
116 117 118
	if (transport)
		snprintf(uri,sizeof(uri),"sip:%s;transport=%s",test_domain,transport);
	else snprintf(uri,sizeof(uri),"sip:%s",test_domain);
jehan's avatar
jehan committed
119

Simon Morlat's avatar
Simon Morlat committed
120 121 122 123 124
	if (transport && strcasecmp("tls",transport)==0 && belle_sip_provider_get_listening_point(prov,"tls")==NULL){
		belle_sip_error("No TLS support, test skipped.");
		return;
	}

Simon Morlat's avatar
Simon Morlat committed
125
	snprintf(identity,sizeof(identity),"Tester <sip:tester@%s>",test_domain);
Simon Morlat's avatar
Simon Morlat committed
126
	req=belle_sip_request_create(
Simon Morlat's avatar
Simon Morlat committed
127
	                    belle_sip_uri_parse(uri),
Simon Morlat's avatar
Simon Morlat committed
128 129 130
	                    "REGISTER",
	                    belle_sip_provider_create_call_id(prov),
	                    belle_sip_header_cseq_create(20,"REGISTER"),
Simon Morlat's avatar
Simon Morlat committed
131 132
	                    belle_sip_header_from_create(identity,belle_sip_random_token(token,sizeof(token))),
	                    belle_sip_header_to_create(identity,NULL),
133
	                    belle_sip_header_via_new(),
Simon Morlat's avatar
Simon Morlat committed
134 135
	                    70);

jehan's avatar
jehan committed
136
	is_register_ok=0;
137
	using_transaction=0;
jehan's avatar
jehan committed
138
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_expires_create(600)));
Simon Morlat's avatar
Simon Morlat committed
139 140 141 142 143 144
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_contact_new()));
	if (use_transaction){
		belle_sip_client_transaction_t *t=belle_sip_provider_create_client_transaction(prov,req);
		belle_sip_client_transaction_send_request(t);
	}else belle_sip_provider_send_request(prov,req);
	belle_sip_stack_sleep(stack,33000);
jehan's avatar
jehan committed
145
	CU_ASSERT_EQUAL(is_register_ok,1);
146
	CU_ASSERT_EQUAL(using_transaction,use_transaction);
jehan's avatar
jehan committed
147
	return;
Simon Morlat's avatar
Simon Morlat committed
148
}
Simon Morlat's avatar
Simon Morlat committed
149 150 151 152


static void stateless_register_udp(void){
	register_test(NULL,0);
jehan's avatar
jehan committed
153
}
Simon Morlat's avatar
Simon Morlat committed
154

jehan's avatar
jehan committed
155 156 157 158
static void stateless_register_tls(void){
	register_test("tls",0);
}

Simon Morlat's avatar
Simon Morlat committed
159 160 161 162 163 164 165 166 167 168
static void stateless_register_tcp(void){
	register_test("tcp",0);
}

static void stateful_register_udp(void){
	register_test(NULL,1);
}

static void stateful_register_tcp(void){
	register_test("tcp",1);
jehan's avatar
jehan committed
169
}
170 171 172
static void stateful_register_tls(void){
	register_test("tls",1);
}
jehan's avatar
jehan committed
173

jehan's avatar
jehan committed
174
int belle_sip_register_test_suite(){
jehan's avatar
jehan committed
175
	CU_pSuite pSuite = CU_add_suite("Register test suite", init, uninit);
jehan's avatar
jehan committed
176

Simon Morlat's avatar
Simon Morlat committed
177 178 179 180 181 182
	if (NULL == CU_add_test(pSuite, "stateful udp register", stateful_register_udp)) {
		return CU_get_error();
	}
	if (NULL == CU_add_test(pSuite, "stateful tcp register", stateful_register_tcp)) {
		return CU_get_error();
	}
183 184 185
	if (NULL == CU_add_test(pSuite, "stateful tls register", stateful_register_tls)) {
		return CU_get_error();
	}
Simon Morlat's avatar
Simon Morlat committed
186
	if (NULL == CU_add_test(pSuite, "stateless udp register", stateless_register_udp)) {
jehan's avatar
jehan committed
187 188
		return CU_get_error();
	}
Simon Morlat's avatar
Simon Morlat committed
189
	if (NULL == CU_add_test(pSuite, "stateless tcp register", stateless_register_tcp)) {
jehan's avatar
jehan committed
190 191
		return CU_get_error();
	}
jehan's avatar
jehan committed
192 193 194
	if (NULL == CU_add_test(pSuite, "stateless tls register", stateless_register_tls)) {
			return CU_get_error();
	}
jehan's avatar
jehan committed
195
	return 0;
jehan's avatar
jehan committed
196 197
}