diff options
author | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2015-02-12 18:02:21 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-02-12 21:54:14 -0500 |
commit | 64d1d77a44697af8e314939ecef30642c68309cb (patch) | |
tree | f41a82fde6001ed05ab7c0ae560e6e1e9f83fb80 | |
parent | ad3d5d2f7deca6d0fd72163573bcb0cca6337e33 (diff) |
hexdump: introduce test suite
Test different scenarios of function calls located in lib/hexdump.c.
Currently hex_dump_to_buffer() is only tested and test data is provided
for little endian CPUs.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | lib/Kconfig.debug | 3 | ||||
-rw-r--r-- | lib/Makefile | 4 | ||||
-rw-r--r-- | lib/test-hexdump.c | 135 |
3 files changed, 141 insertions, 1 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index e5ea3ab856bf..79a9bb67aeaf 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
@@ -1580,6 +1580,9 @@ config ASYNC_RAID6_TEST | |||
1580 | 1580 | ||
1581 | If unsure, say N. | 1581 | If unsure, say N. |
1582 | 1582 | ||
1583 | config TEST_HEXDUMP | ||
1584 | tristate "Test functions located in the hexdump module at runtime" | ||
1585 | |||
1583 | config TEST_STRING_HELPERS | 1586 | config TEST_STRING_HELPERS |
1584 | tristate "Test functions located in the string_helpers module at runtime" | 1587 | tristate "Test functions located in the string_helpers module at runtime" |
1585 | 1588 | ||
diff --git a/lib/Makefile b/lib/Makefile index 25c061f77df7..e456defd1021 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -23,12 +23,14 @@ lib-y += kobject.o klist.o | |||
23 | obj-y += lockref.o | 23 | obj-y += lockref.o |
24 | 24 | ||
25 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ | 25 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ |
26 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ | 26 | bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ |
27 | gcd.o lcm.o list_sort.o uuid.o flex_array.o clz_ctz.o \ | 27 | gcd.o lcm.o list_sort.o uuid.o flex_array.o clz_ctz.o \ |
28 | bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \ | 28 | bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \ |
29 | percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o | 29 | percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o |
30 | obj-y += string_helpers.o | 30 | obj-y += string_helpers.o |
31 | obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o | 31 | obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o |
32 | obj-y += hexdump.o | ||
33 | obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o | ||
32 | obj-y += kstrtox.o | 34 | obj-y += kstrtox.o |
33 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o | 35 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o |
34 | obj-$(CONFIG_TEST_LKM) += test_module.o | 36 | obj-$(CONFIG_TEST_LKM) += test_module.o |
diff --git a/lib/test-hexdump.c b/lib/test-hexdump.c new file mode 100644 index 000000000000..9d3bd1e9ae48 --- /dev/null +++ b/lib/test-hexdump.c | |||
@@ -0,0 +1,135 @@ | |||
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 | |||
21 | static const char *test_data_1_le[] __initconst = { | ||
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 | |||
28 | static const char *test_data_2_le[] __initconst = { | ||
29 | "32be", "7bdb", "180a", "b293", | ||
30 | "ba70", "24c4", "837d", "9b34", | ||
31 | "9ca6", "ad31", "0f9c", "e9ac", | ||
32 | "d14c", "9919", "b143", "0caf", | ||
33 | }; | ||
34 | |||
35 | static const char *test_data_4_le[] __initconst = { | ||
36 | "7bdb32be", "b293180a", "24c4ba70", "9b34837d", | ||
37 | "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143", | ||
38 | }; | ||
39 | |||
40 | static const char *test_data_8_le[] __initconst = { | ||
41 | "b293180a7bdb32be", "9b34837d24c4ba70", | ||
42 | "e9ac0f9cad319ca6", "0cafb1439919d14c", | ||
43 | }; | ||
44 | |||
45 | static void __init test_hexdump(size_t len, int rowsize, int groupsize, | ||
46 | bool ascii) | ||
47 | { | ||
48 | char test[32 * 3 + 2 + 32 + 1]; | ||
49 | char real[32 * 3 + 2 + 32 + 1]; | ||
50 | char *p; | ||
51 | const char **result; | ||
52 | size_t l = len; | ||
53 | int gs = groupsize, rs = rowsize; | ||
54 | unsigned int i; | ||
55 | |||
56 | hex_dump_to_buffer(data_b, l, rs, gs, real, sizeof(real), ascii); | ||
57 | |||
58 | if (rs != 16 && rs != 32) | ||
59 | rs = 16; | ||
60 | |||
61 | if (l > rs) | ||
62 | l = rs; | ||
63 | |||
64 | if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0)) | ||
65 | gs = 1; | ||
66 | |||
67 | if (gs == 8) | ||
68 | result = test_data_8_le; | ||
69 | else if (gs == 4) | ||
70 | result = test_data_4_le; | ||
71 | else if (gs == 2) | ||
72 | result = test_data_2_le; | ||
73 | else | ||
74 | result = test_data_1_le; | ||
75 | |||
76 | memset(test, ' ', sizeof(test)); | ||
77 | |||
78 | /* hex dump */ | ||
79 | p = test; | ||
80 | for (i = 0; i < l / gs; i++) { | ||
81 | const char *q = *result++; | ||
82 | size_t amount = strlen(q); | ||
83 | |||
84 | strncpy(p, q, amount); | ||
85 | p += amount + 1; | ||
86 | } | ||
87 | if (i) | ||
88 | p--; | ||
89 | |||
90 | /* ASCII part */ | ||
91 | if (ascii) { | ||
92 | p = test + rs * 2 + rs / gs + 1; | ||
93 | strncpy(p, data_a, l); | ||
94 | p += l; | ||
95 | } | ||
96 | |||
97 | *p = '\0'; | ||
98 | |||
99 | if (strcmp(test, real)) { | ||
100 | pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize); | ||
101 | pr_err("Result: '%s'\n", real); | ||
102 | pr_err("Expect: '%s'\n", test); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | static void __init test_hexdump_set(int rowsize, bool ascii) | ||
107 | { | ||
108 | size_t d = min_t(size_t, sizeof(data_b), rowsize); | ||
109 | size_t len = get_random_int() % d + 1; | ||
110 | |||
111 | test_hexdump(len, rowsize, 4, ascii); | ||
112 | test_hexdump(len, rowsize, 2, ascii); | ||
113 | test_hexdump(len, rowsize, 8, ascii); | ||
114 | test_hexdump(len, rowsize, 1, ascii); | ||
115 | } | ||
116 | |||
117 | static int __init test_hexdump_init(void) | ||
118 | { | ||
119 | unsigned int i; | ||
120 | int rowsize; | ||
121 | |||
122 | pr_info("Running tests...\n"); | ||
123 | |||
124 | rowsize = (get_random_int() % 2 + 1) * 16; | ||
125 | for (i = 0; i < 16; i++) | ||
126 | test_hexdump_set(rowsize, false); | ||
127 | |||
128 | rowsize = (get_random_int() % 2 + 1) * 16; | ||
129 | for (i = 0; i < 16; i++) | ||
130 | test_hexdump_set(rowsize, true); | ||
131 | |||
132 | return -EINVAL; | ||
133 | } | ||
134 | module_init(test_hexdump_init); | ||
135 | MODULE_LICENSE("Dual BSD/GPL"); | ||