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 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/application.h> |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 26 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 27 | #include <errno.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 28 | |
| 29 | void *ctx; |
| 30 | |
| 31 | #define buf_len 4096 |
| 32 | |
| 33 | #if 0 |
| 34 | static struct msgb *from_hex(const char *hex) |
| 35 | { |
| 36 | struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex"); |
| 37 | unsigned int l = osmo_hexparse(hex, msg->data, buf_len); |
| 38 | msg->l2h = msgb_put(msg, l); |
| 39 | return msg; |
| 40 | } |
| 41 | |
| 42 | static struct msgb *mgcp_from_str(const char *head, const char *params) |
| 43 | { |
| 44 | struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str"); |
| 45 | unsigned int l; |
| 46 | char *data; |
| 47 | l = strlen(head); |
| 48 | msg->l2h = msgb_put(msg, l); |
| 49 | data = (char*)msgb_l2(msg); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 50 | osmo_strlcpy(data, head, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 51 | |
| 52 | data = (char*)msgb_put(msg, 1); |
| 53 | *data = '\n'; |
| 54 | |
| 55 | l = strlen(params); |
| 56 | data = (char*)msgb_put(msg, l); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 57 | osmo_strlcpy(data, params, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 58 | |
| 59 | return msg; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | static struct msgb *from_str(const char *str) |
| 64 | { |
| 65 | struct msgb *msg = msgb_alloc(buf_len, "from_str"); |
| 66 | unsigned int l = strlen(str); |
| 67 | char *data; |
| 68 | msg->l2h = msgb_put(msg, l); |
| 69 | data = (char*)msgb_l2(msg); |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 70 | osmo_strlcpy(data, str, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 71 | return msg; |
| 72 | } |
| 73 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 74 | static struct mgcp_client_conf conf; |
| 75 | struct mgcp_client *mgcp = NULL; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 76 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 77 | 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] | 78 | int conn_id, const char *params) |
| 79 | { |
| 80 | static char compose[4096 - 128]; |
| 81 | int len; |
| 82 | |
| 83 | len = snprintf(compose, sizeof(compose), |
| 84 | "%d %u %s\r\nI: %d\n\n%s", |
| 85 | code, trans_id, comment, conn_id, params); |
| 86 | OSMO_ASSERT(len < sizeof(compose)); |
| 87 | OSMO_ASSERT(len > 0); |
| 88 | |
| 89 | printf("composed response:\n-----\n%s\n-----\n", |
| 90 | compose); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 91 | return mgcp_client_rx(mgcp, from_str(compose)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void test_response_cb(struct mgcp_response *response, void *priv) |
| 95 | { |
| 96 | OSMO_ASSERT(priv == mgcp); |
| 97 | mgcp_response_parse_params(response); |
| 98 | |
| 99 | printf("response cb received:\n" |
| 100 | " head.response_code = %d\n" |
| 101 | " head.trans_id = %u\n" |
| 102 | " head.comment = %s\n" |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 103 | " audio_port = %u\n" |
| 104 | " audio_ip = %s\n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 105 | response->head.response_code, |
| 106 | response->head.trans_id, |
| 107 | response->head.comment, |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 108 | response->audio_port, |
| 109 | response->audio_ip |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 110 | ); |
| 111 | } |
| 112 | |
| 113 | mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) |
| 114 | { |
| 115 | mgcp_trans_id_t trans_id; |
| 116 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 117 | char *end; |
| 118 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | end = (char*)msgb_put(msg, 1); |
| 122 | *end = '\0'; |
| 123 | printf("composed:\n-----\n%s\n-----\n", |
| 124 | (char*)msgb_l2(msg)); |
| 125 | |
| 126 | talloc_free(msg); |
| 127 | return trans_id; |
| 128 | } |
| 129 | |
| 130 | void test_crcx(void) |
| 131 | { |
| 132 | struct msgb *msg; |
| 133 | mgcp_trans_id_t trans_id; |
| 134 | |
| 135 | printf("\n===== %s =====\n", __func__); |
| 136 | |
| 137 | if (mgcp) |
| 138 | talloc_free(mgcp); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 139 | mgcp = mgcp_client_init(ctx, &conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 140 | |
| 141 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 142 | trans_id = dummy_mgcp_send(msg); |
| 143 | |
| 144 | reply_to(trans_id, 200, "OK", 1, |
| 145 | "v=0\r\n" |
| 146 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 147 | "s=-\r\n" |
| 148 | "c=IN IP4 10.9.1.120\r\n" |
| 149 | "t=0 0\r\n" |
| 150 | "m=audio 16002 RTP/AVP 98\r\n" |
| 151 | "a=rtpmap:98 AMR/8000\r\n" |
| 152 | "a=ptime:20\r\n"); |
| 153 | } |
| 154 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 155 | void test_mgcp_msg(void) |
| 156 | { |
| 157 | struct msgb *msg; |
| 158 | char audio_ip_overflow[5000]; |
| 159 | |
| 160 | /* A message struct prefilled with some arbitary values */ |
| 161 | struct mgcp_msg mgcp_msg = { |
| 162 | .audio_ip = "192.168.100.23", |
| 163 | .endpoint = "23@mgw", |
| 164 | .audio_port = 1234, |
| 165 | .call_id = 47, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 166 | .conn_id = "11", |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 167 | .conn_mode = MGCP_CONN_RECV_SEND |
| 168 | }; |
| 169 | |
| 170 | if (mgcp) |
| 171 | talloc_free(mgcp); |
| 172 | mgcp = mgcp_client_init(ctx, &conf); |
| 173 | |
| 174 | printf("\n"); |
| 175 | |
| 176 | printf("Generated CRCX message:\n"); |
| 177 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 178 | mgcp_msg.presence = |
| 179 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 180 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 181 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 182 | printf("%s\n", (char *)msg->data); |
| 183 | |
| 184 | printf("Generated MDCX message:\n"); |
| 185 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 186 | mgcp_msg.presence = |
| 187 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 188 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 189 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 190 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 191 | printf("%s\n", (char *)msg->data); |
| 192 | |
| 193 | printf("Generated DLCX message:\n"); |
| 194 | mgcp_msg.verb = MGCP_VERB_DLCX; |
| 195 | mgcp_msg.presence = |
| 196 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 197 | MGCP_MSG_PRESENCE_CONN_ID); |
| 198 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 199 | printf("%s\n", (char *)msg->data); |
| 200 | |
| 201 | printf("Generated AUEP message:\n"); |
| 202 | mgcp_msg.verb = MGCP_VERB_AUEP; |
| 203 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 204 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 205 | printf("%s\n", msg->data); |
| 206 | |
| 207 | printf("Generated RSIP message:\n"); |
| 208 | mgcp_msg.verb = MGCP_VERB_RSIP; |
| 209 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 210 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 211 | printf("%s\n", (char *)msg->data); |
| 212 | |
| 213 | printf("Overfolow test:\n"); |
| 214 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 215 | mgcp_msg.presence = |
| 216 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 217 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 218 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 219 | memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow)); |
| 220 | audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0'; |
| 221 | mgcp_msg.audio_ip = audio_ip_overflow; |
| 222 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 223 | OSMO_ASSERT(msg == NULL); |
| 224 | |
| 225 | printf("\n"); |
| 226 | msgb_free(msg); |
| 227 | } |
| 228 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 229 | void test_mgcp_client_cancel() |
| 230 | { |
| 231 | mgcp_trans_id_t trans_id; |
| 232 | struct msgb *msg; |
| 233 | struct mgcp_msg mgcp_msg = { |
| 234 | .verb = MGCP_VERB_CRCX, |
| 235 | .audio_ip = "192.168.100.23", |
| 236 | .endpoint = "23@mgw", |
| 237 | .audio_port = 1234, |
| 238 | .call_id = 47, |
Philipp Maier | 922ae9a | 2017-12-04 15:53:37 +0100 | [diff] [blame] | 239 | .conn_id = "11", |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 240 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 241 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
| 242 | | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE), |
| 243 | }; |
| 244 | |
| 245 | printf("\n%s():\n", __func__); |
| 246 | fprintf(stderr, "\n%s():\n", __func__); |
| 247 | |
| 248 | if (mgcp) |
| 249 | talloc_free(mgcp); |
| 250 | mgcp = mgcp_client_init(ctx, &conf); |
| 251 | |
| 252 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 253 | trans_id = mgcp_msg_trans_id(msg); |
| 254 | fprintf(stderr, "- composed msg with trans_id=%u\n", trans_id); |
| 255 | |
| 256 | fprintf(stderr, "- not in queue yet, cannot cancel yet\n"); |
| 257 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 258 | |
| 259 | fprintf(stderr, "- enqueue\n"); |
| 260 | dummy_mgcp_send(msg); |
| 261 | |
| 262 | fprintf(stderr, "- cancel succeeds\n"); |
| 263 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == 0); |
| 264 | |
| 265 | fprintf(stderr, "- late response gets discarded\n"); |
| 266 | OSMO_ASSERT(reply_to(trans_id, 200, "OK", 1, "v=0\r\n") == -ENOENT); |
| 267 | |
| 268 | fprintf(stderr, "- canceling again does nothing\n"); |
| 269 | OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT); |
| 270 | |
| 271 | fprintf(stderr, "%s() done\n", __func__); |
| 272 | } |
| 273 | |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 274 | struct sdp_section_start_test { |
| 275 | const char *body; |
| 276 | int expect_rc; |
| 277 | struct mgcp_response expect_params; |
| 278 | }; |
| 279 | |
| 280 | static struct sdp_section_start_test sdp_section_start_tests[] = { |
| 281 | { |
| 282 | .body = "", |
| 283 | .expect_rc = -EINVAL, |
| 284 | }, |
| 285 | { |
| 286 | .body = "\n\n", |
| 287 | }, |
| 288 | { |
| 289 | .body = "\r\n\r\n", |
| 290 | }, |
| 291 | { |
| 292 | .body = "\n\r\n\r", |
| 293 | }, |
| 294 | { |
| 295 | .body = "some mgcp header data\r\nand header params" |
| 296 | "\n\n" |
| 297 | "m=audio 23\r\n", |
| 298 | .expect_params = { |
| 299 | .audio_port = 23, |
| 300 | }, |
| 301 | }, |
| 302 | { |
| 303 | .body = "some mgcp header data\r\nand header params" |
| 304 | "\r\n\r\n" |
| 305 | "m=audio 23\r\n", |
| 306 | .expect_params = { |
| 307 | .audio_port = 23, |
| 308 | }, |
| 309 | }, |
| 310 | { |
| 311 | .body = "some mgcp header data\r\nand header params" |
| 312 | "\n\r\n\r" |
| 313 | "m=audio 23\r\n", |
| 314 | .expect_params = { |
| 315 | .audio_port = 23, |
| 316 | }, |
| 317 | }, |
| 318 | { |
| 319 | .body = "some mgcp header data\r\nand header params" |
| 320 | "\n\r\n" |
| 321 | "m=audio 23\r\n", |
| 322 | .expect_rc = -EINVAL, |
| 323 | }, |
| 324 | { |
| 325 | .body = "some mgcp header data\r\nand header params" |
| 326 | "\r\n\r" |
| 327 | "m=audio 23\r\n", |
| 328 | .expect_rc = -EINVAL, |
| 329 | }, |
| 330 | { |
| 331 | .body = "some mgcp header data\r\nand header params" |
| 332 | "\n\r\r" |
| 333 | "m=audio 23\r\n", |
| 334 | .expect_rc = -EINVAL, |
| 335 | }, |
| 336 | }; |
| 337 | |
| 338 | void test_sdp_section_start() |
| 339 | { |
| 340 | int i; |
| 341 | int failures = 0; |
| 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(sdp_section_start_tests); i++) { |
| 344 | int rc; |
| 345 | struct sdp_section_start_test *t = &sdp_section_start_tests[i]; |
| 346 | struct mgcp_response *r = talloc_zero(ctx, struct mgcp_response); |
| 347 | |
| 348 | r->body = talloc_strdup(r, t->body); |
| 349 | |
| 350 | printf("\n%s() test [%d]:\n", __func__, i); |
| 351 | fprintf(stderr, "\n%s() test [%d]:\n", __func__, i); |
| 352 | fprintf(stderr, "body: \"%s\"\n", osmo_escape_str(r->body, -1)); |
| 353 | |
| 354 | rc = mgcp_response_parse_params(r); |
| 355 | |
| 356 | fprintf(stderr, "got rc=%d\n", rc); |
| 357 | if (rc != t->expect_rc) { |
| 358 | fprintf(stderr, "FAIL: Expected rc=%d\n", t->expect_rc); |
| 359 | failures++; |
| 360 | } |
| 361 | if (rc) { |
| 362 | talloc_free(r); |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | fprintf(stderr, "got audio_port=%u\n", t->expect_params.audio_port); |
| 367 | if (r->audio_port != t->expect_params.audio_port) { |
| 368 | fprintf(stderr, "FAIL: Expected audio_port=%u\n", t->expect_params.audio_port); |
| 369 | failures++; |
| 370 | } |
| 371 | talloc_free(r); |
| 372 | } |
| 373 | |
| 374 | /* Expecting failures due to known bugs, will be resolved in a subsequent commit. |
| 375 | OSMO_ASSERT(!failures); |
| 376 | */ |
| 377 | } |
| 378 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 379 | static const struct log_info_cat log_categories[] = { |
| 380 | }; |
| 381 | |
| 382 | const struct log_info log_info = { |
| 383 | .cat = log_categories, |
| 384 | .num_cat = ARRAY_SIZE(log_categories), |
| 385 | }; |
| 386 | |
| 387 | |
| 388 | int main(int argc, char **argv) |
| 389 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 390 | ctx = talloc_named_const(NULL, 1, "mgcp_client_test"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 391 | msgb_talloc_ctx_init(ctx, 0); |
| 392 | osmo_init_logging(&log_info); |
Philipp Maier | 8348abb | 2017-10-25 12:14:13 +0200 | [diff] [blame] | 393 | log_set_print_filename(osmo_stderr_target, 0); |
| 394 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 395 | log_set_use_color(osmo_stderr_target, 0); |
| 396 | log_set_print_category(osmo_stderr_target, 1); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 397 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 398 | log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG); |
| 399 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 400 | mgcp_client_conf_init(&conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 401 | |
| 402 | test_crcx(); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 403 | test_mgcp_msg(); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 404 | test_mgcp_client_cancel(); |
Neels Hofmeyr | a8c6a9c | 2018-02-21 15:36:45 +0100 | [diff] [blame] | 405 | test_sdp_section_start(); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 406 | |
| 407 | printf("Done\n"); |
| 408 | fprintf(stderr, "Done\n"); |
| 409 | return EXIT_SUCCESS; |
| 410 | } |