diff options
| author | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2013-04-30 18:27:30 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-04-30 20:04:03 -0400 |
| commit | 16c7fa05829e8b91db48e3539c5d6ff3c2b18a23 (patch) | |
| tree | 6658e9f27c033e5f6de19284fe9a6b8485360aa6 /lib/test-string_helpers.c | |
| parent | e1d12f327037a59e06c66520951ab4e0bd29f9c4 (diff) | |
lib/string_helpers: introduce generic string_unescape
There are several places in kernel where modules unescapes input to convert
C-Style Escape Sequences into byte codes.
The patch provides generic implementation of such approach. Test cases are
also included into the patch.
[akpm@linux-foundation.org: clarify comment]
[akpm@linux-foundation.org: export get_random_int() to modules]
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: William Hubbs <w.d.hubbs@gmail.com>
Cc: Chris Brannon <chris@the-brannons.com>
Cc: Kirk Reiser <kirk@braille.uwo.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/test-string_helpers.c')
| -rw-r--r-- | lib/test-string_helpers.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c new file mode 100644 index 000000000000..6ac48de04c0e --- /dev/null +++ b/lib/test-string_helpers.c | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | /* | ||
| 2 | * Test cases for lib/string_helpers.c module. | ||
| 3 | */ | ||
| 4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
| 5 | |||
| 6 | #include <linux/init.h> | ||
| 7 | #include <linux/kernel.h> | ||
| 8 | #include <linux/module.h> | ||
| 9 | #include <linux/random.h> | ||
| 10 | #include <linux/string.h> | ||
| 11 | #include <linux/string_helpers.h> | ||
| 12 | |||
| 13 | struct test_string { | ||
| 14 | const char *in; | ||
| 15 | const char *out; | ||
| 16 | unsigned int flags; | ||
| 17 | }; | ||
| 18 | |||
| 19 | static const struct test_string strings[] __initconst = { | ||
| 20 | { | ||
| 21 | .in = "\\f\\ \\n\\r\\t\\v", | ||
| 22 | .out = "\f\\ \n\r\t\v", | ||
| 23 | .flags = UNESCAPE_SPACE, | ||
| 24 | }, | ||
| 25 | { | ||
| 26 | .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777", | ||
| 27 | .out = " \001\00387\0064\005 \\8aH?7", | ||
| 28 | .flags = UNESCAPE_OCTAL, | ||
| 29 | }, | ||
| 30 | { | ||
| 31 | .in = "\\xv\\xa\\x2c\\xD\\x6f2", | ||
| 32 | .out = "\\xv\n,\ro2", | ||
| 33 | .flags = UNESCAPE_HEX, | ||
| 34 | }, | ||
| 35 | { | ||
| 36 | .in = "\\h\\\\\\\"\\a\\e\\", | ||
| 37 | .out = "\\h\\\"\a\e\\", | ||
| 38 | .flags = UNESCAPE_SPECIAL, | ||
| 39 | }, | ||
| 40 | }; | ||
| 41 | |||
| 42 | static void __init test_string_unescape(unsigned int flags, bool inplace) | ||
| 43 | { | ||
| 44 | char in[256]; | ||
| 45 | char out_test[256]; | ||
| 46 | char out_real[256]; | ||
| 47 | int i, p = 0, q_test = 0, q_real = sizeof(out_real); | ||
| 48 | |||
| 49 | for (i = 0; i < ARRAY_SIZE(strings); i++) { | ||
| 50 | const char *s = strings[i].in; | ||
| 51 | int len = strlen(strings[i].in); | ||
| 52 | |||
| 53 | /* Copy string to in buffer */ | ||
| 54 | memcpy(&in[p], s, len); | ||
| 55 | p += len; | ||
| 56 | |||
| 57 | /* Copy expected result for given flags */ | ||
| 58 | if (flags & strings[i].flags) { | ||
| 59 | s = strings[i].out; | ||
| 60 | len = strlen(strings[i].out); | ||
| 61 | } | ||
| 62 | memcpy(&out_test[q_test], s, len); | ||
| 63 | q_test += len; | ||
| 64 | } | ||
| 65 | in[p++] = '\0'; | ||
| 66 | |||
| 67 | /* Call string_unescape and compare result */ | ||
| 68 | if (inplace) { | ||
| 69 | memcpy(out_real, in, p); | ||
| 70 | if (flags == UNESCAPE_ANY) | ||
| 71 | q_real = string_unescape_any_inplace(out_real); | ||
| 72 | else | ||
| 73 | q_real = string_unescape_inplace(out_real, flags); | ||
| 74 | } else if (flags == UNESCAPE_ANY) { | ||
| 75 | q_real = string_unescape_any(in, out_real, q_real); | ||
| 76 | } else { | ||
| 77 | q_real = string_unescape(in, out_real, q_real, flags); | ||
| 78 | } | ||
| 79 | |||
| 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 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | static int __init test_string_helpers_init(void) | ||
| 92 | { | ||
| 93 | unsigned int i; | ||
| 94 | |||
| 95 | pr_info("Running tests...\n"); | ||
| 96 | 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); | ||
| 99 | |||
| 100 | return -EINVAL; | ||
| 101 | } | ||
| 102 | module_init(test_string_helpers_init); | ||
| 103 | MODULE_LICENSE("Dual BSD/GPL"); | ||
