test_printf.c 8.0 KB

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