// SPDX-License-Identifier: GPL-2.0-only
/*
* Test cases for printf facility.
*/
#include <kunit/test.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/random.h>
#include <linux/rtc.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
#include <linux/bitmap.h>
#include <linux/dcache.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/property.h>
#define BUF_SIZE 256
#define PAD_SIZE 16
#define FILL_CHAR '$'
#define NOWARN(option, comment, block) \
__diag_push(); \
__diag_ignore_all(#option, comment); \
block \
__diag_pop();
static unsigned int total_tests;
static char *test_buffer;
static char *alloced_buffer;
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;
total_tests++;
memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
va_copy(aq, ap);
ret = vsnprintf(test_buffer, bufsize, fmt, aq);
va_end(aq);
if (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,
"%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,
"%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
file, line, fmt);
}
return;
}
written = min(bufsize-1, elen);
if (test_buffer[written]) {
KUNIT_FAIL(kunittest,
"%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,
"%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,
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n",
file, line, bufsize, fmt);
return;
}
if (memcmp(test_buffer, expect, written)) {
KUNIT_FAIL(kunittest,
"%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
file, line, bufsize, fmt, test_buffer, written, expect);
return;
}
}
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;
char *p;
if (elen >= BUF_SIZE) {
KUNIT_FAIL(kunittest,
"%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n",
file, line, elen, BUF_SIZE, fmt);
return;
}
va_start(ap, fmt);
/*
* Every fmt+args is subjected to four tests: Three where we
* tell vsnprintf varying buffer sizes (plenty, not quite
* enough and 0), and then we also test that kvasprintf would
* be able to print it as expected.
*/
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, 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,
"%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
file, line, fmt, p, expect);
}
kfree(p);
}
va_end(ap);
}
#define test(expect, fmt, ...) \
__test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__)
static void
test_basic(struct kunit *kunittest)
{
/* Work around annoying "warning: zero-length gnu_printf format string". */
char nul = '\0';
test("", &nul);
test("100%", "100%%");
test("xxx%yyy", "xxx%cyyy", '%');
__test(kunittest, __FILE__, __LINE__, "xxx