]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - lib/test_printf.c
kasan, arm64: allow using KUnit tests with HW_TAGS mode
[thirdparty/kernel/linux.git] / lib / test_printf.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
707cc728
RV
2/*
3 * Test cases for printf facility.
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
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>
4d42c447 13#include <linux/rtc.h>
707cc728
RV
14#include <linux/slab.h>
15#include <linux/string.h>
16
857cca4d 17#include <linux/bitmap.h>
251c7234 18#include <linux/dcache.h>
707cc728
RV
19#include <linux/socket.h>
20#include <linux/in.h>
21
edf14cdb
VB
22#include <linux/gfp.h>
23#include <linux/mm.h>
24
f1ce39df
SA
25#include <linux/property.h>
26
6b1a4d5b
TH
27#include "../tools/testing/selftests/kselftest_module.h"
28
707cc728 29#define BUF_SIZE 256
331e4deb 30#define PAD_SIZE 16
707cc728
RV
31#define FILL_CHAR '$'
32
4e89a787
TT
33KSTM_MODULE_GLOBALS();
34
707cc728 35static char *test_buffer __initdata;
331e4deb 36static char *alloced_buffer __initdata;
707cc728 37
5ead723a
TT
38extern bool no_hash_pointers;
39
707cc728
RV
40static int __printf(4, 0) __init
41do_test(int bufsize, const char *expect, int elen,
42 const char *fmt, va_list ap)
43{
44 va_list aq;
45 int ret, written;
46
47 total_tests++;
48
331e4deb 49 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
707cc728
RV
50 va_copy(aq, ap);
51 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
52 va_end(aq);
53
54 if (ret != elen) {
55 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
56 bufsize, fmt, ret, elen);
57 return 1;
58 }
59
331e4deb
RV
60 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
61 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
62 return 1;
63 }
64
707cc728 65 if (!bufsize) {
331e4deb 66 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
707cc728
RV
67 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
68 fmt);
69 return 1;
70 }
71 return 0;
72 }
73
74 written = min(bufsize-1, elen);
75 if (test_buffer[written]) {
76 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
77 bufsize, fmt);
78 return 1;
79 }
80
331e4deb
RV
81 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
82 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
83 bufsize, fmt);
84 return 1;
85 }
86
707cc728
RV
87 if (memcmp(test_buffer, expect, written)) {
88 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
89 bufsize, fmt, test_buffer, written, expect);
90 return 1;
91 }
92 return 0;
93}
94
95static void __printf(3, 4) __init
96__test(const char *expect, int elen, const char *fmt, ...)
97{
98 va_list ap;
99 int rand;
100 char *p;
101
fd0515d5
RV
102 if (elen >= BUF_SIZE) {
103 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
104 elen, fmt);
105 failed_tests++;
106 return;
107 }
707cc728
RV
108
109 va_start(ap, fmt);
110
111 /*
112 * Every fmt+args is subjected to four tests: Three where we
113 * tell vsnprintf varying buffer sizes (plenty, not quite
114 * enough and 0), and then we also test that kvasprintf would
115 * be able to print it as expected.
116 */
117 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
118 rand = 1 + prandom_u32_max(elen+1);
119 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
120 failed_tests += do_test(rand, expect, elen, fmt, ap);
121 failed_tests += do_test(0, expect, elen, fmt, ap);
122
123 p = kvasprintf(GFP_KERNEL, fmt, ap);
124 if (p) {
b79a7db3 125 total_tests++;
707cc728
RV
126 if (memcmp(p, expect, elen+1)) {
127 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
128 fmt, p, expect);
129 failed_tests++;
130 }
131 kfree(p);
132 }
133 va_end(ap);
134}
135
136#define test(expect, fmt, ...) \
137 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
138
139static void __init
140test_basic(void)
141{
142 /* Work around annoying "warning: zero-length gnu_printf format string". */
143 char nul = '\0';
144
145 test("", &nul);
146 test("100%", "100%%");
147 test("xxx%yyy", "xxx%cyyy", '%');
148 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
149}
150
151static void __init
152test_number(void)
153{
154 test("0x1234abcd ", "%#-12x", 0x1234abcd);
155 test(" 0x1234abcd", "%#12x", 0x1234abcd);
156 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
1ca8e8eb
RV
157 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
158 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
159 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
160 /*
161 * POSIX/C99: »The result of converting zero with an explicit
162 * precision of zero shall be no characters.« Hence the output
163 * from the below test should really be "00|0||| ". However,
164 * the kernel's printf also produces a single 0 in that
165 * case. This test case simply documents the current
166 * behaviour.
167 */
168 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
169#ifndef __CHAR_UNSIGNED__
170 {
171 /*
172 * Passing a 'char' to a %02x specifier doesn't do
173 * what was presumably the intention when char is
174 * signed and the value is negative. One must either &
175 * with 0xff or cast to u8.
176 */
177 char val = -16;
178 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
179 }
180#endif
707cc728
RV
181}
182
183static void __init
184test_string(void)
185{
186 test("", "%s%.0s", "", "123");
187 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
188 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
f176eb4c
RV
189 test("1234 ", "%-10.4s", "123456");
190 test(" 1234", "%10.4s", "123456");
707cc728 191 /*
f176eb4c
RV
192 * POSIX and C99 say that a negative precision (which is only
193 * possible to pass via a * argument) should be treated as if
194 * the precision wasn't present, and that if the precision is
195 * omitted (as in %.s), the precision should be taken to be
196 * 0. However, the kernel's printf behave exactly opposite,
197 * treating a negative precision as 0 and treating an omitted
198 * precision specifier as if no precision was given.
199 *
200 * These test cases document the current behaviour; should
201 * anyone ever feel the need to follow the standards more
202 * closely, this can be revisited.
707cc728 203 */
f176eb4c
RV
204 test(" ", "%4.*s", -5, "123456");
205 test("123456", "%.s", "123456");
707cc728
RV
206 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
207 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
208}
209
ad67b74d
TH
210#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
211
212#if BITS_PER_LONG == 64
213
214#define PTR_WIDTH 16
c604b407 215#define PTR ((void *)0xffff0123456789abUL)
ad67b74d 216#define PTR_STR "ffff0123456789ab"
ce041c43 217#define PTR_VAL_NO_CRNG "(____ptrval____)"
ad67b74d 218#define ZEROS "00000000" /* hex 32 zero bits */
7bd57fbc 219#define ONES "ffffffff" /* hex 32 one bits */
ad67b74d
TH
220
221static int __init
222plain_format(void)
223{
224 char buf[PLAIN_BUF_SIZE];
225 int nchars;
226
227 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
228
ce041c43
TE
229 if (nchars != PTR_WIDTH)
230 return -1;
231
232 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
233 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
234 PTR_VAL_NO_CRNG);
235 return 0;
236 }
237
238 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
ad67b74d
TH
239 return -1;
240
241 return 0;
242}
243
244#else
245
246#define PTR_WIDTH 8
247#define PTR ((void *)0x456789ab)
248#define PTR_STR "456789ab"
ce041c43 249#define PTR_VAL_NO_CRNG "(ptrval)"
3e5903eb 250#define ZEROS ""
7bd57fbc 251#define ONES ""
ad67b74d
TH
252
253static int __init
254plain_format(void)
255{
256 /* Format is implicitly tested for 32 bit machines by plain_hash() */
257 return 0;
258}
259
260#endif /* BITS_PER_LONG == 64 */
261
262static int __init
4d42c447 263plain_hash_to_buffer(const void *p, char *buf, size_t len)
ad67b74d 264{
ad67b74d
TH
265 int nchars;
266
4d42c447 267 nchars = snprintf(buf, len, "%p", p);
ad67b74d 268
ce041c43
TE
269 if (nchars != PTR_WIDTH)
270 return -1;
271
272 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
273 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
274 PTR_VAL_NO_CRNG);
275 return 0;
276 }
277
4d42c447
AS
278 return 0;
279}
280
4d42c447
AS
281static int __init
282plain_hash(void)
283{
284 char buf[PLAIN_BUF_SIZE];
285 int ret;
286
287 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
288 if (ret)
289 return ret;
290
ce041c43 291 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
ad67b74d
TH
292 return -1;
293
294 return 0;
295}
296
297/*
298 * We can't use test() to test %p because we don't know what output to expect
299 * after an address is hashed.
300 */
707cc728
RV
301static void __init
302plain(void)
303{
ad67b74d 304 int err;
707cc728 305
5ead723a
TT
306 if (no_hash_pointers) {
307 pr_warn("skipping plain 'p' tests");
308 skipped_tests += 2;
309 return;
310 }
311
ad67b74d
TH
312 err = plain_hash();
313 if (err) {
314 pr_warn("plain 'p' does not appear to be hashed\n");
315 failed_tests++;
316 return;
317 }
318
319 err = plain_format();
320 if (err) {
321 pr_warn("hashing plain 'p' has unexpected format\n");
322 failed_tests++;
323 }
707cc728
RV
324}
325
4d42c447
AS
326static void __init
327test_hashed(const char *fmt, const void *p)
328{
329 char buf[PLAIN_BUF_SIZE];
330 int ret;
331
332 /*
333 * No need to increase failed test counter since this is assumed
334 * to be called after plain().
335 */
336 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
337 if (ret)
338 return;
339
340 test(buf, fmt, p);
341}
342
7bd57fbc
ID
343/*
344 * NULL pointers aren't hashed.
345 */
3e5903eb
PM
346static void __init
347null_pointer(void)
348{
7bd57fbc 349 test(ZEROS "00000000", "%p", NULL);
3e5903eb
PM
350 test(ZEROS "00000000", "%px", NULL);
351 test("(null)", "%pE", NULL);
352}
353
7bd57fbc
ID
354/*
355 * Error pointers aren't hashed.
356 */
357static void __init
358error_pointer(void)
359{
360 test(ONES "fffffff5", "%p", ERR_PTR(-11));
361 test(ONES "fffffff5", "%px", ERR_PTR(-11));
362 test("(efault)", "%pE", ERR_PTR(-11));
363}
364
3e5903eb
PM
365#define PTR_INVALID ((void *)0x000000ab)
366
367static void __init
368invalid_pointer(void)
369{
370 test_hashed("%p", PTR_INVALID);
371 test(ZEROS "000000ab", "%px", PTR_INVALID);
372 test("(efault)", "%pE", PTR_INVALID);
373}
374
707cc728
RV
375static void __init
376symbol_ptr(void)
377{
378}
379
380static void __init
381kernel_ptr(void)
382{
ad67b74d 383 /* We can't test this without access to kptr_restrict. */
707cc728
RV
384}
385
386static void __init
387struct_resource(void)
388{
389}
390
391static void __init
392addr(void)
393{
394}
395
396static void __init
397escaped_str(void)
398{
399}
400
401static void __init
402hex_string(void)
403{
404 const char buf[3] = {0xc0, 0xff, 0xee};
405
406 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
407 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
408 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
409 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
410}
411
412static void __init
413mac(void)
414{
415 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
416
417 test("2d:48:d6:fc:7a:05", "%pM", addr);
418 test("05:7a:fc:d6:48:2d", "%pMR", addr);
419 test("2d-48-d6-fc-7a-05", "%pMF", addr);
420 test("2d48d6fc7a05", "%pm", addr);
421 test("057afcd6482d", "%pmR", addr);
422}
423
424static void __init
425ip4(void)
426{
427 struct sockaddr_in sa;
428
429 sa.sin_family = AF_INET;
430 sa.sin_port = cpu_to_be16(12345);
431 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
432
433 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
434 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
435 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
436 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
437}
438
439static void __init
440ip6(void)
441{
442}
443
444static void __init
445ip(void)
446{
447 ip4();
448 ip6();
449}
450
451static void __init
452uuid(void)
453{
454 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
455 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
456
457 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
458 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
459 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
460 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
461}
462
251c7234
RV
463static struct dentry test_dentry[4] __initdata = {
464 { .d_parent = &test_dentry[0],
465 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
466 .d_iname = "foo" },
467 { .d_parent = &test_dentry[0],
468 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
469 .d_iname = "bravo" },
470 { .d_parent = &test_dentry[1],
471 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
472 .d_iname = "alfa" },
473 { .d_parent = &test_dentry[2],
474 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
475 .d_iname = "romeo" },
476};
477
707cc728
RV
478static void __init
479dentry(void)
480{
251c7234
RV
481 test("foo", "%pd", &test_dentry[0]);
482 test("foo", "%pd2", &test_dentry[0]);
483
cf6b7921
JH
484 test("(null)", "%pd", NULL);
485 test("(efault)", "%pd", PTR_INVALID);
cf6b7921
JH
486 test("(null)", "%pD", NULL);
487 test("(efault)", "%pD", PTR_INVALID);
488
251c7234
RV
489 test("romeo", "%pd", &test_dentry[3]);
490 test("alfa/romeo", "%pd2", &test_dentry[3]);
491 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
492 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
493 test("/bravo/alfa", "%pd4", &test_dentry[2]);
494
495 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
496 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
707cc728
RV
497}
498
499static void __init
500struct_va_format(void)
501{
502}
503
4d42c447 504static void __init
7daac5b2 505time_and_date(void)
4d42c447
AS
506{
507 /* 1543210543 */
508 const struct rtc_time tm = {
509 .tm_sec = 43,
510 .tm_min = 35,
511 .tm_hour = 5,
512 .tm_mday = 26,
513 .tm_mon = 10,
514 .tm_year = 118,
515 };
7daac5b2
AS
516 /* 2019-01-04T15:32:23 */
517 time64_t t = 1546615943;
4d42c447 518
7daac5b2 519 test("(%pt?)", "%pt", &tm);
4d42c447
AS
520 test("2018-11-26T05:35:43", "%ptR", &tm);
521 test("0118-10-26T05:35:43", "%ptRr", &tm);
522 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
523 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
524 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
525 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
7daac5b2
AS
526
527 test("2019-01-04T15:32:23", "%ptT", &t);
528 test("0119-00-04T15:32:23", "%ptTr", &t);
529 test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
530 test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
4d42c447
AS
531}
532
707cc728
RV
533static void __init
534struct_clk(void)
535{
536}
537
857cca4d
RV
538static void __init
539large_bitmap(void)
540{
541 const int nbits = 1 << 16;
2821fd0c 542 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
857cca4d
RV
543 if (!bits)
544 return;
545
546 bitmap_set(bits, 1, 20);
547 bitmap_set(bits, 60000, 15);
548 test("1-20,60000-60014", "%*pbl", nbits, bits);
2821fd0c 549 bitmap_free(bits);
857cca4d
RV
550}
551
707cc728
RV
552static void __init
553bitmap(void)
554{
555 DECLARE_BITMAP(bits, 20);
556 const int primes[] = {2,3,5,7,11,13,17,19};
557 int i;
558
559 bitmap_zero(bits, 20);
560 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
561 test("|", "%20pbl|%*pbl", bits, 20, bits);
562
563 for (i = 0; i < ARRAY_SIZE(primes); ++i)
564 set_bit(primes[i], bits);
565 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
566 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
567
568 bitmap_fill(bits, 20);
569 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
570 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
857cca4d
RV
571
572 large_bitmap();
707cc728
RV
573}
574
575static void __init
576netdev_features(void)
577{
578}
579
edf14cdb
VB
580static void __init
581flags(void)
582{
583 unsigned long flags;
584 gfp_t gfp;
585 char *cmp_buffer;
586
587 flags = 0;
588 test("", "%pGp", &flags);
589
590 /* Page flags should filter the zone id */
591 flags = 1UL << NR_PAGEFLAGS;
592 test("", "%pGp", &flags);
593
594 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
595 | 1UL << PG_active | 1UL << PG_swapbacked;
596 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
597
598
599 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
600 | VM_DENYWRITE;
601 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
602
603 gfp = GFP_TRANSHUGE;
604 test("GFP_TRANSHUGE", "%pGg", &gfp);
605
606 gfp = GFP_ATOMIC|__GFP_DMA;
607 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
608
609 gfp = __GFP_ATOMIC;
610 test("__GFP_ATOMIC", "%pGg", &gfp);
611
612 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
613 if (!cmp_buffer)
614 return;
615
616 /* Any flags not translated by the table should remain numeric */
617 gfp = ~__GFP_BITS_MASK;
618 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
619 test(cmp_buffer, "%pGg", &gfp);
620
621 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
622 (unsigned long) gfp);
623 gfp |= __GFP_ATOMIC;
624 test(cmp_buffer, "%pGg", &gfp);
625
626 kfree(cmp_buffer);
627}
628
f1ce39df
SA
629static void __init fwnode_pointer(void)
630{
631 const struct software_node softnodes[] = {
632 { .name = "first", },
633 { .name = "second", .parent = &softnodes[0], },
634 { .name = "third", .parent = &softnodes[1], },
635 { NULL /* Guardian */ }
636 };
637 const char * const full_name = "first/second/third";
638 const char * const full_name_second = "first/second";
639 const char * const second_name = "second";
640 const char * const third_name = "third";
641 int rval;
642
643 rval = software_node_register_nodes(softnodes);
644 if (rval) {
645 pr_warn("cannot register softnodes; rval %d\n", rval);
646 return;
647 }
648
649 test(full_name_second, "%pfw", software_node_fwnode(&softnodes[1]));
650 test(full_name, "%pfw", software_node_fwnode(&softnodes[2]));
651 test(full_name, "%pfwf", software_node_fwnode(&softnodes[2]));
652 test(second_name, "%pfwP", software_node_fwnode(&softnodes[1]));
653 test(third_name, "%pfwP", software_node_fwnode(&softnodes[2]));
654
f0328be5 655 software_node_unregister_nodes(softnodes);
f1ce39df
SA
656}
657
57f5677e
RV
658static void __init
659errptr(void)
660{
661 test("-1234", "%pe", ERR_PTR(-1234));
662
663 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
664 BUILD_BUG_ON(IS_ERR(PTR));
665 test_hashed("%pe", PTR);
666
667#ifdef CONFIG_SYMBOLIC_ERRNAME
668 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
669 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
670 BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
671 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
672 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
673 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
674 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
675#endif
676}
677
707cc728
RV
678static void __init
679test_pointer(void)
680{
681 plain();
3e5903eb 682 null_pointer();
7bd57fbc 683 error_pointer();
3e5903eb 684 invalid_pointer();
707cc728
RV
685 symbol_ptr();
686 kernel_ptr();
687 struct_resource();
688 addr();
689 escaped_str();
690 hex_string();
691 mac();
692 ip();
693 uuid();
694 dentry();
695 struct_va_format();
7daac5b2 696 time_and_date();
707cc728
RV
697 struct_clk();
698 bitmap();
699 netdev_features();
edf14cdb 700 flags();
57f5677e 701 errptr();
f1ce39df 702 fwnode_pointer();
707cc728
RV
703}
704
6b1a4d5b 705static void __init selftest(void)
707cc728 706{
331e4deb
RV
707 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
708 if (!alloced_buffer)
6b1a4d5b 709 return;
331e4deb 710 test_buffer = alloced_buffer + PAD_SIZE;
707cc728
RV
711
712 test_basic();
713 test_number();
714 test_string();
715 test_pointer();
716
331e4deb 717 kfree(alloced_buffer);
707cc728
RV
718}
719
6b1a4d5b 720KSTM_MODULE_LOADERS(test_printf);
707cc728
RV
721MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
722MODULE_LICENSE("GPL");