aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <vyanitskiy@sysmocom.de>2021-01-30 00:27:06 +0100
committerfixeria <vyanitskiy@sysmocom.de>2021-02-01 16:14:55 +0000
commit44ac4f6c9294abdbc5344b68b49b088e7f67b0ff (patch)
treea33560798785423c3f532839e20b88df76d36a6a
parent2319014f7efd98a8f86ac0fcb0dfaee257249e96 (diff)
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
-rw-r--r--tests/sms/sms_test.c49
-rw-r--r--tests/sms/sms_test.ok21
2 files changed, 70 insertions, 0 deletions
diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c
index 06153964..c7f47e20 100644
--- a/tests/sms/sms_test.c
+++ b/tests/sms/sms_test.c
@@ -268,6 +268,54 @@ static void test_gen_oa(void)
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 @@ int main(int argc, char** argv)
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 a71567de..724c166b 100644
--- a/tests/sms/sms_test.ok
+++ b/tests/sms/sms_test.ok
@@ -18,4 +18,25 @@ Result: len(12) data(14 a1 21 43 65 87 09 21 43 65 87 19 )
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