test_printf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for printf facility.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/random.h>
  11. #include <linux/rtc.h>
  12. #include <linux/slab.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. #include "../tools/testing/selftests/kselftest_module.h"
  22. #define BUF_SIZE 256
  23. #define PAD_SIZE 16
  24. #define FILL_CHAR '$'
  25. KSTM_MODULE_GLOBALS();
  26. static char *test_buffer __initdata;
  27. static char *alloced_buffer __initdata;
  28. extern bool no_hash_pointers;
  29. static int __printf(4, 0) __init
  30. do_test(int bufsize, const char *expect, int elen,
  31. const char *fmt, va_list ap)
  32. {
  33. va_list aq;
  34. int ret, written;
  35. total_tests++;
  36. memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
  37. va_copy(aq, ap);
  38. ret = vsnprintf(test_buffer, bufsize, fmt, aq);
  39. va_end(aq);
  40. if (ret != elen) {
  41. pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
  42. bufsize, fmt, ret, elen);
  43. return 1;
  44. }
  45. if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
  46. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
  47. return 1;
  48. }
  49. if (!bufsize) {
  50. if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
  51. pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
  52. fmt);
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. written = min(bufsize-1, elen);
  58. if (test_buffer[written]) {
  59. pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
  60. bufsize, fmt);
  61. return 1;
  62. }
  63. if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
  64. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
  65. bufsize, fmt);
  66. return 1;
  67. }
  68. if (memcmp(test_buffer, expect, written)) {
  69. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
  70. bufsize, fmt, test_buffer, written, expect);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. static void __printf(3, 4) __init
  76. __test(const char *expect, int elen, const char *fmt, ...)
  77. {
  78. va_list ap;
  79. int rand;
  80. char *p;
  81. if (elen >= BUF_SIZE) {
  82. pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
  83. elen, fmt);
  84. failed_tests++;
  85. return;
  86. }
  87. va_start(ap, fmt);
  88. /*
  89. * Every fmt+args is subjected to four tests: Three where we
  90. * tell vsnprintf varying buffer sizes (plenty, not quite
  91. * enough and 0), and then we also test that kvasprintf would
  92. * be able to print it as expected.
  93. */
  94. failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
  95. rand = 1 + prandom_u32_max(elen+1);
  96. /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
  97. failed_tests += do_test(rand, expect, elen, fmt, ap);
  98. failed_tests += do_test(0, expect, elen, fmt, ap);
  99. p = kvasprintf(GFP_KERNEL, fmt, ap);
  100. if (p) {
  101. total_tests++;
  102. if (memcmp(p, expect, elen+1)) {
  103. pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
  104. fmt, p, expect);
  105. failed_tests++;
  106. }
  107. kfree(p);
  108. }
  109. va_end(ap);
  110. }
  111. #define test(expect, fmt, ...) \
  112. __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
  113. static void __init
  114. test_basic(void)
  115. {
  116. /* Work around annoying "warning: zero-length gnu_printf format string". */
  117. char nul = '\0';
  118. test("", &nul);
  119. test("100%", "100%%");
  120. test("xxx%yyy", "xxx%cyyy", '%');
  121. __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
  122. }
  123. static void __init
  124. test_number(void)
  125. {
  126. test("0x1234abcd ", "%#-12x", 0x1234abcd);
  127. test(" 0x1234abcd", "%#12x", 0x1234abcd);
  128. test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
  129. test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
  130. test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
  131. test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
  132. /*
  133. * POSIX/C99: »The result of converting zero with an explicit
  134. * precision of zero shall be no characters.« Hence the output
  135. * from the below test should really be "00|0||| ". However,
  136. * the kernel's printf also produces a single 0 in that
  137. * case. This test case simply documents the current
  138. * behaviour.
  139. */
  140. test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
  141. #ifndef __CHAR_UNSIGNED__
  142. {
  143. /*
  144. * Passing a 'char' to a %02x specifier doesn't do
  145. * what was presumably the intention when char is
  146. * signed and the value is negative. One must either &
  147. * with 0xff or cast to u8.
  148. */
  149. char val = -16;
  150. test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
  151. }
  152. #endif
  153. }
  154. static void __init
  155. test_string(void)
  156. {
  157. test("", "%s%.0s", "", "123");
  158. test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
  159. test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
  160. test("1234 ", "%-10.4s", "123456");
  161. test(" 1234", "%10.4s", "123456");
  162. /*
  163. * POSIX and C99 say that a negative precision (which is only
  164. * possible to pass via a * argument) should be treated as if
  165. * the precision wasn't present, and that if the precision is
  166. * omitted (as in %.s), the precision should be taken to be
  167. * 0. However, the kernel's printf behave exactly opposite,
  168. * treating a negative precision as 0 and treating an omitted
  169. * precision specifier as if no precision was given.
  170. *
  171. * These test cases document the current behaviour; should
  172. * anyone ever feel the need to follow the standards more
  173. * closely, this can be revisited.
  174. */
  175. test(" ", "%4.*s", -5, "123456");
  176. test("123456", "%.s", "123456");
  177. test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
  178. test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
  179. }
  180. #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
  181. #if BITS_PER_LONG == 64
  182. #define PTR_WIDTH 16
  183. #define PTR ((void *)0xffff0123456789abUL)
  184. #define PTR_STR "ffff0123456789ab"
  185. #define PTR_VAL_NO_CRNG "(____ptrval____)"
  186. #define ZEROS "00000000" /* hex 32 zero bits */
  187. #define ONES "ffffffff" /* hex 32 one bits */
  188. static int __init
  189. plain_format(void)
  190. {
  191. char buf[PLAIN_BUF_SIZE];
  192. int nchars;
  193. nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
  194. if (nchars != PTR_WIDTH)
  195. return -1;
  196. if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
  197. pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
  198. PTR_VAL_NO_CRNG);
  199. return 0;
  200. }
  201. if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
  202. return -1;
  203. return 0;
  204. }
  205. #else
  206. #define PTR_WIDTH 8
  207. #define PTR ((void *)0x456789ab)
  208. #define PTR_STR "456789ab"
  209. #define PTR_VAL_NO_CRNG "(ptrval)"
  210. #define ZEROS ""
  211. #define ONES ""
  212. static int __init
  213. plain_format(void)
  214. {
  215. /* Format is implicitly tested for 32 bit machines by plain_hash() */
  216. return 0;
  217. }
  218. #endif /* BITS_PER_LONG == 64 */
  219. static int __init
  220. plain_hash_to_buffer(const void *p, char *buf, size_t len)
  221. {
  222. int nchars;
  223. nchars = snprintf(buf, len, "%p", p);
  224. if (nchars != PTR_WIDTH)
  225. return -1;
  226. if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
  227. pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
  228. PTR_VAL_NO_CRNG);
  229. return 0;
  230. }
  231. return 0;
  232. }
  233. static int __init
  234. plain_hash(void)
  235. {
  236. char buf[PLAIN_BUF_SIZE];
  237. int ret;
  238. ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
  239. if (ret)
  240. return ret;
  241. if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
  242. return -1;
  243. return 0;
  244. }
  245. /*
  246. * We can't use test() to test %p because we don't know what output to expect
  247. * after an address is hashed.
  248. */
  249. static void __init
  250. plain(void)
  251. {
  252. int err;
  253. if (no_hash_pointers) {
  254. pr_warn("skipping plain 'p' tests");
  255. skipped_tests += 2;
  256. return;
  257. }
  258. err = plain_hash();
  259. if (err) {
  260. pr_warn("plain 'p' does not appear to be hashed\n");
  261. failed_tests++;
  262. return;
  263. }
  264. err = plain_format();
  265. if (err) {
  266. pr_warn("hashing plain 'p' has unexpected format\n");
  267. failed_tests++;
  268. }
  269. }
  270. static void __init
  271. test_hashed(const char *fmt, const void *p)
  272. {
  273. char buf[PLAIN_BUF_SIZE];
  274. int ret;
  275. /*
  276. * No need to increase failed test counter since this is assumed
  277. * to be called after plain().
  278. */
  279. ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
  280. if (ret)
  281. return;
  282. test(buf, fmt, p);
  283. }
  284. /*
  285. * NULL pointers aren't hashed.
  286. */
  287. static void __init
  288. null_pointer(void)
  289. {
  290. test(ZEROS "00000000", "%p", NULL);
  291. test(ZEROS "00000000", "%px", NULL);
  292. test("(null)", "%pE", NULL);
  293. }
  294. /*
  295. * Error pointers aren't hashed.
  296. */
  297. static void __init
  298. error_pointer(void)
  299. {
  300. test(ONES "fffffff5", "%p", ERR_PTR(-11));
  301. test(ONES "fffffff5", "%px", ERR_PTR(-11));
  302. test("(efault)", "%pE", ERR_PTR(-11));
  303. }
  304. #define PTR_INVALID ((void *)0x000000ab)
  305. static void __init
  306. invalid_pointer(void)
  307. {
  308. test_hashed("%p", PTR_INVALID);
  309. test(ZEROS "000000ab", "%px", PTR_INVALID);
  310. test("(efault)", "%pE", PTR_INVALID);
  311. }
  312. static void __init
  313. symbol_ptr(void)
  314. {
  315. }
  316. static void __init
  317. kernel_ptr(void)
  318. {
  319. /* We can't test this without access to kptr_restrict. */
  320. }
  321. static void __init
  322. struct_resource(void)
  323. {
  324. }
  325. static void __init
  326. addr(void)
  327. {
  328. }
  329. static void __init
  330. escaped_str(void)
  331. {
  332. }
  333. static void __init
  334. hex_string(void)
  335. {
  336. const char buf[3] = {0xc0, 0xff, 0xee};
  337. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  338. "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
  339. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  340. "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
  341. }
  342. static void __init
  343. mac(void)
  344. {
  345. const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
  346. test("2d:48:d6:fc:7a:05", "%pM", addr);
  347. test("05:7a:fc:d6:48:2d", "%pMR", addr);
  348. test("2d-48-d6-fc-7a-05", "%pMF", addr);
  349. test("2d48d6fc7a05", "%pm", addr);
  350. test("057afcd6482d", "%pmR", addr);
  351. }
  352. static void __init
  353. ip4(void)
  354. {
  355. struct sockaddr_in sa;
  356. sa.sin_family = AF_INET;
  357. sa.sin_port = cpu_to_be16(12345);
  358. sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
  359. test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
  360. test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
  361. sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
  362. test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
  363. }
  364. static void __init
  365. ip6(void)
  366. {
  367. }
  368. static void __init
  369. ip(void)
  370. {
  371. ip4();
  372. ip6();
  373. }
  374. static void __init
  375. uuid(void)
  376. {
  377. const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
  378. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  379. test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
  380. test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
  381. test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
  382. test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
  383. }
  384. static struct dentry test_dentry[4] __initdata = {
  385. { .d_parent = &test_dentry[0],
  386. .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
  387. .d_iname = "foo" },
  388. { .d_parent = &test_dentry[0],
  389. .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
  390. .d_iname = "bravo" },
  391. { .d_parent = &test_dentry[1],
  392. .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
  393. .d_iname = "alfa" },
  394. { .d_parent = &test_dentry[2],
  395. .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
  396. .d_iname = "romeo" },
  397. };
  398. static void __init
  399. dentry(void)
  400. {
  401. test("foo", "%pd", &test_dentry[0]);
  402. test("foo", "%pd2", &test_dentry[0]);
  403. test("(null)", "%pd", NULL);
  404. test("(efault)", "%pd", PTR_INVALID);
  405. test("(null)", "%pD", NULL);
  406. test("(efault)", "%pD", PTR_INVALID);
  407. test("romeo", "%pd", &test_dentry[3]);
  408. test("alfa/romeo", "%pd2", &test_dentry[3]);
  409. test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
  410. test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
  411. test("/bravo/alfa", "%pd4", &test_dentry[2]);
  412. test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
  413. test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
  414. }
  415. static void __init
  416. struct_va_format(void)
  417. {
  418. }
  419. static void __init
  420. time_and_date(void)
  421. {
  422. /* 1543210543 */
  423. const struct rtc_time tm = {
  424. .tm_sec = 43,
  425. .tm_min = 35,
  426. .tm_hour = 5,
  427. .tm_mday = 26,
  428. .tm_mon = 10,
  429. .tm_year = 118,
  430. };
  431. /* 2019-01-04T15:32:23 */
  432. time64_t t = 1546615943;
  433. test("(%pt?)", "%pt", &tm);
  434. test("2018-11-26T05:35:43", "%ptR", &tm);
  435. test("0118-10-26T05:35:43", "%ptRr", &tm);
  436. test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
  437. test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
  438. test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
  439. test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
  440. test("2019-01-04T15:32:23", "%ptT", &t);
  441. test("0119-00-04T15:32:23", "%ptTr", &t);
  442. test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
  443. test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
  444. }
  445. static void __init
  446. struct_clk(void)
  447. {
  448. }
  449. static void __init
  450. large_bitmap(void)
  451. {
  452. const int nbits = 1 << 16;
  453. unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
  454. if (!bits)
  455. return;
  456. bitmap_set(bits, 1, 20);
  457. bitmap_set(bits, 60000, 15);
  458. test("1-20,60000-60014", "%*pbl", nbits, bits);
  459. bitmap_free(bits);
  460. }
  461. static void __init
  462. bitmap(void)
  463. {
  464. DECLARE_BITMAP(bits, 20);
  465. const int primes[] = {2,3,5,7,11,13,17,19};
  466. int i;
  467. bitmap_zero(bits, 20);
  468. test("00000|00000", "%20pb|%*pb", bits, 20, bits);
  469. test("|", "%20pbl|%*pbl", bits, 20, bits);
  470. for (i = 0; i < ARRAY_SIZE(primes); ++i)
  471. set_bit(primes[i], bits);
  472. test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
  473. test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
  474. bitmap_fill(bits, 20);
  475. test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
  476. test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
  477. large_bitmap();
  478. }
  479. static void __init
  480. netdev_features(void)
  481. {
  482. }
  483. static void __init
  484. flags(void)
  485. {
  486. unsigned long flags;
  487. gfp_t gfp;
  488. char *cmp_buffer;
  489. flags = 0;
  490. test("", "%pGp", &flags);
  491. /* Page flags should filter the zone id */
  492. flags = 1UL << NR_PAGEFLAGS;
  493. test("", "%pGp", &flags);
  494. flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
  495. | 1UL << PG_active | 1UL << PG_swapbacked;
  496. test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
  497. flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
  498. | VM_DENYWRITE;
  499. test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
  500. gfp = GFP_TRANSHUGE;
  501. test("GFP_TRANSHUGE", "%pGg", &gfp);
  502. gfp = GFP_ATOMIC|__GFP_DMA;
  503. test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
  504. gfp = __GFP_ATOMIC;
  505. test("__GFP_ATOMIC", "%pGg", &gfp);
  506. cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
  507. if (!cmp_buffer)
  508. return;
  509. /* Any flags not translated by the table should remain numeric */
  510. gfp = ~__GFP_BITS_MASK;
  511. snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
  512. test(cmp_buffer, "%pGg", &gfp);
  513. snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
  514. (unsigned long) gfp);
  515. gfp |= __GFP_ATOMIC;
  516. test(cmp_buffer, "%pGg", &gfp);
  517. kfree(cmp_buffer);
  518. }
  519. static void __init fwnode_pointer(void)
  520. {
  521. const struct software_node softnodes[] = {
  522. { .name = "first", },
  523. { .name = "second", .parent = &softnodes[0], },
  524. { .name = "third", .parent = &softnodes[1], },
  525. { NULL /* Guardian */ }
  526. };
  527. const char * const full_name = "first/second/third";
  528. const char * const full_name_second = "first/second";
  529. const char * const second_name = "second";
  530. const char * const third_name = "third";
  531. int rval;
  532. rval = software_node_register_nodes(softnodes);
  533. if (rval) {
  534. pr_warn("cannot register softnodes; rval %d\n", rval);
  535. return;
  536. }
  537. test(full_name_second, "%pfw", software_node_fwnode(&softnodes[1]));
  538. test(full_name, "%pfw", software_node_fwnode(&softnodes[2]));
  539. test(full_name, "%pfwf", software_node_fwnode(&softnodes[2]));
  540. test(second_name, "%pfwP", software_node_fwnode(&softnodes[1]));
  541. test(third_name, "%pfwP", software_node_fwnode(&softnodes[2]));
  542. software_node_unregister(&softnodes[2]);
  543. software_node_unregister(&softnodes[1]);
  544. software_node_unregister(&softnodes[0]);
  545. }
  546. static void __init
  547. errptr(void)
  548. {
  549. test("-1234", "%pe", ERR_PTR(-1234));
  550. /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
  551. BUILD_BUG_ON(IS_ERR(PTR));
  552. test_hashed("%pe", PTR);
  553. #ifdef CONFIG_SYMBOLIC_ERRNAME
  554. test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
  555. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
  556. BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
  557. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
  558. test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
  559. test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
  560. test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
  561. #endif
  562. }
  563. static void __init
  564. test_pointer(void)
  565. {
  566. plain();
  567. null_pointer();
  568. error_pointer();
  569. invalid_pointer();
  570. symbol_ptr();
  571. kernel_ptr();
  572. struct_resource();
  573. addr();
  574. escaped_str();
  575. hex_string();
  576. mac();
  577. ip();
  578. uuid();
  579. dentry();
  580. struct_va_format();
  581. time_and_date();
  582. struct_clk();
  583. bitmap();
  584. netdev_features();
  585. flags();
  586. errptr();
  587. fwnode_pointer();
  588. }
  589. static void __init selftest(void)
  590. {
  591. alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
  592. if (!alloced_buffer)
  593. return;
  594. test_buffer = alloced_buffer + PAD_SIZE;
  595. test_basic();
  596. test_number();
  597. test_string();
  598. test_pointer();
  599. kfree(alloced_buffer);
  600. }
  601. KSTM_MODULE_LOADERS(test_printf);
  602. MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
  603. MODULE_LICENSE("GPL");