test_hexdump.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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[] __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_2_be[] __initconst = {
  30. "be32", "db7b", "0a18", "93b2",
  31. "70ba", "c424", "7d83", "349b",
  32. "a69c", "31ad", "9c0f", "ace9",
  33. "4cd1", "1999", "43b1", "af0c",
  34. };
  35. static const char * const test_data_4_le[] __initconst = {
  36. "7bdb32be", "b293180a", "24c4ba70", "9b34837d",
  37. "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
  38. };
  39. static const char * const test_data_4_be[] __initconst = {
  40. "be32db7b", "0a1893b2", "70bac424", "7d83349b",
  41. "a69c31ad", "9c0face9", "4cd11999", "43b1af0c",
  42. };
  43. static const char * const test_data_8_le[] __initconst = {
  44. "b293180a7bdb32be", "9b34837d24c4ba70",
  45. "e9ac0f9cad319ca6", "0cafb1439919d14c",
  46. };
  47. static const char * const test_data_8_be[] __initconst = {
  48. "be32db7b0a1893b2", "70bac4247d83349b",
  49. "a69c31ad9c0face9", "4cd1199943b1af0c",
  50. };
  51. #define FILL_CHAR '#'
  52. static unsigned total_tests __initdata;
  53. static unsigned failed_tests __initdata;
  54. static void __init test_hexdump_prepare_test(size_t len, int rowsize,
  55. int groupsize, char *test,
  56. size_t testlen, bool ascii)
  57. {
  58. char *p;
  59. const char * const *result;
  60. size_t l = len;
  61. int gs = groupsize, rs = rowsize;
  62. unsigned int i;
  63. const bool is_be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
  64. if (rs != 16 && rs != 32)
  65. rs = 16;
  66. if (l > rs)
  67. l = rs;
  68. if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
  69. gs = 1;
  70. if (gs == 8)
  71. result = is_be ? test_data_8_be : test_data_8_le;
  72. else if (gs == 4)
  73. result = is_be ? test_data_4_be : test_data_4_le;
  74. else if (gs == 2)
  75. result = is_be ? test_data_2_be : test_data_2_le;
  76. else
  77. result = test_data_1;
  78. /* hex dump */
  79. p = test;
  80. for (i = 0; i < l / gs; i++) {
  81. const char *q = *result++;
  82. size_t amount = strlen(q);
  83. memcpy(p, q, amount);
  84. p += amount;
  85. *p++ = ' ';
  86. }
  87. if (i)
  88. p--;
  89. /* ASCII part */
  90. if (ascii) {
  91. do {
  92. *p++ = ' ';
  93. } while (p < test + rs * 2 + rs / gs + 1);
  94. strncpy(p, data_a, l);
  95. p += l;
  96. }
  97. *p = '\0';
  98. }
  99. #define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1)
  100. static void __init test_hexdump(size_t len, int rowsize, int groupsize,
  101. bool ascii)
  102. {
  103. char test[TEST_HEXDUMP_BUF_SIZE];
  104. char real[TEST_HEXDUMP_BUF_SIZE];
  105. total_tests++;
  106. memset(real, FILL_CHAR, sizeof(real));
  107. hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
  108. ascii);
  109. memset(test, FILL_CHAR, sizeof(test));
  110. test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
  111. ascii);
  112. if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
  113. pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
  114. pr_err("Result: '%s'\n", real);
  115. pr_err("Expect: '%s'\n", test);
  116. failed_tests++;
  117. }
  118. }
  119. static void __init test_hexdump_set(int rowsize, bool ascii)
  120. {
  121. size_t d = min_t(size_t, sizeof(data_b), rowsize);
  122. size_t len = get_random_int() % d + 1;
  123. test_hexdump(len, rowsize, 4, ascii);
  124. test_hexdump(len, rowsize, 2, ascii);
  125. test_hexdump(len, rowsize, 8, ascii);
  126. test_hexdump(len, rowsize, 1, ascii);
  127. }
  128. static void __init test_hexdump_overflow(size_t buflen, size_t len,
  129. int rowsize, int groupsize,
  130. bool ascii)
  131. {
  132. char test[TEST_HEXDUMP_BUF_SIZE];
  133. char buf[TEST_HEXDUMP_BUF_SIZE];
  134. int rs = rowsize, gs = groupsize;
  135. int ae, he, e, f, r;
  136. bool a;
  137. total_tests++;
  138. memset(buf, FILL_CHAR, sizeof(buf));
  139. r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
  140. /*
  141. * Caller must provide the data length multiple of groupsize. The
  142. * calculations below are made with that assumption in mind.
  143. */
  144. ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
  145. he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
  146. if (ascii)
  147. e = ae;
  148. else
  149. e = he;
  150. f = min_t(int, e + 1, buflen);
  151. if (buflen) {
  152. test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
  153. test[f - 1] = '\0';
  154. }
  155. memset(test + f, FILL_CHAR, sizeof(test) - f);
  156. a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
  157. buf[sizeof(buf) - 1] = '\0';
  158. if (!a) {
  159. pr_err("Len: %zu buflen: %zu strlen: %zu\n",
  160. len, buflen, strnlen(buf, sizeof(buf)));
  161. pr_err("Result: %d '%s'\n", r, buf);
  162. pr_err("Expect: %d '%s'\n", e, test);
  163. failed_tests++;
  164. }
  165. }
  166. static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
  167. {
  168. unsigned int i = 0;
  169. int rs = (get_random_int() % 2 + 1) * 16;
  170. do {
  171. int gs = 1 << i;
  172. size_t len = get_random_int() % rs + gs;
  173. test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
  174. } while (i++ < 3);
  175. }
  176. static int __init test_hexdump_init(void)
  177. {
  178. unsigned int i;
  179. int rowsize;
  180. rowsize = (get_random_int() % 2 + 1) * 16;
  181. for (i = 0; i < 16; i++)
  182. test_hexdump_set(rowsize, false);
  183. rowsize = (get_random_int() % 2 + 1) * 16;
  184. for (i = 0; i < 16; i++)
  185. test_hexdump_set(rowsize, true);
  186. for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
  187. test_hexdump_overflow_set(i, false);
  188. for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
  189. test_hexdump_overflow_set(i, true);
  190. if (failed_tests == 0)
  191. pr_info("all %u tests passed\n", total_tests);
  192. else
  193. pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
  194. return failed_tests ? -EINVAL : 0;
  195. }
  196. module_init(test_hexdump_init);
  197. static void __exit test_hexdump_exit(void)
  198. {
  199. /* do nothing */
  200. }
  201. module_exit(test_hexdump_exit);
  202. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  203. MODULE_LICENSE("Dual BSD/GPL");