Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 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 | |
Rasmus Villemoes | 857cca4 | 2016-01-15 16:59:06 -0800 | [diff] [blame] | 15 | #include <linux/bitmap.h> |
Rasmus Villemoes | 251c723 | 2016-01-15 16:59:09 -0800 | [diff] [blame] | 16 | #include <linux/dcache.h> |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 17 | #include <linux/socket.h> |
| 18 | #include <linux/in.h> |
| 19 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 20 | #include <linux/gfp.h> |
| 21 | #include <linux/mm.h> |
| 22 | |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 23 | #define BUF_SIZE 256 |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 24 | #define PAD_SIZE 16 |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 25 | #define FILL_CHAR '$' |
| 26 | |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 27 | static unsigned total_tests __initdata; |
| 28 | static unsigned failed_tests __initdata; |
| 29 | static char *test_buffer __initdata; |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 30 | static char *alloced_buffer __initdata; |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 31 | |
| 32 | static int __printf(4, 0) __init |
| 33 | do_test(int bufsize, const char *expect, int elen, |
| 34 | const char *fmt, va_list ap) |
| 35 | { |
| 36 | va_list aq; |
| 37 | int ret, written; |
| 38 | |
| 39 | total_tests++; |
| 40 | |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 41 | memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 42 | va_copy(aq, ap); |
| 43 | ret = vsnprintf(test_buffer, bufsize, fmt, aq); |
| 44 | va_end(aq); |
| 45 | |
| 46 | if (ret != elen) { |
| 47 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n", |
| 48 | bufsize, fmt, ret, elen); |
| 49 | return 1; |
| 50 | } |
| 51 | |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 52 | if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) { |
| 53 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt); |
| 54 | return 1; |
| 55 | } |
| 56 | |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 57 | if (!bufsize) { |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 58 | if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) { |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 59 | pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", |
| 60 | fmt); |
| 61 | return 1; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | written = min(bufsize-1, elen); |
| 67 | if (test_buffer[written]) { |
| 68 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n", |
| 69 | bufsize, fmt); |
| 70 | return 1; |
| 71 | } |
| 72 | |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 73 | if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) { |
| 74 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", |
| 75 | bufsize, fmt); |
| 76 | return 1; |
| 77 | } |
| 78 | |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 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 | |
Rasmus Villemoes | fd0515d | 2016-01-15 16:58:50 -0800 | [diff] [blame] | 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 | } |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 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) { |
Rasmus Villemoes | b79a7db | 2016-01-15 16:59:02 -0800 | [diff] [blame] | 117 | total_tests++; |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 118 | if (memcmp(p, expect, elen+1)) { |
| 119 | pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", |
| 120 | fmt, p, expect); |
| 121 | failed_tests++; |
| 122 | } |
| 123 | kfree(p); |
| 124 | } |
| 125 | va_end(ap); |
| 126 | } |
| 127 | |
| 128 | #define test(expect, fmt, ...) \ |
| 129 | __test(expect, strlen(expect), fmt, ##__VA_ARGS__) |
| 130 | |
| 131 | static void __init |
| 132 | test_basic(void) |
| 133 | { |
| 134 | /* Work around annoying "warning: zero-length gnu_printf format string". */ |
| 135 | char nul = '\0'; |
| 136 | |
| 137 | test("", &nul); |
| 138 | test("100%", "100%%"); |
| 139 | test("xxx%yyy", "xxx%cyyy", '%'); |
| 140 | __test("xxx\0yyy", 7, "xxx%cyyy", '\0'); |
| 141 | } |
| 142 | |
| 143 | static void __init |
| 144 | test_number(void) |
| 145 | { |
| 146 | test("0x1234abcd ", "%#-12x", 0x1234abcd); |
| 147 | test(" 0x1234abcd", "%#12x", 0x1234abcd); |
| 148 | test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234); |
Rasmus Villemoes | 1ca8e8e | 2016-01-15 16:58:59 -0800 | [diff] [blame] | 149 | test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); |
| 150 | test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); |
| 151 | test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); |
| 152 | /* |
| 153 | * POSIX/C99: »The result of converting zero with an explicit |
| 154 | * precision of zero shall be no characters.« Hence the output |
| 155 | * from the below test should really be "00|0||| ". However, |
| 156 | * the kernel's printf also produces a single 0 in that |
| 157 | * case. This test case simply documents the current |
| 158 | * behaviour. |
| 159 | */ |
| 160 | test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0); |
| 161 | #ifndef __CHAR_UNSIGNED__ |
| 162 | { |
| 163 | /* |
| 164 | * Passing a 'char' to a %02x specifier doesn't do |
| 165 | * what was presumably the intention when char is |
| 166 | * signed and the value is negative. One must either & |
| 167 | * with 0xff or cast to u8. |
| 168 | */ |
| 169 | char val = -16; |
| 170 | test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val); |
| 171 | } |
| 172 | #endif |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void __init |
| 176 | test_string(void) |
| 177 | { |
| 178 | test("", "%s%.0s", "", "123"); |
| 179 | test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); |
| 180 | test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); |
Rasmus Villemoes | f176eb4 | 2016-01-15 16:58:56 -0800 | [diff] [blame] | 181 | test("1234 ", "%-10.4s", "123456"); |
| 182 | test(" 1234", "%10.4s", "123456"); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 183 | /* |
Rasmus Villemoes | f176eb4 | 2016-01-15 16:58:56 -0800 | [diff] [blame] | 184 | * POSIX and C99 say that a negative precision (which is only |
| 185 | * possible to pass via a * argument) should be treated as if |
| 186 | * the precision wasn't present, and that if the precision is |
| 187 | * omitted (as in %.s), the precision should be taken to be |
| 188 | * 0. However, the kernel's printf behave exactly opposite, |
| 189 | * treating a negative precision as 0 and treating an omitted |
| 190 | * precision specifier as if no precision was given. |
| 191 | * |
| 192 | * These test cases document the current behaviour; should |
| 193 | * anyone ever feel the need to follow the standards more |
| 194 | * closely, this can be revisited. |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 195 | */ |
Rasmus Villemoes | f176eb4 | 2016-01-15 16:58:56 -0800 | [diff] [blame] | 196 | test(" ", "%4.*s", -5, "123456"); |
| 197 | test("123456", "%.s", "123456"); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 198 | test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); |
| 199 | test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); |
| 200 | } |
| 201 | |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 202 | #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */ |
| 203 | |
| 204 | #if BITS_PER_LONG == 64 |
| 205 | |
| 206 | #define PTR_WIDTH 16 |
Andy Shevchenko | c604b40 | 2018-02-16 23:07:03 +0200 | [diff] [blame] | 207 | #define PTR ((void *)0xffff0123456789abUL) |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 208 | #define PTR_STR "ffff0123456789ab" |
Thierry Escande | ce041c4 | 2018-06-13 19:18:40 +0200 | [diff] [blame] | 209 | #define PTR_VAL_NO_CRNG "(____ptrval____)" |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 210 | #define ZEROS "00000000" /* hex 32 zero bits */ |
| 211 | |
| 212 | static int __init |
| 213 | plain_format(void) |
| 214 | { |
| 215 | char buf[PLAIN_BUF_SIZE]; |
| 216 | int nchars; |
| 217 | |
| 218 | nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR); |
| 219 | |
Thierry Escande | ce041c4 | 2018-06-13 19:18:40 +0200 | [diff] [blame] | 220 | if (nchars != PTR_WIDTH) |
| 221 | return -1; |
| 222 | |
| 223 | if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { |
| 224 | pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"", |
| 225 | PTR_VAL_NO_CRNG); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0) |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 230 | return -1; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | #else |
| 236 | |
| 237 | #define PTR_WIDTH 8 |
| 238 | #define PTR ((void *)0x456789ab) |
| 239 | #define PTR_STR "456789ab" |
Thierry Escande | ce041c4 | 2018-06-13 19:18:40 +0200 | [diff] [blame] | 240 | #define PTR_VAL_NO_CRNG "(ptrval)" |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 241 | |
| 242 | static int __init |
| 243 | plain_format(void) |
| 244 | { |
| 245 | /* Format is implicitly tested for 32 bit machines by plain_hash() */ |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | #endif /* BITS_PER_LONG == 64 */ |
| 250 | |
| 251 | static int __init |
| 252 | plain_hash(void) |
| 253 | { |
| 254 | char buf[PLAIN_BUF_SIZE]; |
| 255 | int nchars; |
| 256 | |
| 257 | nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR); |
| 258 | |
Thierry Escande | ce041c4 | 2018-06-13 19:18:40 +0200 | [diff] [blame] | 259 | if (nchars != PTR_WIDTH) |
| 260 | return -1; |
| 261 | |
| 262 | if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { |
| 263 | pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"", |
| 264 | PTR_VAL_NO_CRNG); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0) |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 269 | return -1; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * We can't use test() to test %p because we don't know what output to expect |
| 276 | * after an address is hashed. |
| 277 | */ |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 278 | static void __init |
| 279 | plain(void) |
| 280 | { |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 281 | int err; |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 282 | |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 283 | err = plain_hash(); |
| 284 | if (err) { |
| 285 | pr_warn("plain 'p' does not appear to be hashed\n"); |
| 286 | failed_tests++; |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | err = plain_format(); |
| 291 | if (err) { |
| 292 | pr_warn("hashing plain 'p' has unexpected format\n"); |
| 293 | failed_tests++; |
| 294 | } |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void __init |
| 298 | symbol_ptr(void) |
| 299 | { |
| 300 | } |
| 301 | |
| 302 | static void __init |
| 303 | kernel_ptr(void) |
| 304 | { |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 305 | /* We can't test this without access to kptr_restrict. */ |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void __init |
| 309 | struct_resource(void) |
| 310 | { |
| 311 | } |
| 312 | |
| 313 | static void __init |
| 314 | addr(void) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | static void __init |
| 319 | escaped_str(void) |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | static void __init |
| 324 | hex_string(void) |
| 325 | { |
| 326 | const char buf[3] = {0xc0, 0xff, 0xee}; |
| 327 | |
| 328 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", |
| 329 | "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); |
| 330 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", |
| 331 | "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf); |
| 332 | } |
| 333 | |
| 334 | static void __init |
| 335 | mac(void) |
| 336 | { |
| 337 | const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; |
| 338 | |
| 339 | test("2d:48:d6:fc:7a:05", "%pM", addr); |
| 340 | test("05:7a:fc:d6:48:2d", "%pMR", addr); |
| 341 | test("2d-48-d6-fc-7a-05", "%pMF", addr); |
| 342 | test("2d48d6fc7a05", "%pm", addr); |
| 343 | test("057afcd6482d", "%pmR", addr); |
| 344 | } |
| 345 | |
| 346 | static void __init |
| 347 | ip4(void) |
| 348 | { |
| 349 | struct sockaddr_in sa; |
| 350 | |
| 351 | sa.sin_family = AF_INET; |
| 352 | sa.sin_port = cpu_to_be16(12345); |
| 353 | sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); |
| 354 | |
| 355 | test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); |
| 356 | test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); |
| 357 | sa.sin_addr.s_addr = cpu_to_be32(0x01020304); |
| 358 | test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa); |
| 359 | } |
| 360 | |
| 361 | static void __init |
| 362 | ip6(void) |
| 363 | { |
| 364 | } |
| 365 | |
| 366 | static void __init |
| 367 | ip(void) |
| 368 | { |
| 369 | ip4(); |
| 370 | ip6(); |
| 371 | } |
| 372 | |
| 373 | static void __init |
| 374 | uuid(void) |
| 375 | { |
| 376 | const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, |
| 377 | 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; |
| 378 | |
| 379 | test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); |
| 380 | test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); |
| 381 | test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); |
| 382 | test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); |
| 383 | } |
| 384 | |
Rasmus Villemoes | 251c723 | 2016-01-15 16:59:09 -0800 | [diff] [blame] | 385 | static struct dentry test_dentry[4] __initdata = { |
| 386 | { .d_parent = &test_dentry[0], |
| 387 | .d_name = QSTR_INIT(test_dentry[0].d_iname, 3), |
| 388 | .d_iname = "foo" }, |
| 389 | { .d_parent = &test_dentry[0], |
| 390 | .d_name = QSTR_INIT(test_dentry[1].d_iname, 5), |
| 391 | .d_iname = "bravo" }, |
| 392 | { .d_parent = &test_dentry[1], |
| 393 | .d_name = QSTR_INIT(test_dentry[2].d_iname, 4), |
| 394 | .d_iname = "alfa" }, |
| 395 | { .d_parent = &test_dentry[2], |
| 396 | .d_name = QSTR_INIT(test_dentry[3].d_iname, 5), |
| 397 | .d_iname = "romeo" }, |
| 398 | }; |
| 399 | |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 400 | static void __init |
| 401 | dentry(void) |
| 402 | { |
Rasmus Villemoes | 251c723 | 2016-01-15 16:59:09 -0800 | [diff] [blame] | 403 | test("foo", "%pd", &test_dentry[0]); |
| 404 | test("foo", "%pd2", &test_dentry[0]); |
| 405 | |
| 406 | test("romeo", "%pd", &test_dentry[3]); |
| 407 | test("alfa/romeo", "%pd2", &test_dentry[3]); |
| 408 | test("bravo/alfa/romeo", "%pd3", &test_dentry[3]); |
| 409 | test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]); |
| 410 | test("/bravo/alfa", "%pd4", &test_dentry[2]); |
| 411 | |
| 412 | test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]); |
| 413 | test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void __init |
| 417 | struct_va_format(void) |
| 418 | { |
| 419 | } |
| 420 | |
| 421 | static void __init |
| 422 | struct_clk(void) |
| 423 | { |
| 424 | } |
| 425 | |
| 426 | static void __init |
Rasmus Villemoes | 857cca4 | 2016-01-15 16:59:06 -0800 | [diff] [blame] | 427 | large_bitmap(void) |
| 428 | { |
| 429 | const int nbits = 1 << 16; |
| 430 | unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL); |
| 431 | if (!bits) |
| 432 | return; |
| 433 | |
| 434 | bitmap_set(bits, 1, 20); |
| 435 | bitmap_set(bits, 60000, 15); |
| 436 | test("1-20,60000-60014", "%*pbl", nbits, bits); |
| 437 | kfree(bits); |
| 438 | } |
| 439 | |
| 440 | static void __init |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 441 | bitmap(void) |
| 442 | { |
| 443 | DECLARE_BITMAP(bits, 20); |
| 444 | const int primes[] = {2,3,5,7,11,13,17,19}; |
| 445 | int i; |
| 446 | |
| 447 | bitmap_zero(bits, 20); |
| 448 | test("00000|00000", "%20pb|%*pb", bits, 20, bits); |
| 449 | test("|", "%20pbl|%*pbl", bits, 20, bits); |
| 450 | |
| 451 | for (i = 0; i < ARRAY_SIZE(primes); ++i) |
| 452 | set_bit(primes[i], bits); |
| 453 | test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits); |
| 454 | test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits); |
| 455 | |
| 456 | bitmap_fill(bits, 20); |
| 457 | test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); |
| 458 | test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); |
Rasmus Villemoes | 857cca4 | 2016-01-15 16:59:06 -0800 | [diff] [blame] | 459 | |
| 460 | large_bitmap(); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static void __init |
| 464 | netdev_features(void) |
| 465 | { |
| 466 | } |
| 467 | |
| 468 | static void __init |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 469 | flags(void) |
| 470 | { |
| 471 | unsigned long flags; |
| 472 | gfp_t gfp; |
| 473 | char *cmp_buffer; |
| 474 | |
| 475 | flags = 0; |
| 476 | test("", "%pGp", &flags); |
| 477 | |
| 478 | /* Page flags should filter the zone id */ |
| 479 | flags = 1UL << NR_PAGEFLAGS; |
| 480 | test("", "%pGp", &flags); |
| 481 | |
| 482 | flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru |
| 483 | | 1UL << PG_active | 1UL << PG_swapbacked; |
| 484 | test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags); |
| 485 | |
| 486 | |
| 487 | flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC |
| 488 | | VM_DENYWRITE; |
| 489 | test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags); |
| 490 | |
| 491 | gfp = GFP_TRANSHUGE; |
| 492 | test("GFP_TRANSHUGE", "%pGg", &gfp); |
| 493 | |
| 494 | gfp = GFP_ATOMIC|__GFP_DMA; |
| 495 | test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp); |
| 496 | |
| 497 | gfp = __GFP_ATOMIC; |
| 498 | test("__GFP_ATOMIC", "%pGg", &gfp); |
| 499 | |
| 500 | cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); |
| 501 | if (!cmp_buffer) |
| 502 | return; |
| 503 | |
| 504 | /* Any flags not translated by the table should remain numeric */ |
| 505 | gfp = ~__GFP_BITS_MASK; |
| 506 | snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp); |
| 507 | test(cmp_buffer, "%pGg", &gfp); |
| 508 | |
| 509 | snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx", |
| 510 | (unsigned long) gfp); |
| 511 | gfp |= __GFP_ATOMIC; |
| 512 | test(cmp_buffer, "%pGg", &gfp); |
| 513 | |
| 514 | kfree(cmp_buffer); |
| 515 | } |
| 516 | |
| 517 | static void __init |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 518 | test_pointer(void) |
| 519 | { |
| 520 | plain(); |
| 521 | symbol_ptr(); |
| 522 | kernel_ptr(); |
| 523 | struct_resource(); |
| 524 | addr(); |
| 525 | escaped_str(); |
| 526 | hex_string(); |
| 527 | mac(); |
| 528 | ip(); |
| 529 | uuid(); |
| 530 | dentry(); |
| 531 | struct_va_format(); |
| 532 | struct_clk(); |
| 533 | bitmap(); |
| 534 | netdev_features(); |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 535 | flags(); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int __init |
| 539 | test_printf_init(void) |
| 540 | { |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 541 | alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); |
| 542 | if (!alloced_buffer) |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 543 | return -ENOMEM; |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 544 | test_buffer = alloced_buffer + PAD_SIZE; |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 545 | |
| 546 | test_basic(); |
| 547 | test_number(); |
| 548 | test_string(); |
| 549 | test_pointer(); |
| 550 | |
Rasmus Villemoes | 331e4de | 2016-01-15 16:58:53 -0800 | [diff] [blame] | 551 | kfree(alloced_buffer); |
Rasmus Villemoes | 707cc72 | 2015-11-06 16:30:29 -0800 | [diff] [blame] | 552 | |
| 553 | if (failed_tests == 0) |
| 554 | pr_info("all %u tests passed\n", total_tests); |
| 555 | else |
| 556 | pr_warn("failed %u out of %u tests\n", failed_tests, total_tests); |
| 557 | |
| 558 | return failed_tests ? -EINVAL : 0; |
| 559 | } |
| 560 | |
| 561 | module_init(test_printf_init); |
| 562 | |
| 563 | MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>"); |
| 564 | MODULE_LICENSE("GPL"); |