|
8 | 8 | #include <kunit/test.h> |
9 | 9 | #include <kunit/test-bug.h> |
10 | 10 |
|
| 11 | +#include "string-stream.h" |
11 | 12 | #include "try-catch-impl.h" |
12 | 13 |
|
13 | 14 | struct kunit_try_catch_test_context { |
@@ -530,43 +531,80 @@ static struct kunit_suite kunit_resource_test_suite = { |
530 | 531 | .test_cases = kunit_resource_test_cases, |
531 | 532 | }; |
532 | 533 |
|
| 534 | +/* |
| 535 | + * Log tests call string_stream functions, which aren't exported. So only |
| 536 | + * build this code if this test is built-in. |
| 537 | + */ |
| 538 | +#if IS_BUILTIN(CONFIG_KUNIT_TEST) |
| 539 | + |
| 540 | +/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ |
| 541 | +static void kfree_wrapper(void *p) |
| 542 | +{ |
| 543 | + kfree(p); |
| 544 | +} |
| 545 | + |
533 | 546 | static void kunit_log_test(struct kunit *test) |
534 | 547 | { |
535 | 548 | struct kunit_suite suite; |
536 | | - |
537 | | - suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL); |
| 549 | +#ifdef CONFIG_KUNIT_DEBUGFS |
| 550 | + char *full_log; |
| 551 | +#endif |
| 552 | + suite.log = kunit_alloc_string_stream(test, GFP_KERNEL); |
538 | 553 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); |
| 554 | + string_stream_set_append_newlines(suite.log, true); |
539 | 555 |
|
540 | 556 | kunit_log(KERN_INFO, test, "put this in log."); |
541 | 557 | kunit_log(KERN_INFO, test, "this too."); |
542 | 558 | kunit_log(KERN_INFO, &suite, "add to suite log."); |
543 | 559 | kunit_log(KERN_INFO, &suite, "along with this."); |
544 | 560 |
|
545 | 561 | #ifdef CONFIG_KUNIT_DEBUGFS |
| 562 | + KUNIT_EXPECT_TRUE(test, test->log->append_newlines); |
| 563 | + |
| 564 | + full_log = string_stream_get_string(test->log); |
| 565 | + kunit_add_action(test, (kunit_action_t *)kfree, full_log); |
546 | 566 | KUNIT_EXPECT_NOT_ERR_OR_NULL(test, |
547 | | - strstr(test->log, "put this in log.")); |
| 567 | + strstr(full_log, "put this in log.")); |
548 | 568 | KUNIT_EXPECT_NOT_ERR_OR_NULL(test, |
549 | | - strstr(test->log, "this too.")); |
| 569 | + strstr(full_log, "this too.")); |
| 570 | + |
| 571 | + full_log = string_stream_get_string(suite.log); |
| 572 | + kunit_add_action(test, kfree_wrapper, full_log); |
550 | 573 | KUNIT_EXPECT_NOT_ERR_OR_NULL(test, |
551 | | - strstr(suite.log, "add to suite log.")); |
| 574 | + strstr(full_log, "add to suite log.")); |
552 | 575 | KUNIT_EXPECT_NOT_ERR_OR_NULL(test, |
553 | | - strstr(suite.log, "along with this.")); |
| 576 | + strstr(full_log, "along with this.")); |
554 | 577 | #else |
555 | 578 | KUNIT_EXPECT_NULL(test, test->log); |
556 | 579 | #endif |
557 | 580 | } |
558 | 581 |
|
559 | 582 | static void kunit_log_newline_test(struct kunit *test) |
560 | 583 | { |
| 584 | + char *full_log; |
| 585 | + |
561 | 586 | kunit_info(test, "Add newline\n"); |
562 | 587 | if (test->log) { |
563 | | - KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"), |
564 | | - "Missing log line, full log:\n%s", test->log); |
565 | | - KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n")); |
| 588 | + full_log = string_stream_get_string(test->log); |
| 589 | + kunit_add_action(test, kfree_wrapper, full_log); |
| 590 | + KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"), |
| 591 | + "Missing log line, full log:\n%s", full_log); |
| 592 | + KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n")); |
566 | 593 | } else { |
567 | 594 | kunit_skip(test, "only useful when debugfs is enabled"); |
568 | 595 | } |
569 | 596 | } |
| 597 | +#else |
| 598 | +static void kunit_log_test(struct kunit *test) |
| 599 | +{ |
| 600 | + kunit_skip(test, "Log tests only run when built-in"); |
| 601 | +} |
| 602 | + |
| 603 | +static void kunit_log_newline_test(struct kunit *test) |
| 604 | +{ |
| 605 | + kunit_skip(test, "Log tests only run when built-in"); |
| 606 | +} |
| 607 | +#endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */ |
570 | 608 |
|
571 | 609 | static struct kunit_case kunit_log_test_cases[] = { |
572 | 610 | KUNIT_CASE(kunit_log_test), |
|
0 commit comments