Blame view

lib/test_hexdump.c 5.68 KB
64d1d77a4   Andy Shevchenko   hexdump: introduc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   * Test cases for lib/hexdump.c module.
   */
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/random.h>
  #include <linux/string.h>
  
  static const unsigned char data_b[] = {
  	'\xbe', '\x32', '\xdb', '\x7b', '\x0a', '\x18', '\x93', '\xb2',	/* 00 - 07 */
  	'\x70', '\xba', '\xc4', '\x24', '\x7d', '\x83', '\x34', '\x9b',	/* 08 - 0f */
  	'\xa6', '\x9c', '\x31', '\xad', '\x9c', '\x0f', '\xac', '\xe9',	/* 10 - 17 */
  	'\x4c', '\xd1', '\x19', '\x99', '\x43', '\xb1', '\xaf', '\x0c',	/* 18 - 1f */
  };
  
  static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C...";
c79574abe   Andi Kleen   lib/test-hexdump....
20
  static const char * const test_data_1_le[] __initconst = {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
21
22
23
24
25
  	"be", "32", "db", "7b", "0a", "18", "93", "b2",
  	"70", "ba", "c4", "24", "7d", "83", "34", "9b",
  	"a6", "9c", "31", "ad", "9c", "0f", "ac", "e9",
  	"4c", "d1", "19", "99", "43", "b1", "af", "0c",
  };
79e23d577   Geert Uytterhoeven   hexdump: Make tes...
26
  static const char * const test_data_2_le[] __initconst = {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
27
28
29
30
31
  	"32be", "7bdb", "180a", "b293",
  	"ba70", "24c4", "837d", "9b34",
  	"9ca6", "ad31", "0f9c", "e9ac",
  	"d14c", "9919", "b143", "0caf",
  };
79e23d577   Geert Uytterhoeven   hexdump: Make tes...
32
  static const char * const test_data_4_le[] __initconst = {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
33
34
35
  	"7bdb32be", "b293180a", "24c4ba70", "9b34837d",
  	"ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
  };
79e23d577   Geert Uytterhoeven   hexdump: Make tes...
36
  static const char * const test_data_8_le[] __initconst = {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
37
38
39
  	"b293180a7bdb32be", "9b34837d24c4ba70",
  	"e9ac0f9cad319ca6", "0cafb1439919d14c",
  };
3db4a9871   Andy Shevchenko   test_hexdump: def...
40
  #define FILL_CHAR	'#'
7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
41
42
  static unsigned total_tests __initdata;
  static unsigned failed_tests __initdata;
87977ca6b   Andy Shevchenko   test_hexdump: int...
43
44
45
  static void __init test_hexdump_prepare_test(size_t len, int rowsize,
  					     int groupsize, char *test,
  					     size_t testlen, bool ascii)
64d1d77a4   Andy Shevchenko   hexdump: introduc...
46
  {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
47
  	char *p;
17974c054   Linus Torvalds   hexdump: avoid wa...
48
  	const char * const *result;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
49
50
51
  	size_t l = len;
  	int gs = groupsize, rs = rowsize;
  	unsigned int i;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  	if (rs != 16 && rs != 32)
  		rs = 16;
  
  	if (l > rs)
  		l = rs;
  
  	if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
  		gs = 1;
  
  	if (gs == 8)
  		result = test_data_8_le;
  	else if (gs == 4)
  		result = test_data_4_le;
  	else if (gs == 2)
  		result = test_data_2_le;
  	else
  		result = test_data_1_le;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
69
70
71
72
73
  	/* hex dump */
  	p = test;
  	for (i = 0; i < l / gs; i++) {
  		const char *q = *result++;
  		size_t amount = strlen(q);
337f2837c   Linus Torvalds   test_hexdump: use...
74
  		memcpy(p, q, amount);
3db4a9871   Andy Shevchenko   test_hexdump: def...
75
76
77
  		p += amount;
  
  		*p++ = ' ';
64d1d77a4   Andy Shevchenko   hexdump: introduc...
78
79
80
81
82
83
  	}
  	if (i)
  		p--;
  
  	/* ASCII part */
  	if (ascii) {
3db4a9871   Andy Shevchenko   test_hexdump: def...
84
85
86
  		do {
  			*p++ = ' ';
  		} while (p < test + rs * 2 + rs / gs + 1);
64d1d77a4   Andy Shevchenko   hexdump: introduc...
87
88
89
90
91
  		strncpy(p, data_a, l);
  		p += l;
  	}
  
  	*p = '\0';
87977ca6b   Andy Shevchenko   test_hexdump: int...
92
93
94
95
96
97
98
99
100
  }
  
  #define TEST_HEXDUMP_BUF_SIZE		(32 * 3 + 2 + 32 + 1)
  
  static void __init test_hexdump(size_t len, int rowsize, int groupsize,
  				bool ascii)
  {
  	char test[TEST_HEXDUMP_BUF_SIZE];
  	char real[TEST_HEXDUMP_BUF_SIZE];
7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
101
  	total_tests++;
7047d8137   Andy Shevchenko   test_hexdump: swi...
102
  	memset(real, FILL_CHAR, sizeof(real));
87977ca6b   Andy Shevchenko   test_hexdump: int...
103
104
  	hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
  			   ascii);
7047d8137   Andy Shevchenko   test_hexdump: swi...
105
  	memset(test, FILL_CHAR, sizeof(test));
87977ca6b   Andy Shevchenko   test_hexdump: int...
106
107
  	test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
  				  ascii);
64d1d77a4   Andy Shevchenko   hexdump: introduc...
108

7047d8137   Andy Shevchenko   test_hexdump: swi...
109
  	if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
64d1d77a4   Andy Shevchenko   hexdump: introduc...
110
111
112
113
114
115
  		pr_err("Len: %zu row: %d group: %d
  ", len, rowsize, groupsize);
  		pr_err("Result: '%s'
  ", real);
  		pr_err("Expect: '%s'
  ", test);
7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
116
  		failed_tests++;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
117
118
119
120
121
122
123
124
125
126
127
128
129
  	}
  }
  
  static void __init test_hexdump_set(int rowsize, bool ascii)
  {
  	size_t d = min_t(size_t, sizeof(data_b), rowsize);
  	size_t len = get_random_int() % d + 1;
  
  	test_hexdump(len, rowsize, 4, ascii);
  	test_hexdump(len, rowsize, 2, ascii);
  	test_hexdump(len, rowsize, 8, ascii);
  	test_hexdump(len, rowsize, 1, ascii);
  }
1dacd9ddd   Andy Shevchenko   test_hexdump: tes...
130
131
132
  static void __init test_hexdump_overflow(size_t buflen, size_t len,
  					 int rowsize, int groupsize,
  					 bool ascii)
114fc1afb   Andy Shevchenko   hexdump: make it ...
133
  {
cc77a719a   Andy Shevchenko   test_hexdump: che...
134
  	char test[TEST_HEXDUMP_BUF_SIZE];
a3d601fcc   Andy Shevchenko   test_hexdump: go ...
135
  	char buf[TEST_HEXDUMP_BUF_SIZE];
cc77a719a   Andy Shevchenko   test_hexdump: che...
136
137
  	int rs = rowsize, gs = groupsize;
  	int ae, he, e, f, r;
114fc1afb   Andy Shevchenko   hexdump: make it ...
138
  	bool a;
114fc1afb   Andy Shevchenko   hexdump: make it ...
139

7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
140
  	total_tests++;
3db4a9871   Andy Shevchenko   test_hexdump: def...
141
  	memset(buf, FILL_CHAR, sizeof(buf));
114fc1afb   Andy Shevchenko   hexdump: make it ...
142

ad27a7559   Andy Shevchenko   test_hexdump: rep...
143
144
145
146
147
148
149
150
  	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
  
  	/*
  	 * Caller must provide the data length multiple of groupsize. The
  	 * calculations below are made with that assumption in mind.
  	 */
  	ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
  	he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
114fc1afb   Andy Shevchenko   hexdump: make it ...
151
152
  
  	if (ascii)
ad27a7559   Andy Shevchenko   test_hexdump: rep...
153
  		e = ae;
114fc1afb   Andy Shevchenko   hexdump: make it ...
154
  	else
ad27a7559   Andy Shevchenko   test_hexdump: rep...
155
  		e = he;
cc77a719a   Andy Shevchenko   test_hexdump: che...
156
157
158
159
160
  
  	f = min_t(int, e + 1, buflen);
  	if (buflen) {
  		test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
  		test[f - 1] = '\0';
114fc1afb   Andy Shevchenko   hexdump: make it ...
161
  	}
cc77a719a   Andy Shevchenko   test_hexdump: che...
162
163
164
165
166
  	memset(test + f, FILL_CHAR, sizeof(test) - f);
  
  	a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
  
  	buf[sizeof(buf) - 1] = '\0';
114fc1afb   Andy Shevchenko   hexdump: make it ...
167
168
  
  	if (!a) {
cc77a719a   Andy Shevchenko   test_hexdump: che...
169
170
171
172
173
174
175
  		pr_err("Len: %zu buflen: %zu strlen: %zu
  ",
  			len, buflen, strnlen(buf, sizeof(buf)));
  		pr_err("Result: %d '%s'
  ", r, buf);
  		pr_err("Expect: %d '%s'
  ", e, test);
7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
176
  		failed_tests++;
114fc1afb   Andy Shevchenko   hexdump: make it ...
177
178
  	}
  }
1dacd9ddd   Andy Shevchenko   test_hexdump: tes...
179
180
181
182
183
184
185
186
187
188
189
190
  static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
  {
  	unsigned int i = 0;
  	int rs = (get_random_int() % 2 + 1) * 16;
  
  	do {
  		int gs = 1 << i;
  		size_t len = get_random_int() % rs + gs;
  
  		test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
  	} while (i++ < 3);
  }
64d1d77a4   Andy Shevchenko   hexdump: introduc...
191
192
193
194
  static int __init test_hexdump_init(void)
  {
  	unsigned int i;
  	int rowsize;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
195
196
197
198
199
200
201
  	rowsize = (get_random_int() % 2 + 1) * 16;
  	for (i = 0; i < 16; i++)
  		test_hexdump_set(rowsize, false);
  
  	rowsize = (get_random_int() % 2 + 1) * 16;
  	for (i = 0; i < 16; i++)
  		test_hexdump_set(rowsize, true);
a3d601fcc   Andy Shevchenko   test_hexdump: go ...
202
  	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
1dacd9ddd   Andy Shevchenko   test_hexdump: tes...
203
  		test_hexdump_overflow_set(i, false);
114fc1afb   Andy Shevchenko   hexdump: make it ...
204

a3d601fcc   Andy Shevchenko   test_hexdump: go ...
205
  	for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
1dacd9ddd   Andy Shevchenko   test_hexdump: tes...
206
  		test_hexdump_overflow_set(i, true);
114fc1afb   Andy Shevchenko   hexdump: make it ...
207

7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
208
209
210
211
212
213
214
215
  	if (failed_tests == 0)
  		pr_info("all %u tests passed
  ", total_tests);
  	else
  		pr_err("failed %u out of %u tests
  ", failed_tests, total_tests);
  
  	return failed_tests ? -EINVAL : 0;
64d1d77a4   Andy Shevchenko   hexdump: introduc...
216
217
  }
  module_init(test_hexdump_init);
7aaf4c3e1   Andy Shevchenko   test_hexdump: pri...
218
219
220
221
222
223
224
225
  
  static void __exit test_hexdump_exit(void)
  {
  	/* do nothing */
  }
  module_exit(test_hexdump_exit);
  
  MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
64d1d77a4   Andy Shevchenko   hexdump: introduc...
226
  MODULE_LICENSE("Dual BSD/GPL");