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