Change in libosmocore[master]: gsm_7bit_encode_n(): test encoding of more than 250 septets

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

fixeria gerrit-no-reply at lists.osmocom.org
Mon Feb 1 16:14:55 UTC 2021


fixeria has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/22542 )

Change subject: gsm_7bit_encode_n(): test encoding of more than 250 septets
......................................................................

gsm_7bit_encode_n(): test encoding of more than 250 septets

As can be seen, this unit test reveals problems with encoding
of more than 250 septets using gsm_7bit_encode_n().  The problem
is that some API functions use type 'uint8_t' for the length, so
we basically suffer from integer overflows.

Change-Id: I723300578d5ab0c7b94cf49c14d962b2dbf47740
---
M tests/sms/sms_test.c
M tests/sms/sms_test.ok
2 files changed, 70 insertions(+), 0 deletions(-)

Approvals:
  laforge: Looks good to me, but someone else must approve
  pespin: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c
index 0615396..c7f47e2 100644
--- a/tests/sms/sms_test.c
+++ b/tests/sms/sms_test.c
@@ -268,6 +268,54 @@
 	printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
 }
 
+static void test_enc_large_msg(void)
+{
+	uint8_t enc_buf[2048 * 7 / 8];
+	char large_msg[2048 + 1];
+	int i, j, nsep, noct = 0;
+
+	printf("\nRunning %s\n", __func__);
+
+	/* Expected chunks (repeated) in the output buffer */
+	const uint8_t exp_chunk[] = { 0xc1, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x83 };
+
+	/* Length variants to be tested */
+	static const size_t nlen[] = { 2048, 1024, 555, 512, 260, 255, 250 };
+
+	memset(&large_msg[0], (int) 'A', sizeof(large_msg) - 1);
+
+	for (i = 0; i < ARRAY_SIZE(nlen); i++) {
+		/* Clear the output buffer first */
+		memset(&enc_buf[0], 0x00, sizeof(enc_buf));
+		/* Limit length of the input string */
+		large_msg[nlen[i]] = '\0';
+
+		/* How many octets we expect to be used? */
+		int noct_exp = nlen[i] * 7 / 8;
+		if (nlen[i] % 8 != 0)
+			noct_exp++;
+
+		/* Encode a sequence of 'A' repeated nlen[i] times */
+		nsep = gsm_7bit_encode_n(&enc_buf[0], sizeof(enc_buf), large_msg, &noct);
+		printf("gsm_7bit_encode_n(len=%zu) processed %d septets (expected %zu): %s\n",
+		       nlen[i], nsep, nlen[i], nsep == nlen[i] ? "OK" : "FAIL");
+		printf("gsm_7bit_encode_n(len=%zu) used %d octets in the buffer (expected %d): %s\n",
+		       nlen[i], noct, noct_exp, noct == noct_exp ? "OK" : "FAIL");
+
+		/* The encoding result is expected to consist of repeated chunks */
+		for (j = 0; j < noct_exp; j += sizeof(exp_chunk)) {
+			size_t len = OSMO_MIN(noct_exp - j, sizeof(exp_chunk));
+			if (nlen[i] % 8 != 0) /* skip incomplete octets */
+				len--;
+			if (memcmp(&enc_buf[j], exp_chunk, len) != 0) {
+				printf("\tUnexpected chunk at enc_buf[%d:%zu]: %s\n",
+				       j, len, osmo_hexdump(&enc_buf[j], len));
+				break; /* No need to show them all */
+			}
+		}
+	}
+}
+
 int main(int argc, char** argv)
 {
 	printf("SMS testing\n");
@@ -396,6 +444,7 @@
 
 	test_octet_return();
 	test_gen_oa();
+	test_enc_large_msg();
 
 	printf("OK\n");
 	return 0;
diff --git a/tests/sms/sms_test.ok b/tests/sms/sms_test.ok
index a71567d..724c166 100644
--- a/tests/sms/sms_test.ok
+++ b/tests/sms/sms_test.ok
@@ -18,4 +18,25 @@
 Result: len(2) data(00 91 )
 Result: len(9) data(0e d0 4f 78 d9 2d 9c 0e 01 )
 Result: len(12) data(14 d0 4f 78 d9 2d 9c 0e c3 e2 31 19 )
+
+Running test_enc_large_msg
+gsm_7bit_encode_n(len=2048) processed 2048 septets (expected 2048): OK
+gsm_7bit_encode_n(len=2048) used 0 octets in the buffer (expected 1792): FAIL
+	Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 
+gsm_7bit_encode_n(len=1024) processed 1024 septets (expected 1024): OK
+gsm_7bit_encode_n(len=1024) used 0 octets in the buffer (expected 896): FAIL
+	Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 
+gsm_7bit_encode_n(len=555) processed 555 septets (expected 555): OK
+gsm_7bit_encode_n(len=555) used 38 octets in the buffer (expected 486): FAIL
+	Unexpected chunk at enc_buf[35:6]: c1 60 10 00 00 00 
+gsm_7bit_encode_n(len=512) processed 512 septets (expected 512): OK
+gsm_7bit_encode_n(len=512) used 0 octets in the buffer (expected 448): FAIL
+	Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 
+gsm_7bit_encode_n(len=260) processed 260 septets (expected 260): OK
+gsm_7bit_encode_n(len=260) used 4 octets in the buffer (expected 228): FAIL
+	Unexpected chunk at enc_buf[0:6]: c1 60 30 08 00 00 
+gsm_7bit_encode_n(len=255) processed 255 septets (expected 255): OK
+gsm_7bit_encode_n(len=255) used 224 octets in the buffer (expected 224): OK
+gsm_7bit_encode_n(len=250) processed 250 septets (expected 250): OK
+gsm_7bit_encode_n(len=250) used 219 octets in the buffer (expected 219): OK
 OK

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/22542
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I723300578d5ab0c7b94cf49c14d962b2dbf47740
Gerrit-Change-Number: 22542
Gerrit-PatchSet: 2
Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210201/c25995af/attachment.htm>


More information about the gerrit-log mailing list