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