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