at commit 205cccd0
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Test cases for printf facility. |
| 4 | */ |
| 5 | |
| 6 | #include <kunit/test.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/printk.h> |
| 10 | #include <linux/random.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sprintf.h> |
| 14 | #include <linux/string.h> |
| 15 | |
| 16 | #include <linux/bitmap.h> |
| 17 | #include <linux/dcache.h> |
| 18 | #include <linux/socket.h> |
| 19 | #include <linux/in.h> |
| 20 | #include <linux/in6.h> |
| 21 | |
| 22 | #include <linux/gfp.h> |
| 23 | #include <linux/mm.h> |
| 24 | |
| 25 | #include <linux/property.h> |
| 26 | |
| 27 | #define BUF_SIZE 256 |
| 28 | #define PAD_SIZE 16 |
| 29 | #define FILL_CHAR '$' |
| 30 | |
| 31 | #define NOWARN(option, comment, block) \ |
| 32 | __diag_push(); \ |
| 33 | __diag_ignore_all(#option, comment); \ |
| 34 | block \ |
| 35 | __diag_pop(); |
| 36 | |
| 37 | static unsigned int total_tests; |
| 38 | |
| 39 | static char *test_buffer; |
| 40 | static char *alloced_buffer; |
| 41 | |
| 42 | static void __printf(7, 0) |
| 43 | do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect, |
| 44 | int elen, const char *fmt, va_list ap) |
| 45 | { |
| 46 | va_list aq; |
| 47 | int ret, written; |
| 48 | |
| 49 | total_tests++; |
| 50 | |
| 51 | memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE); |
| 52 | va_copy(aq, ap); |
| 53 | ret = vsnprintf(test_buffer, bufsize, fmt, aq); |
| 54 | va_end(aq); |
| 55 | |
| 56 | if (ret != elen) { |
| 57 | KUNIT_FAIL(kunittest, |
| 58 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n", |
| 59 | file, line, bufsize, fmt, ret, elen); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) { |
| 64 | KUNIT_FAIL(kunittest, |
| 65 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", |
| 66 | file, line, bufsize, fmt); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (!bufsize) { |
| 71 | if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) { |
| 72 | KUNIT_FAIL(kunittest, |
| 73 | "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", |
| 74 | file, line, fmt); |
| 75 | } |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | written = min(bufsize-1, elen); |
| 80 | if (test_buffer[written]) { |
| 81 | KUNIT_FAIL(kunittest, |
| 82 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n", |
| 83 | file, line, bufsize, fmt); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { |
| 88 | KUNIT_FAIL(kunittest, |
| 89 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", |
| 90 | file, line, bufsize, fmt); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) { |
| 95 | KUNIT_FAIL(kunittest, |
| 96 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", |
| 97 | file, line, bufsize, fmt); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (memcmp(test_buffer, expect, written)) { |
| 102 | KUNIT_FAIL(kunittest, |
| 103 | "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", |
| 104 | file, line, bufsize, fmt, test_buffer, written, expect); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void __printf(6, 7) |
| 110 | __test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen, |
| 111 | const char *fmt, ...) |
| 112 | { |
| 113 | va_list ap; |
| 114 | int rand; |
| 115 | char *p; |
| 116 | |
| 117 | if (elen >= BUF_SIZE) { |
| 118 | KUNIT_FAIL(kunittest, |
| 119 | "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n", |
| 120 | file, line, elen, BUF_SIZE, fmt); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | va_start(ap, fmt); |
| 125 | |
| 126 | /* |
| 127 | * Every fmt+args is subjected to four tests: Three where we |
| 128 | * tell vsnprintf varying buffer sizes (plenty, not quite |
| 129 | * enough and 0), and then we also test that kvasprintf would |
| 130 | * be able to print it as expected. |
| 131 | */ |
| 132 | do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap); |
| 133 | rand = get_random_u32_inclusive(1, elen + 1); |
| 134 | /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ |
| 135 | do_test(kunittest, file, line, rand, expect, elen, fmt, ap); |
| 136 | do_test(kunittest, file, line, 0, expect, elen, fmt, ap); |
| 137 | |
| 138 | p = kvasprintf(GFP_KERNEL, fmt, ap); |
| 139 | if (p) { |
| 140 | total_tests++; |
| 141 | if (memcmp(p, expect, elen+1)) { |
| 142 | KUNIT_FAIL(kunittest, |
| 143 | "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", |
| 144 | file, line, fmt, p, expect); |
| 145 | } |
| 146 | kfree(p); |
| 147 | } |
| 148 | va_end(ap); |
| 149 | } |
| 150 | |
| 151 | #define test(expect, fmt, ...) \ |
| 152 | __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__) |
| 153 | |
| 154 | static void |
| 155 | test_basic(struct kunit *kunittest) |
| 156 | { |
| 157 | /* Work around annoying "warning: zero-length gnu_printf format string". */ |
| 158 | char nul = '\0'; |
| 159 | |
| 160 | test("", &nul); |
| 161 | test("100%", "100%%"); |
| 162 | test("xxx%yyy", "xxx%cyyy", '%'); |
| 163 | __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0'); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | test_number(struct kunit *kunittest) |
| 168 | { |
| 169 | test("0x1234abcd ", "%#-12x", 0x1234abcd); |
| 170 | test(" 0x1234abcd", "%#12x", 0x1234abcd); |
| 171 | test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234); |
| 172 | NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", { |
| 173 | test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); |
| 174 | test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); |
| 175 | test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); |
| 176 | }) |
| 177 | /* |
| 178 | * POSIX/C99: »The result of converting zero with an explicit |
| 179 | * precision of zero shall be no characters.« Hence the output |
| 180 | * from the below test should really be "00|0||| ". However, |
| 181 | * the kernel's printf also produces a single 0 in that |
| 182 | * case. This test case simply documents the current |
| 183 | * behaviour. |
| 184 | */ |
| 185 | test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | test_string(struct kunit *kunittest) |
| 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"); |
| 194 | test("1234 ", "%-10.4s", "123456"); |
| 195 | test(" 1234", "%10.4s", "123456"); |
| 196 | /* |
| 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. |
| 208 | */ |
| 209 | test(" ", "%4.*s", -5, "123456"); |
| 210 | test("123456", "%.s", "123456"); |
| 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 | #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */ |
| 216 | |
| 217 | #if BITS_PER_LONG == 64 |
| 218 | |
| 219 | #define PTR_WIDTH 16 |
| 220 | #define PTR ((void *)0xffff0123456789abUL) |
| 221 | #define PTR_STR "ffff0123456789ab" |
| 222 | #define PTR_VAL_NO_CRNG "(____ptrval____)" |
| 223 | #define ZEROS "00000000" /* hex 32 zero bits */ |
| 224 | #define ONES "ffffffff" /* hex 32 one bits */ |
| 225 | |
| 226 | #else |
| 227 | |
| 228 | #define PTR_WIDTH 8 |
| 229 | #define PTR ((void *)0x456789ab) |
| 230 | #define PTR_STR "456789ab" |
| 231 | #define PTR_VAL_NO_CRNG "(ptrval)" |
| 232 | #define ZEROS "" |
| 233 | #define ONES "" |
| 234 | |
| 235 | #endif /* BITS_PER_LONG == 64 */ |
| 236 | |
| 237 | static void |
| 238 | plain_hash_to_buffer(struct kunit *kunittest, const void *p, char *buf, size_t len) |
| 239 | { |
| 240 | KUNIT_ASSERT_EQ(kunittest, snprintf(buf, len, "%p", p), PTR_WIDTH); |
| 241 | |
| 242 | if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { |
| 243 | kunit_skip(kunittest, |
| 244 | "crng possibly not yet initialized. plain 'p' buffer contains \"%s\"\n", |
| 245 | PTR_VAL_NO_CRNG); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | hash_pointer(struct kunit *kunittest) |
| 251 | { |
| 252 | if (no_hash_pointers) |
| 253 | kunit_skip(kunittest, "hash pointers disabled"); |
| 254 | |
| 255 | char buf[PLAIN_BUF_SIZE]; |
| 256 | |
| 257 | plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); |
| 258 | |
| 259 | /* |
| 260 | * The hash of %p is unpredictable, therefore test() cannot be used. |
| 261 | * |
| 262 | * Instead verify that the first 32 bits are zeros on a 64-bit system |
| 263 | * and that the non-hashed value is not printed. |
| 264 | */ |
| 265 | |
| 266 | KUNIT_EXPECT_MEMEQ(kunittest, buf, ZEROS, strlen(ZEROS)); |
| 267 | KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * This is a macro so that the compiler can compare its arguments to the |
| 272 | * __printf() attribute on __test(). This cannot be a function with a __printf() |
| 273 | * attribute because GCC requires __printf() functions to be variadic. |
| 274 | */ |
| 275 | #define test_hashed(kunittest, fmt, p) \ |
| 276 | do { \ |
| 277 | char buf[PLAIN_BUF_SIZE]; \ |
| 278 | plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ |
| 279 | test(buf, fmt, p); \ |
| 280 | } while (0) |
| 281 | |
| 282 | /* |
| 283 | * NULL pointers aren't hashed. |
| 284 | */ |
| 285 | static void |
| 286 | null_pointer(struct kunit *kunittest) |
| 287 | { |
| 288 | test(ZEROS "00000000", "%p", NULL); |
| 289 | test(ZEROS "00000000", "%px", NULL); |
| 290 | test("(null)", "%pE", NULL); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Error pointers aren't hashed. |
| 295 | */ |
| 296 | static void |
| 297 | error_pointer(struct kunit *kunittest) |
| 298 | { |
| 299 | test(ONES "fffffff5", "%p", ERR_PTR(-11)); |
| 300 | test(ONES "fffffff5", "%px", ERR_PTR(-11)); |
| 301 | test("(efault)", "%pE", ERR_PTR(-11)); |
| 302 | } |
| 303 | |
| 304 | #define PTR_INVALID ((void *)0x000000ab) |
| 305 | |
| 306 | static void |
| 307 | invalid_pointer(struct kunit *kunittest) |
| 308 | { |
| 309 | test_hashed(kunittest, "%p", PTR_INVALID); |
| 310 | test(ZEROS "000000ab", "%px", PTR_INVALID); |
| 311 | test("(efault)", "%pE", PTR_INVALID); |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | symbol_ptr(struct kunit *kunittest) |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | static void |
| 320 | kernel_ptr(struct kunit *kunittest) |
| 321 | { |
| 322 | switch (kptr_restrict) { |
| 323 | case 0: |
| 324 | if (no_hash_pointers) { |
| 325 | test(PTR_STR, "%pK", PTR); |
| 326 | } else { |
| 327 | char buf[PLAIN_BUF_SIZE]; |
| 328 | |
| 329 | plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); |
| 330 | /* %pK behaves the same as hashing */ |
| 331 | test(buf, "%pK", PTR); |
| 332 | } |
| 333 | break; |
| 334 | case 1: |
| 335 | /* The KUnit kthread has all capabilities, including CAP_SYSLOG */ |
| 336 | test(PTR_STR, "%pK", PTR); |
| 337 | break; |
| 338 | case 2: |
| 339 | default: |
| 340 | test(ZEROS "00000000", "%pK", PTR); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void |
| 346 | struct_resource(struct kunit *kunittest) |
| 347 | { |
| 348 | struct resource test_resource = { |
| 349 | .start = 0xc0ffee00, |
| 350 | .end = 0xc0ffee00, |
| 351 | .flags = IORESOURCE_MEM, |
| 352 | }; |
| 353 | |
| 354 | test("[mem 0xc0ffee00 flags 0x200]", |
| 355 | "%pr", &test_resource); |
| 356 | |
| 357 | test_resource = (struct resource) { |
| 358 | .start = 0xc0ffee, |
| 359 | .end = 0xba5eba11, |
| 360 | .flags = IORESOURCE_MEM, |
| 361 | }; |
| 362 | test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]", |
| 363 | "%pr", &test_resource); |
| 364 | |
| 365 | test_resource = (struct resource) { |
| 366 | .start = 0xba5eba11, |
| 367 | .end = 0xc0ffee, |
| 368 | .flags = IORESOURCE_MEM, |
| 369 | }; |
| 370 | test("[mem 0xba5eba11-0x00c0ffee flags 0x200]", |
| 371 | "%pr", &test_resource); |
| 372 | |
| 373 | test_resource = (struct resource) { |
| 374 | .start = 0xba5eba11, |
| 375 | .end = 0xba5eca11, |
| 376 | .flags = IORESOURCE_MEM, |
| 377 | }; |
| 378 | |
| 379 | test("[mem 0xba5eba11-0xba5eca11 flags 0x200]", |
| 380 | "%pr", &test_resource); |
| 381 | |
| 382 | test_resource = (struct resource) { |
| 383 | .start = 0xba11, |
| 384 | .end = 0xca10, |
| 385 | .flags = IORESOURCE_IO | |
| 386 | IORESOURCE_DISABLED | |
| 387 | IORESOURCE_UNSET, |
| 388 | }; |
| 389 | |
| 390 | test("[io size 0x1000 disabled]", |
| 391 | "%pR", &test_resource); |
| 392 | } |
| 393 | |
| 394 | static void |
| 395 | struct_range(struct kunit *kunittest) |
| 396 | { |
| 397 | struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11, |
| 398 | 0xc0ffee00ba5eba11); |
| 399 | test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range); |
| 400 | |
| 401 | test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11); |
| 402 | test("[range 0x0000000000c0ffee-0x00000000ba5eba11]", |
| 403 | "%pra", &test_range); |
| 404 | |
| 405 | test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee); |
| 406 | test("[range 0x00000000ba5eba11-0x0000000000c0ffee]", |
| 407 | "%pra", &test_range); |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | addr(struct kunit *kunittest) |
| 412 | { |
| 413 | } |
| 414 | |
| 415 | static void |
| 416 | escaped_str(struct kunit *kunittest) |
| 417 | { |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | hex_string(struct kunit *kunittest) |
| 422 | { |
| 423 | const char buf[3] = {0xc0, 0xff, 0xee}; |
| 424 | |
| 425 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", |
| 426 | "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); |
| 427 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", |
| 428 | "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf); |
| 429 | } |
| 430 | |
| 431 | static void |
| 432 | mac(struct kunit *kunittest) |
| 433 | { |
| 434 | const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; |
| 435 | |
| 436 | test("2d:48:d6:fc:7a:05", "%pM", addr); |
| 437 | test("05:7a:fc:d6:48:2d", "%pMR", addr); |
| 438 | test("05:7A:FC:D6:48:2D", "%pMRU", addr); |
| 439 | test("2d-48-d6-fc-7a-05", "%pMF", addr); |
| 440 | test("2d48d6fc7a05", "%pm", addr); |
| 441 | test("2D48D6FC7A05", "%pmU", addr); |
| 442 | test("057afcd6482d", "%pmR", addr); |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | ip4(struct kunit *kunittest) |
| 447 | { |
| 448 | struct sockaddr_in sa; |
| 449 | |
| 450 | sa.sin_family = AF_INET; |
| 451 | sa.sin_port = cpu_to_be16(12345); |
| 452 | sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); |
| 453 | |
| 454 | test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); |
| 455 | test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); |
| 456 | sa.sin_addr.s_addr = cpu_to_be32(0x01020304); |
| 457 | test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa); |
| 458 | } |
| 459 | |
| 460 | static void |
| 461 | ip6(struct kunit *kunittest) |
| 462 | { |
| 463 | const struct in6_addr addr = { |
| 464 | .s6_addr = { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, |
| 465 | 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } |
| 466 | }; |
| 467 | const struct in6_addr single_zero = { |
| 468 | .s6_addr = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, |
| 469 | 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } |
| 470 | }; |
| 471 | struct sockaddr_in6 sa = { |
| 472 | .sin6_family = AF_INET6, |
| 473 | .sin6_port = cpu_to_be16(12345), |
| 474 | .sin6_addr = addr, |
| 475 | }; |
| 476 | |
| 477 | test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", |
| 478 | "%pi6|%pI6", &addr, &addr); |
| 479 | test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", |
| 480 | "%piS|%pIS", &sa, &sa); |
| 481 | test("1:2:3:4:5:6:7:8", "%pI6c", &addr); |
| 482 | test("1:0:3:4:5:6:7:8", "%pI6c", &single_zero); |
| 483 | test("[1:2:3:4:5:6:7:8]:12345", "%pISpc", &sa); |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | uuid(struct kunit *kunittest) |
| 488 | { |
| 489 | const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, |
| 490 | 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; |
| 491 | |
| 492 | test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); |
| 493 | test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); |
| 494 | test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); |
| 495 | test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); |
| 496 | } |
| 497 | |
| 498 | static struct dentry test_dentry[4] = { |
| 499 | { .d_parent = &test_dentry[0], |
| 500 | .d_name = QSTR_INIT(test_dentry[0].d_iname, 3), |
| 501 | .d_iname = "foo" }, |
| 502 | { .d_parent = &test_dentry[0], |
| 503 | .d_name = QSTR_INIT(test_dentry[1].d_iname, 5), |
| 504 | .d_iname = "bravo" }, |
| 505 | { .d_parent = &test_dentry[1], |
| 506 | .d_name = QSTR_INIT(test_dentry[2].d_iname, 4), |
| 507 | .d_iname = "alfa" }, |
| 508 | { .d_parent = &test_dentry[2], |
| 509 | .d_name = QSTR_INIT(test_dentry[3].d_iname, 5), |
| 510 | .d_iname = "romeo" }, |
| 511 | }; |
| 512 | |
| 513 | static void |
| 514 | dentry(struct kunit *kunittest) |
| 515 | { |
| 516 | test("foo", "%pd", &test_dentry[0]); |
| 517 | test("foo", "%pd2", &test_dentry[0]); |
| 518 | |
| 519 | test("(null)", "%pd", NULL); |
| 520 | test("(efault)", "%pd", PTR_INVALID); |
| 521 | test("(null)", "%pD", NULL); |
| 522 | test("(efault)", "%pD", PTR_INVALID); |
| 523 | |
| 524 | test("romeo", "%pd", &test_dentry[3]); |
| 525 | test("alfa/romeo", "%pd2", &test_dentry[3]); |
| 526 | test("bravo/alfa/romeo", "%pd3", &test_dentry[3]); |
| 527 | test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]); |
| 528 | test("/bravo/alfa", "%pd4", &test_dentry[2]); |
| 529 | |
| 530 | test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]); |
| 531 | test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]); |
| 532 | } |
| 533 | |
| 534 | static void |
| 535 | struct_va_format(struct kunit *kunittest) |
| 536 | { |
| 537 | } |
| 538 | |
| 539 | static void |
| 540 | time_and_date(struct kunit *kunittest) |
| 541 | { |
| 542 | /* 1543210543 */ |
| 543 | const struct rtc_time tm = { |
| 544 | .tm_sec = 43, |
| 545 | .tm_min = 35, |
| 546 | .tm_hour = 5, |
| 547 | .tm_mday = 26, |
| 548 | .tm_mon = 10, |
| 549 | .tm_year = 118, |
| 550 | }; |
| 551 | /* 2019-01-04T15:32:23 */ |
| 552 | time64_t t = 1546615943; |
| 553 | struct timespec64 ts = { .tv_sec = t, .tv_nsec = 11235813 }; |
| 554 | |
| 555 | test("(%pt?)", "%pt", &tm); |
| 556 | test("2018-11-26T05:35:43", "%ptR", &tm); |
| 557 | test("0118-10-26T05:35:43", "%ptRr", &tm); |
| 558 | test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm); |
| 559 | test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm); |
| 560 | test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm); |
| 561 | test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm); |
| 562 | |
| 563 | test("2019-01-04T15:32:23", "%ptT", &t); |
| 564 | test("0119-00-04T15:32:23", "%ptTr", &t); |
| 565 | test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t); |
| 566 | test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t); |
| 567 | |
| 568 | test("2019-01-04 15:32:23", "%ptTs", &t); |
| 569 | test("0119-00-04 15:32:23", "%ptTsr", &t); |
| 570 | test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t); |
| 571 | test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t); |
| 572 | |
| 573 | test("2019-01-04T15:32:23.011235813", "%ptS", &ts); |
| 574 | test("1546615943.011235813", "%ptSp", &ts); |
| 575 | } |
| 576 | |
| 577 | static void |
| 578 | struct_clk(struct kunit *kunittest) |
| 579 | { |
| 580 | } |
| 581 | |
| 582 | static void |
| 583 | large_bitmap(struct kunit *kunittest) |
| 584 | { |
| 585 | const int nbits = 1 << 16; |
| 586 | unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL); |
| 587 | if (!bits) |
| 588 | return; |
| 589 | |
| 590 | bitmap_set(bits, 1, 20); |
| 591 | bitmap_set(bits, 60000, 15); |
| 592 | test("1-20,60000-60014", "%*pbl", nbits, bits); |
| 593 | bitmap_free(bits); |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | bitmap(struct kunit *kunittest) |
| 598 | { |
| 599 | DECLARE_BITMAP(bits, 20); |
| 600 | const int primes[] = {2,3,5,7,11,13,17,19}; |
| 601 | int i; |
| 602 | |
| 603 | bitmap_zero(bits, 20); |
| 604 | test("00000|00000", "%20pb|%*pb", bits, 20, bits); |
| 605 | test("|", "%20pbl|%*pbl", bits, 20, bits); |
| 606 | |
| 607 | for (i = 0; i < ARRAY_SIZE(primes); ++i) |
| 608 | set_bit(primes[i], bits); |
| 609 | test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits); |
| 610 | test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits); |
| 611 | |
| 612 | bitmap_fill(bits, 20); |
| 613 | test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); |
| 614 | test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); |
| 615 | |
| 616 | large_bitmap(kunittest); |
| 617 | } |
| 618 | |
| 619 | static void |
| 620 | netdev_features(struct kunit *kunittest) |
| 621 | { |
| 622 | } |
| 623 | |
| 624 | struct page_flags_test { |
| 625 | int width; |
| 626 | int shift; |
| 627 | int mask; |
| 628 | const char *fmt; |
| 629 | const char *name; |
| 630 | }; |
| 631 | |
| 632 | static const struct page_flags_test pft[] = { |
| 633 | {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, |
| 634 | "%d", "section"}, |
| 635 | {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, |
| 636 | "%d", "node"}, |
| 637 | {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, |
| 638 | "%d", "zone"}, |
| 639 | {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, |
| 640 | "%#x", "lastcpupid"}, |
| 641 | {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, |
| 642 | "%#x", "kasantag"}, |
| 643 | }; |
| 644 | |
| 645 | static void |
| 646 | page_flags_test(struct kunit *kunittest, int section, int node, int zone, |
| 647 | int last_cpupid, int kasan_tag, unsigned long flags, const char *name, |
| 648 | char *cmp_buf) |
| 649 | { |
| 650 | unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; |
| 651 | unsigned long size; |
| 652 | bool append = false; |
| 653 | int i; |
| 654 | |
| 655 | for (i = 0; i < ARRAY_SIZE(values); i++) |
| 656 | flags |= (values[i] & pft[i].mask) << pft[i].shift; |
| 657 | |
| 658 | size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags); |
| 659 | if (flags & PAGEFLAGS_MASK) { |
| 660 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); |
| 661 | append = true; |
| 662 | } |
| 663 | |
| 664 | for (i = 0; i < ARRAY_SIZE(pft); i++) { |
| 665 | if (!pft[i].width) |
| 666 | continue; |
| 667 | |
| 668 | if (append) |
| 669 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); |
| 670 | |
| 671 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", |
| 672 | pft[i].name); |
| 673 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, |
| 674 | values[i] & pft[i].mask); |
| 675 | append = true; |
| 676 | } |
| 677 | |
| 678 | snprintf(cmp_buf + size, BUF_SIZE - size, ")"); |
| 679 | |
| 680 | test(cmp_buf, "%pGp", &flags); |
| 681 | } |
| 682 | |
| 683 | static void |
| 684 | flags(struct kunit *kunittest) |
| 685 | { |
| 686 | unsigned long flags; |
| 687 | char *cmp_buffer; |
| 688 | gfp_t gfp; |
| 689 | |
| 690 | cmp_buffer = kunit_kmalloc(kunittest, BUF_SIZE, GFP_KERNEL); |
| 691 | KUNIT_ASSERT_NOT_NULL(kunittest, cmp_buffer); |
| 692 | |
| 693 | flags = 0; |
| 694 | page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); |
| 695 | |
| 696 | flags = 1UL << NR_PAGEFLAGS; |
| 697 | page_flags_test(kunittest, 0, 0, 0, 0, 0, flags, "", cmp_buffer); |
| 698 | |
| 699 | flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru |
| 700 | | 1UL << PG_active | 1UL << PG_swapbacked; |
| 701 | page_flags_test(kunittest, 1, 1, 1, 0x1fffff, 1, flags, |
| 702 | "uptodate|dirty|lru|active|swapbacked", |
| 703 | cmp_buffer); |
| 704 | |
| 705 | flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
| 706 | test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags); |
| 707 | |
| 708 | gfp = GFP_TRANSHUGE; |
| 709 | test("GFP_TRANSHUGE", "%pGg", &gfp); |
| 710 | |
| 711 | gfp = GFP_ATOMIC|__GFP_DMA; |
| 712 | test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp); |
| 713 | |
| 714 | gfp = __GFP_HIGH; |
| 715 | test("__GFP_HIGH", "%pGg", &gfp); |
| 716 | |
| 717 | /* Any flags not translated by the table should remain numeric */ |
| 718 | gfp = ~__GFP_BITS_MASK; |
| 719 | snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp); |
| 720 | test(cmp_buffer, "%pGg", &gfp); |
| 721 | |
| 722 | snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx", |
| 723 | (unsigned long) gfp); |
| 724 | gfp |= __GFP_HIGH; |
| 725 | test(cmp_buffer, "%pGg", &gfp); |
| 726 | } |
| 727 | |
| 728 | static void fwnode_pointer(struct kunit *kunittest) |
| 729 | { |
| 730 | const struct software_node first = { .name = "first" }; |
| 731 | const struct software_node second = { .name = "second", .parent = &first }; |
| 732 | const struct software_node third = { .name = "third", .parent = &second }; |
| 733 | const struct software_node *group[] = { &first, &second, &third, NULL }; |
| 734 | const char * const full_name_second = "first/second"; |
| 735 | const char * const full_name_third = "first/second/third"; |
| 736 | const char * const second_name = "second"; |
| 737 | const char * const third_name = "third"; |
| 738 | int rval; |
| 739 | |
| 740 | rval = software_node_register_node_group(group); |
| 741 | if (rval) { |
| 742 | kunit_skip(kunittest, "cannot register softnodes; rval %d\n", rval); |
| 743 | } |
| 744 | |
| 745 | test(full_name_second, "%pfw", software_node_fwnode(&second)); |
| 746 | test(full_name_third, "%pfw", software_node_fwnode(&third)); |
| 747 | test(full_name_third, "%pfwf", software_node_fwnode(&third)); |
| 748 | test(second_name, "%pfwP", software_node_fwnode(&second)); |
| 749 | test(third_name, "%pfwP", software_node_fwnode(&third)); |
| 750 | |
| 751 | software_node_unregister_node_group(group); |
| 752 | } |
| 753 | |
| 754 | struct fourcc_struct { |
| 755 | u32 code; |
| 756 | const char *str; |
| 757 | }; |
| 758 | |
| 759 | static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc, |
| 760 | size_t n, const char *fmt) |
| 761 | { |
| 762 | size_t i; |
| 763 | |
| 764 | for (i = 0; i < n; i++) |
| 765 | test(fc[i].str, fmt, &fc[i].code); |
| 766 | } |
| 767 | |
| 768 | static void fourcc_pointer(struct kunit *kunittest) |
| 769 | { |
| 770 | static const struct fourcc_struct try_cc[] = { |
| 771 | { 0x3231564e, "NV12 little-endian (0x3231564e)", }, |
| 772 | { 0xb231564e, "NV12 big-endian (0xb231564e)", }, |
| 773 | { 0x10111213, ".... little-endian (0x10111213)", }, |
| 774 | { 0x20303159, "Y10 little-endian (0x20303159)", }, |
| 775 | }; |
| 776 | static const struct fourcc_struct try_ch[] = { |
| 777 | { 0x41424344, "ABCD (0x41424344)", }, |
| 778 | }; |
| 779 | static const struct fourcc_struct try_chR[] = { |
| 780 | { 0x41424344, "DCBA (0x44434241)", }, |
| 781 | }; |
| 782 | static const struct fourcc_struct try_cl[] = { |
| 783 | { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", }, |
| 784 | }; |
| 785 | static const struct fourcc_struct try_cb[] = { |
| 786 | { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", }, |
| 787 | }; |
| 788 | |
| 789 | fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc"); |
| 790 | fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch"); |
| 791 | fourcc_pointer_test(kunittest, try_chR, ARRAY_SIZE(try_chR), "%p4chR"); |
| 792 | fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl"); |
| 793 | fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb"); |
| 794 | } |
| 795 | |
| 796 | static void |
| 797 | errptr(struct kunit *kunittest) |
| 798 | { |
| 799 | test("-1234", "%pe", ERR_PTR(-1234)); |
| 800 | |
| 801 | /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */ |
| 802 | BUILD_BUG_ON(IS_ERR(PTR)); |
| 803 | test_hashed(kunittest, "%pe", PTR); |
| 804 | |
| 805 | #ifdef CONFIG_SYMBOLIC_ERRNAME |
| 806 | test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK)); |
| 807 | test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN)); |
| 808 | BUILD_BUG_ON(EAGAIN != EWOULDBLOCK); |
| 809 | test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK)); |
| 810 | test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO)); |
| 811 | test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO)); |
| 812 | test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER)); |
| 813 | #endif |
| 814 | } |
| 815 | |
| 816 | static int printf_suite_init(struct kunit_suite *suite) |
| 817 | { |
| 818 | total_tests = 0; |
| 819 | |
| 820 | alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); |
| 821 | if (!alloced_buffer) |
| 822 | return -ENOMEM; |
| 823 | test_buffer = alloced_buffer + PAD_SIZE; |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static void printf_suite_exit(struct kunit_suite *suite) |
| 829 | { |
| 830 | kfree(alloced_buffer); |
| 831 | |
| 832 | kunit_info(suite, "ran %u tests\n", total_tests); |
| 833 | } |
| 834 | |
| 835 | static struct kunit_case printf_test_cases[] = { |
| 836 | KUNIT_CASE(test_basic), |
| 837 | KUNIT_CASE(test_number), |
| 838 | KUNIT_CASE(test_string), |
| 839 | KUNIT_CASE(hash_pointer), |
| 840 | KUNIT_CASE(null_pointer), |
| 841 | KUNIT_CASE(error_pointer), |
| 842 | KUNIT_CASE(invalid_pointer), |
| 843 | KUNIT_CASE(symbol_ptr), |
| 844 | KUNIT_CASE(kernel_ptr), |
| 845 | KUNIT_CASE(struct_resource), |
| 846 | KUNIT_CASE(struct_range), |
| 847 | KUNIT_CASE(addr), |
| 848 | KUNIT_CASE(escaped_str), |
| 849 | KUNIT_CASE(hex_string), |
| 850 | KUNIT_CASE(mac), |
| 851 | KUNIT_CASE(ip4), |
| 852 | KUNIT_CASE(ip6), |
| 853 | KUNIT_CASE(uuid), |
| 854 | KUNIT_CASE(dentry), |
| 855 | KUNIT_CASE(struct_va_format), |
| 856 | KUNIT_CASE(time_and_date), |
| 857 | KUNIT_CASE(struct_clk), |
| 858 | KUNIT_CASE(bitmap), |
| 859 | KUNIT_CASE(netdev_features), |
| 860 | KUNIT_CASE(flags), |
| 861 | KUNIT_CASE(errptr), |
| 862 | KUNIT_CASE(fwnode_pointer), |
| 863 | KUNIT_CASE(fourcc_pointer), |
| 864 | {} |
| 865 | }; |
| 866 | |
| 867 | static struct kunit_suite printf_test_suite = { |
| 868 | .name = "printf", |
| 869 | .suite_init = printf_suite_init, |
| 870 | .suite_exit = printf_suite_exit, |
| 871 | .test_cases = printf_test_cases, |
| 872 | }; |
| 873 | |
| 874 | kunit_test_suite(printf_test_suite); |
| 875 | |
| 876 | MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>"); |
| 877 | MODULE_DESCRIPTION("Test cases for printf facility"); |
| 878 | MODULE_LICENSE("GPL"); |