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