Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Test cases for lib/hexdump.c module. |
| 3 | */ |
| 4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/random.h> |
| 10 | #include <linux/string.h> |
| 11 | |
| 12 | static const unsigned char data_b[] = { |
| 13 | '\xbe', '\x32', '\xdb', '\x7b', '\x0a', '\x18', '\x93', '\xb2', /* 00 - 07 */ |
| 14 | '\x70', '\xba', '\xc4', '\x24', '\x7d', '\x83', '\x34', '\x9b', /* 08 - 0f */ |
| 15 | '\xa6', '\x9c', '\x31', '\xad', '\x9c', '\x0f', '\xac', '\xe9', /* 10 - 17 */ |
| 16 | '\x4c', '\xd1', '\x19', '\x99', '\x43', '\xb1', '\xaf', '\x0c', /* 18 - 1f */ |
| 17 | }; |
| 18 | |
| 19 | static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C..."; |
| 20 | |
Andi Kleen | c79574a | 2015-04-15 16:16:39 -0700 | [diff] [blame] | 21 | static const char * const test_data_1_le[] __initconst = { |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 22 | "be", "32", "db", "7b", "0a", "18", "93", "b2", |
| 23 | "70", "ba", "c4", "24", "7d", "83", "34", "9b", |
| 24 | "a6", "9c", "31", "ad", "9c", "0f", "ac", "e9", |
| 25 | "4c", "d1", "19", "99", "43", "b1", "af", "0c", |
| 26 | }; |
| 27 | |
Geert Uytterhoeven | 79e23d5 | 2015-06-25 15:02:11 -0700 | [diff] [blame] | 28 | static const char * const test_data_2_le[] __initconst = { |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 29 | "32be", "7bdb", "180a", "b293", |
| 30 | "ba70", "24c4", "837d", "9b34", |
| 31 | "9ca6", "ad31", "0f9c", "e9ac", |
| 32 | "d14c", "9919", "b143", "0caf", |
| 33 | }; |
| 34 | |
Geert Uytterhoeven | 79e23d5 | 2015-06-25 15:02:11 -0700 | [diff] [blame] | 35 | static const char * const test_data_4_le[] __initconst = { |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 36 | "7bdb32be", "b293180a", "24c4ba70", "9b34837d", |
| 37 | "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143", |
| 38 | }; |
| 39 | |
Geert Uytterhoeven | 79e23d5 | 2015-06-25 15:02:11 -0700 | [diff] [blame] | 40 | static const char * const test_data_8_le[] __initconst = { |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 41 | "b293180a7bdb32be", "9b34837d24c4ba70", |
| 42 | "e9ac0f9cad319ca6", "0cafb1439919d14c", |
| 43 | }; |
| 44 | |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 45 | #define FILL_CHAR '#' |
| 46 | |
Andy Shevchenko | 87977ca6b | 2016-01-20 14:58:47 -0800 | [diff] [blame] | 47 | static void __init test_hexdump_prepare_test(size_t len, int rowsize, |
| 48 | int groupsize, char *test, |
| 49 | size_t testlen, bool ascii) |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 50 | { |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 51 | char *p; |
Linus Torvalds | 17974c05 | 2015-04-19 13:48:40 -0700 | [diff] [blame] | 52 | const char * const *result; |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 53 | size_t l = len; |
| 54 | int gs = groupsize, rs = rowsize; |
| 55 | unsigned int i; |
| 56 | |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 57 | if (rs != 16 && rs != 32) |
| 58 | rs = 16; |
| 59 | |
| 60 | if (l > rs) |
| 61 | l = rs; |
| 62 | |
| 63 | if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0)) |
| 64 | gs = 1; |
| 65 | |
| 66 | if (gs == 8) |
| 67 | result = test_data_8_le; |
| 68 | else if (gs == 4) |
| 69 | result = test_data_4_le; |
| 70 | else if (gs == 2) |
| 71 | result = test_data_2_le; |
| 72 | else |
| 73 | result = test_data_1_le; |
| 74 | |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 75 | memset(test, FILL_CHAR, testlen); |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 76 | |
| 77 | /* hex dump */ |
| 78 | p = test; |
| 79 | for (i = 0; i < l / gs; i++) { |
| 80 | const char *q = *result++; |
| 81 | size_t amount = strlen(q); |
| 82 | |
| 83 | strncpy(p, q, amount); |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 84 | p += amount; |
| 85 | |
| 86 | *p++ = ' '; |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 87 | } |
| 88 | if (i) |
| 89 | p--; |
| 90 | |
| 91 | /* ASCII part */ |
| 92 | if (ascii) { |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 93 | do { |
| 94 | *p++ = ' '; |
| 95 | } while (p < test + rs * 2 + rs / gs + 1); |
| 96 | |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 97 | strncpy(p, data_a, l); |
| 98 | p += l; |
| 99 | } |
| 100 | |
| 101 | *p = '\0'; |
Andy Shevchenko | 87977ca6b | 2016-01-20 14:58:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1) |
| 105 | |
| 106 | static void __init test_hexdump(size_t len, int rowsize, int groupsize, |
| 107 | bool ascii) |
| 108 | { |
| 109 | char test[TEST_HEXDUMP_BUF_SIZE]; |
| 110 | char real[TEST_HEXDUMP_BUF_SIZE]; |
| 111 | |
| 112 | hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real), |
| 113 | ascii); |
| 114 | |
| 115 | test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test), |
| 116 | ascii); |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 117 | |
| 118 | if (strcmp(test, real)) { |
| 119 | pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize); |
| 120 | pr_err("Result: '%s'\n", real); |
| 121 | pr_err("Expect: '%s'\n", test); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void __init test_hexdump_set(int rowsize, bool ascii) |
| 126 | { |
| 127 | size_t d = min_t(size_t, sizeof(data_b), rowsize); |
| 128 | size_t len = get_random_int() % d + 1; |
| 129 | |
| 130 | test_hexdump(len, rowsize, 4, ascii); |
| 131 | test_hexdump(len, rowsize, 2, ascii); |
| 132 | test_hexdump(len, rowsize, 8, ascii); |
| 133 | test_hexdump(len, rowsize, 1, ascii); |
| 134 | } |
| 135 | |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 136 | static void __init test_hexdump_overflow(size_t buflen, bool ascii) |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 137 | { |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 138 | char buf[TEST_HEXDUMP_BUF_SIZE]; |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 139 | const char *t = test_data_1_le[0]; |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 140 | bool a; |
| 141 | int e, r; |
| 142 | |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 143 | memset(buf, FILL_CHAR, sizeof(buf)); |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 144 | |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 145 | r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, buflen, ascii); |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 146 | |
| 147 | if (ascii) |
| 148 | e = 50; |
| 149 | else |
| 150 | e = 2; |
| 151 | buf[e + 2] = '\0'; |
| 152 | |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 153 | if (!buflen) { |
Andy Shevchenko | 3db4a98 | 2016-01-20 14:58:50 -0800 | [diff] [blame] | 154 | a = r == e && buf[0] == FILL_CHAR; |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 155 | } else if (buflen < 3) { |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 156 | a = r == e && buf[0] == '\0'; |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 157 | } else if (buflen < 4) { |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 158 | a = r == e && !strcmp(buf, t); |
| 159 | } else if (ascii) { |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 160 | if (buflen < 51) |
| 161 | a = r == e && buf[buflen - 1] == '\0' && buf[buflen - 2] == FILL_CHAR; |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 162 | else |
| 163 | a = r == e && buf[50] == '\0' && buf[49] == '.'; |
| 164 | } else { |
| 165 | a = r == e && buf[e] == '\0'; |
| 166 | } |
| 167 | |
| 168 | if (!a) { |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 169 | pr_err("Len: %zu rc: %u strlen: %zu\n", buflen, r, strlen(buf)); |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 170 | pr_err("Result: '%s'\n", buf); |
| 171 | } |
| 172 | } |
| 173 | |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 174 | static int __init test_hexdump_init(void) |
| 175 | { |
| 176 | unsigned int i; |
| 177 | int rowsize; |
| 178 | |
| 179 | pr_info("Running tests...\n"); |
| 180 | |
| 181 | rowsize = (get_random_int() % 2 + 1) * 16; |
| 182 | for (i = 0; i < 16; i++) |
| 183 | test_hexdump_set(rowsize, false); |
| 184 | |
| 185 | rowsize = (get_random_int() % 2 + 1) * 16; |
| 186 | for (i = 0; i < 16; i++) |
| 187 | test_hexdump_set(rowsize, true); |
| 188 | |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 189 | for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) |
| 190 | test_hexdump_overflow(i, false); |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 191 | |
Andy Shevchenko | a3d601f | 2016-01-20 14:58:53 -0800 | [diff] [blame^] | 192 | for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) |
| 193 | test_hexdump_overflow(i, true); |
Andy Shevchenko | 114fc1a | 2015-02-12 15:02:29 -0800 | [diff] [blame] | 194 | |
Andy Shevchenko | 64d1d77 | 2015-02-12 15:02:21 -0800 | [diff] [blame] | 195 | return -EINVAL; |
| 196 | } |
| 197 | module_init(test_hexdump_init); |
| 198 | MODULE_LICENSE("Dual BSD/GPL"); |