[PATCH] libosmo-sccp[master]: implement unit tests for osmo_sccp_addr_{parse, encode}()

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Oct 27 15:55:25 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/4449

to look at the new patch set (#2).

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
---
M src/sccp2sua.c
M src/xua_internal.h
M tests/testsuite.at
M tests/xua/xua_test.c
A tests/xua/xua_test.err
M tests/xua/xua_test.ok
6 files changed, 269 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmo-sccp refs/changes/49/4449/2

diff --git a/src/sccp2sua.c b/src/sccp2sua.c
index ac2b2c2..d97906e 100644
--- a/src/sccp2sua.c
+++ b/src/sccp2sua.c
@@ -262,6 +262,16 @@
 		goto out;
 	}
 
+	if (in->gt.npi && (in->gt.npi > 0xF)) {
+		LOGP(DLSUA, LOGL_ERROR, "Unsupported Numbering Plan %u", in->gt.npi);
+		return -EINVAL;
+	}
+
+	if (in->gt.nai && (in->gt.nai > 0x7F)) {
+		LOGP(DLSUA, LOGL_ERROR, "Unsupported Nature of Address %u", in->gt.nai);
+		return -EINVAL;
+	}
+
 	odd = strlen(in->gt.digits) & 1;
 	switch (in->gt.gti) {
 	case OSMO_SCCP_GTI_NO_GT:
diff --git a/src/xua_internal.h b/src/xua_internal.h
index 991110b..96bd153 100644
--- a/src/xua_internal.h
+++ b/src/xua_internal.h
@@ -77,3 +77,4 @@
 			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 b810bdf..ebc43e7 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -22,7 +22,8 @@
 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/xua_test.c b/tests/xua/xua_test.c
index c496cc4..5a9d0ab 100644
--- a/tests/xua/xua_test.c
+++ b/tests/xua/xua_test.c
@@ -34,6 +34,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
+#include <errno.h>
 
 static void test_isup_parse(void)
 {
@@ -138,6 +139,183 @@
 		printf("sccp_addr_parse test case %u\n", i);
 		test_sccp_addr_parse(&tcase->expected, tcase->bin, tcase->bin_len);
 	}
+}
+
+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 */
@@ -408,12 +586,15 @@
 	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 0000000..17870ee
--- /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 12d817d..6b0cb33 100644
--- a/tests/xua/xua_test.ok
+++ b/tests/xua/xua_test.ok
@@ -132,4 +132,77 @@
 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.

-- 
To view, visit https://gerrit.osmocom.org/4449
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I612352736ab33462ca0dd97798a2c437eadccb86
Gerrit-PatchSet: 2
Gerrit-Project: libosmo-sccp
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder


More information about the gerrit-log mailing list