Skip to contentSkip to conversation

hexdump: update test suite

PATCH Superseded Andy Shevchenko 3 participants 23 messages Nov 11, 2015 – Nov 26, 2015 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hexdump: update test suite

The test suite currently doesn't cover many corner cases when hex_dump_to_buffer() runs into overflow. Refactor and amend test suite to cover most of the cases.

Andy Shevchenko (7):
test_hexdump: rename to test_hexdump
test_hexdump: introduce test_hexdump_prepare_test() helper
test_hexdump: go through all possible lengths of buffer
test_hexdump: replace magic numbers by their meaning
test_hexdump: check all bytes in real buffer
test_hexdump: test all possible group sizes for overflow
test_hexdump: print statistics at the end

 lib/Makefile                           |   2 +-
 lib/{test-hexdump.c => test_hexdump.c} | 120 ++++++++++++++++++++++-----------
 2 files changed, 83 insertions(+), 39 deletions(-)
 rename lib/{test-hexdump.c => test_hexdump.c} (57%)
Signature · 1 line
2.6.2
#1 lore raw

PATCH 1/7 test_hexdump: rename to test_hexdump

Just to follow the scheme that most of the test modules are using.

There is no fuctional change.

---
 lib/Makefile                           | 2 +-
 lib/{test-hexdump.c => test_hexdump.c} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename lib/{test-hexdump.c => test_hexdump.c} (100%)
lib/Makefile +1 -1
index 5f4a9e8..64f834a 100644
@@ -32,7 +32,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
 obj-y += string_helpers.o
 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
 obj-y += hexdump.o
-obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o
+obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
 obj-y += kstrtox.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
 obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
lib/test_hexdump.c renamed from lib/test-hexdump.c +0 -0
similarity index 100%
rename from lib/test-hexdump.c
rename to lib/test_hexdump.c
Signature · 1 line
2.6.2
Acked-by on v1 1/7
#2 lore raw

Re: [PATCH v1 1/7] test_hexdump: rename to test_hexdump

Quoted 3 lines · Andy Shevchenko

Just to follow the scheme that most of the test modules are using.

There is no fuctional change.

Yeah, this has also been bugging me slightly.

#3 lore raw

PATCH 2/7 test_hexdump: introduce test_hexdump_prepare_test() helper

In reply to Andy Shevchenko

The function prepares the expected result in the provided buffer.

---
 lib/test_hexdump.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/test_hexdump.c +19 -7
index 5241df3..ed7c6a7 100644
@@ -42,19 +42,16 @@ static const char * const test_data_8_le[] __initconst = {
 	"e9ac0f9cad319ca6", "0cafb1439919d14c",
 };
 
-static void __init test_hexdump(size_t len, int rowsize, int groupsize,
-				bool ascii)
+static void __init test_hexdump_prepare_test(size_t len, int rowsize,
+					     int groupsize, char *test,
+					     size_t testlen, bool ascii)
 {
-	char test[32 * 3 + 2 + 32 + 1];
-	char real[32 * 3 + 2 + 32 + 1];
 	char *p;
 	const char * const *result;
 	size_t l = len;
 	int gs = groupsize, rs = rowsize;
 	unsigned int i;
 
-	hex_dump_to_buffer(data_b, l, rs, gs, real, sizeof(real), ascii);
-
 	if (rs != 16 && rs != 32)
 		rs = 16;
 
@@ -73,7 +70,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 	else
 		result = test_data_1_le;
 
-	memset(test, ' ', sizeof(test));
+	memset(test, ' ', testlen);
 
 	/* hex dump */
 	p = test;
@@ -95,6 +92,21 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 	}
 
 	*p = '\0';
+}
+
+#define TEST_HEXDUMP_BUF_SIZE		(32 * 3 + 2 + 32 + 1)
+
+static void __init test_hexdump(size_t len, int rowsize, int groupsize,
+				bool ascii)
+{
+	char test[TEST_HEXDUMP_BUF_SIZE];
+	char real[TEST_HEXDUMP_BUF_SIZE];
+
+	hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
+			   ascii);
+
+	test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
+				  ascii);
 
 	if (strcmp(test, real)) {
 		pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
Signature · 1 line
2.6.2
Acked-by on v1 2/7
#4 lore raw

Re: [PATCH v1 2/7] test_hexdump: introduce test_hexdump_prepare_test() helper

Quoted 6 lines · Andy Shevchenko

The function prepares the expected result in the provided buffer.

---
 lib/test_hexdump.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

Makes sense. It does improve readability slightly.

#5 lore raw

PATCH 3/7 test_hexdump: go through all possible lengths of buffer

In reply to Andy Shevchenko

When test for overflow do iterate the buffer length in a range 0 .. BUF_SIZE.

---
 lib/test_hexdump.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
lib/test_hexdump.c +10 -10
index ed7c6a7..15a6440 100644
@@ -126,17 +126,17 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 	test_hexdump(len, rowsize, 1, ascii);
 }
 
-static void __init test_hexdump_overflow(bool ascii)
+static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
-	char buf[56];
+	char buf[TEST_HEXDUMP_BUF_SIZE];
 	const char *t = test_data_1_le[0];
-	size_t l = get_random_int() % sizeof(buf);
+	size_t l = buflen;
 	bool a;
 	int e, r;
 
 	memset(buf, ' ', sizeof(buf));
 
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
+	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen, ascii);
 
 	if (ascii)
 		e = 50;
@@ -144,7 +144,7 @@ static void __init test_hexdump_overflow(bool ascii)
 		e = 2;
 	buf[e + 2] = '\0';
 
-	if (!l) {
+	if (!buflen) {
 		a = r == e && buf[0] == ' ';
 	} else if (l < 3) {
 		a = r == e && buf[0] == '\0';
@@ -160,7 +160,7 @@ static void __init test_hexdump_overflow(bool ascii)
 	}
 
 	if (!a) {
-		pr_err("Len: %zu rc: %u strlen: %zu\n", l, r, strlen(buf));
+		pr_err("Len: %zu rc: %u strlen: %zu\n", buflen, r, strlen(buf));
 		pr_err("Result: '%s'\n", buf);
 	}
 }
@@ -180,11 +180,11 @@ static int __init test_hexdump_init(void)
 	for (i = 0; i < 16; i++)
 		test_hexdump_set(rowsize, true);
 
-	for (i = 0; i < 16; i++)
-		test_hexdump_overflow(false);
+	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
+		test_hexdump_overflow(i, false);
 
-	for (i = 0; i < 16; i++)
-		test_hexdump_overflow(true);
+	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
+		test_hexdump_overflow(i, true);
 
 	return -EINVAL;
 }
Signature · 1 line
2.6.2
#6 lore raw

Re: [PATCH v1 3/7] test_hexdump: go through all possible lengths of buffer

Quoted 35 lines · Andy Shevchenko

When test for overflow do iterate the buffer length in a range 0 .. BUF_SIZE.

---
 lib/test_hexdump.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
lib/test_hexdump.c +4 -4
index ed7c6a7..15a6440 100644
@@ -126,17 +126,17 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 	test_hexdump(len, rowsize, 1, ascii);
 }
 
-static void __init test_hexdump_overflow(bool ascii)
+static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
-	char buf[56];
+	char buf[TEST_HEXDUMP_BUF_SIZE];
 	const char *t = test_data_1_le[0];
-	size_t l = get_random_int() % sizeof(buf);
+	size_t l = buflen;
 	bool a;
 	int e, r;
 
 	memset(buf, ' ', sizeof(buf));
 
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
+	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen, ascii);
 
 	if (ascii)
 		e = 50;

lib/test_hexdump.c static void __init test_hexdump_overflow(bool ascii) · line 150

Quoted 8 lines
lib/test_hexdump.c +1 -1
@@ -144,7 +144,7 @@ static void __init test_hexdump_overflow(bool ascii)
 		e = 2;
 	buf[e + 2] = '\0';
 
-	if (!l) {
+	if (!buflen) {
 		a = r == e && buf[0] == ' ';
 	} else if (l < 3) {
 		a = r == e && buf[0] == '\0';

Why keep the variable l when it is just a synonym for the new parameter buflen? It is quite confusing that you change some but not all occurrences of l to buflen. If you want to make the diff minimal but still have a descriptive parameter name, just keep the 'size_t l = buflen;' assignment and don't otherwise refer to buflen. But I think it's better to eliminate 'l' and just change everything to buflen. Don't mix the two approaches, though.

Rasmus

#7 lore raw

Re: [PATCH v1 3/7] test_hexdump: go through all possible lengths of buffer

Quoted 52 lines · Rasmus Villemoes
Quoted 44 lines · Andy Shevchenko

When test for overflow do iterate the buffer length in a range 0 .. BUF_SIZE.

---
 lib/test_hexdump.c | 20 ++++++++++----------

 1 file changed, 10 insertions(+), 10 deletions(-)
lib/test_hexdump.c +0 -0
index ed7c6a7..15a6440 100644
@@ -126,17 +126,17 @@ static void __init test_hexdump_set(int
 rowsize, bool ascii)
  	test_hexdump(len, rowsize, 1, ascii);
  }

lib/test_hexdump.c +1 -1
-static void __init test_hexdump_overflow(bool ascii)
+static void __init test_hexdump_overflow(size_t buflen, bool

ascii)
 {

lib/test_hexdump.c +1 -1
-	char buf[56];
+	char buf[TEST_HEXDUMP_BUF_SIZE];

  const char *t = test_data_1_le[0];

lib/test_hexdump.c +1 -1
-	size_t l = get_random_int() % sizeof(buf);
+	size_t l = buflen;

  bool a;
  int e, r;

  memset(buf, ' ', sizeof(buf));

lib/test_hexdump.c +1 -1
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
+	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen,

ascii);

  if (ascii)
  e = 50;

lib/test_hexdump.c +0 -0
@@ -144,7 +144,7 @@ static void __init test_hexdump_overflow(bool
 ascii)
  		e = 2;
  	buf[e + 2] = '\0';

lib/test_hexdump.c +1 -1
-	if (!l) {
+	if (!buflen) {
Quoted 3 lines

  a = r == e && buf[0] == ' ';
  } else if (l < 3) {
  a = r == e && buf[0] == '\0';

Quoted 8 lines

Why keep the variable l when it is just a synonym for the new parameter buflen? It is quite confusing that you change some but not all occurrences of l to buflen. If you want to make the diff minimal but still have a descriptive parameter name, just keep the 'size_t l =
buflen;' assignment and don't otherwise refer to buflen. But I think it's better to eliminate 'l' and just change everything to buflen. Don't mix the two approaches, though.

Okay, I got it for the future, though the series is already in linux- next, so do we really need to re-hack half of it because of that?

I suppose everything else you noticed I may send as one follow up patch.

Quoted 2 lines

Rasmus

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#8 lore raw

Re: [PATCH v1 3/7] test_hexdump: go through all possible lengths of buffer

Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Rasmus Villemoes

Why keep the variable l when it is just a synonym for the new parameter buflen? It is quite confusing that you change some but not all occurrences of l to buflen. If you want to make the diff minimal but still have a descriptive parameter name, just keep the 'size_t l =
buflen;' assignment and don't otherwise refer to buflen. But I think it's better to eliminate 'l' and just change everything to buflen. Don't mix the two approaches, though.

Quoted 2 lines

Okay, I got it for the future, though the series is already in linux- next, so do we really need to re-hack half of it because of that?

Stuff in -next isn't set in stone. I'm pretty sure Andrew can replace one set of patches with another.

Rasmus

#9 lore raw

Re: [PATCH v1 3/7] test_hexdump: go through all possible lengths of buffer

Quoted 14 lines · Rasmus Villemoes
Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Rasmus Villemoes

Why keep the variable l when it is just a synonym for the new parameter buflen? It is quite confusing that you change some but not all occurrences of l to buflen. If you want to make the diff minimal but still have a descriptive parameter name, just keep the 'size_t l =
buflen;' assignment and don't otherwise refer to buflen. But I think it's better to eliminate 'l' and just change everything to buflen. Don't mix the two approaches, though.

Quoted 5 lines
Quoted 2 lines

Okay, I got it for the future, though the series is already in linux- next, so do we really need to re-hack half of it because of that?

Stuff in -next isn't set in stone. I'm pretty sure Andrew can replace one set of patches with another.

Thanks for your review. I;m busy currently with some other stuff, you might have noticed, I will return back to the series maybe next week.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH 4/7 test_hexdump: replace magic numbers by their meaning

In reply to Andy Shevchenko

The magic numbers of the length are converted to their actual meaning, such as end of the buffer with and without ASCII part.

We don't touch the rest magic constants that will be removed in the following commits.

---
 lib/test_hexdump.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/test_hexdump.c +9 -4
index 15a6440..a3e3b01 100644
@@ -130,18 +130,23 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
 	char buf[TEST_HEXDUMP_BUF_SIZE];
 	const char *t = test_data_1_le[0];
+	size_t len = 1;
 	size_t l = buflen;
+	int rs = 16, gs = 1;
+	int ae, he, e, r;
 	bool a;
-	int e, r;
 
 	memset(buf, ' ', sizeof(buf));
 
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen, ascii);
+	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
+
+	ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
+	he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
 
 	if (ascii)
-		e = 50;
+		e = ae;
 	else
-		e = 2;
+		e = he;
 	buf[e + 2] = '\0';
 
 	if (!buflen) {
Signature · 1 line
2.6.2
#11 lore raw

Re: [PATCH v1 4/7] test_hexdump: replace magic numbers by their meaning

Quoted 23 lines · Andy Shevchenko

The magic numbers of the length are converted to their actual meaning, such as end of the buffer with and without ASCII part.

We don't touch the rest magic constants that will be removed in the following commits.

---
 lib/test_hexdump.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/test_hexdump.c +3 -0
index 15a6440..a3e3b01 100644
@@ -130,18 +130,23 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
 	char buf[TEST_HEXDUMP_BUF_SIZE];
 	const char *t = test_data_1_le[0];
+	size_t len = 1;
 	size_t l = buflen;
+	int rs = 16, gs = 1;
+	int ae, he, e, r;

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 144

Quoted 10 lines
lib/test_hexdump.c +4 -2
 	bool a;
-	int e, r;
 
 	memset(buf, ' ', sizeof(buf));
 
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen, ascii);
+	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
+
+	ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
+	he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;

This computation seems to rely on len being an exact multiple of gs - which of course it is in this case. hex_dump_to_buffer also enforces it by setting groupsize to 1 when it's not the case, but that doesn't really help us here. So maybe an extra comment would be justified.

Rasmus

#12 lore raw

Re: [PATCH v1 4/7] test_hexdump: replace magic numbers by their meaning

Quoted 40 lines · Rasmus Villemoes
Quoted 37 lines · Andy Shevchenko

The magic numbers of the length are converted to their actual meaning, such as
end of the buffer with and without ASCII part.

We don't touch the rest magic constants that will be removed in the following
commits.

---
 lib/test_hexdump.c | 13 +++++++++----

 1 file changed, 9 insertions(+), 4 deletions(-)
lib/test_hexdump.c +3 -1
index 15a6440..a3e3b01 100644
@@ -130,18 +130,23 @@ static void __init
 test_hexdump_overflow(size_t buflen, bool ascii)
  {
  	char buf[TEST_HEXDUMP_BUF_SIZE];
  	const char *t = test_data_1_le[0];
+	size_t len = 1;
  	size_t l = buflen;
+	int rs = 16, gs = 1;
+	int ae, he, e, r;
  	bool a;
-	int e, r;


  memset(buf, ' ', sizeof(buf));

lib/test_hexdump.c +0 -1
-	r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen,

ascii);

lib/test_hexdump.c +1 -0
+	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen,

ascii);

lib/test_hexdump.c +2 -0
+
+	ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space
Quoted 8 lines
Quoted 3 lines

*/ + len /* ascii */;

lib/test_hexdump.c +1 -0
+	he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /*

no trailing space */;

This computation seems to rely on len being an exact multiple of gs - which of course it is in this case. hex_dump_to_buffer also enforces it by setting groupsize to 1 when it's not the case, but that doesn't really help us here. So maybe an extra comment would be justified.

Agree. A comment would be nice to have.

Quoted 2 lines

Rasmus

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#13 lore raw

PATCH 5/7 test_hexdump: check all bytes in real buffer

In reply to Andy Shevchenko

After processing by hex_dump_to_buffer() check all the parts to be expected.

Part 1. The actual expected hex dump with or without ASCII part.
This is provided by plain strcmp() call including check for the terminating NUL.

Part 2. Check if the buffer is dirty beyond needed.
We fill the buffer by ' ' (space) characters, so, we expect to have the tail of buffer will be left untouched. Check all bytes in the tail of the buffer.

Part 3. Return code should be as expected.

---
 lib/test_hexdump.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
lib/test_hexdump.c +16 -16
index a3e3b01..9b95b67 100644
@@ -128,10 +128,9 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 
 static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
+	char test[TEST_HEXDUMP_BUF_SIZE];
 	char buf[TEST_HEXDUMP_BUF_SIZE];
-	const char *t = test_data_1_le[0];
 	size_t len = 1;
-	size_t l = buflen;
 	int rs = 16, gs = 1;
 	int ae, he, e, r;
 	bool a;
@@ -147,26 +146,27 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 		e = ae;
 	else
 		e = he;
-	buf[e + 2] = '\0';
 
 	if (!buflen) {
-		a = r == e && buf[0] == ' ';
-	} else if (l < 3) {
-		a = r == e && buf[0] == '\0';
-	} else if (l < 4) {
-		a = r == e && !strcmp(buf, t);
-	} else if (ascii) {
-		if (l < 51)
-			a = r == e && buf[l - 1] == '\0' && buf[l - 2] == ' ';
-		else
-			a = r == e && buf[50] == '\0' && buf[49] == '.';
+		memset(test, ' ', sizeof(test));
+		test[sizeof(buf) - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf, ' ', sizeof(buf));
 	} else {
-		a = r == e && buf[e] == '\0';
+		int f = min_t(int, e + 1, buflen);
+
+		test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
+		test[f - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf + f, ' ', sizeof(buf) - f) && !strcmp(buf, test);
 	}
 
+	buf[sizeof(buf) - 1] = '\0';
+
 	if (!a) {
-		pr_err("Len: %zu rc: %u strlen: %zu\n", buflen, r, strlen(buf));
-		pr_err("Result: '%s'\n", buf);
+		pr_err("Len: %zu buflen: %zu strlen: %zu\n", len, buflen, strlen(buf));
+		pr_err("Result: %d '%s'\n", r, buf);
+		pr_err("Expect: %d '%s'\n", e, test);
 	}
 }
 
Signature · 1 line
2.6.2
#14 lore raw

Re: [PATCH v1 5/7] test_hexdump: check all bytes in real buffer

Quoted 10 lines · Andy Shevchenko

After processing by hex_dump_to_buffer() check all the parts to be expected.

Part 1. The actual expected hex dump with or without ASCII part.
This is provided by plain strcmp() call including check for the terminating NUL.

Part 2. Check if the buffer is dirty beyond needed.
We fill the buffer by ' ' (space) characters, so, we expect to have the tail of buffer will be left untouched. Check all bytes in the tail of the buffer.

First of all, ' ' is one of the characters which hexdump is certainly supposed to spit out, so I think it's better to use some other character for prefilling. Otherwise we wouldn't be able to detect a stray write of a space which wasn't properly guarded by a size check. I'd suggest '\xff' or any other non-ascii character (and make it a #define so that it's less magic).

Quoted 34 lines

Part 3. Return code should be as expected.

---
 lib/test_hexdump.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
lib/test_hexdump.c +1 -7
index a3e3b01..9b95b67 100644
@@ -128,10 +128,9 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 
 static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {
+	char test[TEST_HEXDUMP_BUF_SIZE];
 	char buf[TEST_HEXDUMP_BUF_SIZE];
-	const char *t = test_data_1_le[0];
 	size_t len = 1;
-	size_t l = buflen;
 	int rs = 16, gs = 1;
 	int ae, he, e, r;
 	bool a;
@@ -147,26 +146,27 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 		e = ae;
 	else
 		e = he;
-	buf[e + 2] = '\0';
 
 	if (!buflen) {
-		a = r == e && buf[0] == ' ';
-	} else if (l < 3) {
-		a = r == e && buf[0] == '\0';
-	} else if (l < 4) {

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 154

Quoted 10 lines
lib/test_hexdump.c +4 -6
-		a = r == e && !strcmp(buf, t);
-	} else if (ascii) {
-		if (l < 51)
-			a = r == e && buf[l - 1] == '\0' && buf[l - 2] == ' ';
-		else
-			a = r == e && buf[50] == '\0' && buf[49] == '.';
+		memset(test, ' ', sizeof(test));
+		test[sizeof(buf) - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf, ' ', sizeof(buf));

test and buf happen to have the same size, but "test[sizeof(buf) - 1] = '\0'" is rather odd. But you don't even seem to use test in this branch?

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 162

Quoted 9 lines
lib/test_hexdump.c +6 -1
 	} else {
-		a = r == e && buf[e] == '\0';
+		int f = min_t(int, e + 1, buflen);
+
+		test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
+		test[f - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf + f, ' ', sizeof(buf) - f) && !strcmp(buf, test);
 	}

There's also a bit of duplication in the !buflen and buflen branches. Why not pull the computation of f (the number of expected bytes written) outside and do

  f = min_t(int, e + 1, buflen);
  a = r == e && !memchr_inv(buf + f, ' ', sizeof(buf) - f);
  if (buflen) {
    test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
    test[f - 1] = '\0';
    a = a && !memcmp(buf, test, f);
  }

(I think it's better to use memcmp for "untrusted" buffers - if hexdump didn't make buf into a proper C string, it's a little fragile passing it to strcmp). This makes it obvious that the entire contents of buf is being tested.

Rasmus

#15 lore raw

Re: [PATCH v1 5/7] test_hexdump: check all bytes in real buffer

Quoted 17 lines · Rasmus Villemoes
Quoted 7 lines · Andy Shevchenko

After processing by hex_dump_to_buffer() check all the parts to be expected.

Part 1. The actual expected hex dump with or without ASCII part.
This is provided by plain strcmp() call including check for the terminating NUL.

Quoted 6 lines

Part 2. Check if the buffer is dirty beyond needed.
We fill the buffer by ' ' (space) characters, so, we expect to
have the
tail of buffer will be left untouched. Check all bytes in the
tail of
the buffer.

Quoted 7 lines

First of all, ' ' is one of the characters which hexdump is certainly supposed
to spit out, so I think it's better to use some other character for prefilling. Otherwise we wouldn't be able to detect a stray write of a
space which wasn't properly guarded by a size check. I'd suggest '\xff' or any other non-ascii

Okay, I may change the ' ' to something, but somehow printable. See also below.

Quoted 49 lines

character (and make it a #define so that
it's less magic).

Quoted 45 lines

Part 3. Return code should be as expected.

---
 lib/test_hexdump.c | 32 ++++++++++++++++----------------

 1 file changed, 16 insertions(+), 16 deletions(-)
lib/test_hexdump.c +0 -0
index a3e3b01..9b95b67 100644
@@ -128,10 +128,9 @@ static void __init test_hexdump_set(int
 rowsize, bool ascii)


 static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 {

lib/test_hexdump.c +1 -0
+	char test[TEST_HEXDUMP_BUF_SIZE];

  char buf[TEST_HEXDUMP_BUF_SIZE];

lib/test_hexdump.c +0 -1
-	const char *t = test_data_1_le[0];

  size_t len = 1;

lib/test_hexdump.c +0 -1
-	size_t l = buflen;

  int rs = 16, gs = 1;
  int ae, he, e, r;
  bool a;

lib/test_hexdump.c +0 -0
@@ -147,26 +146,27 @@ static void __init
 test_hexdump_overflow(size_t buflen, bool ascii)
  		e = ae;

  else
  e = he;

lib/test_hexdump.c +0 -1
-	buf[e + 2] = '\0';


  if (!buflen) {

lib/test_hexdump.c +0 -11
-		a = r == e && buf[0] == ' ';
-	} else if (l < 3) {
-		a = r == e && buf[0] == '\0';
-	} else if (l < 4) {
-		a = r == e && !strcmp(buf, t);
-	} else if (ascii) {
-		if (l < 51)
-			a = r == e && buf[l - 1] == '\0' && buf[l
- 2] == ' ';
-		else
-			a = r == e && buf[50] == '\0' && buf[49]

== '.';

Quoted 8 lines

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 154

Quoted 4 lines
lib/test_hexdump.c +4 -0
+		memset(test, ' ', sizeof(test));
+		test[sizeof(buf) - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf, ' ', sizeof(buf));

test and buf happen to have the same size, but "test[sizeof(buf) - 1] = '\0'" is rather odd. But you don't even seem to use test in this branch?

Here I feel the test buffer just to print below if any error happens when buflen == 0.

That's why I would like to have a somehow printable character.

Quoted 16 lines
Quoted 10 lines

  } else {

lib/test_hexdump.c +3 -1
-		a = r == e && buf[e] == '\0';
+		int f = min_t(int, e + 1, buflen);
+
+		test_hexdump_prepare_test(len, rs, gs, test,

sizeof(test), ascii);

lib/test_hexdump.c +3 -0
+		test[f - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf + f, ' ',
Quoted 2 lines

sizeof(buf) - f) && !strcmp(buf, test);
  }

There's also a bit of duplication in the !buflen and buflen branches. Why not pull the computation of f (the number of expected bytes written) outside and do

See above. buflen == 0 is a special case where buffer shouldn't be touched at all.

Quoted 10 lines

  f = min_t(int, e + 1, buflen);
  a = r == e && !memchr_inv(buf + f, ' ', sizeof(buf) - f); if (buflen) {
    test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
    test[f - 1] = '\0';
    a = a && !memcmp(buf, test, f);
  }

Quoted 4 lines

(I think it's better to use memcmp for "untrusted" buffers - if hexdump didn't make buf into a proper C string, it's a little fragile passing it to strcmp). This makes it obvious that the entire contents of buf is being tested.

Can do that.

Quoted 2 lines

Rasmus

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#16 lore raw

Re: [PATCH v1 5/7] test_hexdump: check all bytes in real buffer

Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Rasmus Villemoes

First of all, ' ' is one of the characters which hexdump is certainly supposed to spit out, so I think it's better to use some other character for prefilling. Otherwise we wouldn't be able to detect a stray write of a space which wasn't properly guarded by a size check. I'd suggest '\xff' or any other non-ascii

Okay, I may change the ' ' to something, but somehow printable. See also below.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +0 -4
-			a = r == e && buf[l - 1] == '\0' && buf[l
- 2] == ' ';
-		else
-			a = r == e && buf[50] == '\0' && buf[49]

== '.';

Quoted 8 lines

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 154

Quoted 4 lines
lib/test_hexdump.c +4 -0
+		memset(test, ' ', sizeof(test));
+		test[sizeof(buf) - 1] = '\0';
+
+		a = r == e && !memchr_inv(buf, ' ', sizeof(buf));

test and buf happen to have the same size, but "test[sizeof(buf) - 1] = '\0'" is rather odd. But you don't even seem to use test in this branch?

Quoted 4 lines

Here I feel the test buffer just to print below if any error happens when buflen == 0.

That's why I would like to have a somehow printable character.

Oh, but that's wrong! That is, printing the filled test buffer does not actually show what was "expected", whether you were filling with spaces,

$ or \xff. I really can't think of a situation where you'd want to print
the supposedly unused part of the buffer, so the character used for
filling shouldn't matter.

In any case, the most important thing is to stay away from [ a-f0-9] which hexdump is certainly expected to produce - the reason I suggested a non-ascii character was because hexdump might produce any printable ascii character in the ascii part (but I suppose one could use e.g. '#' which isn't among the characters we feed it).

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines

  } else {

lib/test_hexdump.c +3 -1
-		a = r == e && buf[e] == '\0';
+		int f = min_t(int, e + 1, buflen);
+
+		test_hexdump_prepare_test(len, rs, gs, test,

sizeof(test), ascii);

lib/test_hexdump.c +1 -0
+		test[f - 1] = '\0';
Quoted 8 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+		a = r == e && !memchr_inv(buf + f, ' ',

sizeof(buf) - f) && !strcmp(buf, test);
  }

There's also a bit of duplication in the !buflen and buflen branches. Why not pull the computation of f (the number of expected bytes written) outside and do

Quoted 2 lines

See above. buflen == 0 is a special case where buffer shouldn't be touched at all.

No, buflen==0 is not that special. We expect hexdump to touch exactly the first buflen bytes (or only e+1, whichever is smaller), and the remaining bytes should be untouched. A memcmp handles the first part, a memchr_inv the second.

Rasmus

#17 lore raw

PATCH 6/7 test_hexdump: test all possible group sizes for overflow

In reply to Andy Shevchenko

Currently only one combination is tested for overflow, i.e. rowsize = 16, groupsize = 1, len = 1. Do various test to go all possible branches.

---
 lib/test_hexdump.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
lib/test_hexdump.c +18 -5
index 9b95b67..fa2f4af 100644
@@ -126,12 +126,13 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 	test_hexdump(len, rowsize, 1, ascii);
 }
 
-static void __init test_hexdump_overflow(size_t buflen, bool ascii)
+static void __init test_hexdump_overflow(size_t buflen, size_t len,
+					 int rowsize, int groupsize,
+					 bool ascii)
 {
 	char test[TEST_HEXDUMP_BUF_SIZE];
 	char buf[TEST_HEXDUMP_BUF_SIZE];
-	size_t len = 1;
-	int rs = 16, gs = 1;
+	int rs = rowsize, gs = groupsize;
 	int ae, he, e, r;
 	bool a;
 
@@ -170,6 +171,18 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 	}
 }
 
+static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
+{
+	unsigned int i = 0;
+
+	do {
+		int gs = 1 << i;
+		size_t len = get_random_int() % 16 + gs;
+
+		test_hexdump_overflow(buflen, rounddown(len, gs), 16, gs, ascii);
+	} while (i++ < 3);
+}
+
 static int __init test_hexdump_init(void)
 {
 	unsigned int i;
@@ -186,10 +199,10 @@ static int __init test_hexdump_init(void)
 		test_hexdump_set(rowsize, true);
 
 	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
-		test_hexdump_overflow(i, false);
+		test_hexdump_overflow_set(i, false);
 
 	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
-		test_hexdump_overflow(i, true);
+		test_hexdump_overflow_set(i, true);
 
 	return -EINVAL;
 }
Signature · 1 line
2.6.2
#18 lore raw

Re: [PATCH v1 6/7] test_hexdump: test all possible group sizes for overflow

Quoted 34 lines · Andy Shevchenko

Currently only one combination is tested for overflow, i.e. rowsize = 16, groupsize = 1, len = 1. Do various test to go all possible branches.

---
 lib/test_hexdump.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
lib/test_hexdump.c +5 -3
index 9b95b67..fa2f4af 100644
@@ -126,12 +126,13 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
 	test_hexdump(len, rowsize, 1, ascii);
 }
 
-static void __init test_hexdump_overflow(size_t buflen, bool ascii)
+static void __init test_hexdump_overflow(size_t buflen, size_t len,
+					 int rowsize, int groupsize,
+					 bool ascii)
 {
 	char test[TEST_HEXDUMP_BUF_SIZE];
 	char buf[TEST_HEXDUMP_BUF_SIZE];
-	size_t len = 1;
-	int rs = 16, gs = 1;
+	int rs = rowsize, gs = groupsize;
 	int ae, he, e, r;
 	bool a;
 
@@ -170,6 +171,18 @@ static void __init test_hexdump_overflow(size_t buflen, bool ascii)
 	}
 }
 
+static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 184

Quoted 10 lines
lib/test_hexdump.c +10 -0
+{
+	unsigned int i = 0;
+
+	do {
+		int gs = 1 << i;
+		size_t len = get_random_int() % 16 + gs;
+
+		test_hexdump_overflow(buflen, rounddown(len, gs), 16, gs, ascii);
+	} while (i++ < 3);
+}

aren't you missing a

  test_hexdump_overflow(buflen, rounddown(len, gs), 32, gs, ascii);

here to also exercise the rowsize==32 code?

Quoted 5 lines
lib/test_hexdump.c +1 -0
+
 static int __init test_hexdump_init(void)
 {
 	unsigned int i;

lib/test_hexdump.c static int __init test_hexdump_init(void) · line 205

Quoted 9 lines
lib/test_hexdump.c +2 -2
@@ -186,10 +199,10 @@ static int __init test_hexdump_init(void)
 		test_hexdump_set(rowsize, true);
 
 	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
-		test_hexdump_overflow(i, false);
+		test_hexdump_overflow_set(i, false);
 
 	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
-		test_hexdump_overflow(i, true);
+		test_hexdump_overflow_set(i, true);

It seems neater to do one loop:

for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) {
test_hexdump_overflow_set(i, false);
test_hexdump_overflow_set(i, true);
}

Rasmus

#19 lore raw

Re: [PATCH v1 6/7] test_hexdump: test all possible group sizes for overflow

Quoted 7 lines · Rasmus Villemoes
Quoted 4 lines · Andy Shevchenko

Currently only one combination is tested for overflow, i.e. rowsize = 16,
groupsize = 1, len = 1.  Do various test to go all possible branches.

[]

Quoted 12 lines

lib/test_hexdump.c static void __init test_hexdump_overflow(size_t buflen, bool ascii) · line 184

Quoted 8 lines
lib/test_hexdump.c +5 -0
+	do {
+		int gs = 1 << i;
+		size_t len = get_random_int() % 16 + gs;
+
+		test_hexdump_overflow(buflen, rounddown(len, gs),

16, gs, ascii);

lib/test_hexdump.c +2 -0
+	} while (i++ < 3);
+}

aren't you missing a

Quoted 3 lines

  test_hexdump_overflow(buflen, rounddown(len, gs), 32, gs, ascii);

here to also exercise the rowsize==32 code?

I could add that as well, though it seems minor since the idea is to go for all branches, which 16 covers anyway.

Quoted 16 lines

 static int __init test_hexdump_init(void)

lib/test_hexdump.c static int __init test_hexdump_init(void) · line 205

Quoted 12 lines

 {
  unsigned int i;

lib/test_hexdump.c +0 -0
@@ -186,10 +199,10 @@ static int __init test_hexdump_init(void)
  		test_hexdump_set(rowsize, true);


  for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)

lib/test_hexdump.c +1 -1
-		test_hexdump_overflow(i, false);
+		test_hexdump_overflow_set(i, false);


  for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)

lib/test_hexdump.c +1 -1
-		test_hexdump_overflow(i, true);
+		test_hexdump_overflow_set(i, true);

It seems neater to do one loop:

Quoted 4 lines

for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) {
  test_hexdump_overflow_set(i, false);
  test_hexdump_overflow_set(i, true);
}

I would like to keep them separately, though I'm also okay to do it in one loop.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#20 lore raw

Re: [PATCH v1 6/7] test_hexdump: test all possible group sizes for overflow

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Rasmus Villemoes

aren't you missing a

  test_hexdump_overflow(buflen, rounddown(len, gs), 32, gs, ascii);

here to also exercise the rowsize==32 code?

I could add that as well, though it seems minor since the idea is to go for all branches, which 16 covers anyway.

Well, I didn't look into the implementation when I wrote that; it just seemed like an obvious thing to check all allowed combinations of rowsize, groupsize and ascii.

Quoted 16 lines
Quoted 16 lines

 static int __init test_hexdump_init(void)

lib/test_hexdump.c static int __init test_hexdump_init(void) · line 205

Quoted 12 lines

 {
  unsigned int i;

lib/test_hexdump.c +0 -0
@@ -186,10 +199,10 @@ static int __init test_hexdump_init(void)
  		test_hexdump_set(rowsize, true);


  for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)

lib/test_hexdump.c +1 -1
-		test_hexdump_overflow(i, false);
+		test_hexdump_overflow_set(i, false);


  for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)

lib/test_hexdump.c +1 -1
-		test_hexdump_overflow(i, true);
+		test_hexdump_overflow_set(i, true);

It seems neater to do one loop:

Quoted 7 lines
Quoted 4 lines

for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) {
  test_hexdump_overflow_set(i, false);
  test_hexdump_overflow_set(i, true);
}

I would like to keep them separately, though I'm also okay to do it in one loop.

Your code, your call.

Rasmus

#21 lore raw

PATCH 7/7 test_hexdump: print statistics at the end

In reply to Andy Shevchenko

Like others test are doing print the gathered statistics after test module is finished. Return from the module based on the result.

---
 lib/test_hexdump.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
lib/test_hexdump.c +17 -3
index fa2f4af..b69341b 100644
@@ -42,6 +42,9 @@ static const char * const test_data_8_le[] __initconst = {
 	"e9ac0f9cad319ca6", "0cafb1439919d14c",
 };
 
+static unsigned total_tests __initdata;
+static unsigned failed_tests __initdata;
+
 static void __init test_hexdump_prepare_test(size_t len, int rowsize,
 					     int groupsize, char *test,
 					     size_t testlen, bool ascii)
@@ -102,6 +105,8 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 	char test[TEST_HEXDUMP_BUF_SIZE];
 	char real[TEST_HEXDUMP_BUF_SIZE];
 
+	total_tests++;
+
 	hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
 			   ascii);
 
@@ -112,6 +117,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 		pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
 		pr_err("Result: '%s'\n", real);
 		pr_err("Expect: '%s'\n", test);
+		failed_tests++;
 	}
 }
 
@@ -136,6 +142,8 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 	int ae, he, e, r;
 	bool a;
 
+	total_tests++;
+
 	memset(buf, ' ', sizeof(buf));
 
 	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
@@ -168,6 +176,7 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 		pr_err("Len: %zu buflen: %zu strlen: %zu\n", len, buflen, strlen(buf));
 		pr_err("Result: %d '%s'\n", r, buf);
 		pr_err("Expect: %d '%s'\n", e, test);
+		failed_tests++;
 	}
 }
 
@@ -188,8 +197,6 @@ static int __init test_hexdump_init(void)
 	unsigned int i;
 	int rowsize;
 
-	pr_info("Running tests...\n");
-
 	rowsize = (get_random_int() % 2 + 1) * 16;
 	for (i = 0; i < 16; i++)
 		test_hexdump_set(rowsize, false);
@@ -204,7 +211,14 @@ static int __init test_hexdump_init(void)
 	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
 		test_hexdump_overflow_set(i, true);
 
-	return -EINVAL;
+	if (failed_tests == 0)
+		pr_info("all %u tests passed\n", total_tests);
+	else
+		pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
+
+	return failed_tests ? -EINVAL : 0;
 }
 module_init(test_hexdump_init);
+
+MODULE_AUTHOR("Andy Shevchenko <[email protected]>");
 MODULE_LICENSE("Dual BSD/GPL");
Signature · 1 line
2.6.2
Acked-by on v1 7/7
#22 lore raw

Re: [PATCH v1 7/7] test_hexdump: print statistics at the end

Quoted 3 lines · Andy Shevchenko

Like others test are doing print the gathered statistics after test module is finished. Return from the module based on the result.

Homogeneity between test modules is good.