^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Test cases for lib/string_helpers.c module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static __init bool test_string_check_buf(const char *name, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) char *in, size_t p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) char *out_real, size_t q_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) char *out_test, size_t q_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (q_real == q_test && !memcmp(out_test, out_real, q_test))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) pr_warn("Test '%s' failed: flags = %u\n", name, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) in, p, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) out_test, q_test, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) out_real, q_real, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct test_string {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const char *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static const struct test_string strings[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .in = "\\f\\ \\n\\r\\t\\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .out = "\f\\ \n\r\t\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .flags = UNESCAPE_SPACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .out = " \001\00387\0064\005 \\8aH?7",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .flags = UNESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .in = "\\xv\\xa\\x2c\\xD\\x6f2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .out = "\\xv\n,\ro2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .flags = UNESCAPE_HEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .in = "\\h\\\\\\\"\\a\\e\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .out = "\\h\\\"\a\e\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .flags = UNESCAPE_SPECIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void __init test_string_unescape(const char *name, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) bool inplace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int q_real = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) char *in = kmalloc(q_real, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *out_test = kmalloc(q_real, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) char *out_real = kmalloc(q_real, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int i, p = 0, q_test = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!in || !out_test || !out_real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for (i = 0; i < ARRAY_SIZE(strings); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const char *s = strings[i].in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int len = strlen(strings[i].in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Copy string to in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) memcpy(&in[p], s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) p += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Copy expected result for given flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (flags & strings[i].flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) s = strings[i].out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) len = strlen(strings[i].out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) memcpy(&out_test[q_test], s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) q_test += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) in[p++] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* Call string_unescape and compare result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (inplace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) memcpy(out_real, in, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (flags == UNESCAPE_ANY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) q_real = string_unescape_any_inplace(out_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) q_real = string_unescape_inplace(out_real, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else if (flags == UNESCAPE_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) q_real = string_unescape_any(in, out_real, q_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) q_real = string_unescape(in, out_real, q_real, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) out_test, q_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kfree(out_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kfree(out_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kfree(in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct test_string_1 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) const char *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define TEST_STRING_2_MAX_S1 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct test_string_2 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const char *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct test_string_1 s1[TEST_STRING_2_MAX_S1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define TEST_STRING_2_DICT_0 NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const struct test_string_2 escape0[] __initconst = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .in = "\f\\ \n\r\t\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .out = "\\f\\ \\n\\r\\t\\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .flags = ESCAPE_SPACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .out = "\\f\\134\\040\\n\\r\\t\\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .out = "\\f\\x5c\\x20\\n\\r\\t\\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .flags = ESCAPE_SPACE | ESCAPE_HEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .in = "\\h\\\"\a\e\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .out = "\\\\h\\\\\"\\a\\e\\\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .flags = ESCAPE_SPECIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .out = "\\\\\\150\\\\\\042\\a\\e\\\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .flags = ESCAPE_SPECIAL | ESCAPE_HEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .in = "\eb \\C\007\"\x90\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .out = "\eb \\C\007\"\x90\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .flags = ESCAPE_SPACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .out = "\\eb \\\\C\\a\"\x90\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .flags = ESCAPE_SPECIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .out = "\\eb \\\\C\\a\"\x90\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .flags = ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .out = "\eb \\C\007\"\x90\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .flags = ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .out = "\eb \\C\007\"\x90\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .flags = ESCAPE_SPACE | ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .out = "\\eb \\C\\a\"\x90\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .flags = ESCAPE_SPECIAL | ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .out = "\\eb \\C\\a\"\x90\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .out = "\\033b \\C\\007\"\\220\\015]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .flags = ESCAPE_OCTAL | ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .out = "\\033b \\C\\007\"\\220\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .out = "\\eb \\C\\a\"\\220\\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .flags = ESCAPE_SPECIAL | ESCAPE_SPACE | ESCAPE_OCTAL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ESCAPE_NP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .out = "\\x1bb \\C\\x07\"\\x90\\x0d]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .flags = ESCAPE_NP | ESCAPE_HEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) #define TEST_STRING_2_DICT_1 "b\\ \t\r"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static const struct test_string_2 escape1[] __initconst = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .in = "\f\\ \n\r\t\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .out = "\f\\134\\040\n\\015\\011\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .flags = ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .out = "\f\\x5c\\x20\n\\x0d\\x09\v",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .flags = ESCAPE_HEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) .in = "\\h\\\"\a\e\\",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) .out = "\\134h\\134\"\a\e\\134",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .flags = ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .in = "\eb \\C\007\"\x90\r]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .s1 = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .out = "\e\\142\\040\\134C\007\"\x90\\015]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .flags = ESCAPE_OCTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) },{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static const struct test_string strings_upper[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) .in = "abcdefgh1234567890test",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) .out = "ABCDEFGH1234567890TEST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .in = "abCdeFgH1234567890TesT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .out = "ABCDEFGH1234567890TEST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static const struct test_string strings_lower[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .in = "ABCDEFGH1234567890TEST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) .out = "abcdefgh1234567890test",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .in = "abCdeFgH1234567890TesT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .out = "abcdefgh1234567890test",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static __init const char *test_string_find_match(const struct test_string_2 *s2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) const struct test_string_1 *s1 = s2->s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return s2->in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* Test cases are NULL-aware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) flags &= ~ESCAPE_NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* ESCAPE_OCTAL has a higher priority */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (flags & ESCAPE_OCTAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) flags &= ~ESCAPE_HEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (i = 0; i < TEST_STRING_2_MAX_S1 && s1->out; i++, s1++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (s1->flags == flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return s1->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static __init void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) test_string_escape_overflow(const char *in, int p, unsigned int flags, const char *esc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) int q_test, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int q_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (q_real != q_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pr_warn("Test '%s' failed: flags = %u, osz = 0, expected %d, got %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) name, flags, q_test, q_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static __init void test_string_escape(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) const struct test_string_2 *s2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unsigned int flags, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) size_t out_size = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) char *out_test = kmalloc(out_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) char *out_real = kmalloc(out_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) char *in = kmalloc(256, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int p = 0, q_test = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int q_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!out_test || !out_real || !in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) for (; s2->in; s2++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) const char *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* NULL injection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (flags & ESCAPE_NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) in[p++] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) out_test[q_test++] = '\\';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) out_test[q_test++] = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* Don't try strings that have no output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) out = test_string_find_match(s2, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /* Copy string to in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) len = strlen(s2->in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) memcpy(&in[p], s2->in, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) p += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* Copy expected result for given flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) len = strlen(out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) memcpy(&out_test[q_test], out, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) q_test += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) test_string_check_buf(name, flags, in, p, out_real, q_real, out_test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) q_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) test_string_escape_overflow(in, p, flags, esc, q_test, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) kfree(in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) kfree(out_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) kfree(out_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #define string_get_size_maxbuf 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) __test_string_get_size((size), (blk_size), (exp_result10), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) (exp_result2)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static __init void test_string_get_size_check(const char *units,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) const char *exp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) char *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) const u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) const u64 blk_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!memcmp(res, exp, strlen(exp) + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) res[string_get_size_maxbuf - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pr_warn("Test 'test_string_get_size' failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) size, blk_size, units);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) pr_warn("expected: '%s', got '%s'\n", exp, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static __init void __test_string_get_size(const u64 size, const u64 blk_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) const char *exp_result10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) const char *exp_result2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) char buf10[string_get_size_maxbuf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) char buf2[string_get_size_maxbuf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) size, blk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) size, blk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static __init void test_string_get_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* small values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) test_string_get_size_one(0, 512, "0 B", "0 B");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) test_string_get_size_one(1, 512, "512 B", "512 B");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* normal values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* weird block sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* huge values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void __init test_string_upper_lower(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) char *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) for (i = 0; i < ARRAY_SIZE(strings_upper); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) const char *s = strings_upper[i].in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int len = strlen(strings_upper[i].in) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) dst = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) string_upper(dst, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (memcmp(dst, strings_upper[i].out, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) pr_warn("Test 'string_upper' failed : expected %s, got %s!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) strings_upper[i].out, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) kfree(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) kfree(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) for (i = 0; i < ARRAY_SIZE(strings_lower); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) const char *s = strings_lower[i].in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int len = strlen(strings_lower[i].in) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) dst = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) string_lower(dst, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (memcmp(dst, strings_lower[i].out, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) pr_warn("Test 'string_lower failed : : expected %s, got %s!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) strings_lower[i].out, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) kfree(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) kfree(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static int __init test_string_helpers_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) pr_info("Running tests...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) for (i = 0; i < UNESCAPE_ANY + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) test_string_unescape("unescape", i, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) test_string_unescape("unescape inplace",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) get_random_int() % (UNESCAPE_ANY + 1), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* Without dictionary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /* With dictionary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* Test string_get_size() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) test_string_get_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* Test string upper(), string_lower() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) test_string_upper_lower();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) module_init(test_string_helpers_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) MODULE_LICENSE("Dual BSD/GPL");