Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * Author: Neels Hofmeyr |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
Neels Hofmeyr | bd86f94 | 2018-02-21 16:45:38 +0100 | [diff] [blame] | 21 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 22 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/application.h> |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 27 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 28 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 30 | |
| 31 | void *ctx; |
| 32 | |
| 33 | #define buf_len 4096 |
| 34 | |
| 35 | #if 0 |
| 36 | static struct msgb *from_hex(const char *hex) |
| 37 | { |
| 38 | struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex"); |
| 39 | unsigned int l = osmo_hexparse(hex, msg->data, buf_len); |
| 40 | msg->l2h = msgb_put(msg, l); |
| 41 | return msg; |
| 42 | } |
| 43 | |
| 44 | static struct msgb *mgcp_from_str(const char *head, const char *params) |
| 45 | { |
| 46 | struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str"); |
| 47 | unsigned int l; |
| 48 | char *data; |
| 49 | l = strlen(head); |
| 50 | msg->l2h = msgb_put(msg, l); |
| 51 | data = (char*)msgb_l2(msg); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 52 | osmo_strlcpy(data, head, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 53 | |
| 54 | data = (char*)msgb_put(msg, 1); |
| 55 | *data = '\n'; |
| 56 | |
| 57 | l = strlen(params); |
| 58 | data = (char*)msgb_put(msg, l); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 59 | osmo_strlcpy(data, params, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 60 | |
| 61 | return msg; |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | static struct msgb *from_str(const char *str) |
| 66 | { |
| 67 | struct msgb *msg = msgb_alloc(buf_len, "from_str"); |
| 68 | unsigned int l = strlen(str); |
| 69 | char *data; |
| 70 | msg->l2h = msgb_put(msg, l); |
| 71 | data = (char*)msgb_l2(msg); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 72 | osmo_strlcpy(data, str, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 73 | return msg; |
| 74 | } |
| 75 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 76 | static struct mgcp_client_conf conf; |
| 77 | struct mgcp_client *mgcp = NULL; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 78 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 79 | static int reply_to(mgcp_trans_id_t trans_id, int code, const char *comment, |
Neels Hofmeyr | 1d12148 | 2018-09-03 21:05:13 +0200 | [diff] [blame] | 80 | const char *params) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 81 | { |
| 82 | static char compose[4096 - 128]; |
| 83 | int len; |
| 84 | |
| 85 | len = snprintf(compose, sizeof(compose), |
Neels Hofmeyr | 1d12148 | 2018-09-03 21:05:13 +0200 | [diff] [blame] | 86 | "%d %u %s\r\n%s", |
| 87 | code, trans_id, comment, params); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 88 | OSMO_ASSERT(len < sizeof(compose)); |
| 89 | OSMO_ASSERT(len > 0); |
| 90 | |
| 91 | printf("composed response:\n-----\n%s\n-----\n", |
| 92 | compose); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 93 | return mgcp_client_rx(mgcp, from_str(compose)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void test_response_cb(struct mgcp_response *response, void *priv) |
| 97 | { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 98 | unsigned int i; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 99 | OSMO_ASSERT(priv == mgcp); |
| 100 | mgcp_response_parse_params(response); |
| 101 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 102 | printf("response cb received:\n"); |
| 103 | printf(" head.response_code = %d\n", response->head.response_code); |
| 104 | printf(" head.trans_id = %u\n", response->head.trans_id); |
Neels Hofmeyr | 40f5033 | 2018-09-03 21:12:48 +0200 | [diff] [blame] | 105 | printf(" head.conn_id = %s\n", response->head.conn_id); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 106 | printf(" head.comment = %s\n", response->head.comment); |
| 107 | printf(" audio_port = %u\n", response->audio_port); |
| 108 | printf(" audio_ip = %s\n", response->audio_ip); |
| 109 | printf(" ptime = %u\n", response->ptime); |
| 110 | printf(" codecs_len = %u\n", response->codecs_len); |
| 111 | for(i=0;i<response->codecs_len;i++) |
| 112 | printf(" codecs[%u] = %u\n", i, response->codecs[i]); |
| 113 | printf(" ptmap_len = %u\n", response->ptmap_len); |
| 114 | for(i=0;i<response->ptmap_len;i++) { |
| 115 | printf(" ptmap[%u].codec = %u\n", i, response->ptmap[i].codec); |
| 116 | printf(" ptmap[%u].pt = %u\n", i, response->ptmap[i].pt); |
| 117 | } |
| 118 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) |
| 122 | { |
| 123 | mgcp_trans_id_t trans_id; |
| 124 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 125 | char *end; |
| 126 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 127 | OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 128 | |
| 129 | end = (char*)msgb_put(msg, 1); |
| 130 | *end = '\0'; |
| 131 | printf("composed:\n-----\n%s\n-----\n", |
| 132 | (char*)msgb_l2(msg)); |
| 133 | |
| 134 | talloc_free(msg); |
| 135 | return trans_id; |
| 136 | } |
| 137 | |
| 138 | void test_crcx(void) |
| 139 | { |
| 140 | struct msgb *msg; |
| 141 | mgcp_trans_id_t trans_id; |
| 142 | |
| 143 | printf("\n===== %s =====\n", __func__); |
| 144 | |
| 145 | if (mgcp) |
| 146 | talloc_free(mgcp); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 147 | mgcp = mgcp_client_init(ctx, &conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 148 | |
| 149 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 150 | trans_id = dummy_mgcp_send(msg); |
| 151 | |
Neels Hofmeyr | 1d12148 | 2018-09-03 21:05:13 +0200 | [diff] [blame] | 152 | reply_to(trans_id, 200, "OK", |
Neels Hofmeyr | b1bb1fa | 2018-09-03 21:07:26 +0200 | [diff] [blame] | 153 | "I: 1\r\n\r\n" |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 154 | "v=0\r\n" |
| 155 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 156 | "s=-\r\n" |
| 157 | "c=IN IP4 10.9.1.120\r\n" |
| 158 | "t=0 0\r\n" |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 159 | "m=audio 16002 RTP/AVP 110 96\r\n" |
| 160 | "a=rtpmap:110 AMR/8000\r\n" |
| 161 | "a=rtpmap:96 GSM-EFR/8000\r\n" |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 162 | "a=ptime:20\r\n"); |
| 163 | } |
| 164 | |
Neels Hofmeyr | 10d487e | 2018-09-03 21:13:01 +0200 | [diff] [blame] | 165 | void test_crcx_long_conn_id(void) |
| 166 | { |
| 167 | struct msgb *msg; |
| 168 | mgcp_trans_id_t trans_id; |
| 169 | |
| 170 | printf("\n===== %s =====\n", __func__); |
| 171 | |
| 172 | if (mgcp) |
| 173 | talloc_free(mgcp); |
| 174 | mgcp = mgcp_client_init(ctx, &conf); |
| 175 | |
| 176 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 177 | trans_id = dummy_mgcp_send(msg); |
| 178 | |
| 179 | reply_to(trans_id, 200, "OK", |
| 180 | "I: 123456789abcdef0123456789ABCDEF0\r\n\r\n" |
| 181 | "v=0\r\n" |
| 182 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 183 | "s=-\r\n" |
| 184 | "c=IN IP4 10.9.1.120\r\n" |
| 185 | "t=0 0\r\n" |
| 186 | "m=audio 16002 RTP/AVP 110 96\r\n" |
| 187 | "a=rtpmap:110 AMR/8000\r\n" |
| 188 | "a=rtpmap:96 GSM-EFR/8000\r\n" |
| 189 | "a=ptime:20\r\n"); |
| 190 | } |
| 191 | |
| 192 | void test_crcx_too_long_conn_id(void) |
| 193 | { |
| 194 | struct msgb *msg; |
| 195 | mgcp_trans_id_t trans_id; |
| 196 | |
| 197 | printf("\n===== %s =====\n", __func__); |
| 198 | |
| 199 | if (mgcp) |
| 200 | talloc_free(mgcp); |
| 201 | mgcp = mgcp_client_init(ctx, &conf); |
| 202 | |
| 203 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 204 | trans_id = dummy_mgcp_send(msg); |
| 205 | |
| 206 | reply_to(trans_id, 200, "OK", |
| 207 | "I: 123456789abcdef0123456789ABCDEF01001029\r\n\r\n" |
| 208 | "v=0\r\n" |
| 209 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 210 | "s=-\r\n" |
| 211 | "c=IN IP4 10.9.1.120\r\n" |
| 212 | "t=0 0\r\n" |
| 213 | "m=audio 16002 RTP/AVP 110 96\r\n" |
| 214 | "a=rtpmap:110 AMR/8000\r\n" |
| 215 | "a=rtpmap:96 GSM-EFR/8000\r\n" |
| 216 | "a=ptime:20\r\n"); |
| 217 | } |
| 218 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 219 | void test_mgcp_msg(void) |
| 220 | { |
| 221 | struct msgb *msg; |
| 222 | char audio_ip_overflow[5000]; |
| 223 | |
| 224 | /* A message struct prefilled with some arbitary values */ |
| 225 | struct mgcp_msg mgcp_msg = { |
| 226 | .audio_ip = "192.168.100.23", |
| 227 | .endpoint = "23@mgw", |
| 228 | .audio_port = 1234, |
| 229 | .call_id = 47, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 230 | .conn_id = "11", |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 231 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 232 | .ptime = 20, |
| 233 | .codecs[0] = CODEC_GSM_8000_1, |
| 234 | .codecs[1] = CODEC_AMR_8000_1, |
| 235 | .codecs[2] = CODEC_GSMEFR_8000_1, |
| 236 | .codecs_len = 1, |
| 237 | .ptmap[0].codec = CODEC_GSMEFR_8000_1, |
| 238 | .ptmap[0].pt = 96, |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 239 | .ptmap_len = 1, |
| 240 | .x_osmo_ign = MGCP_X_OSMO_IGN_CALLID, |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | if (mgcp) |
| 244 | talloc_free(mgcp); |
| 245 | mgcp = mgcp_client_init(ctx, &conf); |
| 246 | |
| 247 | printf("\n"); |
| 248 | |
| 249 | printf("Generated CRCX message:\n"); |
| 250 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 251 | mgcp_msg.presence = |
| 252 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 253 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 254 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 255 | printf("%s\n", (char *)msg->data); |
| 256 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 257 | printf("Generated CRCX message (two codecs):\n"); |
| 258 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 259 | mgcp_msg.presence = |
| 260 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 261 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 262 | mgcp_msg.codecs_len = 2; |
| 263 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 264 | mgcp_msg.codecs_len = 1; |
| 265 | printf("%s\n", (char *)msg->data); |
| 266 | |
| 267 | printf("Generated CRCX message (three codecs, one with custom pt):\n"); |
| 268 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 269 | mgcp_msg.presence = |
| 270 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 271 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 272 | mgcp_msg.codecs_len = 3; |
| 273 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 274 | mgcp_msg.codecs_len = 1; |
| 275 | printf("%s\n", (char *)msg->data); |
| 276 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 277 | printf("Generated MDCX message:\n"); |
| 278 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 279 | mgcp_msg.presence = |
| 280 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 281 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 282 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 283 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 284 | printf("%s\n", (char *)msg->data); |
| 285 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 286 | printf("Generated MDCX message (two codecs):\n"); |
| 287 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 288 | mgcp_msg.presence = |
| 289 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 290 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 291 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 292 | mgcp_msg.codecs_len = 2; |
| 293 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 294 | mgcp_msg.codecs_len = 1; |
| 295 | printf("%s\n", (char *)msg->data); |
| 296 | |
| 297 | printf("Generated MDCX message (three codecs, one with custom pt):\n"); |
| 298 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 299 | mgcp_msg.presence = |
| 300 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 301 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 302 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 303 | mgcp_msg.codecs_len = 3; |
| 304 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 305 | mgcp_msg.codecs_len = 1; |
| 306 | printf("%s\n", (char *)msg->data); |
| 307 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 308 | printf("Generated DLCX message:\n"); |
| 309 | mgcp_msg.verb = MGCP_VERB_DLCX; |
| 310 | mgcp_msg.presence = |
| 311 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 312 | MGCP_MSG_PRESENCE_CONN_ID); |
| 313 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 314 | printf("%s\n", (char *)msg->data); |
| 315 | |
| 316 | printf("Generated AUEP message:\n"); |
| 317 | mgcp_msg.verb = MGCP_VERB_AUEP; |
| 318 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 319 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 320 | printf("%s\n", msg->data); |
| 321 | |
| 322 | printf("Generated RSIP message:\n"); |
| 323 | mgcp_msg.verb = MGCP_VERB_RSIP; |
| 324 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 325 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 326 | printf("%s\n", (char *)msg->data); |
| 327 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 328 | printf("Generate X-Osmo-IGN message:\n"); |
| 329 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 330 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 331 | mgcp_msg.presence = |
| 332 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 333 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
| 334 | | MGCP_MSG_PRESENCE_X_OSMO_IGN); |
| 335 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 336 | printf("%s\n", (char *)msg->data); |
| 337 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 338 | printf("Overfolow test:\n"); |
| 339 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 340 | mgcp_msg.presence = |
| 341 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 342 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 343 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 344 | memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow)); |
| 345 | audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0'; |
| 346 | mgcp_msg.audio_ip = audio_ip_overflow; |
| 347 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 348 | OSMO_ASSERT(msg == NULL); |
| 349 | |
| 350 | printf("\n"); |
| 351 | msgb_free(msg); |
| 352 | } |
| 353 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 354 | void test_mgcp_client_cancel() |
| 355 | { |
| 356 | mgcp_trans_id_t trans_id; |
| 357 | struct msgb *msg; |
| 358 | struct mgcp_msg mgcp_msg = { |
| 359 | .verb = MGCP_VERB_CRCX, |
| 360 | .audio_ip = "192.168.100.23", |
| 361 | .endpoint = "23@mgw", |
| 362 | .audio_port = 1234, |
| 363 | .call_id = 47, |
Philipp Maier | 922ae9a | 2017-12-04 15:53:37 +0100 | [diff] [blame] | 364 | .conn_id = "11", |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 365 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 366 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
| 367 | | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE), |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 368 | .ptime = 20, |
| 369 | .codecs[0] = CODEC_AMR_8000_1, |
| 370 | .codecs_len = 1 |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | printf("\n%s():\n", __func__); |
| 374 | fprintf(stderr, "\n%s():\n", __func__); |
| 375 | |
| 376 | if (mgcp) |
| 377 | talloc_free(mgcp); |
| 378 | mgcp = mgcp_client_init(ctx, &conf); |
| 379 | |
| 380 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 381 | trans_id = mgcp_msg_trans_id(msg); |
| 382 | fprintf(stderr, "- composed msg with trans_id=%u\n", trans_id); |
| 383 | |
| 384 | fprintf(stderr, "- not in queue yet, cannot cancel yet\n"); |
| 385 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 386 | |
| 387 | fprintf(stderr, "- enqueue\n"); |
| 388 | dummy_mgcp_send(msg); |
| 389 | |
| 390 | fprintf(stderr, "- cancel succeeds\n"); |
| 391 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == 0); |
| 392 | |
| 393 | fprintf(stderr, "- late response gets discarded\n"); |
Neels Hofmeyr | b1bb1fa | 2018-09-03 21:07:26 +0200 | [diff] [blame] | 394 | OSMO_ASSERT(reply_to(trans_id, 200, "OK", "I: 1\r\n\r\nv=0\r\n") == -ENOENT); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 395 | |
| 396 | fprintf(stderr, "- canceling again does nothing\n"); |
| 397 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 398 | |
| 399 | fprintf(stderr, "%s() done\n", __func__); |
| 400 | } |
| 401 | |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 402 | struct sdp_section_start_test { |
| 403 | const char *body; |
| 404 | int expect_rc; |
| 405 | struct mgcp_response expect_params; |
| 406 | }; |
| 407 | |
| 408 | static struct sdp_section_start_test sdp_section_start_tests[] = { |
| 409 | { |
| 410 | .body = "", |
| 411 | .expect_rc = -EINVAL, |
| 412 | }, |
| 413 | { |
| 414 | .body = "\n\n", |
| 415 | }, |
| 416 | { |
| 417 | .body = "\r\n\r\n", |
| 418 | }, |
| 419 | { |
| 420 | .body = "\n\r\n\r", |
| 421 | }, |
| 422 | { |
| 423 | .body = "some mgcp header data\r\nand header params" |
| 424 | "\n\n" |
| 425 | "m=audio 23\r\n", |
| 426 | .expect_params = { |
| 427 | .audio_port = 23, |
| 428 | }, |
| 429 | }, |
| 430 | { |
| 431 | .body = "some mgcp header data\r\nand header params" |
| 432 | "\r\n\r\n" |
| 433 | "m=audio 23\r\n", |
| 434 | .expect_params = { |
| 435 | .audio_port = 23, |
| 436 | }, |
| 437 | }, |
| 438 | { |
| 439 | .body = "some mgcp header data\r\nand header params" |
| 440 | "\n\r\n\r" |
| 441 | "m=audio 23\r\n", |
| 442 | .expect_params = { |
| 443 | .audio_port = 23, |
| 444 | }, |
| 445 | }, |
| 446 | { |
| 447 | .body = "some mgcp header data\r\nand header params" |
| 448 | "\n\r\n" |
| 449 | "m=audio 23\r\n", |
| 450 | .expect_rc = -EINVAL, |
| 451 | }, |
| 452 | { |
| 453 | .body = "some mgcp header data\r\nand header params" |
| 454 | "\r\n\r" |
| 455 | "m=audio 23\r\n", |
| 456 | .expect_rc = -EINVAL, |
| 457 | }, |
| 458 | { |
| 459 | .body = "some mgcp header data\r\nand header params" |
| 460 | "\n\r\r" |
| 461 | "m=audio 23\r\n", |
| 462 | .expect_rc = -EINVAL, |
| 463 | }, |
| 464 | }; |
| 465 | |
| 466 | void test_sdp_section_start() |
| 467 | { |
| 468 | int i; |
| 469 | int failures = 0; |
| 470 | |
| 471 | for (i = 0; i < ARRAY_SIZE(sdp_section_start_tests); i++) { |
| 472 | int rc; |
| 473 | struct sdp_section_start_test *t = &sdp_section_start_tests[i]; |
| 474 | struct mgcp_response *r = talloc_zero(ctx, struct mgcp_response); |
| 475 | |
| 476 | r->body = talloc_strdup(r, t->body); |
| 477 | |
| 478 | printf("\n%s() test [%d]:\n", __func__, i); |
| 479 | fprintf(stderr, "\n%s() test [%d]:\n", __func__, i); |
| 480 | fprintf(stderr, "body: \"%s\"\n", osmo_escape_str(r->body, -1)); |
| 481 | |
| 482 | rc = mgcp_response_parse_params(r); |
| 483 | |
| 484 | fprintf(stderr, "got rc=%d\n", rc); |
| 485 | if (rc != t->expect_rc) { |
| 486 | fprintf(stderr, "FAIL: Expected rc=%d\n", t->expect_rc); |
| 487 | failures++; |
| 488 | } |
| 489 | if (rc) { |
| 490 | talloc_free(r); |
| 491 | continue; |
| 492 | } |
| 493 | |
| 494 | fprintf(stderr, "got audio_port=%u\n", t->expect_params.audio_port); |
| 495 | if (r->audio_port != t->expect_params.audio_port) { |
| 496 | fprintf(stderr, "FAIL: Expected audio_port=%u\n", t->expect_params.audio_port); |
| 497 | failures++; |
| 498 | } |
| 499 | talloc_free(r); |
| 500 | } |
| 501 | |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 502 | OSMO_ASSERT(!failures); |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 505 | static void test_map_pt_to_codec(void) |
| 506 | { |
| 507 | /* Full form */ |
| 508 | OSMO_ASSERT(map_str_to_codec("PCMU/8000/1") == CODEC_PCMU_8000_1); |
| 509 | OSMO_ASSERT(map_str_to_codec("GSM/8000/1") == CODEC_GSM_8000_1); |
| 510 | OSMO_ASSERT(map_str_to_codec("PCMA/8000/1") == CODEC_PCMA_8000_1); |
| 511 | OSMO_ASSERT(map_str_to_codec("G729/8000/1") == CODEC_G729_8000_1); |
| 512 | OSMO_ASSERT(map_str_to_codec("GSM-EFR/8000/1") == CODEC_GSMEFR_8000_1); |
| 513 | OSMO_ASSERT(map_str_to_codec("GSM-HR-08/8000/1") == CODEC_GSMHR_8000_1); |
| 514 | OSMO_ASSERT(map_str_to_codec("AMR/8000/1") == CODEC_AMR_8000_1); |
| 515 | OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1") == CODEC_AMRWB_16000_1); |
| 516 | |
| 517 | /* Short form */ |
| 518 | OSMO_ASSERT(map_str_to_codec("GSM-EFR") == CODEC_GSMEFR_8000_1); |
| 519 | OSMO_ASSERT(map_str_to_codec("G729") == CODEC_G729_8000_1); |
| 520 | OSMO_ASSERT(map_str_to_codec("GSM-HR-08") == CODEC_GSMHR_8000_1); |
| 521 | |
| 522 | /* We do not care about what is after the first delimiter */ |
| 523 | OSMO_ASSERT(map_str_to_codec("AMR-WB/123///456") == CODEC_AMRWB_16000_1); |
| 524 | OSMO_ASSERT(map_str_to_codec("PCMA/asdf") == CODEC_PCMA_8000_1); |
| 525 | OSMO_ASSERT(map_str_to_codec("GSM/qwertz") == CODEC_GSM_8000_1); |
| 526 | |
| 527 | /* A trailing delimiter should not hurt */ |
| 528 | OSMO_ASSERT(map_str_to_codec("AMR/") == CODEC_AMR_8000_1); |
| 529 | OSMO_ASSERT(map_str_to_codec("G729/") == CODEC_G729_8000_1); |
| 530 | OSMO_ASSERT(map_str_to_codec("GSM/") == CODEC_GSM_8000_1); |
| 531 | |
| 532 | /* This is expected to fail */ |
| 533 | OSMO_ASSERT(map_str_to_codec("INVALID/1234/7") == -1); |
| 534 | OSMO_ASSERT(map_str_to_codec(NULL) == -1); |
| 535 | OSMO_ASSERT(map_str_to_codec("") == -1); |
| 536 | OSMO_ASSERT(map_str_to_codec("/////") == -1); |
| 537 | |
| 538 | /* The buffers are 64 bytes long, check what happens with overlong |
| 539 | * strings as input (This schould still work.) */ |
| 540 | OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1############################################################################################################") == CODEC_AMRWB_16000_1); |
| 541 | |
| 542 | /* This should not work, as there is no delimiter after the codec |
| 543 | * name */ |
| 544 | OSMO_ASSERT(map_str_to_codec("AMR-WB####################################################################################################################") == -1); |
| 545 | } |
| 546 | |
| 547 | static void test_map_codec_to_pt_and_map_pt_to_codec(void) |
| 548 | { |
| 549 | struct ptmap ptmap[10]; |
| 550 | unsigned int ptmap_len; |
| 551 | unsigned int i; |
| 552 | |
| 553 | ptmap[0].codec = CODEC_GSMEFR_8000_1; |
| 554 | ptmap[0].pt = 96; |
| 555 | ptmap[1].codec = CODEC_GSMHR_8000_1; |
| 556 | ptmap[1].pt = 97; |
| 557 | ptmap[2].codec = CODEC_AMR_8000_1; |
| 558 | ptmap[2].pt = 98; |
| 559 | ptmap[3].codec = CODEC_AMRWB_16000_1; |
| 560 | ptmap[3].pt = 99; |
| 561 | ptmap_len = 4; |
| 562 | |
| 563 | /* Mappings that are covered by the table */ |
| 564 | for (i = 0; i < ptmap_len; i++) |
| 565 | printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec)); |
| 566 | for (i = 0; i < ptmap_len; i++) |
| 567 | printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt)); |
| 568 | printf("\n"); |
| 569 | |
| 570 | /* Map some codecs/payload types from the static range, result must |
| 571 | * always be a 1:1 mapping */ |
| 572 | printf(" %u => %u\n", CODEC_PCMU_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMU_8000_1)); |
| 573 | printf(" %u => %u\n", CODEC_GSM_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_GSM_8000_1)); |
| 574 | printf(" %u => %u\n", CODEC_PCMA_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMA_8000_1)); |
| 575 | printf(" %u => %u\n", CODEC_G729_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_G729_8000_1)); |
| 576 | printf(" %u <= %u\n", CODEC_PCMU_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMU_8000_1)); |
| 577 | printf(" %u <= %u\n", CODEC_GSM_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_GSM_8000_1)); |
| 578 | printf(" %u <= %u\n", CODEC_PCMA_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMA_8000_1)); |
| 579 | printf(" %u <= %u\n", CODEC_G729_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_G729_8000_1)); |
| 580 | printf("\n"); |
| 581 | |
| 582 | /* Try to do mappings from statically defined range to danymic range and vice versa. This |
| 583 | * is illegal and should result into a 1:1 mapping */ |
| 584 | ptmap[3].codec = CODEC_AMRWB_16000_1; |
| 585 | ptmap[3].pt = 2; |
| 586 | ptmap[4].codec = CODEC_PCMU_8000_1; |
| 587 | ptmap[4].pt = 100; |
| 588 | ptmap_len = 5; |
| 589 | |
| 590 | /* Apply all mappings again, the illegal ones we defined should result into 1:1 mappings */ |
| 591 | for (i = 0; i < ptmap_len; i++) |
| 592 | printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec)); |
| 593 | for (i = 0; i < ptmap_len; i++) |
| 594 | printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt)); |
| 595 | printf("\n"); |
| 596 | } |
| 597 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 598 | static const struct log_info_cat log_categories[] = { |
| 599 | }; |
| 600 | |
| 601 | const struct log_info log_info = { |
| 602 | .cat = log_categories, |
| 603 | .num_cat = ARRAY_SIZE(log_categories), |
| 604 | }; |
| 605 | |
| 606 | |
| 607 | int main(int argc, char **argv) |
| 608 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 609 | ctx = talloc_named_const(NULL, 1, "mgcp_client_test"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 610 | msgb_talloc_ctx_init(ctx, 0); |
Neels Hofmeyr | 60f8e31 | 2018-03-30 23:01:07 +0200 | [diff] [blame] | 611 | osmo_init_logging2(ctx, &log_info); |
Philipp Maier | 8348abb | 2017-10-25 12:14:13 +0200 | [diff] [blame] | 612 | log_set_print_filename(osmo_stderr_target, 0); |
| 613 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 614 | log_set_use_color(osmo_stderr_target, 0); |
| 615 | log_set_print_category(osmo_stderr_target, 1); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 616 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 617 | log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG); |
| 618 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 619 | mgcp_client_conf_init(&conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 620 | |
| 621 | test_crcx(); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 622 | test_mgcp_msg(); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 623 | test_mgcp_client_cancel(); |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 624 | test_sdp_section_start(); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 625 | test_map_codec_to_pt_and_map_pt_to_codec(); |
| 626 | test_map_pt_to_codec(); |
Neels Hofmeyr | 10d487e | 2018-09-03 21:13:01 +0200 | [diff] [blame] | 627 | test_crcx_long_conn_id(); |
| 628 | test_crcx_too_long_conn_id(); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 629 | |
| 630 | printf("Done\n"); |
| 631 | fprintf(stderr, "Done\n"); |
| 632 | return EXIT_SUCCESS; |
| 633 | } |