2015-02-13 00:02:21 +01:00
|
|
|
/*
|
|
|
|
* 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...";
|
|
|
|
|
2015-04-16 01:16:39 +02:00
|
|
|
static const char * const test_data_1_le[] __initconst = {
|
2015-02-13 00:02:21 +01:00
|
|
|
"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",
|
|
|
|
};
|
|
|
|
|
2015-06-26 00:02:11 +02:00
|
|
|
static const char * const test_data_2_le[] __initconst = {
|
2015-02-13 00:02:21 +01:00
|
|
|
"32be", "7bdb", "180a", "b293",
|
|
|
|
"ba70", "24c4", "837d", "9b34",
|
|
|
|
"9ca6", "ad31", "0f9c", "e9ac",
|
|
|
|
"d14c", "9919", "b143", "0caf",
|
|
|
|
};
|
|
|
|
|
2015-06-26 00:02:11 +02:00
|
|
|
static const char * const test_data_4_le[] __initconst = {
|
2015-02-13 00:02:21 +01:00
|
|
|
"7bdb32be", "b293180a", "24c4ba70", "9b34837d",
|
|
|
|
"ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
|
|
|
|
};
|
|
|
|
|
2015-06-26 00:02:11 +02:00
|
|
|
static const char * const test_data_8_le[] __initconst = {
|
2015-02-13 00:02:21 +01:00
|
|
|
"b293180a7bdb32be", "9b34837d24c4ba70",
|
|
|
|
"e9ac0f9cad319ca6", "0cafb1439919d14c",
|
|
|
|
};
|
|
|
|
|
2016-01-20 23:58:50 +01:00
|
|
|
#define FILL_CHAR '#'
|
|
|
|
|
2016-01-20 23:58:47 +01:00
|
|
|
static void __init test_hexdump_prepare_test(size_t len, int rowsize,
|
|
|
|
int groupsize, char *test,
|
|
|
|
size_t testlen, bool ascii)
|
2015-02-13 00:02:21 +01:00
|
|
|
{
|
|
|
|
char *p;
|
2015-04-19 22:48:40 +02:00
|
|
|
const char * const *result;
|
2015-02-13 00:02:21 +01:00
|
|
|
size_t l = len;
|
|
|
|
int gs = groupsize, rs = rowsize;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* hex dump */
|
|
|
|
p = test;
|
|
|
|
for (i = 0; i < l / gs; i++) {
|
|
|
|
const char *q = *result++;
|
|
|
|
size_t amount = strlen(q);
|
|
|
|
|
|
|
|
strncpy(p, q, amount);
|
2016-01-20 23:58:50 +01:00
|
|
|
p += amount;
|
|
|
|
|
|
|
|
*p++ = ' ';
|
2015-02-13 00:02:21 +01:00
|
|
|
}
|
|
|
|
if (i)
|
|
|
|
p--;
|
|
|
|
|
|
|
|
/* ASCII part */
|
|
|
|
if (ascii) {
|
2016-01-20 23:58:50 +01:00
|
|
|
do {
|
|
|
|
*p++ = ' ';
|
|
|
|
} while (p < test + rs * 2 + rs / gs + 1);
|
|
|
|
|
2015-02-13 00:02:21 +01:00
|
|
|
strncpy(p, data_a, l);
|
|
|
|
p += l;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
2016-01-20 23:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#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];
|
|
|
|
|
2016-01-20 23:58:58 +01:00
|
|
|
memset(real, FILL_CHAR, sizeof(real));
|
2016-01-20 23:58:47 +01:00
|
|
|
hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
|
|
|
|
ascii);
|
|
|
|
|
2016-01-20 23:58:58 +01:00
|
|
|
memset(test, FILL_CHAR, sizeof(test));
|
2016-01-20 23:58:47 +01:00
|
|
|
test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
|
|
|
|
ascii);
|
2015-02-13 00:02:21 +01:00
|
|
|
|
2016-01-20 23:58:58 +01:00
|
|
|
if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
|
2015-02-13 00:02:21 +01:00
|
|
|
pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
|
|
|
|
pr_err("Result: '%s'\n", real);
|
|
|
|
pr_err("Expect: '%s'\n", test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:58:53 +01:00
|
|
|
static void __init test_hexdump_overflow(size_t buflen, bool ascii)
|
2015-02-13 00:02:29 +01:00
|
|
|
{
|
2016-01-20 23:58:53 +01:00
|
|
|
char buf[TEST_HEXDUMP_BUF_SIZE];
|
2015-02-13 00:02:29 +01:00
|
|
|
const char *t = test_data_1_le[0];
|
2016-01-20 23:58:56 +01:00
|
|
|
size_t len = 1;
|
|
|
|
int rs = 16, gs = 1;
|
|
|
|
int ae, he, e, r;
|
2015-02-13 00:02:29 +01:00
|
|
|
bool a;
|
|
|
|
|
2016-01-20 23:58:50 +01:00
|
|
|
memset(buf, FILL_CHAR, sizeof(buf));
|
2015-02-13 00:02:29 +01:00
|
|
|
|
2016-01-20 23:58:56 +01:00
|
|
|
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 */;
|
2015-02-13 00:02:29 +01:00
|
|
|
|
|
|
|
if (ascii)
|
2016-01-20 23:58:56 +01:00
|
|
|
e = ae;
|
2015-02-13 00:02:29 +01:00
|
|
|
else
|
2016-01-20 23:58:56 +01:00
|
|
|
e = he;
|
2015-02-13 00:02:29 +01:00
|
|
|
buf[e + 2] = '\0';
|
|
|
|
|
2016-01-20 23:58:53 +01:00
|
|
|
if (!buflen) {
|
2016-01-20 23:58:50 +01:00
|
|
|
a = r == e && buf[0] == FILL_CHAR;
|
2016-01-20 23:58:53 +01:00
|
|
|
} else if (buflen < 3) {
|
2015-02-13 00:02:29 +01:00
|
|
|
a = r == e && buf[0] == '\0';
|
2016-01-20 23:58:53 +01:00
|
|
|
} else if (buflen < 4) {
|
2015-02-13 00:02:29 +01:00
|
|
|
a = r == e && !strcmp(buf, t);
|
|
|
|
} else if (ascii) {
|
2016-01-20 23:58:53 +01:00
|
|
|
if (buflen < 51)
|
|
|
|
a = r == e && buf[buflen - 1] == '\0' && buf[buflen - 2] == FILL_CHAR;
|
2015-02-13 00:02:29 +01:00
|
|
|
else
|
|
|
|
a = r == e && buf[50] == '\0' && buf[49] == '.';
|
|
|
|
} else {
|
|
|
|
a = r == e && buf[e] == '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a) {
|
2016-01-20 23:58:53 +01:00
|
|
|
pr_err("Len: %zu rc: %u strlen: %zu\n", buflen, r, strlen(buf));
|
2015-02-13 00:02:29 +01:00
|
|
|
pr_err("Result: '%s'\n", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 00:02:21 +01:00
|
|
|
static int __init test_hexdump_init(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int rowsize;
|
|
|
|
|
|
|
|
pr_info("Running tests...\n");
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-01-20 23:58:53 +01:00
|
|
|
for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
|
|
|
|
test_hexdump_overflow(i, false);
|
2015-02-13 00:02:29 +01:00
|
|
|
|
2016-01-20 23:58:53 +01:00
|
|
|
for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
|
|
|
|
test_hexdump_overflow(i, true);
|
2015-02-13 00:02:29 +01:00
|
|
|
|
2015-02-13 00:02:21 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
module_init(test_hexdump_init);
|
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|