This source file includes following definitions.
- __printf
- __printf
- test_basic
- test_number
- test_string
- plain_format
- plain_format
- plain_hash_to_buffer
- plain_hash
- plain
- test_hashed
- null_pointer
- error_pointer
- invalid_pointer
- symbol_ptr
- kernel_ptr
- struct_resource
- addr
- escaped_str
- hex_string
- mac
- ip4
- ip6
- ip
- uuid
- dentry
- struct_va_format
- struct_rtc_time
- struct_clk
- large_bitmap
- bitmap
- netdev_features
- flags
- test_pointer
- selftest
1
2
3
4
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/printk.h>
12 #include <linux/random.h>
13 #include <linux/rtc.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16
17 #include <linux/bitmap.h>
18 #include <linux/dcache.h>
19 #include <linux/socket.h>
20 #include <linux/in.h>
21
22 #include <linux/gfp.h>
23 #include <linux/mm.h>
24
25 #include "../tools/testing/selftests/kselftest_module.h"
26
27 #define BUF_SIZE 256
28 #define PAD_SIZE 16
29 #define FILL_CHAR '$'
30
31 static unsigned total_tests __initdata;
32 static unsigned failed_tests __initdata;
33 static char *test_buffer __initdata;
34 static char *alloced_buffer __initdata;
35
36 static int __printf(4, 0) __init
37 do_test(int bufsize, const char *expect, int elen,
38 const char *fmt, va_list ap)
39 {
40 va_list aq;
41 int ret, written;
42
43 total_tests++;
44
45 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
46 va_copy(aq, ap);
47 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
48 va_end(aq);
49
50 if (ret != elen) {
51 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
52 bufsize, fmt, ret, elen);
53 return 1;
54 }
55
56 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
57 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
58 return 1;
59 }
60
61 if (!bufsize) {
62 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
63 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
64 fmt);
65 return 1;
66 }
67 return 0;
68 }
69
70 written = min(bufsize-1, elen);
71 if (test_buffer[written]) {
72 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
73 bufsize, fmt);
74 return 1;
75 }
76
77 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
78 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
79 bufsize, fmt);
80 return 1;
81 }
82
83 if (memcmp(test_buffer, expect, written)) {
84 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
85 bufsize, fmt, test_buffer, written, expect);
86 return 1;
87 }
88 return 0;
89 }
90
91 static void __printf(3, 4) __init
92 __test(const char *expect, int elen, const char *fmt, ...)
93 {
94 va_list ap;
95 int rand;
96 char *p;
97
98 if (elen >= BUF_SIZE) {
99 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
100 elen, fmt);
101 failed_tests++;
102 return;
103 }
104
105 va_start(ap, fmt);
106
107
108
109
110
111
112
113 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
114 rand = 1 + prandom_u32_max(elen+1);
115
116 failed_tests += do_test(rand, expect, elen, fmt, ap);
117 failed_tests += do_test(0, expect, elen, fmt, ap);
118
119 p = kvasprintf(GFP_KERNEL, fmt, ap);
120 if (p) {
121 total_tests++;
122 if (memcmp(p, expect, elen+1)) {
123 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
124 fmt, p, expect);
125 failed_tests++;
126 }
127 kfree(p);
128 }
129 va_end(ap);
130 }
131
132 #define test(expect, fmt, ...) \
133 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
134
135 static void __init
136 test_basic(void)
137 {
138
139 char nul = '\0';
140
141 test("", &nul);
142 test("100%", "100%%");
143 test("xxx%yyy", "xxx%cyyy", '%');
144 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
145 }
146
147 static void __init
148 test_number(void)
149 {
150 test("0x1234abcd ", "%#-12x", 0x1234abcd);
151 test(" 0x1234abcd", "%#12x", 0x1234abcd);
152 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
153 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
154 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
155 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
156
157
158
159
160
161
162
163
164 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
165 #ifndef __CHAR_UNSIGNED__
166 {
167
168
169
170
171
172
173 char val = -16;
174 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
175 }
176 #endif
177 }
178
179 static void __init
180 test_string(void)
181 {
182 test("", "%s%.0s", "", "123");
183 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
184 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
185 test("1234 ", "%-10.4s", "123456");
186 test(" 1234", "%10.4s", "123456");
187
188
189
190
191
192
193
194
195
196
197
198
199
200 test(" ", "%4.*s", -5, "123456");
201 test("123456", "%.s", "123456");
202 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
203 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
204 }
205
206 #define PLAIN_BUF_SIZE 64
207
208 #if BITS_PER_LONG == 64
209
210 #define PTR_WIDTH 16
211 #define PTR ((void *)0xffff0123456789abUL)
212 #define PTR_STR "ffff0123456789ab"
213 #define PTR_VAL_NO_CRNG "(____ptrval____)"
214 #define ZEROS "00000000"
215 #define ONES "ffffffff"
216
217 static int __init
218 plain_format(void)
219 {
220 char buf[PLAIN_BUF_SIZE];
221 int nchars;
222
223 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
224
225 if (nchars != PTR_WIDTH)
226 return -1;
227
228 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
229 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
230 PTR_VAL_NO_CRNG);
231 return 0;
232 }
233
234 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
235 return -1;
236
237 return 0;
238 }
239
240 #else
241
242 #define PTR_WIDTH 8
243 #define PTR ((void *)0x456789ab)
244 #define PTR_STR "456789ab"
245 #define PTR_VAL_NO_CRNG "(ptrval)"
246 #define ZEROS ""
247 #define ONES ""
248
249 static int __init
250 plain_format(void)
251 {
252
253 return 0;
254 }
255
256 #endif
257
258 static int __init
259 plain_hash_to_buffer(const void *p, char *buf, size_t len)
260 {
261 int nchars;
262
263 nchars = snprintf(buf, len, "%p", p);
264
265 if (nchars != PTR_WIDTH)
266 return -1;
267
268 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
269 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
270 PTR_VAL_NO_CRNG);
271 return 0;
272 }
273
274 return 0;
275 }
276
277 static int __init
278 plain_hash(void)
279 {
280 char buf[PLAIN_BUF_SIZE];
281 int ret;
282
283 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
284 if (ret)
285 return ret;
286
287 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
288 return -1;
289
290 return 0;
291 }
292
293
294
295
296
297 static void __init
298 plain(void)
299 {
300 int err;
301
302 err = plain_hash();
303 if (err) {
304 pr_warn("plain 'p' does not appear to be hashed\n");
305 failed_tests++;
306 return;
307 }
308
309 err = plain_format();
310 if (err) {
311 pr_warn("hashing plain 'p' has unexpected format\n");
312 failed_tests++;
313 }
314 }
315
316 static void __init
317 test_hashed(const char *fmt, const void *p)
318 {
319 char buf[PLAIN_BUF_SIZE];
320 int ret;
321
322
323
324
325
326 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
327 if (ret)
328 return;
329
330 test(buf, fmt, p);
331 }
332
333
334
335
336 static void __init
337 null_pointer(void)
338 {
339 test(ZEROS "00000000", "%p", NULL);
340 test(ZEROS "00000000", "%px", NULL);
341 test("(null)", "%pE", NULL);
342 }
343
344
345
346
347 static void __init
348 error_pointer(void)
349 {
350 test(ONES "fffffff5", "%p", ERR_PTR(-11));
351 test(ONES "fffffff5", "%px", ERR_PTR(-11));
352 test("(efault)", "%pE", ERR_PTR(-11));
353 }
354
355 #define PTR_INVALID ((void *)0x000000ab)
356
357 static void __init
358 invalid_pointer(void)
359 {
360 test_hashed("%p", PTR_INVALID);
361 test(ZEROS "000000ab", "%px", PTR_INVALID);
362 test("(efault)", "%pE", PTR_INVALID);
363 }
364
365 static void __init
366 symbol_ptr(void)
367 {
368 }
369
370 static void __init
371 kernel_ptr(void)
372 {
373
374 }
375
376 static void __init
377 struct_resource(void)
378 {
379 }
380
381 static void __init
382 addr(void)
383 {
384 }
385
386 static void __init
387 escaped_str(void)
388 {
389 }
390
391 static void __init
392 hex_string(void)
393 {
394 const char buf[3] = {0xc0, 0xff, 0xee};
395
396 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
397 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
398 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
399 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
400 }
401
402 static void __init
403 mac(void)
404 {
405 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
406
407 test("2d:48:d6:fc:7a:05", "%pM", addr);
408 test("05:7a:fc:d6:48:2d", "%pMR", addr);
409 test("2d-48-d6-fc-7a-05", "%pMF", addr);
410 test("2d48d6fc7a05", "%pm", addr);
411 test("057afcd6482d", "%pmR", addr);
412 }
413
414 static void __init
415 ip4(void)
416 {
417 struct sockaddr_in sa;
418
419 sa.sin_family = AF_INET;
420 sa.sin_port = cpu_to_be16(12345);
421 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
422
423 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
424 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
425 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
426 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
427 }
428
429 static void __init
430 ip6(void)
431 {
432 }
433
434 static void __init
435 ip(void)
436 {
437 ip4();
438 ip6();
439 }
440
441 static void __init
442 uuid(void)
443 {
444 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
445 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
446
447 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
448 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
449 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
450 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
451 }
452
453 static struct dentry test_dentry[4] __initdata = {
454 { .d_parent = &test_dentry[0],
455 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
456 .d_iname = "foo" },
457 { .d_parent = &test_dentry[0],
458 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
459 .d_iname = "bravo" },
460 { .d_parent = &test_dentry[1],
461 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
462 .d_iname = "alfa" },
463 { .d_parent = &test_dentry[2],
464 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
465 .d_iname = "romeo" },
466 };
467
468 static void __init
469 dentry(void)
470 {
471 test("foo", "%pd", &test_dentry[0]);
472 test("foo", "%pd2", &test_dentry[0]);
473
474 test("(null)", "%pd", NULL);
475 test("(efault)", "%pd", PTR_INVALID);
476 test("(null)", "%pD", NULL);
477 test("(efault)", "%pD", PTR_INVALID);
478
479 test("romeo", "%pd", &test_dentry[3]);
480 test("alfa/romeo", "%pd2", &test_dentry[3]);
481 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
482 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
483 test("/bravo/alfa", "%pd4", &test_dentry[2]);
484
485 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
486 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
487 }
488
489 static void __init
490 struct_va_format(void)
491 {
492 }
493
494 static void __init
495 struct_rtc_time(void)
496 {
497
498 const struct rtc_time tm = {
499 .tm_sec = 43,
500 .tm_min = 35,
501 .tm_hour = 5,
502 .tm_mday = 26,
503 .tm_mon = 10,
504 .tm_year = 118,
505 };
506
507 test("(%ptR?)", "%pt", &tm);
508 test("2018-11-26T05:35:43", "%ptR", &tm);
509 test("0118-10-26T05:35:43", "%ptRr", &tm);
510 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
511 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
512 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
513 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
514 }
515
516 static void __init
517 struct_clk(void)
518 {
519 }
520
521 static void __init
522 large_bitmap(void)
523 {
524 const int nbits = 1 << 16;
525 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
526 if (!bits)
527 return;
528
529 bitmap_set(bits, 1, 20);
530 bitmap_set(bits, 60000, 15);
531 test("1-20,60000-60014", "%*pbl", nbits, bits);
532 bitmap_free(bits);
533 }
534
535 static void __init
536 bitmap(void)
537 {
538 DECLARE_BITMAP(bits, 20);
539 const int primes[] = {2,3,5,7,11,13,17,19};
540 int i;
541
542 bitmap_zero(bits, 20);
543 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
544 test("|", "%20pbl|%*pbl", bits, 20, bits);
545
546 for (i = 0; i < ARRAY_SIZE(primes); ++i)
547 set_bit(primes[i], bits);
548 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
549 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
550
551 bitmap_fill(bits, 20);
552 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
553 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
554
555 large_bitmap();
556 }
557
558 static void __init
559 netdev_features(void)
560 {
561 }
562
563 static void __init
564 flags(void)
565 {
566 unsigned long flags;
567 gfp_t gfp;
568 char *cmp_buffer;
569
570 flags = 0;
571 test("", "%pGp", &flags);
572
573
574 flags = 1UL << NR_PAGEFLAGS;
575 test("", "%pGp", &flags);
576
577 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
578 | 1UL << PG_active | 1UL << PG_swapbacked;
579 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
580
581
582 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
583 | VM_DENYWRITE;
584 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
585
586 gfp = GFP_TRANSHUGE;
587 test("GFP_TRANSHUGE", "%pGg", &gfp);
588
589 gfp = GFP_ATOMIC|__GFP_DMA;
590 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
591
592 gfp = __GFP_ATOMIC;
593 test("__GFP_ATOMIC", "%pGg", &gfp);
594
595 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
596 if (!cmp_buffer)
597 return;
598
599
600 gfp = ~__GFP_BITS_MASK;
601 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
602 test(cmp_buffer, "%pGg", &gfp);
603
604 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
605 (unsigned long) gfp);
606 gfp |= __GFP_ATOMIC;
607 test(cmp_buffer, "%pGg", &gfp);
608
609 kfree(cmp_buffer);
610 }
611
612 static void __init
613 test_pointer(void)
614 {
615 plain();
616 null_pointer();
617 error_pointer();
618 invalid_pointer();
619 symbol_ptr();
620 kernel_ptr();
621 struct_resource();
622 addr();
623 escaped_str();
624 hex_string();
625 mac();
626 ip();
627 uuid();
628 dentry();
629 struct_va_format();
630 struct_rtc_time();
631 struct_clk();
632 bitmap();
633 netdev_features();
634 flags();
635 }
636
637 static void __init selftest(void)
638 {
639 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
640 if (!alloced_buffer)
641 return;
642 test_buffer = alloced_buffer + PAD_SIZE;
643
644 test_basic();
645 test_number();
646 test_string();
647 test_pointer();
648
649 kfree(alloced_buffer);
650 }
651
652 KSTM_MODULE_LOADERS(test_printf);
653 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
654 MODULE_LICENSE("GPL");