printf_kunit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for printf facility.
  4. */
  5. #include <kunit/test.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/printk.h>
  9. #include <linux/random.h>
  10. #include <linux/rtc.h>
  11. #include <linux/slab.h>
  12. #include <linux/sprintf.h>
  13. #include <linux/string.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/dcache.h>
  16. #include <linux/socket.h>
  17. #include <linux/in.h>
  18. #include <linux/gfp.h>
  19. #include <linux/mm.h>
  20. #include <linux/property.h>
  21. #define BUF_SIZE 256
  22. #define PAD_SIZE 16
  23. #define FILL_CHAR '$'
  24. #define NOWARN(option, comment, block) \
  25. __diag_push(); \
  26. __diag_ignore_all(#option, comment); \
  27. block \
  28. __diag_pop();
  29. static unsigned int total_tests;
  30. static char *test_buffer;
  31. static char *alloced_buffer;
  32. static void __printf(7, 0)
  33. do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect,
  34. int elen, const char *fmt, va_list ap)
  35. {
  36. va_list aq;
  37. int ret, written;
  38. total_tests++;
  39. memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
  40. va_copy(aq, ap);
  41. ret = vsnprintf(test_buffer, bufsize, fmt, aq);
  42. va_end(aq);
  43. if (ret != elen) {
  44. KUNIT_FAIL(kunittest,
  45. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
  46. file, line, bufsize, fmt, ret, elen);
  47. return;
  48. }
  49. if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
  50. KUNIT_FAIL(kunittest,
  51. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n",
  52. file, line, bufsize, fmt);
  53. return;
  54. }
  55. if (!bufsize) {
  56. if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
  57. KUNIT_FAIL(kunittest,
  58. "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
  59. file, line, fmt);
  60. }
  61. return;
  62. }
  63. written = min(bufsize-1, elen);
  64. if (test_buffer[written]) {
  65. KUNIT_FAIL(kunittest,
  66. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
  67. file, line, bufsize, fmt);
  68. return;
  69. }
  70. if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
  71. KUNIT_FAIL(kunittest,
  72. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
  73. file, line, bufsize, fmt);
  74. return;
  75. }
  76. if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
  77. KUNIT_FAIL(kunittest,
  78. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
  79. file, line, bufsize, fmt);
  80. return;
  81. }
  82. if (memcmp(test_buffer, expect, written)) {
  83. KUNIT_FAIL(kunittest,
  84. "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
  85. file, line, bufsize, fmt, test_buffer, written, expect);
  86. return;
  87. }
  88. }
  89. static void __printf(6, 7)
  90. __test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen,
  91. const char *fmt, ...)
  92. {
  93. va_list ap;
  94. int rand;
  95. char *p;
  96. if (elen >= BUF_SIZE) {
  97. KUNIT_FAIL(kunittest,
  98. "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
  99. file, line, elen, BUF_SIZE, fmt);
  100. return;
  101. }
  102. va_start(ap, fmt);
  103. /*
  104. * Every fmt+args is subjected to four tests: Three where we
  105. * tell vsnprintf varying buffer sizes (plenty, not quite
  106. * enough and 0), and then we also test that kvasprintf would
  107. * be able to print it as expected.
  108. */
  109. do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap);
  110. rand = get_random_u32_inclusive(1, elen + 1);
  111. /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
  112. do_test(kunittest, file, line, rand, expect, elen, fmt, ap);
  113. do_test(kunittest, file, line, 0, expect, elen, fmt, ap);
  114. p = kvasprintf(GFP_KERNEL, fmt, ap);
  115. if (p) {
  116. total_tests++;
  117. if (memcmp(p, expect, elen+1)) {
  118. KUNIT_FAIL(kunittest,
  119. "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
  120. file, line, fmt, p, expect);
  121. }
  122. kfree(p);
  123. }
  124. va_end(ap);
  125. }
  126. #define test(expect, fmt, ...) \
  127. __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__)
  128. static void
  129. test_basic(struct kunit *kunittest)
  130. {
  131. /* Work around annoying "warning: zero-length gnu_printf format string". */
  132. char nul = '\0';
  133. test("", &nul);
  134. test("100%", "100%%");
  135. test("xxx%yyy", "xxx%cyyy", '%');
  136. __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0');
  137. }
  138. static void
  139. test_number(struct kunit *kunittest)
  140. {
  141. test("0x1234abcd ", "%#-12x", 0x1234abcd);
  142. test(" 0x1234abcd", "%#12x", 0x1234abcd);
  143. test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
  144. NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
  145. test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
  146. test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
  147. test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
  148. })
  149. /*
  150. * POSIX/C99: »The result of converting zero with an explicit
  151. * precision of zero shall be no characters.« Hence the output
  152. * from the below test should really be "00|0||| ". However,
  153. * the kernel's printf also produces a single 0 in that
  154. * case. This test case simply documents the current
  155. * behaviour.
  156. */
  157. test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
  158. }
  159. static void
  160. test_string(struct kunit *kunittest)
  161. {
  162. test("", "%s%.0s", "", "123");
  163. test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
  164. test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
  165. test("1234 ", "%-10.4s", "123456");
  166. test(" 1234", "%10.4s", "123456");
  167. /*
  168. * POSIX and C99 say that a negative precision (which is only
  169. * possible to pass via a * argument) should be treated as if
  170. * the precision wasn't present, and that if the precision is
  171. * omitted (as in %.s), the precision should be taken to be
  172. * 0. However, the kernel's printf behave exactly opposite,
  173. * treating a negative precision as 0 and treating an omitted
  174. * precision specifier as if no precision was given.
  175. *
  176. * These test cases document the current behaviour; should
  177. * anyone ever feel the need to follow the standards more
  178. * closely, this can be revisited.
  179. */
  180. test(" ", "%4.*s", -5, "123456");
  181. test("123456", "%.s", "123456");
  182. test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
  183. test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
  184. }
  185. #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
  186. #if BITS_PER_LONG == 64
  187. #define PTR_WIDTH 16
  188. #define PTR ((void *)0xffff0123456789abUL)
  189. #define PTR_STR "ffff0123456789ab"
  190. #define PTR_VAL_NO_CRNG "(____ptrval____)"
  191. #define ZEROS "00000000" /* hex 32 zero bits */
  192. #define ONES "ffffffff" /* hex 32 one bits */
  193. #else
  194. #define PTR_WIDTH 8
  195. #define PTR ((void *)0x456789ab)
  196. #define PTR_STR "456789ab"
  197. #define PTR_VAL_NO_CRNG "(ptrval)"
  198. #define ZEROS ""
  199. #define ONES ""
  200. #endif /* BITS_PER_LONG == 64 */
  201. static void
  202. plain_hash_to_buffer(struct kunit *kunittest, const void *p, char *buf, size_t len)
  203. {
  204. KUNIT_ASSERT_EQ(kunittest, snprintf(buf, len, "%p", p), PTR_WIDTH);
  205. if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
  206. kunit_skip(kunittest,
  207. "crng possibly not yet initialized. plain 'p' buffer contains \"%s\"\n",
  208. PTR_VAL_NO_CRNG);
  209. }
  210. }
  211. static void
  212. hash_pointer(struct kunit *kunittest)
  213. {
  214. if (no_hash_pointers)
  215. kunit_skip(kunittest, "hash pointers disabled");
  216. char buf[PLAIN_BUF_SIZE];
  217. plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE);
  218. /*
  219. * The hash of %p is unpredictable, therefore test() cannot be used.
  220. *
  221. * Instead verify that the first 32 bits are zeros on a 64-bit system
  222. * and that the non-hashed value is not printed.
  223. */
  224. KUNIT_EXPECT_MEMEQ(kunittest, buf, ZEROS, strlen(ZEROS));
  225. KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH);
  226. }
  227. /*
  228. * This is a macro so that the compiler can compare its arguments to the
  229. * __printf() attribute on __test(). This cannot be a function with a __printf()
  230. * attribute because GCC requires __printf() functions to be variadic.
  231. */
  232. #define test_hashed(kunittest, fmt, p) \
  233. do { \
  234. char buf[PLAIN_BUF_SIZE]; \
  235. plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \
  236. test(buf, fmt, p); \
  237. } while (0)
  238. /*
  239. * NULL pointers aren't hashed.
  240. */
  241. static void
  242. null_pointer(struct kunit *kunittest)
  243. {
  244. test(ZEROS "00000000", "%p", NULL);
  245. test(ZEROS "00000000", "%px", NULL);
  246. test("(null)", "%pE", NULL);
  247. }
  248. /*
  249. * Error pointers aren't hashed.
  250. */
  251. static void
  252. error_pointer(struct kunit *kunittest)
  253. {
  254. test(ONES "fffffff5", "%p", ERR_PTR(-11));
  255. test(ONES "fffffff5", "%px", ERR_PTR(-11));
  256. test("(efault)", "%pE", ERR_PTR(-11));
  257. }
  258. #define PTR_INVALID ((void *)0x000000ab)
  259. static void
  260. invalid_pointer(struct kunit *kunittest)
  261. {
  262. test_hashed(kunittest, "%p", PTR_INVALID);
  263. test(ZEROS "000000ab", "%px", PTR_INVALID);
  264. test("(efault)", "%pE", PTR_INVALID);
  265. }
  266. static void
  267. symbol_ptr(struct kunit *kunittest)
  268. {
  269. }
  270. static void
  271. kernel_ptr(struct kunit *kunittest)
  272. {
  273. /* We can't test this without access to kptr_restrict. */
  274. }
  275. static void
  276. struct_resource(struct kunit *kunittest)
  277. {
  278. struct resource test_resource = {
  279. .start = 0xc0ffee00,
  280. .end = 0xc0ffee00,
  281. .flags = IORESOURCE_MEM,
  282. };
  283. test("[mem 0xc0ffee00 flags 0x200]",
  284. "%pr", &test_resource);
  285. test_resource = (struct resource) {
  286. .start = 0xc0ffee,
  287. .end = 0xba5eba11,
  288. .flags = IORESOURCE_MEM,
  289. };
  290. test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]",
  291. "%pr", &test_resource);
  292. test_resource = (struct resource) {
  293. .start = 0xba5eba11,
  294. .end = 0xc0ffee,
  295. .flags = IORESOURCE_MEM,
  296. };
  297. test("[mem 0xba5eba11-0x00c0ffee flags 0x200]",
  298. "%pr", &test_resource);
  299. test_resource = (struct resource) {
  300. .start = 0xba5eba11,
  301. .end = 0xba5eca11,
  302. .flags = IORESOURCE_MEM,
  303. };
  304. test("[mem 0xba5eba11-0xba5eca11 flags 0x200]",
  305. "%pr", &test_resource);
  306. test_resource = (struct resource) {
  307. .start = 0xba11,
  308. .end = 0xca10,
  309. .flags = IORESOURCE_IO |
  310. IORESOURCE_DISABLED |
  311. IORESOURCE_UNSET,
  312. };
  313. test("[io size 0x1000 disabled]",
  314. "%pR", &test_resource);
  315. }
  316. static void
  317. struct_range(struct kunit *kunittest)
  318. {
  319. struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11,
  320. 0xc0ffee00ba5eba11);
  321. test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range);
  322. test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11);
  323. test("[range 0x0000000000c0ffee-0x00000000ba5eba11]",
  324. "%pra", &test_range);
  325. test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee);
  326. test("[range 0x00000000ba5eba11-0x0000000000c0ffee]",
  327. "%pra", &test_range);
  328. }
  329. static void
  330. addr(struct kunit *kunittest)
  331. {
  332. }
  333. static void
  334. escaped_str(struct kunit *kunittest)
  335. {
  336. }
  337. static void
  338. hex_string(struct kunit *kunittest)
  339. {
  340. const char buf[3] = {0xc0, 0xff, 0xee};
  341. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  342. "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
  343. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  344. "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
  345. }
  346. static void
  347. mac(struct kunit *kunittest)
  348. {
  349. const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
  350. test("2d:48:d6:fc:7a:05", "%pM", addr);
  351. test("05:7a:fc:d6:48:2d", "%pMR", addr);
  352. test("2d-48-d6-fc-7a-05", "%pMF", addr);
  353. test("2d48d6fc7a05", "%pm", addr);
  354. test("057afcd6482d", "%pmR", addr);
  355. }
  356. static void
  357. ip4(struct kunit *kunittest)
  358. {
  359. struct sockaddr_in sa;
  360. sa.sin_family = AF_INET;
  361. sa.sin_port = cpu_to_be16(12345);
  362. sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
  363. test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
  364. test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
  365. sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
  366. test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
  367. }
  368. static void
  369. ip6(struct kunit *kunittest)
  370. {
  371. }
  372. static void
  373. uuid(struct kunit *kunittest)
  374. {
  375. const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
  376. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  377. test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
  378. test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
  379. test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
  380. test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
  381. }
  382. static struct dentry test_dentry[4] = {
  383. { .d_parent = &test_dentry[0],
  384. .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
  385. .d_iname = "foo" },
  386. { .d_parent = &test_dentry[0],
  387. .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
  388. .d_iname = "bravo" },
  389. { .d_parent = &test_dentry[1],
  390. .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
  391. .d_iname = "alfa" },
  392. { .d_parent = &test_dentry[2],
  393. .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
  394. .d_iname = "romeo" },
  395. };
  396. static void
  397. dentry(struct kunit *kunittest)
  398. {
  399. test("foo", "%pd", &test_dentry[0]);
  400. test("foo", "%pd2", &test_dentry[0]);
  401. test("(null)", "%pd", NULL);
  402. test("(efault)", "%pd", PTR_INVALID);
  403. test("(null)", "%pD", NULL);
  404. test("(efault)", "%pD", PTR_INVALID);
  405. test("romeo", "%pd", &test_dentry[3]);
  406. test("alfa/romeo", "%pd2", &test_dentry[3]);
  407. test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
  408. test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
  409. test("/bravo/alfa", "%pd4", &test_dentry[2]);
  410. test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
  411. test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
  412. }
  413. static void
  414. struct_va_format(struct kunit *kunittest)
  415. {
  416. }
  417. static void
  418. time_and_date(struct kunit *kunittest)
  419. {
  420. /* 1543210543 */
  421. const struct rtc_time tm = {
  422. .tm_sec = 43,
  423. .tm_min = 35,
  424. .tm_hour = 5,
  425. .tm_mday = 26,
  426. .tm_mon = 10,
  427. .tm_year = 118,
  428. };
  429. /* 2019-01-04T15:32:23 */
  430. time64_t t = 1546615943;
  431. struct timespec64 ts = { .tv_sec = t, .tv_nsec = 11235813 };
  432. test("(%pt?)", "%pt", &tm);
  433. test("2018-11-26T05:35:43", "%ptR", &tm);
  434. test("0118-10-26T05:35:43", "%ptRr", &tm);
  435. test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
  436. test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
  437. test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
  438. test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
  439. test("2019-01-04T15:32:23", "%ptT", &t);
  440. test("0119-00-04T15:32:23", "%ptTr", &t);
  441. test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
  442. test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
  443. test("2019-01-04 15:32:23", "%ptTs", &t);
  444. test("0119-00-04 15:32:23", "%ptTsr", &t);
  445. test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
  446. test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
  447. test("2019-01-04T15:32:23.011235813", "%ptS", &ts);
  448. test("1546615943.011235813", "%ptSp", &ts);
  449. }
  450. static void
  451. struct_clk(struct kunit *kunittest)
  452. {
  453. }
  454. static void
  455. large_bitmap(struct kunit *kunittest)
  456. {
  457. const int nbits = 1 << 16;
  458. unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
  459. if (!bits)
  460. return;
  461. bitmap_set(bits, 1, 20);
  462. bitmap_set(bits, 60000, 15);
  463. test("1-20,60000-60014", "%*pbl", nbits, bits);
  464. bitmap_free(bits);
  465. }
  466. static void
  467. bitmap(struct kunit *kunittest)
  468. {
  469. DECLARE_BITMAP(bits, 20);
  470. const int primes[] = {2,3,5,7,11,13,17,19};
  471. int i;
  472. bitmap_zero(bits, 20);
  473. test("00000|00000", "%20pb|%*pb", bits, 20, bits);
  474. test("|", "%20pbl|%*pbl", bits, 20, bits);
  475. for (i = 0; i < ARRAY_SIZE(primes); ++i)
  476. set_bit(primes[i], bits);
  477. test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
  478. test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
  479. bitmap_fill(bits, 20);
  480. test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
  481. test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
  482. large_bitmap(kunittest);
  483. }
  484. static void
  485. netdev_features(struct kunit *kunittest)
  486. {
  487. }
  488. struct page_flags_test {
  489. int width;
  490. int shift;
  491. int mask;
  492. const char *fmt;
  493. const char *name;
  494. };
  495. static const struct page_flags_test pft[] = {
  496. {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
  497. "%d", "section"},
  498. {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
  499. "%d", "node"},
  500. {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
  501. "%d", "zone"},
  502. {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
  503. "%#x", "lastcpupid"},
  504. {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
  505. "%#x", "kasantag"},
  506. };
  507. static void
  508. page_flags_test(struct kunit *kunittest, int section, int node, int zone,
  509. int last_cpupid, int kasan_tag, unsigned long flags, const char *name,
  510. char *cmp_buf)
  511. {
  512. unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag};
  513. unsigned long size;
  514. bool append = false;
  515. int i;
  516. for (i = 0; i < ARRAY_SIZE(values); i++)
  517. flags |= (values[i] & pft[i].mask) << pft[i].shift;
  518. size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags);
  519. if (flags & PAGEFLAGS_MASK) {
  520. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
  521. append = true;
  522. }
  523. for (i = 0; i < ARRAY_SIZE(pft); i++) {
  524. if (!pft[i].width)
  525. continue;
  526. if (append)
  527. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
  528. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
  529. pft[i].name);
  530. size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
  531. values[i] & pft[i].mask);
  532. append = true;
  533. }
  534. snprintf(cmp_buf + size, BUF_SIZE - size, ")");
  535. test(cmp_buf, "%pGp", &flags);
  536. }
  537. static void
  538. flags(struct kunit *kunittest)
  539. {
  540. unsigned long flags;
  541. char *cmp_buffer;
  542. gfp_t gfp;
  543. cmp_buffer = kunit_kmalloc(kunittest, BUF_SIZE, GFP_KERNEL);
  544. KUNIT_ASSERT_NOT_NULL(kunittest, cmp_buffer);
  545. flags = 0;
  546. page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer);
  547. flags = 1UL << NR_PAGEFLAGS;
  548. page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer);
  549. flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
  550. | 1UL << PG_active | 1UL << PG_swapbacked;
  551. page_flags_test(kunittest, 1, 1, 1, 0x1fffff, 1, flags,
  552. "uptodate|dirty|lru|active|swapbacked",
  553. cmp_buffer);
  554. flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  555. test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);
  556. gfp = GFP_TRANSHUGE;
  557. test("GFP_TRANSHUGE", "%pGg", &gfp);
  558. gfp = GFP_ATOMIC|__GFP_DMA;
  559. test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
  560. gfp = __GFP_HIGH;
  561. test("__GFP_HIGH", "%pGg", &gfp);
  562. /* Any flags not translated by the table should remain numeric */
  563. gfp = ~__GFP_BITS_MASK;
  564. snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
  565. test(cmp_buffer, "%pGg", &gfp);
  566. snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx",
  567. (unsigned long) gfp);
  568. gfp |= __GFP_HIGH;
  569. test(cmp_buffer, "%pGg", &gfp);
  570. }
  571. static void fwnode_pointer(struct kunit *kunittest)
  572. {
  573. const struct software_node first = { .name = "first" };
  574. const struct software_node second = { .name = "second", .parent = &first };
  575. const struct software_node third = { .name = "third", .parent = &second };
  576. const struct software_node *group[] = { &first, &second, &third, NULL };
  577. const char * const full_name_second = "first/second";
  578. const char * const full_name_third = "first/second/third";
  579. const char * const second_name = "second";
  580. const char * const third_name = "third";
  581. int rval;
  582. rval = software_node_register_node_group(group);
  583. if (rval) {
  584. kunit_skip(kunittest, "cannot register softnodes; rval %d\n", rval);
  585. }
  586. test(full_name_second, "%pfw", software_node_fwnode(&second));
  587. test(full_name_third, "%pfw", software_node_fwnode(&third));
  588. test(full_name_third, "%pfwf", software_node_fwnode(&third));
  589. test(second_name, "%pfwP", software_node_fwnode(&second));
  590. test(third_name, "%pfwP", software_node_fwnode(&third));
  591. software_node_unregister_node_group(group);
  592. }
  593. struct fourcc_struct {
  594. u32 code;
  595. const char *str;
  596. };
  597. static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc,
  598. size_t n, const char *fmt)
  599. {
  600. size_t i;
  601. for (i = 0; i < n; i++)
  602. test(fc[i].str, fmt, &fc[i].code);
  603. }
  604. static void fourcc_pointer(struct kunit *kunittest)
  605. {
  606. static const struct fourcc_struct try_cc[] = {
  607. { 0x3231564e, "NV12 little-endian (0x3231564e)", },
  608. { 0xb231564e, "NV12 big-endian (0xb231564e)", },
  609. { 0x10111213, ".... little-endian (0x10111213)", },
  610. { 0x20303159, "Y10 little-endian (0x20303159)", },
  611. };
  612. static const struct fourcc_struct try_ch[] = {
  613. { 0x41424344, "ABCD (0x41424344)", },
  614. };
  615. static const struct fourcc_struct try_chR[] = {
  616. { 0x41424344, "DCBA (0x44434241)", },
  617. };
  618. static const struct fourcc_struct try_cl[] = {
  619. { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", },
  620. };
  621. static const struct fourcc_struct try_cb[] = {
  622. { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", },
  623. };
  624. fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc");
  625. fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch");
  626. fourcc_pointer_test(kunittest, try_chR, ARRAY_SIZE(try_chR), "%p4chR");
  627. fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl");
  628. fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
  629. }
  630. static void
  631. errptr(struct kunit *kunittest)
  632. {
  633. test("-1234", "%pe", ERR_PTR(-1234));
  634. /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
  635. BUILD_BUG_ON(IS_ERR(PTR));
  636. test_hashed(kunittest, "%pe", PTR);
  637. #ifdef CONFIG_SYMBOLIC_ERRNAME
  638. test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
  639. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
  640. BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
  641. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
  642. test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
  643. test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
  644. test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
  645. #endif
  646. }
  647. static int printf_suite_init(struct kunit_suite *suite)
  648. {
  649. total_tests = 0;
  650. alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
  651. if (!alloced_buffer)
  652. return -ENOMEM;
  653. test_buffer = alloced_buffer + PAD_SIZE;
  654. return 0;
  655. }
  656. static void printf_suite_exit(struct kunit_suite *suite)
  657. {
  658. kfree(alloced_buffer);
  659. kunit_info(suite, "ran %u tests\n", total_tests);
  660. }
  661. static struct kunit_case printf_test_cases[] = {
  662. KUNIT_CASE(test_basic),
  663. KUNIT_CASE(test_number),
  664. KUNIT_CASE(test_string),
  665. KUNIT_CASE(hash_pointer),
  666. KUNIT_CASE(null_pointer),
  667. KUNIT_CASE(error_pointer),
  668. KUNIT_CASE(invalid_pointer),
  669. KUNIT_CASE(symbol_ptr),
  670. KUNIT_CASE(kernel_ptr),
  671. KUNIT_CASE(struct_resource),
  672. KUNIT_CASE(struct_range),
  673. KUNIT_CASE(addr),
  674. KUNIT_CASE(escaped_str),
  675. KUNIT_CASE(hex_string),
  676. KUNIT_CASE(mac),
  677. KUNIT_CASE(ip4),
  678. KUNIT_CASE(ip6),
  679. KUNIT_CASE(uuid),
  680. KUNIT_CASE(dentry),
  681. KUNIT_CASE(struct_va_format),
  682. KUNIT_CASE(time_and_date),
  683. KUNIT_CASE(struct_clk),
  684. KUNIT_CASE(bitmap),
  685. KUNIT_CASE(netdev_features),
  686. KUNIT_CASE(flags),
  687. KUNIT_CASE(errptr),
  688. KUNIT_CASE(fwnode_pointer),
  689. KUNIT_CASE(fourcc_pointer),
  690. {}
  691. };
  692. static struct kunit_suite printf_test_suite = {
  693. .name = "printf",
  694. .suite_init = printf_suite_init,
  695. .suite_exit = printf_suite_exit,
  696. .test_cases = printf_test_cases,
  697. };
  698. kunit_test_suite(printf_test_suite);
  699. MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
  700. MODULE_DESCRIPTION("Test cases for printf facility");
  701. MODULE_LICENSE("GPL");