diff options
author | Neels Hofmeyr <neels@hofmeyr.de> | 2019-01-14 23:32:53 +0100 |
---|---|---|
committer | Neels Hofmeyr <nhofmeyr@sysmocom.de> | 2019-01-28 23:58:53 +0000 |
commit | 0423b61aa8dd75b3141e13cf9276b8fa14ceb473 (patch) | |
tree | 8437f60dc6f6f817f96f3118db61bf91383cbd7a /tests/utils | |
parent | d01ef75ab876d79c9a0a73cdefb4ccfc60bb47f8 (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')
-rw-r--r-- | tests/utils/utils_test.c | 25 | ||||
-rw-r--r-- | tests/utils/utils_test.ok | 97 |
2 files changed, 122 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) diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok index 65e32ed2..8d7ced83 100644 --- a/tests/utils/utils_test.ok +++ b/tests/utils/utils_test.ok @@ -1,8 +1,105 @@ Plain dump 00 01 02 03 +00010203 Corner case 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "[delim]", false) + = "00[delim]01[delim]02[delim]03" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "[delim]", true) + = "00[delim]01[delim]02[delim]03[delim]" +osmo_hexdump_buf(buf, 6, data, 4, "[delim]", false) + = "" +osmo_hexdump_buf(buf, 7, data, 4, "[delim]", false) + = "" +osmo_hexdump_buf(buf, 8, data, 4, "[delim]", false) + = "" +osmo_hexdump_buf(buf, 6, data, 4, "[delim]", true) + = "" +osmo_hexdump_buf(buf, 7, data, 4, "[delim]", true) + = "" +osmo_hexdump_buf(buf, 8, data, 4, "[delim]", true) + = "" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, " ", false) + = "00 01 02 03" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, " ", true) + = "00 01 02 03 " +osmo_hexdump_buf(buf, 6, data, 4, " ", false) + = "00 " +osmo_hexdump_buf(buf, 7, data, 4, " ", false) + = "00 01 " +osmo_hexdump_buf(buf, 8, data, 4, " ", false) + = "00 01 " +osmo_hexdump_buf(buf, 6, data, 4, " ", true) + = "00 " +osmo_hexdump_buf(buf, 7, data, 4, " ", true) + = "00 01 " +osmo_hexdump_buf(buf, 8, data, 4, " ", true) + = "00 01 " +osmo_hexdump_buf(buf, sizeof(buf), data, 4, ":", false) + = "00:01:02:03" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, ":", true) + = "00:01:02:03:" +osmo_hexdump_buf(buf, 6, data, 4, ":", false) + = "00:" +osmo_hexdump_buf(buf, 7, data, 4, ":", false) + = "00:01:" +osmo_hexdump_buf(buf, 8, data, 4, ":", false) + = "00:01:" +osmo_hexdump_buf(buf, 6, data, 4, ":", true) + = "00:" +osmo_hexdump_buf(buf, 7, data, 4, ":", true) + = "00:01:" +osmo_hexdump_buf(buf, 8, data, 4, ":", true) + = "00:01:" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "::", false) + = "00::01::02::03" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "::", true) + = "00::01::02::03::" +osmo_hexdump_buf(buf, 6, data, 4, "::", false) + = "00::" +osmo_hexdump_buf(buf, 7, data, 4, "::", false) + = "00::" +osmo_hexdump_buf(buf, 8, data, 4, "::", false) + = "00::" +osmo_hexdump_buf(buf, 6, data, 4, "::", true) + = "00::" +osmo_hexdump_buf(buf, 7, data, 4, "::", true) + = "00::" +osmo_hexdump_buf(buf, 8, data, 4, "::", true) + = "00::" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "", false) + = "00010203" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, "", true) + = "00010203" +osmo_hexdump_buf(buf, 6, data, 4, "", false) + = "0001" +osmo_hexdump_buf(buf, 7, data, 4, "", false) + = "000102" +osmo_hexdump_buf(buf, 8, data, 4, "", false) + = "000102" +osmo_hexdump_buf(buf, 6, data, 4, "", true) + = "0001" +osmo_hexdump_buf(buf, 7, data, 4, "", true) + = "000102" +osmo_hexdump_buf(buf, 8, data, 4, "", true) + = "000102" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, NULL, false) + = "00010203" +osmo_hexdump_buf(buf, sizeof(buf), data, 4, NULL, true) + = "00010203" +osmo_hexdump_buf(buf, 6, data, 4, NULL, false) + = "0001" +osmo_hexdump_buf(buf, 7, data, 4, NULL, false) + = "000102" +osmo_hexdump_buf(buf, 8, data, 4, NULL, false) + = "000102" +osmo_hexdump_buf(buf, 6, data, 4, NULL, true) + = "0001" +osmo_hexdump_buf(buf, 7, data, 4, NULL, true) + = "000102" +osmo_hexdump_buf(buf, 8, data, 4, NULL, true) + = "000102" Hexparse 0..255 in lower case rc = 256 |