From 601002ad4c0b366966173f0576ba54d118e9bb0f Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 18 Oct 2016 18:38:59 +0200 Subject: mgcp parsing, mgcp test Change-Id: Ibe2ab17b3fa3a506a2e841ba979ea4175e3a21e8 --- openbsc/tests/db/db_test.c | 21 ++-- openbsc/tests/mgcp/Makefile.am | 20 ++++ openbsc/tests/mgcp/mgcpgw_client_test.c | 165 ++++++++++++++++++++++++++++++ openbsc/tests/mgcp/mgcpgw_client_test.err | 1 + openbsc/tests/mgcp/mgcpgw_client_test.ok | 31 ++++++ openbsc/tests/testsuite.at | 7 ++ 6 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 openbsc/tests/mgcp/mgcpgw_client_test.c create mode 100644 openbsc/tests/mgcp/mgcpgw_client_test.err create mode 100644 openbsc/tests/mgcp/mgcpgw_client_test.ok (limited to 'openbsc/tests') diff --git a/openbsc/tests/db/db_test.c b/openbsc/tests/db/db_test.c index f1eadc268..a0c1e79c3 100644 --- a/openbsc/tests/db/db_test.c +++ b/openbsc/tests/db/db_test.c @@ -264,16 +264,17 @@ void vlr_proc_acc_req() {} void vlr_init() {} unsigned int mgcpgw_client_next_endpoint(struct mgcpgw_client *client) { return 0; } -int mgcpgw_client_tx_crcx(struct mgcpgw_client *mgcp, - mgcp_response_cb_t response_cb, void *priv, - uint16_t rtp_endpoint, unsigned int call_id, - enum mgcp_connection_mode mode) -{ return -ENOTSUP; } -int mgcpgw_client_tx_mdcx(struct mgcpgw_client *mgcp, - mgcp_response_cb_t response_cb, void *priv, - uint16_t rtp_endpoint, const char *rtp_conn_addr, - uint16_t rtp_port, enum mgcp_connection_mode mode) -{ return -ENOTSUP; } +struct msgb *mgcp_msg_crcx(struct mgcpgw_client *mgcp, + uint16_t rtp_endpoint, unsigned int call_id, + enum mgcp_connection_mode mode) +{ return NULL; } +struct msgb *mgcp_msg_mdcx(struct mgcpgw_client *mgcp, + uint16_t rtp_endpoint, const char *rtp_conn_addr, + uint16_t rtp_port, enum mgcp_connection_mode mode) +{ return NULL; } +int mgcpgw_client_tx(struct mgcpgw_client *mgcp, struct msgb *msg, + mgcp_response_cb_t response_cb, void *priv) +{ return -EINVAL; } const char *mgcpgw_client_remote_addr_str(struct mgcpgw_client *mgcp) { return "0.0.0.0"; } uint32_t mgcpgw_client_remote_addr_n(struct mgcpgw_client *mgcp) diff --git a/openbsc/tests/mgcp/Makefile.am b/openbsc/tests/mgcp/Makefile.am index 4b18036cc..31d8a51f8 100644 --- a/openbsc/tests/mgcp/Makefile.am +++ b/openbsc/tests/mgcp/Makefile.am @@ -22,10 +22,12 @@ AM_LDFLAGS = \ EXTRA_DIST = \ mgcp_test.ok \ mgcp_transcoding_test.ok \ + mgcpgw_client_test.ok \ $(NULL) noinst_PROGRAMS = \ mgcp_test \ + mgcpgw_client_test \ $(NULL) if BUILD_MGCP_TRANSCODING noinst_PROGRAMS += \ @@ -70,3 +72,21 @@ mgcp_transcoding_test_LDADD = \ -lrt \ -lm \ $(NULL) + +mgcpgw_client_test_SOURCES = \ + mgcpgw_client_test.c \ + $(NULL) + +mgcpgw_client_test_LDADD = \ + $(top_builddir)/src/libbsc/libbsc.a \ + $(top_builddir)/src/libmgcp/libmgcp.a \ + $(top_builddir)/src/libcommon/libcommon.a \ + $(LIBOSMOCORE_LIBS) \ + $(LIBOSMOGSM_LIBS) \ + $(LIBOSMOSCCP_LIBS) \ + $(LIBOSMOVTY_LIBS) \ + $(LIBRARY_DL) \ + $(LIBOSMONETIF_LIBS) \ + -lrt \ + -lm \ + $(NULL) diff --git a/openbsc/tests/mgcp/mgcpgw_client_test.c b/openbsc/tests/mgcp/mgcpgw_client_test.c new file mode 100644 index 000000000..10d109b47 --- /dev/null +++ b/openbsc/tests/mgcp/mgcpgw_client_test.c @@ -0,0 +1,165 @@ +/* + * (C) 2016 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include +#include + +void *ctx; + +#define buf_len 4096 + +#if 0 +static struct msgb *from_hex(const char *hex) +{ + struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex"); + unsigned int l = osmo_hexparse(hex, msg->data, buf_len); + msg->l2h = msgb_put(msg, l); + return msg; +} + +static struct msgb *mgcp_from_str(const char *head, const char *params) +{ + struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str"); + unsigned int l; + char *data; + l = strlen(head); + msg->l2h = msgb_put(msg, l); + data = (char*)msgb_l2(msg); + strncpy(data, head, l); + + data = (char*)msgb_put(msg, 1); + *data = '\n'; + + l = strlen(params); + data = (char*)msgb_put(msg, l); + strncpy(data, params, l); + + return msg; +} +#endif + +static struct msgb *from_str(const char *str) +{ + struct msgb *msg = msgb_alloc(buf_len, "from_str"); + unsigned int l = strlen(str); + char *data; + msg->l2h = msgb_put(msg, l); + data = (char*)msgb_l2(msg); + strncpy(data, str, l); + return msg; +} + +static struct mgcpgw_client_conf conf; +struct mgcpgw_client *mgcp = NULL; + +static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment, + int conn_id, const char *params) +{ + static char compose[4096 - 128]; + int len; + + len = snprintf(compose, sizeof(compose), + "%d %u %s\r\nI: %d\n\n%s", + code, trans_id, comment, conn_id, params); + OSMO_ASSERT(len < sizeof(compose)); + OSMO_ASSERT(len > 0); + + printf("composed response:\n-----\n%s\n-----\n", + compose); + mgcpgw_client_rx(mgcp, from_str(compose)); +} + +void test_response_cb(struct mgcp_response *response, void *priv) +{ + OSMO_ASSERT(priv == mgcp); + mgcp_response_parse_params(response); + + printf("response cb received:\n" + " head.response_code = %d\n" + " head.trans_id = %u\n" + " head.comment = %s\n" + " audio_port = %u\n", + response->head.response_code, + response->head.trans_id, + response->head.comment, + response->audio_port + ); +} + +mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) +{ + mgcp_trans_id_t trans_id; + trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; + char *end; + + OSMO_ASSERT(mgcpgw_client_pending_add(mgcp, trans_id, test_response_cb, mgcp)); + + end = msgb_put(msg, 1); + *end = '\0'; + printf("composed:\n-----\n%s\n-----\n", + (char*)msgb_l2(msg)); + + talloc_free(msg); + return trans_id; +} + +void test_crcx(void) +{ + struct msgb *msg; + mgcp_trans_id_t trans_id; + + printf("\n===== %s =====\n", __func__); + + if (mgcp) + talloc_free(mgcp); + mgcp = mgcpgw_client_init(ctx, &conf); + + msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); + trans_id = dummy_mgcp_send(msg); + + reply_to(trans_id, 200, "OK", 1, + "v=0\r\n" + "o=- 1 23 IN IP4 10.9.1.120\r\n" + "s=-\r\n" + "c=IN IP4 10.9.1.120\r\n" + "t=0 0\r\n" + "m=audio 16002 RTP/AVP 98\r\n" + "a=rtpmap:98 AMR/8000\r\n" + "a=ptime:20\r\n"); +} + +int main(int argc, char **argv) +{ + ctx = talloc_named_const(NULL, 1, "mgcpgw_client_test"); + msgb_talloc_ctx_init(ctx, 0); + osmo_init_logging(&log_info); + + mgcpgw_client_conf_init(&conf); + + test_crcx(); + + printf("Done\n"); + fprintf(stderr, "Done\n"); + return EXIT_SUCCESS; +} diff --git a/openbsc/tests/mgcp/mgcpgw_client_test.err b/openbsc/tests/mgcp/mgcpgw_client_test.err new file mode 100644 index 000000000..a965a70ed --- /dev/null +++ b/openbsc/tests/mgcp/mgcpgw_client_test.err @@ -0,0 +1 @@ +Done diff --git a/openbsc/tests/mgcp/mgcpgw_client_test.ok b/openbsc/tests/mgcp/mgcpgw_client_test.ok new file mode 100644 index 000000000..d35f2d6b0 --- /dev/null +++ b/openbsc/tests/mgcp/mgcpgw_client_test.ok @@ -0,0 +1,31 @@ + +===== test_crcx ===== +composed: +----- +CRCX 1 17@mgw MGCP 1.0 +C: 2a +L: p:20, a:AMR, nt:IN +M: loopback + +----- +composed response: +----- +200 1 OK +I: 1 + +v=0 +o=- 1 23 IN IP4 10.9.1.120 +s=- +c=IN IP4 10.9.1.120 +t=0 0 +m=audio 16002 RTP/AVP 98 +a=rtpmap:98 AMR/8000 +a=ptime:20 + +----- +response cb received: + head.response_code = 200 + head.trans_id = 1 + head.comment = OK + audio_port = 16002 +Done diff --git a/openbsc/tests/testsuite.at b/openbsc/tests/testsuite.at index 375bd8228..5291185d4 100644 --- a/openbsc/tests/testsuite.at +++ b/openbsc/tests/testsuite.at @@ -33,6 +33,13 @@ cat $abs_srcdir/mgcp/mgcp_transcoding_test.ok > expout AT_CHECK([$abs_top_builddir/tests/mgcp/mgcp_transcoding_test], [], [expout], [ignore]) AT_CLEANUP +AT_SETUP([mgcpgw_client]) +AT_KEYWORDS([mgcpgw_client]) +cat $abs_srcdir/mgcp/mgcpgw_client_test.ok > expout +cat $abs_srcdir/mgcp/mgcpgw_client_test.err > experr +AT_CHECK([$abs_top_builddir/tests/mgcp/mgcpgw_client_test], [], [expout], [experr]) +AT_CLEANUP + AT_SETUP([gprs]) AT_KEYWORDS([gprs]) cat $abs_srcdir/gprs/gprs_test.ok > expout -- cgit v1.2.3