2024-03-01 20:27:31 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
|
2013-04-30 22:27:30 +00:00
|
|
|
/*
|
|
|
|
* Test cases for lib/string_helpers.c module.
|
|
|
|
*/
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
#include <kunit/test.h>
|
2024-02-29 20:52:30 +00:00
|
|
|
#include <linux/array_size.h>
|
2013-04-30 22:27:30 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/string_helpers.h>
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_string_check_buf(struct kunit *test,
|
|
|
|
const char *name, unsigned int flags,
|
|
|
|
char *in, size_t p,
|
|
|
|
char *out_real, size_t q_real,
|
|
|
|
char *out_test, size_t q_test)
|
2014-10-13 22:55:14 +00:00
|
|
|
{
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name);
|
|
|
|
KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test,
|
|
|
|
"name:%s", name);
|
2014-10-13 22:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 22:27:30 +00:00
|
|
|
struct test_string {
|
|
|
|
const char *in;
|
|
|
|
const char *out;
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static const struct test_string strings[] = {
|
2013-04-30 22:27:30 +00:00
|
|
|
{
|
|
|
|
.in = "\\f\\ \\n\\r\\t\\v",
|
|
|
|
.out = "\f\\ \n\r\t\v",
|
|
|
|
.flags = UNESCAPE_SPACE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
|
|
|
|
.out = " \001\00387\0064\005 \\8aH?7",
|
|
|
|
.flags = UNESCAPE_OCTAL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.in = "\\xv\\xa\\x2c\\xD\\x6f2",
|
|
|
|
.out = "\\xv\n,\ro2",
|
|
|
|
.flags = UNESCAPE_HEX,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.in = "\\h\\\\\\\"\\a\\e\\",
|
|
|
|
.out = "\\h\\\"\a\e\\",
|
|
|
|
.flags = UNESCAPE_SPECIAL,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_string_unescape(struct kunit *test,
|
|
|
|
const char *name, unsigned int flags,
|
|
|
|
bool inplace)
|
2013-04-30 22:27:30 +00:00
|
|
|
{
|
2014-10-13 22:55:16 +00:00
|
|
|
int q_real = 256;
|
2024-03-01 20:27:31 +00:00
|
|
|
char *in = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
|
|
|
char *out_test = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
|
|
|
char *out_real = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
2014-10-13 22:55:16 +00:00
|
|
|
int i, p = 0, q_test = 0;
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
|
2013-04-30 22:27:30 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(strings); i++) {
|
|
|
|
const char *s = strings[i].in;
|
|
|
|
int len = strlen(strings[i].in);
|
|
|
|
|
|
|
|
/* Copy string to in buffer */
|
|
|
|
memcpy(&in[p], s, len);
|
|
|
|
p += len;
|
|
|
|
|
|
|
|
/* Copy expected result for given flags */
|
|
|
|
if (flags & strings[i].flags) {
|
|
|
|
s = strings[i].out;
|
|
|
|
len = strlen(strings[i].out);
|
|
|
|
}
|
|
|
|
memcpy(&out_test[q_test], s, len);
|
|
|
|
q_test += len;
|
|
|
|
}
|
|
|
|
in[p++] = '\0';
|
|
|
|
|
|
|
|
/* Call string_unescape and compare result */
|
|
|
|
if (inplace) {
|
|
|
|
memcpy(out_real, in, p);
|
|
|
|
if (flags == UNESCAPE_ANY)
|
|
|
|
q_real = string_unescape_any_inplace(out_real);
|
|
|
|
else
|
|
|
|
q_real = string_unescape_inplace(out_real, flags);
|
|
|
|
} else if (flags == UNESCAPE_ANY) {
|
|
|
|
q_real = string_unescape_any(in, out_real, q_real);
|
|
|
|
} else {
|
|
|
|
q_real = string_unescape(in, out_real, q_real, flags);
|
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real,
|
2014-10-13 22:55:14 +00:00
|
|
|
out_test, q_test);
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct test_string_1 {
|
|
|
|
const char *out;
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TEST_STRING_2_MAX_S1 32
|
|
|
|
struct test_string_2 {
|
|
|
|
const char *in;
|
|
|
|
struct test_string_1 s1[TEST_STRING_2_MAX_S1];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TEST_STRING_2_DICT_0 NULL
|
2024-03-01 20:27:31 +00:00
|
|
|
static const struct test_string_2 escape0[] = {{
|
2014-10-13 22:55:16 +00:00
|
|
|
.in = "\f\\ \n\r\t\v",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\\f\\ \\n\\r\\t\\v",
|
|
|
|
.flags = ESCAPE_SPACE,
|
|
|
|
},{
|
|
|
|
.out = "\\f\\134\\040\\n\\r\\t\\v",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
|
|
|
|
},{
|
|
|
|
.out = "\\f\\x5c\\x20\\n\\r\\t\\v",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_HEX,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
.in = "\\h\\\"\a\e\\",
|
|
|
|
.s1 = {{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\\\h\\\\\\\"\\a\\e\\\\",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPECIAL,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\\\\\150\\\\\\\"\\a\\e\\\\",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\\\\\x68\\\\\\\"\\a\\e\\\\",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_HEX,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
.in = "\eb \\C\007\"\x90\r]",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\eb \\C\007\"\x90\\r]",
|
|
|
|
.flags = ESCAPE_SPACE,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\eb \\\\C\\a\\\"\x90\r]",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPECIAL,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\eb \\\\C\\a\\\"\x90\\r]",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
|
|
|
|
},{
|
|
|
|
.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
|
|
|
|
.flags = ESCAPE_OCTAL,
|
|
|
|
},{
|
|
|
|
.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\015\\135",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
|
|
|
|
},{
|
2021-06-15 16:52:45 +00:00
|
|
|
.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\r\\135",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
|
|
|
|
},{
|
|
|
|
.out = "\eb \\C\007\"\x90\r]",
|
|
|
|
.flags = ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\eb \\C\007\"\x90\\r]",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\eb \\C\\a\"\x90\r]",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\eb \\C\\a\"\x90\\r]",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\033b \\C\\007\"\\220\\015]",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\033b \\C\\007\"\\220\\r]",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\eb \\C\\a\"\\220\\r]",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_SPACE | ESCAPE_OCTAL |
|
|
|
|
ESCAPE_NP,
|
|
|
|
},{
|
|
|
|
.out = "\\x1bb \\C\\x07\"\\x90\\x0d]",
|
|
|
|
.flags = ESCAPE_NP | ESCAPE_HEX,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2021-07-01 01:55:29 +00:00
|
|
|
},{
|
|
|
|
.in = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\007 \eb\"\\220\\317\r",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\\x90\\xcf\r",
|
|
|
|
.flags = ESCAPE_HEX | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
/* terminator */
|
|
|
|
}};
|
|
|
|
|
2021-07-01 01:55:29 +00:00
|
|
|
#define TEST_STRING_2_DICT_1 "b\\ \t\r\xCF"
|
2024-03-01 20:27:31 +00:00
|
|
|
static const struct test_string_2 escape1[] = {{
|
2014-10-13 22:55:16 +00:00
|
|
|
.in = "\f\\ \n\r\t\v",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\f\\134\\040\n\\015\\011\v",
|
|
|
|
.flags = ESCAPE_OCTAL,
|
|
|
|
},{
|
|
|
|
.out = "\f\\x5c\\x20\n\\x0d\\x09\v",
|
|
|
|
.flags = ESCAPE_HEX,
|
2021-07-01 01:55:29 +00:00
|
|
|
},{
|
|
|
|
.out = "\f\\134\\040\n\\015\\011\v",
|
|
|
|
.flags = ESCAPE_ANY | ESCAPE_APPEND,
|
|
|
|
},{
|
|
|
|
.out = "\\014\\134\\040\\012\\015\\011\\013",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\\x0c\\x5c\\x20\\x0a\\x0d\\x09\\x0b",
|
|
|
|
.flags = ESCAPE_HEX | ESCAPE_APPEND | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\f\\134\\040\n\\015\\011\v",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\f\\x5c\\x20\n\\x0d\\x09\v",
|
|
|
|
.flags = ESCAPE_HEX | ESCAPE_APPEND | ESCAPE_NA,
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
2021-07-01 01:55:29 +00:00
|
|
|
.in = "\\h\\\"\a\xCF\e\\",
|
2014-10-13 22:55:16 +00:00
|
|
|
.s1 = {{
|
2021-07-01 01:55:29 +00:00
|
|
|
.out = "\\134h\\134\"\a\\317\e\\134",
|
2014-10-13 22:55:16 +00:00
|
|
|
.flags = ESCAPE_OCTAL,
|
2021-07-01 01:55:29 +00:00
|
|
|
},{
|
|
|
|
.out = "\\134h\\134\"\a\\317\e\\134",
|
|
|
|
.flags = ESCAPE_ANY | ESCAPE_APPEND,
|
|
|
|
},{
|
|
|
|
.out = "\\134h\\134\"\\007\\317\\033\\134",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\\134h\\134\"\a\\317\e\\134",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NA,
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
.in = "\eb \\C\007\"\x90\r]",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\e\\142\\040\\134C\007\"\x90\\015]",
|
|
|
|
.flags = ESCAPE_OCTAL,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
2021-07-01 01:55:26 +00:00
|
|
|
}}
|
2021-07-01 01:55:29 +00:00
|
|
|
},{
|
|
|
|
.in = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\r",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\r",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\r",
|
|
|
|
.flags = ESCAPE_ANY | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\r",
|
|
|
|
.flags = ESCAPE_HEX | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_HEX | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\r",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NA,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
|
|
|
}}
|
|
|
|
},{
|
|
|
|
.in = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.s1 = {{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\r",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\xCF\\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\\015",
|
|
|
|
.flags = ESCAPE_OCTAL | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\\015",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\317\r",
|
|
|
|
.flags = ESCAPE_ANY | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\\x0d",
|
|
|
|
.flags = ESCAPE_HEX | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_HEX | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\\x0d",
|
|
|
|
.flags = ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
.out = "\007 \eb\"\x90\\xcf\\r",
|
|
|
|
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NAP,
|
|
|
|
},{
|
|
|
|
/* terminator */
|
|
|
|
}}
|
2014-10-13 22:55:16 +00:00
|
|
|
},{
|
|
|
|
/* terminator */
|
|
|
|
}};
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static const struct test_string strings_upper[] = {
|
2020-07-14 12:01:54 +00:00
|
|
|
{
|
|
|
|
.in = "abcdefgh1234567890test",
|
|
|
|
.out = "ABCDEFGH1234567890TEST",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.in = "abCdeFgH1234567890TesT",
|
|
|
|
.out = "ABCDEFGH1234567890TEST",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static const struct test_string strings_lower[] = {
|
2020-07-14 12:01:54 +00:00
|
|
|
{
|
|
|
|
.in = "ABCDEFGH1234567890TEST",
|
|
|
|
.out = "abcdefgh1234567890test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.in = "abCdeFgH1234567890TesT",
|
|
|
|
.out = "abcdefgh1234567890test",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static const char *test_string_find_match(const struct test_string_2 *s2,
|
|
|
|
unsigned int flags)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
const struct test_string_1 *s1 = s2->s1;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!flags)
|
|
|
|
return s2->in;
|
|
|
|
|
|
|
|
/* Test cases are NULL-aware */
|
|
|
|
flags &= ~ESCAPE_NULL;
|
|
|
|
|
|
|
|
/* ESCAPE_OCTAL has a higher priority */
|
|
|
|
if (flags & ESCAPE_OCTAL)
|
|
|
|
flags &= ~ESCAPE_HEX;
|
|
|
|
|
|
|
|
for (i = 0; i < TEST_STRING_2_MAX_S1 && s1->out; i++, s1++)
|
|
|
|
if (s1->flags == flags)
|
|
|
|
return s1->out;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void
|
|
|
|
test_string_escape_overflow(struct kunit *test,
|
|
|
|
const char *in, int p, unsigned int flags, const char *esc,
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
int q_test, const char *name)
|
|
|
|
{
|
|
|
|
int q_real;
|
|
|
|
|
|
|
|
q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s: flags:%#x", name, flags);
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_string_escape(struct kunit *test, const char *name,
|
|
|
|
const struct test_string_2 *s2,
|
|
|
|
unsigned int flags, const char *esc)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
size_t out_size = 512;
|
2024-03-01 20:27:31 +00:00
|
|
|
char *out_test = kunit_kzalloc(test, out_size, GFP_KERNEL);
|
|
|
|
char *out_real = kunit_kzalloc(test, out_size, GFP_KERNEL);
|
|
|
|
char *in = kunit_kzalloc(test, 256, GFP_KERNEL);
|
2014-10-13 22:55:16 +00:00
|
|
|
int p = 0, q_test = 0;
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
int q_real;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
for (; s2->in; s2++) {
|
|
|
|
const char *out;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* NULL injection */
|
|
|
|
if (flags & ESCAPE_NULL) {
|
|
|
|
in[p++] = '\0';
|
2021-07-01 01:55:29 +00:00
|
|
|
/* '\0' passes isascii() test */
|
|
|
|
if (flags & ESCAPE_NA && !(flags & ESCAPE_APPEND && esc)) {
|
|
|
|
out_test[q_test++] = '\0';
|
|
|
|
} else {
|
|
|
|
out_test[q_test++] = '\\';
|
|
|
|
out_test[q_test++] = '0';
|
|
|
|
}
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't try strings that have no output */
|
|
|
|
out = test_string_find_match(s2, flags);
|
|
|
|
if (!out)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Copy string to in buffer */
|
|
|
|
len = strlen(s2->in);
|
|
|
|
memcpy(&in[p], s2->in, len);
|
|
|
|
p += len;
|
|
|
|
|
|
|
|
/* Copy expected result for given flags */
|
|
|
|
len = strlen(out);
|
|
|
|
memcpy(&out_test[q_test], out, len);
|
|
|
|
q_test += len;
|
|
|
|
}
|
|
|
|
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_test,
|
2014-10-13 22:55:16 +00:00
|
|
|
q_test);
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_escape_overflow(test, in, p, flags, esc, q_test, name);
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 00:31:14 +00:00
|
|
|
#define string_get_size_maxbuf 16
|
2024-03-01 20:27:31 +00:00
|
|
|
#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
|
|
|
|
do { \
|
|
|
|
BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
|
|
|
|
BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
|
|
|
|
__test_string_get_size(test, (size), (blk_size), (exp_result10), \
|
|
|
|
(exp_result2)); \
|
2015-11-07 00:31:14 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_string_get_size_check(struct kunit *test,
|
|
|
|
const char *units,
|
|
|
|
const char *exp,
|
|
|
|
char *res,
|
|
|
|
const u64 size,
|
|
|
|
const u64 blk_size)
|
2015-11-07 00:31:14 +00:00
|
|
|
{
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_EXPECT_MEMEQ_MSG(test, res, exp, strlen(exp) + 1,
|
|
|
|
"string_get_size(size = %llu, blk_size = %llu, units = %s)",
|
2015-11-07 00:31:14 +00:00
|
|
|
size, blk_size, units);
|
2016-02-03 00:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void __strchrcut(char *dst, const char *src, const char *cut)
|
2024-02-29 20:52:30 +00:00
|
|
|
{
|
|
|
|
const char *from = src;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = strcspn(from, cut);
|
|
|
|
memcpy(dst, from, len);
|
|
|
|
dst += len;
|
|
|
|
from += len;
|
|
|
|
} while (*from++);
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void __test_string_get_size_one(struct kunit *test,
|
|
|
|
const u64 size, const u64 blk_size,
|
|
|
|
const char *exp_result10,
|
|
|
|
const char *exp_result2,
|
|
|
|
enum string_size_units units,
|
|
|
|
const char *cut)
|
2016-02-03 00:57:18 +00:00
|
|
|
{
|
|
|
|
char buf10[string_get_size_maxbuf];
|
|
|
|
char buf2[string_get_size_maxbuf];
|
2024-02-29 20:52:30 +00:00
|
|
|
char exp10[string_get_size_maxbuf];
|
|
|
|
char exp2[string_get_size_maxbuf];
|
|
|
|
char prefix10[64];
|
|
|
|
char prefix2[64];
|
|
|
|
|
|
|
|
sprintf(prefix10, "STRING_UNITS_10 [%s]", cut);
|
|
|
|
sprintf(prefix2, "STRING_UNITS_2 [%s]", cut);
|
2016-02-03 00:57:18 +00:00
|
|
|
|
2024-02-29 20:52:30 +00:00
|
|
|
__strchrcut(exp10, exp_result10, cut);
|
|
|
|
__strchrcut(exp2, exp_result2, cut);
|
2016-02-03 00:57:18 +00:00
|
|
|
|
2024-02-29 20:52:30 +00:00
|
|
|
string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(buf10));
|
|
|
|
string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2));
|
2016-02-03 00:57:18 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_get_size_check(test, prefix10, exp10, buf10, size, blk_size);
|
|
|
|
test_string_get_size_check(test, prefix2, exp2, buf2, size, blk_size);
|
2024-02-29 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void __test_string_get_size(struct kunit *test,
|
|
|
|
const u64 size, const u64 blk_size,
|
|
|
|
const char *exp_result10,
|
|
|
|
const char *exp_result2)
|
2024-02-29 20:52:30 +00:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
enum string_size_units units;
|
|
|
|
const char *cut;
|
|
|
|
} get_size_test_cases[] = {
|
|
|
|
{ 0, "" },
|
|
|
|
{ STRING_UNITS_NO_SPACE, " " },
|
|
|
|
{ STRING_UNITS_NO_SPACE | STRING_UNITS_NO_BYTES, " B" },
|
|
|
|
{ STRING_UNITS_NO_BYTES, "B" },
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(get_size_test_cases); i++)
|
2024-03-01 20:27:31 +00:00
|
|
|
__test_string_get_size_one(test, size, blk_size,
|
|
|
|
exp_result10, exp_result2,
|
2024-02-29 20:52:30 +00:00
|
|
|
get_size_test_cases[i].units,
|
|
|
|
get_size_test_cases[i].cut);
|
2015-11-07 00:31:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_get_size(struct kunit *test)
|
2015-11-07 00:31:14 +00:00
|
|
|
{
|
2016-02-03 00:57:18 +00:00
|
|
|
/* small values */
|
|
|
|
test_string_get_size_one(0, 512, "0 B", "0 B");
|
|
|
|
test_string_get_size_one(1, 512, "512 B", "512 B");
|
|
|
|
test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
|
|
|
|
|
|
|
|
/* normal values */
|
|
|
|
test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
|
|
|
|
test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
|
|
|
|
test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
|
|
|
|
|
|
|
|
/* weird block sizes */
|
|
|
|
test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
|
|
|
|
|
|
|
|
/* huge values */
|
|
|
|
test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
|
|
|
|
test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
|
2015-11-07 00:31:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_upper_lower(struct kunit *test)
|
2020-07-14 12:01:54 +00:00
|
|
|
{
|
|
|
|
char *dst;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(strings_upper); i++) {
|
|
|
|
const char *s = strings_upper[i].in;
|
|
|
|
int len = strlen(strings_upper[i].in) + 1;
|
|
|
|
|
|
|
|
dst = kmalloc(len, GFP_KERNEL);
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_ASSERT_NOT_NULL(test, dst);
|
2020-07-14 12:01:54 +00:00
|
|
|
|
|
|
|
string_upper(dst, s);
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_EXPECT_STREQ(test, dst, strings_upper[i].out);
|
2020-07-14 12:01:54 +00:00
|
|
|
kfree(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(strings_lower); i++) {
|
|
|
|
const char *s = strings_lower[i].in;
|
|
|
|
int len = strlen(strings_lower[i].in) + 1;
|
|
|
|
|
|
|
|
dst = kmalloc(len, GFP_KERNEL);
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_ASSERT_NOT_NULL(test, dst);
|
2020-07-14 12:01:54 +00:00
|
|
|
|
|
|
|
string_lower(dst, s);
|
2024-03-01 20:27:31 +00:00
|
|
|
KUNIT_EXPECT_STREQ(test, dst, strings_lower[i].out);
|
2020-07-14 12:01:54 +00:00
|
|
|
kfree(dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static void test_unescape(struct kunit *test)
|
2013-04-30 22:27:30 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2021-07-01 01:55:29 +00:00
|
|
|
for (i = 0; i < UNESCAPE_ALL_MASK + 1; i++)
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_unescape(test, "unescape", i, false);
|
|
|
|
test_string_unescape(test, "unescape inplace",
|
2023-03-27 14:26:04 +00:00
|
|
|
get_random_u32_below(UNESCAPE_ALL_MASK + 1), true);
|
2013-04-30 22:27:30 +00:00
|
|
|
|
2014-10-13 22:55:16 +00:00
|
|
|
/* Without dictionary */
|
2021-07-01 01:55:29 +00:00
|
|
|
for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_escape(test, "escape 0", escape0, i, TEST_STRING_2_DICT_0);
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
/* With dictionary */
|
2021-07-01 01:55:29 +00:00
|
|
|
for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
|
2024-03-01 20:27:31 +00:00
|
|
|
test_string_escape(test, "escape 1", escape1, i, TEST_STRING_2_DICT_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kunit_case string_helpers_test_cases[] = {
|
|
|
|
KUNIT_CASE(test_get_size),
|
|
|
|
KUNIT_CASE(test_upper_lower),
|
|
|
|
KUNIT_CASE(test_unescape),
|
|
|
|
{}
|
|
|
|
};
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
static struct kunit_suite string_helpers_test_suite = {
|
|
|
|
.name = "string_helpers",
|
|
|
|
.test_cases = string_helpers_test_cases,
|
|
|
|
};
|
2015-11-07 00:31:14 +00:00
|
|
|
|
2024-03-01 20:27:31 +00:00
|
|
|
kunit_test_suites(&string_helpers_test_suite);
|
2020-07-14 12:01:54 +00:00
|
|
|
|
2024-05-31 23:07:26 +00:00
|
|
|
MODULE_DESCRIPTION("Test cases for string helpers module");
|
2013-04-30 22:27:30 +00:00
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|