[PATCH] tests: don't hardcode length values of expected encoding gsm_7bit_encode: make sure to return the number of actually written bytes gsm_7bit_decode: calculate length of resulting septets from input length before decoding

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/OpenBSC@lists.osmocom.org/.

Nico Golde nico at ngolde.de
Tue Jul 20 13:43:58 UTC 2010


The input length to gsm_7bit_decode reflects the number of encoded bytes
to be decoded. As the decoding is done on the input in septetes we need
to take this into account and recalculate the length.
---
 src/gsm_utils.c      |   10 ++++++----
 tests/sms/sms_test.c |   21 ++++++++++-----------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/gsm_utils.c b/src/gsm_utils.c
index 3a378ac..818655e 100644
--- a/src/gsm_utils.c
+++ b/src/gsm_utils.c
@@ -49,21 +49,23 @@ static int gsm_septet_lookup(uint8_t ch)
 /* GSM 03.38 6.2.1 Character unpacking */
 int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
 {
-	int i = 0;
+	int i = 0,y;
 	int l = 0;
+	int septet_l = (length * 8) / 7;
 	uint8_t *rtext = calloc(length, sizeof(uint8_t));
 	uint8_t tmp;
 
 	/* FIXME: We need to account for user data headers here */
 	i += l;
-	for (; i < length; i ++){
+	for (; i < septet_l; i++){
 		rtext[i] =
 			((user_data[(i * 7 + 7) >> 3] <<
 			  (7 - ((i * 7 + 7) & 7))) |
 			 (user_data[(i * 7) >> 3] >>
 			  ((i * 7) & 7))) & 0x7f;
 	}
-	for(i = 0; i < length; i++){
+
+	for(i = 0; i < septet_l; i++){
 		/* this is an extension character */
 		if(rtext[i] == 0x1b && i + 1 < length){
 			tmp = rtext[i+1];
@@ -139,7 +141,7 @@ int gsm_7bit_encode(uint8_t *result, const char *data)
 	}
 
 	free(rdata);
-	return i;
+	return z;
 }
 
 /* determine power control level for given dBm value, as indicated
diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c
index 3742dd8..9d87b5b 100644
--- a/tests/sms/sms_test.c
+++ b/tests/sms/sms_test.c
@@ -37,7 +37,7 @@ struct test_case {
 
 static const char simple_text[] = "test text";
 static const uint8_t simple_enc[] = {
-	0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74, 0x00,
+	0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74
 };
 
 static const char escape_text[] = "!$ a more#^- complicated test@@?_\%! case";
@@ -46,7 +46,6 @@ static const uint8_t escape_enc[] = {
 	0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d,
 	0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03,
 	0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65,
-	0x00, 0x00, 0x00, 0x00, 0x00
 };
 
 static const struct test_case test_encode[] =
@@ -54,12 +53,12 @@ static const struct test_case test_encode[] =
 	{
 		.input = simple_text,
 		.expected = simple_enc,
-		.expected_length = 9,
+		.expected_length = sizeof(simple_enc),
 	},
 	{
 		.input = escape_text,
 		.expected = escape_enc,
-		.expected_length = 41,
+		.expected_length = sizeof(escape_enc),
 	},
 };
 
@@ -67,12 +66,12 @@ static const struct test_case test_decode[] =
 {
 	{
 		.input = simple_enc,
-		.input_length = 9,
+		.input_length = sizeof(simple_enc),
 		.expected = simple_text,
 	},
 	{
 		.input = escape_enc,
-		.input_length = 41,
+		.input_length = sizeof(escape_enc),
 		.expected = escape_text,
 	},
 };
@@ -89,13 +88,13 @@ int main(int argc, char** argv)
 	char result[256];
 
 	/* test 7-bit encoding */
-        for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
-		memset(coded, 0, sizeof(coded));
+	for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
+		memset(coded, 0x42, sizeof(coded));
 		length = gsm_7bit_encode(coded, test_encode[i].input);
 
 		if (length != test_encode[i].expected_length) {
-			fprintf(stderr, "Failed to encode case %d. Got %d\n",
-				i, length);
+			fprintf(stderr, "Failed to encode case %d. Got %d, expected %d\n",
+				i, length, test_encode[i].expected_length);
 			return -1;
 		}
 
@@ -108,7 +107,7 @@ int main(int argc, char** argv)
 
 	/* test 7-bit decoding */
 	for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
-		memset(result, 0, sizeof(coded));
+		memset(result, 0x42, sizeof(coded));
 		gsm_7bit_decode(result, test_decode[i].input,
 				test_decode[i].input_length);
 
-- 
1.7.1


--7JfCtLOvnd9MIVvH--




More information about the OpenBSC mailing list