Commit 45ff337a54c154680edf0c538e5c9eb4a2f862cc

Authored by Andy Shevchenko
Committed by Linus Torvalds
1 parent d295634e96

lib / string_helpers: refactoring the test suite

This patch prepares test suite for a following update.  It introduces
test_string_check_buf() helper which checks the result and dumps an error.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: "John W . Linville" <linville@tuxdriver.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 27 additions and 12 deletions Side-by-side Diff

lib/test-string_helpers.c
... ... @@ -10,6 +10,26 @@
10 10 #include <linux/string.h>
11 11 #include <linux/string_helpers.h>
12 12  
  13 +static __init bool test_string_check_buf(const char *name, unsigned int flags,
  14 + char *in, size_t p,
  15 + char *out_real, size_t q_real,
  16 + char *out_test, size_t q_test)
  17 +{
  18 + if (q_real == q_test && !memcmp(out_test, out_real, q_test))
  19 + return true;
  20 +
  21 + pr_warn("Test '%s' failed: flags = %u\n", name, flags);
  22 +
  23 + print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
  24 + in, p, true);
  25 + print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
  26 + out_test, q_test, true);
  27 + print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
  28 + out_real, q_real, true);
  29 +
  30 + return false;
  31 +}
  32 +
13 33 struct test_string {
14 34 const char *in;
15 35 const char *out;
... ... @@ -39,7 +59,8 @@
39 59 },
40 60 };
41 61  
42   -static void __init test_string_unescape(unsigned int flags, bool inplace)
  62 +static void __init test_string_unescape(const char *name, unsigned int flags,
  63 + bool inplace)
43 64 {
44 65 char in[256];
45 66 char out_test[256];
... ... @@ -77,15 +98,8 @@
77 98 q_real = string_unescape(in, out_real, q_real, flags);
78 99 }
79 100  
80   - if (q_real != q_test || memcmp(out_test, out_real, q_test)) {
81   - pr_warn("Test failed: flags = %u\n", flags);
82   - print_hex_dump(KERN_WARNING, "Input: ",
83   - DUMP_PREFIX_NONE, 16, 1, in, p - 1, true);
84   - print_hex_dump(KERN_WARNING, "Expected: ",
85   - DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true);
86   - print_hex_dump(KERN_WARNING, "Got: ",
87   - DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true);
88   - }
  101 + test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
  102 + out_test, q_test);
89 103 }
90 104  
91 105 static int __init test_string_helpers_init(void)
... ... @@ -94,8 +108,9 @@
94 108  
95 109 pr_info("Running tests...\n");
96 110 for (i = 0; i < UNESCAPE_ANY + 1; i++)
97   - test_string_unescape(i, false);
98   - test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true);
  111 + test_string_unescape("unescape", i, false);
  112 + test_string_unescape("unescape inplace",
  113 + get_random_int() % (UNESCAPE_ANY + 1), true);
99 114  
100 115 return -EINVAL;
101 116 }