⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg_test.c

📁 一个开源SIP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: msg_test.c 1081 2007-03-19 10:24:23Z bennylp $ */
/* 
 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include "test.h"
#include <pjsip.h>
#include <pjlib.h>

#define POOL_SIZE	8000
#if defined(PJ_DEBUG) && PJ_DEBUG!=0
#   define LOOP		10000
#else
#   define LOOP		100000
#endif
#define AVERAGE_MSG_LEN	800
#define THIS_FILE	"msg_test.c"

static pjsip_msg *create_msg0(pj_pool_t *pool);
static pjsip_msg *create_msg1(pj_pool_t *pool);

#define STATUS_PARTIAL		1
#define STATUS_SYNTAX_ERROR	2

#define FLAG_DETECT_ONLY	1
#define FLAG_PARSE_ONLY		4
#define FLAG_PRINT_ONLY		8

struct test_msg
{
    char	 msg[1024];
    pjsip_msg *(*creator)(pj_pool_t *pool);
    pj_size_t	 len;
    int		 expected_status;
} test_array[] = 
{
{
    /* 'Normal' message with all headers. */
    "INVITE sip:user@foo SIP/2.0\n"
    "from: Hi I'm Joe <sip:joe.user@bar.otherdomain.com>;tag=123457890123456\r"
    "To: Fellow User <sip:user@foo.bar.domain.com>\r\n"
    "Call-ID: 12345678901234567890@bar\r\n"
    "Content-Length: 0\r\n"
    "CSeq: 123456 INVITE\n"
    "Contact: <sip:joe@bar> ; q=0.5;expires=3600,sip:user@host;q=0.500\r"
    "  ,sip:user2@host2\n"
    "Content-Type: text/html ; charset=ISO-8859-4\r"
    "Route: <sip:bigbox3.site3.atlanta.com;lr>,\r\n"
    "  <sip:server10.biloxi.com;lr>\r"
    "Record-Route: <sip:server10.biloxi.com>,\r\n" /* multiple routes+folding*/
    "  <sip:bigbox3.site3.atlanta.com;lr>\n"
    "v: SIP/2.0/SCTP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c230\n"
    "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\n" /* folding. */
    " ;received=192.0.2.1\r\n"
    "Via: SIP/2.0/UDP 10.2.1.1, SIP/2.0/TCP 192.168.1.1\n"
    "Organization: \r"
    "Max-Forwards: 70\n"
    "X-Header: \r\n"	    /* empty header */
    "P-Associated-URI:\r\n" /* empty header without space */
    "\r\n",
    &create_msg0,
    PJ_SUCCESS
},
{
    /* Typical response message. */
    "SIP/2.0 200 OK\r\n"
    "Via: SIP/2.0/SCTP server10.biloxi.com;branch=z9hG4bKnashds8;rport;received=192.0.2.1\r\n"
    "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n"
    "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.3\r\n"
    "Route: <sip:proxy.sipprovider.com>\r\n"
    "Route: <sip:proxy.supersip.com:5060>\r\n"
    "Max-Forwards: 70\r\n"
    "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n"
    "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n"
    "Call-ID: a84b4c76e66710@pc33.atlanta.com\r\n"
    "CSeq: 314159 INVITE\r\n"
    "Contact: <sips:bob@192.0.2.4>\r\n"
    "Content-Type: application/sdp\r\n"
    "Content-Length: 150\r\n"
    "\r\n"
    "v=0\r\n"
    "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
    "s=-\r\n"
    "t=0 0\r\n"
    "c=IN IP4 pc33.atlanta.com\r\n"
    "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
    "a=rtpmap:0 PCMU/8000\r\n",
    &create_msg1,
    PJ_SUCCESS
}
};

static struct
{
    int flag;
    pj_highprec_t detect_len, parse_len, print_len;
    pj_timestamp  detect_time, parse_time, print_time;
} var;

static pj_status_t test_entry( pj_pool_t *pool, struct test_msg *entry )
{
    pjsip_msg *parsed_msg, *ref_msg = NULL;
    static pjsip_msg *print_msg;
    pj_status_t status = PJ_SUCCESS;
    int len;
    pj_str_t str1, str2;
    pjsip_hdr *hdr1, *hdr2;
    pj_timestamp t1, t2;
    pjsip_parser_err_report err_list;
    pj_size_t msg_size;
    char msgbuf1[PJSIP_MAX_PKT_LEN];
    char msgbuf2[PJSIP_MAX_PKT_LEN];
    enum { BUFLEN = 512 };

    entry->len = pj_native_strlen(entry->msg);

    if (var.flag & FLAG_PARSE_ONLY)
	goto parse_msg;

    if (var.flag & FLAG_PRINT_ONLY) {
	if (print_msg == NULL)
	    print_msg = entry->creator(pool);
	goto print_msg;
    }

    /* Detect message. */
    var.detect_len = var.detect_len + entry->len;
    pj_get_timestamp(&t1);
    status = pjsip_find_msg(entry->msg, entry->len, PJ_FALSE, &msg_size);
    if (status != PJ_SUCCESS) {
	if (status!=PJSIP_EPARTIALMSG || 
	    entry->expected_status!=STATUS_PARTIAL)
	{
	    app_perror("   error: unable to detect message", status);
	    return -5;
	}
    }
    if (msg_size != entry->len) {
	PJ_LOG(3,(THIS_FILE, "   error: size mismatch"));
	return -6;
    }
    pj_get_timestamp(&t2);
    pj_sub_timestamp(&t2, &t1);
    pj_add_timestamp(&var.detect_time, &t2);

    if (var.flag & FLAG_DETECT_ONLY)
	return PJ_SUCCESS;
    
    /* Parse message. */
parse_msg:
    var.parse_len = var.parse_len + entry->len;
    pj_get_timestamp(&t1);
    pj_list_init(&err_list);
    parsed_msg = pjsip_parse_msg(pool, entry->msg, entry->len, &err_list);
    if (parsed_msg == NULL) {
	if (entry->expected_status != STATUS_SYNTAX_ERROR) {
	    status = -10;
	    if (err_list.next != &err_list) {
		PJ_LOG(3,(THIS_FILE, "   Syntax error in line %d col %d",
			      err_list.next->line, err_list.next->col));
	    }
	    goto on_return;
	}
    }
    pj_get_timestamp(&t2);
    pj_sub_timestamp(&t2, &t1);
    pj_add_timestamp(&var.parse_time, &t2);

    if (var.flag & FLAG_PARSE_ONLY)
	return PJ_SUCCESS;

    /* Create reference message. */
    ref_msg = entry->creator(pool);

    /* Create buffer for comparison. */
    str1.ptr = pj_pool_alloc(pool, BUFLEN);
    str2.ptr = pj_pool_alloc(pool, BUFLEN);

    /* Compare message type. */
    if (parsed_msg->type != ref_msg->type) {
	status = -20;
	goto on_return;
    }

    /* Compare request or status line. */
    if (parsed_msg->type == PJSIP_REQUEST_MSG) {
	pjsip_method *m1 = &parsed_msg->line.req.method;
	pjsip_method *m2 = &ref_msg->line.req.method;

	if (pjsip_method_cmp(m1, m2) != 0) {
	    status = -30;
	    goto on_return;
	}
	status = pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI,
			       parsed_msg->line.req.uri, 
			       ref_msg->line.req.uri);
	if (status != PJ_SUCCESS) {
	    app_perror("   error: request URI mismatch", status);
	    status = -31;
	    goto on_return;
	}
    } else {
	if (parsed_msg->line.status.code != ref_msg->line.status.code) {
	    PJ_LOG(3,(THIS_FILE, "   error: status code mismatch"));
	    status = -32;
	    goto on_return;
	}
	if (pj_strcmp(&parsed_msg->line.status.reason, 
		      &ref_msg->line.status.reason) != 0) 
	{
	    PJ_LOG(3,(THIS_FILE, "   error: status text mismatch"));
	    status = -33;
	    goto on_return;
	}
    }

    /* Compare headers. */
    hdr1 = parsed_msg->hdr.next;
    hdr2 = ref_msg->hdr.next;

    while (hdr1 != &parsed_msg->hdr && hdr2 != &ref_msg->hdr) {
	len = hdr1->vptr->print_on(hdr1, str1.ptr, BUFLEN);
	if (len < 1) {
	    status = -40;
	    goto on_return;
	}
	str1.ptr[len] = '\0';
	str1.slen = len;

	len = hdr2->vptr->print_on(hdr2, str2.ptr, BUFLEN);
	if (len < 1) {
	    status = -50;
	    goto on_return;
	}
	str2.ptr[len] = '\0';
	str2.slen = len;

	if (pj_strcmp(&str1, &str2) != 0) {
	    status = -60;
	    PJ_LOG(3,(THIS_FILE, "   error: header string mismatch:\n"
		          "   h1='%s'\n"
			  "   h2='%s'\n",
			  str1.ptr, str2.ptr));
	    goto on_return;
	}

	hdr1 = hdr1->next;
	hdr2 = hdr2->next;
    }

    if (hdr1 != &parsed_msg->hdr || hdr2 != &ref_msg->hdr) {
	status = -70;
	goto on_return;
    }

    /* Compare body? */
    if (parsed_msg->body==NULL && ref_msg->body==NULL)
	goto print_msg;

    /* Compare msg body length. */
    if (parsed_msg->body->len != ref_msg->body->len) {
	status = -80;
	goto on_return;
    }

    /* Compare msg body content type. */
    if (pj_strcmp(&parsed_msg->body->content_type.type,
	          &ref_msg->body->content_type.type) != 0) {
	status = -90;
	goto on_return;
    }
    if (pj_strcmp(&parsed_msg->body->content_type.subtype,
	          &ref_msg->body->content_type.subtype) != 0) {
	status = -100;
	goto on_return;
    }

    /* Compare body content. */
    str1.slen = parsed_msg->body->print_body(parsed_msg->body,
					     msgbuf1, sizeof(msgbuf1));
    if (str1.slen < 1) {
	status = -110;
	goto on_return;
    }
    str1.ptr = msgbuf1;

    str2.slen = ref_msg->body->print_body(ref_msg->body,
					  msgbuf2, sizeof(msgbuf2));
    if (str2.slen < 1) {
	status = -120;
	goto on_return;
    }
    str2.ptr = msgbuf2;

    if (pj_strcmp(&str1, &str2) != 0) {
	status = -140;
	goto on_return;
    }
    
    /* Print message. */
print_msg:
    var.print_len = var.print_len + entry->len;
    pj_get_timestamp(&t1);
    if (var.flag && FLAG_PRINT_ONLY)
	ref_msg = print_msg;
    len = pjsip_msg_print(ref_msg, msgbuf1, PJSIP_MAX_PKT_LEN);
    if (len < 1) {
	status = -150;
	goto on_return;
    }
    pj_get_timestamp(&t2);
    pj_sub_timestamp(&t2, &t1);
    pj_add_timestamp(&var.print_time, &t2);


    status = PJ_SUCCESS;

on_return:
    return status;
}


static pjsip_msg *create_msg0(pj_pool_t *pool)
{

    pjsip_msg *msg;
    pjsip_name_addr *name_addr;
    pjsip_sip_uri *url;
    pjsip_fromto_hdr *fromto;
    pjsip_cid_hdr *cid;
    pjsip_clen_hdr *clen;
    pjsip_cseq_hdr *cseq;
    pjsip_contact_hdr *contact;
    pjsip_ctype_hdr *ctype;
    pjsip_routing_hdr *routing;
    pjsip_via_hdr *via;
    pjsip_generic_string_hdr *generic;
    pj_str_t str;

    msg = pjsip_msg_create(pool, PJSIP_REQUEST_MSG);

    /* "INVITE sip:user@foo SIP/2.0\n" */
    pjsip_method_set(&msg->line.req.method, PJSIP_INVITE_METHOD);
    url = pjsip_sip_uri_create(pool, 0);
    msg->line.req.uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "user");
    pj_strdup2(pool, &url->host, "foo");

    /* "From: Hi I'm Joe <sip:joe.user@bar.otherdomain.com>;tag=123457890123456\r" */
    fromto = pjsip_from_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
    pj_strdup2(pool, &fromto->tag, "123457890123456");
    name_addr = pjsip_name_addr_create(pool);
    fromto->uri = (pjsip_uri*)name_addr;
    pj_strdup2(pool, &name_addr->display, "Hi I'm Joe");
    url = pjsip_sip_uri_create(pool, 0);
    name_addr->uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "joe.user");
    pj_strdup2(pool, &url->host, "bar.otherdomain.com");

    /* "To: Fellow User <sip:user@foo.bar.domain.com>\r\n" */
    fromto = pjsip_to_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
    name_addr = pjsip_name_addr_create(pool);
    fromto->uri = (pjsip_uri*)name_addr;
    pj_strdup2(pool, &name_addr->display, "Fellow User");
    url = pjsip_sip_uri_create(pool, 0);
    name_addr->uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "user");
    pj_strdup2(pool, &url->host, "foo.bar.domain.com");

    /* "Call-ID: 12345678901234567890@bar\r\n" */
    cid = pjsip_cid_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)cid);
    pj_strdup2(pool, &cid->id, "12345678901234567890@bar");

    /* "Content-Length: 0\r\n" */
    clen = pjsip_clen_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)clen);
    clen->len = 0;

    /* "CSeq: 123456 INVITE\n" */
    cseq = pjsip_cseq_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)cseq);
    cseq->cseq = 123456;
    pjsip_method_set(&cseq->method, PJSIP_INVITE_METHOD);

    /* "Contact: <sip:joe@bar>;q=0.5;expires=3600*/
    contact = pjsip_contact_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
    contact->q1000 = 500;
    contact->expires = 3600;
    name_addr = pjsip_name_addr_create(pool);
    contact->uri = (pjsip_uri*)name_addr;
    url = pjsip_sip_uri_create(pool, 0);
    name_addr->uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "joe");
    pj_strdup2(pool, &url->host, "bar");

    /*, sip:user@host;q=0.500\r" */
    contact = pjsip_contact_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
    contact->q1000 = 500;
    name_addr = pjsip_name_addr_create(pool);
    contact->uri = (pjsip_uri*)name_addr;
    url = pjsip_sip_uri_create(pool, 0);
    name_addr->uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "user");
    pj_strdup2(pool, &url->host, "host");

    /* "  ,sip:user2@host2\n" */
    contact = pjsip_contact_hdr_create(pool);
    pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
    name_addr = pjsip_name_addr_create(pool);
    contact->uri = (pjsip_uri*)name_addr;
    url = pjsip_sip_uri_create(pool, 0);
    name_addr->uri = (pjsip_uri*)url;
    pj_strdup2(pool, &url->user, "user2");
    pj_strdup2(pool, &url->host, "host2");

    /* "Content-Type: text/html; charset=ISO-8859-4\r" */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -