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 static void 270 test_hashed(struct kunit *kunittest, const char *fmt, const void *p) 271 { 272 char buf[PLAIN_BUF_SIZE]; 273 274 plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); 275 276 test(buf, fmt, p); 277 } 278 279 /* 280 * NULL pointers aren't hashed. 281 */ 282 static void 283 null_pointer(struct kunit *kunittest) 284 { 285 test(ZEROS "00000000", "%p", NULL); 286 test(ZEROS "00000000", "%px", NULL); 287 test("(null)", "%pE", NULL); 288 } 289 290 /* 291 * Error pointers aren't hashed. 292 */ 293 static void 294 error_pointer(struct kunit *kunittest) 295 { 296 test(ONES "fffffff5", "%p", ERR_PTR(-11)); 297 test(ONES "fffffff5", "%px", ERR_PTR(-11)); 298 test("(efault)", "%pE", ERR_PTR(-11)); 299 } 300 301 #define PTR_INVALID ((void *)0x000000ab) 302 303 static void 304 invalid_pointer(struct kunit *kunittest) 305 { 306 test_hashed(kunittest, "%p", PTR_INVALID); 307 test(ZEROS "000000ab", "%px", PTR_INVALID); 308 test("(efault)", "%pE", PTR_INVALID); 309 } 310 311 static void 312 symbol_ptr(struct kunit *kunittest) 313 { 314 } 315 316 static void 317 kernel_ptr(struct kunit *kunittest) 318 { 319 /* We can't test this without access to kptr_restrict. */ 320 } 321 322 static void 323 struct_resource(struct kunit *kunittest) 324 { 325 struct resource test_resource = { 326 .start = 0xc0ffee00, 327 .end = 0xc0ffee00, 328 .flags = IORESOURCE_MEM, 329 }; 330 331 test("[mem 0xc0ffee00 flags 0x200]", 332 "%pr", &test_resource); 333 334 test_resource = (struct resource) { 335 .start = 0xc0ffee, 336 .end = 0xba5eba11, 337 .flags = IORESOURCE_MEM, 338 }; 339 test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]", 340 "%pr", &test_resource); 341 342 test_resource = (struct resource) { 343 .start = 0xba5eba11, 344 .end = 0xc0ffee, 345 .flags = IORESOURCE_MEM, 346 }; 347 test("[mem 0xba5eba11-0x00c0ffee flags 0x200]", 348 "%pr", &test_resource); 349 350 test_resource = (struct resource) { 351 .start = 0xba5eba11, 352 .end = 0xba5eca11, 353 .flags = IORESOURCE_MEM, 354 }; 355 356 test("[mem 0xba5eba11-0xba5eca11 flags 0x200]", 357 "%pr", &test_resource); 358 359 test_resource = (struct resource) { 360 .start = 0xba11, 361 .end = 0xca10, 362 .flags = IORESOURCE_IO | 363 IORESOURCE_DISABLED | 364 IORESOURCE_UNSET, 365 }; 366 367 test("[io size 0x1000 disabled]", 368 "%pR", &test_resource); 369 } 370 371 static void 372 struct_range(struct kunit *kunittest) 373 { 374 struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11, 375 0xc0ffee00ba5eba11); 376 test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range); 377 378 test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11); 379 test("[range 0x0000000000c0ffee-0x00000000ba5eba11]", 380 "%pra", &test_range); 381 382 test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee); 383 test("[range 0x00000000ba5eba11-0x0000000000c0ffee]", 384 "%pra", &test_range); 385 } 386 387 static void 388 addr(struct kunit *kunittest) 389 { 390 } 391 392 static void 393 escaped_str(struct kunit *kunittest) 394 { 395 } 396 397 static void 398 hex_string(struct kunit *kunittest) 399 { 400 const char buf[3] = {0xc0, 0xff, 0xee}; 401 402 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", 403 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); 404 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", 405 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf); 406 } 407 408 static void 409 mac(struct kunit *kunittest) 410 { 411 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; 412 413 test("2d:48:d6:fc:7a:05", "%pM", addr); 414 test("05:7a:fc:d6:48:2d", "%pMR", addr); 415 test("2d-48-d6-fc-7a-05", "%pMF", addr); 416 test("2d48d6fc7a05", "%pm", addr); 417 test("057afcd6482d", "%pmR", addr); 418 } 419 420 static void 421 ip4(struct kunit *kunittest) 422 { 423 struct sockaddr_in sa; 424 425 sa.sin_family = AF_INET; 426 sa.sin_port = cpu_to_be16(12345); 427 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); 428 429 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); 430 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); 431 sa.sin_addr.s_addr = cpu_to_be32(0x01020304); 432 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa); 433 } 434 435 static void 436 ip6(struct kunit *kunittest) 437 { 438 } 439 440 static void 441 uuid(struct kunit *kunittest) 442 { 443 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 444 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; 445 446 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); 447 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); 448 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); 449 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); 450 } 451 452 static struct dentry test_dentry[4] = { 453 { .d_parent = &test_dentry[0], 454 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3), 455 .d_iname = "foo" }, 456 { .d_parent = &test_dentry[0], 457 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5), 458 .d_iname = "bravo" }, 459 { .d_parent = &test_dentry[1], 460 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4), 461 .d_iname = "alfa" }, 462 { .d_parent = &test_dentry[2], 463 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5), 464 .d_iname = "romeo" }, 465 }; 466 467 static void 468 dentry(struct kunit *kunittest) 469 { 470 test("foo", "%pd", &test_dentry[0]); 471 test("foo", "%pd2", &test_dentry[0]); 472 473 test("(null)", "%pd", NULL); 474 test("(efault)", "%pd", PTR_INVALID); 475 test("(null)", "%pD", NULL); 476 test("(efault)", "%pD", PTR_INVALID); 477 478 test("romeo", "%pd", &test_dentry[3]); 479 test("alfa/romeo", "%pd2", &test_dentry[3]); 480 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]); 481 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]); 482 test("/bravo/alfa", "%pd4", &test_dentry[2]); 483 484 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]); 485 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]); 486 } 487 488 static void 489 struct_va_format(struct kunit *kunittest) 490 { 491 } 492 493 static void 494 time_and_date(struct kunit *kunittest) 495 { 496 /* 1543210543 */ 497 const struct rtc_time tm = { 498 .tm_sec = 43, 499 .tm_min = 35, 500 .tm_hour = 5, 501 .tm_mday = 26, 502 .tm_mon = 10, 503 .tm_year = 118, 504 }; 505 /* 2019-01-04T15:32:23 */ 506 time64_t t = 1546615943; 507 508 test("(%pt?)", "%pt", &tm); 509 test("2018-11-26T05:35:43", "%ptR", &tm); 510 test("0118-10-26T05:35:43", "%ptRr", &tm); 511 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm); 512 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm); 513 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm); 514 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm); 515 516 test("2019-01-04T15:32:23", "%ptT", &t); 517 test("0119-00-04T15:32:23", "%ptTr", &t); 518 test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t); 519 test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t); 520 521 test("2019-01-04 15:32:23", "%ptTs", &t); 522 test("0119-00-04 15:32:23", "%ptTsr", &t); 523 test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t); 524 test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t); 525 } 526 527 static void 528 struct_clk(struct kunit *kunittest) 529 { 530 } 531 532 static void 533 large_bitmap(struct kunit *kunittest) 534 { 535 const int nbits = 1 << 16; 536 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL); 537 if (!bits) 538 return; 539 540 bitmap_set(bits, 1, 20); 541 bitmap_set(bits, 60000, 15); 542 test("1-20,60000-60014", "%*pbl", nbits, bits); 543 bitmap_free(bits); 544 } 545 546 static void 547 bitmap(struct kunit *kunittest) 548 { 549 DECLARE_BITMAP(bits, 20); 550 const int primes[] = {2,3,5,7,11,13,17,19}; 551 int i; 552 553 bitmap_zero(bits, 20); 554 test("00000|00000", "%20pb|%*pb", bits, 20, bits); 555 test("|", "%20pbl|%*pbl", bits, 20, bits); 556 557 for (i = 0; i < ARRAY_SIZE(primes); ++i) 558 set_bit(primes[i], bits); 559 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits); 560 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits); 561 562 bitmap_fill(bits, 20); 563 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); 564 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); 565 566 large_bitmap(kunittest); 567 } 568 569 static void 570 netdev_features(struct kunit *kunittest) 571 { 572 } 573 574 struct page_flags_test { 575 int width; 576 int shift; 577 int mask; 578 const char *fmt; 579 const char *name; 580 }; 581 582 static const struct page_flags_test pft[] = { 583 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 584 "%d", "section"}, 585 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 586 "%d", "node"}, 587 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 588 "%d", "zone"}, 589 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 590 "%#x", "lastcpupid"}, 591 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 592 "%#x", "kasantag"}, 593 }; 594 595 static void 596 page_flags_test(struct kunit *kunittest, int section, int node, int zone, 597 int last_cpupid, int kasan_tag, unsigned long flags, const char *name, 598 char *cmp_buf) 599 { 600 unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; 601 unsigned long size; 602 bool append = false; 603 int i; 604 605 for (i = 0; i < ARRAY_SIZE(values); i++) 606 flags |= (values[i] & pft[i].mask) << pft[i].shift; 607 608 size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags); 609 if (flags & PAGEFLAGS_MASK) { 610 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); 611 append = true; 612 } 613 614 for (i = 0; i < ARRAY_SIZE(pft); i++) { 615 if (!pft[i].width) 616 continue; 617 618 if (append) 619 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); 620 621 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", 622 pft[i].name); 623 size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, 624 values[i] & pft[i].mask); 625 append = true; 626 } 627 628 snprintf(cmp_buf + size, BUF_SIZE - size, ")"); 629 630 test(cmp_buf, "%pGp", &flags); 631 } 632 633 static void 634 flags(struct kunit *kunittest) 635 { 636 unsigned long flags; 637 char *cmp_buffer; 638 gfp_t gfp; 639 640 cmp_buffer = kunit_kmalloc(kunittest, BUF_SIZE, GFP_KERNEL); 641 KUNIT_ASSERT_NOT_NULL(kunittest, cmp_buffer); 642 643 flags = 0; 644 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); 645 646 flags = 1UL << NR_PAGEFLAGS; 647 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); 648 649 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru 650 | 1UL << PG_active | 1UL << PG_swapbacked; 651 page_flags_test(kunittest, 1, 1, 1, 0x1fffff, 1, flags, 652 "uptodate|dirty|lru|active|swapbacked", 653 cmp_buffer); 654 655 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 656 test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags); 657 658 gfp = GFP_TRANSHUGE; 659 test("GFP_TRANSHUGE", "%pGg", &gfp); 660 661 gfp = GFP_ATOMIC|__GFP_DMA; 662 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp); 663 664 gfp = __GFP_HIGH; 665 test("__GFP_HIGH", "%pGg", &gfp); 666 667 /* Any flags not translated by the table should remain numeric */ 668 gfp = ~__GFP_BITS_MASK; 669 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp); 670 test(cmp_buffer, "%pGg", &gfp); 671 672 snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx", 673 (unsigned long) gfp); 674 gfp |= __GFP_HIGH; 675 test(cmp_buffer, "%pGg", &gfp); 676 } 677 678 static void fwnode_pointer(struct kunit *kunittest) 679 { 680 const struct software_node first = { .name = "first" }; 681 const struct software_node second = { .name = "second", .parent = &first }; 682 const struct software_node third = { .name = "third", .parent = &second }; 683 const struct software_node *group[] = { &first, &second, &third, NULL }; 684 const char * const full_name_second = "first/second"; 685 const char * const full_name_third = "first/second/third"; 686 const char * const second_name = "second"; 687 const char * const third_name = "third"; 688 int rval; 689 690 rval = software_node_register_node_group(group); 691 if (rval) { 692 kunit_skip(kunittest, "cannot register softnodes; rval %d\n", rval); 693 } 694 695 test(full_name_second, "%pfw", software_node_fwnode(&second)); 696 test(full_name_third, "%pfw", software_node_fwnode(&third)); 697 test(full_name_third, "%pfwf", software_node_fwnode(&third)); 698 test(second_name, "%pfwP", software_node_fwnode(&second)); 699 test(third_name, "%pfwP", software_node_fwnode(&third)); 700 701 software_node_unregister_node_group(group); 702 } 703 704 struct fourcc_struct { 705 u32 code; 706 const char *str; 707 }; 708 709 static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc, 710 size_t n, const char *fmt) 711 { 712 size_t i; 713 714 for (i = 0; i < n; i++) 715 test(fc[i].str, fmt, &fc[i].code); 716 } 717 718 static void fourcc_pointer(struct kunit *kunittest) 719 { 720 static const struct fourcc_struct try_cc[] = { 721 { 0x3231564e, "NV12 little-endian (0x3231564e)", }, 722 { 0xb231564e, "NV12 big-endian (0xb231564e)", }, 723 { 0x10111213, ".... little-endian (0x10111213)", }, 724 { 0x20303159, "Y10 little-endian (0x20303159)", }, 725 }; 726 static const struct fourcc_struct try_ch[] = { 727 { 0x41424344, "ABCD (0x41424344)", }, 728 }; 729 static const struct fourcc_struct try_chR[] = { 730 { 0x41424344, "DCBA (0x44434241)", }, 731 }; 732 static const struct fourcc_struct try_cl[] = { 733 { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", }, 734 }; 735 static const struct fourcc_struct try_cb[] = { 736 { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", }, 737 }; 738 739 fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc"); 740 fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch"); 741 fourcc_pointer_test(kunittest, try_chR, ARRAY_SIZE(try_chR), "%p4chR"); 742 fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl"); 743 fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb"); 744 } 745 746 static void 747 errptr(struct kunit *kunittest) 748 { 749 test("-1234", "%pe", ERR_PTR(-1234)); 750 751 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */ 752 BUILD_BUG_ON(IS_ERR(PTR)); 753 test_hashed(kunittest, "%pe", PTR); 754 755 #ifdef CONFIG_SYMBOLIC_ERRNAME 756 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK)); 757 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN)); 758 BUILD_BUG_ON(EAGAIN != EWOULDBLOCK); 759 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK)); 760 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO)); 761 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO)); 762 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER)); 763 #endif 764 } 765 766 static int printf_suite_init(struct kunit_suite *suite) 767 { 768 total_tests = 0; 769 770 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); 771 if (!alloced_buffer) 772 return -ENOMEM; 773 test_buffer = alloced_buffer + PAD_SIZE; 774 775 return 0; 776 } 777 778 static void printf_suite_exit(struct kunit_suite *suite) 779 { 780 kfree(alloced_buffer); 781 782 kunit_info(suite, "ran %u tests\n", total_tests); 783 } 784 785 static struct kunit_case printf_test_cases[] = { 786 KUNIT_CASE(test_basic), 787 KUNIT_CASE(test_number), 788 KUNIT_CASE(test_string), 789 KUNIT_CASE(hash_pointer), 790 KUNIT_CASE(null_pointer), 791 KUNIT_CASE(error_pointer), 792 KUNIT_CASE(invalid_pointer), 793 KUNIT_CASE(symbol_ptr), 794 KUNIT_CASE(kernel_ptr), 795 KUNIT_CASE(struct_resource), 796 KUNIT_CASE(struct_range), 797 KUNIT_CASE(addr), 798 KUNIT_CASE(escaped_str), 799 KUNIT_CASE(hex_string), 800 KUNIT_CASE(mac), 801 KUNIT_CASE(ip4), 802 KUNIT_CASE(ip6), 803 KUNIT_CASE(uuid), 804 KUNIT_CASE(dentry), 805 KUNIT_CASE(struct_va_format), 806 KUNIT_CASE(time_and_date), 807 KUNIT_CASE(struct_clk), 808 KUNIT_CASE(bitmap), 809 KUNIT_CASE(netdev_features), 810 KUNIT_CASE(flags), 811 KUNIT_CASE(errptr), 812 KUNIT_CASE(fwnode_pointer), 813 KUNIT_CASE(fourcc_pointer), 814 {} 815 }; 816 817 static struct kunit_suite printf_test_suite = { 818 .name = "printf", 819 .suite_init = printf_suite_init, 820 .suite_exit = printf_suite_exit, 821 .test_cases = printf_test_cases, 822 }; 823 824 kunit_test_suite(printf_test_suite); 825 826 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>"); 827 MODULE_DESCRIPTION("Test cases for printf facility"); 828 MODULE_LICENSE("GPL"); 829