2 * Test cases for lib/hexdump.c module.
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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 */
19 static const unsigned char data_a
[] = ".2.{....p..$}.4...1.....L...C...";
21 static const char * const 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",
28 static const char * const 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",
35 static const char * const test_data_4_le
[] __initconst
= {
36 "7bdb32be", "b293180a", "24c4ba70", "9b34837d",
37 "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
40 static const char * const test_data_8_le
[] __initconst
= {
41 "b293180a7bdb32be", "9b34837d24c4ba70",
42 "e9ac0f9cad319ca6", "0cafb1439919d14c",
47 static void __init
test_hexdump_prepare_test(size_t len
, int rowsize
,
48 int groupsize
, char *test
,
49 size_t testlen
, bool ascii
)
52 const char * const *result
;
54 int gs
= groupsize
, rs
= rowsize
;
57 if (rs
!= 16 && rs
!= 32)
63 if (!is_power_of_2(gs
) || gs
> 8 || (len
% gs
!= 0))
67 result
= test_data_8_le
;
69 result
= test_data_4_le
;
71 result
= test_data_2_le
;
73 result
= test_data_1_le
;
77 for (i
= 0; i
< l
/ gs
; i
++) {
78 const char *q
= *result
++;
79 size_t amount
= strlen(q
);
81 strncpy(p
, q
, amount
);
93 } while (p
< test
+ rs
* 2 + rs
/ gs
+ 1);
95 strncpy(p
, data_a
, l
);
102 #define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1)
104 static void __init
test_hexdump(size_t len
, int rowsize
, int groupsize
,
107 char test
[TEST_HEXDUMP_BUF_SIZE
];
108 char real
[TEST_HEXDUMP_BUF_SIZE
];
110 memset(real
, FILL_CHAR
, sizeof(real
));
111 hex_dump_to_buffer(data_b
, len
, rowsize
, groupsize
, real
, sizeof(real
),
114 memset(test
, FILL_CHAR
, sizeof(test
));
115 test_hexdump_prepare_test(len
, rowsize
, groupsize
, test
, sizeof(test
),
118 if (memcmp(test
, real
, TEST_HEXDUMP_BUF_SIZE
)) {
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
);
125 static void __init
test_hexdump_set(int rowsize
, bool ascii
)
127 size_t d
= min_t(size_t, sizeof(data_b
), rowsize
);
128 size_t len
= get_random_int() % d
+ 1;
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
);
136 static void __init
test_hexdump_overflow(size_t buflen
, bool ascii
)
138 char buf
[TEST_HEXDUMP_BUF_SIZE
];
139 const char *t
= test_data_1_le
[0];
145 memset(buf
, FILL_CHAR
, sizeof(buf
));
147 r
= hex_dump_to_buffer(data_b
, len
, rs
, gs
, buf
, buflen
, ascii
);
150 * Caller must provide the data length multiple of groupsize. The
151 * calculations below are made with that assumption in mind.
153 ae
= rs
* 2 /* hex */ + rs
/ gs
/* spaces */ + 1 /* space */ + len
/* ascii */;
154 he
= (gs
* 2 /* hex */ + 1 /* space */) * len
/ gs
- 1 /* no trailing space */;
163 a
= r
== e
&& buf
[0] == FILL_CHAR
;
164 } else if (buflen
< 3) {
165 a
= r
== e
&& buf
[0] == '\0';
166 } else if (buflen
< 4) {
167 a
= r
== e
&& !strcmp(buf
, t
);
170 a
= r
== e
&& buf
[buflen
- 1] == '\0' && buf
[buflen
- 2] == FILL_CHAR
;
172 a
= r
== e
&& buf
[50] == '\0' && buf
[49] == '.';
174 a
= r
== e
&& buf
[e
] == '\0';
178 pr_err("Len: %zu rc: %u strlen: %zu\n", buflen
, r
, strlen(buf
));
179 pr_err("Result: '%s'\n", buf
);
183 static int __init
test_hexdump_init(void)
188 pr_info("Running tests...\n");
190 rowsize
= (get_random_int() % 2 + 1) * 16;
191 for (i
= 0; i
< 16; i
++)
192 test_hexdump_set(rowsize
, false);
194 rowsize
= (get_random_int() % 2 + 1) * 16;
195 for (i
= 0; i
< 16; i
++)
196 test_hexdump_set(rowsize
, true);
198 for (i
= 0; i
<= TEST_HEXDUMP_BUF_SIZE
; i
++)
199 test_hexdump_overflow(i
, false);
201 for (i
= 0; i
<= TEST_HEXDUMP_BUF_SIZE
; i
++)
202 test_hexdump_overflow(i
, true);
206 module_init(test_hexdump_init
);
207 MODULE_LICENSE("Dual BSD/GPL");
This page took 0.048471 seconds and 6 git commands to generate.