summaryrefslogtreecommitdiff
path: root/pjsip/src/test-pjsip/transport_loop_test.c
blob: 467f327f73c71a72b39942f367b1e737cc2d3d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* $Id$ */
/* 
 * Copyright (C) 2003-2006 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_core.h>
#include <pjlib.h>

static int datagram_loop_test()
{
    pjsip_transport *loop, *tp;
    pj_str_t s;
    int i, log_level;
    pj_sockaddr_in addr;
    pj_status_t status;

    PJ_LOG(3,("", "testing datagram loop transport"));

    /* Create loop transport. */
    status = pjsip_loop_start(endpt, &loop);
    if (status != PJ_SUCCESS) {
	app_perror("   error: unable to create datagram loop transport", 
		   status);
	return -10;
    }

    /* Create dummy address. */
    pj_sockaddr_in_init(&addr, pj_cstr(&s, "130.0.0.1"), TEST_UDP_PORT);

    /* Test acquire transport. */
    status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM,
					    &addr, sizeof(addr), &tp);
    if (status != PJ_SUCCESS) {
	app_perror("   error: unable to acquire transport", status);
	return -20;
    }

    /* Check that this is the right transport. */
    if (tp != loop) {
	return -30;
    }

    /* Test basic transport attributes */
    status = generic_transport_test(loop);
    if (status != PJ_SUCCESS)
	return status;

    /* Basic transport's send/receive loopback test. */
    for (i=0; i<2; ++i) {
	status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, 
					  "sip:bob@130.0.0.1;transport=loop-dgram");
	if (status != 0)
	    return status;
    }

    /* For multithreaded round-trip test to work, delay must be set
     * (otherwise functions will be called recursively until no memory is
     * left in the system)
     */

    /* Put delay. */
    pjsip_loop_set_delay(loop, 1, NULL);

    /* Multi-threaded round-trip test. */
    status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, tp, 
			       "sip:bob@130.0.0.1;transport=loop-dgram");
    if (status != 0)
	return status;


    /* Next test will test without delay.
     * This will stress-test the system.
     */
    PJ_LOG(3,("","  performing another multithreaded round-trip test..."));

    /* Remove delay. */
    pjsip_loop_set_delay(loop, 0, NULL);

    /* Ignore errors. */
    log_level = pj_log_get_level();
    pj_log_set_level(2);

    /* Multi-threaded round-trip test. */
    status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, tp, 
			       "sip:bob@130.0.0.1;transport=loop-dgram");
    if (status != 0)
	return status;

    /* Restore log level. */
    pj_log_set_level(log_level);

    /* Check that reference counter is one. */
    if (pj_atomic_get(loop->ref_cnt) != 1) {
	return -30;
    }

    /* Decrement reference. */
    pjsip_transport_dec_ref(loop);

    return 0;
}

int transport_loop_test(void)
{
    int status;

    status = datagram_loop_test();
    if (status != 0)
	return status;

    return 0;
}