From db736f432aafb54a6b9469cabf5fa3cbbe359e9f Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 27 Oct 2017 17:39:34 +0200 Subject: [PATCH] implement unit tests for osmo_sccp_addr_{parse,encode}() The recent bug with chopped-off point codes in SCCP Address handling has shown that this code could need proper test cases. This patch adds a testsuite for SCCP address encoding and decoding. Related: OS#2441 Change-Id: I612352736ab33462ca0dd97798a2c437eadccb86 --- src/xua_internal.h | 1 + tests/testsuite.at | 3 +- tests/xua/Makefile.am | 2 +- tests/xua/xua_test.c | 181 +++++++++++++++++++++++++++++++++++++++++ tests/xua/xua_test.err | 2 + tests/xua/xua_test.ok | 73 +++++++++++++++++ 6 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 tests/xua/xua_test.err diff --git a/src/xua_internal.h b/src/xua_internal.h index 991110b6..96bd1530 100644 --- a/src/xua_internal.h +++ b/src/xua_internal.h @@ -77,3 +77,4 @@ int osmo_isup_party_parse(char *out_digits, const uint8_t *in, unsigned int in_num_bytes, bool odd); int osmo_sccp_addr_parse(struct osmo_sccp_addr *out, const uint8_t *addr, unsigned int addrlen); +int osmo_sccp_addr_encode(struct msgb *msg, const struct osmo_sccp_addr *in); diff --git a/tests/testsuite.at b/tests/testsuite.at index b810bdf3..ebc43e79 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -22,7 +22,8 @@ AT_CLEANUP AT_SETUP([xua]) AT_KEYWORDS([xua]) cat $abs_srcdir/xua/xua_test.ok > expout -AT_CHECK([$abs_top_builddir/tests/xua/xua_test], [], [expout], [ignore]) +cat $abs_srcdir/xua/xua_test.err > experr +AT_CHECK([$abs_top_builddir/tests/xua/xua_test], [], [expout], [experr]) AT_CLEANUP AT_SETUP([ss7]) diff --git a/tests/xua/Makefile.am b/tests/xua/Makefile.am index c6a99550..f56692be 100644 --- a/tests/xua/Makefile.am +++ b/tests/xua/Makefile.am @@ -5,7 +5,7 @@ AM_LDFLAGS = -static LDADD = $(top_builddir)/src/libosmo-sigtran.la \ $(LIBOSMOCORE_LIBS) $(LIBOSMOVTY_LIBS) $(LIBOSMONETIF_LIBS) $(LIBSCTP_LIBS) -EXTRA_DIST = xua_test.ok +EXTRA_DIST = xua_test.ok xua_test.err noinst_HEADERS = sccp_test_data.h noinst_PROGRAMS = xua_test diff --git a/tests/xua/xua_test.c b/tests/xua/xua_test.c index c496cc44..5a9d0ab9 100644 --- a/tests/xua/xua_test.c +++ b/tests/xua/xua_test.c @@ -34,6 +34,7 @@ #include #include #include +#include static void test_isup_parse(void) { @@ -140,6 +141,183 @@ static void test_sccp_addr_parser(void) } } +struct sccp_addr_enc_testcase { + const char *name; + struct osmo_sccp_addr addr_in; + int rc; + char *exp_out; +}; + +static const struct sccp_addr_enc_testcase enc_cases[] = { + { + .name = "NOGT-PC1024", + .addr_in = { + .ri = OSMO_SCCP_RI_SSN_PC, + .presence = OSMO_SCCP_ADDR_T_PC, + .pc = 1024, + }, + .rc = 3, + .exp_out = "410004", + }, { + .name = "NOGT-PC16383", + .addr_in = { + .ri = OSMO_SCCP_RI_SSN_PC, + .presence = OSMO_SCCP_ADDR_T_PC, + .pc = 16383, + }, + .rc = 3, + .exp_out = "41ff3f", + }, { + .name = "NOGT-PC16383-SSN90", + .addr_in = { + .ri = OSMO_SCCP_RI_SSN_PC, + .presence = OSMO_SCCP_ADDR_T_PC | OSMO_SCCP_ADDR_T_SSN, + .pc = 16383, + .ssn = 0x5A, + }, + .rc = 4, + .exp_out = "43ff3f5a", + }, { + .name = "GT-PC16383-NAIONLY", + .addr_in = { + .ri = OSMO_SCCP_RI_SSN_PC, + .presence = OSMO_SCCP_ADDR_T_PC | OSMO_SCCP_ADDR_T_GT, + .pc = 16383, + .gt.gti = OSMO_SCCP_GTI_NAI_ONLY, + .gt.nai = 0x7f, + }, + .rc = 4, + .exp_out = "45ff3f7f", + }, { + .name = "GT-NOPC-NAIONLY", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_NAI_ONLY, + .gt.nai = 0x03, + }, + .rc = 2, + .exp_out = "0403", + }, { + .name = "GT-NOPC-TTONLY", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_TT_ONLY, + .gt.tt = 0x03, + }, + .rc = -EINVAL, + }, { + .name = "GT-NOPC-TT_NPL_ENC-ODD", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC, + .gt.tt = 0x03, + .gt.npi = 1, + .gt.digits = "123", + }, + .rc = 5, + .exp_out = "0c03112103", + }, { + .name = "GT-NOPC-TT_NPL_ENC-EVEN", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC, + .gt.tt = 0x03, + .gt.npi = 1, + .gt.digits = "1234", + }, + .rc = 5, + .exp_out = "0c03122143", + }, { + .name = "GT-NOPC-TT_NPL_ENC_NAI-EVEN", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC_NAI, + .gt.tt = 0x03, + .gt.npi = 1, + .gt.nai = 4, + .gt.digits = "1234", + }, + .rc = 6, + .exp_out = "100312042143", + }, { + .name = "GT-NOPC-GTI_INVALID", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = 23, + .gt.tt = 0x03, + .gt.npi = 1, + .gt.nai = 4, + .gt.digits = "1234", + }, + .rc = -EINVAL, + }, { + .name = "GT-NOPC-TT_NPL_ENC_NAI-EVEN-NONNUM", + .addr_in = { + .ri = OSMO_SCCP_RI_GT, + .presence = OSMO_SCCP_ADDR_T_GT, + .gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC_NAI, + .gt.tt = 0x03, + .gt.npi = 1, + .gt.nai = 4, + .gt.digits = "1ABF", + }, + .rc = 6, + .exp_out = "10031204a1fb", + }, + +}; + +static void testcase_sccp_addr_encdec(const struct sccp_addr_enc_testcase *tcase) +{ + struct msgb *msg = msgb_alloc(1024, "encdec"); + struct osmo_sccp_addr out; + char *str; + int rc; + + printf("\n=> %s\n", tcase->name); + + printf("input addr: %s\n", osmo_sccp_addr_dump(&tcase->addr_in)); + rc = osmo_sccp_addr_encode(msg, &tcase->addr_in); + printf("rc=%d, expected rc=%d\n", rc, tcase->rc); + OSMO_ASSERT(rc == tcase->rc); + + if (rc <= 0) { + msgb_free(msg); + return; + } + + str = osmo_hexdump_nospc(msg->data, msg->len); + printf("encoded addr: %s\n", str); + if (tcase->exp_out) { + printf("expected addr: %s\n", tcase->exp_out); + OSMO_ASSERT(!strcmp(tcase->exp_out, str)); + } + + rc = osmo_sccp_addr_parse(&out, msg->data, msg->len); + printf("decod addr: %s\n", osmo_sccp_addr_dump(&out)); + + OSMO_ASSERT(!memcmp(&out, &tcase->addr_in, sizeof(out))); + + msgb_free(msg); +} + +static void test_sccp_addr_encdec(void) +{ + int i; + + printf("Testing SCCP Address Encode/Decode\n"); + for (i = 0; i < ARRAY_SIZE(enc_cases); i++) { + testcase_sccp_addr_encdec(&enc_cases[i]); + } + printf("\n"); +} + /* sccp_addr_testcases[0].expected.gt transcoded into a SUA Global Title IE */ static const uint8_t expected_sua_gt[] = { 0x80, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, @@ -408,12 +586,15 @@ int main(int argc, char **argv) log_init(&log_info, NULL); stderr_target = log_target_create_stderr(); log_add_target(stderr_target); + log_set_use_color(stderr_target, 0); + log_set_print_filename(stderr_target, 0); test_isup_parse(); test_sccp_addr_parser(); test_helpers(); test_sccp2sua(); test_rkm(); + test_sccp_addr_encdec(); printf("All tests passed.\n"); return 0; diff --git a/tests/xua/xua_test.err b/tests/xua/xua_test.err new file mode 100644 index 00000000..17870ee8 --- /dev/null +++ b/tests/xua/xua_test.err @@ -0,0 +1,2 @@ +Unsupported Translation Type 2requested +Unsupported GTI 23 requested diff --git a/tests/xua/xua_test.ok b/tests/xua/xua_test.ok index 12d817df..6b0cb330 100644 --- a/tests/xua/xua_test.ok +++ b/tests/xua/xua_test.ok @@ -132,4 +132,77 @@ Re-Encoding decoded SUA to SCCP SCCP Output: 09 81 03 0d 18 0a 12 07 00 12 04 53 84 09 00 17 0b 12 06 00 12 04 44 87 20 00 20 65 9a 65 81 97 48 04 26 00 01 98 49 04 51 01 03 df 6c 81 88 a1 81 85 02 01 44 02 01 07 30 80 a7 80 a0 80 04 01 2b 30 80 30 12 83 01 10 84 01 07 85 07 91 44 57 76 67 16 97 86 01 20 30 06 82 01 18 84 01 04 00 00 00 00 a3 06 04 01 42 84 01 05 a3 06 04 01 51 84 01 05 a3 06 04 01 31 84 01 05 a3 09 04 01 12 84 01 05 82 01 02 a3 09 04 01 11 84 01 05 81 01 01 a3 06 04 01 14 84 01 00 a3 0b 04 01 41 84 01 04 30 03 83 01 10 a3 0b 04 01 41 84 01 04 30 03 82 01 18 00 00 00 00 Parsing M3UA Message Parsing Nested M3UA Routing Key IE +Testing SCCP Address Encode/Decode + +=> NOGT-PC1024 +input addr: RI=2,PC=1024,GTI=0 +rc=3, expected rc=3 +encoded addr: 410004 +expected addr: 410004 +decod addr: RI=2,PC=1024,GTI=0 + +=> NOGT-PC16383 +input addr: RI=2,PC=16383,GTI=0 +rc=3, expected rc=3 +encoded addr: 41ff3f +expected addr: 41ff3f +decod addr: RI=2,PC=16383,GTI=0 + +=> NOGT-PC16383-SSN90 +input addr: RI=2,PC=16383,SSN=90,GTI=0 +rc=4, expected rc=4 +encoded addr: 43ff3f5a +expected addr: 43ff3f5a +decod addr: RI=2,PC=16383,SSN=90,GTI=0 + +=> GT-PC16383-NAIONLY +input addr: RI=2,PC=16383,GTI=1,GT=() +rc=4, expected rc=4 +encoded addr: 45ff3f7f +expected addr: 45ff3f7f +decod addr: RI=2,PC=16383,GTI=1,GT=() + +=> GT-NOPC-NAIONLY +input addr: RI=1,GTI=1,GT=() +rc=2, expected rc=2 +encoded addr: 0403 +expected addr: 0403 +decod addr: RI=1,GTI=1,GT=() + +=> GT-NOPC-TTONLY +input addr: RI=1,GTI=2,GT=(TT=3,DIG=) +rc=-22, expected rc=-22 + +=> GT-NOPC-TT_NPL_ENC-ODD +input addr: RI=1,GTI=3,GT=(TT=3,NPL=1,DIG=123) +rc=5, expected rc=5 +encoded addr: 0c03112103 +expected addr: 0c03112103 +decod addr: RI=1,GTI=3,GT=(TT=3,NPL=1,DIG=123) + +=> GT-NOPC-TT_NPL_ENC-EVEN +input addr: RI=1,GTI=3,GT=(TT=3,NPL=1,DIG=1234) +rc=5, expected rc=5 +encoded addr: 0c03122143 +expected addr: 0c03122143 +decod addr: RI=1,GTI=3,GT=(TT=3,NPL=1,DIG=1234) + +=> GT-NOPC-TT_NPL_ENC_NAI-EVEN +input addr: RI=1,GTI=4,GT=(TT=3,NPL=1,NAI=4,DIG=1234) +rc=6, expected rc=6 +encoded addr: 100312042143 +expected addr: 100312042143 +decod addr: RI=1,GTI=4,GT=(TT=3,NPL=1,NAI=4,DIG=1234) + +=> GT-NOPC-GTI_INVALID +input addr: RI=1,GTI=23,GT=(DIG=1234) +rc=-22, expected rc=-22 + +=> GT-NOPC-TT_NPL_ENC_NAI-EVEN-NONNUM +input addr: RI=1,GTI=4,GT=(TT=3,NPL=1,NAI=4,DIG=1ABF) +rc=6, expected rc=6 +encoded addr: 10031204a1fb +expected addr: 10031204a1fb +decod addr: RI=1,GTI=4,GT=(TT=3,NPL=1,NAI=4,DIG=1ABF) + All tests passed.