test_printf.c 9.9 KB

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