Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 1 | /* $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 Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 21 | #include <pjsip.h> |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 22 | #include <pjlib.h> |
| 23 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 24 | #define THIS_FILE "transport_loop_test.c" |
| 25 | |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 26 | static int datagram_loop_test() |
| 27 | { |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 28 | pjsip_transport *loop; |
| 29 | int i, pkt_lost; |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 30 | pj_sockaddr_in addr; |
| 31 | pj_status_t status; |
| 32 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 33 | PJ_LOG(3,(THIS_FILE, "testing datagram loop transport")); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 34 | |
| 35 | /* Test acquire transport. */ |
| 36 | status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM, |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 37 | &addr, sizeof(addr), &loop); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 38 | if (status != PJ_SUCCESS) { |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 39 | app_perror(" error: loop transport is not configured", status); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 40 | return -20; |
| 41 | } |
| 42 | |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 43 | /* Test basic transport attributes */ |
| 44 | status = generic_transport_test(loop); |
| 45 | if (status != PJ_SUCCESS) |
| 46 | return status; |
| 47 | |
| 48 | /* Basic transport's send/receive loopback test. */ |
| 49 | for (i=0; i<2; ++i) { |
| 50 | status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, |
| 51 | "sip:bob@130.0.0.1;transport=loop-dgram"); |
| 52 | if (status != 0) |
| 53 | return status; |
| 54 | } |
| 55 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 56 | /* Multi-threaded round-trip test. */ |
| 57 | status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, |
| 58 | "sip:bob@130.0.0.1;transport=loop-dgram", |
| 59 | &pkt_lost); |
| 60 | if (status != 0) |
| 61 | return status; |
| 62 | |
| 63 | if (pkt_lost != 0) { |
| 64 | PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost)); |
| 65 | return -40; |
| 66 | } |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 67 | |
| 68 | /* Put delay. */ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 69 | PJ_LOG(3,(THIS_FILE," setting network delay to 10 ms")); |
| 70 | pjsip_loop_set_delay(loop, 10); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 71 | |
| 72 | /* Multi-threaded round-trip test. */ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 73 | status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, |
| 74 | "sip:bob@130.0.0.1;transport=loop-dgram", |
| 75 | &pkt_lost); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 76 | if (status != 0) |
| 77 | return status; |
| 78 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 79 | if (pkt_lost != 0) { |
| 80 | PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost)); |
| 81 | return -50; |
| 82 | } |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 83 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 84 | /* Restore delay. */ |
| 85 | pjsip_loop_set_delay(loop, 0); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 86 | |
| 87 | /* Check that reference counter is one. */ |
| 88 | if (pj_atomic_get(loop->ref_cnt) != 1) { |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 89 | return -50; |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* Decrement reference. */ |
| 93 | pjsip_transport_dec_ref(loop); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int transport_loop_test(void) |
| 99 | { |
| 100 | int status; |
| 101 | |
| 102 | status = datagram_loop_test(); |
| 103 | if (status != 0) |
| 104 | return status; |
| 105 | |
| 106 | return 0; |
| 107 | } |