btrfs: migrate the block group ref counting stuff
[linux/fpc-iii.git] / lib / test_printf.c
blob944eb50f38625313bc58bc3f00f83cc502835c8c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Test cases for printf facility.
4 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/printk.h>
12 #include <linux/random.h>
13 #include <linux/rtc.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
17 #include <linux/bitmap.h>
18 #include <linux/dcache.h>
19 #include <linux/socket.h>
20 #include <linux/in.h>
22 #include <linux/gfp.h>
23 #include <linux/mm.h>
25 #include "../tools/testing/selftests/kselftest_module.h"
27 #define BUF_SIZE 256
28 #define PAD_SIZE 16
29 #define FILL_CHAR '$'
31 static unsigned total_tests __initdata;
32 static unsigned failed_tests __initdata;
33 static char *test_buffer __initdata;
34 static char *alloced_buffer __initdata;
36 static int __printf(4, 0) __init
37 do_test(int bufsize, const char *expect, int elen,
38 const char *fmt, va_list ap)
40 va_list aq;
41 int ret, written;
43 total_tests++;
45 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
46 va_copy(aq, ap);
47 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
48 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;
56 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
57 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
58 return 1;
61 if (!bufsize) {
62 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
63 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
64 fmt);
65 return 1;
67 return 0;
70 written = min(bufsize-1, elen);
71 if (test_buffer[written]) {
72 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
73 bufsize, fmt);
74 return 1;
77 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
78 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
79 bufsize, fmt);
80 return 1;
83 if (memcmp(test_buffer, expect, written)) {
84 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
85 bufsize, fmt, test_buffer, written, expect);
86 return 1;
88 return 0;
91 static void __printf(3, 4) __init
92 __test(const char *expect, int elen, const char *fmt, ...)
94 va_list ap;
95 int rand;
96 char *p;
98 if (elen >= BUF_SIZE) {
99 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
100 elen, fmt);
101 failed_tests++;
102 return;
105 va_start(ap, fmt);
108 * Every fmt+args is subjected to four tests: Three where we
109 * tell vsnprintf varying buffer sizes (plenty, not quite
110 * enough and 0), and then we also test that kvasprintf would
111 * be able to print it as expected.
113 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
114 rand = 1 + prandom_u32_max(elen+1);
115 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
116 failed_tests += do_test(rand, expect, elen, fmt, ap);
117 failed_tests += do_test(0, expect, elen, fmt, ap);
119 p = kvasprintf(GFP_KERNEL, fmt, ap);
120 if (p) {
121 total_tests++;
122 if (memcmp(p, expect, elen+1)) {
123 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
124 fmt, p, expect);
125 failed_tests++;
127 kfree(p);
129 va_end(ap);
132 #define test(expect, fmt, ...) \
133 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
135 static void __init
136 test_basic(void)
138 /* Work around annoying "warning: zero-length gnu_printf format string". */
139 char nul = '\0';
141 test("", &nul);
142 test("100%", "100%%");
143 test("xxx%yyy", "xxx%cyyy", '%');
144 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
147 static void __init
148 test_number(void)
150 test("0x1234abcd ", "%#-12x", 0x1234abcd);
151 test(" 0x1234abcd", "%#12x", 0x1234abcd);
152 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
153 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
154 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
155 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
157 * POSIX/C99: »The result of converting zero with an explicit
158 * precision of zero shall be no characters.« Hence the output
159 * from the below test should really be "00|0||| ". However,
160 * the kernel's printf also produces a single 0 in that
161 * case. This test case simply documents the current
162 * behaviour.
164 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
165 #ifndef __CHAR_UNSIGNED__
168 * Passing a 'char' to a %02x specifier doesn't do
169 * what was presumably the intention when char is
170 * signed and the value is negative. One must either &
171 * with 0xff or cast to u8.
173 char val = -16;
174 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
176 #endif
179 static void __init
180 test_string(void)
182 test("", "%s%.0s", "", "123");
183 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
184 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
185 test("1234 ", "%-10.4s", "123456");
186 test(" 1234", "%10.4s", "123456");
188 * POSIX and C99 say that a negative precision (which is only
189 * possible to pass via a * argument) should be treated as if
190 * the precision wasn't present, and that if the precision is
191 * omitted (as in %.s), the precision should be taken to be
192 * 0. However, the kernel's printf behave exactly opposite,
193 * treating a negative precision as 0 and treating an omitted
194 * precision specifier as if no precision was given.
196 * These test cases document the current behaviour; should
197 * anyone ever feel the need to follow the standards more
198 * closely, this can be revisited.
200 test(" ", "%4.*s", -5, "123456");
201 test("123456", "%.s", "123456");
202 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
203 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
206 #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
208 #if BITS_PER_LONG == 64
210 #define PTR_WIDTH 16
211 #define PTR ((void *)0xffff0123456789abUL)
212 #define PTR_STR "ffff0123456789ab"
213 #define PTR_VAL_NO_CRNG "(____ptrval____)"
214 #define ZEROS "00000000" /* hex 32 zero bits */
216 static int __init
217 plain_format(void)
219 char buf[PLAIN_BUF_SIZE];
220 int nchars;
222 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
224 if (nchars != PTR_WIDTH)
225 return -1;
227 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
228 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
229 PTR_VAL_NO_CRNG);
230 return 0;
233 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
234 return -1;
236 return 0;
239 #else
241 #define PTR_WIDTH 8
242 #define PTR ((void *)0x456789ab)
243 #define PTR_STR "456789ab"
244 #define PTR_VAL_NO_CRNG "(ptrval)"
245 #define ZEROS ""
247 static int __init
248 plain_format(void)
250 /* Format is implicitly tested for 32 bit machines by plain_hash() */
251 return 0;
254 #endif /* BITS_PER_LONG == 64 */
256 static int __init
257 plain_hash_to_buffer(const void *p, char *buf, size_t len)
259 int nchars;
261 nchars = snprintf(buf, len, "%p", p);
263 if (nchars != PTR_WIDTH)
264 return -1;
266 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
267 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
268 PTR_VAL_NO_CRNG);
269 return 0;
272 return 0;
275 static int __init
276 plain_hash(void)
278 char buf[PLAIN_BUF_SIZE];
279 int ret;
281 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
282 if (ret)
283 return ret;
285 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
286 return -1;
288 return 0;
292 * We can't use test() to test %p because we don't know what output to expect
293 * after an address is hashed.
295 static void __init
296 plain(void)
298 int err;
300 err = plain_hash();
301 if (err) {
302 pr_warn("plain 'p' does not appear to be hashed\n");
303 failed_tests++;
304 return;
307 err = plain_format();
308 if (err) {
309 pr_warn("hashing plain 'p' has unexpected format\n");
310 failed_tests++;
314 static void __init
315 test_hashed(const char *fmt, const void *p)
317 char buf[PLAIN_BUF_SIZE];
318 int ret;
321 * No need to increase failed test counter since this is assumed
322 * to be called after plain().
324 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
325 if (ret)
326 return;
328 test(buf, fmt, p);
331 static void __init
332 null_pointer(void)
334 test_hashed("%p", NULL);
335 test(ZEROS "00000000", "%px", NULL);
336 test("(null)", "%pE", NULL);
339 #define PTR_INVALID ((void *)0x000000ab)
341 static void __init
342 invalid_pointer(void)
344 test_hashed("%p", PTR_INVALID);
345 test(ZEROS "000000ab", "%px", PTR_INVALID);
346 test("(efault)", "%pE", PTR_INVALID);
349 static void __init
350 symbol_ptr(void)
354 static void __init
355 kernel_ptr(void)
357 /* We can't test this without access to kptr_restrict. */
360 static void __init
361 struct_resource(void)
365 static void __init
366 addr(void)
370 static void __init
371 escaped_str(void)
375 static void __init
376 hex_string(void)
378 const char buf[3] = {0xc0, 0xff, 0xee};
380 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
381 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
382 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
383 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
386 static void __init
387 mac(void)
389 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
391 test("2d:48:d6:fc:7a:05", "%pM", addr);
392 test("05:7a:fc:d6:48:2d", "%pMR", addr);
393 test("2d-48-d6-fc-7a-05", "%pMF", addr);
394 test("2d48d6fc7a05", "%pm", addr);
395 test("057afcd6482d", "%pmR", addr);
398 static void __init
399 ip4(void)
401 struct sockaddr_in sa;
403 sa.sin_family = AF_INET;
404 sa.sin_port = cpu_to_be16(12345);
405 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
407 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
408 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
409 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
410 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
413 static void __init
414 ip6(void)
418 static void __init
419 ip(void)
421 ip4();
422 ip6();
425 static void __init
426 uuid(void)
428 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
429 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
431 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
432 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
433 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
434 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
437 static struct dentry test_dentry[4] __initdata = {
438 { .d_parent = &test_dentry[0],
439 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
440 .d_iname = "foo" },
441 { .d_parent = &test_dentry[0],
442 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
443 .d_iname = "bravo" },
444 { .d_parent = &test_dentry[1],
445 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
446 .d_iname = "alfa" },
447 { .d_parent = &test_dentry[2],
448 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
449 .d_iname = "romeo" },
452 static void __init
453 dentry(void)
455 test("foo", "%pd", &test_dentry[0]);
456 test("foo", "%pd2", &test_dentry[0]);
458 test("romeo", "%pd", &test_dentry[3]);
459 test("alfa/romeo", "%pd2", &test_dentry[3]);
460 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
461 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
462 test("/bravo/alfa", "%pd4", &test_dentry[2]);
464 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
465 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
468 static void __init
469 struct_va_format(void)
473 static void __init
474 struct_rtc_time(void)
476 /* 1543210543 */
477 const struct rtc_time tm = {
478 .tm_sec = 43,
479 .tm_min = 35,
480 .tm_hour = 5,
481 .tm_mday = 26,
482 .tm_mon = 10,
483 .tm_year = 118,
486 test("(%ptR?)", "%pt", &tm);
487 test("2018-11-26T05:35:43", "%ptR", &tm);
488 test("0118-10-26T05:35:43", "%ptRr", &tm);
489 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
490 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
491 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
492 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
495 static void __init
496 struct_clk(void)
500 static void __init
501 large_bitmap(void)
503 const int nbits = 1 << 16;
504 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
505 if (!bits)
506 return;
508 bitmap_set(bits, 1, 20);
509 bitmap_set(bits, 60000, 15);
510 test("1-20,60000-60014", "%*pbl", nbits, bits);
511 bitmap_free(bits);
514 static void __init
515 bitmap(void)
517 DECLARE_BITMAP(bits, 20);
518 const int primes[] = {2,3,5,7,11,13,17,19};
519 int i;
521 bitmap_zero(bits, 20);
522 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
523 test("|", "%20pbl|%*pbl", bits, 20, bits);
525 for (i = 0; i < ARRAY_SIZE(primes); ++i)
526 set_bit(primes[i], bits);
527 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
528 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
530 bitmap_fill(bits, 20);
531 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
532 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
534 large_bitmap();
537 static void __init
538 netdev_features(void)
542 static void __init
543 flags(void)
545 unsigned long flags;
546 gfp_t gfp;
547 char *cmp_buffer;
549 flags = 0;
550 test("", "%pGp", &flags);
552 /* Page flags should filter the zone id */
553 flags = 1UL << NR_PAGEFLAGS;
554 test("", "%pGp", &flags);
556 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
557 | 1UL << PG_active | 1UL << PG_swapbacked;
558 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
561 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
562 | VM_DENYWRITE;
563 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
565 gfp = GFP_TRANSHUGE;
566 test("GFP_TRANSHUGE", "%pGg", &gfp);
568 gfp = GFP_ATOMIC|__GFP_DMA;
569 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
571 gfp = __GFP_ATOMIC;
572 test("__GFP_ATOMIC", "%pGg", &gfp);
574 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
575 if (!cmp_buffer)
576 return;
578 /* Any flags not translated by the table should remain numeric */
579 gfp = ~__GFP_BITS_MASK;
580 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
581 test(cmp_buffer, "%pGg", &gfp);
583 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
584 (unsigned long) gfp);
585 gfp |= __GFP_ATOMIC;
586 test(cmp_buffer, "%pGg", &gfp);
588 kfree(cmp_buffer);
591 static void __init
592 test_pointer(void)
594 plain();
595 null_pointer();
596 invalid_pointer();
597 symbol_ptr();
598 kernel_ptr();
599 struct_resource();
600 addr();
601 escaped_str();
602 hex_string();
603 mac();
604 ip();
605 uuid();
606 dentry();
607 struct_va_format();
608 struct_rtc_time();
609 struct_clk();
610 bitmap();
611 netdev_features();
612 flags();
615 static void __init selftest(void)
617 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
618 if (!alloced_buffer)
619 return;
620 test_buffer = alloced_buffer + PAD_SIZE;
622 test_basic();
623 test_number();
624 test_string();
625 test_pointer();
627 kfree(alloced_buffer);
630 KSTM_MODULE_LOADERS(test_printf);
631 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
632 MODULE_LICENSE("GPL");