blob: 4f6ae60433bc5d1e1d6df9b7c8d5d359512ad82a [file] [log] [blame]
Rasmus Villemoes707cc722015-11-06 16:30:29 -08001/*
2 * Test cases for printf facility.
3 */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
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>
14
Rasmus Villemoes857cca42016-01-15 16:59:06 -080015#include <linux/bitmap.h>
Rasmus Villemoes251c7232016-01-15 16:59:09 -080016#include <linux/dcache.h>
Rasmus Villemoes707cc722015-11-06 16:30:29 -080017#include <linux/socket.h>
18#include <linux/in.h>
19
20#define BUF_SIZE 256
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080021#define PAD_SIZE 16
Rasmus Villemoes707cc722015-11-06 16:30:29 -080022#define FILL_CHAR '$'
23
24#define PTR1 ((void*)0x01234567)
25#define PTR2 ((void*)(long)(int)0xfedcba98)
26
27#if BITS_PER_LONG == 64
28#define PTR1_ZEROES "000000000"
29#define PTR1_SPACES " "
30#define PTR1_STR "1234567"
31#define PTR2_STR "fffffffffedcba98"
32#define PTR_WIDTH 16
33#else
34#define PTR1_ZEROES "0"
35#define PTR1_SPACES " "
36#define PTR1_STR "1234567"
37#define PTR2_STR "fedcba98"
38#define PTR_WIDTH 8
39#endif
40#define PTR_WIDTH_STR stringify(PTR_WIDTH)
41
42static unsigned total_tests __initdata;
43static unsigned failed_tests __initdata;
44static char *test_buffer __initdata;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080045static char *alloced_buffer __initdata;
Rasmus Villemoes707cc722015-11-06 16:30:29 -080046
47static int __printf(4, 0) __init
48do_test(int bufsize, const char *expect, int elen,
49 const char *fmt, va_list ap)
50{
51 va_list aq;
52 int ret, written;
53
54 total_tests++;
55
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080056 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
Rasmus Villemoes707cc722015-11-06 16:30:29 -080057 va_copy(aq, ap);
58 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
59 va_end(aq);
60
61 if (ret != elen) {
62 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
63 bufsize, fmt, ret, elen);
64 return 1;
65 }
66
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080067 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
68 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
69 return 1;
70 }
71
Rasmus Villemoes707cc722015-11-06 16:30:29 -080072 if (!bufsize) {
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080073 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
Rasmus Villemoes707cc722015-11-06 16:30:29 -080074 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
75 fmt);
76 return 1;
77 }
78 return 0;
79 }
80
81 written = min(bufsize-1, elen);
82 if (test_buffer[written]) {
83 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
84 bufsize, fmt);
85 return 1;
86 }
87
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080088 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
89 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
90 bufsize, fmt);
91 return 1;
92 }
93
Rasmus Villemoes707cc722015-11-06 16:30:29 -080094 if (memcmp(test_buffer, expect, written)) {
95 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
96 bufsize, fmt, test_buffer, written, expect);
97 return 1;
98 }
99 return 0;
100}
101
102static void __printf(3, 4) __init
103__test(const char *expect, int elen, const char *fmt, ...)
104{
105 va_list ap;
106 int rand;
107 char *p;
108
Rasmus Villemoesfd0515d2016-01-15 16:58:50 -0800109 if (elen >= BUF_SIZE) {
110 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
111 elen, fmt);
112 failed_tests++;
113 return;
114 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800115
116 va_start(ap, fmt);
117
118 /*
119 * Every fmt+args is subjected to four tests: Three where we
120 * tell vsnprintf varying buffer sizes (plenty, not quite
121 * enough and 0), and then we also test that kvasprintf would
122 * be able to print it as expected.
123 */
124 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
125 rand = 1 + prandom_u32_max(elen+1);
126 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
127 failed_tests += do_test(rand, expect, elen, fmt, ap);
128 failed_tests += do_test(0, expect, elen, fmt, ap);
129
130 p = kvasprintf(GFP_KERNEL, fmt, ap);
131 if (p) {
Rasmus Villemoesb79a7db2016-01-15 16:59:02 -0800132 total_tests++;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800133 if (memcmp(p, expect, elen+1)) {
134 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
135 fmt, p, expect);
136 failed_tests++;
137 }
138 kfree(p);
139 }
140 va_end(ap);
141}
142
143#define test(expect, fmt, ...) \
144 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
145
146static void __init
147test_basic(void)
148{
149 /* Work around annoying "warning: zero-length gnu_printf format string". */
150 char nul = '\0';
151
152 test("", &nul);
153 test("100%", "100%%");
154 test("xxx%yyy", "xxx%cyyy", '%');
155 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
156}
157
158static void __init
159test_number(void)
160{
161 test("0x1234abcd ", "%#-12x", 0x1234abcd);
162 test(" 0x1234abcd", "%#12x", 0x1234abcd);
163 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
Rasmus Villemoes1ca8e8e2016-01-15 16:58:59 -0800164 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
165 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
166 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
167 /*
168 * POSIX/C99: »The result of converting zero with an explicit
169 * precision of zero shall be no characters.« Hence the output
170 * from the below test should really be "00|0||| ". However,
171 * the kernel's printf also produces a single 0 in that
172 * case. This test case simply documents the current
173 * behaviour.
174 */
175 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
176#ifndef __CHAR_UNSIGNED__
177 {
178 /*
179 * Passing a 'char' to a %02x specifier doesn't do
180 * what was presumably the intention when char is
181 * signed and the value is negative. One must either &
182 * with 0xff or cast to u8.
183 */
184 char val = -16;
185 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
186 }
187#endif
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800188}
189
190static void __init
191test_string(void)
192{
193 test("", "%s%.0s", "", "123");
194 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
195 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800196 test("1234 ", "%-10.4s", "123456");
197 test(" 1234", "%10.4s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800198 /*
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800199 * POSIX and C99 say that a negative precision (which is only
200 * possible to pass via a * argument) should be treated as if
201 * the precision wasn't present, and that if the precision is
202 * omitted (as in %.s), the precision should be taken to be
203 * 0. However, the kernel's printf behave exactly opposite,
204 * treating a negative precision as 0 and treating an omitted
205 * precision specifier as if no precision was given.
206 *
207 * These test cases document the current behaviour; should
208 * anyone ever feel the need to follow the standards more
209 * closely, this can be revisited.
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800210 */
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800211 test(" ", "%4.*s", -5, "123456");
212 test("123456", "%.s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800213 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
214 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
215}
216
217static void __init
218plain(void)
219{
220 test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
221 /*
222 * The field width is overloaded for some %p extensions to
223 * pass another piece of information. For plain pointers, the
224 * behaviour is slightly odd: One cannot pass either the 0
225 * flag nor a precision to %p without gcc complaining, and if
226 * one explicitly gives a field width, the number is no longer
227 * zero-padded.
228 */
229 test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
230 "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
231 test("|" PTR2_STR " | " PTR2_STR "|",
232 "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
233
234 /*
235 * Unrecognized %p extensions are treated as plain %p, but the
236 * alphanumeric suffix is ignored (that is, does not occur in
237 * the output.)
238 */
239 test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
240 test("|"PTR2_STR"|", "|%p0y|", PTR2);
241}
242
243static void __init
244symbol_ptr(void)
245{
246}
247
248static void __init
249kernel_ptr(void)
250{
251}
252
253static void __init
254struct_resource(void)
255{
256}
257
258static void __init
259addr(void)
260{
261}
262
263static void __init
264escaped_str(void)
265{
266}
267
268static void __init
269hex_string(void)
270{
271 const char buf[3] = {0xc0, 0xff, 0xee};
272
273 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
274 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
275 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
276 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
277}
278
279static void __init
280mac(void)
281{
282 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
283
284 test("2d:48:d6:fc:7a:05", "%pM", addr);
285 test("05:7a:fc:d6:48:2d", "%pMR", addr);
286 test("2d-48-d6-fc-7a-05", "%pMF", addr);
287 test("2d48d6fc7a05", "%pm", addr);
288 test("057afcd6482d", "%pmR", addr);
289}
290
291static void __init
292ip4(void)
293{
294 struct sockaddr_in sa;
295
296 sa.sin_family = AF_INET;
297 sa.sin_port = cpu_to_be16(12345);
298 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
299
300 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
301 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
302 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
303 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
304}
305
306static void __init
307ip6(void)
308{
309}
310
311static void __init
312ip(void)
313{
314 ip4();
315 ip6();
316}
317
318static void __init
319uuid(void)
320{
321 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
322 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
323
324 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
325 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
326 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
327 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
328}
329
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800330static struct dentry test_dentry[4] __initdata = {
331 { .d_parent = &test_dentry[0],
332 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
333 .d_iname = "foo" },
334 { .d_parent = &test_dentry[0],
335 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
336 .d_iname = "bravo" },
337 { .d_parent = &test_dentry[1],
338 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
339 .d_iname = "alfa" },
340 { .d_parent = &test_dentry[2],
341 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
342 .d_iname = "romeo" },
343};
344
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800345static void __init
346dentry(void)
347{
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800348 test("foo", "%pd", &test_dentry[0]);
349 test("foo", "%pd2", &test_dentry[0]);
350
351 test("romeo", "%pd", &test_dentry[3]);
352 test("alfa/romeo", "%pd2", &test_dentry[3]);
353 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
354 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
355 test("/bravo/alfa", "%pd4", &test_dentry[2]);
356
357 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
358 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800359}
360
361static void __init
362struct_va_format(void)
363{
364}
365
366static void __init
367struct_clk(void)
368{
369}
370
371static void __init
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800372large_bitmap(void)
373{
374 const int nbits = 1 << 16;
375 unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
376 if (!bits)
377 return;
378
379 bitmap_set(bits, 1, 20);
380 bitmap_set(bits, 60000, 15);
381 test("1-20,60000-60014", "%*pbl", nbits, bits);
382 kfree(bits);
383}
384
385static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800386bitmap(void)
387{
388 DECLARE_BITMAP(bits, 20);
389 const int primes[] = {2,3,5,7,11,13,17,19};
390 int i;
391
392 bitmap_zero(bits, 20);
393 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
394 test("|", "%20pbl|%*pbl", bits, 20, bits);
395
396 for (i = 0; i < ARRAY_SIZE(primes); ++i)
397 set_bit(primes[i], bits);
398 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
399 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
400
401 bitmap_fill(bits, 20);
402 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
403 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800404
405 large_bitmap();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800406}
407
408static void __init
409netdev_features(void)
410{
411}
412
413static void __init
414test_pointer(void)
415{
416 plain();
417 symbol_ptr();
418 kernel_ptr();
419 struct_resource();
420 addr();
421 escaped_str();
422 hex_string();
423 mac();
424 ip();
425 uuid();
426 dentry();
427 struct_va_format();
428 struct_clk();
429 bitmap();
430 netdev_features();
431}
432
433static int __init
434test_printf_init(void)
435{
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800436 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
437 if (!alloced_buffer)
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800438 return -ENOMEM;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800439 test_buffer = alloced_buffer + PAD_SIZE;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800440
441 test_basic();
442 test_number();
443 test_string();
444 test_pointer();
445
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800446 kfree(alloced_buffer);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800447
448 if (failed_tests == 0)
449 pr_info("all %u tests passed\n", total_tests);
450 else
451 pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
452
453 return failed_tests ? -EINVAL : 0;
454}
455
456module_init(test_printf_init);
457
458MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
459MODULE_LICENSE("GPL");