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