diff --git a/src/ranap_common.c b/src/ranap_common.c index 57f2e83a..dc464591 100644 --- a/src/ranap_common.c +++ b/src/ranap_common.c @@ -508,8 +508,8 @@ int ranap_parse_lai(struct gprs_ra_id *ra_id, const RANAP_LAI_t *lai) ra_id->mcc = (ptr[0] & 0xF) * 100 + (ptr[0] >> 4) * 10 + (ptr[1] & 0xF); - ra_id->mnc = (ptr[2] & 0xF) + - (ptr[2] >> 4) * 10; + ra_id->mnc = (ptr[2] & 0xF) * 10 + + (ptr[2] >> 4); if ((ptr[1] >> 4) != 0xF) ra_id->mnc += (ptr[1] >> 4) * 100; diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 739410be..0fd050f4 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -7,7 +7,7 @@ check_PROGRAMS = test-ranap test-helpers test-hnbap hnb-test dummy-cn HNBAP_FILES = $(top_srcdir)/src/hnbap_common.c $(top_srcdir)/src/hnbap_decoder.c $(top_srcdir)/src/hnbap_encoder.c RUA_FILES = $(top_srcdir)/src/rua_common.c $(top_srcdir)/src/rua_decoder.c $(top_srcdir)/src/rua_encoder.c $(top_srcdir)/src/rua_msg_factory.c -test_helpers_SOURCES = test-helpers.c +test_helpers_SOURCES = test-helpers.c test_common.c test_helpers_LDADD = $(COMMON_LIBS) $(top_builddir)/src/libosmo-ranap.la test_hnbap_SOURCES = $(top_srcdir)/src/hnbap_common.c $(top_srcdir)/src/hnbap_decoder.c test-hnbap.c test_common.c diff --git a/src/tests/test-helpers.c b/src/tests/test-helpers.c index 78bbe23c..44fd7356 100644 --- a/src/tests/test-helpers.c +++ b/src/tests/test-helpers.c @@ -18,14 +18,19 @@ * */ +#include "test_common.h" #include +#include #include "asn1helpers.h" #include #define ASSERT(x) assert(x) #include +#include + +#include int asn1_xer_print = 0; void *talloc_asn1_ctx; @@ -93,7 +98,8 @@ void test_asn1_helpers(void) printf("Decoding back to uint32_t: 0x%x\n", res); ASSERT(res == val1); - printf("Encoding %s to 24-bit asn.1 bitstring\n", osmo_hexdump_nospc(&val1, 3)); + printf("Encoding %s to 24-bit asn.1 bitstring\n", + osmo_hexdump_nospc((unsigned char*)&val1, 3)); asn1_u24_to_bitstring(&enc, &tmpval, val1); ASSERT(enc.size == 24/8); @@ -114,10 +120,96 @@ void test_asn1_helpers(void) } +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((char*)lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump((char*)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((char*)lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump((char*)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((char*)lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump((char*)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((char*)lai.pLMNidentity.buf, + lai.pLMNidentity.size)); + printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf, + lai.lAC.size)); + + rc = ranap_parse_lai(&ra_id, &lai); + printf(" rc == %d\n", rc); + OSMO_ASSERT(rc == -1); +} + int main(int argc, char **argv) { + test_common_init(); + test_iu_helpers(); test_asn1_helpers(); + test_ranap_common(); return 0; } diff --git a/src/tests/test-helpers.err b/src/tests/test-helpers.err new file mode 100644 index 00000000..9c04723d --- /dev/null +++ b/src/tests/test-helpers.err @@ -0,0 +1,3 @@ +<0004> ranap_common.c:504 Invalid PLMN Identity size: should be 3, is 2 +<0004> ranap_common.c:518 Invalid LAC size: should be 2, is 1 + \ No newline at end of file diff --git a/src/tests/test-helpers.ok b/src/tests/test-helpers.ok index 4ba4d546..ddfea78d 100644 --- a/src/tests/test-helpers.ok +++ b/src/tests/test-helpers.ok @@ -10,3 +10,16 @@ Encoding efbead 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 diff --git a/src/tests/testsuite.at b/src/tests/testsuite.at index c1daef21..9378f186 100644 --- a/src/tests/testsuite.at +++ b/src/tests/testsuite.at @@ -5,7 +5,8 @@ AT_BANNER([Regression tests.]) AT_SETUP([helpers]) AT_KEYWORDS([helpers]) cat $abs_srcdir/test-helpers.ok > expout -AT_CHECK([$abs_top_builddir/src/tests/test-helpers], [0], [expout]) +cat $abs_srcdir/test-helpers.err > experr +AT_CHECK([$abs_top_builddir/src/tests/test-helpers], [0], [expout], [experr]) AT_CLEANUP AT_SETUP([hnbap])