blob: a28b7b07bc6e6dbf4692d138a038332f40c68ab2 [file] [log] [blame]
Benny Prijonofa73e3e2006-01-05 23:35:46 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonofa73e3e2006-01-05 23:35:46 +00005 *
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"
Benny Prijono40f2f642006-01-30 18:40:05 +000022#include <pjsip.h>
Benny Prijonofa73e3e2006-01-05 23:35:46 +000023#include <pjlib.h>
24
Benny Prijono85598d92006-01-07 18:44:25 +000025#define THIS_FILE "transport_loop_test.c"
26
Benny Prijonofa73e3e2006-01-05 23:35:46 +000027static int datagram_loop_test()
28{
Benny Prijonoe93e2872006-06-28 16:46:49 +000029 enum { LOOP = 8 };
Benny Prijono85598d92006-01-07 18:44:25 +000030 pjsip_transport *loop;
31 int i, pkt_lost;
Benny Prijonofa73e3e2006-01-05 23:35:46 +000032 pj_sockaddr_in addr;
33 pj_status_t status;
Benny Prijonoe93e2872006-06-28 16:46:49 +000034 long ref_cnt;
Benny Prijono7db431e2006-07-23 14:38:49 +000035 int rtt[LOOP], min_rtt;
Benny Prijonofa73e3e2006-01-05 23:35:46 +000036
Benny Prijono85598d92006-01-07 18:44:25 +000037 PJ_LOG(3,(THIS_FILE, "testing datagram loop transport"));
Benny Prijonofa73e3e2006-01-05 23:35:46 +000038
39 /* Test acquire transport. */
40 status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM,
Benny Prijonodf2b71e2007-01-20 19:17:47 +000041 &addr, sizeof(addr), NULL, &loop);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000042 if (status != PJ_SUCCESS) {
Benny Prijono85598d92006-01-07 18:44:25 +000043 app_perror(" error: loop transport is not configured", status);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000044 return -20;
45 }
46
Benny Prijonoe93e2872006-06-28 16:46:49 +000047 /* Get initial reference counter */
48 ref_cnt = pj_atomic_get(loop->ref_cnt);
49
Benny Prijonofa73e3e2006-01-05 23:35:46 +000050 /* 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. */
Benny Prijonoe93e2872006-06-28 16:46:49 +000056 for (i=0; i<LOOP; ++i) {
Benny Prijonofa73e3e2006-01-05 23:35:46 +000057 status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
Benny Prijonoe93e2872006-06-28 16:46:49 +000058 "sip:bob@130.0.0.1;transport=loop-dgram",
59 &rtt[i]);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000060 if (status != 0)
61 return status;
62 }
63
Benny Prijonoe93e2872006-06-28 16:46:49 +000064 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
Benny Prijono85598d92006-01-07 18:44:25 +000074 /* 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 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +000085
86 /* Put delay. */
Benny Prijono85598d92006-01-07 18:44:25 +000087 PJ_LOG(3,(THIS_FILE," setting network delay to 10 ms"));
88 pjsip_loop_set_delay(loop, 10);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000089
90 /* Multi-threaded round-trip test. */
Benny Prijono85598d92006-01-07 18:44:25 +000091 status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
92 "sip:bob@130.0.0.1;transport=loop-dgram",
93 &pkt_lost);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000094 if (status != 0)
95 return status;
96
Benny Prijono85598d92006-01-07 18:44:25 +000097 if (pkt_lost != 0) {
98 PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost));
99 return -50;
100 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000101
Benny Prijono85598d92006-01-07 18:44:25 +0000102 /* Restore delay. */
103 pjsip_loop_set_delay(loop, 0);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000104
Benny Prijonoe93e2872006-06-28 16:46:49 +0000105 /* 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;
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000110 }
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}