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 | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 80 | int conn_id, const char *params) |
| 81 | { |
| 82 | static char compose[4096 - 128]; |
| 83 | int len; |
| 84 | |
| 85 | len = snprintf(compose, sizeof(compose), |
| 86 | "%d %u %s\r\nI: %d\n\n%s", |
| 87 | code, trans_id, comment, conn_id, params); |
| 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); |
| 105 | printf(" head.comment = %s\n", response->head.comment); |
| 106 | printf(" audio_port = %u\n", response->audio_port); |
| 107 | printf(" audio_ip = %s\n", response->audio_ip); |
| 108 | printf(" ptime = %u\n", response->ptime); |
| 109 | printf(" codecs_len = %u\n", response->codecs_len); |
| 110 | for(i=0;i<response->codecs_len;i++) |
| 111 | printf(" codecs[%u] = %u\n", i, response->codecs[i]); |
| 112 | printf(" ptmap_len = %u\n", response->ptmap_len); |
| 113 | for(i=0;i<response->ptmap_len;i++) { |
| 114 | printf(" ptmap[%u].codec = %u\n", i, response->ptmap[i].codec); |
| 115 | printf(" ptmap[%u].pt = %u\n", i, response->ptmap[i].pt); |
| 116 | } |
| 117 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) |
| 121 | { |
| 122 | mgcp_trans_id_t trans_id; |
| 123 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 124 | char *end; |
| 125 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 126 | 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] | 127 | |
| 128 | end = (char*)msgb_put(msg, 1); |
| 129 | *end = '\0'; |
| 130 | printf("composed:\n-----\n%s\n-----\n", |
| 131 | (char*)msgb_l2(msg)); |
| 132 | |
| 133 | talloc_free(msg); |
| 134 | return trans_id; |
| 135 | } |
| 136 | |
| 137 | void test_crcx(void) |
| 138 | { |
| 139 | struct msgb *msg; |
| 140 | mgcp_trans_id_t trans_id; |
| 141 | |
| 142 | printf("\n===== %s =====\n", __func__); |
| 143 | |
| 144 | if (mgcp) |
| 145 | talloc_free(mgcp); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 146 | mgcp = mgcp_client_init(ctx, &conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 147 | |
| 148 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 149 | trans_id = dummy_mgcp_send(msg); |
| 150 | |
| 151 | reply_to(trans_id, 200, "OK", 1, |
| 152 | "v=0\r\n" |
| 153 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 154 | "s=-\r\n" |
| 155 | "c=IN IP4 10.9.1.120\r\n" |
| 156 | "t=0 0\r\n" |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 157 | "m=audio 16002 RTP/AVP 110 96\r\n" |
| 158 | "a=rtpmap:110 AMR/8000\r\n" |
| 159 | "a=rtpmap:96 GSM-EFR/8000\r\n" |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 160 | "a=ptime:20\r\n"); |
| 161 | } |
| 162 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 163 | void test_mgcp_msg(void) |
| 164 | { |
| 165 | struct msgb *msg; |
| 166 | char audio_ip_overflow[5000]; |
| 167 | |
| 168 | /* A message struct prefilled with some arbitary values */ |
| 169 | struct mgcp_msg mgcp_msg = { |
| 170 | .audio_ip = "192.168.100.23", |
| 171 | .endpoint = "23@mgw", |
| 172 | .audio_port = 1234, |
| 173 | .call_id = 47, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 174 | .conn_id = "11", |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 175 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 176 | .ptime = 20, |
| 177 | .codecs[0] = CODEC_GSM_8000_1, |
| 178 | .codecs[1] = CODEC_AMR_8000_1, |
| 179 | .codecs[2] = CODEC_GSMEFR_8000_1, |
| 180 | .codecs_len = 1, |
| 181 | .ptmap[0].codec = CODEC_GSMEFR_8000_1, |
| 182 | .ptmap[0].pt = 96, |
| 183 | .ptmap_len = 1 |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | if (mgcp) |
| 187 | talloc_free(mgcp); |
| 188 | mgcp = mgcp_client_init(ctx, &conf); |
| 189 | |
| 190 | printf("\n"); |
| 191 | |
| 192 | printf("Generated CRCX message:\n"); |
| 193 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 194 | mgcp_msg.presence = |
| 195 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 196 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 197 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 198 | printf("%s\n", (char *)msg->data); |
| 199 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 200 | printf("Generated CRCX message (two codecs):\n"); |
| 201 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 202 | mgcp_msg.presence = |
| 203 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 204 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 205 | mgcp_msg.codecs_len = 2; |
| 206 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 207 | mgcp_msg.codecs_len = 1; |
| 208 | printf("%s\n", (char *)msg->data); |
| 209 | |
| 210 | printf("Generated CRCX message (three codecs, one with custom pt):\n"); |
| 211 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 212 | mgcp_msg.presence = |
| 213 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 214 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 215 | mgcp_msg.codecs_len = 3; |
| 216 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 217 | mgcp_msg.codecs_len = 1; |
| 218 | printf("%s\n", (char *)msg->data); |
| 219 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 220 | printf("Generated MDCX message:\n"); |
| 221 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 222 | mgcp_msg.presence = |
| 223 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 224 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 225 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 226 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 227 | printf("%s\n", (char *)msg->data); |
| 228 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 229 | printf("Generated MDCX message (two codecs):\n"); |
| 230 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 231 | mgcp_msg.presence = |
| 232 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 233 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 234 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 235 | mgcp_msg.codecs_len = 2; |
| 236 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 237 | mgcp_msg.codecs_len = 1; |
| 238 | printf("%s\n", (char *)msg->data); |
| 239 | |
| 240 | printf("Generated MDCX message (three codecs, one with custom pt):\n"); |
| 241 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 242 | mgcp_msg.presence = |
| 243 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 244 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 245 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 246 | mgcp_msg.codecs_len = 3; |
| 247 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 248 | mgcp_msg.codecs_len = 1; |
| 249 | printf("%s\n", (char *)msg->data); |
| 250 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 251 | printf("Generated DLCX message:\n"); |
| 252 | mgcp_msg.verb = MGCP_VERB_DLCX; |
| 253 | mgcp_msg.presence = |
| 254 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 255 | MGCP_MSG_PRESENCE_CONN_ID); |
| 256 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 257 | printf("%s\n", (char *)msg->data); |
| 258 | |
| 259 | printf("Generated AUEP message:\n"); |
| 260 | mgcp_msg.verb = MGCP_VERB_AUEP; |
| 261 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 262 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 263 | printf("%s\n", msg->data); |
| 264 | |
| 265 | printf("Generated RSIP message:\n"); |
| 266 | mgcp_msg.verb = MGCP_VERB_RSIP; |
| 267 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 268 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 269 | printf("%s\n", (char *)msg->data); |
| 270 | |
| 271 | printf("Overfolow test:\n"); |
| 272 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 273 | mgcp_msg.presence = |
| 274 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 275 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 276 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 277 | memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow)); |
| 278 | audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0'; |
| 279 | mgcp_msg.audio_ip = audio_ip_overflow; |
| 280 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 281 | OSMO_ASSERT(msg == NULL); |
| 282 | |
| 283 | printf("\n"); |
| 284 | msgb_free(msg); |
| 285 | } |
| 286 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 287 | void test_mgcp_client_cancel() |
| 288 | { |
| 289 | mgcp_trans_id_t trans_id; |
| 290 | struct msgb *msg; |
| 291 | struct mgcp_msg mgcp_msg = { |
| 292 | .verb = MGCP_VERB_CRCX, |
| 293 | .audio_ip = "192.168.100.23", |
| 294 | .endpoint = "23@mgw", |
| 295 | .audio_port = 1234, |
| 296 | .call_id = 47, |
Philipp Maier | 922ae9a | 2017-12-04 15:53:37 +0100 | [diff] [blame] | 297 | .conn_id = "11", |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 298 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 299 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
| 300 | | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE), |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 301 | .ptime = 20, |
| 302 | .codecs[0] = CODEC_AMR_8000_1, |
| 303 | .codecs_len = 1 |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | printf("\n%s():\n", __func__); |
| 307 | fprintf(stderr, "\n%s():\n", __func__); |
| 308 | |
| 309 | if (mgcp) |
| 310 | talloc_free(mgcp); |
| 311 | mgcp = mgcp_client_init(ctx, &conf); |
| 312 | |
| 313 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 314 | trans_id = mgcp_msg_trans_id(msg); |
| 315 | fprintf(stderr, "- composed msg with trans_id=%u\n", trans_id); |
| 316 | |
| 317 | fprintf(stderr, "- not in queue yet, cannot cancel yet\n"); |
| 318 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 319 | |
| 320 | fprintf(stderr, "- enqueue\n"); |
| 321 | dummy_mgcp_send(msg); |
| 322 | |
| 323 | fprintf(stderr, "- cancel succeeds\n"); |
| 324 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == 0); |
| 325 | |
| 326 | fprintf(stderr, "- late response gets discarded\n"); |
| 327 | OSMO_ASSERT(reply_to(trans_id, 200, "OK", 1, "v=0\r\n") == -ENOENT); |
| 328 | |
| 329 | fprintf(stderr, "- canceling again does nothing\n"); |
| 330 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 331 | |
| 332 | fprintf(stderr, "%s() done\n", __func__); |
| 333 | } |
| 334 | |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 335 | struct sdp_section_start_test { |
| 336 | const char *body; |
| 337 | int expect_rc; |
| 338 | struct mgcp_response expect_params; |
| 339 | }; |
| 340 | |
| 341 | static struct sdp_section_start_test sdp_section_start_tests[] = { |
| 342 | { |
| 343 | .body = "", |
| 344 | .expect_rc = -EINVAL, |
| 345 | }, |
| 346 | { |
| 347 | .body = "\n\n", |
| 348 | }, |
| 349 | { |
| 350 | .body = "\r\n\r\n", |
| 351 | }, |
| 352 | { |
| 353 | .body = "\n\r\n\r", |
| 354 | }, |
| 355 | { |
| 356 | .body = "some mgcp header data\r\nand header params" |
| 357 | "\n\n" |
| 358 | "m=audio 23\r\n", |
| 359 | .expect_params = { |
| 360 | .audio_port = 23, |
| 361 | }, |
| 362 | }, |
| 363 | { |
| 364 | .body = "some mgcp header data\r\nand header params" |
| 365 | "\r\n\r\n" |
| 366 | "m=audio 23\r\n", |
| 367 | .expect_params = { |
| 368 | .audio_port = 23, |
| 369 | }, |
| 370 | }, |
| 371 | { |
| 372 | .body = "some mgcp header data\r\nand header params" |
| 373 | "\n\r\n\r" |
| 374 | "m=audio 23\r\n", |
| 375 | .expect_params = { |
| 376 | .audio_port = 23, |
| 377 | }, |
| 378 | }, |
| 379 | { |
| 380 | .body = "some mgcp header data\r\nand header params" |
| 381 | "\n\r\n" |
| 382 | "m=audio 23\r\n", |
| 383 | .expect_rc = -EINVAL, |
| 384 | }, |
| 385 | { |
| 386 | .body = "some mgcp header data\r\nand header params" |
| 387 | "\r\n\r" |
| 388 | "m=audio 23\r\n", |
| 389 | .expect_rc = -EINVAL, |
| 390 | }, |
| 391 | { |
| 392 | .body = "some mgcp header data\r\nand header params" |
| 393 | "\n\r\r" |
| 394 | "m=audio 23\r\n", |
| 395 | .expect_rc = -EINVAL, |
| 396 | }, |
| 397 | }; |
| 398 | |
| 399 | void test_sdp_section_start() |
| 400 | { |
| 401 | int i; |
| 402 | int failures = 0; |
| 403 | |
| 404 | for (i = 0; i < ARRAY_SIZE(sdp_section_start_tests); i++) { |
| 405 | int rc; |
| 406 | struct sdp_section_start_test *t = &sdp_section_start_tests[i]; |
| 407 | struct mgcp_response *r = talloc_zero(ctx, struct mgcp_response); |
| 408 | |
| 409 | r->body = talloc_strdup(r, t->body); |
| 410 | |
| 411 | printf("\n%s() test [%d]:\n", __func__, i); |
| 412 | fprintf(stderr, "\n%s() test [%d]:\n", __func__, i); |
| 413 | fprintf(stderr, "body: \"%s\"\n", osmo_escape_str(r->body, -1)); |
| 414 | |
| 415 | rc = mgcp_response_parse_params(r); |
| 416 | |
| 417 | fprintf(stderr, "got rc=%d\n", rc); |
| 418 | if (rc != t->expect_rc) { |
| 419 | fprintf(stderr, "FAIL: Expected rc=%d\n", t->expect_rc); |
| 420 | failures++; |
| 421 | } |
| 422 | if (rc) { |
| 423 | talloc_free(r); |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | fprintf(stderr, "got audio_port=%u\n", t->expect_params.audio_port); |
| 428 | if (r->audio_port != t->expect_params.audio_port) { |
| 429 | fprintf(stderr, "FAIL: Expected audio_port=%u\n", t->expect_params.audio_port); |
| 430 | failures++; |
| 431 | } |
| 432 | talloc_free(r); |
| 433 | } |
| 434 | |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 435 | OSMO_ASSERT(!failures); |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 438 | static void test_map_pt_to_codec(void) |
| 439 | { |
| 440 | /* Full form */ |
| 441 | OSMO_ASSERT(map_str_to_codec("PCMU/8000/1") == CODEC_PCMU_8000_1); |
| 442 | OSMO_ASSERT(map_str_to_codec("GSM/8000/1") == CODEC_GSM_8000_1); |
| 443 | OSMO_ASSERT(map_str_to_codec("PCMA/8000/1") == CODEC_PCMA_8000_1); |
| 444 | OSMO_ASSERT(map_str_to_codec("G729/8000/1") == CODEC_G729_8000_1); |
| 445 | OSMO_ASSERT(map_str_to_codec("GSM-EFR/8000/1") == CODEC_GSMEFR_8000_1); |
| 446 | OSMO_ASSERT(map_str_to_codec("GSM-HR-08/8000/1") == CODEC_GSMHR_8000_1); |
| 447 | OSMO_ASSERT(map_str_to_codec("AMR/8000/1") == CODEC_AMR_8000_1); |
| 448 | OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1") == CODEC_AMRWB_16000_1); |
| 449 | |
| 450 | /* Short form */ |
| 451 | OSMO_ASSERT(map_str_to_codec("GSM-EFR") == CODEC_GSMEFR_8000_1); |
| 452 | OSMO_ASSERT(map_str_to_codec("G729") == CODEC_G729_8000_1); |
| 453 | OSMO_ASSERT(map_str_to_codec("GSM-HR-08") == CODEC_GSMHR_8000_1); |
| 454 | |
| 455 | /* We do not care about what is after the first delimiter */ |
| 456 | OSMO_ASSERT(map_str_to_codec("AMR-WB/123///456") == CODEC_AMRWB_16000_1); |
| 457 | OSMO_ASSERT(map_str_to_codec("PCMA/asdf") == CODEC_PCMA_8000_1); |
| 458 | OSMO_ASSERT(map_str_to_codec("GSM/qwertz") == CODEC_GSM_8000_1); |
| 459 | |
| 460 | /* A trailing delimiter should not hurt */ |
| 461 | OSMO_ASSERT(map_str_to_codec("AMR/") == CODEC_AMR_8000_1); |
| 462 | OSMO_ASSERT(map_str_to_codec("G729/") == CODEC_G729_8000_1); |
| 463 | OSMO_ASSERT(map_str_to_codec("GSM/") == CODEC_GSM_8000_1); |
| 464 | |
| 465 | /* This is expected to fail */ |
| 466 | OSMO_ASSERT(map_str_to_codec("INVALID/1234/7") == -1); |
| 467 | OSMO_ASSERT(map_str_to_codec(NULL) == -1); |
| 468 | OSMO_ASSERT(map_str_to_codec("") == -1); |
| 469 | OSMO_ASSERT(map_str_to_codec("/////") == -1); |
| 470 | |
| 471 | /* The buffers are 64 bytes long, check what happens with overlong |
| 472 | * strings as input (This schould still work.) */ |
| 473 | OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1############################################################################################################") == CODEC_AMRWB_16000_1); |
| 474 | |
| 475 | /* This should not work, as there is no delimiter after the codec |
| 476 | * name */ |
| 477 | OSMO_ASSERT(map_str_to_codec("AMR-WB####################################################################################################################") == -1); |
| 478 | } |
| 479 | |
| 480 | static void test_map_codec_to_pt_and_map_pt_to_codec(void) |
| 481 | { |
| 482 | struct ptmap ptmap[10]; |
| 483 | unsigned int ptmap_len; |
| 484 | unsigned int i; |
| 485 | |
| 486 | ptmap[0].codec = CODEC_GSMEFR_8000_1; |
| 487 | ptmap[0].pt = 96; |
| 488 | ptmap[1].codec = CODEC_GSMHR_8000_1; |
| 489 | ptmap[1].pt = 97; |
| 490 | ptmap[2].codec = CODEC_AMR_8000_1; |
| 491 | ptmap[2].pt = 98; |
| 492 | ptmap[3].codec = CODEC_AMRWB_16000_1; |
| 493 | ptmap[3].pt = 99; |
| 494 | ptmap_len = 4; |
| 495 | |
| 496 | /* Mappings that are covered by the table */ |
| 497 | for (i = 0; i < ptmap_len; i++) |
| 498 | printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec)); |
| 499 | for (i = 0; i < ptmap_len; i++) |
| 500 | printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt)); |
| 501 | printf("\n"); |
| 502 | |
| 503 | /* Map some codecs/payload types from the static range, result must |
| 504 | * always be a 1:1 mapping */ |
| 505 | printf(" %u => %u\n", CODEC_PCMU_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMU_8000_1)); |
| 506 | printf(" %u => %u\n", CODEC_GSM_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_GSM_8000_1)); |
| 507 | printf(" %u => %u\n", CODEC_PCMA_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMA_8000_1)); |
| 508 | printf(" %u => %u\n", CODEC_G729_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_G729_8000_1)); |
| 509 | printf(" %u <= %u\n", CODEC_PCMU_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMU_8000_1)); |
| 510 | printf(" %u <= %u\n", CODEC_GSM_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_GSM_8000_1)); |
| 511 | printf(" %u <= %u\n", CODEC_PCMA_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMA_8000_1)); |
| 512 | printf(" %u <= %u\n", CODEC_G729_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_G729_8000_1)); |
| 513 | printf("\n"); |
| 514 | |
| 515 | /* Try to do mappings from statically defined range to danymic range and vice versa. This |
| 516 | * is illegal and should result into a 1:1 mapping */ |
| 517 | ptmap[3].codec = CODEC_AMRWB_16000_1; |
| 518 | ptmap[3].pt = 2; |
| 519 | ptmap[4].codec = CODEC_PCMU_8000_1; |
| 520 | ptmap[4].pt = 100; |
| 521 | ptmap_len = 5; |
| 522 | |
| 523 | /* Apply all mappings again, the illegal ones we defined should result into 1:1 mappings */ |
| 524 | for (i = 0; i < ptmap_len; i++) |
| 525 | printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec)); |
| 526 | for (i = 0; i < ptmap_len; i++) |
| 527 | printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt)); |
| 528 | printf("\n"); |
| 529 | } |
| 530 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 531 | static const struct log_info_cat log_categories[] = { |
| 532 | }; |
| 533 | |
| 534 | const struct log_info log_info = { |
| 535 | .cat = log_categories, |
| 536 | .num_cat = ARRAY_SIZE(log_categories), |
| 537 | }; |
| 538 | |
| 539 | |
| 540 | int main(int argc, char **argv) |
| 541 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 542 | ctx = talloc_named_const(NULL, 1, "mgcp_client_test"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 543 | msgb_talloc_ctx_init(ctx, 0); |
Neels Hofmeyr | 60f8e31 | 2018-03-30 23:01:07 +0200 | [diff] [blame] | 544 | osmo_init_logging2(ctx, &log_info); |
Philipp Maier | 8348abb | 2017-10-25 12:14:13 +0200 | [diff] [blame] | 545 | log_set_print_filename(osmo_stderr_target, 0); |
| 546 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 547 | log_set_use_color(osmo_stderr_target, 0); |
| 548 | log_set_print_category(osmo_stderr_target, 1); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 549 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 550 | log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG); |
| 551 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 552 | mgcp_client_conf_init(&conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 553 | |
| 554 | test_crcx(); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 555 | test_mgcp_msg(); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 556 | test_mgcp_client_cancel(); |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 557 | test_sdp_section_start(); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 558 | test_map_codec_to_pt_and_map_pt_to_codec(); |
| 559 | test_map_pt_to_codec(); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 560 | |
| 561 | printf("Done\n"); |
| 562 | fprintf(stderr, "Done\n"); |
| 563 | return EXIT_SUCCESS; |
| 564 | } |