From 1d7f3b5eb27154b5d1ef698b40edfb9f74a9d4d2 Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Mon, 12 Aug 2013 17:07:53 +0200 Subject: sms: Added result buffer size parameter to 7bit conv funs The 7bit<->8bit encoding/decoding functions didn't check whether there is still enough space in the destination buffer. Therefore a buffer size parameter has been added to each of the functions which is used to truncate the output if the buffer is too small. In addition, the return value of the decoding functions has been changed to number of characters written (excluding \0), so this value is always equal to strlen(decoded). The old functions are still available as wrapper functions. --- tests/ussd/ussd_test.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'tests/ussd/ussd_test.c') diff --git a/tests/ussd/ussd_test.c b/tests/ussd/ussd_test.c index e3e8e08a..b04f8e8a 100644 --- a/tests/ussd/ussd_test.c +++ b/tests/ussd/ussd_test.c @@ -73,20 +73,42 @@ static void test_7bit_ussd(const char *text, const char *encoded_hex, const char { uint8_t coded[256]; char decoded[256]; - int y; + int octets_written; + int buffer_size; + int nchars; printf("original = %s\n", osmo_hexdump((uint8_t *)text, strlen(text))); - gsm_7bit_encode_ussd(coded, text, &y); - printf("encoded = %s\n", osmo_hexdump(coded, y)); + gsm_7bit_encode_n_ussd(coded, sizeof(coded), text, &octets_written); + printf("encoded = %s\n", osmo_hexdump(coded, octets_written)); - OSMO_ASSERT(strcmp(encoded_hex, osmo_hexdump_nospc(coded, y)) == 0); + OSMO_ASSERT(strcmp(encoded_hex, osmo_hexdump_nospc(coded, octets_written)) == 0); - gsm_7bit_decode_ussd(decoded, coded, y * 8 / 7); - y = strlen(decoded); - printf("decoded = %s\n\n", osmo_hexdump((uint8_t *)decoded, y)); + gsm_7bit_decode_n_ussd(decoded, sizeof(decoded), coded, octets_written * 8 / 7); + octets_written = strlen(decoded); + printf("decoded = %s\n\n", osmo_hexdump((uint8_t *)decoded, octets_written)); OSMO_ASSERT(strncmp(text, decoded, strlen(text)) == 0); OSMO_ASSERT(strcmp(appended_after_decode, decoded + strlen(text)) == 0); + + /* check buffer limiting */ + memset(decoded, 0xaa, sizeof(decoded)); + + for (buffer_size = 1; buffer_size < sizeof(decoded) - 1; ++buffer_size) + { + nchars = gsm_7bit_decode_n_ussd(decoded, buffer_size, coded, octets_written * 8 / 7); + OSMO_ASSERT(nchars <= buffer_size); + OSMO_ASSERT(decoded[buffer_size] == (char)0xaa); + OSMO_ASSERT(decoded[nchars] == '\0'); + } + + memset(coded, 0xaa, sizeof(coded)); + + for (buffer_size = 0; buffer_size < sizeof(coded) - 1; ++buffer_size) + { + gsm_7bit_encode_n_ussd(coded, buffer_size, text, &octets_written); + OSMO_ASSERT(octets_written <= buffer_size); + OSMO_ASSERT(coded[buffer_size] == 0xaa); + } } int main(int argc, char **argv) @@ -94,6 +116,7 @@ int main(int argc, char **argv) struct ussd_request req; const int size = sizeof(ussd_request); int i; + struct msgb *msg; osmo_init_logging(&info); @@ -122,5 +145,18 @@ int main(int argc, char **argv) test_7bit_ussd("0123456\r", "b0986c46abd91a0d", "\r"); test_7bit_ussd("012345\r", "b0986c46ab351a", ""); + printf("Checking GSM 04.80 USSD message generation.\n"); + + test_7bit_ussd("", "", ""); + msg = gsm0480_create_unstructuredSS_Notify (0x00, ""); + printf ("Created unstructuredSS_Notify (0x00): %s\n", + osmo_hexdump(msgb_data(msg), msgb_length(msg))); + msgb_free (msg); + + test_7bit_ussd("forty-two", "e6b79c9e6fd1ef6f", ""); + msg = gsm0480_create_unstructuredSS_Notify (0x42, "forty-two"); + printf ("Created unstructuredSS_Notify (0x42): %s\n", + osmo_hexdump(msgb_data(msg), msgb_length(msg))); + msgb_free (msg); return 0; } -- cgit v1.2.3