test_printf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Test cases for printf facility.
  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/printk.h>
  9. #include <linux/random.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/dcache.h>
  14. #include <linux/socket.h>
  15. #include <linux/in.h>
  16. #define BUF_SIZE 256
  17. #define PAD_SIZE 16
  18. #define FILL_CHAR '$'
  19. #define PTR1 ((void*)0x01234567)
  20. #define PTR2 ((void*)(long)(int)0xfedcba98)
  21. #if BITS_PER_LONG == 64
  22. #define PTR1_ZEROES "000000000"
  23. #define PTR1_SPACES " "
  24. #define PTR1_STR "1234567"
  25. #define PTR2_STR "fffffffffedcba98"
  26. #define PTR_WIDTH 16
  27. #else
  28. #define PTR1_ZEROES "0"
  29. #define PTR1_SPACES " "
  30. #define PTR1_STR "1234567"
  31. #define PTR2_STR "fedcba98"
  32. #define PTR_WIDTH 8
  33. #endif
  34. #define PTR_WIDTH_STR stringify(PTR_WIDTH)
  35. static unsigned total_tests __initdata;
  36. static unsigned failed_tests __initdata;
  37. static char *test_buffer __initdata;
  38. static char *alloced_buffer __initdata;
  39. static int __printf(4, 0) __init
  40. do_test(int bufsize, const char *expect, int elen,
  41. const char *fmt, va_list ap)
  42. {
  43. va_list aq;
  44. int ret, written;
  45. total_tests++;
  46. memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
  47. va_copy(aq, ap);
  48. ret = vsnprintf(test_buffer, bufsize, fmt, aq);
  49. va_end(aq);
  50. if (ret != elen) {
  51. pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
  52. bufsize, fmt, ret, elen);
  53. return 1;
  54. }
  55. if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
  56. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
  57. return 1;
  58. }
  59. if (!bufsize) {
  60. if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
  61. pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
  62. fmt);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. written = min(bufsize-1, elen);
  68. if (test_buffer[written]) {
  69. pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
  70. bufsize, fmt);
  71. return 1;
  72. }
  73. if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
  74. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
  75. bufsize, fmt);
  76. return 1;
  77. }
  78. if (memcmp(test_buffer, expect, written)) {
  79. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
  80. bufsize, fmt, test_buffer, written, expect);
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. static void __printf(3, 4) __init
  86. __test(const char *expect, int elen, const char *fmt, ...)
  87. {
  88. va_list ap;
  89. int rand;
  90. char *p;
  91. if (elen >= BUF_SIZE) {
  92. pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
  93. elen, fmt);
  94. failed_tests++;
  95. return;
  96. }
  97. va_start(ap, fmt);
  98. /*
  99. * Every fmt+args is subjected to four tests: Three where we
  100. * tell vsnprintf varying buffer sizes (plenty, not quite
  101. * enough and 0), and then we also test that kvasprintf would
  102. * be able to print it as expected.
  103. */
  104. failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
  105. rand = 1 + prandom_u32_max(elen+1);
  106. /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
  107. failed_tests += do_test(rand, expect, elen, fmt, ap);
  108. failed_tests += do_test(0, expect, elen, fmt, ap);
  109. p = kvasprintf(GFP_KERNEL, fmt, ap);
  110. if (p) {
  111. total_tests++;
  112. if (memcmp(p, expect, elen+1)) {
  113. pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
  114. fmt, p, expect);
  115. failed_tests++;
  116. }
  117. kfree(p);
  118. }
  119. va_end(ap);
  120. }
  121. #define test(expect, fmt, ...) \
  122. __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
  123. static void __init
  124. test_basic(void)
  125. {
  126. /* Work around annoying "warning: zero-length gnu_printf format string". */
  127. char nul = '\0';
  128. test("", &nul);
  129. test("100%", "100%%");
  130. test("xxx%yyy", "xxx%cyyy", '%');
  131. __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
  132. }
  133. static void __init
  134. test_number(void)
  135. {
  136. test("0x1234abcd ", "%#-12x", 0x1234abcd);
  137. test(" 0x1234abcd", "%#12x", 0x1234abcd);
  138. test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
  139. test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
  140. test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
  141. test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
  142. /*
  143. * POSIX/C99: »The result of converting zero with an explicit
  144. * precision of zero shall be no characters.« Hence the output
  145. * from the below test should really be "00|0||| ". However,
  146. * the kernel's printf also produces a single 0 in that
  147. * case. This test case simply documents the current
  148. * behaviour.
  149. */
  150. test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
  151. #ifndef __CHAR_UNSIGNED__
  152. {
  153. /*
  154. * Passing a 'char' to a %02x specifier doesn't do
  155. * what was presumably the intention when char is
  156. * signed and the value is negative. One must either &
  157. * with 0xff or cast to u8.
  158. */
  159. char val = -16;
  160. test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
  161. }
  162. #endif
  163. }
  164. static void __init
  165. test_string(void)
  166. {
  167. test("", "%s%.0s", "", "123");
  168. test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
  169. test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
  170. test("1234 ", "%-10.4s", "123456");
  171. test(" 1234", "%10.4s", "123456");
  172. /*
  173. * POSIX and C99 say that a negative precision (which is only
  174. * possible to pass via a * argument) should be treated as if
  175. * the precision wasn't present, and that if the precision is
  176. * omitted (as in %.s), the precision should be taken to be
  177. * 0. However, the kernel's printf behave exactly opposite,
  178. * treating a negative precision as 0 and treating an omitted
  179. * precision specifier as if no precision was given.
  180. *
  181. * These test cases document the current behaviour; should
  182. * anyone ever feel the need to follow the standards more
  183. * closely, this can be revisited.
  184. */
  185. test(" ", "%4.*s", -5, "123456");
  186. test("123456", "%.s", "123456");
  187. test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
  188. test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
  189. }
  190. static void __init
  191. plain(void)
  192. {
  193. test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
  194. /*
  195. * The field width is overloaded for some %p extensions to
  196. * pass another piece of information. For plain pointers, the
  197. * behaviour is slightly odd: One cannot pass either the 0
  198. * flag nor a precision to %p without gcc complaining, and if
  199. * one explicitly gives a field width, the number is no longer
  200. * zero-padded.
  201. */
  202. test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
  203. "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
  204. test("|" PTR2_STR " | " PTR2_STR "|",
  205. "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
  206. /*
  207. * Unrecognized %p extensions are treated as plain %p, but the
  208. * alphanumeric suffix is ignored (that is, does not occur in
  209. * the output.)
  210. */
  211. test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
  212. test("|"PTR2_STR"|", "|%p0y|", PTR2);
  213. }
  214. static void __init
  215. symbol_ptr(void)
  216. {
  217. }
  218. static void __init
  219. kernel_ptr(void)
  220. {
  221. }
  222. static void __init
  223. struct_resource(void)
  224. {
  225. }
  226. static void __init
  227. addr(void)
  228. {
  229. }
  230. static void __init
  231. escaped_str(void)
  232. {
  233. }
  234. static void __init
  235. hex_string(void)
  236. {
  237. const char buf[3] = {0xc0, 0xff, 0xee};
  238. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  239. "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
  240. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  241. "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
  242. }
  243. static void __init
  244. mac(void)
  245. {
  246. const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
  247. test("2d:48:d6:fc:7a:05", "%pM", addr);
  248. test("05:7a:fc:d6:48:2d", "%pMR", addr);
  249. test("2d-48-d6-fc-7a-05", "%pMF", addr);
  250. test("2d48d6fc7a05", "%pm", addr);
  251. test("057afcd6482d", "%pmR", addr);
  252. }
  253. static void __init
  254. ip4(void)
  255. {
  256. struct sockaddr_in sa;
  257. sa.sin_family = AF_INET;
  258. sa.sin_port = cpu_to_be16(12345);
  259. sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
  260. test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
  261. test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
  262. sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
  263. test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
  264. }
  265. static void __init
  266. ip6(void)
  267. {
  268. }
  269. static void __init
  270. ip(void)
  271. {
  272. ip4();
  273. ip6();
  274. }
  275. static void __init
  276. uuid(void)
  277. {
  278. const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
  279. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  280. test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
  281. test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
  282. test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
  283. test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
  284. }
  285. static struct dentry test_dentry[4] __initdata = {
  286. { .d_parent = &test_dentry[0],
  287. .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
  288. .d_iname = "foo" },
  289. { .d_parent = &test_dentry[0],
  290. .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
  291. .d_iname = "bravo" },
  292. { .d_parent = &test_dentry[1],
  293. .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
  294. .d_iname = "alfa" },
  295. { .d_parent = &test_dentry[2],
  296. .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
  297. .d_iname = "romeo" },
  298. };
  299. static void __init
  300. dentry(void)
  301. {
  302. test("foo", "%pd", &test_dentry[0]);
  303. test("foo", "%pd2", &test_dentry[0]);
  304. test("romeo", "%pd", &test_dentry[3]);
  305. test("alfa/romeo", "%pd2", &test_dentry[3]);
  306. test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
  307. test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
  308. test("/bravo/alfa", "%pd4", &test_dentry[2]);
  309. test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
  310. test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
  311. }
  312. static void __init
  313. struct_va_format(void)
  314. {
  315. }
  316. static void __init
  317. struct_clk(void)
  318. {
  319. }
  320. static void __init
  321. large_bitmap(void)
  322. {
  323. const int nbits = 1 << 16;
  324. unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
  325. if (!bits)
  326. return;
  327. bitmap_set(bits, 1, 20);
  328. bitmap_set(bits, 60000, 15);
  329. test("1-20,60000-60014", "%*pbl", nbits, bits);
  330. kfree(bits);
  331. }
  332. static void __init
  333. bitmap(void)
  334. {
  335. DECLARE_BITMAP(bits, 20);
  336. const int primes[] = {2,3,5,7,11,13,17,19};
  337. int i;
  338. bitmap_zero(bits, 20);
  339. test("00000|00000", "%20pb|%*pb", bits, 20, bits);
  340. test("|", "%20pbl|%*pbl", bits, 20, bits);
  341. for (i = 0; i < ARRAY_SIZE(primes); ++i)
  342. set_bit(primes[i], bits);
  343. test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
  344. test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
  345. bitmap_fill(bits, 20);
  346. test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
  347. test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
  348. large_bitmap();
  349. }
  350. static void __init
  351. netdev_features(void)
  352. {
  353. }
  354. static void __init
  355. test_pointer(void)
  356. {
  357. plain();
  358. symbol_ptr();
  359. kernel_ptr();
  360. struct_resource();
  361. addr();
  362. escaped_str();
  363. hex_string();
  364. mac();
  365. ip();
  366. uuid();
  367. dentry();
  368. struct_va_format();
  369. struct_clk();
  370. bitmap();
  371. netdev_features();
  372. }
  373. static int __init
  374. test_printf_init(void)
  375. {
  376. alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
  377. if (!alloced_buffer)
  378. return -ENOMEM;
  379. test_buffer = alloced_buffer + PAD_SIZE;
  380. test_basic();
  381. test_number();
  382. test_string();
  383. test_pointer();
  384. kfree(alloced_buffer);
  385. if (failed_tests == 0)
  386. pr_info("all %u tests passed\n", total_tests);
  387. else
  388. pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
  389. return failed_tests ? -EINVAL : 0;
  390. }
  391. module_init(test_printf_init);
  392. MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
  393. MODULE_LICENSE("GPL");