blob: b43be773651ae247ee37203da3dddaca42bad408 [file] [log] [blame]
Benny Prijonofa73e3e2006-01-05 23:35:46 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonofa73e3e2006-01-05 23:35:46 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000021#include <pjsip.h>
Benny Prijonofa73e3e2006-01-05 23:35:46 +000022#include <pjlib.h>
23
Benny Prijono85598d92006-01-07 18:44:25 +000024#define THIS_FILE "transport_loop_test.c"
25
Benny Prijonofa73e3e2006-01-05 23:35:46 +000026static int datagram_loop_test()
27{
Benny Prijonoe93e2872006-06-28 16:46:49 +000028 enum { LOOP = 8 };
Benny Prijono85598d92006-01-07 18:44:25 +000029 pjsip_transport *loop;
30 int i, pkt_lost;
Benny Prijonofa73e3e2006-01-05 23:35:46 +000031 pj_sockaddr_in addr;
32 pj_status_t status;
Benny Prijonoe93e2872006-06-28 16:46:49 +000033 long ref_cnt;
Benny Prijono7db431e2006-07-23 14:38:49 +000034 int rtt[LOOP], min_rtt;
Benny Prijonofa73e3e2006-01-05 23:35:46 +000035
Benny Prijono85598d92006-01-07 18:44:25 +000036 PJ_LOG(3,(THIS_FILE, "testing datagram loop transport"));
Benny Prijonofa73e3e2006-01-05 23:35:46 +000037
38 /* Test acquire transport. */
39 status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM,
Benny Prijonodf2b71e2007-01-20 19:17:47 +000040 &addr, sizeof(addr), NULL, &loop);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000041 if (status != PJ_SUCCESS) {
Benny Prijono85598d92006-01-07 18:44:25 +000042 app_perror(" error: loop transport is not configured", status);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000043 return -20;
44 }
45
Benny Prijonoe93e2872006-06-28 16:46:49 +000046 /* Get initial reference counter */
47 ref_cnt = pj_atomic_get(loop->ref_cnt);
48
Benny Prijonofa73e3e2006-01-05 23:35:46 +000049 /* Test basic transport attributes */
50 status = generic_transport_test(loop);
51 if (status != PJ_SUCCESS)
52 return status;
53
54 /* Basic transport's send/receive loopback test. */
Benny Prijonoe93e2872006-06-28 16:46:49 +000055 for (i=0; i<LOOP; ++i) {
Benny Prijonofa73e3e2006-01-05 23:35:46 +000056 status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
Benny Prijonoe93e2872006-06-28 16:46:49 +000057 "sip:bob@130.0.0.1;transport=loop-dgram",
58 &rtt[i]);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000059 if (status != 0)
60 return status;
61 }
62
Benny Prijonoe93e2872006-06-28 16:46:49 +000063 min_rtt = 0xFFFFFFF;
64 for (i=0; i<LOOP; ++i)
65 if (rtt[i] < min_rtt) min_rtt = rtt[i];
66
67 report_ival("loop-rtt-usec", min_rtt, "usec",
68 "Best Loopback transport round trip time, in microseconds "
69 "(time from sending request until response is received. "
70 "Tests were performed on local machine only)");
71
72
Benny Prijono85598d92006-01-07 18:44:25 +000073 /* Multi-threaded round-trip test. */
74 status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
75 "sip:bob@130.0.0.1;transport=loop-dgram",
76 &pkt_lost);
77 if (status != 0)
78 return status;
79
80 if (pkt_lost != 0) {
81 PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost));
82 return -40;
83 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +000084
85 /* Put delay. */
Benny Prijono85598d92006-01-07 18:44:25 +000086 PJ_LOG(3,(THIS_FILE," setting network delay to 10 ms"));
87 pjsip_loop_set_delay(loop, 10);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000088
89 /* Multi-threaded round-trip test. */
Benny Prijono85598d92006-01-07 18:44:25 +000090 status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop,
91 "sip:bob@130.0.0.1;transport=loop-dgram",
92 &pkt_lost);
Benny Prijonofa73e3e2006-01-05 23:35:46 +000093 if (status != 0)
94 return status;
95
Benny Prijono85598d92006-01-07 18:44:25 +000096 if (pkt_lost != 0) {
97 PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost));
98 return -50;
99 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000100
Benny Prijono85598d92006-01-07 18:44:25 +0000101 /* Restore delay. */
102 pjsip_loop_set_delay(loop, 0);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000103
Benny Prijonoe93e2872006-06-28 16:46:49 +0000104 /* Check reference counter. */
105 if (pj_atomic_get(loop->ref_cnt) != ref_cnt) {
106 PJ_LOG(3,(THIS_FILE, " error: ref counter is not %d (%d)",
107 ref_cnt, pj_atomic_get(loop->ref_cnt)));
108 return -51;
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000109 }
110
111 /* Decrement reference. */
112 pjsip_transport_dec_ref(loop);
113
114 return 0;
115}
116
117int transport_loop_test(void)
118{
119 int status;
120
121 status = datagram_loop_test();
122 if (status != 0)
123 return status;
124
125 return 0;
126}