diff options
author | Philipp Maier <pmaier@sysmocom.de> | 2017-10-10 16:53:21 +0200 |
---|---|---|
committer | Neels Hofmeyr <nhofmeyr@sysmocom.de> | 2017-10-22 20:09:26 +0000 |
commit | c5b47cc03200c983981ac4b8de20fb0e26d4f873 (patch) | |
tree | 9072d96478518ce0e5e16be537a746ab9b429620 /tests | |
parent | fcf81b5deb8d02ba45907e90bb6668c67986a028 (diff) |
add function msgb_printf() to print formatted text into msg buf
In ASCII string based protocols it a printf() version that prints
directly to the message buffer may be useful.
Add function msgb_printf(), make sure that msg buffer bounderies
are not exceeded. If the end of the tail buffer is hit, return
with an error code.
Change-Id: I15e1af68616309555d0ed9ac5da027c9833d42e3
Diffstat (limited to 'tests')
-rw-r--r-- | tests/msgb/msgb_test.c | 105 | ||||
-rw-r--r-- | tests/msgb/msgb_test.ok | 8 |
2 files changed, 113 insertions, 0 deletions
diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c index ac103829..05335467 100644 --- a/tests/msgb/msgb_test.c +++ b/tests/msgb/msgb_test.c @@ -277,6 +277,110 @@ static void test_msgb_resize_area() osmo_set_panic_handler(NULL); } +static void test_msgb_printf() +{ + struct msgb *msg; + struct msgb *msg_ref; + int rc; + int total_len; + + msg = msgb_alloc(80, "data"); + + /* Add normal text: */ + printf("Add normal text:\n"); + rc = msgb_printf(msg, "|this is a test %i, %s, %16x|", 4711, "testme", + 0x4711); + total_len = msgb_length(msg); + printf("#1: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(msgb_tailroom(msg) == 33); + + /* Add normal text: */ + rc = msgb_printf(msg, "|some more text|"); + total_len = msgb_length(msg); + printf("#2: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(msgb_tailroom(msg) == 17); + + /* Add normal text which will not fit: */ + rc = msgb_printf(msg, "|more %i %x %s|", 23, 0xfee, + "text will not fit"); + total_len = msgb_length(msg); + printf("#3: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == -EINVAL); + + /* Check if we got the right amount of characters in the message buffer + * until here, so that we can be sure that the following cornercase + * tests yield plausible results */ + OSMO_ASSERT(msgb_tailroom(msg) == 17); + + /* Add normal text which just does not fit by one character, this should not + * alter the message buffers tail pointer */ + rc = msgb_printf(msg, "|more 123456 ABC|"); + total_len = msgb_length(msg); + printf("#4: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == -EINVAL); + + /* Make sure the tailroom, nor the contained string length did change */ + OSMO_ASSERT(msgb_tailroom(msg) == 17); + OSMO_ASSERT(msgb_length(msg) == 63); + + /* Add normal text which just fits */ + rc = msgb_printf(msg, "|more 123456 AB|"); + total_len = msgb_length(msg); + printf("#5: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == 0); + + /* Make sure that the string in the bufer takes up all available Space. + * Also make sure that the available tailroom has space for one byte, + * which actually holds the string terminator */ + OSMO_ASSERT(msgb_tailroom(msg) == 1); + OSMO_ASSERT(msgb_length(msg) == 79); + + /* Try to add a nullstring to the already full buffer, this should + * be ok, since we still have one byte tailroom */ + rc = msgb_printf(msg, ""); + total_len = msgb_length(msg); + printf("#6: rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, + msg->data); + OSMO_ASSERT(rc == 0); + + /* Make sure that we still have the same conditions as before */ + OSMO_ASSERT(msgb_tailroom(msg) == 1); + OSMO_ASSERT(msgb_length(msg) == 79); + + msgb_free(msg); + + /* Test behaviour when a completely full buffer is passed to + * msgb_printf(). We should get rc == -EINVAL and the message + * buffer must not be changed at all */ + msg = msgb_alloc(15, "data"); + msg_ref = msgb_alloc(msgb_tailroom(msg), "data"); + memset(msg->data, 0x41, msgb_tailroom(msg)); + memset(msg_ref->data, 0x41, msgb_tailroom(msg_ref)); + msgb_put(msg, msgb_tailroom(msg)); + msgb_put(msg_ref, msgb_tailroom(msg_ref)); + + printf("#7: before: %s", msgb_hexdump(msg)); + rc = msgb_printf(msg, "ABCDEF"); + printf(" after: rc=%i, %s", rc, msgb_hexdump(msg)); + if (memcmp(msg->data, msg_ref->data, msgb_length(msg)) == 0) + printf(" ==> ok, no change\n"); + else + printf(" ==> error, change detected\n"); + OSMO_ASSERT(rc == -EINVAL); + OSMO_ASSERT(msgb_tailroom(msg) == 0); + OSMO_ASSERT(msgb_length(msg) == msgb_length(msg_ref)); + + msgb_free(msg); + msgb_free(msg_ref); +} + static struct log_info info = {}; int main(int argc, char **argv) @@ -287,6 +391,7 @@ int main(int argc, char **argv) test_msgb_api_errors(); test_msgb_copy(); test_msgb_resize_area(); + test_msgb_printf(); printf("Success.\n"); diff --git a/tests/msgb/msgb_test.ok b/tests/msgb/msgb_test.ok index 6603fe71..826c809c 100644 --- a/tests/msgb/msgb_test.ok +++ b/tests/msgb/msgb_test.ok @@ -32,4 +32,12 @@ Testing msgb_resize_area Original: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f Extended: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f Shrinked: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f +Add normal text: +#1: rc=0, total_len=47, msg->data=|this is a test 4711, testme, 4711| +#2: rc=0, total_len=63, msg->data=|this is a test 4711, testme, 4711||some more text| +#3: rc=-22, total_len=63, msg->data=|this is a test 4711, testme, 4711||some more text||more 23 fee tex +#4: rc=-22, total_len=63, msg->data=|this is a test 4711, testme, 4711||some more text||more 123456 ABC +#5: rc=0, total_len=79, msg->data=|this is a test 4711, testme, 4711||some more text||more 123456 AB| +#6: rc=0, total_len=79, msg->data=|this is a test 4711, testme, 4711||some more text||more 123456 AB| +#7: before: 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 after: rc=-22, 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 ==> ok, no change Success. |