lib/test_printf.c: account for kvasprintf tests
[cascardo/linux.git] / lib / test_printf.c
index c5a666a..3e21170 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/in.h>
 
 #define BUF_SIZE 256
+#define PAD_SIZE 16
 #define FILL_CHAR '$'
 
 #define PTR1 ((void*)0x01234567)
@@ -39,6 +40,7 @@
 static unsigned total_tests __initdata;
 static unsigned failed_tests __initdata;
 static char *test_buffer __initdata;
+static char *alloced_buffer __initdata;
 
 static int __printf(4, 0) __init
 do_test(int bufsize, const char *expect, int elen,
@@ -49,7 +51,7 @@ do_test(int bufsize, const char *expect, int elen,
 
        total_tests++;
 
-       memset(test_buffer, FILL_CHAR, BUF_SIZE);
+       memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
        va_copy(aq, ap);
        ret = vsnprintf(test_buffer, bufsize, fmt, aq);
        va_end(aq);
@@ -60,8 +62,13 @@ do_test(int bufsize, const char *expect, int elen,
                return 1;
        }
 
+       if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
+               pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
+               return 1;
+       }
+
        if (!bufsize) {
-               if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE)) {
+               if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
                        pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
                                fmt);
                        return 1;
@@ -76,6 +83,12 @@ do_test(int bufsize, const char *expect, int elen,
                return 1;
        }
 
+       if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
+               pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
+                       bufsize, fmt);
+               return 1;
+       }
+
        if (memcmp(test_buffer, expect, written)) {
                pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
                        bufsize, fmt, test_buffer, written, expect);
@@ -91,7 +104,12 @@ __test(const char *expect, int elen, const char *fmt, ...)
        int rand;
        char *p;
 
-       BUG_ON(elen >= BUF_SIZE);
+       if (elen >= BUF_SIZE) {
+               pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
+                      elen, fmt);
+               failed_tests++;
+               return;
+       }
 
        va_start(ap, fmt);
 
@@ -109,6 +127,7 @@ __test(const char *expect, int elen, const char *fmt, ...)
 
        p = kvasprintf(GFP_KERNEL, fmt, ap);
        if (p) {
+               total_tests++;
                if (memcmp(p, expect, elen+1)) {
                        pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
                                fmt, p, expect);
@@ -140,6 +159,30 @@ test_number(void)
        test("0x1234abcd  ", "%#-12x", 0x1234abcd);
        test("  0x1234abcd", "%#12x", 0x1234abcd);
        test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
+       test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
+       test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
+       test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
+       /*
+        * POSIX/C99: »The result of converting zero with an explicit
+        * precision of zero shall be no characters.« Hence the output
+        * from the below test should really be "00|0||| ". However,
+        * the kernel's printf also produces a single 0 in that
+        * case. This test case simply documents the current
+        * behaviour.
+        */
+       test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
+#ifndef __CHAR_UNSIGNED__
+       {
+               /*
+                * Passing a 'char' to a %02x specifier doesn't do
+                * what was presumably the intention when char is
+                * signed and the value is negative. One must either &
+                * with 0xff or cast to u8.
+                */
+               char val = -16;
+               test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
+       }
+#endif
 }
 
 static void __init
@@ -148,14 +191,23 @@ test_string(void)
        test("", "%s%.0s", "", "123");
        test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
        test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
+       test("1234      ", "%-10.4s", "123456");
+       test("      1234", "%10.4s", "123456");
        /*
-        * POSIX and C99 say that a missing precision should be
-        * treated as a precision of 0. However, the kernel's printf
-        * implementation treats this case as if the . wasn't
-        * present. Let's add a test case documenting the current
-        * behaviour; should anyone ever feel the need to follow the
-        * standards more closely, this can be revisited.
+        * POSIX and C99 say that a negative precision (which is only
+        * possible to pass via a * argument) should be treated as if
+        * the precision wasn't present, and that if the precision is
+        * omitted (as in %.s), the precision should be taken to be
+        * 0. However, the kernel's printf behave exactly opposite,
+        * treating a negative precision as 0 and treating an omitted
+        * precision specifier as if no precision was given.
+        *
+        * These test cases document the current behaviour; should
+        * anyone ever feel the need to follow the standards more
+        * closely, this can be revisited.
         */
+       test("    ", "%4.*s", -5, "123456");
+       test("123456", "%.s", "123456");
        test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
        test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
 }
@@ -337,16 +389,17 @@ test_pointer(void)
 static int __init
 test_printf_init(void)
 {
-       test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
-       if (!test_buffer)
+       alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
+       if (!alloced_buffer)
                return -ENOMEM;
+       test_buffer = alloced_buffer + PAD_SIZE;
 
        test_basic();
        test_number();
        test_string();
        test_pointer();
 
-       kfree(test_buffer);
+       kfree(alloced_buffer);
 
        if (failed_tests == 0)
                pr_info("all %u tests passed\n", total_tests);