Skip to content

Commit 034bee6

Browse files
tamirdkees
authored andcommitted
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 <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Tested-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 81a03aa commit 034bee6

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

lib/tests/printf_kunit.c

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ static unsigned int total_tests;
3838
static char *test_buffer;
3939
static char *alloced_buffer;
4040

41-
static void __printf(5, 0)
42-
do_test(struct kunit *kunittest, int bufsize, const char *expect, int elen,
43-
const char *fmt, va_list ap)
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)
4444
{
4545
va_list aq;
4646
int ret, written;
@@ -53,64 +53,70 @@ do_test(struct kunit *kunittest, int bufsize, const char *expect, int elen,
5353
va_end(aq);
5454

5555
if (ret != elen) {
56-
KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
57-
bufsize, fmt, 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);
5859
return;
5960
}
6061

6162
if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
62-
KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n",
63-
bufsize, fmt);
63+
KUNIT_FAIL(kunittest,
64+
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n",
65+
file, line, bufsize, fmt);
6466
return;
6567
}
6668

6769
if (!bufsize) {
6870
if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
69-
KUNIT_FAIL(kunittest, "vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", fmt);
71+
KUNIT_FAIL(kunittest,
72+
"%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
73+
file, line, fmt);
7074
}
7175
return;
7276
}
7377

7478
written = min(bufsize-1, elen);
7579
if (test_buffer[written]) {
7680
KUNIT_FAIL(kunittest,
77-
"vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
78-
bufsize, fmt);
81+
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
82+
file, line, bufsize, fmt);
7983
return;
8084
}
8185

8286
if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
8387
KUNIT_FAIL(kunittest,
84-
"vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
85-
bufsize, fmt);
88+
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
89+
file, line, bufsize, fmt);
8690
return;
8791
}
8892

8993
if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
90-
KUNIT_FAIL(kunittest, "vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
91-
bufsize, fmt);
94+
KUNIT_FAIL(kunittest,
95+
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
96+
file, line, bufsize, fmt);
9297
return;
9398
}
9499

95100
if (memcmp(test_buffer, expect, written)) {
96101
KUNIT_FAIL(kunittest,
97-
"vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
98-
bufsize, fmt, test_buffer, written, expect);
102+
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
103+
file, line, bufsize, fmt, test_buffer, written, expect);
99104
return;
100105
}
101106
}
102107

103-
static void __printf(4, 5)
104-
__test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, ...)
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, ...)
105111
{
106112
va_list ap;
107113
int rand;
108114
char *p;
109115

110116
if (elen >= BUF_SIZE) {
111117
KUNIT_FAIL(kunittest,
112-
"error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
113-
elen, BUF_SIZE, fmt);
118+
"%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
119+
file, line, elen, BUF_SIZE, fmt);
114120
return;
115121
}
116122

@@ -122,27 +128,27 @@ __test(struct kunit *kunittest, const char *expect, int elen, const char *fmt, .
122128
* enough and 0), and then we also test that kvasprintf would
123129
* be able to print it as expected.
124130
*/
125-
do_test(kunittest, BUF_SIZE, expect, elen, fmt, ap);
131+
do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap);
126132
rand = get_random_u32_inclusive(1, elen + 1);
127133
/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
128-
do_test(kunittest, rand, expect, elen, fmt, ap);
129-
do_test(kunittest, 0, expect, elen, fmt, ap);
134+
do_test(kunittest, file, line, rand, expect, elen, fmt, ap);
135+
do_test(kunittest, file, line, 0, expect, elen, fmt, ap);
130136

131137
p = kvasprintf(GFP_KERNEL, fmt, ap);
132138
if (p) {
133139
total_tests++;
134140
if (memcmp(p, expect, elen+1)) {
135141
KUNIT_FAIL(kunittest,
136-
"kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
137-
fmt, p, expect);
142+
"%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
143+
file, line, fmt, p, expect);
138144
}
139145
kfree(p);
140146
}
141147
va_end(ap);
142148
}
143149

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

147153
static void
148154
test_basic(struct kunit *kunittest)
@@ -153,7 +159,7 @@ test_basic(struct kunit *kunittest)
153159
test("", &nul);
154160
test("100%", "100%%");
155161
test("xxx%yyy", "xxx%cyyy", '%');
156-
__test(kunittest, "xxx\0yyy", 7, "xxx%cyyy", '\0');
162+
__test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0');
157163
}
158164

159165
static void

0 commit comments

Comments
 (0)