Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Test cases for printf facility.
4 */
5
6#include <kunit/test.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/printk.h>
10#include <linux/random.h>
11#include <linux/rtc.h>
12#include <linux/slab.h>
13#include <linux/sprintf.h>
14#include <linux/string.h>
15
16#include <linux/bitmap.h>
17#include <linux/dcache.h>
18#include <linux/socket.h>
19#include <linux/in.h>
20#include <linux/in6.h>
21
22#include <linux/gfp.h>
23#include <linux/mm.h>
24
25#include <linux/property.h>
26
27#define BUF_SIZE 256
28#define PAD_SIZE 16
29#define FILL_CHAR '$'
30
31#define NOWARN(option, comment, block) \
32 __diag_push(); \
33 __diag_ignore_all(#option, comment); \
34 block \
35 __diag_pop();
36
37static unsigned int total_tests;
38
39static char *test_buffer;
40static char *alloced_buffer;
41
42static void __printf(7, 0)
43do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect,
44 int elen, const char *fmt, va_list ap)
45{
46 va_list aq;
47 int ret, written;
48
49 total_tests++;
50
51 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
52 va_copy(aq, ap);
53 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
54 va_end(aq);
55
56 if (ret != elen) {
57 KUNIT_FAIL(kunittest,
58 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
59 file, line, bufsize, fmt, ret, elen);
60 return;
61 }
62
63 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
64 KUNIT_FAIL(kunittest,
65 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n",
66 file, line, bufsize, fmt);
67 return;
68 }
69
70 if (!bufsize) {
71 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
72 KUNIT_FAIL(kunittest,
73 "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
74 file, line, fmt);
75 }
76 return;
77 }
78
79 written = min(bufsize-1, elen);
80 if (test_buffer[written]) {
81 KUNIT_FAIL(kunittest,
82 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
83 file, line, bufsize, fmt);
84 return;
85 }
86
87 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
88 KUNIT_FAIL(kunittest,
89 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
90 file, line, bufsize, fmt);
91 return;
92 }
93
94 if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
95 KUNIT_FAIL(kunittest,
96 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
97 file, line, bufsize, fmt);
98 return;
99 }
100
101 if (memcmp(test_buffer, expect, written)) {
102 KUNIT_FAIL(kunittest,
103 "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
104 file, line, bufsize, fmt, test_buffer, written, expect);
105 return;
106 }
107}
108
109static void __printf(6, 7)
110__test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen,
111 const char *fmt, ...)
112{
113 va_list ap;
114 int rand;
115 char *p;
116
117 if (elen >= BUF_SIZE) {
118 KUNIT_FAIL(kunittest,
119 "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
120 file, line, elen, BUF_SIZE, fmt);
121 return;
122 }
123
124 va_start(ap, fmt);
125
126 /*
127 * Every fmt+args is subjected to four tests: Three where we
128 * tell vsnprintf varying buffer sizes (plenty, not quite
129 * enough and 0), and then we also test that kvasprintf would
130 * be able to print it as expected.
131 */
132 do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap);
133 rand = get_random_u32_inclusive(1, elen + 1);
134 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
135 do_test(kunittest, file, line, rand, expect, elen, fmt, ap);
136 do_test(kunittest, file, line, 0, expect, elen, fmt, ap);
137
138 p = kvasprintf(GFP_KERNEL, fmt, ap);
139 if (p) {
140 total_tests++;
141 if (memcmp(p, expect, elen+1)) {
142 KUNIT_FAIL(kunittest,
143 "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
144 file, line, fmt, p, expect);
145 }
146 kfree(p);
147 }
148 va_end(ap);
149}
150
151#define test(expect, fmt, ...) \
152 __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__)
153
154static void
155test_basic(struct kunit *kunittest)
156{
157 /* Work around annoying "warning: zero-length gnu_printf format string". */
158 char nul = '\0';
159
160 test("", &nul);
161 test("100%", "100%%");
162 test("xxx%yyy", "xxx%cyyy", '%');
163 __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0');
164}
165
166static void
167test_number(struct kunit *kunittest)
168{
169 test("0x1234abcd ", "%#-12x", 0x1234abcd);
170 test(" 0x1234abcd", "%#12x", 0x1234abcd);
171 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
172 NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
173 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
174 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
175 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
176 })
177 /*
178 * POSIX/C99: »The result of converting zero with an explicit
179 * precision of zero shall be no characters.« Hence the output
180 * from the below test should really be "00|0||| ". However,
181 * the kernel's printf also produces a single 0 in that
182 * case. This test case simply documents the current
183 * behaviour.
184 */
185 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
186}
187
188static void
189test_string(struct kunit *kunittest)
190{
191 test("", "%s%.0s", "", "123");
192 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
193 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
194 test("1234 ", "%-10.4s", "123456");
195 test(" 1234", "%10.4s", "123456");
196 /*
197 * POSIX and C99 say that a negative precision (which is only
198 * possible to pass via a * argument) should be treated as if
199 * the precision wasn't present, and that if the precision is
200 * omitted (as in %.s), the precision should be taken to be
201 * 0. However, the kernel's printf behave exactly opposite,
202 * treating a negative precision as 0 and treating an omitted
203 * precision specifier as if no precision was given.
204 *
205 * These test cases document the current behaviour; should
206 * anyone ever feel the need to follow the standards more
207 * closely, this can be revisited.
208 */
209 test(" ", "%4.*s", -5, "123456");
210 test("123456", "%.s", "123456");
211 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
212 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
213}
214
215#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
216
217#if BITS_PER_LONG == 64
218
219#define PTR_WIDTH 16
220#define PTR ((void *)0xffff0123456789abUL)
221#define PTR_STR "ffff0123456789ab"
222#define PTR_VAL_NO_CRNG "(____ptrval____)"
223#define ZEROS "00000000" /* hex 32 zero bits */
224#define ONES "ffffffff" /* hex 32 one bits */
225
226#else
227
228#define PTR_WIDTH 8
229#define PTR ((void *)0x456789ab)
230#define PTR_STR "456789ab"
231#define PTR_VAL_NO_CRNG "(ptrval)"
232#define ZEROS ""
233#define ONES ""
234
235#endif /* BITS_PER_LONG == 64 */
236
237static void
238plain_hash_to_buffer(struct kunit *kunittest, const void *p, char *buf, size_t len)
239{
240 KUNIT_ASSERT_EQ(kunittest, snprintf(buf, len, "%p", p), PTR_WIDTH);
241
242 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
243 kunit_skip(kunittest,
244 "crng possibly not yet initialized. plain 'p' buffer contains \"%s\"\n",
245 PTR_VAL_NO_CRNG);
246 }
247}
248
249static void
250hash_pointer(struct kunit *kunittest)
251{
252 if (no_hash_pointers)
253 kunit_skip(kunittest, "hash pointers disabled");
254
255 char buf[PLAIN_BUF_SIZE];
256
257 plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE);
258
259 /*
260 * The hash of %p is unpredictable, therefore test() cannot be used.
261 *
262 * Instead verify that the first 32 bits are zeros on a 64-bit system
263 * and that the non-hashed value is not printed.
264 */
265
266 KUNIT_EXPECT_MEMEQ(kunittest, buf, ZEROS, strlen(ZEROS));
267 KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH);
268}
269
270/*
271 * This is a macro so that the compiler can compare its arguments to the
272 * __printf() attribute on __test(). This cannot be a function with a __printf()
273 * attribute because GCC requires __printf() functions to be variadic.
274 */
275#define test_hashed(kunittest, fmt, p) \
276 do { \
277 char buf[PLAIN_BUF_SIZE]; \
278 plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \
279 test(buf, fmt, p); \
280 } while (0)
281
282/*
283 * NULL pointers aren't hashed.
284 */
285static void
286null_pointer(struct kunit *kunittest)
287{
288 test(ZEROS "00000000", "%p", NULL);
289 test(ZEROS "00000000", "%px", NULL);
290 test("(null)", "%pE", NULL);
291}
292
293/*
294 * Error pointers aren't hashed.
295 */
296static void
297error_pointer(struct kunit *kunittest)
298{
299 test(ONES "fffffff5", "%p", ERR_PTR(-11));
300 test(ONES "fffffff5", "%px", ERR_PTR(-11));
301 test("(efault)", "%pE", ERR_PTR(-11));
302}
303
304#define PTR_INVALID ((void *)0x000000ab)
305
306static void
307invalid_pointer(struct kunit *kunittest)
308{
309 test_hashed(kunittest, "%p", PTR_INVALID);
310 test(ZEROS "000000ab", "%px", PTR_INVALID);
311 test("(efault)", "%pE", PTR_INVALID);
312}
313
314static void
315symbol_ptr(struct kunit *kunittest)
316{
317}
318
319static void
320kernel_ptr(struct kunit *kunittest)
321{
322 /* We can't test this without access to kptr_restrict. */
323}
324
325static void
326struct_resource(struct kunit *kunittest)
327{
328 struct resource test_resource = {
329 .start = 0xc0ffee00,
330 .end = 0xc0ffee00,
331 .flags = IORESOURCE_MEM,
332 };
333
334 test("[mem 0xc0ffee00 flags 0x200]",
335 "%pr", &test_resource);
336
337 test_resource = (struct resource) {
338 .start = 0xc0ffee,
339 .end = 0xba5eba11,
340 .flags = IORESOURCE_MEM,
341 };
342 test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]",
343 "%pr", &test_resource);
344
345 test_resource = (struct resource) {
346 .start = 0xba5eba11,
347 .end = 0xc0ffee,
348 .flags = IORESOURCE_MEM,
349 };
350 test("[mem 0xba5eba11-0x00c0ffee flags 0x200]",
351 "%pr", &test_resource);
352
353 test_resource = (struct resource) {
354 .start = 0xba5eba11,
355 .end = 0xba5eca11,
356 .flags = IORESOURCE_MEM,
357 };
358
359 test("[mem 0xba5eba11-0xba5eca11 flags 0x200]",
360 "%pr", &test_resource);
361
362 test_resource = (struct resource) {
363 .start = 0xba11,
364 .end = 0xca10,
365 .flags = IORESOURCE_IO |
366 IORESOURCE_DISABLED |
367 IORESOURCE_UNSET,
368 };
369
370 test("[io size 0x1000 disabled]",
371 "%pR", &test_resource);
372}
373
374static void
375struct_range(struct kunit *kunittest)
376{
377 struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11,
378 0xc0ffee00ba5eba11);
379 test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range);
380
381 test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11);
382 test("[range 0x0000000000c0ffee-0x00000000ba5eba11]",
383 "%pra", &test_range);
384
385 test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee);
386 test("[range 0x00000000ba5eba11-0x0000000000c0ffee]",
387 "%pra", &test_range);
388}
389
390static void
391addr(struct kunit *kunittest)
392{
393}
394
395static void
396escaped_str(struct kunit *kunittest)
397{
398}
399
400static void
401hex_string(struct kunit *kunittest)
402{
403 const char buf[3] = {0xc0, 0xff, 0xee};
404
405 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
406 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
407 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
408 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
409}
410
411static void
412mac(struct kunit *kunittest)
413{
414 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
415
416 test("2d:48:d6:fc:7a:05", "%pM", addr);
417 test("05:7a:fc:d6:48:2d", "%pMR", addr);
418 test("2d-48-d6-fc-7a-05", "%pMF", addr);
419 test("2d48d6fc7a05", "%pm", addr);
420 test("057afcd6482d", "%pmR", addr);
421}
422
423static void
424ip4(struct kunit *kunittest)
425{
426 struct sockaddr_in sa;
427
428 sa.sin_family = AF_INET;
429 sa.sin_port = cpu_to_be16(12345);
430 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
431
432 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
433 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
434 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
435 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
436}
437
438static void
439ip6(struct kunit *kunittest)
440{
441 const struct in6_addr addr = {
442 .s6_addr = { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
443 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 }
444 };
445 const struct in6_addr single_zero = {
446 .s6_addr = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04,
447 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 }
448 };
449 struct sockaddr_in6 sa = {
450 .sin6_family = AF_INET6,
451 .sin6_port = cpu_to_be16(12345),
452 .sin6_addr = addr,
453 };
454
455 test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008",
456 "%pi6|%pI6", &addr, &addr);
457 test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008",
458 "%piS|%pIS", &sa, &sa);
459 test("1:2:3:4:5:6:7:8", "%pI6c", &addr);
460 test("1:0:3:4:5:6:7:8", "%pI6c", &single_zero);
461 test("[1:2:3:4:5:6:7:8]:12345", "%pISpc", &sa);
462}
463
464static void
465uuid(struct kunit *kunittest)
466{
467 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
468 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
469
470 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
471 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
472 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
473 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
474}
475
476static struct dentry test_dentry[4] = {
477 { .d_parent = &test_dentry[0],
478 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
479 .d_iname = "foo" },
480 { .d_parent = &test_dentry[0],
481 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
482 .d_iname = "bravo" },
483 { .d_parent = &test_dentry[1],
484 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
485 .d_iname = "alfa" },
486 { .d_parent = &test_dentry[2],
487 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
488 .d_iname = "romeo" },
489};
490
491static void
492dentry(struct kunit *kunittest)
493{
494 test("foo", "%pd", &test_dentry[0]);
495 test("foo", "%pd2", &test_dentry[0]);
496
497 test("(null)", "%pd", NULL);
498 test("(efault)", "%pd", PTR_INVALID);
499 test("(null)", "%pD", NULL);
500 test("(efault)", "%pD", PTR_INVALID);
501
502 test("romeo", "%pd", &test_dentry[3]);
503 test("alfa/romeo", "%pd2", &test_dentry[3]);
504 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
505 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
506 test("/bravo/alfa", "%pd4", &test_dentry[2]);
507
508 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
509 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
510}
511
512static void
513struct_va_format(struct kunit *kunittest)
514{
515}
516
517static void
518time_and_date(struct kunit *kunittest)
519{
520 /* 1543210543 */
521 const struct rtc_time tm = {
522 .tm_sec = 43,
523 .tm_min = 35,
524 .tm_hour = 5,
525 .tm_mday = 26,
526 .tm_mon = 10,
527 .tm_year = 118,
528 };
529 /* 2019-01-04T15:32:23 */
530 time64_t t = 1546615943;
531 struct timespec64 ts = { .tv_sec = t, .tv_nsec = 11235813 };
532
533 test("(%pt?)", "%pt", &tm);
534 test("2018-11-26T05:35:43", "%ptR", &tm);
535 test("0118-10-26T05:35:43", "%ptRr", &tm);
536 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
537 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
538 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
539 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
540
541 test("2019-01-04T15:32:23", "%ptT", &t);
542 test("0119-00-04T15:32:23", "%ptTr", &t);
543 test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
544 test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
545
546 test("2019-01-04 15:32:23", "%ptTs", &t);
547 test("0119-00-04 15:32:23", "%ptTsr", &t);
548 test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
549 test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
550
551 test("2019-01-04T15:32:23.011235813", "%ptS", &ts);
552 test("1546615943.011235813", "%ptSp", &ts);
553}
554
555static void
556struct_clk(struct kunit *kunittest)
557{
558}
559
560static void
561large_bitmap(struct kunit *kunittest)
562{
563 const int nbits = 1 << 16;
564 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
565 if (!bits)
566 return;
567
568 bitmap_set(bits, 1, 20);
569 bitmap_set(bits, 60000, 15);
570 test("1-20,60000-60014", "%*pbl", nbits, bits);
571 bitmap_free(bits);
572}
573
574static void
575bitmap(struct kunit *kunittest)
576{
577 DECLARE_BITMAP(bits, 20);
578 const int primes[] = {2,3,5,7,11,13,17,19};
579 int i;
580
581 bitmap_zero(bits, 20);
582 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
583 test("|", "%20pbl|%*pbl", bits, 20, bits);
584
585 for (i = 0; i < ARRAY_SIZE(primes); ++i)
586 set_bit(primes[i], bits);
587 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
588 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
589
590 bitmap_fill(bits, 20);
591 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
592 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
593
594 large_bitmap(kunittest);
595}
596
597static void
598netdev_features(struct kunit *kunittest)
599{
600}
601
602struct page_flags_test {
603 int width;
604 int shift;
605 int mask;
606 const char *fmt;
607 const char *name;
608};
609
610static const struct page_flags_test pft[] = {
611 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
612 "%d", "section"},
613 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
614 "%d", "node"},
615 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
616 "%d", "zone"},
617 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
618 "%#x", "lastcpupid"},
619 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
620 "%#x", "kasantag"},
621};
622
623static void
624page_flags_test(struct kunit *kunittest, int section, int node, int zone,
625 int last_cpupid, int kasan_tag, unsigned long flags, const char *name,
626 char *cmp_buf)
627{
628 unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag};
629 unsigned long size;
630 bool append = false;
631 int i;
632
633 for (i = 0; i < ARRAY_SIZE(values); i++)
634 flags |= (values[i] & pft[i].mask) << pft[i].shift;
635
636 size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags);
637 if (flags & PAGEFLAGS_MASK) {
638 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
639 append = true;
640 }
641
642 for (i = 0; i < ARRAY_SIZE(pft); i++) {
643 if (!pft[i].width)
644 continue;
645
646 if (append)
647 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
648
649 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
650 pft[i].name);
651 size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
652 values[i] & pft[i].mask);
653 append = true;
654 }
655
656 snprintf(cmp_buf + size, BUF_SIZE - size, ")");
657
658 test(cmp_buf, "%pGp", &flags);
659}
660
661static void
662flags(struct kunit *kunittest)
663{
664 unsigned long flags;
665 char *cmp_buffer;
666 gfp_t gfp;
667
668 cmp_buffer = kunit_kmalloc(kunittest, BUF_SIZE, GFP_KERNEL);
669 KUNIT_ASSERT_NOT_NULL(kunittest, cmp_buffer);
670
671 flags = 0;
672 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer);
673
674 flags = 1UL << NR_PAGEFLAGS;
675 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer);
676
677 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
678 | 1UL << PG_active | 1UL << PG_swapbacked;
679 page_flags_test(kunittest, 1, 1, 1, 0x1fffff, 1, flags,
680 "uptodate|dirty|lru|active|swapbacked",
681 cmp_buffer);
682
683 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
684 test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);
685
686 gfp = GFP_TRANSHUGE;
687 test("GFP_TRANSHUGE", "%pGg", &gfp);
688
689 gfp = GFP_ATOMIC|__GFP_DMA;
690 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
691
692 gfp = __GFP_HIGH;
693 test("__GFP_HIGH", "%pGg", &gfp);
694
695 /* Any flags not translated by the table should remain numeric */
696 gfp = ~__GFP_BITS_MASK;
697 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
698 test(cmp_buffer, "%pGg", &gfp);
699
700 snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx",
701 (unsigned long) gfp);
702 gfp |= __GFP_HIGH;
703 test(cmp_buffer, "%pGg", &gfp);
704}
705
706static void fwnode_pointer(struct kunit *kunittest)
707{
708 const struct software_node first = { .name = "first" };
709 const struct software_node second = { .name = "second", .parent = &first };
710 const struct software_node third = { .name = "third", .parent = &second };
711 const struct software_node *group[] = { &first, &second, &third, NULL };
712 const char * const full_name_second = "first/second";
713 const char * const full_name_third = "first/second/third";
714 const char * const second_name = "second";
715 const char * const third_name = "third";
716 int rval;
717
718 rval = software_node_register_node_group(group);
719 if (rval) {
720 kunit_skip(kunittest, "cannot register softnodes; rval %d\n", rval);
721 }
722
723 test(full_name_second, "%pfw", software_node_fwnode(&second));
724 test(full_name_third, "%pfw", software_node_fwnode(&third));
725 test(full_name_third, "%pfwf", software_node_fwnode(&third));
726 test(second_name, "%pfwP", software_node_fwnode(&second));
727 test(third_name, "%pfwP", software_node_fwnode(&third));
728
729 software_node_unregister_node_group(group);
730}
731
732struct fourcc_struct {
733 u32 code;
734 const char *str;
735};
736
737static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc,
738 size_t n, const char *fmt)
739{
740 size_t i;
741
742 for (i = 0; i < n; i++)
743 test(fc[i].str, fmt, &fc[i].code);
744}
745
746static void fourcc_pointer(struct kunit *kunittest)
747{
748 static const struct fourcc_struct try_cc[] = {
749 { 0x3231564e, "NV12 little-endian (0x3231564e)", },
750 { 0xb231564e, "NV12 big-endian (0xb231564e)", },
751 { 0x10111213, ".... little-endian (0x10111213)", },
752 { 0x20303159, "Y10 little-endian (0x20303159)", },
753 };
754 static const struct fourcc_struct try_ch[] = {
755 { 0x41424344, "ABCD (0x41424344)", },
756 };
757 static const struct fourcc_struct try_chR[] = {
758 { 0x41424344, "DCBA (0x44434241)", },
759 };
760 static const struct fourcc_struct try_cl[] = {
761 { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", },
762 };
763 static const struct fourcc_struct try_cb[] = {
764 { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", },
765 };
766
767 fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc");
768 fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch");
769 fourcc_pointer_test(kunittest, try_chR, ARRAY_SIZE(try_chR), "%p4chR");
770 fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl");
771 fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
772}
773
774static void
775errptr(struct kunit *kunittest)
776{
777 test("-1234", "%pe", ERR_PTR(-1234));
778
779 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
780 BUILD_BUG_ON(IS_ERR(PTR));
781 test_hashed(kunittest, "%pe", PTR);
782
783#ifdef CONFIG_SYMBOLIC_ERRNAME
784 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
785 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
786 BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
787 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
788 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
789 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
790 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
791#endif
792}
793
794static int printf_suite_init(struct kunit_suite *suite)
795{
796 total_tests = 0;
797
798 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
799 if (!alloced_buffer)
800 return -ENOMEM;
801 test_buffer = alloced_buffer + PAD_SIZE;
802
803 return 0;
804}
805
806static void printf_suite_exit(struct kunit_suite *suite)
807{
808 kfree(alloced_buffer);
809
810 kunit_info(suite, "ran %u tests\n", total_tests);
811}
812
813static struct kunit_case printf_test_cases[] = {
814 KUNIT_CASE(test_basic),
815 KUNIT_CASE(test_number),
816 KUNIT_CASE(test_string),
817 KUNIT_CASE(hash_pointer),
818 KUNIT_CASE(null_pointer),
819 KUNIT_CASE(error_pointer),
820 KUNIT_CASE(invalid_pointer),
821 KUNIT_CASE(symbol_ptr),
822 KUNIT_CASE(kernel_ptr),
823 KUNIT_CASE(struct_resource),
824 KUNIT_CASE(struct_range),
825 KUNIT_CASE(addr),
826 KUNIT_CASE(escaped_str),
827 KUNIT_CASE(hex_string),
828 KUNIT_CASE(mac),
829 KUNIT_CASE(ip4),
830 KUNIT_CASE(ip6),
831 KUNIT_CASE(uuid),
832 KUNIT_CASE(dentry),
833 KUNIT_CASE(struct_va_format),
834 KUNIT_CASE(time_and_date),
835 KUNIT_CASE(struct_clk),
836 KUNIT_CASE(bitmap),
837 KUNIT_CASE(netdev_features),
838 KUNIT_CASE(flags),
839 KUNIT_CASE(errptr),
840 KUNIT_CASE(fwnode_pointer),
841 KUNIT_CASE(fourcc_pointer),
842 {}
843};
844
845static struct kunit_suite printf_test_suite = {
846 .name = "printf",
847 .suite_init = printf_suite_init,
848 .suite_exit = printf_suite_exit,
849 .test_cases = printf_test_cases,
850};
851
852kunit_test_suite(printf_test_suite);
853
854MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
855MODULE_DESCRIPTION("Test cases for printf facility");
856MODULE_LICENSE("GPL");