From: David Laight <david.laight.linux@gmail.com>
To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Cc: David Laight <david.laight.linux@gmail.com>,
Arnd Bergmann <arnd@arndb.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Christophe Leroy <christophe.leroy@c-s.fr>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
nnac123@linux.ibm.com, horms@kernel.org
Subject: [PATCH next 8/8] test_hexdump: Test all 256 byte values
Date: Sat, 8 Mar 2025 09:34:52 +0000 [thread overview]
Message-ID: <20250308093452.3742-9-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20250308093452.3742-1-david.laight.linux@gmail.com>
Pass the hex data buffer as a parameter to test_hexdump_prepare_test()
and test_hexdump().
Add additional tests that pass all 256 byte values requesting 'ascii'.
Checks that unprintable characters are concerted to '.'.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
lib/test_hexdump.c | 46 ++++++++++++++++++++++++++++++++--------------
1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
index 877033a570d4..d2ed1870f869 100644
--- a/lib/test_hexdump.c
+++ b/lib/test_hexdump.c
@@ -21,9 +21,9 @@ static const unsigned char data_b[] = {
static unsigned total_tests __initdata;
static unsigned failed_tests __initdata;
-static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
- size_t groupsize, char *test,
- size_t testlen, bool ascii)
+static size_t __init test_hexdump_prepare_test(const unsigned char *buf, size_t len,
+ size_t rowsize, size_t groupsize,
+ char *test, size_t testlen, bool ascii)
{
char *p;
size_t byteswap, i, j;
@@ -54,7 +54,7 @@ static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
/* hex dump */
p = test;
for (i = 0, j = 0; i < len; i++) {
- unsigned char b = data_b[i ^ byteswap];
+ unsigned char b = buf[i ^ byteswap];
*p++ = "0123456789abcdef"[b >> 4];
*p++ = "0123456789abcdef"[b & 15];
if (++j == groupsize) {
@@ -71,7 +71,7 @@ static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
} while (p < test + rowsize * 2 + rowsize / groupsize + 1);
for (i = 0; i < len; i++) {
- unsigned char b = data_b[i];
+ unsigned char b = buf[i];
*p++ = (isascii(b) && isprint(b)) ? b : '.';
}
}
@@ -82,8 +82,8 @@ static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
#define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1)
-static void __init test_hexdump(size_t len, size_t rowsize, size_t groupsize,
- bool ascii)
+static void __init test_hexdump(const void *buf, size_t len, size_t rowsize,
+ size_t groupsize, bool ascii)
{
char test[TEST_HEXDUMP_BUF_SIZE];
char real[TEST_HEXDUMP_BUF_SIZE];
@@ -91,11 +91,11 @@ static void __init test_hexdump(size_t len, size_t rowsize, size_t groupsize,
total_tests++;
memset(real, FILL_CHAR, sizeof(real));
- hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
+ hex_dump_to_buffer(buf, len, rowsize, groupsize, real, sizeof(real),
ascii);
memset(test, FILL_CHAR, sizeof(test));
- test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
+ test_hexdump_prepare_test(buf, len, rowsize, groupsize, test, sizeof(test),
ascii);
if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
@@ -111,10 +111,10 @@ static void __init test_hexdump_set(size_t rowsize, bool ascii)
size_t len;
for (len = 1; len <= rowsize; len++) {
- test_hexdump(len, rowsize, 4, ascii);
- test_hexdump(len, rowsize, 2, ascii);
- test_hexdump(len, rowsize, 8, ascii);
- test_hexdump(len, rowsize, 1, ascii);
+ test_hexdump(data_b, len, rowsize, 4, ascii);
+ test_hexdump(data_b, len, rowsize, 2, ascii);
+ test_hexdump(data_b, len, rowsize, 8, ascii);
+ test_hexdump(data_b, len, rowsize, 1, ascii);
}
}
@@ -136,7 +136,7 @@ static bool __init test_hexdump_overflow(size_t buflen, size_t len,
buflen, ascii);
/* Test output is generated into a 'long enough' buffer */
- expected = test_hexdump_prepare_test(len, rowsize, groupsize, test,
+ expected = test_hexdump_prepare_test(data_b, len, rowsize, groupsize, test,
sizeof(test), ascii);
f = min(expected + 1, buflen);
@@ -175,6 +175,22 @@ static void __init test_hexdump_overflow_set(size_t rowsize, bool ascii)
}
}
+static void __init test_hexdump_bytes(void)
+{
+ unsigned char buf[16];
+ size_t i, j;
+
+ for (i = 0; i < 256; i += 16) {
+ for (j = 0; j < 16; j++)
+ buf[j] = i + j;
+ test_hexdump(buf, 16, 16, 1, true);
+ test_hexdump(buf, 16, 16, 2, true);
+ test_hexdump(buf, 16, 16, 4, true);
+ test_hexdump(buf, 16, 16, 8, true);
+ }
+}
+
+
static int __init test_hexdump_init(void)
{
size_t rowsize;
@@ -189,6 +205,8 @@ static int __init test_hexdump_init(void)
test_hexdump_overflow_set(32, false);
test_hexdump_overflow_set(32, true);
+ test_hexdump_bytes();
+
if (failed_tests == 0)
pr_info("all %u tests passed\n", total_tests);
else
--
2.39.5
prev parent reply other threads:[~2025-03-08 9:35 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-08 9:34 [PATCH next 0/8] test_hexdump: Improve test coverage David Laight
2025-03-08 9:34 ` [PATCH next 1/8] test_hexdump: Change rowsize and groupsize to size_t David Laight
2025-03-10 8:28 ` Andy Shevchenko
2025-03-08 9:34 ` [PATCH next 2/8] test_hexdump: Create test output data from the binary input data buffer David Laight
2025-03-10 8:31 ` Andy Shevchenko
2025-03-12 19:28 ` David Laight
2025-03-12 19:36 ` Andy Shevchenko
2025-03-13 9:17 ` David Laight
2025-03-08 9:34 ` [PATCH next 3/8] test_hexdump: Use longer variable names David Laight
2025-03-08 9:34 ` [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer David Laight
2025-03-10 9:01 ` Andy Shevchenko
2025-03-12 19:37 ` David Laight
2025-03-08 9:34 ` [PATCH next 5/8] test_hexdump: Fix sample output if zero bytes requested David Laight
2025-03-10 9:03 ` Andy Shevchenko
2025-03-12 19:40 ` David Laight
2025-03-08 9:34 ` [PATCH next 6/8] test_hexdump: If a test fails print all the parameters David Laight
2025-03-08 9:34 ` [PATCH next 7/8] test_hexdump: Run fixed not random tests David Laight
2025-03-08 9:34 ` David Laight [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250308093452.3742-9-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=christophe.leroy@c-s.fr \
--cc=horms@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=nnac123@linux.ibm.com \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox