mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/
synced 2025-04-19 20:58:31 +09:00
printf: implicate test line in failure messages
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> Reviewed-by: Petr Mladek <pmladek@suse.com> Tested-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20250307-printf-kunit-convert-v6-3-4d85c361c241@gmail.com Signed-off-by: Kees Cook <kees@kernel.org>
This commit is contained in:
parent
81a03aa9b8
commit
034bee685f
@ -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,20 +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\n",
|
||||
bufsize, fmt, ret, elen);
|
||||
KUNIT_FAIL(kunittest,
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
|
||||
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\n",
|
||||
bufsize, fmt);
|
||||
KUNIT_FAIL(kunittest,
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n",
|
||||
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\n", fmt);
|
||||
KUNIT_FAIL(kunittest,
|
||||
"%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
|
||||
file, line, fmt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -74,34 +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\n",
|
||||
bufsize, fmt);
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
|
||||
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\n",
|
||||
bufsize, fmt);
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
|
||||
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\n",
|
||||
bufsize, fmt);
|
||||
KUNIT_FAIL(kunittest,
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
|
||||
file, line, bufsize, fmt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (memcmp(test_buffer, expect, written)) {
|
||||
KUNIT_FAIL(kunittest,
|
||||
"vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
|
||||
bufsize, fmt, test_buffer, written, expect);
|
||||
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
|
||||
file, line, bufsize, fmt, test_buffer, written, expect);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void __printf(4, 5)
|
||||
__test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, ...)
|
||||
static void __printf(6, 7)
|
||||
__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\"\n",
|
||||
elen, BUF_SIZE, fmt);
|
||||
"%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
|
||||
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'\n",
|
||||
fmt, p, expect);
|
||||
"%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user