belle_sip_register_tester.c 5.06 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 23 24 25 26
#include "pthread.h"
static int is_register_ok;
static belle_sip_stack_t * stack;
#define TEST_DOMAIN "localhost"
/*#define TEST_DOMAIN "test.linphone.org"*/
jehan's avatar
jehan committed
27

Simon Morlat's avatar
Simon Morlat committed
28 29 30 31 32 33 34 35 36 37 38
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
39 40 41
	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
42
	belle_sip_object_unref(belle_sip_response_event_get_response(event));
jehan's avatar
jehan committed
43
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
Simon Morlat's avatar
Simon Morlat committed
44 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
}
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);
jehan's avatar
jehan committed
80 81 82 83 84 85 86 87 88
static int init(void) {
	stack=belle_sip_stack_new(NULL);
	return 0;
}
static int uninit(void) {
	belle_sip_object_unref(stack);
	return 0;
}
static void register_test(belle_sip_listening_point_t* lp,belle_sip_uri_t* uri) {
Simon Morlat's avatar
Simon Morlat committed
89 90


Simon Morlat's avatar
Simon Morlat committed
91 92
	belle_sip_provider_t *prov;
	belle_sip_request_t *req;
Simon Morlat's avatar
Simon Morlat committed
93
	test_listener_t *listener=belle_sip_object_new(test_listener_t);
Simon Morlat's avatar
Simon Morlat committed
94

Simon Morlat's avatar
Simon Morlat committed
95
	belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
jehan's avatar
jehan committed
96

Simon Morlat's avatar
Simon Morlat committed
97
	prov=belle_sip_stack_create_provider(stack,lp);
Simon Morlat's avatar
Simon Morlat committed
98
	belle_sip_provider_add_sip_listener(prov,BELLE_SIP_LISTENER(listener));
Simon Morlat's avatar
Simon Morlat committed
99
	req=belle_sip_request_create(
jehan's avatar
jehan committed
100
	                    uri,
Simon Morlat's avatar
Simon Morlat committed
101 102 103
	                    "REGISTER",
	                    belle_sip_provider_create_call_id(prov),
	                    belle_sip_header_cseq_create(20,"REGISTER"),
jehan's avatar
jehan committed
104 105
	                    belle_sip_header_from_create("Tester <sip:tester@" TEST_DOMAIN ">","a0dke45"),
	                    belle_sip_header_to_create("Tester <sip:tester@" TEST_DOMAIN ">",NULL),
106
	                    belle_sip_header_via_new(),
Simon Morlat's avatar
Simon Morlat committed
107 108
	                    70);

jehan's avatar
jehan committed
109 110
	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
111
	belle_sip_provider_send_request(prov,req);
Simon Morlat's avatar
Simon Morlat committed
112
	belle_sip_stack_sleep(stack,25000);
jehan's avatar
jehan committed
113
	CU_ASSERT_EQUAL(is_register_ok,1);
Simon Morlat's avatar
Simon Morlat committed
114
	belle_sip_object_unref(prov);
jehan's avatar
jehan committed
115
	belle_sip_object_unref(listener);
jehan's avatar
jehan committed
116

jehan's avatar
jehan committed
117
	return;
Simon Morlat's avatar
Simon Morlat committed
118
}
jehan's avatar
jehan committed
119 120 121 122 123 124 125 126 127 128 129 130 131
static void register_udp_test() {
	belle_sip_listening_point_t *lp;
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
	CU_ASSERT_PTR_NOT_NULL_FATAL(lp);
	register_test(lp,belle_sip_uri_parse("sip:"TEST_DOMAIN));
}
static void register_tcp_test() {
	belle_sip_listening_point_t *lp;
	lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"TCP");
	CU_ASSERT_PTR_NOT_NULL_FATAL(lp);
	register_test(lp,belle_sip_uri_parse("sip:"TEST_DOMAIN";transport=tcp"));
}

jehan's avatar
jehan committed
132
int belle_sip_register_test_suite(){
jehan's avatar
jehan committed
133
	CU_pSuite pSuite = CU_add_suite("Register test suite", init, uninit);
jehan's avatar
jehan committed
134

jehan's avatar
jehan committed
135 136 137 138
	if (NULL == CU_add_test(pSuite, "simple udp register", register_udp_test)) {
		return CU_get_error();
	}
	if (NULL == CU_add_test(pSuite, "simple tcp register", register_tcp_test)) {
jehan's avatar
jehan committed
139 140
		return CU_get_error();
	}
jehan's avatar
jehan committed
141
	return 0;
jehan's avatar
jehan committed
142 143
}