test_hexdump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #define FILL_CHAR '#'
  38. static unsigned total_tests __initdata;
  39. static unsigned failed_tests __initdata;
  40. static void __init test_hexdump_prepare_test(size_t len, int rowsize,
  41. int groupsize, char *test,
  42. size_t testlen, bool ascii)
  43. {
  44. char *p;
  45. const char * const *result;
  46. size_t l = len;
  47. int gs = groupsize, rs = rowsize;
  48. unsigned int i;
  49. if (rs != 16 && rs != 32)
  50. rs = 16;
  51. if (l > rs)
  52. l = rs;
  53. if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
  54. gs = 1;
  55. if (gs == 8)
  56. result = test_data_8_le;
  57. else if (gs == 4)
  58. result = test_data_4_le;
  59. else if (gs == 2)
  60. result = test_data_2_le;
  61. else
  62. result = test_data_1_le;
  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;
  70. *p++ = ' ';
  71. }
  72. if (i)
  73. p--;
  74. /* ASCII part */
  75. if (ascii) {
  76. do {
  77. *p++ = ' ';
  78. } while (p < test + rs * 2 + rs / gs + 1);
  79. strncpy(p, data_a, l);
  80. p += l;
  81. }
  82. *p = '\0';
  83. }
  84. #define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1)
  85. static void __init test_hexdump(size_t len, int rowsize, int groupsize,
  86. bool ascii)
  87. {
  88. char test[TEST_HEXDUMP_BUF_SIZE];
  89. char real[TEST_HEXDUMP_BUF_SIZE];
  90. total_tests++;
  91. memset(real, FILL_CHAR, sizeof(real));
  92. hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
  93. ascii);
  94. memset(test, FILL_CHAR, sizeof(test));
  95. test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
  96. ascii);
  97. if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
  98. pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
  99. pr_err("Result: '%s'\n", real);
  100. pr_err("Expect: '%s'\n", test);
  101. failed_tests++;
  102. }
  103. }
  104. static void __init test_hexdump_set(int rowsize, bool ascii)
  105. {
  106. size_t d = min_t(size_t, sizeof(data_b), rowsize);
  107. size_t len = get_random_int() % d + 1;
  108. test_hexdump(len, rowsize, 4, ascii);
  109. test_hexdump(len, rowsize, 2, ascii);
  110. test_hexdump(len, rowsize, 8, ascii);
  111. test_hexdump(len, rowsize, 1, ascii);
  112. }
  113. static void __init test_hexdump_overflow(size_t buflen, size_t len,
  114. int rowsize, int groupsize,
  115. bool ascii)
  116. {
  117. char test[TEST_HEXDUMP_BUF_SIZE];
  118. char buf[TEST_HEXDUMP_BUF_SIZE];
  119. int rs = rowsize, gs = groupsize;
  120. int ae, he, e, f, r;
  121. bool a;
  122. total_tests++;
  123. memset(buf, FILL_CHAR, sizeof(buf));
  124. r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
  125. /*
  126. * Caller must provide the data length multiple of groupsize. The
  127. * calculations below are made with that assumption in mind.
  128. */
  129. ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
  130. he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
  131. if (ascii)
  132. e = ae;
  133. else
  134. e = he;
  135. f = min_t(int, e + 1, buflen);
  136. if (buflen) {
  137. test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
  138. test[f - 1] = '\0';
  139. }
  140. memset(test + f, FILL_CHAR, sizeof(test) - f);
  141. a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
  142. buf[sizeof(buf) - 1] = '\0';
  143. if (!a) {
  144. pr_err("Len: %zu buflen: %zu strlen: %zu\n",
  145. len, buflen, strnlen(buf, sizeof(buf)));
  146. pr_err("Result: %d '%s'\n", r, buf);
  147. pr_err("Expect: %d '%s'\n", e, test);
  148. failed_tests++;
  149. }
  150. }
  151. static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
  152. {
  153. unsigned int i = 0;
  154. int rs = (get_random_int() % 2 + 1) * 16;
  155. do {
  156. int gs = 1 << i;
  157. size_t len = get_random_int() % rs + gs;
  158. test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
  159. } while (i++ < 3);
  160. }
  161. static int __init test_hexdump_init(void)
  162. {
  163. unsigned int i;
  164. int rowsize;
  165. rowsize = (get_random_int() % 2 + 1) * 16;
  166. for (i = 0; i < 16; i++)
  167. test_hexdump_set(rowsize, false);
  168. rowsize = (get_random_int() % 2 + 1) * 16;
  169. for (i = 0; i < 16; i++)
  170. test_hexdump_set(rowsize, true);
  171. for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
  172. test_hexdump_overflow_set(i, false);
  173. for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
  174. test_hexdump_overflow_set(i, true);
  175. if (failed_tests == 0)
  176. pr_info("all %u tests passed\n", total_tests);
  177. else
  178. pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
  179. return failed_tests ? -EINVAL : 0;
  180. }
  181. module_init(test_hexdump_init);
  182. static void __exit test_hexdump_exit(void)
  183. {
  184. /* do nothing */
  185. }
  186. module_exit(test_hexdump_exit);
  187. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  188. MODULE_LICENSE("Dual BSD/GPL");