test-string_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/random.h>
  10. #include <linux/string.h>
  11. #include <linux/string_helpers.h>
  12. static __init bool test_string_check_buf(const char *name, unsigned int flags,
  13. char *in, size_t p,
  14. char *out_real, size_t q_real,
  15. char *out_test, size_t q_test)
  16. {
  17. if (q_real == q_test && !memcmp(out_test, out_real, q_test))
  18. return true;
  19. pr_warn("Test '%s' failed: flags = %u\n", name, flags);
  20. print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
  21. in, p, true);
  22. print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
  23. out_test, q_test, true);
  24. print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
  25. out_real, q_real, true);
  26. return false;
  27. }
  28. struct test_string {
  29. const char *in;
  30. const char *out;
  31. unsigned int flags;
  32. };
  33. static const struct test_string strings[] __initconst = {
  34. {
  35. .in = "\\f\\ \\n\\r\\t\\v",
  36. .out = "\f\\ \n\r\t\v",
  37. .flags = UNESCAPE_SPACE,
  38. },
  39. {
  40. .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
  41. .out = " \001\00387\0064\005 \\8aH?7",
  42. .flags = UNESCAPE_OCTAL,
  43. },
  44. {
  45. .in = "\\xv\\xa\\x2c\\xD\\x6f2",
  46. .out = "\\xv\n,\ro2",
  47. .flags = UNESCAPE_HEX,
  48. },
  49. {
  50. .in = "\\h\\\\\\\"\\a\\e\\",
  51. .out = "\\h\\\"\a\e\\",
  52. .flags = UNESCAPE_SPECIAL,
  53. },
  54. };
  55. static void __init test_string_unescape(const char *name, unsigned int flags,
  56. bool inplace)
  57. {
  58. int q_real = 256;
  59. char *in = kmalloc(q_real, GFP_KERNEL);
  60. char *out_test = kmalloc(q_real, GFP_KERNEL);
  61. char *out_real = kmalloc(q_real, GFP_KERNEL);
  62. int i, p = 0, q_test = 0;
  63. if (!in || !out_test || !out_real)
  64. goto out;
  65. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  66. const char *s = strings[i].in;
  67. int len = strlen(strings[i].in);
  68. /* Copy string to in buffer */
  69. memcpy(&in[p], s, len);
  70. p += len;
  71. /* Copy expected result for given flags */
  72. if (flags & strings[i].flags) {
  73. s = strings[i].out;
  74. len = strlen(strings[i].out);
  75. }
  76. memcpy(&out_test[q_test], s, len);
  77. q_test += len;
  78. }
  79. in[p++] = '\0';
  80. /* Call string_unescape and compare result */
  81. if (inplace) {
  82. memcpy(out_real, in, p);
  83. if (flags == UNESCAPE_ANY)
  84. q_real = string_unescape_any_inplace(out_real);
  85. else
  86. q_real = string_unescape_inplace(out_real, flags);
  87. } else if (flags == UNESCAPE_ANY) {
  88. q_real = string_unescape_any(in, out_real, q_real);
  89. } else {
  90. q_real = string_unescape(in, out_real, q_real, flags);
  91. }
  92. test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
  93. out_test, q_test);
  94. out:
  95. kfree(out_real);
  96. kfree(out_test);
  97. kfree(in);
  98. }
  99. struct test_string_1 {
  100. const char *out;
  101. unsigned int flags;
  102. };
  103. #define TEST_STRING_2_MAX_S1 32
  104. struct test_string_2 {
  105. const char *in;
  106. struct test_string_1 s1[TEST_STRING_2_MAX_S1];
  107. };
  108. #define TEST_STRING_2_DICT_0 NULL
  109. static const struct test_string_2 escape0[] __initconst = {{
  110. .in = "\f\\ \n\r\t\v",
  111. .s1 = {{
  112. .out = "\\f\\ \\n\\r\\t\\v",
  113. .flags = ESCAPE_SPACE,
  114. },{
  115. .out = "\\f\\134\\040\\n\\r\\t\\v",
  116. .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
  117. },{
  118. .out = "\\f\\x5c\\x20\\n\\r\\t\\v",
  119. .flags = ESCAPE_SPACE | ESCAPE_HEX,
  120. },{
  121. /* terminator */
  122. }},
  123. },{
  124. .in = "\\h\\\"\a\e\\",
  125. .s1 = {{
  126. .out = "\\\\h\\\\\"\\a\\e\\\\",
  127. .flags = ESCAPE_SPECIAL,
  128. },{
  129. .out = "\\\\\\150\\\\\\042\\a\\e\\\\",
  130. .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
  131. },{
  132. .out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
  133. .flags = ESCAPE_SPECIAL | ESCAPE_HEX,
  134. },{
  135. /* terminator */
  136. }},
  137. },{
  138. .in = "\eb \\C\007\"\x90\r]",
  139. .s1 = {{
  140. .out = "\eb \\C\007\"\x90\\r]",
  141. .flags = ESCAPE_SPACE,
  142. },{
  143. .out = "\\eb \\\\C\\a\"\x90\r]",
  144. .flags = ESCAPE_SPECIAL,
  145. },{
  146. .out = "\\eb \\\\C\\a\"\x90\\r]",
  147. .flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
  148. },{
  149. .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
  150. .flags = ESCAPE_OCTAL,
  151. },{
  152. .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
  153. .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
  154. },{
  155. .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
  156. .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
  157. },{
  158. .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
  159. .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
  160. },{
  161. .out = "\eb \\C\007\"\x90\r]",
  162. .flags = ESCAPE_NP,
  163. },{
  164. .out = "\eb \\C\007\"\x90\\r]",
  165. .flags = ESCAPE_SPACE | ESCAPE_NP,
  166. },{
  167. .out = "\\eb \\C\\a\"\x90\r]",
  168. .flags = ESCAPE_SPECIAL | ESCAPE_NP,
  169. },{
  170. .out = "\\eb \\C\\a\"\x90\\r]",
  171. .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NP,
  172. },{
  173. .out = "\\033b \\C\\007\"\\220\\015]",
  174. .flags = ESCAPE_OCTAL | ESCAPE_NP,
  175. },{
  176. .out = "\\033b \\C\\007\"\\220\\r]",
  177. .flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NP,
  178. },{
  179. .out = "\\eb \\C\\a\"\\220\\r]",
  180. .flags = ESCAPE_SPECIAL | ESCAPE_SPACE | ESCAPE_OCTAL |
  181. ESCAPE_NP,
  182. },{
  183. .out = "\\x1bb \\C\\x07\"\\x90\\x0d]",
  184. .flags = ESCAPE_NP | ESCAPE_HEX,
  185. },{
  186. /* terminator */
  187. }},
  188. },{
  189. /* terminator */
  190. }};
  191. #define TEST_STRING_2_DICT_1 "b\\ \t\r"
  192. static const struct test_string_2 escape1[] __initconst = {{
  193. .in = "\f\\ \n\r\t\v",
  194. .s1 = {{
  195. .out = "\f\\134\\040\n\\015\\011\v",
  196. .flags = ESCAPE_OCTAL,
  197. },{
  198. .out = "\f\\x5c\\x20\n\\x0d\\x09\v",
  199. .flags = ESCAPE_HEX,
  200. },{
  201. /* terminator */
  202. }},
  203. },{
  204. .in = "\\h\\\"\a\e\\",
  205. .s1 = {{
  206. .out = "\\134h\\134\"\a\e\\134",
  207. .flags = ESCAPE_OCTAL,
  208. },{
  209. /* terminator */
  210. }},
  211. },{
  212. .in = "\eb \\C\007\"\x90\r]",
  213. .s1 = {{
  214. .out = "\e\\142\\040\\134C\007\"\x90\\015]",
  215. .flags = ESCAPE_OCTAL,
  216. },{
  217. /* terminator */
  218. }},
  219. },{
  220. /* terminator */
  221. }};
  222. static __init const char *test_string_find_match(const struct test_string_2 *s2,
  223. unsigned int flags)
  224. {
  225. const struct test_string_1 *s1 = s2->s1;
  226. unsigned int i;
  227. if (!flags)
  228. return s2->in;
  229. /* Test cases are NULL-aware */
  230. flags &= ~ESCAPE_NULL;
  231. /* ESCAPE_OCTAL has a higher priority */
  232. if (flags & ESCAPE_OCTAL)
  233. flags &= ~ESCAPE_HEX;
  234. for (i = 0; i < TEST_STRING_2_MAX_S1 && s1->out; i++, s1++)
  235. if (s1->flags == flags)
  236. return s1->out;
  237. return NULL;
  238. }
  239. static __init void
  240. test_string_escape_overflow(const char *in, int p, unsigned int flags, const char *esc,
  241. int q_test, const char *name)
  242. {
  243. int q_real;
  244. q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
  245. if (q_real != q_test)
  246. pr_warn("Test '%s' failed: flags = %u, osz = 0, expected %d, got %d\n",
  247. name, flags, q_test, q_real);
  248. }
  249. static __init void test_string_escape(const char *name,
  250. const struct test_string_2 *s2,
  251. unsigned int flags, const char *esc)
  252. {
  253. size_t out_size = 512;
  254. char *out_test = kmalloc(out_size, GFP_KERNEL);
  255. char *out_real = kmalloc(out_size, GFP_KERNEL);
  256. char *in = kmalloc(256, GFP_KERNEL);
  257. int p = 0, q_test = 0;
  258. int q_real;
  259. if (!out_test || !out_real || !in)
  260. goto out;
  261. for (; s2->in; s2++) {
  262. const char *out;
  263. int len;
  264. /* NULL injection */
  265. if (flags & ESCAPE_NULL) {
  266. in[p++] = '\0';
  267. out_test[q_test++] = '\\';
  268. out_test[q_test++] = '0';
  269. }
  270. /* Don't try strings that have no output */
  271. out = test_string_find_match(s2, flags);
  272. if (!out)
  273. continue;
  274. /* Copy string to in buffer */
  275. len = strlen(s2->in);
  276. memcpy(&in[p], s2->in, len);
  277. p += len;
  278. /* Copy expected result for given flags */
  279. len = strlen(out);
  280. memcpy(&out_test[q_test], out, len);
  281. q_test += len;
  282. }
  283. q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);
  284. test_string_check_buf(name, flags, in, p, out_real, q_real, out_test,
  285. q_test);
  286. test_string_escape_overflow(in, p, flags, esc, q_test, name);
  287. out:
  288. kfree(in);
  289. kfree(out_real);
  290. kfree(out_test);
  291. }
  292. #define string_get_size_maxbuf 16
  293. #define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
  294. do { \
  295. BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
  296. BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
  297. __test_string_get_size((size), (blk_size), (exp_result10), \
  298. (exp_result2)); \
  299. } while (0)
  300. static __init void test_string_get_size_check(const char *units,
  301. const char *exp,
  302. char *res,
  303. const u64 size,
  304. const u64 blk_size)
  305. {
  306. if (!memcmp(res, exp, strlen(exp) + 1))
  307. return;
  308. res[string_get_size_maxbuf - 1] = '\0';
  309. pr_warn("Test 'test_string_get_size' failed!\n");
  310. pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
  311. size, blk_size, units);
  312. pr_warn("expected: '%s', got '%s'\n", exp, res);
  313. }
  314. static __init void __test_string_get_size(const u64 size, const u64 blk_size,
  315. const char *exp_result10,
  316. const char *exp_result2)
  317. {
  318. char buf10[string_get_size_maxbuf];
  319. char buf2[string_get_size_maxbuf];
  320. string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
  321. string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
  322. test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
  323. size, blk_size);
  324. test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
  325. size, blk_size);
  326. }
  327. static __init void test_string_get_size(void)
  328. {
  329. /* small values */
  330. test_string_get_size_one(0, 512, "0 B", "0 B");
  331. test_string_get_size_one(1, 512, "512 B", "512 B");
  332. test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
  333. /* normal values */
  334. test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
  335. test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
  336. test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
  337. /* weird block sizes */
  338. test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
  339. /* huge values */
  340. test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
  341. test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
  342. }
  343. static int __init test_string_helpers_init(void)
  344. {
  345. unsigned int i;
  346. pr_info("Running tests...\n");
  347. for (i = 0; i < UNESCAPE_ANY + 1; i++)
  348. test_string_unescape("unescape", i, false);
  349. test_string_unescape("unescape inplace",
  350. get_random_int() % (UNESCAPE_ANY + 1), true);
  351. /* Without dictionary */
  352. for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
  353. test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
  354. /* With dictionary */
  355. for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
  356. test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
  357. /* Test string_get_size() */
  358. test_string_get_size();
  359. return -EINVAL;
  360. }
  361. module_init(test_string_helpers_init);
  362. MODULE_LICENSE("Dual BSD/GPL");