Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 1 | /* |
Holger Hans Peter Freyther | faa6793 | 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 | f0bc075 | 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 | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 21 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 22 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 23 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 25 | #include <string.h> |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 26 | #include <limits.h> |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 27 | |
Jacob Erlbeck | e7060c4 | 2013-11-29 13:43:44 +0100 | [diff] [blame] | 28 | char *strline_r(char *str, char **saveptr); |
| 29 | |
| 30 | const char *strline_test_data = |
| 31 | "one CR\r" |
| 32 | "two CR\r" |
| 33 | "\r" |
| 34 | "one CRLF\r\n" |
| 35 | "two CRLF\r\n" |
| 36 | "\r\n" |
| 37 | "one LF\n" |
| 38 | "two LF\n" |
| 39 | "\n" |
| 40 | "mixed (4 lines)\r\r\n\n\r\n"; |
| 41 | |
| 42 | #define EXPECTED_NUMBER_OF_LINES 13 |
| 43 | |
| 44 | static void test_strline(void) |
| 45 | { |
| 46 | char *save = NULL; |
| 47 | char *line; |
| 48 | char buf[2048]; |
| 49 | int counter = 0; |
| 50 | |
| 51 | strncpy(buf, strline_test_data, sizeof(buf)); |
| 52 | |
| 53 | for (line = strline_r(buf, &save); line; |
| 54 | line = strline_r(NULL, &save)) { |
| 55 | printf("line: '%s'\n", line); |
| 56 | counter++; |
| 57 | } |
| 58 | |
| 59 | OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES); |
| 60 | } |
| 61 | |
Harald Welte | 834f3f1 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 62 | #define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n" |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 63 | #define AUEP1_RET "200 158663169 OK\r\n" |
Harald Welte | 834f3f1 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 64 | #define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n" |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 65 | #define AUEP2_RET "500 18983213 FAIL\r\n" |
Holger Hans Peter Freyther | 0a6455c | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 66 | #define EMPTY "\r\n" |
| 67 | #define EMPTY_RET NULL |
| 68 | #define SHORT "CRCX \r\n" |
| 69 | #define SHORT_RET "510 000000 FAIL\r\n" |
| 70 | |
Holger Hans Peter Freyther | 74645f4 | 2012-12-10 12:54:57 +0100 | [diff] [blame] | 71 | #define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n" |
| 72 | #define MDCX_ERR_RET "510 18983213 FAIL\r\n" |
| 73 | #define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n" |
| 74 | #define MDCX_RET "400 18983214 FAIL\r\n" |
| 75 | #define MDCX3 "MDCX 18983215 1@mgw MGCP 1.0\r\n" |
| 76 | #define MDCX3_RET "200 18983215 OK\r\n" \ |
| 77 | "I: 1\n" \ |
| 78 | "\n" \ |
| 79 | "v=0\r\n" \ |
| 80 | "o=- 1 23 IN IP4 0.0.0.0\r\n" \ |
| 81 | "c=IN IP4 0.0.0.0\r\n" \ |
| 82 | "t=0 0\r\n" \ |
| 83 | "m=audio 0 RTP/AVP 126\r\n" \ |
| 84 | "a=rtpmap:126 AMR/8000\r\n" |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 85 | #define MDCX4 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \ |
| 86 | "C: 2\r\n" \ |
| 87 | "I: 1\r\n" \ |
| 88 | "L: p:20, a:AMR, nt:IN\r\n" \ |
| 89 | "\n" \ |
| 90 | "v=0\r\n" \ |
| 91 | "o=- 1 23 IN IP4 0.0.0.0\r\n" \ |
| 92 | "c=IN IP4 0.0.0.0\r\n" \ |
| 93 | "t=0 0\r\n" \ |
| 94 | "m=audio 4441 RTP/AVP 99\r\n" \ |
| 95 | "a=rtpmap:99 AMR/8000\r\n" |
| 96 | #define MDCX4_RET "200 18983216 OK\r\n" \ |
| 97 | "I: 1\n" \ |
| 98 | "\n" \ |
| 99 | "v=0\r\n" \ |
| 100 | "o=- 1 23 IN IP4 0.0.0.0\r\n" \ |
| 101 | "c=IN IP4 0.0.0.0\r\n" \ |
| 102 | "t=0 0\r\n" \ |
| 103 | "m=audio 0 RTP/AVP 126\r\n" \ |
| 104 | "a=rtpmap:126 AMR/8000\r\n" |
Holger Hans Peter Freyther | 74645f4 | 2012-12-10 12:54:57 +0100 | [diff] [blame] | 105 | |
Holger Hans Peter Freyther | 0a6455c | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 106 | #define SHORT2 "CRCX 1" |
| 107 | #define SHORT2_RET "510 000000 FAIL\r\n" |
| 108 | #define SHORT3 "CRCX 1 1@mgw" |
| 109 | #define SHORT4 "CRCX 1 1@mgw MGCP" |
| 110 | #define SHORT5 "CRCX 1 1@mgw MGCP 1.0" |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 111 | |
Harald Welte | ca765cd | 2012-01-27 00:37:23 +0100 | [diff] [blame] | 112 | #define CRCX "CRCX 2 1@mgw MGCP 1.0\r\n" \ |
| 113 | "M: sendrecv\r\n" \ |
| 114 | "C: 2\r\n" \ |
| 115 | "\r\n" \ |
| 116 | "v=0\r\n" \ |
| 117 | "c=IN IP4 123.12.12.123\r\n" \ |
| 118 | "m=audio 5904 RTP/AVP 97\r\n" \ |
| 119 | "a=rtpmap:97 GSM-EFR/8000\r\n" |
| 120 | |
| 121 | #define CRCX_RET "200 2 OK\r\n" \ |
| 122 | "I: 1\n" \ |
| 123 | "\n" \ |
| 124 | "v=0\r\n" \ |
| 125 | "o=- 1 23 IN IP4 0.0.0.0\r\n" \ |
| 126 | "c=IN IP4 0.0.0.0\r\n" \ |
| 127 | "t=0 0\r\n" \ |
| 128 | "m=audio 0 RTP/AVP 126\r\n" \ |
| 129 | "a=rtpmap:126 AMR/8000\r\n" |
| 130 | |
| 131 | |
| 132 | #define CRCX_ZYN "CRCX 2 1@mgw MGCP 1.0\r" \ |
| 133 | "M: sendrecv\r" \ |
| 134 | "C: 2\r\r" \ |
| 135 | "v=0\r" \ |
| 136 | "c=IN IP4 123.12.12.123\r" \ |
| 137 | "m=audio 5904 RTP/AVP 97\r" \ |
| 138 | "a=rtpmap:97 GSM-EFR/8000\r" |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 139 | |
Holger Hans Peter Freyther | 0e8e18e | 2012-12-07 14:42:03 +0100 | [diff] [blame] | 140 | #define CRCX_ZYN_RET "200 2 OK\r\n" \ |
| 141 | "I: 2\n" \ |
| 142 | "\n" \ |
| 143 | "v=0\r\n" \ |
| 144 | "o=- 2 23 IN IP4 0.0.0.0\r\n" \ |
| 145 | "c=IN IP4 0.0.0.0\r\n" \ |
| 146 | "t=0 0\r\n" \ |
| 147 | "m=audio 0 RTP/AVP 126\r\n" \ |
| 148 | "a=rtpmap:126 AMR/8000\r\n" |
| 149 | |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 150 | #define DLCX "DLCX 7 1@mgw MGCP 1.0\r\n" \ |
| 151 | "C: 2\r\n" |
| 152 | |
| 153 | #define DLCX_RET "250 7 OK\r\n" \ |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 154 | "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \ |
| 155 | "X-Osmo-CP: EC TIS=0, TOS=0, TIR=0, TOR=0\r\n" |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 156 | |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 157 | #define RQNT "RQNT 186908780 1@mgw MGCP 1.0\r\n" \ |
| 158 | "X: B244F267488\r\n" \ |
| 159 | "S: D/9\r\n" |
| 160 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 161 | #define RQNT2 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \ |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 162 | "X: ADD4F26746F\r\n" \ |
| 163 | "R: D/[0-9#*](N), G/ft, fxr/t38\r\n" |
| 164 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 165 | #define RQNT1_RET "200 186908780 OK\r\n" |
| 166 | #define RQNT2_RET "200 186908781 OK\r\n" |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 167 | |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 168 | #define PTYPE_IGNORE 0 /* == default initializer */ |
| 169 | #define PTYPE_NONE 128 |
| 170 | #define PTYPE_NYI PTYPE_NONE |
| 171 | |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 172 | struct mgcp_test { |
| 173 | const char *name; |
| 174 | const char *req; |
| 175 | const char *exp_resp; |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 176 | int exp_net_ptype; |
| 177 | int exp_bts_ptype; |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
Holger Hans Peter Freyther | 0e8e18e | 2012-12-07 14:42:03 +0100 | [diff] [blame] | 180 | static const struct mgcp_test tests[] = { |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 181 | { "AUEP1", AUEP1, AUEP1_RET }, |
| 182 | { "AUEP2", AUEP2, AUEP2_RET }, |
Holger Hans Peter Freyther | 74645f4 | 2012-12-10 12:54:57 +0100 | [diff] [blame] | 183 | { "MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET }, |
| 184 | { "MDCX2", MDCX_UNALLOCATED, MDCX_RET }, |
Jacob Erlbeck | e3440c7 | 2013-11-29 13:43:49 +0100 | [diff] [blame] | 185 | { "CRCX", CRCX, CRCX_RET, 97, 126 }, |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 186 | { "MDCX3", MDCX3, MDCX3_RET, PTYPE_NONE, 126 }, |
| 187 | { "MDCX4", MDCX4, MDCX4_RET, 99, 126 }, |
| 188 | { "DLCX", DLCX, DLCX_RET, -1, -1 }, |
Jacob Erlbeck | e3440c7 | 2013-11-29 13:43:49 +0100 | [diff] [blame] | 189 | { "CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97, 126 }, |
Holger Hans Peter Freyther | 0a6455c | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 190 | { "EMPTY", EMPTY, EMPTY_RET }, |
| 191 | { "SHORT1", SHORT, SHORT_RET }, |
| 192 | { "SHORT2", SHORT2, SHORT2_RET }, |
| 193 | { "SHORT3", SHORT3, SHORT2_RET }, |
| 194 | { "SHORT4", SHORT4, SHORT2_RET }, |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 195 | { "RQNT1", RQNT, RQNT1_RET }, |
| 196 | { "RQNT2", RQNT2, RQNT2_RET }, |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 197 | { "DLCX", DLCX, DLCX_RET, -1, -1 }, |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static const struct mgcp_test retransmit[] = { |
| 201 | { "CRCX", CRCX, CRCX_RET }, |
| 202 | { "RQNT1", RQNT, RQNT1_RET }, |
| 203 | { "RQNT2", RQNT2, RQNT2_RET }, |
| 204 | { "MDCX3", MDCX3, MDCX3_RET }, |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 205 | { "DLCX", DLCX, DLCX_RET }, |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 206 | }; |
Harald Welte | 834f3f1 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 207 | |
| 208 | static struct msgb *create_msg(const char *str) |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 209 | { |
| 210 | struct msgb *msg; |
| 211 | |
| 212 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
Harald Welte | 834f3f1 | 2012-01-27 00:00:44 +0100 | [diff] [blame] | 213 | int len = sprintf((char *)msg->data, str); |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 214 | msg->l2h = msgb_put(msg, len); |
| 215 | return msg; |
| 216 | } |
| 217 | |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 218 | static int last_endpoint = -1; |
| 219 | |
| 220 | static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint, |
| 221 | int state, const char *transactio_id) |
| 222 | { |
| 223 | fprintf(stderr, "Policy CB got state %d on endpoint %d\n", |
| 224 | state, endpoint); |
| 225 | last_endpoint = endpoint; |
| 226 | return MGCP_POLICY_CONT; |
| 227 | } |
| 228 | |
Holger Hans Peter Freyther | 06ce839 | 2012-03-16 09:45:13 +0100 | [diff] [blame] | 229 | static void test_messages(void) |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 230 | { |
Holger Hans Peter Freyther | 38484aa | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 231 | struct mgcp_config *cfg; |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 232 | struct mgcp_endpoint *endp; |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 233 | int i; |
Holger Hans Peter Freyther | 38484aa | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 234 | |
| 235 | cfg = mgcp_config_alloc(); |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 236 | |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 237 | cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 238 | mgcp_endpoints_allocate(&cfg->trunk); |
| 239 | |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 240 | cfg->policy_cb = mgcp_test_policy_cb; |
| 241 | |
Holger Hans Peter Freyther | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 242 | mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1)); |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 243 | |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 244 | /* reset endpoints */ |
| 245 | for (i = 0; i < cfg->trunk.number_endpoints; i++) { |
| 246 | endp = &cfg->trunk.endpoints[i]; |
| 247 | endp->net_end.payload_type = PTYPE_NONE; |
| 248 | } |
| 249 | |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 250 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
| 251 | const struct mgcp_test *t = &tests[i]; |
| 252 | struct msgb *inp; |
| 253 | struct msgb *msg; |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 254 | |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 255 | printf("Testing %s\n", t->name); |
| 256 | |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 257 | last_endpoint = -1; |
| 258 | |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 259 | inp = create_msg(t->req); |
| 260 | msg = mgcp_handle_message(cfg, inp); |
| 261 | msgb_free(inp); |
Holger Hans Peter Freyther | 0a6455c | 2012-03-16 09:53:49 +0100 | [diff] [blame] | 262 | if (!t->exp_resp) { |
| 263 | if (msg) |
| 264 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 265 | } else if (strcmp((char *) msg->data, t->exp_resp) != 0) |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 266 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 267 | msgb_free(msg); |
Jacob Erlbeck | fe9896f | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 268 | |
| 269 | /* Check detected payload type */ |
| 270 | if (t->exp_net_ptype != PTYPE_IGNORE || |
| 271 | t->exp_bts_ptype != PTYPE_IGNORE) { |
| 272 | OSMO_ASSERT(last_endpoint != -1); |
| 273 | endp = &cfg->trunk.endpoints[last_endpoint]; |
| 274 | |
| 275 | fprintf(stderr, "endpoint %d: " |
| 276 | "payload type BTS %d (exp %d), NET %d (exp %d)\n", |
| 277 | last_endpoint, |
| 278 | endp->bts_end.payload_type, t->exp_bts_ptype, |
| 279 | endp->net_end.payload_type, t->exp_net_ptype); |
| 280 | |
| 281 | if (t->exp_bts_ptype != PTYPE_IGNORE) |
| 282 | OSMO_ASSERT(endp->bts_end.payload_type == |
| 283 | t->exp_bts_ptype); |
| 284 | if (t->exp_net_ptype != PTYPE_IGNORE) |
| 285 | OSMO_ASSERT(endp->net_end.payload_type == |
| 286 | t->exp_net_ptype); |
| 287 | |
| 288 | /* Reset them again for next test */ |
| 289 | endp->net_end.payload_type = PTYPE_NONE; |
| 290 | } |
Harald Welte | a7c9a92 | 2012-01-27 00:36:56 +0100 | [diff] [blame] | 291 | } |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 292 | |
| 293 | talloc_free(cfg); |
| 294 | } |
| 295 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 296 | static void test_retransmission(void) |
| 297 | { |
| 298 | struct mgcp_config *cfg; |
| 299 | int i; |
| 300 | |
| 301 | cfg = mgcp_config_alloc(); |
| 302 | |
| 303 | cfg->trunk.number_endpoints = 64; |
| 304 | mgcp_endpoints_allocate(&cfg->trunk); |
| 305 | |
| 306 | mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1)); |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(retransmit); i++) { |
| 309 | const struct mgcp_test *t = &retransmit[i]; |
| 310 | struct msgb *inp; |
| 311 | struct msgb *msg; |
| 312 | |
| 313 | printf("Testing %s\n", t->name); |
| 314 | |
| 315 | inp = create_msg(t->req); |
| 316 | msg = mgcp_handle_message(cfg, inp); |
| 317 | msgb_free(inp); |
| 318 | if (strcmp((char *) msg->data, t->exp_resp) != 0) |
| 319 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 320 | msgb_free(msg); |
| 321 | |
| 322 | /* Retransmit... */ |
| 323 | printf("Re-transmitting %s\n", t->name); |
| 324 | inp = create_msg(t->req); |
| 325 | msg = mgcp_handle_message(cfg, inp); |
| 326 | msgb_free(inp); |
| 327 | if (strcmp((char *) msg->data, t->exp_resp) != 0) |
| 328 | printf("%s failed '%s'\n", t->name, (char *) msg->data); |
| 329 | msgb_free(msg); |
| 330 | } |
| 331 | |
| 332 | talloc_free(cfg); |
| 333 | } |
| 334 | |
| 335 | static int rqnt_cb(struct mgcp_endpoint *endp, char _tone) |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 336 | { |
| 337 | ptrdiff_t tone = _tone; |
| 338 | endp->cfg->data = (void *) tone; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void test_rqnt_cb(void) |
| 343 | { |
| 344 | struct mgcp_config *cfg; |
| 345 | struct msgb *inp, *msg; |
| 346 | |
| 347 | cfg = mgcp_config_alloc(); |
| 348 | cfg->rqnt_cb = rqnt_cb; |
| 349 | |
| 350 | cfg->trunk.number_endpoints = 64; |
| 351 | mgcp_endpoints_allocate(&cfg->trunk); |
| 352 | |
| 353 | mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1)); |
| 354 | |
| 355 | inp = create_msg(CRCX); |
| 356 | msgb_free(mgcp_handle_message(cfg, inp)); |
| 357 | msgb_free(inp); |
| 358 | |
| 359 | /* send the RQNT and check for the CB */ |
| 360 | inp = create_msg(RQNT); |
| 361 | msg = mgcp_handle_message(cfg, inp); |
| 362 | if (strncmp((const char *) msg->l2h, "200", 3) != 0) { |
| 363 | printf("FAILED: message is not 200. '%s'\n", msg->l2h); |
| 364 | abort(); |
| 365 | } |
| 366 | |
| 367 | if (cfg->data != (void *) '9') { |
| 368 | printf("FAILED: callback not called: %p\n", cfg->data); |
| 369 | abort(); |
| 370 | } |
| 371 | |
| 372 | msgb_free(msg); |
| 373 | msgb_free(inp); |
| 374 | |
| 375 | inp = create_msg(DLCX); |
| 376 | msgb_free(mgcp_handle_message(cfg, inp)); |
| 377 | msgb_free(inp); |
| 378 | talloc_free(cfg); |
| 379 | } |
| 380 | |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 381 | struct pl_test { |
| 382 | int cycles; |
| 383 | uint16_t base_seq; |
| 384 | uint16_t max_seq; |
| 385 | uint32_t packets; |
| 386 | |
| 387 | uint32_t expected; |
| 388 | int loss; |
| 389 | }; |
| 390 | |
| 391 | static const struct pl_test pl_test_dat[] = { |
| 392 | /* basic.. just one package */ |
| 393 | { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = 1, .expected = 1, .loss = 0}, |
| 394 | /* some packages and a bit of loss */ |
| 395 | { .cycles = 0, .base_seq = 0, .max_seq = 100, .packets = 100, .expected = 101, .loss = 1}, |
| 396 | /* wrap around */ |
| 397 | { .cycles = 1<<16, .base_seq = 0xffff, .max_seq = 2, .packets = 4, .expected = 4, .loss = 0}, |
| 398 | /* min loss */ |
| 399 | { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = UINT_MAX, .expected = 1, .loss = INT_MIN }, |
| 400 | /* max loss, with wrap around on expected max */ |
| 401 | { .cycles = INT_MAX, .base_seq = 0, .max_seq = UINT16_MAX, .packets = 0, .expected = ((uint32_t)(INT_MAX) + UINT16_MAX + 1), .loss = INT_MAX }, |
| 402 | }; |
| 403 | |
| 404 | static void test_packet_loss_calc(void) |
| 405 | { |
| 406 | int i; |
| 407 | printf("Testing packet loss calculation.\n"); |
| 408 | |
| 409 | for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) { |
| 410 | uint32_t expected; |
| 411 | int loss; |
| 412 | struct mgcp_rtp_state state; |
| 413 | struct mgcp_rtp_end rtp; |
| 414 | memset(&state, 0, sizeof(state)); |
| 415 | memset(&rtp, 0, sizeof(rtp)); |
| 416 | |
| 417 | state.initialized = 1; |
| 418 | state.base_seq = pl_test_dat[i].base_seq; |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 419 | state.out_stream.last_seq = pl_test_dat[i].max_seq; |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 420 | state.cycles = pl_test_dat[i].cycles; |
| 421 | |
| 422 | rtp.packets = pl_test_dat[i].packets; |
| 423 | mgcp_state_calc_loss(&state, &rtp, &expected, &loss); |
| 424 | |
| 425 | if (loss != pl_test_dat[i].loss || expected != pl_test_dat[i].expected) { |
| 426 | printf("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n", |
| 427 | i, loss, pl_test_dat[i].loss, |
| 428 | expected, pl_test_dat[i].expected); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Holger Hans Peter Freyther | 931d698 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 433 | static void test_mgcp_stats(void) |
| 434 | { |
| 435 | printf("Testing stat parsing\n"); |
| 436 | |
| 437 | uint32_t bps, bos, pr, _or, jitter; |
| 438 | struct msgb *msg; |
| 439 | int loss; |
| 440 | int rc; |
| 441 | |
| 442 | msg = create_msg(DLCX_RET); |
| 443 | rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter); |
| 444 | printf("Parsing result: %d\n", rc); |
| 445 | if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0 || jitter != 0) |
| 446 | printf("FAIL: Parsing failed1.\n"); |
| 447 | msgb_free(msg); |
| 448 | |
| 449 | msg = create_msg("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n"); |
| 450 | rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter); |
| 451 | printf("Parsing result: %d\n", rc); |
| 452 | if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3 || jitter != 40) |
| 453 | printf("FAIL: Parsing failed2.\n"); |
| 454 | msgb_free(msg); |
| 455 | } |
| 456 | |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 457 | struct rtp_packet_info { |
| 458 | float txtime; |
| 459 | int len; |
| 460 | char *data; |
| 461 | }; |
| 462 | |
| 463 | struct rtp_packet_info test_rtp_packets1[] = { |
| 464 | /* RTP: SeqNo=0, TS=0 */ |
| 465 | {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44" |
| 466 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 467 | /* RTP: SeqNo=1, TS=160 */ |
| 468 | {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 469 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 470 | /* RTP: SeqNo=2, TS=320 */ |
| 471 | {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44" |
| 472 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 473 | /* Repeat RTP timestamp: */ |
| 474 | /* RTP: SeqNo=3, TS=320 */ |
| 475 | {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44" |
| 476 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 477 | /* RTP: SeqNo=4, TS=480 */ |
| 478 | {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44" |
| 479 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 480 | /* RTP: SeqNo=5, TS=640 */ |
| 481 | {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44" |
| 482 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 483 | /* Double skip RTP timestamp (delta = 2*160): */ |
| 484 | /* RTP: SeqNo=6, TS=960 */ |
| 485 | {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44" |
| 486 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 487 | /* RTP: SeqNo=7, TS=1120 */ |
| 488 | {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44" |
| 489 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 490 | /* RTP: SeqNo=8, TS=1280 */ |
| 491 | {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44" |
| 492 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 493 | /* Non 20ms RTP timestamp (delta = 120): */ |
| 494 | /* RTP: SeqNo=9, TS=1400 */ |
| 495 | {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44" |
| 496 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 497 | /* RTP: SeqNo=10, TS=1560 */ |
| 498 | {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44" |
| 499 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 500 | /* RTP: SeqNo=11, TS=1720 */ |
| 501 | {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44" |
| 502 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 503 | /* SSRC changed to 0x10203040, RTP timestamp jump */ |
| 504 | /* RTP: SeqNo=12, TS=34688 */ |
| 505 | {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40" |
| 506 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 507 | /* RTP: SeqNo=13, TS=34848 */ |
| 508 | {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40" |
| 509 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 510 | /* RTP: SeqNo=14, TS=35008 */ |
| 511 | {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40" |
| 512 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 513 | /* Non 20ms RTP timestamp (delta = 120): */ |
| 514 | /* RTP: SeqNo=15, TS=35128 */ |
| 515 | {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40" |
| 516 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 517 | /* RTP: SeqNo=16, TS=35288 */ |
| 518 | {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40" |
| 519 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 520 | /* RTP: SeqNo=17, TS=35448 */ |
| 521 | {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40" |
| 522 | "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"}, |
| 523 | /* SeqNo increment by 2, RTP timestamp delta = 320: */ |
| 524 | /* RTP: SeqNo=19, TS=35768 */ |
| 525 | {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40" |
| 526 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 527 | /* RTP: SeqNo=20, TS=35928 */ |
| 528 | {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40" |
| 529 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| 530 | /* RTP: SeqNo=21, TS=36088 */ |
| 531 | {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\xF8\x10\x20\x30\x40" |
| 532 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 536 | struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr, |
| 537 | char *data, int len); |
| 538 | |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 539 | static void test_packet_error_detection(int patch_ssrc, int patch_ts) |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 540 | { |
| 541 | int i; |
| 542 | |
| 543 | struct mgcp_trunk_config trunk; |
| 544 | struct mgcp_endpoint endp; |
| 545 | struct mgcp_rtp_state state; |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 546 | struct mgcp_rtp_end *rtp = &endp.net_end; |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 547 | struct sockaddr_in addr = {0}; |
| 548 | char buffer[4096]; |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 549 | uint32_t last_ssrc = 0; |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 550 | |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 551 | printf("Testing packet error detection%s%s.\n", |
| 552 | patch_ssrc ? ", patch SSRC" : "", |
| 553 | patch_ts ? ", patch timestamps" : ""); |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 554 | |
| 555 | memset(&trunk, 0, sizeof(trunk)); |
| 556 | memset(&endp, 0, sizeof(endp)); |
| 557 | memset(&state, 0, sizeof(state)); |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 558 | |
| 559 | trunk.number_endpoints = 1; |
| 560 | trunk.endpoints = &endp; |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 561 | endp.tcfg = &trunk; |
| 562 | |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 563 | /* This doesn't free endp but resets/frees all fields of the structure |
| 564 | * and invokes mgcp_rtp_end_reset() for each mgcp_rtp_end. OTOH, it |
| 565 | * expects valid pointer fields (either NULL or talloc'ed), so the |
| 566 | * memset is still needed. It also requires that endp.tcfg and |
| 567 | * trunk.endpoints are set up properly. */ |
| 568 | mgcp_free_endp(&endp); |
| 569 | |
| 570 | rtp->payload_type = 98; |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 571 | endp.allow_patch = patch_ssrc; |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 572 | |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 573 | for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) { |
| 574 | struct rtp_packet_info *info = test_rtp_packets1 + i; |
| 575 | |
| 576 | OSMO_ASSERT(info->len <= sizeof(buffer)); |
| 577 | OSMO_ASSERT(info->len >= 0); |
| 578 | memmove(buffer, info->data, info->len); |
| 579 | |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 580 | mgcp_patch_and_count(&endp, &state, rtp, &addr, |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 581 | buffer, info->len); |
| 582 | |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 583 | if (state.out_stream.ssrc != last_ssrc) { |
| 584 | printf("Output SSRC changed to %08x\n", |
| 585 | ntohl(state.out_stream.ssrc)); |
| 586 | last_ssrc = state.out_stream.ssrc; |
| 587 | } |
| 588 | |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 589 | printf("TS: %d, dTS: %d, TS Errs: in %d, out %d\n", |
| 590 | state.out_stream.last_timestamp, |
| 591 | state.out_stream.last_tsdelta, |
| 592 | state.in_stream.err_ts_counter, |
| 593 | state.out_stream.err_ts_counter); |
| 594 | } |
| 595 | } |
| 596 | |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 597 | int main(int argc, char **argv) |
| 598 | { |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 599 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 600 | |
Jacob Erlbeck | e7060c4 | 2013-11-29 13:43:44 +0100 | [diff] [blame] | 601 | test_strline(); |
Holger Hans Peter Freyther | 06ce839 | 2012-03-16 09:45:13 +0100 | [diff] [blame] | 602 | test_messages(); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 603 | test_retransmission(); |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 604 | test_packet_loss_calc(); |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 605 | test_rqnt_cb(); |
Holger Hans Peter Freyther | 931d698 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 606 | test_mgcp_stats(); |
Jacob Erlbeck | eb19f55 | 2013-12-03 12:48:07 +0100 | [diff] [blame] | 607 | test_packet_error_detection(1, 0); |
| 608 | test_packet_error_detection(0, 0); |
| 609 | test_packet_error_detection(0, 1); |
| 610 | test_packet_error_detection(1, 1); |
Holger Hans Peter Freyther | 38484aa | 2012-01-06 15:04:43 +0100 | [diff] [blame] | 611 | |
| 612 | printf("Done\n"); |
| 613 | return EXIT_SUCCESS; |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 614 | } |