2 * Test cases for printf facility.
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/printk.h>
11 #include <linux/random.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
15 #include <linux/socket.h>
21 #define PTR1 ((void*)0x01234567)
22 #define PTR2 ((void*)(long)(int)0xfedcba98)
24 #if BITS_PER_LONG == 64
25 #define PTR1_ZEROES "000000000"
26 #define PTR1_SPACES " "
27 #define PTR1_STR "1234567"
28 #define PTR2_STR "fffffffffedcba98"
31 #define PTR1_ZEROES "0"
32 #define PTR1_SPACES " "
33 #define PTR1_STR "1234567"
34 #define PTR2_STR "fedcba98"
37 #define PTR_WIDTH_STR stringify(PTR_WIDTH)
39 static unsigned total_tests __initdata
;
40 static unsigned failed_tests __initdata
;
41 static char *test_buffer __initdata
;
43 static int __printf(4, 0) __init
44 do_test(int bufsize
, const char *expect
, int elen
,
45 const char *fmt
, va_list ap
)
52 memset(test_buffer
, FILL_CHAR
, BUF_SIZE
);
54 ret
= vsnprintf(test_buffer
, bufsize
, fmt
, aq
);
58 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
59 bufsize
, fmt
, ret
, elen
);
64 if (memchr_inv(test_buffer
, FILL_CHAR
, BUF_SIZE
)) {
65 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
72 written
= min(bufsize
-1, elen
);
73 if (test_buffer
[written
]) {
74 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
79 if (memcmp(test_buffer
, expect
, written
)) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
81 bufsize
, fmt
, test_buffer
, written
, expect
);
87 static void __printf(3, 4) __init
88 __test(const char *expect
, int elen
, const char *fmt
, ...)
94 BUG_ON(elen
>= BUF_SIZE
);
99 * Every fmt+args is subjected to four tests: Three where we
100 * tell vsnprintf varying buffer sizes (plenty, not quite
101 * enough and 0), and then we also test that kvasprintf would
102 * be able to print it as expected.
104 failed_tests
+= do_test(BUF_SIZE
, expect
, elen
, fmt
, ap
);
105 rand
= 1 + prandom_u32_max(elen
+1);
106 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
107 failed_tests
+= do_test(rand
, expect
, elen
, fmt
, ap
);
108 failed_tests
+= do_test(0, expect
, elen
, fmt
, ap
);
110 p
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
112 if (memcmp(p
, expect
, elen
+1)) {
113 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
122 #define test(expect, fmt, ...) \
123 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
128 /* Work around annoying "warning: zero-length gnu_printf format string". */
132 test("100%", "100%%");
133 test("xxx%yyy", "xxx%cyyy", '%');
134 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
140 test("0x1234abcd ", "%#-12x", 0x1234abcd);
141 test(" 0x1234abcd", "%#12x", 0x1234abcd);
142 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
148 test("", "%s%.0s", "", "123");
149 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
150 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
152 * POSIX and C99 say that a missing precision should be
153 * treated as a precision of 0. However, the kernel's printf
154 * implementation treats this case as if the . wasn't
155 * present. Let's add a test case documenting the current
156 * behaviour; should anyone ever feel the need to follow the
157 * standards more closely, this can be revisited.
159 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
160 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
166 test(PTR1_ZEROES PTR1_STR
" " PTR2_STR
, "%p %p", PTR1
, PTR2
);
168 * The field width is overloaded for some %p extensions to
169 * pass another piece of information. For plain pointers, the
170 * behaviour is slightly odd: One cannot pass either the 0
171 * flag nor a precision to %p without gcc complaining, and if
172 * one explicitly gives a field width, the number is no longer
175 test("|" PTR1_STR PTR1_SPACES
" | " PTR1_SPACES PTR1_STR
"|",
176 "|%-*p|%*p|", PTR_WIDTH
+2, PTR1
, PTR_WIDTH
+2, PTR1
);
177 test("|" PTR2_STR
" | " PTR2_STR
"|",
178 "|%-*p|%*p|", PTR_WIDTH
+2, PTR2
, PTR_WIDTH
+2, PTR2
);
181 * Unrecognized %p extensions are treated as plain %p, but the
182 * alphanumeric suffix is ignored (that is, does not occur in
185 test("|"PTR1_ZEROES PTR1_STR
"|", "|%p0y|", PTR1
);
186 test("|"PTR2_STR
"|", "|%p0y|", PTR2
);
200 struct_resource(void)
217 const char buf
[3] = {0xc0, 0xff, 0xee};
219 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
220 "%3ph|%3phC|%3phD|%3phN", buf
, buf
, buf
, buf
);
221 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
222 "%*ph|%*phC|%*phD|%*phN", 3, buf
, 3, buf
, 3, buf
, 3, buf
);
228 const u8 addr
[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
230 test("2d:48:d6:fc:7a:05", "%pM", addr
);
231 test("05:7a:fc:d6:48:2d", "%pMR", addr
);
232 test("2d-48-d6-fc-7a-05", "%pMF", addr
);
233 test("2d48d6fc7a05", "%pm", addr
);
234 test("057afcd6482d", "%pmR", addr
);
240 struct sockaddr_in sa
;
242 sa
.sin_family
= AF_INET
;
243 sa
.sin_port
= cpu_to_be16(12345);
244 sa
.sin_addr
.s_addr
= cpu_to_be32(0x7f000001);
246 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa
.sin_addr
, &sa
.sin_addr
);
247 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa
, &sa
);
248 sa
.sin_addr
.s_addr
= cpu_to_be32(0x01020304);
249 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa
, &sa
);
267 const char uuid
[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
268 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
270 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid
);
271 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid
);
272 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid
);
273 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid
);
282 struct_va_format(void)
294 DECLARE_BITMAP(bits
, 20);
295 const int primes
[] = {2,3,5,7,11,13,17,19};
298 bitmap_zero(bits
, 20);
299 test("00000|00000", "%20pb|%*pb", bits
, 20, bits
);
300 test("|", "%20pbl|%*pbl", bits
, 20, bits
);
302 for (i
= 0; i
< ARRAY_SIZE(primes
); ++i
)
303 set_bit(primes
[i
], bits
);
304 test("a28ac|a28ac", "%20pb|%*pb", bits
, 20, bits
);
305 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits
, 20, bits
);
307 bitmap_fill(bits
, 20);
308 test("fffff|fffff", "%20pb|%*pb", bits
, 20, bits
);
309 test("0-19|0-19", "%20pbl|%*pbl", bits
, 20, bits
);
313 netdev_features(void)
338 test_printf_init(void)
340 test_buffer
= kmalloc(BUF_SIZE
, GFP_KERNEL
);
351 if (failed_tests
== 0)
352 pr_info("all %u tests passed\n", total_tests
);
354 pr_warn("failed %u out of %u tests\n", failed_tests
, total_tests
);
356 return failed_tests
? -EINVAL
: 0;
359 module_init(test_printf_init
);
361 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
362 MODULE_LICENSE("GPL");