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
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index a773b3f..822861f 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 @@
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)