diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2023-05-04 16:18:20 +0200 |
---|---|---|
committer | pespin <pespin@sysmocom.de> | 2023-05-15 10:01:45 +0000 |
commit | 0ded29a701187d2b17341db29d05ebea31fff998 (patch) | |
tree | b594e07d64dba0dd608bc3a4a28cafa4119c953a /tests | |
parent | 520c406b850a9d2ef306e870cf4357726fcd9116 (diff) |
Move testdir src/tests/ -> tests/
Move tests to usual directory in most projects.
Change-Id: Ib9ca59d1dffbbb13fcb9ae2e597b3f114f77524e
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 79 | ||||
-rw-r--r-- | tests/dummy_cn_sua.c | 386 | ||||
-rw-r--r-- | tests/hnb-test-layers.h | 19 | ||||
-rw-r--r-- | tests/hnb-test-ranap.c | 86 | ||||
-rw-r--r-- | tests/hnb-test-rua.c | 41 | ||||
-rw-r--r-- | tests/hnb-test.c | 1018 | ||||
-rw-r--r-- | tests/hnb-test.h | 77 | ||||
-rw-r--r-- | tests/test-helpers.c | 427 | ||||
-rw-r--r-- | tests/test-helpers.err | 2 | ||||
-rw-r--r-- | tests/test-helpers.ok | 67 | ||||
-rw-r--r-- | tests/test-hnbap.c | 177 | ||||
-rw-r--r-- | tests/test-hnbap.ok | 4 | ||||
-rw-r--r-- | tests/test-ranap.c | 207 | ||||
-rw-r--r-- | tests/test-ranap.ok | 222 | ||||
-rw-r--r-- | tests/test_common.c | 112 | ||||
-rw-r--r-- | tests/test_common.h | 11 | ||||
-rw-r--r-- | tests/testsuite.at | 22 |
17 files changed, 2957 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..c08c6d1 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,79 @@ +AM_CFLAGS = -g -Wall -I$(top_srcdir)/tests \ + -I$(top_srcdir)/include -I$(top_builddir)/include \ + $(OSMOVTY_CFLAGS) $(OSMOCORE_CFLAGS) $(OSMOGSM_CFLAGS) \ + $(OSMONETIF_CFLAGS) $(ASN1C_CFLAGS) $(OSMOSIGTRAN_CFLAGS) + +AM_LDFLAGS = -no-install + +COMMON_LIBS = $(OSMOVTY_LIBS) $(OSMOCORE_LIBS) $(OSMOGSM_LIBS) $(ASN1C_LIBS) $(OSMOSIGTRAN_LIBS) -lsctp + +check_PROGRAMS = test-ranap test-helpers test-hnbap hnb-test #dummy-cn + +noinst_HEADERS = test_common.h hnb-test.h hnb-test-layers.h + +test_helpers_SOURCES = test-helpers.c test_common.c +test_helpers_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-ranap.la + +test_hnbap_SOURCES = test-hnbap.c test_common.c +test_hnbap_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-hnbap.la $(top_builddir)/src/libosmo-ranap.la + +hnb_test_SOURCES = hnb-test.c test_common.c hnb-test-ranap.c hnb-test-rua.c +hnb_test_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-hnbap.la $(top_builddir)/src/libosmo-rua.la $(top_builddir)/src/libosmo-ranap.la + +test_ranap_SOURCES = test-ranap.c test_common.c +test_ranap_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-ranap.la + +# TODO: re-add using SCCP? +#dummy_cn_SOURCES = test_common.c dummy_cn_sua.c +#dummy_cn_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-ranap.la + +$(top_builddir)/src/libosmo-hnbap.la: + $(MAKE) -C $(top_builddir)/src libosmo-hnbap.la + +$(top_builddir)/src/libosmo-rua.la: + $(MAKE) -C $(top_builddir)/src libosmo-rua.la + +$(top_builddir)/src/libosmo-ranap.la: + $(MAKE) -C $(top_builddir)/src libosmo-ranap.la + +# The `:;' works around a Bash 3.2 bug when the output is not writeable. +$(srcdir)/package.m4: $(top_srcdir)/configure.ac + :;{ \ + echo '# Signature of the current package.' && \ + echo 'm4_define([AT_PACKAGE_NAME],' && \ + echo ' [$(PACKAGE_NAME)])' && \ + echo 'm4_define([AT_PACKAGE_TARNAME],' && \ + echo ' [$(PACKAGE_TARNAME)])' && \ + echo 'm4_define([AT_PACKAGE_VERSION],' && \ + echo ' [$(PACKAGE_VERSION)])' && \ + echo 'm4_define([AT_PACKAGE_STRING],' && \ + echo ' [$(PACKAGE_STRING)])' && \ + echo 'm4_define([AT_PACKAGE_BUGREPORT],' && \ + echo ' [$(PACKAGE_BUGREPORT)])'; \ + echo 'm4_define([AT_PACKAGE_URL],' && \ + echo ' [$(PACKAGE_URL)])'; \ + } >'$(srcdir)/package.m4' + +TESTSUITE = $(srcdir)/testsuite +EXTRA_DIST = testsuite.at $(TESTSUITE) $(srcdir)/package.m4 \ + test-helpers.err test-helpers.ok test-hnbap.ok test-ranap.ok + +DISTCLEANFILES = atconfig + +check-local: atconfig $(TESTSUITE) + $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS) + +installcheck-local: atconfig $(TESTSUITE) + $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \ + $(TESTSUITEFLAGS) + +clean-local: + test ! -f '$(TESTSUITE)' || \ + $(SHELL) '$(TESTSUITE)' --clean + +AUTOM4TE = $(SHELL) $(top_srcdir)/missing --run autom4te +AUTOTEST = $(AUTOM4TE) --language=autotest + +$(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4 + $(AUTOTEST) -I '$(top_srcdir)/include' -I '$(srcdir)' -o $@.tmp $@.at + mv $@.tmp $@ diff --git a/tests/dummy_cn_sua.c b/tests/dummy_cn_sua.c new file mode 100644 index 0000000..cf71608 --- /dev/null +++ b/tests/dummy_cn_sua.c @@ -0,0 +1,386 @@ +#include <stdint.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define _GNU_SOURCE +#include <getopt.h> + +#include <osmocom/core/select.h> +#include <osmocom/core/prim.h> +#include <osmocom/core/talloc.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/application.h> +#include <osmocom/vty/logging.h> + +#include <osmocom/gsm/gsm48.h> + +#include <osmocom/sigtran/sua.h> +#include <osmocom/sigtran/sccp_sap.h> + +#include "test_common.h" + +#include <osmocom/ranap/ranap_ies_defs.h> +#include <osmocom/ranap/ranap_common_cn.h> +#include <osmocom/ranap/ranap_msg_factory.h> + +const char *cmdline_bind_addr = "127.0.0.1"; + +struct ue_conn_ctx { + struct llist_head list; + struct osmo_sccp_link *link; + uint32_t conn_id; +}; + +static LLIST_HEAD(conn_ctx_list); + +struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_link *link, uint32_t conn_id) +{ + struct ue_conn_ctx *ctx = talloc_zero(NULL, struct ue_conn_ctx); + + ctx->link = link; + ctx->conn_id = conn_id; + llist_add(&ctx->list, &conn_ctx_list); + + return ctx; +} + +struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sccp_link *link, uint32_t conn_id) +{ + struct ue_conn_ctx *ctx; + + llist_for_each_entry(ctx, &conn_ctx_list, list) { + if (ctx->link == link && ctx->conn_id == conn_id) + return ctx; + } + return NULL; +} + +/*********************************************************************** + * RANAP handling + ***********************************************************************/ + +static int ranap_handle_co_initial_ue(struct ue_conn_ctx *ctx, RANAP_InitialUE_MessageIEs_t *ies) +{ + struct gprs_ra_id ra_id; + uint16_t sai; + struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); + uint8_t *cur; + struct osmo_scu_prim *prim; + + ranap_parse_lai(&ra_id, &ies->lai); + sai = asn1str_to_u16(&ies->sai.sAC); + cur = msgb_put(msg, ies->nas_pdu.size); + memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size); + msgb_free(msg); + /* FIXME: set msgb_gmmh() */ + + /* FIXME: Feed into the MM layer */ + //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id); + + msg = ranap_new_msg_dt(0, NULL, 0); + + msg->l2h = msg->data; + prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); + prim->u.data.conn_id = ctx->conn_id; + osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_DATA, + PRIM_OP_REQUEST, msg); + + osmo_sua_user_link_down(ctx->link, &prim->oph); + + return 0; +} + +static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies) +{ + struct gprs_ra_id _ra_id, *ra_id = NULL; + uint16_t _sai, *sai = NULL; + struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); + uint8_t *cur; + + if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) { + ranap_parse_lai(&_ra_id, &ies->lai); + ra_id = &_ra_id; + if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) { + _ra_id.rac = asn1str_to_u8(&ies->rac); + } + if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) { + _sai = asn1str_to_u16(&ies->sai.sAC); + sai = &_sai; + } + } + + cur = msgb_put(msg, ies->nas_pdu.size); + memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size); + /* FIXME: set msgb_gmmh() */ + + /* FIXME: Feed into the MM/CC/SMS-CP layer */ + //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id); + + return 0; +} + +static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) +{ + if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) + LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n", + ranap_cause_str(&ies->cause)); + else + LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n"); + + return 0; +} + +static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies) +{ + struct msgb *msg; + struct osmo_scu_prim *prim; + + LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n"); + msg = ranap_new_msg_iu_rel_cmd(&ies->cause); + msg->l2h = msg->data; + prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); + prim->u.data.conn_id = ctx->conn_id; + osmo_prim_init(&prim->oph, SCCP_SAP_USER, + OSMO_SCU_PRIM_N_DATA, + PRIM_OP_REQUEST, msg); + osmo_sua_user_link_down(ctx->link, &prim->oph); + return 0; +} + +/* Entry point for connection-oriented ANAP message */ +static void cn_ranap_handle_co(void *ctx, ranap_message *message) +{ + int rc = 0; + + switch (message->direction) { + case RANAP_RANAP_PDU_PR_initiatingMessage: + switch (message->procedureCode) { + case RANAP_ProcedureCode_id_InitialUE_Message: + rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs); + break; + case RANAP_ProcedureCode_id_DirectTransfer: + rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs); + break; + case RANAP_ProcedureCode_id_ErrorIndication: + rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs); + break; + case RANAP_ProcedureCode_id_Iu_ReleaseRequest: + /* Iu Release Request */ + rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs); + break; + } + break; + case RANAP_RANAP_PDU_PR_successfulOutcome: + switch (message->procedureCode) { + case RANAP_ProcedureCode_id_RAB_Assignment: + /* RAB Assignment Response */ + break; + case RANAP_ProcedureCode_id_SecurityModeControl: + /* Security Mode Complete */ + break; + case RANAP_ProcedureCode_id_Iu_Release: + /* Iu Release Complete */ + break; + } + case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: + case RANAP_RANAP_PDU_PR_outcome: + default: + break; + } +} + +static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies) +{ + /* FIXME: send reset response */ + + LOGP(DRANAP, LOGL_ERROR, "Rx Reset Request\n"); +} + +static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) +{ + if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) + LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n", + ranap_cause_str(&ies->cause)); + else + LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n"); + + return 0; +} + +/* Entry point for connection-less RANAP message */ +static void cn_ranap_handle_cl(void *ctx, ranap_message *message) +{ + int rc = 0; + + switch (message->direction) { + case RANAP_RANAP_PDU_PR_initiatingMessage: + switch (message->procedureCode) { + case RANAP_ProcedureCode_id_Reset: + /* received reset.req, send reset.resp */ + rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs); + break; + case RANAP_ProcedureCode_id_ErrorIndication: + rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs); + break; + } + break; + case RANAP_RANAP_PDU_PR_successfulOutcome: + case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: + case RANAP_RANAP_PDU_PR_outcome: + default: + break; + } +} + +/*********************************************************************** + * + ***********************************************************************/ + +int tx_unitdata(struct osmo_sccp_link *link); +int tx_conn_req(struct osmo_sccp_link *link, uint32_t conn_id); + +struct osmo_prim_hdr *make_conn_req(uint32_t conn_id); +struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len); + +struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param) +{ + struct msgb *msg = msgb_alloc(1024, "conn_resp"); + struct osmo_scu_prim *prim; + + prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); + osmo_prim_init(&prim->oph, SCCP_SAP_USER, + OSMO_SCU_PRIM_N_CONNECT, + PRIM_OP_RESPONSE, msg); + memcpy(&prim->u.connect, param, sizeof(prim->u.connect)); + return &prim->oph; +} + +static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) +{ + struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; + struct osmo_prim_hdr *resp = NULL; + const uint8_t payload[] = { 0xb1, 0xb2, 0xb3 }; + int rc; + struct ue_conn_ctx *ue; + + printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); + + switch (OSMO_PRIM_HDR(oph)) { + case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): + /* confirmation of outbound connection */ + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): + /* indication of new inbound connection request*/ + printf("N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id); + if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */ + !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) { + LOGP(DMAIN, LOGL_NOTICE, "Received invalid N-CONNECT.ind\n"); + return 0; + } + /* FIXME: allocate UE context */ + ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id); + /* first ensure the local SUA/SCCP socket is ACTIVE */ + resp = make_conn_resp(&prim->u.connect); + osmo_sua_user_link_down(link, resp); + /* then handle the RANAP payload */ + rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): + /* indication of disconnect */ + printf("N-DISCONNECT.ind(%u)\n", prim->u.disconnect.conn_id); + ue = ue_conn_ctx_find(link, prim->u.disconnect.conn_id); + rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): + /* connection-oriented data received */ + printf("N-DATA.ind(%u, %s)\n", prim->u.data.conn_id, + osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); + /* resolve UE context */ + ue = ue_conn_ctx_find(link, prim->u.data.conn_id); + rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): + /* connection-oriented data received */ + printf("N-UNITDATA.ind(%s)\n", + osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); + rc = ranap_cn_rx_cl(cn_ranap_handle_cl, link, msgb_l2(oph->msg), msgb_l2len(oph->msg)); + break; + } + + msgb_free(oph->msg); + return 0; +} + +static void print_usage() +{ + printf("Usage: dummy-cn\n"); +} + +static void print_help() +{ + printf(" -h --help This text.\n"); + printf(" -b --bind addr Bind to local IP address (default 127.0.0.1)\n"); +} + +static void handle_options(int argc, char **argv) +{ + while (1) { + int option_index = 0, c; + static struct option long_options[] = { + {"help", 0, 0, 'h'}, + {"bind", 1, 0, 'b'}, + {0, 0, 0, 0} + }; + + c = getopt_long(argc, argv, "hb:V", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + print_usage(); + print_help(); + exit(0); + case 'b': + cmdline_bind_addr = optarg; + break; + default: + printf("Unknown cmdline argument (-h shows help)\n"); + exit(1); + break; + } + } +} + +int main(int argc, char **argv) +{ + struct osmo_sccp_user *user; + void *ctx = talloc_named_const(NULL, 1, "root"); + int rc; + int port = 14001; + + asn1_xer_print = 1; + + osmo_sua_set_log_area(DSUA); + ranap_set_log_area(DRANAP); + + test_common_init(); + + handle_options(argc, argv); + + user = osmo_sua_user_create(ctx, sccp_sap_up, ctx); + + rc = osmo_sua_server_listen(user, cmdline_bind_addr, port); + if (rc < 0) { + exit(1); + } + + printf("dummy-cn listening on %s %d\n", cmdline_bind_addr, port); + + while (1) { + osmo_select_main(0); + } +} diff --git a/tests/hnb-test-layers.h b/tests/hnb-test-layers.h new file mode 100644 index 0000000..6211661 --- /dev/null +++ b/tests/hnb-test-layers.h @@ -0,0 +1,19 @@ +#pragma once + +struct ANY; +struct ranap_message_s; +struct hnb_test; + +/* main calls RUA */ +void hnb_test_rua_dt_handle(struct hnb_test *hnb, struct ANY *in); +void hnb_test_rua_cl_handle(struct hnb_test *hnb, struct ANY *in); + +/* RUA calls RANAP */ +void hnb_test_rua_dt_handle_ranap(void *priv, struct ranap_message_s *ranap_msg); +void hnb_test_rua_cl_handle_ranap(void *priv, struct ranap_message_s *ranap_msg); + +/* RANAP calls main with actual payload*/ +void hnb_test_nas_rx_dtap(struct hnb_test *hnb, void *data, int len); +void hnb_test_rx_secmode_cmd(struct hnb_test *hnb, long ip_alg); +void hnb_test_rx_iu_release(struct hnb_test *hnb); +void hnb_test_rx_paging(struct hnb_test *hnb, const char *imsi); diff --git a/tests/hnb-test-ranap.c b/tests/hnb-test-ranap.c new file mode 100644 index 0000000..fad5b8f --- /dev/null +++ b/tests/hnb-test-ranap.c @@ -0,0 +1,86 @@ +#include <osmocom/core/msgb.h> +#include <osmocom/ranap/ranap_ies_defs.h> +#include <osmocom/ranap/iu_helpers.h> + +#include "hnb-test-layers.h" + +static const char *printstr(OCTET_STRING_t *s) +{ + return osmo_hexdump((const unsigned char*)s->buf, s->size); +} + +#define PP(octet_string_t) \ + printf(#octet_string_t " = %s\n",\ + printstr(&octet_string_t)) + +void hnb_test_rua_dt_handle_ranap(void *priv, struct ranap_message_s *ranap_msg) +{ + int len; + uint8_t *data; + RANAP_PermittedIntegrityProtectionAlgorithms_t *algs; + RANAP_IntegrityProtectionAlgorithm_t *first_alg; + struct hnb_test *hnb = priv; + + printf("rx ranap_msg->procedureCode %d\n", + ranap_msg->procedureCode); + + switch (ranap_msg->procedureCode) { + case RANAP_ProcedureCode_id_DirectTransfer: + printf("rx DirectTransfer: presence = %hx\n", + ranap_msg->msg.directTransferIEs.presenceMask); + PP(ranap_msg->msg.directTransferIEs.nas_pdu); + + len = ranap_msg->msg.directTransferIEs.nas_pdu.size; + data = ranap_msg->msg.directTransferIEs.nas_pdu.buf; + + hnb_test_nas_rx_dtap(hnb, data, len); + return; + + case RANAP_ProcedureCode_id_SecurityModeControl: + printf("rx SecurityModeControl: presence = %hx\n", + ranap_msg->msg.securityModeCommandIEs.presenceMask); + + /* Just pick the first available IP alg, don't care about + * encryption (yet?) */ + algs = &ranap_msg->msg.securityModeCommandIEs.integrityProtectionInformation.permittedAlgorithms; + if (algs->list.count < 1) { + printf("Security Mode Command: No permitted algorithms.\n"); + return; + } + first_alg = *algs->list.array; + + hnb_test_rx_secmode_cmd(hnb, *first_alg); + return; + + case RANAP_ProcedureCode_id_Iu_Release: + hnb_test_rx_iu_release(hnb); + return; + } +} + +void hnb_test_rua_cl_handle_ranap(void *priv, struct ranap_message_s *ranap_msg) +{ + char imsi[16]; + struct hnb_test *hnb = priv; + + printf("rx ranap_msg->procedureCode %d\n", + ranap_msg->procedureCode); + + switch (ranap_msg->procedureCode) { + case RANAP_ProcedureCode_id_Paging: + if (ranap_msg->msg.pagingIEs.permanentNAS_UE_ID.present == RANAP_PermanentNAS_UE_ID_PR_iMSI) { + ranap_bcd_decode(imsi, sizeof(imsi), + ranap_msg->msg.pagingIEs.permanentNAS_UE_ID.choice.iMSI.buf, + ranap_msg->msg.pagingIEs.permanentNAS_UE_ID.choice.iMSI.size); + } else imsi[0] = '\0'; + + printf("rx Paging: presence=%hx domain=%ld IMSI=%s\n", + ranap_msg->msg.pagingIEs.presenceMask, + ranap_msg->msg.pagingIEs.cN_DomainIndicator, + imsi + ); + + hnb_test_rx_paging(hnb, imsi); + return; + } +} diff --git a/tests/hnb-test-rua.c b/tests/hnb-test-rua.c new file mode 100644 index 0000000..0882515 --- /dev/null +++ b/tests/hnb-test-rua.c @@ -0,0 +1,41 @@ + +#include <asn1c/ANY.h> +#include <osmocom/ranap/ranap_common_cn.h> +#include <osmocom/rua/rua_ies_defs.h> + +#include "hnb-test-layers.h" + +void hnb_test_rua_dt_handle(struct hnb_test *hnb, ANY_t *in) +{ + RUA_DirectTransferIEs_t ies; + int rc; + + rc = rua_decode_directtransferies(&ies, in); + if (rc < 0) { + printf("failed to decode RUA DT IEs\n"); + return; + } + + rc = ranap_cn_rx_co(hnb_test_rua_dt_handle_ranap, hnb, ies.ranaP_Message.buf, ies.ranaP_Message.size); + + /* FIXME: what to do with the asn1c-allocated memory */ + rua_free_directtransferies(&ies); +} + +void hnb_test_rua_cl_handle(struct hnb_test *hnb, ANY_t *in) +{ + RUA_ConnectionlessTransferIEs_t ies; + int rc; + + rc = rua_decode_connectionlesstransferies(&ies, in); + if (rc < 0) { + printf("failed to decode RUA CL IEs\n"); + return; + } + + rc = ranap_cn_rx_cl(hnb_test_rua_cl_handle_ranap, hnb, ies.ranaP_Message.buf, ies.ranaP_Message.size); + + /* FIXME: what to do with the asn1c-allocated memory */ + rua_free_connectionlesstransferies(&ies); +} + diff --git a/tests/hnb-test.c b/tests/hnb-test.c new file mode 100644 index 0000000..de75186 --- /dev/null +++ b/tests/hnb-test.c @@ -0,0 +1,1018 @@ +/* Test HNB */ + +/* (C) 2015 by Daniel Willmann <dwillmann@sysmocom.de> + * (C) 2015 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <getopt.h> +#include <errno.h> +#include <signal.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/sctp.h> +#include <arpa/inet.h> + +#include <osmocom/core/application.h> +#include <osmocom/core/talloc.h> +#include <osmocom/core/select.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/socket.h> +#include <osmocom/core/msgb.h> +#include <osmocom/core/write_queue.h> +#include <osmocom/netif/stream.h> +#include <osmocom/gsm/tlv.h> +#include <osmocom/gsm/gsm48.h> + +#include <osmocom/vty/telnet_interface.h> +#include <osmocom/vty/logging.h> +#include <osmocom/vty/command.h> + +#include <osmocom/crypt/auth.h> + +#include "hnb-test.h" +#include "hnb-test-layers.h" +#include <osmocom/hnbap/hnbap_common.h> +#include <osmocom/hnbap/hnbap_ies_defs.h> +#include <osmocom/rua/rua_msg_factory.h> +#include "asn1helpers.h" +#include <osmocom/ranap/iu_helpers.h> +#include "test_common.h" + +#include <osmocom/ranap/ranap_msg_factory.h> + +#include <osmocom/rua/RUA_RUA-PDU.h> + +#include <osmocom/gsm/protocol/gsm_04_08.h> + +#include <osmocom/ranap/RANAP_ProcedureCode.h> +#include <osmocom/ranap/RANAP_Criticality.h> +#include <osmocom/ranap/RANAP_DirectTransfer.h> + +static void *tall_hnb_ctx; + +struct hnb_test g_hnb_test = { + .gw_addr = "127.0.0.1", + .gw_port = IUH_DEFAULT_SCTP_PORT, +}; + +struct msgb *rua_new_udt(struct msgb *inmsg); + +#if 0 +static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test) +{ + struct msgb *msg; + int rc; + uint32_t ctx_id; + + UEDe_Register_t dereg; + UEDe_RegisterIEs_t dereg_ies; + memset(&dereg_ies, 0, sizeof(dereg_ies)); + + asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id); + dereg_ies.cause.present = Cause_PR_radioNetwork; + dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost; + + memset(&dereg, 0, sizeof(dereg)); + rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies); + OSMO_ASSERT(rc == 0); + + msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register, + Criticality_ignore, + &asn_DEF_UEDe_Register, + &dereg); + + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg); + + msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; + + return osmo_wqueue_enqueue(&hnb_test->wqueue, msg); +} +#endif + +static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str) +{ + struct msgb *msg; + int rc, imsi_len; + + uint8_t imsi_buf[16]; + + HNBAP_UERegisterRequest_t request_out; + HNBAP_UERegisterRequestIEs_t request; + memset(&request, 0, sizeof(request)); + + request.uE_Identity.present = HNBAP_UE_Identity_PR_iMSI; + + imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str); + OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, (const char*)imsi_buf, imsi_len); + + request.registration_Cause = HNBAP_Registration_Cause_normal; + request.uE_Capabilities.access_stratum_release_indicator = HNBAP_Access_stratum_release_indicator_rel_6; + request.uE_Capabilities.csg_capability = HNBAP_CSG_Capability_not_csg_capable; + + memset(&request_out, 0, sizeof(request_out)); + rc = hnbap_encode_ueregisterrequesties(&request_out, &request); + OSMO_ASSERT(rc == 0); + + msg = hnbap_generate_initiating_message(HNBAP_ProcedureCode_id_UERegister, + HNBAP_Criticality_reject, + &asn_DEF_HNBAP_UERegisterRequest, + &request_out); + + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_UERegisterRequest, &request_out); + + msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; + + return osmo_wqueue_enqueue(&hnb_test->wqueue, msg); +} + +static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in) +{ + int rc; + HNBAP_HNBRegisterAcceptIEs_t accept; + + rc = hnbap_decode_hnbregisteraccepties(&accept, in); + if (rc < 0) { + } + + hnb->rnc_id = accept.rnc_id; + printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id); + + hnbap_free_hnbregisteraccepties(&accept); + return 0; +} + +static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in) +{ + int rc; + uint32_t ctx_id; + HNBAP_UERegisterAcceptIEs_t accept; + char imsi[16]; + + rc = hnbap_decode_ueregisteraccepties(&accept, in); + if (rc < 0) { + return rc; + } + + if (accept.uE_Identity.present != HNBAP_UE_Identity_PR_iMSI) { + printf("Wrong type in UE register accept\n"); + return -1; + } + + ctx_id = asn1bitstr_to_u24(&accept.context_ID); + + ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf, + accept.uE_Identity.choice.iMSI.size); + printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id); + + hnb->ctx_id = ctx_id; + hnbap_free_ueregisteraccepties(&accept); + + return 0; +} + +static struct msgb *gen_nas_id_resp() +{ + uint8_t id_resp[] = { + GSM48_PDISC_MM, + GSM48_MT_MM_ID_RESP, + /* IMEISV */ + 0x09, /* len */ + 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */ + 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */ + 0xf2, /* filler (1111) + last digit (0010) */ + }; + + return ranap_new_msg_dt(0, id_resp, sizeof(id_resp)); +} + +static struct msgb *gen_nas_tmsi_realloc_compl() +{ + uint8_t id_resp[] = { + GSM48_PDISC_MM, + GSM48_MT_MM_TMSI_REALL_COMPL, + }; + + return ranap_new_msg_dt(0, id_resp, sizeof(id_resp)); +} + +static struct msgb *gen_nas_auth_resp(uint8_t *sres) +{ + uint8_t id_resp[] = { + GSM48_PDISC_MM, + 0x80 | GSM48_MT_MM_AUTH_RESP, /* simulate sequence nr 2 */ + 0x61, 0xb5, 0x69, 0xf5 /* hardcoded SRES */ + }; + + memcpy(id_resp + 2, sres, 4); + + return ranap_new_msg_dt(0, id_resp, sizeof(id_resp)); +} + +static int hnb_test_tx_dt(struct hnb_test *hnb, struct msgb *txm) +{ + struct hnbtest_chan *chan; + struct msgb *rua; + + chan = hnb->cs.chan; + if (!chan) { + printf("hnb_test_nas_tx_tmsi_realloc_compl(): No CS channel established yet.\n"); + return -1; + } + + rua = rua_new_dt(chan->is_ps, chan->conn_id, txm); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + return 0; +} + +static struct tlv_parsed *parse_mm(struct gsm48_hdr *gh, int len) +{ + static struct tlv_parsed tp; + int parse_res; + + len -= (const char *)&gh->data[0] - (const char *)gh; + + OSMO_ASSERT(gsm48_hdr_pdisc(gh) == GSM48_PDISC_MM); + + parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0); + if (parse_res <= 0) { + uint8_t msg_type = gsm48_hdr_msg_type(gh); + printf("Error parsing MM message 0x%hhx: %d\n", msg_type, parse_res); + return NULL; + } + + return &tp; +} + +int hnb_test_nas_rx_lu_accept(struct gsm48_hdr *gh, int len, int *sent_tmsi) +{ + printf(" :D Location Update Accept :D\n"); + struct gsm48_loc_area_id *lai; + + lai = (struct gsm48_loc_area_id *)&gh->data[0]; + + struct osmo_location_area_id laid; + gsm48_decode_lai2(lai, &laid); + printf("LU: mcc %s mnc %s lac %hd\n", + osmo_mcc_name(laid.plmn.mcc), osmo_mnc_name(laid.plmn.mnc, laid.plmn.mnc_3_digits), + laid.lac); + + struct tlv_parsed tp; + int parse_res; + + len -= (const char *)&gh->data[0] - (const char *)gh; + parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0); + if (parse_res <= 0) { + printf("Error parsing Location Update Accept message: %d\n", parse_res); + return -1; + } + + if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) { + uint8_t type = TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)[0] & 0x0f; + if (type == GSM_MI_TYPE_TMSI) + *sent_tmsi = 1; + else *sent_tmsi = 0; + } + return 0; +} + +void hnb_test_nas_rx_mm_info(struct gsm48_hdr *gh, int len) +{ + printf(" :) MM Info :)\n"); + struct tlv_parsed *tp = parse_mm(gh, len); + if (!tp) + return; + + if (TLVP_PRESENT(tp, GSM48_IE_NAME_SHORT)) { + char name[128] = {0}; + gsm_7bit_decode_n(name, 127, + TLVP_VAL(tp, GSM48_IE_NAME_SHORT)+1, + (TLVP_LEN(tp, GSM48_IE_NAME_SHORT)-1)*8/7); + printf("Info: Short Network Name: %s\n", name); + } + + if (TLVP_PRESENT(tp, GSM48_IE_NAME_LONG)) { + char name[128] = {0}; + gsm_7bit_decode_n(name, 127, + TLVP_VAL(tp, GSM48_IE_NAME_LONG)+1, + (TLVP_LEN(tp, GSM48_IE_NAME_LONG)-1)*8/7); + printf("Info: Long Network Name: %s\n", name); + } +} + +static int hnb_test_nas_rx_auth_req(struct hnb_test *hnb, struct gsm48_hdr *gh, + int len) +{ + struct gsm48_auth_req *ar; + + len -= (const char *)&gh->data[0] - (const char *)gh; + + if (len < sizeof(*ar)) { + printf("GSM48 Auth Req does not fit.\n"); + return -1; + } + + printf(" :) Authentication Request :)\n"); + + ar = (struct gsm48_auth_req*) &gh->data[0]; + int seq = ar->key_seq; + + /* Generate SRES from *HARDCODED* Ki for Iuh testing */ + struct osmo_auth_vector vec; + /* Ki 000102030405060708090a0b0c0d0e0f */ + struct osmo_sub_auth_data auth = { + .type = OSMO_AUTH_TYPE_GSM, + .algo = OSMO_AUTH_ALG_COMP128v1, + .u.gsm.ki = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, + 0x0e, 0x0f + }, + }; + + memset(&vec, 0, sizeof(vec)); + osmo_auth_gen_vec(&vec, &auth, ar->rand); + + printf("seq %d rand %s", + seq, osmo_hexdump(ar->rand, sizeof(ar->rand))); + printf(" --> sres %s\n", + osmo_hexdump(vec.sres, 4)); + + return hnb_test_tx_dt(hnb, gen_nas_auth_resp(vec.sres)); +} + +void hnb_test_tx_iu_release_req(struct hnb_test *hnb) +{ + RANAP_Cause_t cause = { + .present = RANAP_Cause_PR_radioNetwork, + .choice.transmissionNetwork = RANAP_CauseRadioNetwork_release_due_to_UE_generated_signalling_connection_release, + }; + hnb_test_tx_dt(hnb, ranap_new_msg_iu_rel_req(&cause)); +} + +void hnb_test_tx_iu_release_compl(struct hnb_test *hnb) +{ + hnb_test_tx_dt(hnb, ranap_new_msg_iu_rel_compl()); +} + +static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct gsm48_hdr *gh, int len) +{ + struct hnbtest_chan *chan; + + chan = hnb->cs.chan; + if (!chan) { + printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n"); + return -1; + } + + OSMO_ASSERT(!chan->is_ps); + + uint8_t msg_type = gsm48_hdr_msg_type(gh); + int sent_tmsi; + + switch (msg_type) { + case GSM48_MT_MM_ID_REQ: + return hnb_test_tx_dt(hnb, gen_nas_id_resp()); + + case GSM48_MT_MM_LOC_UPD_ACCEPT: + if (hnb_test_nas_rx_lu_accept(gh, len, &sent_tmsi)) + return -1; + if (sent_tmsi) + return hnb_test_tx_dt(hnb, gen_nas_tmsi_realloc_compl()); + else + return 0; + + case GSM48_MT_MM_LOC_UPD_REJECT: + printf("Received Location Update Reject\n"); + return 0; + + case GSM48_MT_MM_INFO: + hnb_test_nas_rx_mm_info(gh, len); + hnb_test_tx_iu_release_req(hnb); + return 0; + + case GSM48_MT_MM_AUTH_REQ: + return hnb_test_nas_rx_auth_req(hnb, gh, len); + + default: + printf("04.08 message type not handled by hnb-test: 0x%x\n", + msg_type); + return 0; + } + +} + +void hnb_test_nas_rx_dtap(struct hnb_test *hnb, void *data, int len) +{ + int rc; + printf("got %d bytes: %s\n", len, osmo_hexdump(data, len)); + + // nas_pdu == '05 08 12' ==> IMEI Identity request + // '05 04 0d' ==> LU reject + + struct gsm48_hdr *gh = data; + if (len < sizeof(*gh)) { + printf("hnb_test_nas_rx_dtap(): NAS PDU is too short: %d. Ignoring.\n", + len); + return; + } + uint8_t pdisc = gsm48_hdr_pdisc(gh); + + switch (pdisc) { + case GSM48_PDISC_MM: + rc = hnb_test_nas_rx_mm(hnb, gh, len); + if (rc != 0) + printf("Error receiving MM message: %d\n", rc); + return; + default: + printf("04.08 discriminator not handled by hnb-test: %d\n", + pdisc); + return; + } +} + +void hnb_test_rx_secmode_cmd(struct hnb_test *hnb, long ip_alg) +{ + printf(" :) Security Mode Command :)\n"); + /* not caring about encryption yet, just pass 0 for No Encryption. */ + hnb_test_tx_dt(hnb, ranap_new_msg_sec_mod_compl(ip_alg, 0)); +} + +void hnb_test_rx_iu_release(struct hnb_test *hnb) +{ + hnb_test_tx_iu_release_compl(hnb); +} + +void hnb_test_rx_paging(struct hnb_test *hnb, const char *imsi) +{ + printf(" :) Paging Request for %s :)\n", imsi); + /* TODO reply */ +} + +int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg) +{ + HNBAP_HNBAP_PDU_t _pdu, *pdu = &_pdu; + asn_dec_rval_t dec_ret; + int rc; + + memset(pdu, 0, sizeof(*pdu)); + dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_HNBAP_PDU, (void **) &pdu, + msg->data, msgb_length(msg), 0, 0); + if (dec_ret.code != RC_OK) { + LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n"); + return -EINVAL; + } + + if (pdu->present != HNBAP_HNBAP_PDU_PR_successfulOutcome) { + printf("Unexpected HNBAP message received\n"); + } + + switch (pdu->choice.successfulOutcome.procedureCode) { + case HNBAP_ProcedureCode_id_HNBRegister: + /* Get HNB id and send UE Register request */ + rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value); + break; + case HNBAP_ProcedureCode_id_UERegister: + rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value); + break; + default: + rc = -ENOSPC; + break; + } + + return rc; +} + +extern void direct_transfer_nas_pdu_print(ANY_t *in); + +int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg) +{ + RUA_RUA_PDU_t _pdu, *pdu = &_pdu; + asn_dec_rval_t dec_ret; + + memset(pdu, 0, sizeof(*pdu)); + dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu, + msg->data, msgb_length(msg), 0, 0); + if (dec_ret.code != RC_OK) { + LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n"); + return -EINVAL; + } + + switch (pdu->present) { + case RUA_RUA_PDU_PR_successfulOutcome: + printf("RUA_RUA_PDU_PR_successfulOutcome\n"); + break; + case RUA_RUA_PDU_PR_initiatingMessage: + printf("RUA_RUA_PDU_PR_initiatingMessage\n"); + break; + case RUA_RUA_PDU_PR_NOTHING: + printf("RUA_RUA_PDU_PR_NOTHING\n"); + break; + case RUA_RUA_PDU_PR_unsuccessfulOutcome: + printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n"); + break; + default: + printf("Unexpected RUA message received\n"); + break; + } + + switch (pdu->choice.successfulOutcome.procedureCode) { + case RUA_ProcedureCode_id_ConnectionlessTransfer: + printf("RUA rx Connectionless Transfer\n"); + hnb_test_rua_cl_handle(hnb, &pdu->choice.successfulOutcome.value); + break; + case RUA_ProcedureCode_id_Connect: + printf("RUA rx Connect\n"); + break; + case RUA_ProcedureCode_id_DirectTransfer: + printf("RUA rx DirectTransfer\n"); + hnb_test_rua_dt_handle(hnb, &pdu->choice.successfulOutcome.value); + break; + case RUA_ProcedureCode_id_Disconnect: + printf("RUA rx Disconnect\n"); + break; + case RUA_ProcedureCode_id_ErrorIndication: + printf("RUA rx ErrorIndication\n"); + break; + case RUA_ProcedureCode_id_privateMessage: + printf("RUA rx privateMessage\n"); + break; + default: + printf("RUA rx unknown message\n"); + break; + } + + return 0; +} + +static int hnb_read_cb(struct osmo_fd *fd) +{ + struct hnb_test *hnb_test = fd->data; + struct sctp_sndrcvinfo sinfo; + struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); + int flags = 0; + int rc; + + if (!msg) + return -ENOMEM; + + rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), + NULL, NULL, &sinfo, &flags); + if (rc < 0) { + LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); + /* FIXME: clean up after disappeared HNB */ + close(fd->fd); + osmo_fd_unregister(fd); + goto free_ret; + } else if (rc == 0) { + LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n"); + close(fd->fd); + osmo_fd_unregister(fd); + fd->fd = -1; + rc = -1; + goto free_ret; + } else { + msgb_put(msg, rc); + } + + if (flags & MSG_NOTIFICATION) { + LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n"); + rc = 0; + goto free_ret; + } + + sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); + + switch (sinfo.sinfo_ppid) { + case IUH_PPI_HNBAP: + printf("HNBAP message received\n"); + rc = hnb_test_hnbap_rx(hnb_test, msg); + break; + case IUH_PPI_RUA: + printf("RUA message received\n"); + rc = hnb_test_rua_rx(hnb_test, msg); + break; + case IUH_PPI_SABP: + case IUH_PPI_RNA: + case IUH_PPI_PUA: + LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", + sinfo.sinfo_ppid); + rc = 0; + break; + default: + LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", + sinfo.sinfo_ppid); + rc = 0; + break; + } + +free_ret: + msgb_free(msg); + return rc; +} + +static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) +{ + /* struct hnb_test *ctx = fd->data; */ + struct sctp_sndrcvinfo sinfo = { + .sinfo_ppid = htonl(msgb_sctp_ppid(msg)), + .sinfo_stream = 0, + }; + int rc; + + printf("Sending: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); + rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), + &sinfo, 0); + /* we don't need to msgb_free(), write_queue does this for us */ + return rc; +} + +static void hnb_send_register_req(struct hnb_test *hnb_test) +{ + HNBAP_HNBRegisterRequest_t request_out; + struct msgb *msg; + int rc; + uint16_t lac, sac; + uint8_t rac; + uint32_t cid; + uint8_t plmn[] = {0x09, 0xf1, 0x99}; + char identity[50] = "ATestHNB@"; + + HNBAP_HNBRegisterRequestIEs_t request; + memset(&request, 0, sizeof(request)); + + lac = 0xc0fe; + sac = 0xabab; + rac = 0x42; + cid = 0xadceaab; + + asn1_u16_to_str(&request.lac, &lac, lac); + asn1_u16_to_str(&request.sac, &sac, sac); + asn1_u8_to_str(&request.rac, &rac, rac); + asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid); + + request.hnB_Identity.hNB_Identity_Info.buf = (uint8_t*) identity; + request.hnB_Identity.hNB_Identity_Info.size = strlen(identity); + + request.plmNidentity.buf = plmn; + request.plmNidentity.size = 3; + + + + memset(&request_out, 0, sizeof(request_out)); + rc = hnbap_encode_hnbregisterrequesties(&request_out, &request); + if (rc < 0) { + printf("Could not encode HNB register request IEs\n"); + } + + msg = hnbap_generate_initiating_message(HNBAP_ProcedureCode_id_HNBRegister, + HNBAP_Criticality_reject, + &asn_DEF_HNBAP_HNBRegisterRequest, + &request_out); + + + msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; + + osmo_wqueue_enqueue(&hnb_test->wqueue, msg); +} + +static void hnb_send_deregister_req(struct hnb_test *hnb_test) +{ + struct msgb *msg; + int rc; + + HNBAP_HNBDe_RegisterIEs_t request; + memset(&request, 0, sizeof(request)); + + request.cause.present = HNBAP_Cause_PR_misc; + request.cause.choice.misc = HNBAP_CauseMisc_o_and_m_intervention; + + HNBAP_HNBDe_Register_t request_out; + memset(&request_out, 0, sizeof(request_out)); + rc = hnbap_encode_hnbde_registeries(&request_out, &request); + if (rc < 0) { + printf("Could not encode HNB deregister request IEs\n"); + } + + msg = hnbap_generate_initiating_message(HNBAP_ProcedureCode_id_HNBDe_Register, + HNBAP_Criticality_reject, + &asn_DEF_HNBAP_HNBDe_Register, + &request_out); + + msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; + + osmo_wqueue_enqueue(&hnb_test->wqueue, msg); +} + + +static const struct log_info_cat log_cat[] = { + [DMAIN] = { + .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1, + .color = "", + .description = "Main program", + }, + [DHNBAP] = { + .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "Home Node B Application Part", + }, +}; + +static const struct log_info hnb_test_log_info = { + .cat = log_cat, + .num_cat = ARRAY_SIZE(log_cat), +}; + +static struct vty_app_info vty_info = { + .name = "OsmoHNB-Test", + .version = "0", +}; + +static int sctp_sock_init(int fd) +{ + struct sctp_event_subscribe event; + int rc; + + /* subscribe for all events */ + memset((uint8_t *)&event, 1, sizeof(event)); + rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, + &event, sizeof(event)); + + return rc; +} + +#define HNBAP_STR "HNBAP related commands\n" +#define HNB_STR "HomeNodeB commands\n" +#define UE_STR "User Equipment commands\n" +#define RANAP_STR "RANAP related commands\n" +#define CSPS_STR "Circuit Switched\n" "Packet Switched\n" + +DEFUN(hnb_register, hnb_register_cmd, + "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST") +{ + hnb_send_register_req(&g_hnb_test); + + return CMD_SUCCESS; +} + +DEFUN(hnb_deregister, hnb_deregister_cmd, + "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST") +{ + hnb_send_deregister_req(&g_hnb_test); + + return CMD_SUCCESS; +} + +DEFUN(ue_register, ue_register_cmd, + "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST") +{ + hnb_test_ue_register_tx(&g_hnb_test, argv[0]); + + return CMD_SUCCESS; +} + +DEFUN(asn_dbg, asn_dbg_cmd, + "asn-debug (1|0)", "Enable or disable libasn1c debugging") +{ + asn_debug = atoi(argv[0]); + + return CMD_SUCCESS; +} + +DEFUN(ranap_reset, ranap_reset_cmd, + "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR) +{ + int is_ps = 0; + struct msgb *msg, *rua; + + RANAP_Cause_t cause = { + .present = RANAP_Cause_PR_transmissionNetwork, + .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure, + }; + + if (!strcmp(argv[0], "ps")) + is_ps = 1; + + msg = ranap_new_msg_reset(is_ps, &cause); + rua = rua_new_udt(msg); + //msgb_free(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + return CMD_SUCCESS; +} + + +enum my_vty_nodes { + CHAN_NODE = _LAST_OSMOVTY_NODE, +}; + +static struct cmd_node chan_node = { + CHAN_NODE, + "%s(chan)> ", + 1, +}; + + +static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi) +{ + uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST, + 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57, + /* len, IMSI/type, IMSI-------------------------------- */ + 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98, + 0x33, 0x03, 0x57, 0x18 , 0xb2 }; + uint8_t plmn_id[] = { 0x09, 0x01, 0x99 }; + RANAP_GlobalRNC_ID_t rnc_id = { + .rNC_ID = 23, + .pLMNidentity.buf = plmn_id, + .pLMNidentity.size = sizeof(plmn_id), + }; + + /* FIXME: patch imsi */ + /* Note: the Mobile Identitiy IE's IMSI data has the identity type and + * an even/odd indicator bit encoded in the first octet. So the first + * octet looks like this: + * + * 8 7 6 5 | 4 | 3 2 1 + * IMSI-digit | even/odd | type + * + * followed by the remaining IMSI digits. + * If digit count is even (bit 4 == 0), that first high-nibble is 0xf. + * (derived from Iu pcap Location Update Request msg and TS 25.413) + * + * TODO I'm only 90% sure about this + */ + + return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu)); +} + +DEFUN(chan, chan_cmd, + "channel (cs|ps) lu imsi IMSI", + "Open a new Signalling Connection\n" + "To Circuit-Switched CN\n" + "To Packet-Switched CN\n" + "Performing a Location Update\n" + ) +{ + struct hnbtest_chan *chan; + struct msgb *msg, *rua; + static uint16_t conn_id = 42; + + chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan); + if (!strcmp(argv[0], "ps")) + chan->is_ps = 1; + chan->imsi = talloc_strdup(chan, argv[1]); + chan->conn_id = conn_id; + conn_id++; + + msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi); + rua = rua_new_conn(chan->is_ps, chan->conn_id, msg); + + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + vty->index = chan; + vty->node = CHAN_NODE; + + if (!chan->is_ps) + g_hnb_test.cs.chan = chan; + + + return CMD_SUCCESS; +} + +static void hnbtest_vty_init(void) +{ + install_element_ve(&asn_dbg_cmd); + install_element_ve(&hnb_register_cmd); + install_element_ve(&hnb_deregister_cmd); + install_element_ve(&ue_register_cmd); + install_element_ve(&ranap_reset_cmd); + install_element_ve(&chan_cmd); + + install_node(&chan_node, NULL); +} + +static void handle_options(int argc, char **argv) +{ + while (1) { + int idx = 0, c; + static const struct option long_options[] = { + { "ues", 1, 0, 'u' }, + { "gw-addr", 1, 0, 'g' }, + { 0, 0, 0, 0 }, + }; + + c = getopt_long(argc, argv, "u:g:", long_options, &idx); + + if (c == -1) + break; + + switch (c) { + case 'u': + g_hnb_test.ues = atoi(optarg); + break; + case 'g': + g_hnb_test.gw_addr = optarg; + break; + } + } +} + +int main(int argc, char **argv) +{ + int rc; + + test_common_init(); + + tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); + + osmo_init_logging2(tall_hnb_ctx, &hnb_test_log_info); + + vty_init(&vty_info); + hnbtest_vty_init(); + + rc = telnet_init_default(NULL, NULL, 2324); + if (rc < 0) { + perror("Error binding VTY port"); + exit(1); + } + + handle_options(argc, argv); + + osmo_wqueue_init(&g_hnb_test.wqueue, 16); + g_hnb_test.wqueue.bfd.data = &g_hnb_test; + g_hnb_test.wqueue.read_cb = hnb_read_cb; + g_hnb_test.wqueue.write_cb = hnb_write_cb; + + rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM, + IPPROTO_SCTP, g_hnb_test.gw_addr, + g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT); + if (rc < 0) { + perror("Error connecting to Iuh port"); + exit(1); + } + sctp_sock_init(g_hnb_test.wqueue.bfd.fd); + +#if 0 + /* some hard-coded message generation. Doesn't make sense from + * a protocol point of view but enables to look at the encoded + * results in wireshark for manual verification */ + { + struct msgb *msg, *rua; + const uint8_t nas[] = { 0, 1, 2, 3 }; + const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + + msg = ranap_new_msg_dt(0, nas, sizeof(nas)); + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + msg = ranap_new_msg_sec_mod_cmd(ik, ik, RANAP_KeyStatus_new); + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + msg = ranap_new_msg_iu_rel_cmd() + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0); + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020); + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + + msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344); + rua = rua_new_udt(msg); + osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); + } +#endif + + while (1) { + rc = osmo_select_main(0); + if (rc < 0) + exit(3); + } + + /* not reached */ + exit(0); +} diff --git a/tests/hnb-test.h b/tests/hnb-test.h new file mode 100644 index 0000000..02efce0 --- /dev/null +++ b/tests/hnb-test.h @@ -0,0 +1,77 @@ +#pragma once + +#include <osmocom/core/select.h> +#include <osmocom/core/linuxlist.h> +#include <osmocom/core/write_queue.h> + +#define DEBUG +#include <osmocom/core/logging.h> + +#include "test_common.h" + + +/* 25.467 Section 7.1 */ +#define IUH_DEFAULT_SCTP_PORT 29169 +#define RNA_DEFAULT_SCTP_PORT 25471 + +#define IUH_PPI_RUA 19 +#define IUH_PPI_HNBAP 20 +#define IUH_PPI_SABP 31 +#define IUH_PPI_RNA 42 +#define IUH_PPI_PUA 55 + +#define IUH_MSGB_SIZE 2048 + +struct umts_cell_id { + uint16_t mcc; /*!< Mobile Country Code */ + uint16_t mnc; /*!< Mobile Network Code */ + uint16_t lac; /*!< Locaton Area Code */ + uint16_t rac; /*!< Routing Area Code */ + uint16_t sac; /*!< Service Area Code */ + uint32_t cid; /*!< Cell ID */ +}; + +struct ue_context { + /*! Entry in the HNB-global list of UE */ + struct llist_head list; + /*! Unique Context ID for this UE */ + uint32_t context_id; + char imsi[16+1]; +}; + +struct hnbtest_chan { + int is_ps; + uint32_t conn_id; + char *imsi; +}; + +struct hnb_test { + const char *gw_addr; + uint16_t gw_port; + /*! SCTP listen socket for incoming connections */ + struct osmo_fd conn_fd; + + /*! SCTP socket + write queue for Iuh to this specific HNB */ + struct osmo_wqueue wqueue; + /*! copied from HNB-Identity-Info IE */ + char identity_info[256]; + /*! copied from Cell Identity IE */ + struct umts_cell_id id; + + /*! SCTP stream ID for HNBAP */ + uint16_t hnbap_stream; + /*! SCTP stream ID for RUA */ + uint16_t rua_stream; + + uint16_t rnc_id; + + uint32_t ctx_id; + + int ues; + + struct { + struct hnbtest_chan *chan; + } cs; +}; + +extern struct hnb_test g_hnb_test; diff --git a/tests/test-helpers.c b/tests/test-helpers.c new file mode 100644 index 0000000..42e8b26 --- /dev/null +++ b/tests/test-helpers.c @@ -0,0 +1,427 @@ +/* some humble start of unit testing */ + +/* (C) 2015 by Harald Welte <laforge@gnumonks.org> + * All Rights Reserved + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include "test_common.h" + +#include <osmocom/ranap/iu_helpers.h> +#include <osmocom/ranap/ranap_common.h> +#include "asn1helpers.h" + +#include <assert.h> +#define ASSERT(x) assert(x) + +#include <osmocom/core/utils.h> +#include <osmocom/core/socket.h> +#include <osmocom/core/sockaddr_str.h> +#include <osmocom/gsm/gsm48.h> + +#include <osmocom/ranap/RANAP_LAI.h> + +extern void *talloc_asn1_ctx; + +/* use odd number of digits */ +const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 }; +const char imsi_decoded[] = "012345678"; + +void test_iu_helpers(void) +{ + char outstr[32]; + uint8_t outbuf[16]; + int rc; + + printf("Testing Iu helper functions\n"); + + printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded, + sizeof(imsi_encoded))); + rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded, + sizeof(imsi_encoded)); + ASSERT(rc >= 0); + printf("decoded: %s\n", outstr); + ASSERT(!strcmp(outstr, imsi_decoded)); + + rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded); + ASSERT(rc >= 0); + printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc)); + ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded))); +} + +const uint32_t val1 = 0xdeadbeef; + +const OCTET_STRING_t text1 = { + .buf = (uint8_t *) "0123456789012345", + .size = 16, +}; + +const OCTET_STRING_t text2 = { + .buf = (uint8_t *) "01234567890123456789012345678901234567890", + .size = 40, +}; + +void test_asn1_helpers(void) +{ + int rc; + + void *buffer; + BIT_STRING_t enc; + uint32_t res, tmpval; + char text[32]; + + printf("Testing asn.1 helper functions\n"); + + printf("Encoding 0x%x to asn.1 bitstring\n", val1); + asn1_u32_to_bitstring(&enc, &tmpval, val1); + + ASSERT(enc.size == sizeof(uint32_t)); + ASSERT(enc.bits_unused == 0); + + rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer); + printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc)); + + res = asn1bitstr_to_u32(&enc); + + printf("Decoding back to uint32_t: 0x%x\n", res); + ASSERT(res == val1); + + printf("Encoding 0x%x to 24-bit asn.1 bitstring\n", val1); + asn1_u24_to_bitstring(&enc, &tmpval, val1); + + ASSERT(enc.size == 24/8); + ASSERT(enc.bits_unused == 0); + + talloc_free(buffer); + + rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer); + printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc)); + + rc = asn1_strncpy(text, &text1, sizeof(text)); + printf("Decoding string from asn.1: %s\n", text); + + ASSERT(rc == 16); + ASSERT(!strcmp(text, (char *)text1.buf)); + + rc = asn1_strncpy(text, &text2, sizeof(text)); + printf("Decoding large string from asn1: %s\n", text); + ASSERT(rc == 31); + + talloc_free(buffer); +} + +void test_ranap_common(void) +{ + uint8_t plmnid_buf[] = { 0x21, 0xf3, 0x54 }; + uint8_t lac_buf[] = { 0xab, 0xcd }; + + struct gprs_ra_id ra_id = {0}; + + int rc; + + RANAP_LAI_t lai = { + .pLMNidentity = { + .buf = plmnid_buf, + .size = 3 + }, + .lAC = { + .buf = lac_buf, + .size = 2 + } + }; + + printf("Testing ranap common functions\n"); + + printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, + lai.lAC.size)); + + rc = ranap_parse_lai(&ra_id, &lai); + printf(" rc == %d\n", rc); + OSMO_ASSERT(rc == 0); + printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc); + OSMO_ASSERT(ra_id.mcc == 123); + OSMO_ASSERT(ra_id.mnc == 45); + printf(" lac == 0x%x\n", ra_id.lac); + OSMO_ASSERT(ra_id.lac == 0xabcd); + + + /* three digit MNC */ + uint8_t plmnid_buf_mnc3[] = { 0x21, 0x43, 0x65 }; + lai.pLMNidentity.buf = plmnid_buf_mnc3; + + printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, + lai.lAC.size)); + + rc = ranap_parse_lai(&ra_id, &lai); + printf(" rc == %d\n", rc); + OSMO_ASSERT(rc == 0); + printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc); + OSMO_ASSERT(ra_id.mcc == 123); + OSMO_ASSERT(ra_id.mnc == 456); + printf(" lac == 0x%x\n", ra_id.lac); + OSMO_ASSERT(ra_id.lac == 0xabcd); + + + /* wrong PLMN-Id size */ + lai.pLMNidentity.size = 2; + + printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, + lai.lAC.size)); + + rc = ranap_parse_lai(&ra_id, &lai); + printf(" rc == %d\n", rc); + OSMO_ASSERT(rc == -1); + + + /* wrong LAC size */ + lai.pLMNidentity.size = 3; + lai.lAC.size = 1; + + printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, + lai.lAC.size)); + + rc = ranap_parse_lai(&ra_id, &lai); + printf(" rc == %d\n", rc); + OSMO_ASSERT(rc == -1); +} + +void test_ranap_new_transp_info_rtp(void) +{ + RANAP_TransportLayerInformation_t *tli; + + printf("Testing function ranap_new_transp_info_rtp()\n"); + + struct osmo_sockaddr addr; + + addr.u.sin.sin_family = AF_INET; + addr.u.sin.sin_port = htons(0x1122); + inet_pton(AF_INET, "1.2.3.4", &addr.u.sin.sin_addr); + + printf(" ipv4, x213_nsap\n"); + tli = ranap_new_transp_info_rtp(&addr, true); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" bindingID = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.bindingID.buf, + tli->iuTransportAssociation.choice.bindingID.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + printf(" ipv4\n"); + tli = ranap_new_transp_info_rtp(&addr, false); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" bindingID = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.bindingID.buf, + tli->iuTransportAssociation.choice.bindingID.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + addr.u.sin.sin_family = AF_INET6; + addr.u.sin.sin_port = htons(0x1122); + inet_pton(AF_INET6, "f11f:f22f:f33f:f44f:f55f:f66f:f77f:f88f", &addr.u.sin6.sin6_addr); + + printf(" ipv6, x213_nsap\n"); + tli = ranap_new_transp_info_rtp(&addr, true); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" bindingID = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.bindingID.buf, + tli->iuTransportAssociation.choice.bindingID.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + printf(" ipv6\n"); + tli = ranap_new_transp_info_rtp(&addr, false); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" bindingID = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.bindingID.buf, + tli->iuTransportAssociation.choice.bindingID.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + addr.u.sin.sin_family = AF_X25; + printf(" unsupported address family\n"); + tli = ranap_new_transp_info_rtp(&addr, false); + OSMO_ASSERT(tli == NULL); +} + +void test_ranap_new_transp_info_gtp(void) +{ + RANAP_TransportLayerInformation_t *tli; + + printf("Testing function ranap_new_transp_info_gtp()\n"); + + struct osmo_sockaddr addr; + + addr.u.sin.sin_family = AF_INET; + inet_pton(AF_INET, "1.2.3.4", &addr.u.sin.sin_addr); + + printf(" ipv4, x213_nsap\n"); + tli = ranap_new_transp_info_gtp(&addr, 0x11223344, true); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" gTP_TEI = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.gTP_TEI.buf, + tli->iuTransportAssociation.choice.gTP_TEI.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + printf(" ipv4\n"); + tli = ranap_new_transp_info_gtp(&addr, 0x11223344, false); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" gTP_TEI = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.gTP_TEI.buf, + tli->iuTransportAssociation.choice.gTP_TEI.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + addr.u.sin.sin_family = AF_INET6; + inet_pton(AF_INET6, "f11f:f22f:f33f:f44f:f55f:f66f:f77f:f88f", &addr.u.sin6.sin6_addr); + + printf(" ipv6, x213_nsap\n"); + tli = ranap_new_transp_info_gtp(&addr, 0x11223344, true); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" gTP_TEI = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.gTP_TEI.buf, + tli->iuTransportAssociation.choice.gTP_TEI.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + printf(" ipv6\n"); + tli = ranap_new_transp_info_gtp(&addr, 0x11223344, false); + printf(" transportLayerAddress = %s\n", + osmo_hexdump_nospc(tli->transportLayerAddress.buf, tli->transportLayerAddress.size)); + printf(" gTP_TEI = %s\n", + osmo_hexdump_nospc(tli->iuTransportAssociation.choice.gTP_TEI.buf, + tli->iuTransportAssociation.choice.gTP_TEI.size)); + ASN_STRUCT_FREE(asn_DEF_RANAP_TransportLayerInformation, tli); + + addr.u.sin.sin_family = AF_X25; + printf(" unsupported address family\n"); + tli = ranap_new_transp_info_gtp(&addr, 0x11223344, false); + OSMO_ASSERT(tli == NULL); +} + +void test_ranap_transp_layer_addr_decode(void) +{ + int rc; + char addr[100]; + RANAP_TransportLayerAddress_t trasp_layer_addr = { 0 }; + uint8_t encoded_addr_x213_nsap[] = { + 0x35, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 }; + uint8_t encoded_addr[] = { 0x01, 0x02, 0x03, 0x04 }; + + printf("Testing function ranap_transp_layer_addr_decode()\n"); + + printf(" ipv4, x213_nsap\n"); + trasp_layer_addr.buf = encoded_addr_x213_nsap; + trasp_layer_addr.size = sizeof(encoded_addr_x213_nsap); + rc = ranap_transp_layer_addr_decode(addr, sizeof(addr), &trasp_layer_addr); + printf(" addr=%s\n", addr); + OSMO_ASSERT(rc == 0); + + printf(" ipv4\n"); + trasp_layer_addr.buf = encoded_addr; + trasp_layer_addr.size = sizeof(encoded_addr); + rc = ranap_transp_layer_addr_decode(addr, sizeof(addr), &trasp_layer_addr); + printf(" addr=%s\n", addr); + OSMO_ASSERT(rc == 0); +} + +void test_ranap_transp_layer_addr_decode2(void) +{ + int rc; + struct osmo_sockaddr addr; + struct osmo_sockaddr_str sockaddr_str; + bool uses_x213_nsap; + RANAP_TransportLayerAddress_t trasp_layer_addr = { 0 }; + uint8_t encoded_ipv4_addr_x213_nsap[] = { + 0x35, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 }; + uint8_t encoded_ipv4_addr[] = { 0x01, 0x02, 0x03, 0x04 }; + uint8_t encoded_ipv6_addr_x213_nsap[] = { + 0x35, 0x00, 0x00, 0xf1, 0x1f, 0xf2, 0x2f, 0xf3, + 0x3f, 0xf4, 0x4f, 0xf5, 0x5f, 0xf6, 0x6f, 0xf7, + 0x7f, 0xf8, 0x8f, 0x00 }; + uint8_t encoded_ipv6_addr[] = { + 0xf1, 0x1f, 0xf2, 0x2f, 0xf3, 0x3f, 0xf4, 0x4f, + 0xf5, 0x5f, 0xf6, 0x6f, 0xf7, 0x7f, 0xf8, 0x8f }; + + printf("Testing function ranap_transp_layer_addr_decode2()\n"); + + printf(" ipv4, x213_nsap\n"); + trasp_layer_addr.buf = encoded_ipv4_addr_x213_nsap; + trasp_layer_addr.size = sizeof(encoded_ipv4_addr_x213_nsap); + rc = ranap_transp_layer_addr_decode2(&addr, &uses_x213_nsap, &trasp_layer_addr); + OSMO_ASSERT(rc == 0); + memset(&sockaddr_str, 0, sizeof(sockaddr_str)); + osmo_sockaddr_str_from_sockaddr_in(&sockaddr_str, &addr.u.sin); + printf(" addr=%s, af=%d, x213_nsap=%u\n", sockaddr_str.ip, sockaddr_str.af, uses_x213_nsap); + + printf(" ipv4\n"); + trasp_layer_addr.buf = encoded_ipv4_addr; + trasp_layer_addr.size = sizeof(encoded_ipv4_addr); + rc = ranap_transp_layer_addr_decode2(&addr, &uses_x213_nsap, &trasp_layer_addr); + OSMO_ASSERT(rc == 0); + memset(&sockaddr_str, 0, sizeof(sockaddr_str)); + osmo_sockaddr_str_from_sockaddr_in(&sockaddr_str, &addr.u.sin); + printf(" addr=%s, af=%d, x213_nsap=%u\n", sockaddr_str.ip, sockaddr_str.af, uses_x213_nsap); + + printf(" ipv6, x213_nsap\n"); + trasp_layer_addr.buf = encoded_ipv6_addr_x213_nsap; + trasp_layer_addr.size = sizeof(encoded_ipv6_addr_x213_nsap); + rc = ranap_transp_layer_addr_decode2(&addr, &uses_x213_nsap, &trasp_layer_addr); + OSMO_ASSERT(rc == 0); + memset(&sockaddr_str, 0, sizeof(sockaddr_str)); + osmo_sockaddr_str_from_sockaddr_in6(&sockaddr_str, &addr.u.sin6); + printf(" addr=%s, af=%d, x213_nsap=%u\n", sockaddr_str.ip, sockaddr_str.af, uses_x213_nsap); + + printf(" ipv6\n"); + trasp_layer_addr.buf = encoded_ipv6_addr; + trasp_layer_addr.size = sizeof(encoded_ipv6_addr); + rc = ranap_transp_layer_addr_decode2(&addr, &uses_x213_nsap, &trasp_layer_addr); + OSMO_ASSERT(rc == 0); + memset(&sockaddr_str, 0, sizeof(sockaddr_str)); + osmo_sockaddr_str_from_sockaddr_in6(&sockaddr_str, &addr.u.sin6); + printf(" addr=%s, af=%d, x213_nsap=%u\n", sockaddr_str.ip, sockaddr_str.af, uses_x213_nsap); +} + +int main(int argc, char **argv) +{ + asn1_xer_print = 0; + + test_common_init(); + ranap_set_log_area(DRANAP); + + test_iu_helpers(); + test_asn1_helpers(); + test_ranap_common(); + test_ranap_new_transp_info_gtp(); + test_ranap_new_transp_info_rtp(); + test_ranap_transp_layer_addr_decode(); + test_ranap_transp_layer_addr_decode2(); + + test_common_cleanup(); + return 0; +} diff --git a/tests/test-helpers.err b/tests/test-helpers.err new file mode 100644 index 0000000..891094a --- /dev/null +++ b/tests/test-helpers.err @@ -0,0 +1,2 @@ +Invalid PLMN Identity size: should be 3, is 2 +Invalid LAC size: should be 2, is 1 diff --git a/tests/test-helpers.ok b/tests/test-helpers.ok new file mode 100644 index 0000000..0626413 --- /dev/null +++ b/tests/test-helpers.ok @@ -0,0 +1,67 @@ +Testing Iu helper functions +pre-encoded: 10325476f8 +decoded: 012345678 +re-encoded: 10325476f8 +Testing asn.1 helper functions +Encoding 0xdeadbeef to asn.1 bitstring +Encoded: 20deadbeef +Decoding back to uint32_t: 0xdeadbeef +Encoding 0xdeadbeef to 24-bit asn.1 bitstring +Encoded: 18adbeef +Decoding string from asn.1: 0123456789012345 +Decoding large string from asn1: 0123456789012345678901234567890 +Testing ranap common functions +PLMN-Id [ 21 f3 54 ], LAC [ ab cd ] + rc == 0 + mcc == 123 mnc == 45 + lac == 0xabcd +PLMN-Id [ 21 43 65 ], LAC [ ab cd ] + rc == 0 + mcc == 123 mnc == 456 + lac == 0xabcd +PLMN-Id [ 21 43 ], LAC [ ab cd ] + rc == -1 +PLMN-Id [ 21 43 65 ], LAC [ ab ] + rc == -1 +Testing function ranap_new_transp_info_gtp() + ipv4, x213_nsap + transportLayerAddress = 3500010102030400000000000000000000000000 + gTP_TEI = 11223344 + ipv4 + transportLayerAddress = 01020304 + gTP_TEI = 11223344 + ipv6, x213_nsap + transportLayerAddress = 350000f11ff22ff33ff44ff55ff66ff77ff88f00 + gTP_TEI = 11223344 + ipv6 + transportLayerAddress = f11ff22ff33ff44ff55ff66ff77ff88f + gTP_TEI = 11223344 + unsupported address family +Testing function ranap_new_transp_info_rtp() + ipv4, x213_nsap + transportLayerAddress = 3500010102030400000000000000000000000000 + bindingID = 11220000 + ipv4 + transportLayerAddress = 01020304 + bindingID = 11220000 + ipv6, x213_nsap + transportLayerAddress = 350000f11ff22ff33ff44ff55ff66ff77ff88f00 + bindingID = 11220000 + ipv6 + transportLayerAddress = f11ff22ff33ff44ff55ff66ff77ff88f + bindingID = 11220000 + unsupported address family +Testing function ranap_transp_layer_addr_decode() + ipv4, x213_nsap + addr=1.2.3.4 + ipv4 + addr=1.2.3.4 +Testing function ranap_transp_layer_addr_decode2() + ipv4, x213_nsap + addr=1.2.3.4, af=2, x213_nsap=1 + ipv4 + addr=1.2.3.4, af=2, x213_nsap=0 + ipv6, x213_nsap + addr=f11f:f22f:f33f:f44f:f55f:f66f:f77f:f88f, af=10, x213_nsap=1 + ipv6 + addr=f11f:f22f:f33f:f44f:f55f:f66f:f77f:f88f, af=10, x213_nsap=0 diff --git a/tests/test-hnbap.c b/tests/test-hnbap.c new file mode 100644 index 0000000..835a331 --- /dev/null +++ b/tests/test-hnbap.c @@ -0,0 +1,177 @@ +/* Test de-/encoding of HBNAP messages */ + +/* (C) 2015 by Daniel Willmann <dwillmann@sysmocom.de> + * (C) 2015 by sysmocom s.f.m.c GmbH + * All Rights Reserved + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include <osmocom/ranap/iu_helpers.h> +#include "asn1helpers.h" + +#include <osmocom/hnbap/hnbap_common.h> +#include <osmocom/hnbap/hnbap_ies_defs.h> +#include <osmocom/ranap/ranap_common.h> +#include "test_common.h" + +#include <assert.h> +#define ASSERT(x) assert(x) + +#include <osmocom/core/utils.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/application.h> + +static const unsigned char hnbap_reg_req[] = { +0x00, 0x01, +0x00, 0x57, 0x00, 0x00, 0x07, 0x00, 0x03, 0x00, +0x15, 0x04, 0x80, 0x31, 0x30, 0x30, 0x30, 0x35, +0x42, 0x39, 0x2d, 0x30, 0x30, 0x31, 0x30, 0x39, +0x34, 0x32, 0x30, 0x35, 0x30, 0x40, 0x00, 0x08, +0x00, 0x17, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, +0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x11, 0x00, 0x05, 0x00, 0xc0, 0xa8, 0x00, +0x32, 0x00, 0x09, 0x00, 0x03, 0x09, 0xf1, 0x99, +0x00, 0x0b, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, +0x00, 0x06, 0x00, 0x02, 0x40, 0x20, 0x00, 0x07, +0x00, 0x01, 0x64, 0x00, 0x0a, 0x00, 0x02, 0x00, +0x01 +}; + +static const unsigned char hnbap_ue_reg_req[] = { +0x00, 0x03, +0x00, 0x1a, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, +0x09, 0x0a, 0x62, 0x02, 0x11, 0x32, 0x54, 0x76, +0x98, 0xf0, 0x00, 0x0c, 0x40, 0x01, 0x40, 0x00, +0x0d, 0x00, 0x01, 0x0d +}; + +static const unsigned char hnbap_ue_reg_acc[] = { +0x20, 0x03, +0x00, 0x17, 0x00, 0x00, 0x02, 0x00, 0x05, 0x00, +0x09, 0x0a, 0x62, 0x02, 0x11, 0x32, 0x54, 0x76, +0x98, 0xf0, 0x00, 0x04, 0x00, 0x03, 0x17, 0x00, +0x00 +}; + +void test_asn1_decoding(void) +{ + int rc; + + HNBAP_HNBAP_PDU_t _pdu, *pdu = &_pdu; + HNBAP_InitiatingMessage_t *im; + HNBAP_SuccessfulOutcome_t *so; + HNBAP_UERegisterRequestIEs_t ue_req_ies; + HNBAP_UERegisterAcceptIEs_t ue_acc_ies; + HNBAP_HNBRegisterRequestIEs_t hnb_ies; + + char imsi[16]; + + asn_dec_rval_t dec_ret; + + memset(pdu, 0, sizeof(*pdu)); + printf("Testing asn.1 HNBAP decoding\n"); + + dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_HNBAP_PDU, (void **) &pdu, + hnbap_reg_req, sizeof(hnbap_reg_req), 0, 0); + + + ASSERT(dec_ret.code == RC_OK); + ASSERT(pdu->present == HNBAP_HNBAP_PDU_PR_initiatingMessage); + + im = &pdu->choice.initiatingMessage; + + ASSERT(im->procedureCode == HNBAP_ProcedureCode_id_HNBRegister); + + rc = hnbap_decode_hnbregisterrequesties(&hnb_ies, &im->value); + ASSERT(rc >= 0); + + uint16_t lac, sac; + uint8_t rac; + uint32_t cid; + + lac = asn1str_to_u16(&hnb_ies.lac); + sac = asn1str_to_u16(&hnb_ies.sac); + rac = asn1str_to_u8(&hnb_ies.rac); + cid = asn1bitstr_to_u32(&hnb_ies.cellIdentity); + + ASSERT(lac == 0x4020); + ASSERT(sac == 0x0001); + ASSERT(rac == 0x64); + ASSERT(cid == 0x0000000); + + ASSERT(!strcmp((char *) hnb_ies.hnB_Identity.hNB_Identity_Info.buf, "10005B9-0010942050@")); + + printf("HNBAP register request for HNB %s\n", (char *) hnb_ies.hnB_Identity.hNB_Identity_Info.buf); + hnbap_free_hnbregisterrequesties(&hnb_ies); + + dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_HNBAP_PDU, (void **) &pdu, + hnbap_ue_reg_req, sizeof(hnbap_ue_reg_req), 0, 0); + + + ASSERT(dec_ret.code == RC_OK); + ASSERT(pdu->present == HNBAP_HNBAP_PDU_PR_initiatingMessage); + + im = &pdu->choice.initiatingMessage; + ASSERT(im->procedureCode == HNBAP_ProcedureCode_id_UERegister); + + rc = hnbap_decode_ueregisterrequesties(&ue_req_ies, &im->value); + ASSERT(rc >= 0); + + ASSERT(ue_req_ies.uE_Identity.present == HNBAP_UE_Identity_PR_iMSI); + ranap_bcd_decode(imsi, sizeof(imsi), ue_req_ies.uE_Identity.choice.iMSI.buf, + ue_req_ies.uE_Identity.choice.iMSI.size); + + printf("HNBAP UE Register request from IMSI %s\n", imsi); + hnbap_free_ueregisterrequesties(&ue_req_ies); + + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_HNBAP_PDU, pdu); + + memset(pdu, 0, sizeof(*pdu)); + dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_HNBAP_PDU, (void **) &pdu, + hnbap_ue_reg_acc, sizeof(hnbap_ue_reg_acc), 0, 0); + + + ASSERT(dec_ret.code == RC_OK); + ASSERT(pdu->present == HNBAP_HNBAP_PDU_PR_successfulOutcome); + + so = &pdu->choice.successfulOutcome; + ASSERT(so->procedureCode == HNBAP_ProcedureCode_id_UERegister); + + rc = hnbap_decode_ueregisteraccepties(&ue_acc_ies, &so->value); + ASSERT(rc >= 0); + + ASSERT(ue_acc_ies.uE_Identity.present == HNBAP_UE_Identity_PR_iMSI); + ranap_bcd_decode(imsi, sizeof(imsi), ue_acc_ies.uE_Identity.choice.iMSI.buf, + ue_acc_ies.uE_Identity.choice.iMSI.size); + + printf("HNBAP UE Register accept to IMSI %s\n", imsi); + hnbap_free_ueregisteraccepties(&ue_acc_ies); + + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_HNBAP_PDU, pdu); +} + +int main(int argc, char **argv) +{ + test_common_init(); + ranap_set_log_area(DRANAP); + hnbap_set_log_area(DHNBAP); + log_set_log_level(osmo_stderr_target, LOGL_INFO); + + test_asn1_decoding(); + test_common_cleanup(); + + return 0; +} diff --git a/tests/test-hnbap.ok b/tests/test-hnbap.ok new file mode 100644 index 0000000..03331c4 --- /dev/null +++ b/tests/test-hnbap.ok @@ -0,0 +1,4 @@ +Testing asn.1 HNBAP decoding +HNBAP register request for HNB 10005B9-0010942050@ +HNBAP UE Register request from IMSI 262011234567890 +HNBAP UE Register accept to IMSI 262011234567890 diff --git a/tests/test-ranap.c b/tests/test-ranap.c new file mode 100644 index 0000000..e593bd9 --- /dev/null +++ b/tests/test-ranap.c @@ -0,0 +1,207 @@ +/* (C) 2015 by Harald Welte <laforge@gnumonks.org> + * All Rights Reserved + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include <osmocom/core/utils.h> +#include <osmocom/core/msgb.h> +#include <osmocom/core/logging.h> +#include <osmocom/vty/logging.h> + +#include "asn1helpers.h" +#include <osmocom/ranap/iu_helpers.h> + +#include <osmocom/ranap/ranap_common.h> +#include <osmocom/ranap/ranap_ies_defs.h> +#include <osmocom/ranap/ranap_msg_factory.h> +#include <osmocom/ranap/RANAP_MaxBitrate.h> +#include <osmocom/ranap/RANAP_CauseMisc.h> + +#include "test_common.h" + +extern void *msgb_ctx; + +static void test_aper_int(uint32_t inp) +{ + RANAP_MaxBitrate_t mbr = inp; + asn_enc_rval_t rv; + uint8_t buf[32]; + + memset(buf, 0, sizeof(buf)); + + rv = aper_encode_to_buffer(&asn_DEF_RANAP_MaxBitrate, &mbr, buf, sizeof(buf)); + if (rv.encoded == -1) { + fprintf(stderr, "Failed\n"); + return; + } + printf("Encoded MaxBitRate %ld to %s\n", mbr, osmo_hexdump(buf, rv.encoded/8)); +} + +static void test_aper_causemisc(uint32_t inp, uint8_t exp_enc) +{ + RANAP_Cause_t c = { .present = RANAP_Cause_PR_misc, .choice.misc = inp }; + RANAP_Cause_t *c_dec = NULL; + asn_enc_rval_t rv; + uint8_t buf[32]; + + memset(buf, 0, sizeof(buf)); + + rv = aper_encode_to_buffer(&asn_DEF_RANAP_Cause, &c, buf, sizeof(buf)); + if (rv.encoded == -1) { + fprintf(stderr, "Failed\n"); + return; + } + /* test encoding */ + printf("Encoded Cause Misc=%u to %s\n", inp, osmo_hexdump(buf, rv.encoded/8)); + OSMO_ASSERT(buf[0] == exp_enc); + OSMO_ASSERT(rv.encoded == 8); + + /* test re-decoding */ + aper_decode(NULL, &asn_DEF_RANAP_Cause, (void **)&c_dec, buf, 1, 0, 0); + printf("Decoded Cause Misc=%ld\n", c_dec->choice.misc); + OSMO_ASSERT(c_dec->present == RANAP_Cause_PR_misc); + OSMO_ASSERT(c_dec->choice.misc == inp); + ASN_STRUCT_FREE(asn_DEF_RANAP_Cause, c_dec); +} + +static void test_ranap_messages(void) +{ + uint8_t nas_buf[] = { 0xaa, 0xbb, 0xcc }; + struct msgb *msg; + const char *imsi = "901700123456789"; + uint32_t tmsi = 0x01234567; + uint32_t rtp_ip = 0x0a0b0c0d; + uint16_t rtp_port = 2342; + uint32_t gtp_ip = 0x1a1b1c1d; + uint32_t gtp_tei = 0x11223344; + uint8_t ik[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + uint8_t ck[16] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; + int i; + + for (i = 0; i < 1; i++) { + printf("\n==> DIRECT TRANSFER\n"); + msg = ranap_new_msg_dt(0, nas_buf, sizeof(nas_buf)); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> SECURITY MODE COMMAND\n"); + msg = ranap_new_msg_sec_mod_cmd(ik, ck, RANAP_KeyStatus_new); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> COMMON ID\n"); + msg = ranap_new_msg_common_id(imsi); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> IU RELEASE CMD\n"); + RANAP_Cause_t cause = { .present = RANAP_Cause_PR_radioNetwork, + .choice.radioNetwork = RANAP_CauseRadioNetwork_radio_connection_with_UE_Lost }; + msg = ranap_new_msg_iu_rel_cmd(&cause); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> PAGING CMD\n"); + msg = ranap_new_msg_paging_cmd(imsi, &tmsi, 0, RANAP_PagingCause_terminating_conversational_call); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> RAB ASSIGNMENT COMMAND (VOICE)\n"); + msg = ranap_new_msg_rab_assign_voice(1, rtp_ip, rtp_port, 1); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> RAB ASSIGNMENT COMMAND (DATA)\n"); + msg = ranap_new_msg_rab_assign_data(2, gtp_ip, gtp_tei, 1); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> RESET CMD\n"); + msg = ranap_new_msg_reset(RANAP_CN_DomainIndicator_cs_domain, &cause); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> RESET ACK\n"); + uint8_t plmn_id[] = { 0x09, 0x01, 0x99 }; + RANAP_GlobalRNC_ID_t rnc_id; + rnc_id.pLMNidentity.buf = plmn_id; + rnc_id.pLMNidentity.size = sizeof(plmn_id); + rnc_id.rNC_ID = 2342; + msg = ranap_new_msg_reset_ack(RANAP_CN_DomainIndicator_cs_domain, &rnc_id); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> INITIAL UE\n"); + msg = ranap_new_msg_initial_ue(23, 0, &rnc_id, nas_buf, sizeof(nas_buf)); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> IU RELEASE REQ\n"); + msg = ranap_new_msg_iu_rel_req(&cause); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + + printf("\n==> RAB RELEASE REQ\n"); + msg = ranap_new_msg_rab_rel_req(23, &cause); + if (msg) + printf("%s\n", msgb_hexdump(msg)); + msgb_free(msg); + } +} + +int main(int argc, char **argv) +{ + asn1_xer_print = 1; + + //asn_debug = 1; + + test_common_init(); + ranap_set_log_area(DRANAP); + + test_aper_int(1); + test_aper_int(2); + test_aper_int(3); + test_aper_int(255); + test_aper_int(256); + test_aper_int(257); + test_aper_int(64000); + test_aper_int(0xffff); + test_aper_int(0xffff+1); + test_aper_int(0xffff+2); + test_aper_int(16000000); + test_aper_causemisc(RANAP_CauseMisc_unspecified_failure, 0x42); + test_ranap_messages(); + + printf("report\n"); + talloc_report(talloc_asn1_ctx, stdout); + talloc_report(msgb_ctx, stdout); + //talloc_report(NULL, stdout); + + test_common_cleanup(); + printf("exit\n"); + exit(0); +} diff --git a/tests/test-ranap.ok b/tests/test-ranap.ok new file mode 100644 index 0000000..a0ea521 --- /dev/null +++ b/tests/test-ranap.ok @@ -0,0 +1,222 @@ +Encoded MaxBitRate 1 to 00 00 +Encoded MaxBitRate 2 to 00 01 +Encoded MaxBitRate 3 to 00 02 +Encoded MaxBitRate 255 to 00 fe +Encoded MaxBitRate 256 to 00 ff +Encoded MaxBitRate 257 to 40 01 00 +Encoded MaxBitRate 64000 to 40 f9 ff +Encoded MaxBitRate 65535 to 40 ff fe +Encoded MaxBitRate 65536 to 40 ff ff +Encoded MaxBitRate 65537 to 80 01 00 00 +Encoded MaxBitRate 16000000 to 80 f4 23 ff +Encoded Cause Misc=115 to 42 +Decoded Cause Misc=115 + +==> DIRECT TRANSFER +<RANAP_IE> + <id>16</id> + <criticality><ignore/></criticality> + <value>03 AA BB CC</value> +</RANAP_IE> +<RANAP_IE> + <id>59</id> + <criticality><ignore/></criticality> + <value>00</value> +</RANAP_IE> +00 14 40 10 00 00 02 00 10 40 04 03 aa bb cc 00 3b 40 01 00 + +==> SECURITY MODE COMMAND +<RANAP_IE> + <id>12</id> + <criticality><reject/></criticality> + <value> + 08 08 01 01 01 01 01 01 01 01 01 01 01 01 01 01 + 01 01 + </value> +</RANAP_IE> +<RANAP_IE> + <id>11</id> + <criticality><ignore/></criticality> + <value> + 08 90 02 02 02 02 02 02 02 02 02 02 02 02 02 02 + 02 02 + </value> +</RANAP_IE> +<RANAP_IE> + <id>75</id> + <criticality><reject/></criticality> + <value>40</value> +</RANAP_IE> +00 06 00 34 00 00 03 00 0c 00 12 08 08 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 00 0b 40 12 08 90 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 00 4b 00 01 40 + +==> COMMON ID +<RANAP_IE> + <id>23</id> + <criticality><ignore/></criticality> + <value>50 09 71 00 21 43 65 87 F9</value> +</RANAP_IE> +00 0f 40 10 00 00 01 00 17 40 09 50 09 71 00 21 43 65 87 f9 + +==> IU RELEASE CMD +<RANAP_IE> + <id>4</id> + <criticality><ignore/></criticality> + <value>0B 40</value> +</RANAP_IE> +00 01 00 09 00 00 01 00 04 40 02 0b 40 + +==> PAGING CMD +<RANAP_IE> + <id>3</id> + <criticality><ignore/></criticality> + <value>00</value> +</RANAP_IE> +<RANAP_IE> + <id>23</id> + <criticality><ignore/></criticality> + <value>50 09 71 00 21 43 65 87 F9</value> +</RANAP_IE> +<RANAP_IE> + <id>64</id> + <criticality><ignore/></criticality> + <value>00 01 23 45 67</value> +</RANAP_IE> +00 0e 40 1e 00 00 03 00 03 40 01 00 00 17 40 09 50 09 71 00 21 43 65 87 f9 00 40 40 05 00 01 23 45 67 + +==> RAB ASSIGNMENT COMMAND (VOICE) +<RANAP_ProtocolIE-FieldPair> + <id>53</id> + <firstCriticality><reject/></firstCriticality> + <firstValue> + 78 02 CD 80 10 2F A7 20 1A 2C 00 00 F4 4C 08 0A + 02 80 00 51 40 00 27 20 28 14 00 67 40 00 00 22 + 28 14 00 3C 40 00 00 00 50 3D 02 00 02 27 C0 35 + 00 01 0A 0B 0C 0D 00 00 00 00 00 00 00 00 00 00 + 00 00 00 40 09 26 00 00 + </firstValue> + <secondCriticality><ignore/></secondCriticality> + <secondValue>00</secondValue> +</RANAP_ProtocolIE-FieldPair> +<RANAP_IE> + <id>54</id> + <criticality><ignore/></criticality> + <value> + 00 00 01 00 35 00 48 78 02 CD 80 10 2F A7 20 1A + 2C 00 00 F4 4C 08 0A 02 80 00 51 40 00 27 20 28 + 14 00 67 40 00 00 22 28 14 00 3C 40 00 00 00 50 + 3D 02 00 02 27 C0 35 00 01 0A 0B 0C 0D 00 00 00 + 00 00 00 00 00 00 00 00 00 00 40 09 26 00 00 40 + 01 00 + </value> +</RANAP_IE> +00 00 00 59 00 00 01 00 36 40 52 00 00 01 00 35 00 48 78 02 cd 80 10 2f a7 20 1a 2c 00 00 f4 4c 08 0a 02 80 00 51 40 00 27 20 28 14 00 67 40 00 00 22 28 14 00 3c 40 00 00 00 50 3d 02 00 02 27 c0 35 00 01 0a 0b 0c 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 40 09 26 00 00 40 01 00 + +==> RAB ASSIGNMENT COMMAND (DATA) +<RANAP_ProtocolIE-FieldPair> + <id>53</id> + <firstCriticality><reject/></firstCriticality> + <firstValue> + 38 04 12 DE 18 69 FF 80 0C 34 FF 00 1F 40 08 06 + 08 9E 00 00 00 00 B1 40 05 60 01 8C BA 7F 00 00 + 11 3E 35 00 01 1A 1B 1C 1D 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 11 22 33 44 + </firstValue> + <secondCriticality><ignore/></secondCriticality> + <secondValue>78 1C 00 00 00 00</secondValue> +</RANAP_ProtocolIE-FieldPair> +<RANAP_IE> + <id>54</id> + <criticality><ignore/></criticality> + <value> + 00 00 01 00 35 00 3B 38 04 12 DE 18 69 FF 80 0C + 34 FF 00 1F 40 08 06 08 9E 00 00 00 00 B1 40 05 + 60 01 8C BA 7F 00 00 11 3E 35 00 01 1A 1B 1C 1D + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 22 + 33 44 40 06 78 1C 00 00 00 00 + </value> +</RANAP_IE> +00 00 00 51 00 00 01 00 36 40 4a 00 00 01 00 35 00 3b 38 04 12 de 18 69 ff 80 0c 34 ff 00 1f 40 08 06 08 9e 00 00 00 00 b1 40 05 60 01 8c ba 7f 00 00 11 3e 35 00 01 1a 1b 1c 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 22 33 44 40 06 78 1c 00 00 00 00 + +==> RESET CMD +<RANAP_IE> + <id>4</id> + <criticality><ignore/></criticality> + <value>0B 40</value> +</RANAP_IE> +<RANAP_IE> + <id>3</id> + <criticality><reject/></criticality> + <value>00</value> +</RANAP_IE> +00 09 00 0e 00 00 02 00 04 40 02 0b 40 00 03 00 01 00 + +==> RESET ACK +<RANAP_IE> + <id>3</id> + <criticality><reject/></criticality> + <value>00</value> +</RANAP_IE> +<RANAP_IE> + <id>86</id> + <criticality><ignore/></criticality> + <value>09 01 99 09 26</value> +</RANAP_IE> +20 09 00 11 00 00 02 00 03 00 01 00 00 56 40 05 09 01 99 09 26 + +==> INITIAL UE +<RANAP_IE> + <id>3</id> + <criticality><ignore/></criticality> + <value>00</value> +</RANAP_IE> +<RANAP_IE> + <id>15</id> + <criticality><ignore/></criticality> + <value>00 09 01 99 42 23</value> +</RANAP_IE> +<RANAP_IE> + <id>58</id> + <criticality><ignore/></criticality> + <value>00 09 01 99 42 23 42 23</value> +</RANAP_IE> +<RANAP_IE> + <id>16</id> + <criticality><ignore/></criticality> + <value>03 AA BB CC</value> +</RANAP_IE> +<RANAP_IE> + <id>79</id> + <criticality><ignore/></criticality> + <value>00 00 17</value> +</RANAP_IE> +<RANAP_IE> + <id>86</id> + <criticality><ignore/></criticality> + <value>09 01 99 09 26</value> +</RANAP_IE> +00 13 40 36 00 00 06 00 03 40 01 00 00 0f 40 06 00 09 01 99 42 23 00 3a 40 08 00 09 01 99 42 23 42 23 00 10 40 04 03 aa bb cc 00 4f 40 03 00 00 17 00 56 40 05 09 01 99 09 26 + +==> IU RELEASE REQ +<RANAP_IE> + <id>4</id> + <criticality><ignore/></criticality> + <value>0B 40</value> +</RANAP_IE> +00 0b 40 09 00 00 01 00 04 40 02 0b 40 + +==> RAB RELEASE REQ +<RANAP_IE> + <id>40</id> + <criticality><ignore/></criticality> + <value>05 C2 D0</value> +</RANAP_IE> +<RANAP_IE> + <id>41</id> + <criticality><ignore/></criticality> + <value>00 00 01 00 28 40 03 05 C2 D0</value> +</RANAP_IE> +00 0a 40 11 00 00 01 00 29 40 0a 00 00 01 00 28 40 03 05 c2 d0 +report +talloc report on 'asn1_context' (total 0 bytes in 1 blocks) +talloc report on 'msgb' (total 0 bytes in 1 blocks) +exit diff --git a/tests/test_common.c b/tests/test_common.c new file mode 100644 index 0000000..36d9954 --- /dev/null +++ b/tests/test_common.c @@ -0,0 +1,112 @@ +/* Common osmo-iuh test stub code */ + +/* (C) 2015 by Sysmocom s.f.m.c. GmbH + * Author: Daniel Willmann <dwillmann@sysmocom.de> + * All Rights Reserved + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <getopt.h> +#include <errno.h> +#include <signal.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/sctp.h> +#include <arpa/inet.h> + +#include <asn1c/asn_internal.h> + +#include <osmocom/core/application.h> +#include <osmocom/core/talloc.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/msgb.h> + +#include "test_common.h" + +static const struct log_info_cat log_cat[] = { + [DMAIN] = { + .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1, + .color = "", + .description = "Main program", + }, + [DHNBAP] = { + .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "Home Node B Application Part", + }, + [DRANAP] = { + .name = "RANAP", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "RAN Application Part", + }, + [DRUA] = { + .name = "RUA", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "RANAP User Adaptation", + }, +}; + +static const struct log_info test_log_info = { + .cat = log_cat, + .num_cat = ARRAY_SIZE(log_cat), +}; + +static void *tall_hnb_ctx; +void *msgb_ctx; + +int test_common_init(void) +{ + int rc; + + tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); + msgb_ctx = msgb_talloc_ctx_init(NULL, 0); + talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); + + rc = osmo_init_logging2(tall_hnb_ctx, &test_log_info); + if (rc < 0) + exit(1); + + log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); + log_set_use_color(osmo_stderr_target, 0); + log_set_print_category(osmo_stderr_target, 0); + log_set_print_category_hex(osmo_stderr_target, 0); + return rc; +} + +void test_common_cleanup(void) +{ + if (talloc_total_blocks(msgb_ctx) != 1 + || talloc_total_size(msgb_ctx) != 0) + talloc_report_full(msgb_ctx, stderr); + + OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1); + OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0); + talloc_free(msgb_ctx); + + if (talloc_total_blocks(talloc_asn1_ctx) != 1 + || talloc_total_size(talloc_asn1_ctx) != 0) + talloc_report_full(talloc_asn1_ctx, stderr); + + OSMO_ASSERT(talloc_total_blocks(talloc_asn1_ctx) == 1); + OSMO_ASSERT(talloc_total_size(talloc_asn1_ctx) == 0); + talloc_free(talloc_asn1_ctx); +} diff --git a/tests/test_common.h b/tests/test_common.h new file mode 100644 index 0000000..fdcac05 --- /dev/null +++ b/tests/test_common.h @@ -0,0 +1,11 @@ +#pragma once + +enum { + DMAIN, + DHNBAP, + DRUA, + DRANAP, +}; + +int test_common_init(void); +void test_common_cleanup(void); diff --git a/tests/testsuite.at b/tests/testsuite.at new file mode 100644 index 0000000..b0bd68f --- /dev/null +++ b/tests/testsuite.at @@ -0,0 +1,22 @@ +AT_INIT +AT_BANNER([Regression tests.]) + + +AT_SETUP([helpers]) +AT_KEYWORDS([helpers]) +cat $abs_srcdir/test-helpers.ok > expout +cat $abs_srcdir/test-helpers.err > experr +AT_CHECK([$abs_top_builddir/tests/test-helpers], [0], [expout], [experr]) +AT_CLEANUP + +AT_SETUP([hnbap]) +AT_KEYWORDS([hnbap]) +cat $abs_srcdir/test-hnbap.ok > expout +AT_CHECK([$abs_top_builddir/tests/test-hnbap], [0], [expout]) +AT_CLEANUP + +AT_SETUP([ranap]) +AT_KEYWORDS([ranap]) +cat $abs_srcdir/test-ranap.ok > expout +AT_CHECK([$abs_top_builddir/tests/test-ranap], [0], [expout]) +AT_CLEANUP |