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>
22 #define PTR1 ((void*)0x01234567)
23 #define PTR2 ((void*)(long)(int)0xfedcba98)
25 #if BITS_PER_LONG == 64
26 #define PTR1_ZEROES "000000000"
27 #define PTR1_SPACES " "
28 #define PTR1_STR "1234567"
29 #define PTR2_STR "fffffffffedcba98"
32 #define PTR1_ZEROES "0"
33 #define PTR1_SPACES " "
34 #define PTR1_STR "1234567"
35 #define PTR2_STR "fedcba98"
38 #define PTR_WIDTH_STR stringify(PTR_WIDTH)
40 static unsigned total_tests __initdata
;
41 static unsigned failed_tests __initdata
;
42 static char *test_buffer __initdata
;
43 static char *alloced_buffer __initdata
;
45 static int __printf(4, 0) __init
46 do_test(int bufsize
, const char *expect
, int elen
,
47 const char *fmt
, va_list ap
)
54 memset(alloced_buffer
, FILL_CHAR
, BUF_SIZE
+ 2*PAD_SIZE
);
56 ret
= vsnprintf(test_buffer
, bufsize
, fmt
, aq
);
60 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
61 bufsize
, fmt
, ret
, elen
);
65 if (memchr_inv(alloced_buffer
, FILL_CHAR
, PAD_SIZE
)) {
66 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize
, fmt
);
71 if (memchr_inv(test_buffer
, FILL_CHAR
, BUF_SIZE
+ PAD_SIZE
)) {
72 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
79 written
= min(bufsize
-1, elen
);
80 if (test_buffer
[written
]) {
81 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
86 if (memchr_inv(test_buffer
+ written
+ 1, FILL_CHAR
, BUF_SIZE
+ PAD_SIZE
- (written
+ 1))) {
87 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
92 if (memcmp(test_buffer
, expect
, written
)) {
93 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
94 bufsize
, fmt
, test_buffer
, written
, expect
);
100 static void __printf(3, 4) __init
101 __test(const char *expect
, int elen
, const char *fmt
, ...)
107 if (elen
>= BUF_SIZE
) {
108 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
117 * Every fmt+args is subjected to four tests: Three where we
118 * tell vsnprintf varying buffer sizes (plenty, not quite
119 * enough and 0), and then we also test that kvasprintf would
120 * be able to print it as expected.
122 failed_tests
+= do_test(BUF_SIZE
, expect
, elen
, fmt
, ap
);
123 rand
= 1 + prandom_u32_max(elen
+1);
124 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
125 failed_tests
+= do_test(rand
, expect
, elen
, fmt
, ap
);
126 failed_tests
+= do_test(0, expect
, elen
, fmt
, ap
);
128 p
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
130 if (memcmp(p
, expect
, elen
+1)) {
131 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
140 #define test(expect, fmt, ...) \
141 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
146 /* Work around annoying "warning: zero-length gnu_printf format string". */
150 test("100%", "100%%");
151 test("xxx%yyy", "xxx%cyyy", '%');
152 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
158 test("0x1234abcd ", "%#-12x", 0x1234abcd);
159 test(" 0x1234abcd", "%#12x", 0x1234abcd);
160 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
166 test("", "%s%.0s", "", "123");
167 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
168 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
170 * POSIX and C99 say that a missing precision should be
171 * treated as a precision of 0. However, the kernel's printf
172 * implementation treats this case as if the . wasn't
173 * present. Let's add a test case documenting the current
174 * behaviour; should anyone ever feel the need to follow the
175 * standards more closely, this can be revisited.
177 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
178 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
184 test(PTR1_ZEROES PTR1_STR
" " PTR2_STR
, "%p %p", PTR1
, PTR2
);
186 * The field width is overloaded for some %p extensions to
187 * pass another piece of information. For plain pointers, the
188 * behaviour is slightly odd: One cannot pass either the 0
189 * flag nor a precision to %p without gcc complaining, and if
190 * one explicitly gives a field width, the number is no longer
193 test("|" PTR1_STR PTR1_SPACES
" | " PTR1_SPACES PTR1_STR
"|",
194 "|%-*p|%*p|", PTR_WIDTH
+2, PTR1
, PTR_WIDTH
+2, PTR1
);
195 test("|" PTR2_STR
" | " PTR2_STR
"|",
196 "|%-*p|%*p|", PTR_WIDTH
+2, PTR2
, PTR_WIDTH
+2, PTR2
);
199 * Unrecognized %p extensions are treated as plain %p, but the
200 * alphanumeric suffix is ignored (that is, does not occur in
203 test("|"PTR1_ZEROES PTR1_STR
"|", "|%p0y|", PTR1
);
204 test("|"PTR2_STR
"|", "|%p0y|", PTR2
);
218 struct_resource(void)
235 const char buf
[3] = {0xc0, 0xff, 0xee};
237 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
238 "%3ph|%3phC|%3phD|%3phN", buf
, buf
, buf
, buf
);
239 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
240 "%*ph|%*phC|%*phD|%*phN", 3, buf
, 3, buf
, 3, buf
, 3, buf
);
246 const u8 addr
[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
248 test("2d:48:d6:fc:7a:05", "%pM", addr
);
249 test("05:7a:fc:d6:48:2d", "%pMR", addr
);
250 test("2d-48-d6-fc-7a-05", "%pMF", addr
);
251 test("2d48d6fc7a05", "%pm", addr
);
252 test("057afcd6482d", "%pmR", addr
);
258 struct sockaddr_in sa
;
260 sa
.sin_family
= AF_INET
;
261 sa
.sin_port
= cpu_to_be16(12345);
262 sa
.sin_addr
.s_addr
= cpu_to_be32(0x7f000001);
264 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa
.sin_addr
, &sa
.sin_addr
);
265 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa
, &sa
);
266 sa
.sin_addr
.s_addr
= cpu_to_be32(0x01020304);
267 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa
, &sa
);
285 const char uuid
[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
286 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
288 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid
);
289 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid
);
290 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid
);
291 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid
);
300 struct_va_format(void)
312 DECLARE_BITMAP(bits
, 20);
313 const int primes
[] = {2,3,5,7,11,13,17,19};
316 bitmap_zero(bits
, 20);
317 test("00000|00000", "%20pb|%*pb", bits
, 20, bits
);
318 test("|", "%20pbl|%*pbl", bits
, 20, bits
);
320 for (i
= 0; i
< ARRAY_SIZE(primes
); ++i
)
321 set_bit(primes
[i
], bits
);
322 test("a28ac|a28ac", "%20pb|%*pb", bits
, 20, bits
);
323 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits
, 20, bits
);
325 bitmap_fill(bits
, 20);
326 test("fffff|fffff", "%20pb|%*pb", bits
, 20, bits
);
327 test("0-19|0-19", "%20pbl|%*pbl", bits
, 20, bits
);
331 netdev_features(void)
356 test_printf_init(void)
358 alloced_buffer
= kmalloc(BUF_SIZE
+ 2*PAD_SIZE
, GFP_KERNEL
);
361 test_buffer
= alloced_buffer
+ PAD_SIZE
;
368 kfree(alloced_buffer
);
370 if (failed_tests
== 0)
371 pr_info("all %u tests passed\n", total_tests
);
373 pr_warn("failed %u out of %u tests\n", failed_tests
, total_tests
);
375 return failed_tests
? -EINVAL
: 0;
378 module_init(test_printf_init
);
380 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
381 MODULE_LICENSE("GPL");