2 * Test cases for printf facility.
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/printk.h>
11 #include <linux/random.h>
12 #include <linux/rtc.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
16 #include <linux/bitmap.h>
17 #include <linux/dcache.h>
18 #include <linux/socket.h>
21 #include <linux/gfp.h>
24 #include "../tools/testing/selftests/kselftest_module.h"
30 static unsigned total_tests __initdata;
31 static unsigned failed_tests __initdata;
32 static char *test_buffer __initdata;
33 static char *alloced_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)
44 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
46 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
50 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
51 bufsize, fmt, ret, elen);
55 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
56 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
61 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
62 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
69 written = min(bufsize-1, elen);
70 if (test_buffer[written]) {
71 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
76 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
77 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
82 if (memcmp(test_buffer, expect, written)) {
83 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
84 bufsize, fmt, test_buffer, written, expect);
90 static void __printf(3, 4) __init
91 __test(const char *expect, int elen, const char *fmt, ...)
97 if (elen >= BUF_SIZE) {
98 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
107 * Every fmt+args is subjected to four tests: Three where we
108 * tell vsnprintf varying buffer sizes (plenty, not quite
109 * enough and 0), and then we also test that kvasprintf would
110 * be able to print it as expected.
112 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
113 rand = 1 + prandom_u32_max(elen+1);
114 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
115 failed_tests += do_test(rand, expect, elen, fmt, ap);
116 failed_tests += do_test(0, expect, elen, fmt, ap);
118 p = kvasprintf(GFP_KERNEL, fmt, ap);
121 if (memcmp(p, expect, elen+1)) {
122 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
131 #define test(expect, fmt, ...) \
132 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
137 /* Work around annoying "warning: zero-length gnu_printf format string". */
141 test("100%", "100%%");
142 test("xxx%yyy", "xxx%cyyy", '%');
143 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
149 test("0x1234abcd ", "%#-12x", 0x1234abcd);
150 test(" 0x1234abcd", "%#12x", 0x1234abcd);
151 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
152 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
153 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
154 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
156 * POSIX/C99: »The result of converting zero with an explicit
157 * precision of zero shall be no characters.« Hence the output
158 * from the below test should really be "00|0||| ". However,
159 * the kernel's printf also produces a single 0 in that
160 * case. This test case simply documents the current
163 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
164 #ifndef __CHAR_UNSIGNED__
167 * Passing a 'char' to a %02x specifier doesn't do
168 * what was presumably the intention when char is
169 * signed and the value is negative. One must either &
170 * with 0xff or cast to u8.
173 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
181 test("", "%s%.0s", "", "123");
182 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
183 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
184 test("1234 ", "%-10.4s", "123456");
185 test(" 1234", "%10.4s", "123456");
187 * POSIX and C99 say that a negative precision (which is only
188 * possible to pass via a * argument) should be treated as if
189 * the precision wasn't present, and that if the precision is
190 * omitted (as in %.s), the precision should be taken to be
191 * 0. However, the kernel's printf behave exactly opposite,
192 * treating a negative precision as 0 and treating an omitted
193 * precision specifier as if no precision was given.
195 * These test cases document the current behaviour; should
196 * anyone ever feel the need to follow the standards more
197 * closely, this can be revisited.
199 test(" ", "%4.*s", -5, "123456");
200 test("123456", "%.s", "123456");
201 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
202 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
205 #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
207 #if BITS_PER_LONG == 64
210 #define PTR ((void *)0xffff0123456789abUL)
211 #define PTR_STR "ffff0123456789ab"
212 #define PTR_VAL_NO_CRNG "(____ptrval____)"
213 #define ZEROS "00000000" /* hex 32 zero bits */
218 char buf[PLAIN_BUF_SIZE];
221 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
223 if (nchars != PTR_WIDTH)
226 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
227 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
232 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
241 #define PTR ((void *)0x456789ab)
242 #define PTR_STR "456789ab"
243 #define PTR_VAL_NO_CRNG "(ptrval)"
249 /* Format is implicitly tested for 32 bit machines by plain_hash() */
253 #endif /* BITS_PER_LONG == 64 */
256 plain_hash_to_buffer(const void *p, char *buf, size_t len)
260 nchars = snprintf(buf, len, "%p", p);
262 if (nchars != PTR_WIDTH)
265 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
266 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
277 char buf[PLAIN_BUF_SIZE];
280 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
284 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
291 * We can't use test() to test %p because we don't know what output to expect
292 * after an address is hashed.
301 pr_warn("plain 'p' does not appear to be hashed\n");
306 err = plain_format();
308 pr_warn("hashing plain 'p' has unexpected format\n");
314 test_hashed(const char *fmt, const void *p)
316 char buf[PLAIN_BUF_SIZE];
320 * No need to increase failed test counter since this is assumed
321 * to be called after plain().
323 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
333 test_hashed("%p", NULL);
334 test(ZEROS "00000000", "%px", NULL);
335 test("(null)", "%pE", NULL);
338 #define PTR_INVALID ((void *)0x000000ab)
341 invalid_pointer(void)
343 test_hashed("%p", PTR_INVALID);
344 test(ZEROS "000000ab", "%px", PTR_INVALID);
345 test("(efault)", "%pE", PTR_INVALID);
356 /* We can't test this without access to kptr_restrict. */
360 struct_resource(void)
377 const char buf[3] = {0xc0, 0xff, 0xee};
379 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
380 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
381 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
382 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
388 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
390 test("2d:48:d6:fc:7a:05", "%pM", addr);
391 test("05:7a:fc:d6:48:2d", "%pMR", addr);
392 test("2d-48-d6-fc-7a-05", "%pMF", addr);
393 test("2d48d6fc7a05", "%pm", addr);
394 test("057afcd6482d", "%pmR", addr);
400 struct sockaddr_in sa;
402 sa.sin_family = AF_INET;
403 sa.sin_port = cpu_to_be16(12345);
404 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
406 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
407 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
408 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
409 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
427 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
428 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
430 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
431 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
432 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
433 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
436 static struct dentry test_dentry[4] __initdata = {
437 { .d_parent = &test_dentry[0],
438 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
440 { .d_parent = &test_dentry[0],
441 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
442 .d_iname = "bravo" },
443 { .d_parent = &test_dentry[1],
444 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
446 { .d_parent = &test_dentry[2],
447 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
448 .d_iname = "romeo" },
454 test("foo", "%pd", &test_dentry[0]);
455 test("foo", "%pd2", &test_dentry[0]);
457 test("romeo", "%pd", &test_dentry[3]);
458 test("alfa/romeo", "%pd2", &test_dentry[3]);
459 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
460 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
461 test("/bravo/alfa", "%pd4", &test_dentry[2]);
463 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
464 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
468 struct_va_format(void)
473 struct_rtc_time(void)
476 const struct rtc_time tm = {
485 test("(%ptR?)", "%pt", &tm);
486 test("2018-11-26T05:35:43", "%ptR", &tm);
487 test("0118-10-26T05:35:43", "%ptRr", &tm);
488 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
489 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
490 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
491 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
502 const int nbits = 1 << 16;
503 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
507 bitmap_set(bits, 1, 20);
508 bitmap_set(bits, 60000, 15);
509 test("1-20,60000-60014", "%*pbl", nbits, bits);
516 DECLARE_BITMAP(bits, 20);
517 const int primes[] = {2,3,5,7,11,13,17,19};
520 bitmap_zero(bits, 20);
521 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
522 test("|", "%20pbl|%*pbl", bits, 20, bits);
524 for (i = 0; i < ARRAY_SIZE(primes); ++i)
525 set_bit(primes[i], bits);
526 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
527 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
529 bitmap_fill(bits, 20);
530 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
531 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
537 netdev_features(void)
549 test("", "%pGp", &flags);
551 /* Page flags should filter the zone id */
552 flags = 1UL << NR_PAGEFLAGS;
553 test("", "%pGp", &flags);
555 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
556 | 1UL << PG_active | 1UL << PG_swapbacked;
557 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
560 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
562 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
565 test("GFP_TRANSHUGE", "%pGg", &gfp);
567 gfp = GFP_ATOMIC|__GFP_DMA;
568 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
571 test("__GFP_ATOMIC", "%pGg", &gfp);
573 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
577 /* Any flags not translated by the table should remain numeric */
578 gfp = ~__GFP_BITS_MASK;
579 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
580 test(cmp_buffer, "%pGg", &gfp);
582 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
583 (unsigned long) gfp);
585 test(cmp_buffer, "%pGg", &gfp);
614 static void __init selftest(void)
616 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
619 test_buffer = alloced_buffer + PAD_SIZE;
626 kfree(alloced_buffer);
629 KSTM_MODULE_LOADERS(test_printf);
630 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
631 MODULE_LICENSE("GPL");