Commit 37ccabae authored by jehan's avatar jehan

add cunit test

renable belle_sip
parent 132430e2
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>parser-antlr</name>
<name>belle-sip</name>
<comment></comment>
<projects>
</projects>
......
......@@ -5,155 +5,155 @@
* Author: jehanmonnier
*/
#ifndef SIPURI_H_
#define SIPURI_H_
#ifndef BELLE_SIP_URI_H_
#define BELLE_SIP_URI_H_
typedef struct _sip_uri sip_uri;
typedef struct _belle_sip_uri belle_sip_uri;
sip_uri* sip_uri_new();
belle_sip_uri* belle_sip_uri_new();
void sip_uri_delete(sip_uri* uri);
sip_uri* sip_uri_parse (const char* uri) ;
void belle_sip_uri_delete(belle_sip_uri* uri);
belle_sip_uri* belle_sip_uri_parse (const char* uri) ;
/**
* Returns the value of the named header, or null if it is not set.
*
*/
const char* sip_uri_get_header(sip_uri* uri,const char* name);
const char* belle_sip_uri_get_header(belle_sip_uri* uri,const char* name);
/**
* Returns an Iterator over the const char*names of all headers present in this SipURI.
*
*/
/*list<const char*>::iterator sip_uri_get_header_names(SipUri* uri) ;*/
/*list<const char*>::iterator belle_sip_uri_get_header_names(SipUri* uri) ;*/
/**
* Returns the host part of this SipURI.
*
*/
const char* sip_uri_get_host(sip_uri* uri) ;
const char* belle_sip_uri_get_host(belle_sip_uri* uri) ;
/**
* Returns the value of the maddr parameter, or null if this is not set.
*
*/
const char* sip_uri_get_maddr_param(sip_uri* uri) ;
const char* belle_sip_uri_get_maddr_param(belle_sip_uri* uri) ;
/**
* Returns the value of the method parameter, or null if this is not set.
*
*/
const char* sip_uri_get_method_param(sip_uri* uri) ;
const char* belle_sip_uri_get_method_param(belle_sip_uri* uri) ;
/**
* Returns the port part of this SipURI.
*
*/
unsigned short sip_uri_get_port(sip_uri* uri) ;
unsigned int belle_sip_uri_get_port(belle_sip_uri* uri) ;
/**
* Returns the value of the "transport" parameter, or null if this is not set.
*
*/
const char* sip_uri_get_transport_param(sip_uri* uri) ;
const char* belle_sip_uri_get_transport_param(belle_sip_uri* uri) ;
/**
* Returns the value of the "ttl" parameter, or -1 if this is not set.
*
*/
int sip_uri_get_ttl_param(sip_uri* uri) ;
int belle_sip_uri_get_ttl_param(belle_sip_uri* uri) ;
/**
* Returns the user part of this SipURI.
*
*/
const char* sip_uri_get_user(sip_uri* uri) ;
const char* belle_sip_uri_get_user(belle_sip_uri* uri) ;
/**
* Returns the value of the userParam, or null if this is not set.
*
*/
const char* sip_uri_get_user_param(sip_uri* uri) ;
const char* belle_sip_uri_get_user_param(belle_sip_uri* uri) ;
/**
* Gets user password of SipURI, or null if it is not set.
*
*/
const char* sip_uri_get_user_password(sip_uri* uri) ;
const char* belle_sip_uri_get_user_password(belle_sip_uri* uri) ;
/**
* Returns whether the the lr parameter is set.
*
*/
unsigned int sip_uri_has_lr_param(sip_uri* uri) ;
unsigned int belle_sip_uri_has_lr_param(belle_sip_uri* uri) ;
/**
*
* Returns true if this SipURI is secure i.e. if this SipURI represents a sips URI.
*
*/
unsigned int sip_uri_is_secure(sip_uri* uri) ;
unsigned int belle_sip_uri_is_secure(belle_sip_uri* uri) ;
/**
* Removes the port part of this SipURI.
*
*/
void sip_uri_remove_port(sip_uri* uri) ;
void belle_sip_uri_remove_port(belle_sip_uri* uri) ;
/**
* Sets the value of the specified header fields to be included in a request constructed from the URI.
*
*/
void sip_uri_set_header(sip_uri* uri,const char*name, const char*value) ;
void belle_sip_uri_set_header(belle_sip_uri* uri,const char*name, const char*value) ;
/**
* Set the host part of this SipURI to the newly supplied host parameter.
*
*/
void sip_uri_set_host(sip_uri* uri,const char*host) ;
void belle_sip_uri_set_host(belle_sip_uri* uri,const char*host) ;
/**
* Sets the value of the lr parameter of this SipURI.
*
*/
void sip_uri_set_lr_param(sip_uri* uri) ;
void belle_sip_uri_set_lr_param(belle_sip_uri* uri) ;
/**
* Sets the value of the maddr parameter of this SipURI.
*
*/
void sip_uri_set_maddr_param(sip_uri* uri,const char*mAddr) ;
void belle_sip_uri_set_maddr_param(belle_sip_uri* uri,const char*mAddr) ;
/**
* Sets the value of the method parameter.
*
*/
void sip_uri_set_method_param(sip_uri* uri,const char*method) ;
void belle_sip_uri_set_method_param(belle_sip_uri* uri,const char*method) ;
/**
* Set the port part of this SipURI to the newly supplied port parameter.
*
*/
void sip_uri_set_port(sip_uri* uri,unsigned short port) ;
void belle_sip_uri_set_port(belle_sip_uri* uri,unsigned int port) ;
/**
* Sets the scheme of this URI to sip or sips depending on whether the argument is true or false.
*
*/
void sip_uri_set_secure(sip_uri* uri,unsigned secure) ;
void belle_sip_uri_set_secure(belle_sip_uri* uri,unsigned secure) ;
/**
* Sets the value of the "transport" parameter.
*
*/
void sip_uri_set_transport_param(sip_uri* uri,const char*transport) ;
void belle_sip_uri_set_transport_param(belle_sip_uri* uri,const char*transport) ;
/**
* Sets the value of the ttl parameter.
*
*/
void sip_uri_set_ttl_param(sip_uri* uri,int ttl) ;
void belle_sip_uri_set_ttl_param(belle_sip_uri* uri,int ttl) ;
/**
* Sets the user of SipURI.
*
*/
void sip_uri_set_user(sip_uri* uri,const char*user) ;
void belle_sip_uri_set_user(belle_sip_uri* uri,const char*user) ;
/**
* Sets the value of the user parameter.
*
*/
void sip_uri_set_user_param(sip_uri* uri,const char*userParam) ;
void belle_sip_uri_set_user_param(belle_sip_uri* uri,const char*userParam) ;
/**
* Sets the user password associated with the user of SipURI.
*
*/
void sip_uri_set_user_password(sip_uri* uri,const char*userPassword) ;
void belle_sip_uri_set_user_password(belle_sip_uri* uri,const char*userPassword) ;
/**
* This method returns the URI as a string.
*
*/
char* sip_uri_to_string(sip_uri* uri) ;
char* belle_sip_uri_to_string(belle_sip_uri* uri) ;
#endif /*SIPURI_H_*/
#endif /*BELLE_SIP_URI_H_*/
grammar sipuri;
grammar belle_sip_uri;
options { language = C;}
@header { #include "sip_uri.h"}
@header { #include "belle_sip_uri.h"}
an_sip_uri returns [sip_uri* ret]
scope { sip_uri* current; }
@init { $an_sip_uri::current = sip_uri_new(); }
an_sip_uri returns [belle_sip_uri* ret]
scope { belle_sip_uri* current; }
@init { $an_sip_uri::current = belle_sip_uri_new(); }
: 'sip:' userinfo? hostport uri_parameters {$ret = $an_sip_uri::current;};
userinfo : user ( ':' password )? '@' ;
user : ( unreserved | user_unreserved )+ {sip_uri_set_user($an_sip_uri::current,(const char *)$text->chars);};
user : ( unreserved | user_unreserved )+ {belle_sip_uri_set_user($an_sip_uri::current,(const char *)$text->chars);};
user_unreserved : '&' | '=' | '+' | '$' | ',' | ';' | '?' | '/';
password : ( unreserved |'&' | '=' | '+' | '$' | ',' )*;
hostport : host ( ':' port )?;
host : hostname {sip_uri_set_host($an_sip_uri::current,(const char *)$text->chars);};
host : hostname {belle_sip_uri_set_host($an_sip_uri::current,(const char *)$text->chars);};
hostname : ( domainlabel '.' )* toplabel '.'? ;
domainlabel : alphanum | alphanum ( alphanum | '-' )* alphanum ;
toplabel : ALPHA | ALPHA ( alphanum | '-' )* alphanum;
port : DIGIT+ ;
port : DIGIT+ {belle_sip_uri_set_port($an_sip_uri::current,atoi((const char *)$text->chars));};
uri_parameters
......@@ -28,7 +28,7 @@ uri_parameter
transport_param
: 'transport=' transport_value;
transport_value: ('udp' | 'tcp' | 'sctp' | 'tls'| other_transport)
{sip_uri_set_transport_param($an_sip_uri::current,(const char *)$text->chars);};
{belle_sip_uri_set_transport_param($an_sip_uri::current,(const char *)$text->chars);};
other_transport
: token ;
user_param
......
......@@ -5,12 +5,12 @@
* Author: jehanmonnier
*/
#include "sip_uri.h"
#include "belle_sip_uri.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "sipuriParser.h"
#include "sipuriLexer.h"
#include <stdarg.h>
#include "belle_sip_uriParser.h"
#include "belle_sip_uriLexer.h"
#define GET_SET_STRING(object_type,attribute) \
const char* object_type##_get_##attribute (object_type* obj) {\
......@@ -22,42 +22,43 @@
strcpy((char*)(obj->attribute),value);\
}
#define SIP_URI_GET_SET_STRING(attribute) GET_SET_STRING(sip_uri,attribute)
#define SIP_URI_GET_SET_STRING(attribute) GET_SET_STRING(belle_sip_uri,attribute)
#define GET_SET_UINT(object_type,attribute) \
unsigned int* object_type##_get_##attribute (object_type* obj) {\
unsigned int object_type##_get_##attribute (object_type* obj) {\
return obj->attribute;\
}\
void object_type##_set_##attribute (object_type* obj,unsigned int value) {\
obj->attribute=value;\
}
#define SIP_URI_GET_SET_UINT(attribute) GET_SET_UINT(sip_uri,attribute)
#define SIP_URI_GET_SET_UINT(attribute) GET_SET_UINT(belle_sip_uri,attribute)
struct _sip_uri {
struct _belle_sip_uri {
const char* user;
const char* host;
const char* transport_param;
unsigned int port;
};
sip_uri* sip_uri_parse (const char* uri) {
belle_sip_uri* belle_sip_uri_parse (const char* uri) {
pANTLR3_INPUT_STREAM input;
psipuriLexer lex;
pbelle_sip_uriLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
psipuriParser parser;
pbelle_sip_uriParser parser;
input = antlr3NewAsciiStringCopyStream (
(pANTLR3_UINT8)uri,
(ANTLR3_UINT32)strlen(uri),
NULL);
lex = sipuriLexerNew (input);
lex = belle_sip_uriLexerNew (input);
tokens = antlr3CommonTokenStreamSourceNew (ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
parser = sipuriParserNew (tokens);
parser = belle_sip_uriParserNew (tokens);
sip_uri* l_parsed_uri = parser->an_sip_uri(parser);
belle_sip_uri* l_parsed_uri = parser->an_sip_uri(parser);
// Must manually clean up
//
......@@ -67,12 +68,12 @@ sip_uri* sip_uri_parse (const char* uri) {
input ->close(input);
return l_parsed_uri;
}
sip_uri* sip_uri_new () {
sip_uri* lUri = (sip_uri*)malloc(sizeof(sip_uri));
memset(lUri,0,sizeof(sip_uri));
belle_sip_uri* belle_sip_uri_new () {
belle_sip_uri* lUri = (belle_sip_uri*)malloc(sizeof(belle_sip_uri));
memset(lUri,0,sizeof(belle_sip_uri));
return lUri;
}
void sip_uri_delete(sip_uri* uri) {
void belle_sip_uri_delete(belle_sip_uri* uri) {
free(uri);
}
......@@ -123,7 +124,7 @@ static char * concat (const char *str, ...) {
return result;
}
char* sip_uri_to_string(sip_uri* uri) {
char* belle_sip_uri_to_string(belle_sip_uri* uri) {
return concat( "sip:"
,(uri->user?uri->user:"")
,(uri->user?"@":"")
......@@ -136,4 +137,4 @@ char* sip_uri_to_string(sip_uri* uri) {
SIP_URI_GET_SET_STRING(user)
SIP_URI_GET_SET_STRING(host)
SIP_URI_GET_SET_STRING(transport_param)
SIP_URI_GET_SET_UINT(port)
//============================================================================
// Name : parser-antlr.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "sip_uri.h"
#include <stdio.h>
int main (int argc, char *argv[]) {
sip_uri* L_uri = sip_uri_parse("sip:toto@titi.com:5060;transport=tcp");
printf(sip_uri_to_string(L_uri));
return 0;
}
//============================================================================
// Name : parser-antlr.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "belle_sip_uri.h"
#include <stdio.h>
#include "CUnit/Basic.h"
int init_suite1(void) {
return 0;
}
int clean_suite1(void) {
return 0;
}
void testSIMPLEURI(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:titi.com");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_user(L_uri));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_transport_param(L_uri));
}
void testCOMPLEXURI(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:toto@titi.com:5060;transport=tcp");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "toto");
CU_ASSERT_EQUAL(belle_sip_uri_get_port(L_uri), 5060);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "tcp");
}
int main (int argc, char *argv[]) {
CU_pSuite pSuite = NULL;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
/* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
if ((NULL == CU_add_test(pSuite, "test of simple uri", testSIMPLEURI)) ||
(NULL == CU_add_test(pSuite, "test of complex uri", testCOMPLEXURI)))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment