Back to home page

LXR

 
 

    


0001 /*
0002  * Test cases for printf facility.
0003  */
0004 
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006 
0007 #include <linux/init.h>
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/printk.h>
0011 #include <linux/random.h>
0012 #include <linux/slab.h>
0013 #include <linux/string.h>
0014 
0015 #include <linux/bitmap.h>
0016 #include <linux/dcache.h>
0017 #include <linux/socket.h>
0018 #include <linux/in.h>
0019 
0020 #include <linux/gfp.h>
0021 #include <linux/mm.h>
0022 
0023 #define BUF_SIZE 256
0024 #define PAD_SIZE 16
0025 #define FILL_CHAR '$'
0026 
0027 #define PTR1 ((void*)0x01234567)
0028 #define PTR2 ((void*)(long)(int)0xfedcba98)
0029 
0030 #if BITS_PER_LONG == 64
0031 #define PTR1_ZEROES "000000000"
0032 #define PTR1_SPACES "         "
0033 #define PTR1_STR "1234567"
0034 #define PTR2_STR "fffffffffedcba98"
0035 #define PTR_WIDTH 16
0036 #else
0037 #define PTR1_ZEROES "0"
0038 #define PTR1_SPACES " "
0039 #define PTR1_STR "1234567"
0040 #define PTR2_STR "fedcba98"
0041 #define PTR_WIDTH 8
0042 #endif
0043 #define PTR_WIDTH_STR stringify(PTR_WIDTH)
0044 
0045 static unsigned total_tests __initdata;
0046 static unsigned failed_tests __initdata;
0047 static char *test_buffer __initdata;
0048 static char *alloced_buffer __initdata;
0049 
0050 static int __printf(4, 0) __init
0051 do_test(int bufsize, const char *expect, int elen,
0052     const char *fmt, va_list ap)
0053 {
0054     va_list aq;
0055     int ret, written;
0056 
0057     total_tests++;
0058 
0059     memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
0060     va_copy(aq, ap);
0061     ret = vsnprintf(test_buffer, bufsize, fmt, aq);
0062     va_end(aq);
0063 
0064     if (ret != elen) {
0065         pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
0066             bufsize, fmt, ret, elen);
0067         return 1;
0068     }
0069 
0070     if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
0071         pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
0072         return 1;
0073     }
0074 
0075     if (!bufsize) {
0076         if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
0077             pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
0078                 fmt);
0079             return 1;
0080         }
0081         return 0;
0082     }
0083 
0084     written = min(bufsize-1, elen);
0085     if (test_buffer[written]) {
0086         pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
0087             bufsize, fmt);
0088         return 1;
0089     }
0090 
0091     if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
0092         pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
0093             bufsize, fmt);
0094         return 1;
0095     }
0096 
0097     if (memcmp(test_buffer, expect, written)) {
0098         pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
0099             bufsize, fmt, test_buffer, written, expect);
0100         return 1;
0101     }
0102     return 0;
0103 }
0104 
0105 static void __printf(3, 4) __init
0106 __test(const char *expect, int elen, const char *fmt, ...)
0107 {
0108     va_list ap;
0109     int rand;
0110     char *p;
0111 
0112     if (elen >= BUF_SIZE) {
0113         pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
0114                elen, fmt);
0115         failed_tests++;
0116         return;
0117     }
0118 
0119     va_start(ap, fmt);
0120 
0121     /*
0122      * Every fmt+args is subjected to four tests: Three where we
0123      * tell vsnprintf varying buffer sizes (plenty, not quite
0124      * enough and 0), and then we also test that kvasprintf would
0125      * be able to print it as expected.
0126      */
0127     failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
0128     rand = 1 + prandom_u32_max(elen+1);
0129     /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
0130     failed_tests += do_test(rand, expect, elen, fmt, ap);
0131     failed_tests += do_test(0, expect, elen, fmt, ap);
0132 
0133     p = kvasprintf(GFP_KERNEL, fmt, ap);
0134     if (p) {
0135         total_tests++;
0136         if (memcmp(p, expect, elen+1)) {
0137             pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
0138                 fmt, p, expect);
0139             failed_tests++;
0140         }
0141         kfree(p);
0142     }
0143     va_end(ap);
0144 }
0145 
0146 #define test(expect, fmt, ...)                  \
0147     __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
0148 
0149 static void __init
0150 test_basic(void)
0151 {
0152     /* Work around annoying "warning: zero-length gnu_printf format string". */
0153     char nul = '\0';
0154 
0155     test("", &nul);
0156     test("100%", "100%%");
0157     test("xxx%yyy", "xxx%cyyy", '%');
0158     __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
0159 }
0160 
0161 static void __init
0162 test_number(void)
0163 {
0164     test("0x1234abcd  ", "%#-12x", 0x1234abcd);
0165     test("  0x1234abcd", "%#12x", 0x1234abcd);
0166     test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
0167     test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
0168     test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
0169     test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
0170     /*
0171      * POSIX/C99: »The result of converting zero with an explicit
0172      * precision of zero shall be no characters.« Hence the output
0173      * from the below test should really be "00|0||| ". However,
0174      * the kernel's printf also produces a single 0 in that
0175      * case. This test case simply documents the current
0176      * behaviour.
0177      */
0178     test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
0179 #ifndef __CHAR_UNSIGNED__
0180     {
0181         /*
0182          * Passing a 'char' to a %02x specifier doesn't do
0183          * what was presumably the intention when char is
0184          * signed and the value is negative. One must either &
0185          * with 0xff or cast to u8.
0186          */
0187         char val = -16;
0188         test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
0189     }
0190 #endif
0191 }
0192 
0193 static void __init
0194 test_string(void)
0195 {
0196     test("", "%s%.0s", "", "123");
0197     test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
0198     test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
0199     test("1234      ", "%-10.4s", "123456");
0200     test("      1234", "%10.4s", "123456");
0201     /*
0202      * POSIX and C99 say that a negative precision (which is only
0203      * possible to pass via a * argument) should be treated as if
0204      * the precision wasn't present, and that if the precision is
0205      * omitted (as in %.s), the precision should be taken to be
0206      * 0. However, the kernel's printf behave exactly opposite,
0207      * treating a negative precision as 0 and treating an omitted
0208      * precision specifier as if no precision was given.
0209      *
0210      * These test cases document the current behaviour; should
0211      * anyone ever feel the need to follow the standards more
0212      * closely, this can be revisited.
0213      */
0214     test("    ", "%4.*s", -5, "123456");
0215     test("123456", "%.s", "123456");
0216     test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
0217     test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
0218 }
0219 
0220 static void __init
0221 plain(void)
0222 {
0223     test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
0224     /*
0225      * The field width is overloaded for some %p extensions to
0226      * pass another piece of information. For plain pointers, the
0227      * behaviour is slightly odd: One cannot pass either the 0
0228      * flag nor a precision to %p without gcc complaining, and if
0229      * one explicitly gives a field width, the number is no longer
0230      * zero-padded.
0231      */
0232     test("|" PTR1_STR PTR1_SPACES "  |  " PTR1_SPACES PTR1_STR "|",
0233          "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
0234     test("|" PTR2_STR "  |  " PTR2_STR "|",
0235          "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
0236 
0237     /*
0238      * Unrecognized %p extensions are treated as plain %p, but the
0239      * alphanumeric suffix is ignored (that is, does not occur in
0240      * the output.)
0241      */
0242     test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
0243     test("|"PTR2_STR"|", "|%p0y|", PTR2);
0244 }
0245 
0246 static void __init
0247 symbol_ptr(void)
0248 {
0249 }
0250 
0251 static void __init
0252 kernel_ptr(void)
0253 {
0254 }
0255 
0256 static void __init
0257 struct_resource(void)
0258 {
0259 }
0260 
0261 static void __init
0262 addr(void)
0263 {
0264 }
0265 
0266 static void __init
0267 escaped_str(void)
0268 {
0269 }
0270 
0271 static void __init
0272 hex_string(void)
0273 {
0274     const char buf[3] = {0xc0, 0xff, 0xee};
0275 
0276     test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
0277          "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
0278     test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
0279          "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
0280 }
0281 
0282 static void __init
0283 mac(void)
0284 {
0285     const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
0286 
0287     test("2d:48:d6:fc:7a:05", "%pM", addr);
0288     test("05:7a:fc:d6:48:2d", "%pMR", addr);
0289     test("2d-48-d6-fc-7a-05", "%pMF", addr);
0290     test("2d48d6fc7a05", "%pm", addr);
0291     test("057afcd6482d", "%pmR", addr);
0292 }
0293 
0294 static void __init
0295 ip4(void)
0296 {
0297     struct sockaddr_in sa;
0298 
0299     sa.sin_family = AF_INET;
0300     sa.sin_port = cpu_to_be16(12345);
0301     sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
0302 
0303     test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
0304     test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
0305     sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
0306     test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
0307 }
0308 
0309 static void __init
0310 ip6(void)
0311 {
0312 }
0313 
0314 static void __init
0315 ip(void)
0316 {
0317     ip4();
0318     ip6();
0319 }
0320 
0321 static void __init
0322 uuid(void)
0323 {
0324     const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0325                    0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
0326 
0327     test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
0328     test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
0329     test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
0330     test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
0331 }
0332 
0333 static struct dentry test_dentry[4] __initdata = {
0334     { .d_parent = &test_dentry[0],
0335       .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
0336       .d_iname = "foo" },
0337     { .d_parent = &test_dentry[0],
0338       .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
0339       .d_iname = "bravo" },
0340     { .d_parent = &test_dentry[1],
0341       .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
0342       .d_iname = "alfa" },
0343     { .d_parent = &test_dentry[2],
0344       .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
0345       .d_iname = "romeo" },
0346 };
0347 
0348 static void __init
0349 dentry(void)
0350 {
0351     test("foo", "%pd", &test_dentry[0]);
0352     test("foo", "%pd2", &test_dentry[0]);
0353 
0354     test("romeo", "%pd", &test_dentry[3]);
0355     test("alfa/romeo", "%pd2", &test_dentry[3]);
0356     test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
0357     test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
0358     test("/bravo/alfa", "%pd4", &test_dentry[2]);
0359 
0360     test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
0361     test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
0362 }
0363 
0364 static void __init
0365 struct_va_format(void)
0366 {
0367 }
0368 
0369 static void __init
0370 struct_clk(void)
0371 {
0372 }
0373 
0374 static void __init
0375 large_bitmap(void)
0376 {
0377     const int nbits = 1 << 16;
0378     unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
0379     if (!bits)
0380         return;
0381 
0382     bitmap_set(bits, 1, 20);
0383     bitmap_set(bits, 60000, 15);
0384     test("1-20,60000-60014", "%*pbl", nbits, bits);
0385     kfree(bits);
0386 }
0387 
0388 static void __init
0389 bitmap(void)
0390 {
0391     DECLARE_BITMAP(bits, 20);
0392     const int primes[] = {2,3,5,7,11,13,17,19};
0393     int i;
0394 
0395     bitmap_zero(bits, 20);
0396     test("00000|00000", "%20pb|%*pb", bits, 20, bits);
0397     test("|", "%20pbl|%*pbl", bits, 20, bits);
0398 
0399     for (i = 0; i < ARRAY_SIZE(primes); ++i)
0400         set_bit(primes[i], bits);
0401     test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
0402     test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
0403 
0404     bitmap_fill(bits, 20);
0405     test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
0406     test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
0407 
0408     large_bitmap();
0409 }
0410 
0411 static void __init
0412 netdev_features(void)
0413 {
0414 }
0415 
0416 static void __init
0417 flags(void)
0418 {
0419     unsigned long flags;
0420     gfp_t gfp;
0421     char *cmp_buffer;
0422 
0423     flags = 0;
0424     test("", "%pGp", &flags);
0425 
0426     /* Page flags should filter the zone id */
0427     flags = 1UL << NR_PAGEFLAGS;
0428     test("", "%pGp", &flags);
0429 
0430     flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
0431         | 1UL << PG_active | 1UL << PG_swapbacked;
0432     test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
0433 
0434 
0435     flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
0436             | VM_DENYWRITE;
0437     test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
0438 
0439     gfp = GFP_TRANSHUGE;
0440     test("GFP_TRANSHUGE", "%pGg", &gfp);
0441 
0442     gfp = GFP_ATOMIC|__GFP_DMA;
0443     test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
0444 
0445     gfp = __GFP_ATOMIC;
0446     test("__GFP_ATOMIC", "%pGg", &gfp);
0447 
0448     cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
0449     if (!cmp_buffer)
0450         return;
0451 
0452     /* Any flags not translated by the table should remain numeric */
0453     gfp = ~__GFP_BITS_MASK;
0454     snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
0455     test(cmp_buffer, "%pGg", &gfp);
0456 
0457     snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
0458                             (unsigned long) gfp);
0459     gfp |= __GFP_ATOMIC;
0460     test(cmp_buffer, "%pGg", &gfp);
0461 
0462     kfree(cmp_buffer);
0463 }
0464 
0465 static void __init
0466 test_pointer(void)
0467 {
0468     plain();
0469     symbol_ptr();
0470     kernel_ptr();
0471     struct_resource();
0472     addr();
0473     escaped_str();
0474     hex_string();
0475     mac();
0476     ip();
0477     uuid();
0478     dentry();
0479     struct_va_format();
0480     struct_clk();
0481     bitmap();
0482     netdev_features();
0483     flags();
0484 }
0485 
0486 static int __init
0487 test_printf_init(void)
0488 {
0489     alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
0490     if (!alloced_buffer)
0491         return -ENOMEM;
0492     test_buffer = alloced_buffer + PAD_SIZE;
0493 
0494     test_basic();
0495     test_number();
0496     test_string();
0497     test_pointer();
0498 
0499     kfree(alloced_buffer);
0500 
0501     if (failed_tests == 0)
0502         pr_info("all %u tests passed\n", total_tests);
0503     else
0504         pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
0505 
0506     return failed_tests ? -EINVAL : 0;
0507 }
0508 
0509 module_init(test_printf_init);
0510 
0511 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
0512 MODULE_LICENSE("GPL");