test-hexdump.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Test cases for lib/hexdump.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. static const unsigned char data_b[] = {
  11. '\xbe', '\x32', '\xdb', '\x7b', '\x0a', '\x18', '\x93', '\xb2', /* 00 - 07 */
  12. '\x70', '\xba', '\xc4', '\x24', '\x7d', '\x83', '\x34', '\x9b', /* 08 - 0f */
  13. '\xa6', '\x9c', '\x31', '\xad', '\x9c', '\x0f', '\xac', '\xe9', /* 10 - 17 */
  14. '\x4c', '\xd1', '\x19', '\x99', '\x43', '\xb1', '\xaf', '\x0c', /* 18 - 1f */
  15. };
  16. static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C...";
  17. static const char * const test_data_1_le[] __initconst = {
  18. "be", "32", "db", "7b", "0a", "18", "93", "b2",
  19. "70", "ba", "c4", "24", "7d", "83", "34", "9b",
  20. "a6", "9c", "31", "ad", "9c", "0f", "ac", "e9",
  21. "4c", "d1", "19", "99", "43", "b1", "af", "0c",
  22. };
  23. static const char * const test_data_2_le[] __initconst = {
  24. "32be", "7bdb", "180a", "b293",
  25. "ba70", "24c4", "837d", "9b34",
  26. "9ca6", "ad31", "0f9c", "e9ac",
  27. "d14c", "9919", "b143", "0caf",
  28. };
  29. static const char * const test_data_4_le[] __initconst = {
  30. "7bdb32be", "b293180a", "24c4ba70", "9b34837d",
  31. "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
  32. };
  33. static const char * const test_data_8_le[] __initconst = {
  34. "b293180a7bdb32be", "9b34837d24c4ba70",
  35. "e9ac0f9cad319ca6", "0cafb1439919d14c",
  36. };
  37. static void __init test_hexdump(size_t len, int rowsize, int groupsize,
  38. bool ascii)
  39. {
  40. char test[32 * 3 + 2 + 32 + 1];
  41. char real[32 * 3 + 2 + 32 + 1];
  42. char *p;
  43. const char * const *result;
  44. size_t l = len;
  45. int gs = groupsize, rs = rowsize;
  46. unsigned int i;
  47. hex_dump_to_buffer(data_b, l, rs, gs, real, sizeof(real), ascii);
  48. if (rs != 16 && rs != 32)
  49. rs = 16;
  50. if (l > rs)
  51. l = rs;
  52. if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
  53. gs = 1;
  54. if (gs == 8)
  55. result = test_data_8_le;
  56. else if (gs == 4)
  57. result = test_data_4_le;
  58. else if (gs == 2)
  59. result = test_data_2_le;
  60. else
  61. result = test_data_1_le;
  62. memset(test, ' ', sizeof(test));
  63. /* hex dump */
  64. p = test;
  65. for (i = 0; i < l / gs; i++) {
  66. const char *q = *result++;
  67. size_t amount = strlen(q);
  68. strncpy(p, q, amount);
  69. p += amount + 1;
  70. }
  71. if (i)
  72. p--;
  73. /* ASCII part */
  74. if (ascii) {
  75. p = test + rs * 2 + rs / gs + 1;
  76. strncpy(p, data_a, l);
  77. p += l;
  78. }
  79. *p = '\0';
  80. if (strcmp(test, real)) {
  81. pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
  82. pr_err("Result: '%s'\n", real);
  83. pr_err("Expect: '%s'\n", test);
  84. }
  85. }
  86. static void __init test_hexdump_set(int rowsize, bool ascii)
  87. {
  88. size_t d = min_t(size_t, sizeof(data_b), rowsize);
  89. size_t len = get_random_int() % d + 1;
  90. test_hexdump(len, rowsize, 4, ascii);
  91. test_hexdump(len, rowsize, 2, ascii);
  92. test_hexdump(len, rowsize, 8, ascii);
  93. test_hexdump(len, rowsize, 1, ascii);
  94. }
  95. static void __init test_hexdump_overflow(bool ascii)
  96. {
  97. char buf[56];
  98. const char *t = test_data_1_le[0];
  99. size_t l = get_random_int() % sizeof(buf);
  100. bool a;
  101. int e, r;
  102. memset(buf, ' ', sizeof(buf));
  103. r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
  104. if (ascii)
  105. e = 50;
  106. else
  107. e = 2;
  108. buf[e + 2] = '\0';
  109. if (!l) {
  110. a = r == e && buf[0] == ' ';
  111. } else if (l < 3) {
  112. a = r == e && buf[0] == '\0';
  113. } else if (l < 4) {
  114. a = r == e && !strcmp(buf, t);
  115. } else if (ascii) {
  116. if (l < 51)
  117. a = r == e && buf[l - 1] == '\0' && buf[l - 2] == ' ';
  118. else
  119. a = r == e && buf[50] == '\0' && buf[49] == '.';
  120. } else {
  121. a = r == e && buf[e] == '\0';
  122. }
  123. if (!a) {
  124. pr_err("Len: %zu rc: %u strlen: %zu\n", l, r, strlen(buf));
  125. pr_err("Result: '%s'\n", buf);
  126. }
  127. }
  128. static int __init test_hexdump_init(void)
  129. {
  130. unsigned int i;
  131. int rowsize;
  132. pr_info("Running tests...\n");
  133. rowsize = (get_random_int() % 2 + 1) * 16;
  134. for (i = 0; i < 16; i++)
  135. test_hexdump_set(rowsize, false);
  136. rowsize = (get_random_int() % 2 + 1) * 16;
  137. for (i = 0; i < 16; i++)
  138. test_hexdump_set(rowsize, true);
  139. for (i = 0; i < 16; i++)
  140. test_hexdump_overflow(false);
  141. for (i = 0; i < 16; i++)
  142. test_hexdump_overflow(true);
  143. return -EINVAL;
  144. }
  145. module_init(test_hexdump_init);
  146. MODULE_LICENSE("Dual BSD/GPL");