ovl: copy up times
[linux/fpc-iii.git] / lib / test_printf.c
blobb2aa8f5148449de1557e3ee48feebb8f1cab3083
1 /*
2 * Test cases for printf facility.
3 */
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/slab.h>
13 #include <linux/string.h>
15 #include <linux/bitmap.h>
16 #include <linux/dcache.h>
17 #include <linux/socket.h>
18 #include <linux/in.h>
20 #include <linux/gfp.h>
21 #include <linux/mm.h>
23 #define BUF_SIZE 256
24 #define PAD_SIZE 16
25 #define FILL_CHAR '$'
27 static unsigned total_tests __initdata;
28 static unsigned failed_tests __initdata;
29 static char *test_buffer __initdata;
30 static char *alloced_buffer __initdata;
32 static int __printf(4, 0) __init
33 do_test(int bufsize, const char *expect, int elen,
34 const char *fmt, va_list ap)
36 va_list aq;
37 int ret, written;
39 total_tests++;
41 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
42 va_copy(aq, ap);
43 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
44 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;
52 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
53 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
54 return 1;
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;
63 return 0;
66 written = min(bufsize-1, elen);
67 if (test_buffer[written]) {
68 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
69 bufsize, fmt);
70 return 1;
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;
79 if (memcmp(test_buffer, expect, written)) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
81 bufsize, fmt, test_buffer, written, expect);
82 return 1;
84 return 0;
87 static void __printf(3, 4) __init
88 __test(const char *expect, int elen, const char *fmt, ...)
90 va_list ap;
91 int rand;
92 char *p;
94 if (elen >= BUF_SIZE) {
95 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
96 elen, fmt);
97 failed_tests++;
98 return;
101 va_start(ap, fmt);
104 * Every fmt+args is subjected to four tests: Three where we
105 * tell vsnprintf varying buffer sizes (plenty, not quite
106 * enough and 0), and then we also test that kvasprintf would
107 * be able to print it as expected.
109 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
110 rand = 1 + prandom_u32_max(elen+1);
111 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
112 failed_tests += do_test(rand, expect, elen, fmt, ap);
113 failed_tests += do_test(0, expect, elen, fmt, ap);
115 p = kvasprintf(GFP_KERNEL, fmt, ap);
116 if (p) {
117 total_tests++;
118 if (memcmp(p, expect, elen+1)) {
119 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
120 fmt, p, expect);
121 failed_tests++;
123 kfree(p);
125 va_end(ap);
128 #define test(expect, fmt, ...) \
129 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
131 static void __init
132 test_basic(void)
134 /* Work around annoying "warning: zero-length gnu_printf format string". */
135 char nul = '\0';
137 test("", &nul);
138 test("100%", "100%%");
139 test("xxx%yyy", "xxx%cyyy", '%');
140 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
143 static void __init
144 test_number(void)
146 test("0x1234abcd ", "%#-12x", 0x1234abcd);
147 test(" 0x1234abcd", "%#12x", 0x1234abcd);
148 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
149 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
150 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
151 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
153 * POSIX/C99: »The result of converting zero with an explicit
154 * precision of zero shall be no characters.« Hence the output
155 * from the below test should really be "00|0||| ". However,
156 * the kernel's printf also produces a single 0 in that
157 * case. This test case simply documents the current
158 * behaviour.
160 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
161 #ifndef __CHAR_UNSIGNED__
164 * Passing a 'char' to a %02x specifier doesn't do
165 * what was presumably the intention when char is
166 * signed and the value is negative. One must either &
167 * with 0xff or cast to u8.
169 char val = -16;
170 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
172 #endif
175 static void __init
176 test_string(void)
178 test("", "%s%.0s", "", "123");
179 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
180 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
181 test("1234 ", "%-10.4s", "123456");
182 test(" 1234", "%10.4s", "123456");
184 * POSIX and C99 say that a negative precision (which is only
185 * possible to pass via a * argument) should be treated as if
186 * the precision wasn't present, and that if the precision is
187 * omitted (as in %.s), the precision should be taken to be
188 * 0. However, the kernel's printf behave exactly opposite,
189 * treating a negative precision as 0 and treating an omitted
190 * precision specifier as if no precision was given.
192 * These test cases document the current behaviour; should
193 * anyone ever feel the need to follow the standards more
194 * closely, this can be revisited.
196 test(" ", "%4.*s", -5, "123456");
197 test("123456", "%.s", "123456");
198 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
199 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
202 #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
204 #if BITS_PER_LONG == 64
206 #define PTR_WIDTH 16
207 #define PTR ((void *)0xffff0123456789abUL)
208 #define PTR_STR "ffff0123456789ab"
209 #define ZEROS "00000000" /* hex 32 zero bits */
211 static int __init
212 plain_format(void)
214 char buf[PLAIN_BUF_SIZE];
215 int nchars;
217 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
219 if (nchars != PTR_WIDTH || strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
220 return -1;
222 return 0;
225 #else
227 #define PTR_WIDTH 8
228 #define PTR ((void *)0x456789ab)
229 #define PTR_STR "456789ab"
231 static int __init
232 plain_format(void)
234 /* Format is implicitly tested for 32 bit machines by plain_hash() */
235 return 0;
238 #endif /* BITS_PER_LONG == 64 */
240 static int __init
241 plain_hash(void)
243 char buf[PLAIN_BUF_SIZE];
244 int nchars;
246 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
248 if (nchars != PTR_WIDTH || strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
249 return -1;
251 return 0;
255 * We can't use test() to test %p because we don't know what output to expect
256 * after an address is hashed.
258 static void __init
259 plain(void)
261 int err;
264 * Make sure crng is ready. Otherwise we get "(ptrval)" instead
265 * of a hashed address when printing '%p' in plain_hash() and
266 * plain_format().
268 wait_for_random_bytes();
270 err = plain_hash();
271 if (err) {
272 pr_warn("plain 'p' does not appear to be hashed\n");
273 failed_tests++;
274 return;
277 err = plain_format();
278 if (err) {
279 pr_warn("hashing plain 'p' has unexpected format\n");
280 failed_tests++;
284 static void __init
285 symbol_ptr(void)
289 static void __init
290 kernel_ptr(void)
292 /* We can't test this without access to kptr_restrict. */
295 static void __init
296 struct_resource(void)
300 static void __init
301 addr(void)
305 static void __init
306 escaped_str(void)
310 static void __init
311 hex_string(void)
313 const char buf[3] = {0xc0, 0xff, 0xee};
315 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
316 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
317 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
318 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
321 static void __init
322 mac(void)
324 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
326 test("2d:48:d6:fc:7a:05", "%pM", addr);
327 test("05:7a:fc:d6:48:2d", "%pMR", addr);
328 test("2d-48-d6-fc-7a-05", "%pMF", addr);
329 test("2d48d6fc7a05", "%pm", addr);
330 test("057afcd6482d", "%pmR", addr);
333 static void __init
334 ip4(void)
336 struct sockaddr_in sa;
338 sa.sin_family = AF_INET;
339 sa.sin_port = cpu_to_be16(12345);
340 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
342 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
343 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
344 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
345 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
348 static void __init
349 ip6(void)
353 static void __init
354 ip(void)
356 ip4();
357 ip6();
360 static void __init
361 uuid(void)
363 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
364 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
366 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
367 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
368 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
369 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
372 static struct dentry test_dentry[4] __initdata = {
373 { .d_parent = &test_dentry[0],
374 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
375 .d_iname = "foo" },
376 { .d_parent = &test_dentry[0],
377 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
378 .d_iname = "bravo" },
379 { .d_parent = &test_dentry[1],
380 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
381 .d_iname = "alfa" },
382 { .d_parent = &test_dentry[2],
383 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
384 .d_iname = "romeo" },
387 static void __init
388 dentry(void)
390 test("foo", "%pd", &test_dentry[0]);
391 test("foo", "%pd2", &test_dentry[0]);
393 test("romeo", "%pd", &test_dentry[3]);
394 test("alfa/romeo", "%pd2", &test_dentry[3]);
395 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
396 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
397 test("/bravo/alfa", "%pd4", &test_dentry[2]);
399 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
400 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
403 static void __init
404 struct_va_format(void)
408 static void __init
409 struct_clk(void)
413 static void __init
414 large_bitmap(void)
416 const int nbits = 1 << 16;
417 unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
418 if (!bits)
419 return;
421 bitmap_set(bits, 1, 20);
422 bitmap_set(bits, 60000, 15);
423 test("1-20,60000-60014", "%*pbl", nbits, bits);
424 kfree(bits);
427 static void __init
428 bitmap(void)
430 DECLARE_BITMAP(bits, 20);
431 const int primes[] = {2,3,5,7,11,13,17,19};
432 int i;
434 bitmap_zero(bits, 20);
435 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
436 test("|", "%20pbl|%*pbl", bits, 20, bits);
438 for (i = 0; i < ARRAY_SIZE(primes); ++i)
439 set_bit(primes[i], bits);
440 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
441 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
443 bitmap_fill(bits, 20);
444 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
445 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
447 large_bitmap();
450 static void __init
451 netdev_features(void)
455 static void __init
456 flags(void)
458 unsigned long flags;
459 gfp_t gfp;
460 char *cmp_buffer;
462 flags = 0;
463 test("", "%pGp", &flags);
465 /* Page flags should filter the zone id */
466 flags = 1UL << NR_PAGEFLAGS;
467 test("", "%pGp", &flags);
469 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
470 | 1UL << PG_active | 1UL << PG_swapbacked;
471 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
474 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
475 | VM_DENYWRITE;
476 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
478 gfp = GFP_TRANSHUGE;
479 test("GFP_TRANSHUGE", "%pGg", &gfp);
481 gfp = GFP_ATOMIC|__GFP_DMA;
482 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
484 gfp = __GFP_ATOMIC;
485 test("__GFP_ATOMIC", "%pGg", &gfp);
487 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
488 if (!cmp_buffer)
489 return;
491 /* Any flags not translated by the table should remain numeric */
492 gfp = ~__GFP_BITS_MASK;
493 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
494 test(cmp_buffer, "%pGg", &gfp);
496 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
497 (unsigned long) gfp);
498 gfp |= __GFP_ATOMIC;
499 test(cmp_buffer, "%pGg", &gfp);
501 kfree(cmp_buffer);
504 static void __init
505 test_pointer(void)
507 plain();
508 symbol_ptr();
509 kernel_ptr();
510 struct_resource();
511 addr();
512 escaped_str();
513 hex_string();
514 mac();
515 ip();
516 uuid();
517 dentry();
518 struct_va_format();
519 struct_clk();
520 bitmap();
521 netdev_features();
522 flags();
525 static int __init
526 test_printf_init(void)
528 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
529 if (!alloced_buffer)
530 return -ENOMEM;
531 test_buffer = alloced_buffer + PAD_SIZE;
533 test_basic();
534 test_number();
535 test_string();
536 test_pointer();
538 kfree(alloced_buffer);
540 if (failed_tests == 0)
541 pr_info("all %u tests passed\n", total_tests);
542 else
543 pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
545 return failed_tests ? -EINVAL : 0;
548 module_init(test_printf_init);
550 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
551 MODULE_LICENSE("GPL");