Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 1 | /* |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 2 | * (C) 2011-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2011-2012 by On-Waves |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | 1f0c5b4 | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 21 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 22 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 23 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 25 | #include <string.h> |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 26 | #include <limits.h> |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 27 | |
Harald Welte | 8819d82 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 28 | #define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n" |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 29 | #define AUEP1_RET "200 158663169 OK\r\n" |
Harald Welte | 8819d82 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 30 | #define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n" |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 31 | #define AUEP2_RET "500 18983213 FAIL\r\n" |
Holger Hans Peter Freyther | d77023b | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 32 | #define EMPTY "\r\n" |
| 33 | #define EMPTY_RET NULL |
| 34 | #define SHORT "CRCX \r\n" |
| 35 | #define SHORT_RET "510 000000 FAIL\r\n" |
| 36 | |
| 37 | #define SHORT2 "CRCX 1" |
| 38 | #define SHORT2_RET "510 000000 FAIL\r\n" |
| 39 | #define SHORT3 "CRCX 1 1@mgw" |
| 40 | #define SHORT4 "CRCX 1 1@mgw MGCP" |
| 41 | #define SHORT5 "CRCX 1 1@mgw MGCP 1.0" |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 42 | |
Harald Welte | 27fa790 | 2012-01-27 00:37:23 +0100 | [diff] [blame] | 43 | #define CRCX "CRCX 2 1@mgw MGCP 1.0\r\n" \ |
| 44 | "M: sendrecv\r\n" \ |
| 45 | "C: 2\r\n" \ |
| 46 | "\r\n" \ |
| 47 | "v=0\r\n" \ |
| 48 | "c=IN IP4 123.12.12.123\r\n" \ |
| 49 | "m=audio 5904 RTP/AVP 97\r\n" \ |
| 50 | "a=rtpmap:97 GSM-EFR/8000\r\n" |
| 51 | |
| 52 | #define CRCX_RET "200 2 OK\r\n" \ |
| 53 | "I: 1\n" \ |
| 54 | "\n" \ |
| 55 | "v=0\r\n" \ |
| 56 | "o=- 1 23 IN IP4 0.0.0.0\r\n" \ |
| 57 | "c=IN IP4 0.0.0.0\r\n" \ |
| 58 | "t=0 0\r\n" \ |
| 59 | "m=audio 0 RTP/AVP 126\r\n" \ |
| 60 | "a=rtpmap:126 AMR/8000\r\n" |
| 61 | |
| 62 | |
| 63 | #define CRCX_ZYN "CRCX 2 1@mgw MGCP 1.0\r" \ |
| 64 | "M: sendrecv\r" \ |
| 65 | "C: 2\r\r" \ |
| 66 | "v=0\r" \ |
| 67 | "c=IN IP4 123.12.12.123\r" \ |
| 68 | "m=audio 5904 RTP/AVP 97\r" \ |
| 69 | "a=rtpmap:97 GSM-EFR/8000\r" |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 70 | |
Holger Hans Peter Freyther | 0bf15a8 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 71 | #define DLCX "DLCX 7 1@mgw MGCP 1.0\r\n" \ |
| 72 | "C: 2\r\n" |
| 73 | |
| 74 | #define DLCX_RET "250 7 OK\r\n" \ |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 75 | "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" |
Holger Hans Peter Freyther | 0bf15a8 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 76 | |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 77 | #define RQNT "RQNT 186908780 1@mgw MGCP 1.0\r\n" \ |
| 78 | "X: B244F267488\r\n" \ |
| 79 | "S: D/9\r\n" |
| 80 | |
| 81 | #define RQNT2 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \ |
| 82 | "X: ADD4F26746F\r\n" \ |
| 83 | "R: D/[0-9#*](N), G/ft, fxr/t38\r\n" |
| 84 | |
| 85 | #define RQNT_RET "200 186908780 OK\r\n" |
Holger Hans Peter Freyther | 0bf15a8 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 86 | |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 87 | struct mgcp_test { |
| 88 | const char *name; |
| 89 | const char *req; |
| 90 | const char *exp_resp; |
| 91 | }; |
| 92 | |
| 93 | const struct mgcp_test tests[] = { |
| 94 | { "AUEP1", AUEP1, AUEP1_RET }, |
| 95 | { "AUEP2", AUEP2, AUEP2_RET }, |
Harald Welte | 27fa790 | 2012-01-27 00:37:23 +0100 | [diff] [blame] | 96 | { "CRCX", CRCX, CRCX_RET }, |
| 97 | { "CRCX_ZYN", CRCX_ZYN, CRCX_RET }, |
Holger Hans Peter Freyther | d77023b | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 98 | { "EMPTY", EMPTY, EMPTY_RET }, |
| 99 | { "SHORT1", SHORT, SHORT_RET }, |
| 100 | { "SHORT2", SHORT2, SHORT2_RET }, |
| 101 | { "SHORT3", SHORT3, SHORT2_RET }, |
| 102 | { "SHORT4", SHORT4, SHORT2_RET }, |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 103 | { "RQNT1", RQNT, RQNT_RET }, |
| 104 | { "RQNT2", RQNT2, RQNT_RET }, |
Holger Hans Peter Freyther | 0bf15a8 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 105 | { "DLCX", DLCX, DLCX_RET }, |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 106 | }; |
Harald Welte | 8819d82 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 107 | |
| 108 | static struct msgb *create_msg(const char *str) |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 109 | { |
| 110 | struct msgb *msg; |
| 111 | |
| 112 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
Harald Welte | 8819d82 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 113 | int len = sprintf((char *)msg->data, str); |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 114 | msg->l2h = msgb_put(msg, len); |
| 115 | return msg; |
| 116 | } |
| 117 | |
Holger Hans Peter Freyther | 5563e6c | 2012-03-16 09:45:13 +0100 | [diff] [blame] | 118 | static void test_messages(void) |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 119 | { |
Holger Hans Peter Freyther | d5b3ca6 | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 120 | struct mgcp_config *cfg; |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 121 | int i; |
Holger Hans Peter Freyther | d5b3ca6 | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 122 | |
| 123 | cfg = mgcp_config_alloc(); |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 124 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 125 | cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | 1f0c5b4 | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 126 | mgcp_endpoints_allocate(&cfg->trunk); |
| 127 | |
| 128 | mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1)); |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 129 | |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 130 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
| 131 | const struct mgcp_test *t = &tests[i]; |
| 132 | struct msgb *inp; |
| 133 | struct msgb *msg; |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 134 | |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 135 | printf("Testing %s\n", t->name); |
| 136 | |
| 137 | inp = create_msg(t->req); |
| 138 | msg = mgcp_handle_message(cfg, inp); |
| 139 | msgb_free(inp); |
Holger Hans Peter Freyther | d77023b | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 140 | if (!t->exp_resp) { |
| 141 | if (msg) |
| 142 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 143 | } else if (strcmp((char *) msg->data, t->exp_resp) != 0) |
Harald Welte | f74da14 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 144 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 145 | msgb_free(msg); |
| 146 | } |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 147 | |
| 148 | talloc_free(cfg); |
| 149 | } |
| 150 | |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 151 | static int rqnt_cb(struct mgcp_endpoint *endp, char _tone, const char *data) |
| 152 | { |
| 153 | ptrdiff_t tone = _tone; |
| 154 | endp->cfg->data = (void *) tone; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void test_rqnt_cb(void) |
| 159 | { |
| 160 | struct mgcp_config *cfg; |
| 161 | struct msgb *inp, *msg; |
| 162 | |
| 163 | cfg = mgcp_config_alloc(); |
| 164 | cfg->rqnt_cb = rqnt_cb; |
| 165 | |
| 166 | cfg->trunk.number_endpoints = 64; |
| 167 | mgcp_endpoints_allocate(&cfg->trunk); |
| 168 | |
| 169 | mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1)); |
| 170 | |
| 171 | inp = create_msg(CRCX); |
| 172 | msgb_free(mgcp_handle_message(cfg, inp)); |
| 173 | msgb_free(inp); |
| 174 | |
| 175 | /* send the RQNT and check for the CB */ |
| 176 | inp = create_msg(RQNT); |
| 177 | msg = mgcp_handle_message(cfg, inp); |
| 178 | if (strncmp((const char *) msg->l2h, "200", 3) != 0) { |
| 179 | printf("FAILED: message is not 200. '%s'\n", msg->l2h); |
| 180 | abort(); |
| 181 | } |
| 182 | |
| 183 | if (cfg->data != (void *) '9') { |
| 184 | printf("FAILED: callback not called: %p\n", cfg->data); |
| 185 | abort(); |
| 186 | } |
| 187 | |
| 188 | msgb_free(msg); |
| 189 | msgb_free(inp); |
| 190 | |
| 191 | inp = create_msg(DLCX); |
| 192 | msgb_free(mgcp_handle_message(cfg, inp)); |
| 193 | msgb_free(inp); |
| 194 | talloc_free(cfg); |
| 195 | } |
| 196 | |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 197 | struct pl_test { |
| 198 | int cycles; |
| 199 | uint16_t base_seq; |
| 200 | uint16_t max_seq; |
| 201 | uint32_t packets; |
| 202 | |
| 203 | uint32_t expected; |
| 204 | int loss; |
| 205 | }; |
| 206 | |
| 207 | static const struct pl_test pl_test_dat[] = { |
| 208 | /* basic.. just one package */ |
| 209 | { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = 1, .expected = 1, .loss = 0}, |
| 210 | /* some packages and a bit of loss */ |
| 211 | { .cycles = 0, .base_seq = 0, .max_seq = 100, .packets = 100, .expected = 101, .loss = 1}, |
| 212 | /* wrap around */ |
| 213 | { .cycles = 1<<16, .base_seq = 0xffff, .max_seq = 2, .packets = 4, .expected = 4, .loss = 0}, |
| 214 | /* min loss */ |
| 215 | { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = UINT_MAX, .expected = 1, .loss = INT_MIN }, |
| 216 | /* max loss, with wrap around on expected max */ |
| 217 | { .cycles = INT_MAX, .base_seq = 0, .max_seq = UINT16_MAX, .packets = 0, .expected = ((uint32_t)(INT_MAX) + UINT16_MAX + 1), .loss = INT_MAX }, |
| 218 | }; |
| 219 | |
| 220 | static void test_packet_loss_calc(void) |
| 221 | { |
| 222 | int i; |
| 223 | printf("Testing packet loss calculation.\n"); |
| 224 | |
| 225 | for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) { |
| 226 | uint32_t expected; |
| 227 | int loss; |
| 228 | struct mgcp_rtp_state state; |
| 229 | struct mgcp_rtp_end rtp; |
| 230 | memset(&state, 0, sizeof(state)); |
| 231 | memset(&rtp, 0, sizeof(rtp)); |
| 232 | |
| 233 | state.initialized = 1; |
| 234 | state.base_seq = pl_test_dat[i].base_seq; |
| 235 | state.max_seq = pl_test_dat[i].max_seq; |
| 236 | state.cycles = pl_test_dat[i].cycles; |
| 237 | |
| 238 | rtp.packets = pl_test_dat[i].packets; |
| 239 | mgcp_state_calc_loss(&state, &rtp, &expected, &loss); |
| 240 | |
| 241 | if (loss != pl_test_dat[i].loss || expected != pl_test_dat[i].expected) { |
| 242 | printf("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n", |
| 243 | i, loss, pl_test_dat[i].loss, |
| 244 | expected, pl_test_dat[i].expected); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 249 | int main(int argc, char **argv) |
| 250 | { |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 251 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 252 | |
Holger Hans Peter Freyther | 5563e6c | 2012-03-16 09:45:13 +0100 | [diff] [blame] | 253 | test_messages(); |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 254 | test_packet_loss_calc(); |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 255 | test_rqnt_cb(); |
Holger Hans Peter Freyther | d5b3ca6 | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 256 | |
| 257 | printf("Done\n"); |
| 258 | return EXIT_SUCCESS; |
Holger Hans Peter Freyther | 9f239a2 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 259 | } |