blob: f4fcc1c43739b31c9d3c39c1b1eb8f2e1b444c5d [file] [log] [blame]
Rasmus Villemoes707cc722015-11-06 16:30:29 -08001/*
2 * Test cases for printf facility.
3 */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
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>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020012#include <linux/rtc.h>
Rasmus Villemoes707cc722015-11-06 16:30:29 -080013#include <linux/slab.h>
14#include <linux/string.h>
15
Rasmus Villemoes857cca42016-01-15 16:59:06 -080016#include <linux/bitmap.h>
Rasmus Villemoes251c7232016-01-15 16:59:09 -080017#include <linux/dcache.h>
Rasmus Villemoes707cc722015-11-06 16:30:29 -080018#include <linux/socket.h>
19#include <linux/in.h>
20
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070021#include <linux/gfp.h>
22#include <linux/mm.h>
23
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +110024#include "../tools/testing/selftests/kselftest_module.h"
25
Rasmus Villemoes707cc722015-11-06 16:30:29 -080026#define BUF_SIZE 256
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080027#define PAD_SIZE 16
Rasmus Villemoes707cc722015-11-06 16:30:29 -080028#define FILL_CHAR '$'
29
Rasmus Villemoes707cc722015-11-06 16:30:29 -080030static unsigned total_tests __initdata;
31static unsigned failed_tests __initdata;
32static char *test_buffer __initdata;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080033static char *alloced_buffer __initdata;
Rasmus Villemoes707cc722015-11-06 16:30:29 -080034
35static int __printf(4, 0) __init
36do_test(int bufsize, const char *expect, int elen,
37 const char *fmt, va_list ap)
38{
39 va_list aq;
40 int ret, written;
41
42 total_tests++;
43
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080044 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
Rasmus Villemoes707cc722015-11-06 16:30:29 -080045 va_copy(aq, ap);
46 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
47 va_end(aq);
48
49 if (ret != elen) {
50 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
51 bufsize, fmt, ret, elen);
52 return 1;
53 }
54
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080055 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
56 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
57 return 1;
58 }
59
Rasmus Villemoes707cc722015-11-06 16:30:29 -080060 if (!bufsize) {
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080061 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
Rasmus Villemoes707cc722015-11-06 16:30:29 -080062 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
63 fmt);
64 return 1;
65 }
66 return 0;
67 }
68
69 written = min(bufsize-1, elen);
70 if (test_buffer[written]) {
71 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
72 bufsize, fmt);
73 return 1;
74 }
75
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080076 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",
78 bufsize, fmt);
79 return 1;
80 }
81
Rasmus Villemoes707cc722015-11-06 16:30:29 -080082 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);
85 return 1;
86 }
87 return 0;
88}
89
90static void __printf(3, 4) __init
91__test(const char *expect, int elen, const char *fmt, ...)
92{
93 va_list ap;
94 int rand;
95 char *p;
96
Rasmus Villemoesfd0515d2016-01-15 16:58:50 -080097 if (elen >= BUF_SIZE) {
98 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
99 elen, fmt);
100 failed_tests++;
101 return;
102 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800103
104 va_start(ap, fmt);
105
106 /*
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.
111 */
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);
117
118 p = kvasprintf(GFP_KERNEL, fmt, ap);
119 if (p) {
Rasmus Villemoesb79a7db2016-01-15 16:59:02 -0800120 total_tests++;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800121 if (memcmp(p, expect, elen+1)) {
122 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
123 fmt, p, expect);
124 failed_tests++;
125 }
126 kfree(p);
127 }
128 va_end(ap);
129}
130
131#define test(expect, fmt, ...) \
132 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
133
134static void __init
135test_basic(void)
136{
137 /* Work around annoying "warning: zero-length gnu_printf format string". */
138 char nul = '\0';
139
140 test("", &nul);
141 test("100%", "100%%");
142 test("xxx%yyy", "xxx%cyyy", '%');
143 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
144}
145
146static void __init
147test_number(void)
148{
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);
Rasmus Villemoes1ca8e8e2016-01-15 16:58:59 -0800152 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);
155 /*
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
161 * behaviour.
162 */
163 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
164#ifndef __CHAR_UNSIGNED__
165 {
166 /*
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.
171 */
172 char val = -16;
173 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
174 }
175#endif
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800176}
177
178static void __init
179test_string(void)
180{
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");
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800184 test("1234 ", "%-10.4s", "123456");
185 test(" 1234", "%10.4s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800186 /*
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800187 * 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.
194 *
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.
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800198 */
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800199 test(" ", "%4.*s", -5, "123456");
200 test("123456", "%.s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800201 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
202 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
203}
204
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100205#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
206
207#if BITS_PER_LONG == 64
208
209#define PTR_WIDTH 16
Andy Shevchenkoc604b402018-02-16 23:07:03 +0200210#define PTR ((void *)0xffff0123456789abUL)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100211#define PTR_STR "ffff0123456789ab"
Thierry Escandece041c42018-06-13 19:18:40 +0200212#define PTR_VAL_NO_CRNG "(____ptrval____)"
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100213#define ZEROS "00000000" /* hex 32 zero bits */
214
215static int __init
216plain_format(void)
217{
218 char buf[PLAIN_BUF_SIZE];
219 int nchars;
220
221 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
222
Thierry Escandece041c42018-06-13 19:18:40 +0200223 if (nchars != PTR_WIDTH)
224 return -1;
225
226 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
227 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
228 PTR_VAL_NO_CRNG);
229 return 0;
230 }
231
232 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100233 return -1;
234
235 return 0;
236}
237
238#else
239
240#define PTR_WIDTH 8
241#define PTR ((void *)0x456789ab)
242#define PTR_STR "456789ab"
Thierry Escandece041c42018-06-13 19:18:40 +0200243#define PTR_VAL_NO_CRNG "(ptrval)"
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100244
245static int __init
246plain_format(void)
247{
248 /* Format is implicitly tested for 32 bit machines by plain_hash() */
249 return 0;
250}
251
252#endif /* BITS_PER_LONG == 64 */
253
254static int __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200255plain_hash_to_buffer(const void *p, char *buf, size_t len)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100256{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100257 int nchars;
258
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200259 nchars = snprintf(buf, len, "%p", p);
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100260
Thierry Escandece041c42018-06-13 19:18:40 +0200261 if (nchars != PTR_WIDTH)
262 return -1;
263
264 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
265 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
266 PTR_VAL_NO_CRNG);
267 return 0;
268 }
269
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200270 return 0;
271}
272
273
274static int __init
275plain_hash(void)
276{
277 char buf[PLAIN_BUF_SIZE];
278 int ret;
279
280 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
281 if (ret)
282 return ret;
283
Thierry Escandece041c42018-06-13 19:18:40 +0200284 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100285 return -1;
286
287 return 0;
288}
289
290/*
291 * We can't use test() to test %p because we don't know what output to expect
292 * after an address is hashed.
293 */
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800294static void __init
295plain(void)
296{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100297 int err;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800298
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100299 err = plain_hash();
300 if (err) {
301 pr_warn("plain 'p' does not appear to be hashed\n");
302 failed_tests++;
303 return;
304 }
305
306 err = plain_format();
307 if (err) {
308 pr_warn("hashing plain 'p' has unexpected format\n");
309 failed_tests++;
310 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800311}
312
313static void __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200314test_hashed(const char *fmt, const void *p)
315{
316 char buf[PLAIN_BUF_SIZE];
317 int ret;
318
319 /*
320 * No need to increase failed test counter since this is assumed
321 * to be called after plain().
322 */
323 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
324 if (ret)
325 return;
326
327 test(buf, fmt, p);
328}
329
330static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800331symbol_ptr(void)
332{
333}
334
335static void __init
336kernel_ptr(void)
337{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100338 /* We can't test this without access to kptr_restrict. */
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800339}
340
341static void __init
342struct_resource(void)
343{
344}
345
346static void __init
347addr(void)
348{
349}
350
351static void __init
352escaped_str(void)
353{
354}
355
356static void __init
357hex_string(void)
358{
359 const char buf[3] = {0xc0, 0xff, 0xee};
360
361 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
362 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
363 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
364 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
365}
366
367static void __init
368mac(void)
369{
370 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
371
372 test("2d:48:d6:fc:7a:05", "%pM", addr);
373 test("05:7a:fc:d6:48:2d", "%pMR", addr);
374 test("2d-48-d6-fc-7a-05", "%pMF", addr);
375 test("2d48d6fc7a05", "%pm", addr);
376 test("057afcd6482d", "%pmR", addr);
377}
378
379static void __init
380ip4(void)
381{
382 struct sockaddr_in sa;
383
384 sa.sin_family = AF_INET;
385 sa.sin_port = cpu_to_be16(12345);
386 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
387
388 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
389 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
390 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
391 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
392}
393
394static void __init
395ip6(void)
396{
397}
398
399static void __init
400ip(void)
401{
402 ip4();
403 ip6();
404}
405
406static void __init
407uuid(void)
408{
409 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
410 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
411
412 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
413 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
414 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
415 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
416}
417
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800418static struct dentry test_dentry[4] __initdata = {
419 { .d_parent = &test_dentry[0],
420 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
421 .d_iname = "foo" },
422 { .d_parent = &test_dentry[0],
423 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
424 .d_iname = "bravo" },
425 { .d_parent = &test_dentry[1],
426 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
427 .d_iname = "alfa" },
428 { .d_parent = &test_dentry[2],
429 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
430 .d_iname = "romeo" },
431};
432
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800433static void __init
434dentry(void)
435{
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800436 test("foo", "%pd", &test_dentry[0]);
437 test("foo", "%pd2", &test_dentry[0]);
438
439 test("romeo", "%pd", &test_dentry[3]);
440 test("alfa/romeo", "%pd2", &test_dentry[3]);
441 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
442 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
443 test("/bravo/alfa", "%pd4", &test_dentry[2]);
444
445 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
446 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800447}
448
449static void __init
450struct_va_format(void)
451{
452}
453
454static void __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200455struct_rtc_time(void)
456{
457 /* 1543210543 */
458 const struct rtc_time tm = {
459 .tm_sec = 43,
460 .tm_min = 35,
461 .tm_hour = 5,
462 .tm_mday = 26,
463 .tm_mon = 10,
464 .tm_year = 118,
465 };
466
467 test_hashed("%pt", &tm);
468
469 test("2018-11-26T05:35:43", "%ptR", &tm);
470 test("0118-10-26T05:35:43", "%ptRr", &tm);
471 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
472 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
473 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
474 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
475}
476
477static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800478struct_clk(void)
479{
480}
481
482static void __init
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800483large_bitmap(void)
484{
485 const int nbits = 1 << 16;
486 unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
487 if (!bits)
488 return;
489
490 bitmap_set(bits, 1, 20);
491 bitmap_set(bits, 60000, 15);
492 test("1-20,60000-60014", "%*pbl", nbits, bits);
493 kfree(bits);
494}
495
496static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800497bitmap(void)
498{
499 DECLARE_BITMAP(bits, 20);
500 const int primes[] = {2,3,5,7,11,13,17,19};
501 int i;
502
503 bitmap_zero(bits, 20);
504 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
505 test("|", "%20pbl|%*pbl", bits, 20, bits);
506
507 for (i = 0; i < ARRAY_SIZE(primes); ++i)
508 set_bit(primes[i], bits);
509 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
510 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
511
512 bitmap_fill(bits, 20);
513 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
514 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800515
516 large_bitmap();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800517}
518
519static void __init
520netdev_features(void)
521{
522}
523
524static void __init
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -0700525flags(void)
526{
527 unsigned long flags;
528 gfp_t gfp;
529 char *cmp_buffer;
530
531 flags = 0;
532 test("", "%pGp", &flags);
533
534 /* Page flags should filter the zone id */
535 flags = 1UL << NR_PAGEFLAGS;
536 test("", "%pGp", &flags);
537
538 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
539 | 1UL << PG_active | 1UL << PG_swapbacked;
540 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
541
542
543 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
544 | VM_DENYWRITE;
545 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
546
547 gfp = GFP_TRANSHUGE;
548 test("GFP_TRANSHUGE", "%pGg", &gfp);
549
550 gfp = GFP_ATOMIC|__GFP_DMA;
551 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
552
553 gfp = __GFP_ATOMIC;
554 test("__GFP_ATOMIC", "%pGg", &gfp);
555
556 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
557 if (!cmp_buffer)
558 return;
559
560 /* Any flags not translated by the table should remain numeric */
561 gfp = ~__GFP_BITS_MASK;
562 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
563 test(cmp_buffer, "%pGg", &gfp);
564
565 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
566 (unsigned long) gfp);
567 gfp |= __GFP_ATOMIC;
568 test(cmp_buffer, "%pGg", &gfp);
569
570 kfree(cmp_buffer);
571}
572
573static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800574test_pointer(void)
575{
576 plain();
577 symbol_ptr();
578 kernel_ptr();
579 struct_resource();
580 addr();
581 escaped_str();
582 hex_string();
583 mac();
584 ip();
585 uuid();
586 dentry();
587 struct_va_format();
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200588 struct_rtc_time();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800589 struct_clk();
590 bitmap();
591 netdev_features();
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -0700592 flags();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800593}
594
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100595static void __init selftest(void)
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800596{
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800597 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
598 if (!alloced_buffer)
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100599 return;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800600 test_buffer = alloced_buffer + PAD_SIZE;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800601
602 test_basic();
603 test_number();
604 test_string();
605 test_pointer();
606
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800607 kfree(alloced_buffer);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800608}
609
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100610KSTM_MODULE_LOADERS(test_printf);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800611MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
612MODULE_LICENSE("GPL");