test-string_helpers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Test cases for lib/string_helpers.c module.
  3. */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/random.h>
  9. #include <linux/string.h>
  10. #include <linux/string_helpers.h>
  11. struct test_string {
  12. const char *in;
  13. const char *out;
  14. unsigned int flags;
  15. };
  16. static const struct test_string strings[] __initconst = {
  17. {
  18. .in = "\\f\\ \\n\\r\\t\\v",
  19. .out = "\f\\ \n\r\t\v",
  20. .flags = UNESCAPE_SPACE,
  21. },
  22. {
  23. .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
  24. .out = " \001\00387\0064\005 \\8aH?7",
  25. .flags = UNESCAPE_OCTAL,
  26. },
  27. {
  28. .in = "\\xv\\xa\\x2c\\xD\\x6f2",
  29. .out = "\\xv\n,\ro2",
  30. .flags = UNESCAPE_HEX,
  31. },
  32. {
  33. .in = "\\h\\\\\\\"\\a\\e\\",
  34. .out = "\\h\\\"\a\e\\",
  35. .flags = UNESCAPE_SPECIAL,
  36. },
  37. };
  38. static void __init test_string_unescape(unsigned int flags, bool inplace)
  39. {
  40. char in[256];
  41. char out_test[256];
  42. char out_real[256];
  43. int i, p = 0, q_test = 0, q_real = sizeof(out_real);
  44. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  45. const char *s = strings[i].in;
  46. int len = strlen(strings[i].in);
  47. /* Copy string to in buffer */
  48. memcpy(&in[p], s, len);
  49. p += len;
  50. /* Copy expected result for given flags */
  51. if (flags & strings[i].flags) {
  52. s = strings[i].out;
  53. len = strlen(strings[i].out);
  54. }
  55. memcpy(&out_test[q_test], s, len);
  56. q_test += len;
  57. }
  58. in[p++] = '\0';
  59. /* Call string_unescape and compare result */
  60. if (inplace) {
  61. memcpy(out_real, in, p);
  62. if (flags == UNESCAPE_ANY)
  63. q_real = string_unescape_any_inplace(out_real);
  64. else
  65. q_real = string_unescape_inplace(out_real, flags);
  66. } else if (flags == UNESCAPE_ANY) {
  67. q_real = string_unescape_any(in, out_real, q_real);
  68. } else {
  69. q_real = string_unescape(in, out_real, q_real, flags);
  70. }
  71. if (q_real != q_test || memcmp(out_test, out_real, q_test)) {
  72. pr_warn("Test failed: flags = %u\n", flags);
  73. print_hex_dump(KERN_WARNING, "Input: ",
  74. DUMP_PREFIX_NONE, 16, 1, in, p - 1, true);
  75. print_hex_dump(KERN_WARNING, "Expected: ",
  76. DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true);
  77. print_hex_dump(KERN_WARNING, "Got: ",
  78. DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true);
  79. }
  80. }
  81. static int __init test_string_helpers_init(void)
  82. {
  83. unsigned int i;
  84. pr_info("Running tests...\n");
  85. for (i = 0; i < UNESCAPE_ANY + 1; i++)
  86. test_string_unescape(i, false);
  87. test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true);
  88. return -EINVAL;
  89. }
  90. module_init(test_string_helpers_init);
  91. MODULE_LICENSE("Dual BSD/GPL");