[RFC PATCH 4/7] lib/test_printf.c: add a few fmtcheck() test cases

From: Rasmus Villemoes
Date: Fri Oct 26 2018 - 19:24:22 EST


It should be trivial to add more test cases, once we figure out the
exact rules for being compatible or not. Perhaps we'll have to extend
the struct test with a flags element if we add flags that affect the
return value.

Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
lib/test_printf.c | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)

diff --git a/lib/test_printf.c b/lib/test_printf.c
index 53527ea822b5..5b6ba0e817d5 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -535,6 +535,47 @@ test_pointer(void)
flags();
}

+static void __init
+test_fmtcheck(void)
+{
+#ifdef CONFIG_FMTCHECK
+ struct test { const char *fmt; const char *tmpl; };
+ static const struct test compatible[] __initconst = {
+ {"", ""},
+ {"wlan%d", "%d"},
+ {"aa%llxbb", "cc%Lxdd%%"},
+ };
+ static const struct test incompatible[] __initconst = {
+ {"a %d b %lx", "%d %x"},
+ {"%llo", "%Lx"},
+ };
+ unsigned i;
+ const struct test *t;
+ const char *ret;
+
+ for (i = 0; i < ARRAY_SIZE(compatible); ++i) {
+ total_tests++;
+ t = &compatible[i];
+ ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT);
+ if (ret != t->fmt) {
+ failed_tests++;
+ pr_warn("'%s' and '%s' deemed incompatible by fmtcheck()",
+ t->fmt, t->tmpl);
+ }
+ }
+ for (i = 0; i < ARRAY_SIZE(incompatible); ++i) {
+ total_tests++;
+ t = &incompatible[i];
+ ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT);
+ if (ret != t->tmpl) {
+ failed_tests++;
+ pr_warn("'%s' and '%s' deemed compatible by fmtcheck()",
+ t->fmt, t->tmpl);
+ }
+ }
+#endif
+}
+
static int __init
test_printf_init(void)
{
@@ -548,6 +589,8 @@ test_printf_init(void)
test_string();
test_pointer();

+ test_fmtcheck();
+
kfree(alloced_buffer);

if (failed_tests == 0)
--
2.19.1.6.gbde171bbf5