summaryrefslogtreecommitdiffstats
path: root/tests/utils/utils_test.c
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-01-14 23:32:53 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2019-01-28 23:58:53 +0000
commit0423b61aa8dd75b3141e13cf9276b8fa14ceb473 (patch)
tree8437f60dc6f6f817f96f3118db61bf91383cbd7a /tests/utils/utils_test.c
parentd01ef75ab876d79c9a0a73cdefb4ccfc60bb47f8 (diff)
add osmo_hexdump_buf() and test
Add osmo_hexdump_buf() as an all-purpose hexdump function, which all other osmo_hexdump_*() implementations now call. It absorbs the static _osmo_hexdump(). Add tests for osmo_hexdump_buf(). Rationale: recently during patch review, a situation came up where two hexdumps in a single printf would have been useful. Now I've faced a similar situation again, in ongoing development. So I decided it is time to provide this API. The traditional osmo_hexdump() API returns a non-const char*, which should probably have been a const instead. Particularly this new function may return a string constant "" if the buf is NULL or empty, so return const char*. That is why the older implementations calling osmo_hexdump_buf() separately return the buffer instead of the const return value directly. Change-Id: I590595567b218b24e53c9eb1fd8736c0324d371d
Diffstat (limited to 'tests/utils/utils_test.c')
-rw-r--r--tests/utils/utils_test.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index a773b3f5..822861fb 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -37,6 +37,7 @@
static void hexdump_test(void)
{
uint8_t data[4098];
+ char buf[256];
int i;
for (i = 0; i < ARRAY_SIZE(data); ++i)
@@ -44,10 +45,34 @@ static void hexdump_test(void)
printf("Plain dump\n");
printf("%s\n", osmo_hexdump(data, 4));
+ printf("%s\n", osmo_hexdump_nospc(data, 4));
printf("Corner case\n");
printf("%s\n", osmo_hexdump(data, ARRAY_SIZE(data)));
printf("%s\n", osmo_hexdump_nospc(data, ARRAY_SIZE(data)));
+
+#define _HEXDUMP_BUF_TEST(SIZE, DELIM, DELIM_AFTER) \
+ buf[0] = '!'; \
+ buf[1] = '\0'; \
+ printf("osmo_hexdump_buf(buf, " #SIZE ", data, 4, %s, " #DELIM_AFTER ")\n = \"%s\"\n", \
+ DELIM ? #DELIM : "NULL", \
+ osmo_hexdump_buf(buf, SIZE, data, 4, DELIM, DELIM_AFTER))
+#define HEXDUMP_BUF_TEST(DELIM) \
+ _HEXDUMP_BUF_TEST(sizeof(buf), DELIM, false); \
+ _HEXDUMP_BUF_TEST(sizeof(buf), DELIM, true); \
+ _HEXDUMP_BUF_TEST(6, DELIM, false); \
+ _HEXDUMP_BUF_TEST(7, DELIM, false); \
+ _HEXDUMP_BUF_TEST(8, DELIM, false); \
+ _HEXDUMP_BUF_TEST(6, DELIM, true); \
+ _HEXDUMP_BUF_TEST(7, DELIM, true); \
+ _HEXDUMP_BUF_TEST(8, DELIM, true)
+
+ HEXDUMP_BUF_TEST("[delim]");
+ HEXDUMP_BUF_TEST(" ");
+ HEXDUMP_BUF_TEST(":");
+ HEXDUMP_BUF_TEST("::");
+ HEXDUMP_BUF_TEST("");
+ HEXDUMP_BUF_TEST(NULL);
}
static void hexparse_test(void)