Linux 4.14.199
[linux/fpc-iii.git] / lib / test_printf.c
blob563f10e6876aecf6e3932e0a2e955305f271b249
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 #define PTR1 ((void*)0x01234567)
28 #define PTR2 ((void*)(long)(int)0xfedcba98)
30 #if BITS_PER_LONG == 64
31 #define PTR1_ZEROES "000000000"
32 #define PTR1_SPACES " "
33 #define PTR1_STR "1234567"
34 #define PTR2_STR "fffffffffedcba98"
35 #define PTR_WIDTH 16
36 #else
37 #define PTR1_ZEROES "0"
38 #define PTR1_SPACES " "
39 #define PTR1_STR "1234567"
40 #define PTR2_STR "fedcba98"
41 #define PTR_WIDTH 8
42 #endif
43 #define PTR_WIDTH_STR stringify(PTR_WIDTH)
45 static unsigned total_tests __initdata;
46 static unsigned failed_tests __initdata;
47 static char *test_buffer __initdata;
48 static char *alloced_buffer __initdata;
50 static int __printf(4, 0) __init
51 do_test(int bufsize, const char *expect, int elen,
52 const char *fmt, va_list ap)
54 va_list aq;
55 int ret, written;
57 total_tests++;
59 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
60 va_copy(aq, ap);
61 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
62 va_end(aq);
64 if (ret != elen) {
65 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
66 bufsize, fmt, ret, elen);
67 return 1;
70 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
71 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
72 return 1;
75 if (!bufsize) {
76 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
77 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
78 fmt);
79 return 1;
81 return 0;
84 written = min(bufsize-1, elen);
85 if (test_buffer[written]) {
86 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
87 bufsize, fmt);
88 return 1;
91 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
92 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
93 bufsize, fmt);
94 return 1;
97 if (memcmp(test_buffer, expect, written)) {
98 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
99 bufsize, fmt, test_buffer, written, expect);
100 return 1;
102 return 0;
105 static void __printf(3, 4) __init
106 __test(const char *expect, int elen, const char *fmt, ...)
108 va_list ap;
109 int rand;
110 char *p;
112 if (elen >= BUF_SIZE) {
113 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
114 elen, fmt);
115 failed_tests++;
116 return;
119 va_start(ap, fmt);
122 * Every fmt+args is subjected to four tests: Three where we
123 * tell vsnprintf varying buffer sizes (plenty, not quite
124 * enough and 0), and then we also test that kvasprintf would
125 * be able to print it as expected.
127 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
128 rand = 1 + prandom_u32_max(elen+1);
129 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
130 failed_tests += do_test(rand, expect, elen, fmt, ap);
131 failed_tests += do_test(0, expect, elen, fmt, ap);
133 p = kvasprintf(GFP_KERNEL, fmt, ap);
134 if (p) {
135 total_tests++;
136 if (memcmp(p, expect, elen+1)) {
137 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
138 fmt, p, expect);
139 failed_tests++;
141 kfree(p);
143 va_end(ap);
146 #define test(expect, fmt, ...) \
147 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
149 static void __init
150 test_basic(void)
152 /* Work around annoying "warning: zero-length gnu_printf format string". */
153 char nul = '\0';
155 test("", &nul);
156 test("100%", "100%%");
157 test("xxx%yyy", "xxx%cyyy", '%');
158 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
161 static void __init
162 test_number(void)
164 test("0x1234abcd ", "%#-12x", 0x1234abcd);
165 test(" 0x1234abcd", "%#12x", 0x1234abcd);
166 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
167 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
168 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
169 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
171 * POSIX/C99: »The result of converting zero with an explicit
172 * precision of zero shall be no characters.« Hence the output
173 * from the below test should really be "00|0||| ". However,
174 * the kernel's printf also produces a single 0 in that
175 * case. This test case simply documents the current
176 * behaviour.
178 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
179 #ifndef __CHAR_UNSIGNED__
182 * Passing a 'char' to a %02x specifier doesn't do
183 * what was presumably the intention when char is
184 * signed and the value is negative. One must either &
185 * with 0xff or cast to u8.
187 char val = -16;
188 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
190 #endif
193 static void __init
194 test_string(void)
196 test("", "%s%.0s", "", "123");
197 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
198 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
199 test("1234 ", "%-10.4s", "123456");
200 test(" 1234", "%10.4s", "123456");
202 * POSIX and C99 say that a negative precision (which is only
203 * possible to pass via a * argument) should be treated as if
204 * the precision wasn't present, and that if the precision is
205 * omitted (as in %.s), the precision should be taken to be
206 * 0. However, the kernel's printf behave exactly opposite,
207 * treating a negative precision as 0 and treating an omitted
208 * precision specifier as if no precision was given.
210 * These test cases document the current behaviour; should
211 * anyone ever feel the need to follow the standards more
212 * closely, this can be revisited.
214 test(" ", "%4.*s", -5, "123456");
215 test("123456", "%.s", "123456");
216 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
217 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
220 static void __init
221 plain(void)
223 test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
225 * The field width is overloaded for some %p extensions to
226 * pass another piece of information. For plain pointers, the
227 * behaviour is slightly odd: One cannot pass either the 0
228 * flag nor a precision to %p without gcc complaining, and if
229 * one explicitly gives a field width, the number is no longer
230 * zero-padded.
232 test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
233 "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
234 test("|" PTR2_STR " | " PTR2_STR "|",
235 "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
238 * Unrecognized %p extensions are treated as plain %p, but the
239 * alphanumeric suffix is ignored (that is, does not occur in
240 * the output.)
242 test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
243 test("|"PTR2_STR"|", "|%p0y|", PTR2);
246 static void __init
247 symbol_ptr(void)
251 static void __init
252 kernel_ptr(void)
256 static void __init
257 struct_resource(void)
261 static void __init
262 addr(void)
266 static void __init
267 escaped_str(void)
271 static void __init
272 hex_string(void)
274 const char buf[3] = {0xc0, 0xff, 0xee};
276 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
277 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
278 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
279 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
282 static void __init
283 mac(void)
285 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
287 test("2d:48:d6:fc:7a:05", "%pM", addr);
288 test("05:7a:fc:d6:48:2d", "%pMR", addr);
289 test("2d-48-d6-fc-7a-05", "%pMF", addr);
290 test("2d48d6fc7a05", "%pm", addr);
291 test("057afcd6482d", "%pmR", addr);
294 static void __init
295 ip4(void)
297 struct sockaddr_in sa;
299 sa.sin_family = AF_INET;
300 sa.sin_port = cpu_to_be16(12345);
301 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
303 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
304 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
305 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
306 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
309 static void __init
310 ip6(void)
314 static void __init
315 ip(void)
317 ip4();
318 ip6();
321 static void __init
322 uuid(void)
324 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
325 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
327 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
328 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
329 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
330 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
333 static struct dentry test_dentry[4] __initdata = {
334 { .d_parent = &test_dentry[0],
335 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
336 .d_iname = "foo" },
337 { .d_parent = &test_dentry[0],
338 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
339 .d_iname = "bravo" },
340 { .d_parent = &test_dentry[1],
341 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
342 .d_iname = "alfa" },
343 { .d_parent = &test_dentry[2],
344 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
345 .d_iname = "romeo" },
348 static void __init
349 dentry(void)
351 test("foo", "%pd", &test_dentry[0]);
352 test("foo", "%pd2", &test_dentry[0]);
354 test("romeo", "%pd", &test_dentry[3]);
355 test("alfa/romeo", "%pd2", &test_dentry[3]);
356 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
357 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
358 test("/bravo/alfa", "%pd4", &test_dentry[2]);
360 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
361 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
364 static void __init
365 struct_va_format(void)
369 static void __init
370 struct_clk(void)
374 static void __init
375 large_bitmap(void)
377 const int nbits = 1 << 16;
378 unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
379 if (!bits)
380 return;
382 bitmap_set(bits, 1, 20);
383 bitmap_set(bits, 60000, 15);
384 test("1-20,60000-60014", "%*pbl", nbits, bits);
385 kfree(bits);
388 static void __init
389 bitmap(void)
391 DECLARE_BITMAP(bits, 20);
392 const int primes[] = {2,3,5,7,11,13,17,19};
393 int i;
395 bitmap_zero(bits, 20);
396 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
397 test("|", "%20pbl|%*pbl", bits, 20, bits);
399 for (i = 0; i < ARRAY_SIZE(primes); ++i)
400 set_bit(primes[i], bits);
401 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
402 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
404 bitmap_fill(bits, 20);
405 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
406 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
408 large_bitmap();
411 static void __init
412 netdev_features(void)
416 static void __init
417 flags(void)
419 unsigned long flags;
420 gfp_t gfp;
421 char *cmp_buffer;
423 flags = 0;
424 test("", "%pGp", &flags);
426 /* Page flags should filter the zone id */
427 flags = 1UL << NR_PAGEFLAGS;
428 test("", "%pGp", &flags);
430 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
431 | 1UL << PG_active | 1UL << PG_swapbacked;
432 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
435 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
436 | VM_DENYWRITE;
437 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
439 gfp = GFP_TRANSHUGE;
440 test("GFP_TRANSHUGE", "%pGg", &gfp);
442 gfp = GFP_ATOMIC|__GFP_DMA;
443 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
445 gfp = __GFP_ATOMIC;
446 test("__GFP_ATOMIC", "%pGg", &gfp);
448 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
449 if (!cmp_buffer)
450 return;
452 /* Any flags not translated by the table should remain numeric */
453 gfp = ~__GFP_BITS_MASK;
454 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
455 test(cmp_buffer, "%pGg", &gfp);
457 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
458 (unsigned long) gfp);
459 gfp |= __GFP_ATOMIC;
460 test(cmp_buffer, "%pGg", &gfp);
462 kfree(cmp_buffer);
465 static void __init
466 test_pointer(void)
468 plain();
469 symbol_ptr();
470 kernel_ptr();
471 struct_resource();
472 addr();
473 escaped_str();
474 hex_string();
475 mac();
476 ip();
477 uuid();
478 dentry();
479 struct_va_format();
480 struct_clk();
481 bitmap();
482 netdev_features();
483 flags();
486 static int __init
487 test_printf_init(void)
489 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
490 if (!alloced_buffer)
491 return -ENOMEM;
492 test_buffer = alloced_buffer + PAD_SIZE;
494 test_basic();
495 test_number();
496 test_string();
497 test_pointer();
499 kfree(alloced_buffer);
501 if (failed_tests == 0)
502 pr_info("all %u tests passed\n", total_tests);
503 else
504 pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
506 return failed_tests ? -EINVAL : 0;
509 module_init(test_printf_init);
511 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
512 MODULE_LICENSE("GPL");