Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 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 | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 28 | enum { LOOP = 8 }; |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 29 | pjsip_transport *loop; |
| 30 | int i, pkt_lost; |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 31 | pj_sockaddr_in addr; |
| 32 | pj_status_t status; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 33 | long ref_cnt; |
Benny Prijono | 7db431e | 2006-07-23 14:38:49 +0000 | [diff] [blame] | 34 | int rtt[LOOP], min_rtt; |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 35 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 36 | PJ_LOG(3,(THIS_FILE, "testing datagram loop transport")); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 37 | |
| 38 | /* Test acquire transport. */ |
| 39 | status = pjsip_endpt_acquire_transport( endpt, PJSIP_TRANSPORT_LOOP_DGRAM, |
Benny Prijono | df2b71e | 2007-01-20 19:17:47 +0000 | [diff] [blame] | 40 | &addr, sizeof(addr), NULL, &loop); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 41 | if (status != PJ_SUCCESS) { |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 42 | app_perror(" error: loop transport is not configured", status); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 43 | return -20; |
| 44 | } |
| 45 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 46 | /* Get initial reference counter */ |
| 47 | ref_cnt = pj_atomic_get(loop->ref_cnt); |
| 48 | |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 49 | /* 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 Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 55 | for (i=0; i<LOOP; ++i) { |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 56 | status = transport_send_recv_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 57 | "sip:bob@130.0.0.1;transport=loop-dgram", |
| 58 | &rtt[i]); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 59 | if (status != 0) |
| 60 | return status; |
| 61 | } |
| 62 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 63 | 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 Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 73 | /* 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 Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 84 | |
| 85 | /* Put delay. */ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 86 | PJ_LOG(3,(THIS_FILE," setting network delay to 10 ms")); |
| 87 | pjsip_loop_set_delay(loop, 10); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 88 | |
| 89 | /* Multi-threaded round-trip test. */ |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 90 | status = transport_rt_test(PJSIP_TRANSPORT_LOOP_DGRAM, loop, |
| 91 | "sip:bob@130.0.0.1;transport=loop-dgram", |
| 92 | &pkt_lost); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 93 | if (status != 0) |
| 94 | return status; |
| 95 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 96 | if (pkt_lost != 0) { |
| 97 | PJ_LOG(3,(THIS_FILE, " error: %d packet(s) was lost", pkt_lost)); |
| 98 | return -50; |
| 99 | } |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 100 | |
Benny Prijono | 85598d9 | 2006-01-07 18:44:25 +0000 | [diff] [blame] | 101 | /* Restore delay. */ |
| 102 | pjsip_loop_set_delay(loop, 0); |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 103 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 104 | /* 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 Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Decrement reference. */ |
| 112 | pjsip_transport_dec_ref(loop); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int 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 | } |