blob: efa2ea116e212a7f1b2930a60cdbbce15e28cdce [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "test.h"
22#include <pjsip.h>
23#include <pjlib.h>
24
25#define THIS_FILE "transport_loop_test.c"
26
27static int datagram_loop_test()
28{
29 enum { LOOP = 8 };
30 pjsip_transport *loop;
31 int i, pkt_lost;
32 pj_sockaddr_in addr;
33 pj_status_t status;
34 long ref_cnt;
35 int rtt[LOOP], min_rtt;
36
37 PJ_LOG(3,(THIS_FILE, "testing datagram loop transport"));
38
39 /* Test acquire transport. */
40 status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM,
41 &addr, sizeof(addr), NULL, &loop);
42 if (status != PJ_SUCCESS) {
43 app_perror(" error: loop transport is not configured", status);
44 return -20;
45 }
46
47 /* Get initial reference counter */
48 ref_cnt = pj_atomic_get(loop->ref_cnt);
49
50 /* Test basic transport attributes */
51 status = generic_transport_test(loop);
52 if (status != PJ_SUCCESS)
53 return status;
54
55 /* Basic transport's send/receive loopback test. */
56 for (i=0; i<LOOP; ++i) {
57 status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
58 "sip:bob@130.0.0.1;transport=loop-dgram",
59 &rtt[i]);
60 if (status != 0)
61 return status;
62 }
63
64 min_rtt = 0xFFFFFFF;
65 for (i=0; i<LOOP; ++i)
66 if (rtt[i] < min_rtt) min_rtt = rtt[i];
67
68 report_ival("loop-rtt-usec", min_rtt, "usec",
69 "Best Loopback transport round trip time, in microseconds "
70 "(time from sending request until response is received. "
71 "Tests were performed on local machine only)");
72
73
74 /* Multi-threaded round-trip test. */
75 status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
76 "sip:bob@130.0.0.1;transport=loop-dgram",
77 &pkt_lost);
78 if (status != 0)
79 return status;
80
81 if (pkt_lost != 0) {
82 PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost));
83 return -40;
84 }
85
86 /* Put delay. */
87 PJ_LOG(3,(THIS_FILE," setting network delay to 10 ms"));
88 pjsip_loop_set_delay(loop, 10);
89
90 /* Multi-threaded round-trip test. */
91 status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
92 "sip:bob@130.0.0.1;transport=loop-dgram",
93 &pkt_lost);
94 if (status != 0)
95 return status;
96
97 if (pkt_lost != 0) {
98 PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost));
99 return -50;
100 }
101
102 /* Restore delay. */
103 pjsip_loop_set_delay(loop, 0);
104
105 /* Check reference counter. */
106 if (pj_atomic_get(loop->ref_cnt) != ref_cnt) {
107 PJ_LOG(3,(THIS_FILE, " error: ref counter is not %d (%d)",
108 ref_cnt, pj_atomic_get(loop->ref_cnt)));
109 return -51;
110 }
111
112 /* Decrement reference. */
113 pjsip_transport_dec_ref(loop);
114
115 return 0;
116}
117
118int transport_loop_test(void)
119{
120 int status;
121
122 status = datagram_loop_test();
123 if (status != 0)
124 return status;
125
126 return 0;
127}