diff options
author | Nico Golde <nico@ngolde.de> | 2010-07-20 15:43:58 +0200 |
---|---|---|
committer | Holger Hans Peter Freyther <zecke@selfish.org> | 2010-07-21 02:43:58 +0800 |
commit | c0ce9aa20d6ad6095c9a11d00bbd14d200a7ee4e (patch) | |
tree | 28c08d07d2690957acc604255a56e1dc1ddd65a5 | |
parent | 446bf376984c0ab0453ec79577b590225e36d991 (diff) |
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
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.
-rw-r--r-- | src/gsm_utils.c | 10 | ||||
-rw-r--r-- | 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 3a378ace..1a7ba0e4 100644 --- a/src/gsm_utils.c +++ b/src/gsm_utils.c @@ -51,19 +51,21 @@ int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length) { int i = 0; int l = 0; - uint8_t *rtext = calloc(length, sizeof(uint8_t)); + int septet_l = (length * 8) / 7; + uint8_t *rtext = calloc(septet_l + 1, 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 3742dd88..9d87b5bc 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); |