ranap_parse_lai(): Fix wrong BCD decoding for MNC. Add test.

Fix the ranap_parse_lai() part that decodes the MNC: place the *10 at the
proper MNC digit.

Add a comprehensive test for ranap_parse_lai() in test-helpers.c. Because
ranap_parse_lai() logs things, add test_common.c to test-helpers compilation
and an expected stderr output to test-helpers' testsuite.at def.
This commit is contained in:
Neels Hofmeyr 2016-04-06 13:56:02 +02:00
parent 8f4cd86904
commit d8013d1f8c
6 changed files with 114 additions and 5 deletions

View File

@ -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;

View File

@ -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

View File

@ -18,14 +18,19 @@
*
*/
#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/gsm/gsm48.h>
#include <osmocom/ranap/RANAP_LAI.h>
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;
}

View File

@ -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


View File

@ -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

View File

@ -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])