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