[PATCH v5 3/3] printf: implicate test line in failure messages

Tamir Duberstein
2025-02-21T20:34:42Z

This improves the failure output by pointing to the failing line at the
top level of the test, e.g.:
# test_number: EXPECTATION FAILED at lib/printf_kunit.c:103
lib/printf_kunit.c:167: vsnprintf(buf, 256, "%#-12x", ...) wrote '0x1234abcd ', expected '0x1234abce '
# test_number: EXPECTATION FAILED at lib/printf_kunit.c:142
lib/printf_kunit.c:167: kvasprintf(..., "%#-12x", ...) returned '0x1234abcd ', expected '0x1234abce '

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
lib/tests/printf_kunit.c | 62 ++++++++++++++++++++++++++----------------------
1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index 013df6f6dd49..ed6e708ddb61 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -38,9 +38,9 @@ static unsigned int total_tests;
static char *test_buffer;
static char *alloced_buffer;

-static void __printf(5, 0)
-do_test(struct kunit *kunittest, int bufsize, const char *expect, int elen,
- const char *fmt, va_list ap)
+static void __printf(7, 0)
+do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect,
+ int elen, const char *fmt, va_list ap)
{
va_list aq;
int ret, written;
@@ -53,21 +53,24 @@ do_test(struct kunit *kunittest, int bufsize, const char *expect, int elen,
va_end(aq);

if (ret != elen) {
- KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d",
- bufsize, fmt, ret, elen);
+ KUNIT_FAIL(kunittest,
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d",
+ file, line, bufsize, fmt, ret, elen);
return;
}

if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
- KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) wrote before buffer",
- bufsize, fmt);
+ KUNIT_FAIL(kunittest,
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer",
+ file, line, bufsize, fmt);
return;
}

if (!bufsize) {
if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
- KUNIT_FAIL(kunittest, "vsnprintf(buf, 0, \"%s\", ...) wrote to buffer",
- fmt);
+ KUNIT_FAIL(kunittest,
+ "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer",
+ file, line, fmt);
}
return;
}
@@ -75,33 +78,36 @@ do_test(struct kunit *kunittest, int bufsize, const char *expect, int elen,
written = min(bufsize-1, elen);
if (test_buffer[written]) {
KUNIT_FAIL(kunittest,
- "vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer",
- bufsize, fmt);
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer",
+ file, line, bufsize, fmt);
return;
}

if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
KUNIT_FAIL(kunittest,
- "vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator",
- bufsize, fmt);
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator",
+ file, line, bufsize, fmt);
return;
}

if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
- KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer",
- bufsize, fmt);
+ KUNIT_FAIL(kunittest,
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer",
+ file, line, bufsize, fmt);
return;
}

if (memcmp(test_buffer, expect, written)) {
- KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'",
- bufsize, fmt, test_buffer, written, expect);
+ KUNIT_FAIL(kunittest,
+ "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'",
+ file, line, bufsize, fmt, test_buffer, written, expect);
return;
}
}

-static void __printf(4, 0)
-__test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, ...)
+static void __printf(6, 0)
+__test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen,
+ const char *fmt, ...)
{
va_list ap;
int rand;
@@ -109,8 +115,8 @@ __test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, .

if (elen >= BUF_SIZE) {
KUNIT_FAIL(kunittest,
- "error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"",
- elen, BUF_SIZE, fmt);
+ "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"",
+ file, line, elen, BUF_SIZE, fmt);
return;
}

@@ -122,19 +128,19 @@ __test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, .
* enough and 0), and then we also test that kvasprintf would
* be able to print it as expected.
*/
- do_test(kunittest, BUF_SIZE, expect, elen, fmt, ap);
+ do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap);
rand = get_random_u32_inclusive(1, elen + 1);
/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
- do_test(kunittest, rand, expect, elen, fmt, ap);
- do_test(kunittest, 0, expect, elen, fmt, ap);
+ do_test(kunittest, file, line, rand, expect, elen, fmt, ap);
+ do_test(kunittest, file, line, 0, expect, elen, fmt, ap);

p = kvasprintf(GFP_KERNEL, fmt, ap);
if (p) {
total_tests++;
if (memcmp(p, expect, elen+1)) {
KUNIT_FAIL(kunittest,
- "kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'",
- fmt, p, expect);
+ "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'",
+ file, line, fmt, p, expect);
}
kfree(p);
}
@@ -142,7 +148,7 @@ __test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, .
}

#define test(expect, fmt, ...) \
- __test(kunittest, expect, strlen(expect), fmt, ##__VA_ARGS__)
+ __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__)

static void
test_basic(struct kunit *kunittest)
@@ -153,7 +159,7 @@ test_basic(struct kunit *kunittest)
test("", &nul);
test("100%", "100%%");
test("xxx%yyy", "xxx%cyyy", '%');
- __test(kunittest, "xxx\0yyy", 7, "xxx%cyyy", '\0');
+ __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0');
}

static void

--
2.48.1