belle_sip_register_tester.c 5.94 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
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 34 35 36 37 38 39 40
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
41 42 43
	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;
44
	using_transaction=belle_sip_response_event_get_client_transaction(event)!=NULL;
jehan's avatar
jehan committed
45
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
Simon Morlat's avatar
Simon Morlat committed
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 81
}
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
82 83 84

static test_listener_t *listener;

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

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

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

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


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

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
153 154
}

jehan's avatar
jehan committed
155
int belle_sip_register_test_suite(){
jehan's avatar
jehan committed
156
	CU_pSuite pSuite = CU_add_suite("Register test suite", init, uninit);
jehan's avatar
jehan committed
157

Simon Morlat's avatar
Simon Morlat committed
158 159 160 161 162 163 164 165
	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
166 167
		return CU_get_error();
	}
Simon Morlat's avatar
Simon Morlat committed
168
	if (NULL == CU_add_test(pSuite, "stateless tcp register", stateless_register_tcp)) {
jehan's avatar
jehan committed
169 170
		return CU_get_error();
	}
jehan's avatar
jehan committed
171
	return 0;
jehan's avatar
jehan committed
172 173
}