blob: b23ce824766f7e0a41e2293217643e6b9e183b7d [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
15#include <linux/socket.h>
16#include <linux/in.h>
17
18#define BUF_SIZE 256
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080019#define PAD_SIZE 16
Rasmus Villemoes707cc722015-11-06 16:30:29 -080020#define FILL_CHAR '$'
21
22#define PTR1 ((void*)0x01234567)
23#define PTR2 ((void*)(long)(int)0xfedcba98)
24
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"
30#define PTR_WIDTH 16
31#else
32#define PTR1_ZEROES "0"
33#define PTR1_SPACES " "
34#define PTR1_STR "1234567"
35#define PTR2_STR "fedcba98"
36#define PTR_WIDTH 8
37#endif
38#define PTR_WIDTH_STR stringify(PTR_WIDTH)
39
40static unsigned total_tests __initdata;
41static unsigned failed_tests __initdata;
42static char *test_buffer __initdata;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080043static char *alloced_buffer __initdata;
Rasmus Villemoes707cc722015-11-06 16:30:29 -080044
45static int __printf(4, 0) __init
46do_test(int bufsize, const char *expect, int elen,
47 const char *fmt, va_list ap)
48{
49 va_list aq;
50 int ret, written;
51
52 total_tests++;
53
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080054 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
Rasmus Villemoes707cc722015-11-06 16:30:29 -080055 va_copy(aq, ap);
56 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
57 va_end(aq);
58
59 if (ret != elen) {
60 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
61 bufsize, fmt, ret, elen);
62 return 1;
63 }
64
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080065 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
66 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
67 return 1;
68 }
69
Rasmus Villemoes707cc722015-11-06 16:30:29 -080070 if (!bufsize) {
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080071 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
Rasmus Villemoes707cc722015-11-06 16:30:29 -080072 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
73 fmt);
74 return 1;
75 }
76 return 0;
77 }
78
79 written = min(bufsize-1, elen);
80 if (test_buffer[written]) {
81 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
82 bufsize, fmt);
83 return 1;
84 }
85
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080086 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",
88 bufsize, fmt);
89 return 1;
90 }
91
Rasmus Villemoes707cc722015-11-06 16:30:29 -080092 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);
95 return 1;
96 }
97 return 0;
98}
99
100static void __printf(3, 4) __init
101__test(const char *expect, int elen, const char *fmt, ...)
102{
103 va_list ap;
104 int rand;
105 char *p;
106
Rasmus Villemoesfd0515d2016-01-15 16:58:50 -0800107 if (elen >= BUF_SIZE) {
108 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
109 elen, fmt);
110 failed_tests++;
111 return;
112 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800113
114 va_start(ap, fmt);
115
116 /*
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.
121 */
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);
127
128 p = kvasprintf(GFP_KERNEL, fmt, ap);
129 if (p) {
130 if (memcmp(p, expect, elen+1)) {
131 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
132 fmt, p, expect);
133 failed_tests++;
134 }
135 kfree(p);
136 }
137 va_end(ap);
138}
139
140#define test(expect, fmt, ...) \
141 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
142
143static void __init
144test_basic(void)
145{
146 /* Work around annoying "warning: zero-length gnu_printf format string". */
147 char nul = '\0';
148
149 test("", &nul);
150 test("100%", "100%%");
151 test("xxx%yyy", "xxx%cyyy", '%');
152 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
153}
154
155static void __init
156test_number(void)
157{
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);
Rasmus Villemoes1ca8e8e2016-01-15 16:58:59 -0800161 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
162 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
163 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
164 /*
165 * POSIX/C99: »The result of converting zero with an explicit
166 * precision of zero shall be no characters.« Hence the output
167 * from the below test should really be "00|0||| ". However,
168 * the kernel's printf also produces a single 0 in that
169 * case. This test case simply documents the current
170 * behaviour.
171 */
172 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
173#ifndef __CHAR_UNSIGNED__
174 {
175 /*
176 * Passing a 'char' to a %02x specifier doesn't do
177 * what was presumably the intention when char is
178 * signed and the value is negative. One must either &
179 * with 0xff or cast to u8.
180 */
181 char val = -16;
182 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
183 }
184#endif
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800185}
186
187static void __init
188test_string(void)
189{
190 test("", "%s%.0s", "", "123");
191 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
192 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 -0800193 test("1234 ", "%-10.4s", "123456");
194 test(" 1234", "%10.4s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800195 /*
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800196 * POSIX and C99 say that a negative precision (which is only
197 * possible to pass via a * argument) should be treated as if
198 * the precision wasn't present, and that if the precision is
199 * omitted (as in %.s), the precision should be taken to be
200 * 0. However, the kernel's printf behave exactly opposite,
201 * treating a negative precision as 0 and treating an omitted
202 * precision specifier as if no precision was given.
203 *
204 * These test cases document the current behaviour; should
205 * anyone ever feel the need to follow the standards more
206 * closely, this can be revisited.
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800207 */
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800208 test(" ", "%4.*s", -5, "123456");
209 test("123456", "%.s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800210 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
211 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
212}
213
214static void __init
215plain(void)
216{
217 test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
218 /*
219 * The field width is overloaded for some %p extensions to
220 * pass another piece of information. For plain pointers, the
221 * behaviour is slightly odd: One cannot pass either the 0
222 * flag nor a precision to %p without gcc complaining, and if
223 * one explicitly gives a field width, the number is no longer
224 * zero-padded.
225 */
226 test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
227 "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
228 test("|" PTR2_STR " | " PTR2_STR "|",
229 "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
230
231 /*
232 * Unrecognized %p extensions are treated as plain %p, but the
233 * alphanumeric suffix is ignored (that is, does not occur in
234 * the output.)
235 */
236 test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
237 test("|"PTR2_STR"|", "|%p0y|", PTR2);
238}
239
240static void __init
241symbol_ptr(void)
242{
243}
244
245static void __init
246kernel_ptr(void)
247{
248}
249
250static void __init
251struct_resource(void)
252{
253}
254
255static void __init
256addr(void)
257{
258}
259
260static void __init
261escaped_str(void)
262{
263}
264
265static void __init
266hex_string(void)
267{
268 const char buf[3] = {0xc0, 0xff, 0xee};
269
270 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
271 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
272 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
273 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
274}
275
276static void __init
277mac(void)
278{
279 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
280
281 test("2d:48:d6:fc:7a:05", "%pM", addr);
282 test("05:7a:fc:d6:48:2d", "%pMR", addr);
283 test("2d-48-d6-fc-7a-05", "%pMF", addr);
284 test("2d48d6fc7a05", "%pm", addr);
285 test("057afcd6482d", "%pmR", addr);
286}
287
288static void __init
289ip4(void)
290{
291 struct sockaddr_in sa;
292
293 sa.sin_family = AF_INET;
294 sa.sin_port = cpu_to_be16(12345);
295 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
296
297 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
298 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
299 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
300 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
301}
302
303static void __init
304ip6(void)
305{
306}
307
308static void __init
309ip(void)
310{
311 ip4();
312 ip6();
313}
314
315static void __init
316uuid(void)
317{
318 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
319 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
320
321 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
322 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
323 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
324 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
325}
326
327static void __init
328dentry(void)
329{
330}
331
332static void __init
333struct_va_format(void)
334{
335}
336
337static void __init
338struct_clk(void)
339{
340}
341
342static void __init
343bitmap(void)
344{
345 DECLARE_BITMAP(bits, 20);
346 const int primes[] = {2,3,5,7,11,13,17,19};
347 int i;
348
349 bitmap_zero(bits, 20);
350 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
351 test("|", "%20pbl|%*pbl", bits, 20, bits);
352
353 for (i = 0; i < ARRAY_SIZE(primes); ++i)
354 set_bit(primes[i], bits);
355 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
356 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
357
358 bitmap_fill(bits, 20);
359 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
360 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
361}
362
363static void __init
364netdev_features(void)
365{
366}
367
368static void __init
369test_pointer(void)
370{
371 plain();
372 symbol_ptr();
373 kernel_ptr();
374 struct_resource();
375 addr();
376 escaped_str();
377 hex_string();
378 mac();
379 ip();
380 uuid();
381 dentry();
382 struct_va_format();
383 struct_clk();
384 bitmap();
385 netdev_features();
386}
387
388static int __init
389test_printf_init(void)
390{
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800391 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
392 if (!alloced_buffer)
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800393 return -ENOMEM;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800394 test_buffer = alloced_buffer + PAD_SIZE;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800395
396 test_basic();
397 test_number();
398 test_string();
399 test_pointer();
400
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800401 kfree(alloced_buffer);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800402
403 if (failed_tests == 0)
404 pr_info("all %u tests passed\n", total_tests);
405 else
406 pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
407
408 return failed_tests ? -EINVAL : 0;
409}
410
411module_init(test_printf_init);
412
413MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
414MODULE_LICENSE("GPL");