blob: cf5f0ca59bad070aca5442e5b4b7dfbe3c0a4158 [file] [log] [blame]
Benny Prijono0ca04b62005-12-30 23:50:15 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
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 Prijono0ca04b62005-12-30 23:50:15 +000022#include <pjlib.h>
23
Benny Prijono85598d92006-01-07 18:44:25 +000024#define THIS_FILE "transport_udp_test.c"
25
Benny Prijono0ca04b62005-12-30 23:50:15 +000026
27/*
28 * UDP transport test.
29 */
30int transport_udp_test(void)
31{
32 enum { SEND_RECV_LOOP = 2 };
33 pjsip_transport *udp_tp, *tp;
34 pj_sockaddr_in addr, rem_addr;
35 pj_str_t s;
36 pj_status_t status;
Benny Prijono85598d92006-01-07 18:44:25 +000037 int i, pkt_lost;
Benny Prijono0ca04b62005-12-30 23:50:15 +000038
39 pj_sockaddr_in_init(&addr, NULL, TEST_UDP_PORT);
40
41 /* Start UDP transport. */
42 status = pjsip_udp_transport_start( endpt, &addr, NULL, 1, &udp_tp);
43 if (status != PJ_SUCCESS) {
44 app_perror(" Error: unable to start UDP transport", status);
45 return -10;
46 }
47
48 /* UDP transport must have initial reference counter set to 1. */
49 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
50 return -20;
51
52 /* Test basic transport attributes */
53 status = generic_transport_test(udp_tp);
54 if (status != PJ_SUCCESS)
55 return status;
56
57 /* Test that transport manager is returning the correct
58 * transport.
59 */
60 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80);
61 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
62 &rem_addr, sizeof(rem_addr),
63 &tp);
64 if (status != PJ_SUCCESS)
65 return -50;
66 if (tp != udp_tp)
67 return -60;
68
69 /* pjsip_endpt_acquire_transport() adds reference, so we need
70 * to decrement it.
71 */
72 pjsip_transport_dec_ref(tp);
73
74 /* Check again that reference counter is 1. */
75 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
76 return -70;
77
78 /* Basic transport's send/receive loopback test. */
79 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "127.0.0.1"), TEST_UDP_PORT);
80 for (i=0; i<SEND_RECV_LOOP; ++i) {
Benny Prijonofa73e3e2006-01-05 23:35:46 +000081 status = transport_send_recv_test(PJSIP_TRANSPORT_UDP, tp,
82 "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR);
Benny Prijono0ca04b62005-12-30 23:50:15 +000083 if (status != 0)
84 return status;
85 }
86
87 /* Multi-threaded round-trip test. */
Benny Prijonofa73e3e2006-01-05 23:35:46 +000088 status = transport_rt_test(PJSIP_TRANSPORT_UDP, tp,
Benny Prijono85598d92006-01-07 18:44:25 +000089 "sip:alice@127.0.0.1:"TEST_UDP_PORT_STR,
90 &pkt_lost);
Benny Prijono0ca04b62005-12-30 23:50:15 +000091 if (status != 0)
92 return status;
93
Benny Prijono85598d92006-01-07 18:44:25 +000094 if (pkt_lost != 0)
95 PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost));
96
Benny Prijono0ca04b62005-12-30 23:50:15 +000097 /* Check again that reference counter is 1. */
98 if (pj_atomic_get(udp_tp->ref_cnt) != 1)
99 return -80;
100
101 /* Destroy this transport. */
102 pjsip_transport_dec_ref(udp_tp);
103
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000104 /* Force destroy this transport. */
105 status = pjsip_transport_unregister( pjsip_endpt_get_tpmgr(endpt), udp_tp);
106 if (status != PJ_SUCCESS)
107 return -90;
108
Benny Prijono85598d92006-01-07 18:44:25 +0000109 /* Flush events. */
110 PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second..."));
111 flush_events(1000);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000112
Benny Prijono0ca04b62005-12-30 23:50:15 +0000113 /* Done */
114 return 0;
115}