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.
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
index 739410b..0fd050f 100644
--- a/src/tests/Makefile.am
+++ b/src/tests/Makefile.am
@@ -7,7 +7,7 @@
 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 78bbe23..44fd735 100644
--- a/src/tests/test-helpers.c
+++ b/src/tests/test-helpers.c
@@ -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 @@
 	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_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 0000000..9c04723
--- /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 4ba4d54..ddfea78 100644
--- a/src/tests/test-helpers.ok
+++ b/src/tests/test-helpers.ok
@@ -10,3 +10,16 @@
 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 c1daef2..9378f18 100644
--- a/src/tests/testsuite.at
+++ b/src/tests/testsuite.at
@@ -5,7 +5,8 @@
 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])