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 struct timespec64 ts = { .tv_sec = t, .tv_nsec = 11235813 }; 508 509 test("(%pt?)", "%pt", &tm); 510 test("2018-11-26T05:35:43", "%ptR", &tm); 511 test("0118-10-26T05:35:43", "%ptRr", &tm); 512 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm); 513 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm); 514 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm); 515 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm); 516 517 test("2019-01-04T15:32:23", "%ptT", &t); 518 test("0119-00-04T15:32:23", "%ptTr", &t); 519 test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t); 520 test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t); 521 522 test("2019-01-04 15:32:23", "%ptTs", &t); 523 test("0119-00-04 15:32:23", "%ptTsr", &t); 524 test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t); 525 test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t); 526 527 test("2019-01-04T15:32:23.011235813", "%ptS", &ts); 528 test("1546615943.011235813", "%ptSp", &ts); 529 } 530 531 static void 532 struct_clk(struct kunit *kunittest) 533 { 534 } 535 536 static void 537 large_bitmap(struct kunit *kunittest) 538 { 539 const int nbits = 1 << 16; 540 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL); 541 if (!bits) 542 return; 543 544 bitmap_set(bits, 1, 20); 545 bitmap_set(bits, 60000, 15); 546 test("1-20,60000-60014", "%*pbl", nbits, bits); 547 bitmap_free(bits); 548 } 549 550 static void 551 bitmap(struct kunit *kunittest) 552 { 553 DECLARE_BITMAP(bits, 20); 554 const int primes[] = {2,3,5,7,11,13,17,19}; 555 int i; 556 557 bitmap_zero(bits, 20); 558 test("00000|00000", "%20pb|%*pb", bits, 20, bits); 559 test("|", "%20pbl|%*pbl", bits, 20, bits); 560 561 for (i = 0; i < ARRAY_SIZE(primes); ++i) 562 set_bit(primes[i], bits); 563 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits); 564 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits); 565 566 bitmap_fill(bits, 20); 567 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); 568 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); 569 570 large_bitmap(kunittest); 571 } 572 573 static void 574 netdev_features(struct kunit *kunittest) 575 { 576 } 577 578 struct page_flags_test { 579 int width; 580 int shift; 581 int mask; 582 const char *fmt; 583 const char *name; 584 }; 585 586 static const struct page_flags_test pft[] = { 587 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 588 "%d", "section"}, 589 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 590 "%d", "node"}, 591 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 592 "%d", "zone"}, 593 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 594 "%#x", "lastcpupid"}, 595 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 596 "%#x", "kasantag"}, 597 }; 598 599 static void 600 page_flags_test(struct kunit *kunittest, int section, int node, int zone, 601 int last_cpupid, int kasan_tag, unsigned long flags, const char *name, 602 char *cmp_buf) 603 { 604 unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; 605 unsigned long size; 606 bool append = false; 607 int i; 608 609 for (i = 0; i < ARRAY_SIZE(values); i++) 610 flags |= (values[i] & pft[i].mask) << pft[i].shift; 611 612 size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags); 613 if (flags & PAGEFLAGS_MASK) { 614 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); 615 append = true; 616 } 617 618 for (i = 0; i < ARRAY_SIZE(pft); i++) { 619 if (!pft[i].width) 620 continue; 621 622 if (append) 623 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); 624 625 size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", 626 pft[i].name); 627 size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, 628 values[i] & pft[i].mask); 629 append = true; 630 } 631 632 snprintf(cmp_buf + size, BUF_SIZE - size, ")"); 633 634 test(cmp_buf, "%pGp", &flags); 635 } 636 637 static void 638 flags(struct kunit *kunittest) 639 { 640 unsigned long flags; 641 char *cmp_buffer; 642 gfp_t gfp; 643 644 cmp_buffer = kunit_kmalloc(kunittest, BUF_SIZE, GFP_KERNEL); 645 KUNIT_ASSERT_NOT_NULL(kunittest, cmp_buffer); 646 647 flags = 0; 648 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); 649 650 flags = 1UL << NR_PAGEFLAGS; 651 page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); 652 653 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru 654 | 1UL << PG_active | 1UL << PG_swapbacked; 655 page_flags_test(kunittest, 1, 1, 1, 0x1fffff, 1, flags, 656 "uptodate|dirty|lru|active|swapbacked", 657 cmp_buffer); 658 659 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 660 test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags); 661 662 gfp = GFP_TRANSHUGE; 663 test("GFP_TRANSHUGE", "%pGg", &gfp); 664 665 gfp = GFP_ATOMIC|__GFP_DMA; 666 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp); 667 668 gfp = __GFP_HIGH; 669 test("__GFP_HIGH", "%pGg", &gfp); 670 671 /* Any flags not translated by the table should remain numeric */ 672 gfp = ~__GFP_BITS_MASK; 673 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp); 674 test(cmp_buffer, "%pGg", &gfp); 675 676 snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx", 677 (unsigned long) gfp); 678 gfp |= __GFP_HIGH; 679 test(cmp_buffer, "%pGg", &gfp); 680 } 681 682 static void fwnode_pointer(struct kunit *kunittest) 683 { 684 const struct software_node first = { .name = "first" }; 685 const struct software_node second = { .name = "second", .parent = &first }; 686 const struct software_node third = { .name = "third", .parent = &second }; 687 const struct software_node *group[] = { &first, &second, &third, NULL }; 688 const char * const full_name_second = "first/second"; 689 const char * const full_name_third = "first/second/third"; 690 const char * const second_name = "second"; 691 const char * const third_name = "third"; 692 int rval; 693 694 rval = software_node_register_node_group(group); 695 if (rval) { 696 kunit_skip(kunittest, "cannot register softnodes; rval %d\n", rval); 697 } 698 699 test(full_name_second, "%pfw", software_node_fwnode(&second)); 700 test(full_name_third, "%pfw", software_node_fwnode(&third)); 701 test(full_name_third, "%pfwf", software_node_fwnode(&third)); 702 test(second_name, "%pfwP", software_node_fwnode(&second)); 703 test(third_name, "%pfwP", software_node_fwnode(&third)); 704 705 software_node_unregister_node_group(group); 706 } 707 708 struct fourcc_struct { 709 u32 code; 710 const char *str; 711 }; 712 713 static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc, 714 size_t n, const char *fmt) 715 { 716 size_t i; 717 718 for (i = 0; i < n; i++) 719 test(fc[i].str, fmt, &fc[i].code); 720 } 721 722 static void fourcc_pointer(struct kunit *kunittest) 723 { 724 static const struct fourcc_struct try_cc[] = { 725 { 0x3231564e, "NV12 little-endian (0x3231564e)", }, 726 { 0xb231564e, "NV12 big-endian (0xb231564e)", }, 727 { 0x10111213, ".... little-endian (0x10111213)", }, 728 { 0x20303159, "Y10 little-endian (0x20303159)", }, 729 }; 730 static const struct fourcc_struct try_ch[] = { 731 { 0x41424344, "ABCD (0x41424344)", }, 732 }; 733 static const struct fourcc_struct try_chR[] = { 734 { 0x41424344, "DCBA (0x44434241)", }, 735 }; 736 static const struct fourcc_struct try_cl[] = { 737 { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", }, 738 }; 739 static const struct fourcc_struct try_cb[] = { 740 { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", }, 741 }; 742 743 fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc"); 744 fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch"); 745 fourcc_pointer_test(kunittest, try_chR, ARRAY_SIZE(try_chR), "%p4chR"); 746 fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl"); 747 fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb"); 748 } 749 750 static void 751 errptr(struct kunit *kunittest) 752 { 753 test("-1234", "%pe", ERR_PTR(-1234)); 754 755 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */ 756 BUILD_BUG_ON(IS_ERR(PTR)); 757 test_hashed(kunittest, "%pe", PTR); 758 759 #ifdef CONFIG_SYMBOLIC_ERRNAME 760 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK)); 761 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN)); 762 BUILD_BUG_ON(EAGAIN != EWOULDBLOCK); 763 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK)); 764 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO)); 765 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO)); 766 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER)); 767 #endif 768 } 769 770 static int printf_suite_init(struct kunit_suite *suite) 771 { 772 total_tests = 0; 773 774 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); 775 if (!alloced_buffer) 776 return -ENOMEM; 777 test_buffer = alloced_buffer + PAD_SIZE; 778 779 return 0; 780 } 781 782 static void printf_suite_exit(struct kunit_suite *suite) 783 { 784 kfree(alloced_buffer); 785 786 kunit_info(suite, "ran %u tests\n", total_tests); 787 } 788 789 static struct kunit_case printf_test_cases[] = { 790 KUNIT_CASE(test_basic), 791 KUNIT_CASE(test_number), 792 KUNIT_CASE(test_string), 793 KUNIT_CASE(hash_pointer), 794 KUNIT_CASE(null_pointer), 795 KUNIT_CASE(error_pointer), 796 KUNIT_CASE(invalid_pointer), 797 KUNIT_CASE(symbol_ptr), 798 KUNIT_CASE(kernel_ptr), 799 KUNIT_CASE(struct_resource), 800 KUNIT_CASE(struct_range), 801 KUNIT_CASE(addr), 802 KUNIT_CASE(escaped_str), 803 KUNIT_CASE(hex_string), 804 KUNIT_CASE(mac), 805 KUNIT_CASE(ip4), 806 KUNIT_CASE(ip6), 807 KUNIT_CASE(uuid), 808 KUNIT_CASE(dentry), 809 KUNIT_CASE(struct_va_format), 810 KUNIT_CASE(time_and_date), 811 KUNIT_CASE(struct_clk), 812 KUNIT_CASE(bitmap), 813 KUNIT_CASE(netdev_features), 814 KUNIT_CASE(flags), 815 KUNIT_CASE(errptr), 816 KUNIT_CASE(fwnode_pointer), 817 KUNIT_CASE(fourcc_pointer), 818 {} 819 }; 820 821 static struct kunit_suite printf_test_suite = { 822 .name = "printf", 823 .suite_init = printf_suite_init, 824 .suite_exit = printf_suite_exit, 825 .test_cases = printf_test_cases, 826 }; 827 828 kunit_test_suite(printf_test_suite); 829 830 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>"); 831 MODULE_DESCRIPTION("Test cases for printf facility"); 832 MODULE_LICENSE("GPL"); 833