2 * Test cases for lib/string_helpers.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>
11 #include <linux/string_helpers.h>
13 static __init
bool test_string_check_buf(const char *name
, unsigned int flags
,
15 char *out_real
, size_t q_real
,
16 char *out_test
, size_t q_test
)
18 if (q_real
== q_test
&& !memcmp(out_test
, out_real
, q_test
))
21 pr_warn("Test '%s' failed: flags = %u\n", name
, flags
);
23 print_hex_dump(KERN_WARNING
, "Input: ", DUMP_PREFIX_NONE
, 16, 1,
25 print_hex_dump(KERN_WARNING
, "Expected: ", DUMP_PREFIX_NONE
, 16, 1,
26 out_test
, q_test
, true);
27 print_hex_dump(KERN_WARNING
, "Got: ", DUMP_PREFIX_NONE
, 16, 1,
28 out_real
, q_real
, true);
39 static const struct test_string strings
[] __initconst
= {
41 .in
= "\\f\\ \\n\\r\\t\\v",
42 .out
= "\f\\ \n\r\t\v",
43 .flags
= UNESCAPE_SPACE
,
46 .in
= "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
47 .out
= " \001\00387\0064\005 \\8aH?7",
48 .flags
= UNESCAPE_OCTAL
,
51 .in
= "\\xv\\xa\\x2c\\xD\\x6f2",
53 .flags
= UNESCAPE_HEX
,
56 .in
= "\\h\\\\\\\"\\a\\e\\",
57 .out
= "\\h\\\"\a\e\\",
58 .flags
= UNESCAPE_SPECIAL
,
62 static void __init
test_string_unescape(const char *name
, unsigned int flags
,
68 int i
, p
= 0, q_test
= 0, q_real
= sizeof(out_real
);
70 for (i
= 0; i
< ARRAY_SIZE(strings
); i
++) {
71 const char *s
= strings
[i
].in
;
72 int len
= strlen(strings
[i
].in
);
74 /* Copy string to in buffer */
75 memcpy(&in
[p
], s
, len
);
78 /* Copy expected result for given flags */
79 if (flags
& strings
[i
].flags
) {
81 len
= strlen(strings
[i
].out
);
83 memcpy(&out_test
[q_test
], s
, len
);
88 /* Call string_unescape and compare result */
90 memcpy(out_real
, in
, p
);
91 if (flags
== UNESCAPE_ANY
)
92 q_real
= string_unescape_any_inplace(out_real
);
94 q_real
= string_unescape_inplace(out_real
, flags
);
95 } else if (flags
== UNESCAPE_ANY
) {
96 q_real
= string_unescape_any(in
, out_real
, q_real
);
98 q_real
= string_unescape(in
, out_real
, q_real
, flags
);
101 test_string_check_buf(name
, flags
, in
, p
- 1, out_real
, q_real
,
105 static int __init
test_string_helpers_init(void)
109 pr_info("Running tests...\n");
110 for (i
= 0; i
< UNESCAPE_ANY
+ 1; i
++)
111 test_string_unescape("unescape", i
, false);
112 test_string_unescape("unescape inplace",
113 get_random_int() % (UNESCAPE_ANY
+ 1), true);
117 module_init(test_string_helpers_init
);
118 MODULE_LICENSE("Dual BSD/GPL");
This page took 0.077148 seconds and 6 git commands to generate.