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