// SPDX-License-Identifier: GPL-2.0-only/* * Test cases for printf facility. */ #include <kunit/test.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/printk.h>#include <linux/random.h>#include <linux/rtc.h>#include <linux/slab.h>#include <linux/sprintf.h>#include <linux/string.h> #include <linux/bitmap.h>#include <linux/dcache.h>#include <linux/socket.h>#include <linux/in.h>#include <linux/in6.h> #include <linux/gfp.h>#include <linux/mm.h> #include <linux/property.h> #define BUF_SIZE 256#define PAD_SIZE 16#define FILL_CHAR '$' #define NOWARN(option, comment, block) \ __diag_push(); \ __diag_ignore_all(#option, comment); \ block \ __diag_pop(); static unsigned int total_tests; static char *test_buffer;static char *alloced_buffer; static void __printf(7, 0)do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect, int elen, const char *fmt, va_list ap){ va_list aq; int ret, written; total_tests++; memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE); va_copy(aq, ap); ret = vsnprintf(test_buffer, bufsize, fmt, aq); va_end(aq); if (ret != elen) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n", file, line, bufsize, fmt, ret, elen); return; } if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", file, line, bufsize, fmt); return; } if (!bufsize) { if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", file, line, fmt); } return; } written = min(bufsize-1, elen); if (test_buffer[written]) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n", file, line, bufsize, fmt); return; } if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", file, line, bufsize, fmt); return; } if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", file, line, bufsize, fmt); return; } if (memcmp(test_buffer, expect, written)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", file, line, bufsize, fmt, test_buffer, written, expect); return; }} static void __printf(6, 7)__test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen, const char *fmt, ...){ va_list ap; int rand; char *p; if (elen >= BUF_SIZE) { KUNIT_FAIL(kunittest, "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n", file, line, elen, BUF_SIZE, fmt); return; } va_start(ap, fmt); /* * Every fmt+args is subjected to four tests: Three where we * tell vsnprintf varying buffer sizes (plenty, not quite * enough and 0), and then we also test that kvasprintf would * be able to print it as expected. */ do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap); rand = get_random_u32_inclusive(1, elen + 1); /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ do_test(kunittest, file, line, rand, expect, elen, fmt, ap); do_test(kunittest, file, line, 0, expect, elen, fmt, ap); p = kvasprintf(GFP_KERNEL, fmt, ap); if (p) { total_tests++; if (memcmp(p, expect, elen+1)) { KUNIT_FAIL(kunittest, "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", file, line, fmt, p, expect); } kfree(p); } va_end(ap);} #define test(expect, fmt, ...) \ __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__) static voidtest_basic(struct kunit *kunittest){ /* Work around annoying "warning: zero-length gnu_printf format string". */ char nul = '\0'; test("", &nul); test("100%", "100%%"); test("xxx%yyy", "xxx%cyyy", '%'); __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0');} static voidtest_number(struct kunit *kunittest){ test("0x1234abcd ", "%#-12x", 0x1234abcd); test(" 0x1234abcd", "%#12x", 0x1234abcd); test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234); NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", { test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); }) /* * POSIX/C99: »The result of converting zero with an explicit * precision of zero shall be no characters.« Hence the output * from the below test should really be "00|0||| ". However, * the kernel's printf also produces a single 0 in that * case. This test case simply documents the current * behaviour. */ test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);} static voidtest_string(struct kunit *kunittest){ test("", "%s%.0s", "", "123"); test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); test("1234 ", "%-10.4s", "123456"); test(" 1234", "%10.4s", "123456"); /* * POSIX and C99 say that a negative precision (which is only * possible to pass via a * argument) should be treated as if * the precision wasn't present, and that if the precision is * omitted (as in %.s), the precision should be taken to be * 0. However, the kernel's printf behave exactly opposite, * treating a negative precision as 0 and treating an omitted * precision specifier as if no precision was given. * * These test cases document the current behaviour; should * anyone ever feel the need to follow the standards more * closely, this can be revisited. */ test(" ", "%4.*s", -5, "123456"); test("123456", "%.s", "123456"); test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");} #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */ #if BITS_PER_LONG == 64 #define PTR_WIDTH 16#define PTR ((void *)0xffff0123456789abUL)#define PTR_STR "ffff0123456789ab"#define PTR_VAL_NO_CRNG "(____ptrval____)"#define ZEROS "00000000" /* hex 32 zero bits */#define ONES "ffffffff" /* hex 32 one bits */ #else #define PTR_WIDTH 8#define PTR ((void *)0x456789ab)#define PTR_STR "456789ab"#define PTR_VAL_NO_CRNG "(ptrval)"#define ZEROS ""#define ONES "" #endif /* BITS_PER_LONG == 64 */ static voidplain_hash_to_buffer(struct kunit *kunittest, const void *p, char *buf, size_t len){ KUNIT_ASSERT_EQ(kunittest, snprintf(buf, len, "%p", p), PTR_WIDTH); if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { kunit_skip(kunittest, "crng possibly not yet initialized. plain 'p' buffer contains \"%s\"\n", PTR_VAL_NO_CRNG); }} static voidhash_pointer(struct kunit *kunittest){ if (no_hash_pointers) kunit_skip(kunittest, "hash pointers disabled"); char buf[PLAIN_BUF_SIZE]; plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); /* * The hash of %p is unpredictable, therefore test() cannot be used. * * Instead verify that the first 32 bits are zeros on a 64-bit system * and that the non-hashed value is not printed. */ KUNIT_EXPECT_MEMEQ(kunittest, buf, ZEROS, strlen(ZEROS)); KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH);} /* * This is a macro so that the compiler can compare its arguments to the * __printf() attribute on __test(). This cannot be a function with a __printf() * attribute because GCC requires __printf() functions to be variadic. */#define test_hashed(kunittest, fmt, p) \ do { \ char buf[PLAIN_BUF_SIZE]; \ plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ test(buf, fmt, p); \ } while (0) /* * NULL pointers aren't hashed. */static voidnull_pointer(struct kunit *kunittest){ test(ZEROS "00000000", "%p", NULL); test(ZEROS "00000000", "%px", NULL); test("(null)", "%pE", NULL);} /* * Error pointers aren't hashed. */static voiderror_pointer(struct kunit *kunittest){ test(ONES "fffffff5", "%p", ERR_PTR(-11)); test(ONES "fffffff5", "%px", ERR_PTR(-11)); test("(efault)", "%pE", ERR_PTR(-11));} #define PTR_INVALID ((void *)0x000000ab) static voidinvalid_pointer(struct kunit *kunittest){ test_hashed(kunittest, "%p", PTR_INVALID); test(ZEROS "000000ab", "%px", PTR_INVALID); test("(efault)", "%pE", PTR_INVALID);} static voidsymbol_ptr(struct kunit *kunittest){} static voidkernel_ptr(struct kunit *kunittest){ switch (kptr_restrict) { case 0: if (no_hash_pointers) { test(PTR_STR, "%pK", PTR); } else { char buf[PLAIN_BUF_SIZE]; plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); /* %pK behaves the same as hashing */ test(buf, "%pK", PTR); } break; case 1: /* The KUnit kthread has all capabilities, including CAP_SYSLOG */ test(PTR_STR, "%pK", PTR); break; case 2: default: test(ZEROS "00000000", "%pK", PTR); break; }} static voidstruct_resource(struct kunit *kunittest){ struct resource test_resource = { .start = 0xc0ffee00, .end = 0xc0ffee00, .flags = IORESOURCE_MEM, }; test("[mem 0xc0ffee00 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xc0ffee, .end = 0xba5eba11, .flags = IORESOURCE_MEM, }; test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba5eba11, .end = 0xc0ffee, .flags = IORESOURCE_MEM, }; test("[mem 0xba5eba11-0x00c0ffee flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba5eba11, .end = 0xba5eca11, .flags = IORESOURCE_MEM, }; test("[mem 0xba5eba11-0xba5eca11 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba11, .end = 0xca10, .flags = IORESOURCE_IO | IORESOURCE_DISABLED | IORESOURCE_UNSET, }; test("[io size 0x1000 disabled]", "%pR", &test_resource);} static voidstruct_range(struct kunit *kunittest){ struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11, 0xc0ffee00ba5eba11); test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range); test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11); test("[range 0x0000000000c0ffee-0x00000000ba5eba11]", "%pra", &test_range); test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee); test("[range 0x00000000ba5eba11-0x0000000000c0ffee]", "%pra", &test_range);} static voidaddr(struct kunit *kunittest){} static voidescaped_str(struct kunit *kunittest){} static voidhex_string(struct kunit *kunittest){ const char buf[3] = {0xc0, 0xff, 0xee}; test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);} static voidmac(struct kunit *kunittest){ const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; test("2d:48:d6:fc:7a:05", "%pM", addr); test("05:7a:fc:d6:48:2d", "%pMR", addr); test("05:7A:FC:D6:48:2D", "%pMRU", addr); test("2d-48-d6-fc-7a-05", "%pMF", addr); test("2d48d6fc7a05", "%pm", addr); test("2D48D6FC7A05", "%pmU", addr); test("057afcd6482d", "%pmR", addr);} static voidip4(struct kunit *kunittest){ struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_port = cpu_to_be16(12345); sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); sa.sin_addr.s_addr = cpu_to_be32(0x01020304); test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);} static voidip6(struct kunit *kunittest){ const struct in6_addr addr = { .s6_addr = { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } }; const struct in6_addr single_zero = { .s6_addr = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } }; struct sockaddr_in6 sa = { .sin6_family = AF_INET6, .sin6_port = cpu_to_be16(12345), .sin6_addr = addr, }; test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", "%pi6|%pI6", &addr, &addr); test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", "%piS|%pIS", &sa, &sa); test("1:2:3:4:5:6:7:8", "%pI6c", &addr); test("1:0:3:4:5:6:7:8", "%pI6c", &single_zero); test("[1:2:3:4:5:6:7:8]:12345", "%pISpc", &sa);} static voiduuid(struct kunit *kunittest){ const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);} static struct dentry test_dentry[4] = { { .d_parent = &test_dentry[0], .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),Lines 1–500 of 878. Load more
// SPDX-License-Identifier: GPL-2.0-only /* * Test cases for printf facility. */ #include <kunit/test.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/printk.h> #include <linux/random.h> #include <linux/rtc.h> #include <linux/slab.h> #include <linux/sprintf.h> #include <linux/string.h> #include <linux/bitmap.h> #include <linux/dcache.h> #include <linux/socket.h> #include <linux/in.h> #include <linux/in6.h> #include <linux/gfp.h> #include <linux/mm.h> #include <linux/property.h> #define BUF_SIZE 256 #define PAD_SIZE 16 #define FILL_CHAR '$' #define NOWARN(option, comment, block) \ __diag_push(); \ __diag_ignore_all(#option, comment); \ block \ __diag_pop(); static unsigned int total_tests; static char *test_buffer; static char *alloced_buffer; static void __printf(7, 0) do_test(struct kunit *kunittest, const char *file, const int line, int bufsize, const char *expect, int elen, const char *fmt, va_list ap) { va_list aq; int ret, written; total_tests++; memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE); va_copy(aq, ap); ret = vsnprintf(test_buffer, bufsize, fmt, aq); va_end(aq); if (ret != elen) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n", file, line, bufsize, fmt, ret, elen); return; } if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", file, line, bufsize, fmt); return; } if (!bufsize) { if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", file, line, fmt); } return; } written = min(bufsize-1, elen); if (test_buffer[written]) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n", file, line, bufsize, fmt); return; } if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", file, line, bufsize, fmt); return; } if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", file, line, bufsize, fmt); return; } if (memcmp(test_buffer, expect, written)) { KUNIT_FAIL(kunittest, "%s:%d: vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", file, line, bufsize, fmt, test_buffer, written, expect); return; } } static void __printf(6, 7) __test(struct kunit *kunittest, const char *file, const int line, const char *expect, int elen, const char *fmt, ...) { va_list ap; int rand; char *p; if (elen >= BUF_SIZE) { KUNIT_FAIL(kunittest, "%s:%d: error in test suite: expected length (%d) >= BUF_SIZE (%d). fmt=\"%s\"\n", file, line, elen, BUF_SIZE, fmt); return; } va_start(ap, fmt); /* * Every fmt+args is subjected to four tests: Three where we * tell vsnprintf varying buffer sizes (plenty, not quite * enough and 0), and then we also test that kvasprintf would * be able to print it as expected. */ do_test(kunittest, file, line, BUF_SIZE, expect, elen, fmt, ap); rand = get_random_u32_inclusive(1, elen + 1); /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ do_test(kunittest, file, line, rand, expect, elen, fmt, ap); do_test(kunittest, file, line, 0, expect, elen, fmt, ap); p = kvasprintf(GFP_KERNEL, fmt, ap); if (p) { total_tests++; if (memcmp(p, expect, elen+1)) { KUNIT_FAIL(kunittest, "%s:%d: kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", file, line, fmt, p, expect); } kfree(p); } va_end(ap); } #define test(expect, fmt, ...) \ __test(kunittest, __FILE__, __LINE__, expect, strlen(expect), fmt, ##__VA_ARGS__) static void test_basic(struct kunit *kunittest) { /* Work around annoying "warning: zero-length gnu_printf format string". */ char nul = '\0'; test("", &nul); test("100%", "100%%"); test("xxx%yyy", "xxx%cyyy", '%'); __test(kunittest, __FILE__, __LINE__, "xxx\0yyy", 7, "xxx%cyyy", '\0'); } static void test_number(struct kunit *kunittest) { test("0x1234abcd ", "%#-12x", 0x1234abcd); test(" 0x1234abcd", "%#12x", 0x1234abcd); test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234); NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", { test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); }) /* * POSIX/C99: »The result of converting zero with an explicit * precision of zero shall be no characters.« Hence the output * from the below test should really be "00|0||| ". However, * the kernel's printf also produces a single 0 in that * case. This test case simply documents the current * behaviour. */ test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0); } static void test_string(struct kunit *kunittest) { test("", "%s%.0s", "", "123"); test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); test("1234 ", "%-10.4s", "123456"); test(" 1234", "%10.4s", "123456"); /* * POSIX and C99 say that a negative precision (which is only * possible to pass via a * argument) should be treated as if * the precision wasn't present, and that if the precision is * omitted (as in %.s), the precision should be taken to be * 0. However, the kernel's printf behave exactly opposite, * treating a negative precision as 0 and treating an omitted * precision specifier as if no precision was given. * * These test cases document the current behaviour; should * anyone ever feel the need to follow the standards more * closely, this can be revisited. */ test(" ", "%4.*s", -5, "123456"); test("123456", "%.s", "123456"); test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); } #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */ #if BITS_PER_LONG == 64 #define PTR_WIDTH 16 #define PTR ((void *)0xffff0123456789abUL) #define PTR_STR "ffff0123456789ab" #define PTR_VAL_NO_CRNG "(____ptrval____)" #define ZEROS "00000000" /* hex 32 zero bits */ #define ONES "ffffffff" /* hex 32 one bits */ #else #define PTR_WIDTH 8 #define PTR ((void *)0x456789ab) #define PTR_STR "456789ab" #define PTR_VAL_NO_CRNG "(ptrval)" #define ZEROS "" #define ONES "" #endif /* BITS_PER_LONG == 64 */ static void plain_hash_to_buffer(struct kunit *kunittest, const void *p, char *buf, size_t len) { KUNIT_ASSERT_EQ(kunittest, snprintf(buf, len, "%p", p), PTR_WIDTH); if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { kunit_skip(kunittest, "crng possibly not yet initialized. plain 'p' buffer contains \"%s\"\n", PTR_VAL_NO_CRNG); } } static void hash_pointer(struct kunit *kunittest) { if (no_hash_pointers) kunit_skip(kunittest, "hash pointers disabled"); char buf[PLAIN_BUF_SIZE]; plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); /* * The hash of %p is unpredictable, therefore test() cannot be used. * * Instead verify that the first 32 bits are zeros on a 64-bit system * and that the non-hashed value is not printed. */ KUNIT_EXPECT_MEMEQ(kunittest, buf, ZEROS, strlen(ZEROS)); KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH); } /* * This is a macro so that the compiler can compare its arguments to the * __printf() attribute on __test(). This cannot be a function with a __printf() * attribute because GCC requires __printf() functions to be variadic. */ #define test_hashed(kunittest, fmt, p) \ do { \ char buf[PLAIN_BUF_SIZE]; \ plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ test(buf, fmt, p); \ } while (0) /* * NULL pointers aren't hashed. */ static void null_pointer(struct kunit *kunittest) { test(ZEROS "00000000", "%p", NULL); test(ZEROS "00000000", "%px", NULL); test("(null)", "%pE", NULL); } /* * Error pointers aren't hashed. */ static void error_pointer(struct kunit *kunittest) { test(ONES "fffffff5", "%p", ERR_PTR(-11)); test(ONES "fffffff5", "%px", ERR_PTR(-11)); test("(efault)", "%pE", ERR_PTR(-11)); } #define PTR_INVALID ((void *)0x000000ab) static void invalid_pointer(struct kunit *kunittest) { test_hashed(kunittest, "%p", PTR_INVALID); test(ZEROS "000000ab", "%px", PTR_INVALID); test("(efault)", "%pE", PTR_INVALID); } static void symbol_ptr(struct kunit *kunittest) { } static void kernel_ptr(struct kunit *kunittest) { switch (kptr_restrict) { case 0: if (no_hash_pointers) { test(PTR_STR, "%pK", PTR); } else { char buf[PLAIN_BUF_SIZE]; plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE); /* %pK behaves the same as hashing */ test(buf, "%pK", PTR); } break; case 1: /* The KUnit kthread has all capabilities, including CAP_SYSLOG */ test(PTR_STR, "%pK", PTR); break; case 2: default: test(ZEROS "00000000", "%pK", PTR); break; } } static void struct_resource(struct kunit *kunittest) { struct resource test_resource = { .start = 0xc0ffee00, .end = 0xc0ffee00, .flags = IORESOURCE_MEM, }; test("[mem 0xc0ffee00 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xc0ffee, .end = 0xba5eba11, .flags = IORESOURCE_MEM, }; test("[mem 0x00c0ffee-0xba5eba11 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba5eba11, .end = 0xc0ffee, .flags = IORESOURCE_MEM, }; test("[mem 0xba5eba11-0x00c0ffee flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba5eba11, .end = 0xba5eca11, .flags = IORESOURCE_MEM, }; test("[mem 0xba5eba11-0xba5eca11 flags 0x200]", "%pr", &test_resource); test_resource = (struct resource) { .start = 0xba11, .end = 0xca10, .flags = IORESOURCE_IO | IORESOURCE_DISABLED | IORESOURCE_UNSET, }; test("[io size 0x1000 disabled]", "%pR", &test_resource); } static void struct_range(struct kunit *kunittest) { struct range test_range = DEFINE_RANGE(0xc0ffee00ba5eba11, 0xc0ffee00ba5eba11); test("[range 0xc0ffee00ba5eba11]", "%pra", &test_range); test_range = DEFINE_RANGE(0xc0ffee, 0xba5eba11); test("[range 0x0000000000c0ffee-0x00000000ba5eba11]", "%pra", &test_range); test_range = DEFINE_RANGE(0xba5eba11, 0xc0ffee); test("[range 0x00000000ba5eba11-0x0000000000c0ffee]", "%pra", &test_range); } static void addr(struct kunit *kunittest) { } static void escaped_str(struct kunit *kunittest) { } static void hex_string(struct kunit *kunittest) { const char buf[3] = {0xc0, 0xff, 0xee}; test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf); } static void mac(struct kunit *kunittest) { const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; test("2d:48:d6:fc:7a:05", "%pM", addr); test("05:7a:fc:d6:48:2d", "%pMR", addr); test("05:7A:FC:D6:48:2D", "%pMRU", addr); test("2d-48-d6-fc-7a-05", "%pMF", addr); test("2d48d6fc7a05", "%pm", addr); test("2D48D6FC7A05", "%pmU", addr); test("057afcd6482d", "%pmR", addr); } static void ip4(struct kunit *kunittest) { struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_port = cpu_to_be16(12345); sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); sa.sin_addr.s_addr = cpu_to_be32(0x01020304); test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa); } static void ip6(struct kunit *kunittest) { const struct in6_addr addr = { .s6_addr = { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } }; const struct in6_addr single_zero = { .s6_addr = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 } }; struct sockaddr_in6 sa = { .sin6_family = AF_INET6, .sin6_port = cpu_to_be16(12345), .sin6_addr = addr, }; test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", "%pi6|%pI6", &addr, &addr); test("00010002000300040005000600070008|0001:0002:0003:0004:0005:0006:0007:0008", "%piS|%pIS", &sa, &sa); test("1:2:3:4:5:6:7:8", "%pI6c", &addr); test("1:0:3:4:5:6:7:8", "%pI6c", &single_zero); test("[1:2:3:4:5:6:7:8]:12345", "%pISpc", &sa); } static void uuid(struct kunit *kunittest) { const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); } static struct dentry test_dentry[4] = { { .d_parent = &test_dentry[0], .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),