blob: d61078325f4a230cc01a9434c0772c30dd59ec55 [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,
Philipp Maier1dc6be62017-10-05 18:25:37 +0200160 };
161
162 if (mgcp)
163 talloc_free(mgcp);
164 mgcp = mgcp_client_init(ctx, &conf);
165
166 printf("\n");
167
168 printf("Generated CRCX message:\n");
169 mgcp_msg.verb = MGCP_VERB_CRCX;
170 mgcp_msg.presence =
171 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
172 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE);
173 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
174 printf("%s\n", (char *)msg->data);
175
Philipp Maier704c4f02018-06-07 18:51:31 +0200176 printf("Generated CRCX message (two codecs):\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 mgcp_msg.codecs_len = 2;
182 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200183 mgcp_msg.codecs_len = 1;
Philipp Maier704c4f02018-06-07 18:51:31 +0200184 printf("%s\n", (char *)msg->data);
185
186 printf("Generated CRCX message (three codecs, one with custom pt):\n");
187 mgcp_msg.verb = MGCP_VERB_CRCX;
188 mgcp_msg.presence =
189 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
190 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE);
191 mgcp_msg.codecs_len = 3;
192 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200193 mgcp_msg.codecs_len = 1;
194 printf("%s\n", (char *)msg->data);
Philipp Maier704c4f02018-06-07 18:51:31 +0200195
Philipp Maier1dc6be62017-10-05 18:25:37 +0200196 printf("Generated MDCX message:\n");
197 mgcp_msg.verb = MGCP_VERB_MDCX;
198 mgcp_msg.presence =
199 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
200 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
201 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
202 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
203 printf("%s\n", (char *)msg->data);
204
Philipp Maier704c4f02018-06-07 18:51:31 +0200205 printf("Generated MDCX message (two codecs):\n");
206 mgcp_msg.verb = MGCP_VERB_MDCX;
207 mgcp_msg.presence =
208 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
209 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
210 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
211 mgcp_msg.codecs_len = 2;
212 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200213 mgcp_msg.codecs_len = 1;
Philipp Maier704c4f02018-06-07 18:51:31 +0200214 printf("%s\n", (char *)msg->data);
215
216 printf("Generated MDCX message (three codecs, one with custom pt):\n");
217 mgcp_msg.verb = MGCP_VERB_MDCX;
218 mgcp_msg.presence =
219 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
220 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
221 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
222 mgcp_msg.codecs_len = 3;
223 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200224 mgcp_msg.codecs_len = 1;
225 printf("%s\n", (char *)msg->data);
Philipp Maier704c4f02018-06-07 18:51:31 +0200226
Philipp Maier1dc6be62017-10-05 18:25:37 +0200227 printf("Generated DLCX message:\n");
228 mgcp_msg.verb = MGCP_VERB_DLCX;
229 mgcp_msg.presence =
230 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
231 MGCP_MSG_PRESENCE_CONN_ID);
232 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
233 printf("%s\n", (char *)msg->data);
234
235 printf("Generated AUEP message:\n");
236 mgcp_msg.verb = MGCP_VERB_AUEP;
237 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
238 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
239 printf("%s\n", msg->data);
240
241 printf("Generated RSIP message:\n");
242 mgcp_msg.verb = MGCP_VERB_RSIP;
243 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
244 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
245 printf("%s\n", (char *)msg->data);
246
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200247 printf("Generate X-Osmo-IGN message:\n");
248 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
249 mgcp_msg.verb = MGCP_VERB_CRCX;
250 mgcp_msg.presence =
251 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
252 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE
253 | MGCP_MSG_PRESENCE_X_OSMO_IGN);
254 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
255 printf("%s\n", (char *)msg->data);
256
Philipp Maier1dc6be62017-10-05 18:25:37 +0200257 printf("Overfolow test:\n");
258 mgcp_msg.verb = MGCP_VERB_MDCX;
259 mgcp_msg.presence =
260 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
261 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
262 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
263 memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow));
264 audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0';
265 mgcp_msg.audio_ip = audio_ip_overflow;
266 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
267 OSMO_ASSERT(msg == NULL);
268
269 printf("\n");
270 msgb_free(msg);
271}
272
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100273void test_mgcp_client_cancel()
274{
275 mgcp_trans_id_t trans_id;
276 struct msgb *msg;
277 struct mgcp_msg mgcp_msg = {
278 .verb = MGCP_VERB_CRCX,
279 .audio_ip = "192.168.100.23",
280 .endpoint = "23@mgw",
281 .audio_port = 1234,
282 .call_id = 47,
Philipp Maier922ae9a2017-12-04 15:53:37 +0100283 .conn_id = "11",
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100284 .conn_mode = MGCP_CONN_RECV_SEND,
285 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
286 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE),
Philipp Maier704c4f02018-06-07 18:51:31 +0200287 .ptime = 20,
288 .codecs[0] = CODEC_AMR_8000_1,
Pau Espin Pedrol21cdbfc2019-04-23 12:36:32 +0200289 .codecs_len = 1
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100290 };
291
292 printf("\n%s():\n", __func__);
293 fprintf(stderr, "\n%s():\n", __func__);
294
295 if (mgcp)
296 talloc_free(mgcp);
297 mgcp = mgcp_client_init(ctx, &conf);
298
299 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
300 trans_id = mgcp_msg_trans_id(msg);
301 fprintf(stderr, "- composed msg with trans_id=%u\n", trans_id);
302
303 fprintf(stderr, "- not in queue yet, cannot cancel yet\n");
304 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT);
305
306 fprintf(stderr, "- enqueue\n");
307 dummy_mgcp_send(msg);
308
309 fprintf(stderr, "- cancel succeeds\n");
310 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == 0);
311
312 fprintf(stderr, "- late response gets discarded\n");
Neels Hofmeyrb1bb1fa2018-09-03 21:07:26 +0200313 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 +0100314
315 fprintf(stderr, "- canceling again does nothing\n");
316 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT);
317
318 fprintf(stderr, "%s() done\n", __func__);
319}
320
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100321struct sdp_section_start_test {
322 const char *body;
323 int expect_rc;
324 struct mgcp_response expect_params;
325};
326
327static struct sdp_section_start_test sdp_section_start_tests[] = {
328 {
329 .body = "",
330 .expect_rc = -EINVAL,
331 },
332 {
333 .body = "\n\n",
334 },
335 {
336 .body = "\r\n\r\n",
337 },
338 {
339 .body = "\n\r\n\r",
340 },
341 {
342 .body = "some mgcp header data\r\nand header params"
343 "\n\n"
344 "m=audio 23\r\n",
345 .expect_params = {
346 .audio_port = 23,
347 },
348 },
349 {
350 .body = "some mgcp header data\r\nand header params"
351 "\r\n\r\n"
352 "m=audio 23\r\n",
353 .expect_params = {
354 .audio_port = 23,
355 },
356 },
357 {
358 .body = "some mgcp header data\r\nand header params"
359 "\n\r\n\r"
360 "m=audio 23\r\n",
361 .expect_params = {
362 .audio_port = 23,
363 },
364 },
365 {
366 .body = "some mgcp header data\r\nand header params"
367 "\n\r\n"
368 "m=audio 23\r\n",
369 .expect_rc = -EINVAL,
370 },
371 {
372 .body = "some mgcp header data\r\nand header params"
373 "\r\n\r"
374 "m=audio 23\r\n",
375 .expect_rc = -EINVAL,
376 },
377 {
378 .body = "some mgcp header data\r\nand header params"
379 "\n\r\r"
380 "m=audio 23\r\n",
381 .expect_rc = -EINVAL,
382 },
383};
384
385void test_sdp_section_start()
386{
387 int i;
388 int failures = 0;
389
390 for (i = 0; i < ARRAY_SIZE(sdp_section_start_tests); i++) {
391 int rc;
392 struct sdp_section_start_test *t = &sdp_section_start_tests[i];
393 struct mgcp_response *r = talloc_zero(ctx, struct mgcp_response);
394
395 r->body = talloc_strdup(r, t->body);
396
397 printf("\n%s() test [%d]:\n", __func__, i);
398 fprintf(stderr, "\n%s() test [%d]:\n", __func__, i);
399 fprintf(stderr, "body: \"%s\"\n", osmo_escape_str(r->body, -1));
400
401 rc = mgcp_response_parse_params(r);
402
403 fprintf(stderr, "got rc=%d\n", rc);
404 if (rc != t->expect_rc) {
405 fprintf(stderr, "FAIL: Expected rc=%d\n", t->expect_rc);
406 failures++;
407 }
408 if (rc) {
409 talloc_free(r);
410 continue;
411 }
412
413 fprintf(stderr, "got audio_port=%u\n", t->expect_params.audio_port);
414 if (r->audio_port != t->expect_params.audio_port) {
415 fprintf(stderr, "FAIL: Expected audio_port=%u\n", t->expect_params.audio_port);
416 failures++;
417 }
418 talloc_free(r);
419 }
420
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100421 OSMO_ASSERT(!failures);
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100422}
423
Philipp Maier704c4f02018-06-07 18:51:31 +0200424static void test_map_pt_to_codec(void)
425{
426 /* Full form */
427 OSMO_ASSERT(map_str_to_codec("PCMU/8000/1") == CODEC_PCMU_8000_1);
428 OSMO_ASSERT(map_str_to_codec("GSM/8000/1") == CODEC_GSM_8000_1);
429 OSMO_ASSERT(map_str_to_codec("PCMA/8000/1") == CODEC_PCMA_8000_1);
430 OSMO_ASSERT(map_str_to_codec("G729/8000/1") == CODEC_G729_8000_1);
431 OSMO_ASSERT(map_str_to_codec("GSM-EFR/8000/1") == CODEC_GSMEFR_8000_1);
432 OSMO_ASSERT(map_str_to_codec("GSM-HR-08/8000/1") == CODEC_GSMHR_8000_1);
433 OSMO_ASSERT(map_str_to_codec("AMR/8000/1") == CODEC_AMR_8000_1);
434 OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1") == CODEC_AMRWB_16000_1);
435
436 /* Short form */
437 OSMO_ASSERT(map_str_to_codec("GSM-EFR") == CODEC_GSMEFR_8000_1);
438 OSMO_ASSERT(map_str_to_codec("G729") == CODEC_G729_8000_1);
439 OSMO_ASSERT(map_str_to_codec("GSM-HR-08") == CODEC_GSMHR_8000_1);
440
441 /* We do not care about what is after the first delimiter */
442 OSMO_ASSERT(map_str_to_codec("AMR-WB/123///456") == CODEC_AMRWB_16000_1);
443 OSMO_ASSERT(map_str_to_codec("PCMA/asdf") == CODEC_PCMA_8000_1);
444 OSMO_ASSERT(map_str_to_codec("GSM/qwertz") == CODEC_GSM_8000_1);
445
446 /* A trailing delimiter should not hurt */
447 OSMO_ASSERT(map_str_to_codec("AMR/") == CODEC_AMR_8000_1);
448 OSMO_ASSERT(map_str_to_codec("G729/") == CODEC_G729_8000_1);
449 OSMO_ASSERT(map_str_to_codec("GSM/") == CODEC_GSM_8000_1);
450
451 /* This is expected to fail */
452 OSMO_ASSERT(map_str_to_codec("INVALID/1234/7") == -1);
453 OSMO_ASSERT(map_str_to_codec(NULL) == -1);
454 OSMO_ASSERT(map_str_to_codec("") == -1);
455 OSMO_ASSERT(map_str_to_codec("/////") == -1);
456
457 /* The buffers are 64 bytes long, check what happens with overlong
458 * strings as input (This schould still work.) */
459 OSMO_ASSERT(map_str_to_codec("AMR-WB/16000/1############################################################################################################") == CODEC_AMRWB_16000_1);
460
461 /* This should not work, as there is no delimiter after the codec
462 * name */
463 OSMO_ASSERT(map_str_to_codec("AMR-WB####################################################################################################################") == -1);
464}
465
466static void test_map_codec_to_pt_and_map_pt_to_codec(void)
467{
468 struct ptmap ptmap[10];
469 unsigned int ptmap_len;
470 unsigned int i;
471
472 ptmap[0].codec = CODEC_GSMEFR_8000_1;
473 ptmap[0].pt = 96;
474 ptmap[1].codec = CODEC_GSMHR_8000_1;
475 ptmap[1].pt = 97;
476 ptmap[2].codec = CODEC_AMR_8000_1;
477 ptmap[2].pt = 98;
478 ptmap[3].codec = CODEC_AMRWB_16000_1;
479 ptmap[3].pt = 99;
480 ptmap_len = 4;
481
482 /* Mappings that are covered by the table */
483 for (i = 0; i < ptmap_len; i++)
484 printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec));
485 for (i = 0; i < ptmap_len; i++)
486 printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt));
487 printf("\n");
488
489 /* Map some codecs/payload types from the static range, result must
490 * always be a 1:1 mapping */
491 printf(" %u => %u\n", CODEC_PCMU_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMU_8000_1));
492 printf(" %u => %u\n", CODEC_GSM_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_GSM_8000_1));
493 printf(" %u => %u\n", CODEC_PCMA_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_PCMA_8000_1));
494 printf(" %u => %u\n", CODEC_G729_8000_1, map_codec_to_pt(ptmap, ptmap_len, CODEC_G729_8000_1));
495 printf(" %u <= %u\n", CODEC_PCMU_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMU_8000_1));
496 printf(" %u <= %u\n", CODEC_GSM_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_GSM_8000_1));
497 printf(" %u <= %u\n", CODEC_PCMA_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_PCMA_8000_1));
498 printf(" %u <= %u\n", CODEC_G729_8000_1, map_pt_to_codec(ptmap, ptmap_len, CODEC_G729_8000_1));
499 printf("\n");
500
501 /* Try to do mappings from statically defined range to danymic range and vice versa. This
502 * is illegal and should result into a 1:1 mapping */
503 ptmap[3].codec = CODEC_AMRWB_16000_1;
504 ptmap[3].pt = 2;
505 ptmap[4].codec = CODEC_PCMU_8000_1;
506 ptmap[4].pt = 100;
507 ptmap_len = 5;
508
509 /* Apply all mappings again, the illegal ones we defined should result into 1:1 mappings */
510 for (i = 0; i < ptmap_len; i++)
511 printf(" %u => %u\n", ptmap[i].codec, map_codec_to_pt(ptmap, ptmap_len, ptmap[i].codec));
512 for (i = 0; i < ptmap_len; i++)
513 printf(" %u <= %u\n", ptmap[i].pt, map_pt_to_codec(ptmap, ptmap_len, ptmap[i].pt));
514 printf("\n");
515}
516
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200517static const struct log_info_cat log_categories[] = {
518};
519
520const struct log_info log_info = {
521 .cat = log_categories,
522 .num_cat = ARRAY_SIZE(log_categories),
523};
524
525
526int main(int argc, char **argv)
527{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200528 ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200529 msgb_talloc_ctx_init(ctx, 0);
Neels Hofmeyr60f8e312018-03-30 23:01:07 +0200530 osmo_init_logging2(ctx, &log_info);
Philipp Maier8348abb2017-10-25 12:14:13 +0200531 log_set_print_filename(osmo_stderr_target, 0);
532 log_set_print_timestamp(osmo_stderr_target, 0);
533 log_set_use_color(osmo_stderr_target, 0);
534 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200535
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100536 log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG);
537
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200538 mgcp_client_conf_init(&conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200539
Philipp Maier1dc6be62017-10-05 18:25:37 +0200540 test_mgcp_msg();
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100541 test_mgcp_client_cancel();
Neels Hofmeyra8c6a9c2018-02-21 15:36:45 +0100542 test_sdp_section_start();
Philipp Maier704c4f02018-06-07 18:51:31 +0200543 test_map_codec_to_pt_and_map_pt_to_codec();
544 test_map_pt_to_codec();
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200545
546 printf("Done\n");
547 fprintf(stderr, "Done\n");
548 return EXIT_SUCCESS;
549}