Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kunit: fix bug of extra newline characters in debugfs logs

Fix bug of the extra newline characters in debugfs logs. When a
line is added to debugfs with a newline character at the end,
an extra line appears in the debugfs log.

This is due to a discrepancy between how the lines are printed and how they
are added to the logs. Remove this discrepancy by checking if a newline
character is present before adding a newline character. This should closely
match the printk behavior.

Add kunit_log_newline_test to provide test coverage for this issue. (Also,
move kunit_log_test above suite definition to remove the unnecessary
declaration prior to the suite definition)

As an example, say we add these two lines to the log:

kunit_log(..., "KTAP version 1\n");
kunit_log(..., "1..1");

The debugfs log before this fix:

KTAP version 1

1..1

The debugfs log after this fix:

KTAP version 1
1..1

Signed-off-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Rae Moar and committed by
Shuah Khan
2c6a96da f9a301c3

+42 -13
+1 -1
include/kunit/test.h
··· 420 420 #define kunit_log(lvl, test_or_suite, fmt, ...) \ 421 421 do { \ 422 422 printk(lvl fmt, ##__VA_ARGS__); \ 423 - kunit_log_append((test_or_suite)->log, fmt "\n", \ 423 + kunit_log_append((test_or_suite)->log, fmt, \ 424 424 ##__VA_ARGS__); \ 425 425 } while (0) 426 426
+23 -12
lib/kunit/kunit-test.c
··· 443 443 .test_cases = kunit_resource_test_cases, 444 444 }; 445 445 446 - static void kunit_log_test(struct kunit *test); 447 - 448 - static struct kunit_case kunit_log_test_cases[] = { 449 - KUNIT_CASE(kunit_log_test), 450 - {} 451 - }; 452 - 453 - static struct kunit_suite kunit_log_test_suite = { 454 - .name = "kunit-log-test", 455 - .test_cases = kunit_log_test_cases, 456 - }; 457 - 458 446 static void kunit_log_test(struct kunit *test) 459 447 { 460 448 struct kunit_suite suite; ··· 468 480 KUNIT_EXPECT_NULL(test, test->log); 469 481 #endif 470 482 } 483 + 484 + static void kunit_log_newline_test(struct kunit *test) 485 + { 486 + kunit_info(test, "Add newline\n"); 487 + if (test->log) { 488 + KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"), 489 + "Missing log line, full log:\n%s", test->log); 490 + KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n")); 491 + } else { 492 + kunit_skip(test, "only useful when debugfs is enabled"); 493 + } 494 + } 495 + 496 + static struct kunit_case kunit_log_test_cases[] = { 497 + KUNIT_CASE(kunit_log_test), 498 + KUNIT_CASE(kunit_log_newline_test), 499 + {} 500 + }; 501 + 502 + static struct kunit_suite kunit_log_test_suite = { 503 + .name = "kunit-log-test", 504 + .test_cases = kunit_log_test_cases, 505 + }; 471 506 472 507 static void kunit_status_set_failure_test(struct kunit *test) 473 508 {
+18
lib/kunit/test.c
··· 108 108 stats.total); 109 109 } 110 110 111 + /** 112 + * kunit_log_newline() - Add newline to the end of log if one is not 113 + * already present. 114 + * @log: The log to add the newline to. 115 + */ 116 + static void kunit_log_newline(char *log) 117 + { 118 + int log_len, len_left; 119 + 120 + log_len = strlen(log); 121 + len_left = KUNIT_LOG_SIZE - log_len - 1; 122 + 123 + if (log_len > 0 && log[log_len - 1] != '\n') 124 + strncat(log, "\n", len_left); 125 + } 126 + 111 127 /* 112 128 * Append formatted message to log, size of which is limited to 113 129 * KUNIT_LOG_SIZE bytes (including null terminating byte). ··· 151 135 vsnprintf(log + log_len, min(len, len_left), fmt, args); 152 136 va_end(args); 153 137 138 + /* Add newline to end of log if not already present. */ 139 + kunit_log_newline(log); 154 140 } 155 141 EXPORT_SYMBOL_GPL(kunit_log_append); 156 142