belle_sip_register_tester.c 5.83 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 24

const char *test_domain="localhost";
jehan's avatar
jehan committed
25 26
static int is_register_ok;
static belle_sip_stack_t * stack;
Simon Morlat's avatar
Simon Morlat committed
27
static belle_sip_provider_t *prov;
jehan's avatar
jehan committed
28

Simon Morlat's avatar
Simon Morlat committed
29 30 31 32 33 34 35 36 37 38 39
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){
	belle_sip_message("process_io_error");
}
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
40 41 42
	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;
jehan's avatar
jehan committed
43
	belle_sip_object_unref(belle_sip_response_event_get_response(event));
jehan's avatar
jehan committed
44
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
Simon Morlat's avatar
Simon Morlat committed
45 46 47 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
}
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
81 82 83

static test_listener_t *listener;

jehan's avatar
jehan committed
84 85
static int init(void) {
	stack=belle_sip_stack_new(NULL);
Simon Morlat's avatar
Simon Morlat committed
86 87 88 89 90 91 92 93
	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);
	listener=belle_sip_object_new(test_listener_t);
	belle_sip_provider_add_sip_listener(prov,BELLE_SIP_LISTENER(listener));
jehan's avatar
jehan committed
94 95 96
	return 0;
}
static int uninit(void) {
Simon Morlat's avatar
Simon Morlat committed
97
	belle_sip_object_unref(prov);
jehan's avatar
jehan committed
98
	belle_sip_object_unref(stack);
Simon Morlat's avatar
Simon Morlat committed
99
	belle_sip_object_unref(listener);
jehan's avatar
jehan committed
100 101
	return 0;
}
Simon Morlat's avatar
Simon Morlat committed
102
static void register_test(const char *transport, int use_transaction) {
Simon Morlat's avatar
Simon Morlat committed
103
	belle_sip_request_t *req;
Simon Morlat's avatar
Simon Morlat committed
104 105 106
	char token[10];
	char identity[256];
	char uri[256];
Simon Morlat's avatar
Simon Morlat committed
107

Simon Morlat's avatar
Simon Morlat committed
108 109 110
	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
111

Simon Morlat's avatar
Simon Morlat committed
112
	snprintf(identity,sizeof(identity),"Tester <sip:tester@%s>",test_domain);
Simon Morlat's avatar
Simon Morlat committed
113
	req=belle_sip_request_create(
Simon Morlat's avatar
Simon Morlat committed
114
	                    belle_sip_uri_parse(uri),
Simon Morlat's avatar
Simon Morlat committed
115 116 117
	                    "REGISTER",
	                    belle_sip_provider_create_call_id(prov),
	                    belle_sip_header_cseq_create(20,"REGISTER"),
Simon Morlat's avatar
Simon Morlat committed
118 119
	                    belle_sip_header_from_create(identity,belle_sip_random_token(token,sizeof(token))),
	                    belle_sip_header_to_create(identity,NULL),
120
	                    belle_sip_header_via_new(),
Simon Morlat's avatar
Simon Morlat committed
121 122
	                    70);

jehan's avatar
jehan committed
123 124
	is_register_ok=0;
	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
125 126 127 128 129 130
	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
131
	CU_ASSERT_EQUAL(is_register_ok,1);
jehan's avatar
jehan committed
132
	return;
Simon Morlat's avatar
Simon Morlat committed
133
}
Simon Morlat's avatar
Simon Morlat committed
134 135 136 137


static void stateless_register_udp(void){
	register_test(NULL,0);
jehan's avatar
jehan committed
138
}
Simon Morlat's avatar
Simon Morlat committed
139 140 141 142 143 144 145 146 147 148 149

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
150 151
}

jehan's avatar
jehan committed
152
int belle_sip_register_test_suite(){
jehan's avatar
jehan committed
153
	CU_pSuite pSuite = CU_add_suite("Register test suite", init, uninit);
jehan's avatar
jehan committed
154

Simon Morlat's avatar
Simon Morlat committed
155 156 157 158 159 160 161 162
	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();
	}

	if (NULL == CU_add_test(pSuite, "stateless udp register", stateless_register_udp)) {
jehan's avatar
jehan committed
163 164
		return CU_get_error();
	}
Simon Morlat's avatar
Simon Morlat committed
165
	if (NULL == CU_add_test(pSuite, "stateless tcp register", stateless_register_tcp)) {
jehan's avatar
jehan committed
166 167
		return CU_get_error();
	}
jehan's avatar
jehan committed
168
	return 0;
jehan's avatar
jehan committed
169 170
}