blob: db9f4f8c74a0dc1320cc553861bc50dafc71657d [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/*
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 Hofmeyrbd86f942018-02-21 16:45:38 +010021#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
22
Neels Hofmeyre9920f22017-07-10 15:07:22 +020023#include <string.h>
24
25#include <osmocom/core/msgb.h>
26#include <osmocom/core/application.h>
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020027#include <osmocom/mgcp_client/mgcp_client.h>
28#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010029#include <errno.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020030
31void *ctx;
32
33#define buf_len 4096
34
35#if 0
36static 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
44static 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 Maierf8bfbe82017-11-23 19:32:31 +010052 osmo_strlcpy(data, head, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020053
54 data = (char*)msgb_put(msg, 1);
55 *data = '\n';
56
57 l = strlen(params);
58 data = (char*)msgb_put(msg, l);
Philipp Maierf8bfbe82017-11-23 19:32:31 +010059 osmo_strlcpy(data, params, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020060
61 return msg;
62}
63#endif
64
65static 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 Maierf8bfbe82017-11-23 19:32:31 +010072 osmo_strlcpy(data, str, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020073 return msg;
74}
75
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020076static struct mgcp_client_conf conf;
77struct mgcp_client *mgcp = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +020078
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010079static int reply_to(mgcp_trans_id_t trans_id, int code, const char *comment,
Neels Hofmeyr1d121482018-09-03 21:05:13 +020080 const char *params)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020081{
82 static char compose[4096 - 128];
83 int len;
84
85 len = snprintf(compose, sizeof(compose),
Neels Hofmeyr1d121482018-09-03 21:05:13 +020086 "%d %u %s\r\n%s",
87 code, trans_id, comment, params);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020088 OSMO_ASSERT(len < sizeof(compose));
89 OSMO_ASSERT(len > 0);
90
91 printf("composed response:\n-----\n%s\n-----\n",
92 compose);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010093 return mgcp_client_rx(mgcp, from_str(compose));
Neels Hofmeyre9920f22017-07-10 15:07:22 +020094}
95
96void test_response_cb(struct mgcp_response *response, void *priv)
97{
Philipp Maier704c4f02018-06-07 18:51:31 +020098 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +020099 OSMO_ASSERT(priv == mgcp);
100 mgcp_response_parse_params(response);
101
Philipp Maier704c4f02018-06-07 18:51:31 +0200102 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 Hofmeyr40f50332018-09-03 21:12:48 +0200105 printf(" head.conn_id = %s\n", response->head.conn_id);
Philipp Maier704c4f02018-06-07 18:51:31 +0200106 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 Pedrol21cdbfc2019-04-23 12:36:32 +0200116 printf(" ptmap[%u].pt = %u\n", i, response->ptmap[i].pt);
Philipp Maier704c4f02018-06-07 18:51:31 +0200117 }
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200118
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200119}
120
121mgcp_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 Hofmeyr3a8e7232017-09-04 01:02:56 +0200127 OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200128
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 Maier1dc6be62017-10-05 18:25:37 +0200138void 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 Maier01d24a32017-11-21 17:26:09 +0100149 .conn_id = "11",
Philipp Maier704c4f02018-06-07 18:51:31 +0200150 .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 Hofmeyre6d8e912018-08-23 16:36:48 +0200158 .ptmap_len = 1,
159 .x_osmo_ign = MGCP_X_OSMO_IGN_CALLID,
Pau Espin Pedrol900cd652019-04-24 22:06:22 +0200160 .x_osmo_osmux_cid = -1, /* wildcard */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200161 };
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 Maier704c4f02018-06-07 18:51:31 +0200177 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 Pedrol21cdbfc2019-04-23 12:36:32 +0200184 mgcp_msg.codecs_len = 1;
Philipp Maier704c4f02018-06-07 18:51:31 +0200185 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 Pedrol21cdbfc2019-04-23 12:36:32 +0200194 mgcp_msg.codecs_len = 1;
195 printf("%s\n", (char *)msg->data);
Philipp Maier704c4f02018-06-07 18:51:31 +0200196
Philipp Maier1dc6be62017-10-05 18:25:37 +0200197 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 Maier704c4f02018-06-07 18:51:31 +0200206 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 Pedrol21cdbfc2019-04-23 12:36:32 +0200214 mgcp_msg.codecs_len = 1;
Philipp Maier704c4f02018-06-07 18:51:31 +0200215 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 Pedrol21cdbfc2019-04-23 12:36:32 +0200225 mgcp_msg.codecs_len = 1;
226 printf("%s\n", (char *)msg->data);
Philipp Maier704c4f02018-06-07 18:51:31 +0200227
Philipp Maier1dc6be62017-10-05 18:25:37 +0200228 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 Hofmeyre6d8e912018-08-23 16:36:48 +0200248 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 Pedrol900cd652019-04-24 22:06:22 +0200258 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 Pedrolca538fc2019-05-10 16:49:59 +0200279 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 Maier1dc6be62017-10-05 18:25:37 +0200290 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 Hofmeyrc8f37cb2017-11-30 13:43:11 +0100306void 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 Maier922ae9a2017-12-04 15:53:37 +0100316 .conn_id = "11",
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100317 .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 Maier704c4f02018-06-07 18:51:31 +0200320 .ptime = 20,
321 .codecs[0] = CODEC_AMR_8000_1,
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200322 .codecs_len = 1
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100323 };
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 Hofmeyrb1bb1fa2018-09-03 21:07:26 +0200346 OSMO_ASSERT(reply_to(trans_id, 200, "OK", "I: 1\r\n\r\nv=0\r\n") == -ENOENT);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100347
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 Hofmeyra8c6a9c2018-02-21 15:36:45 +0100354struct sdp_section_start_test {
355 const char *body;
356 int expect_rc;
357 struct mgcp_response expect_params;
358};
359
360static struct sdp_section_start_test sdp_section_start_tests[] = {
361 {
362 .body = "",
Neels Hofmeyre8278312019-09-19 03:06:46 +0200363 .expect_rc = 0,
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100364 },
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 Hofmeyre8278312019-09-19 03:06:46 +0200402 .expect_rc = 0,
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100403 },
404 {
405 .body = "some mgcp header data\r\nand header params"
406 "\r\n\r"
407 "m=audio 23\r\n",
Neels Hofmeyre8278312019-09-19 03:06:46 +0200408 .expect_rc = 0,
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100409 },
410 {
411 .body = "some mgcp header data\r\nand header params"
412 "\n\r\r"
413 "m=audio 23\r\n",
Neels Hofmeyre8278312019-09-19 03:06:46 +0200414 .expect_rc = 0,
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100415 },
416};
417
418void 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 Hofmeyra8c6a9c2018-02-21 15:36:45 +0100454 OSMO_ASSERT(!failures);
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100455}
456
Philipp Maier704c4f02018-06-07 18:51:31 +0200457static 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
499static 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
Philipp Maier48635912020-06-25 20:16:22 +0200550void test_mgcp_client_e1_epname(void)
551{
552 char *epname;
553
554 if (mgcp)
555 talloc_free(mgcp);
556 mgcp = mgcp_client_init(ctx, &conf);
557
558 /* Valid endpoint names */
559 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 1, 15, 64, 0);
560 printf("%s\n", epname);
561 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 2, 14, 32, 0);
562 printf("%s\n", epname);
563 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 3, 13, 32, 4);
564 printf("%s\n", epname);
565 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 4, 12, 16, 0);
566 printf("%s\n", epname);
567 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 5, 11, 16, 2);
568 printf("%s\n", epname);
569 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 6, 10, 16, 4);
570 printf("%s\n", epname);
571 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 7, 9, 16, 6);
572 printf("%s\n", epname);
573 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 8, 8, 8, 0);
574 printf("%s\n", epname);
575 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 9, 7, 8, 1);
576 printf("%s\n", epname);
577 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 10, 6, 8, 2);
578 printf("%s\n", epname);
579 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 11, 5, 8, 3);
580 printf("%s\n", epname);
581 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 12, 4, 8, 4);
582 printf("%s\n", epname);
583 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 13, 3, 8, 5);
584 printf("%s\n", epname);
585 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 14, 2, 8, 6);
586 printf("%s\n", epname);
587 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 15, 1, 8, 7);
588 printf("%s\n", epname);
589
590 /* A few invalid enpoint names */
591 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 15, 1, 128, 0);
592 OSMO_ASSERT(epname == NULL);
593 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 15, 1, 8, 16);
594 OSMO_ASSERT(epname == NULL);
595 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 15, 0, 8, 2);
596 OSMO_ASSERT(epname == NULL);
597 epname = (char *)mgcp_client_e1_epname(ctx, mgcp, 15, 64, 8, 2);
598 OSMO_ASSERT(epname == NULL);
599}
600
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200601static const struct log_info_cat log_categories[] = {
602};
603
604const struct log_info log_info = {
605 .cat = log_categories,
606 .num_cat = ARRAY_SIZE(log_categories),
607};
608
609
610int main(int argc, char **argv)
611{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200612 ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200613 msgb_talloc_ctx_init(ctx, 0);
Neels Hofmeyr60f8e312018-03-30 23:01:07 +0200614 osmo_init_logging2(ctx, &log_info);
Philipp Maier8348abb2017-10-25 12:14:13 +0200615 log_set_print_filename(osmo_stderr_target, 0);
616 log_set_print_timestamp(osmo_stderr_target, 0);
617 log_set_use_color(osmo_stderr_target, 0);
618 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200619
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100620 log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG);
621
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200622 mgcp_client_conf_init(&conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200623
Philipp Maier1dc6be62017-10-05 18:25:37 +0200624 test_mgcp_msg();
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100625 test_mgcp_client_cancel();
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100626 test_sdp_section_start();
Philipp Maier704c4f02018-06-07 18:51:31 +0200627 test_map_codec_to_pt_and_map_pt_to_codec();
628 test_map_pt_to_codec();
Philipp Maier48635912020-06-25 20:16:22 +0200629 test_mgcp_client_e1_epname();
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200630
631 printf("Done\n");
632 fprintf(stderr, "Done\n");
633 return EXIT_SUCCESS;
634}