Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45ff337a authored by Andy Shevchenko's avatar Andy Shevchenko Committed by Linus Torvalds
Browse files

lib / string_helpers: refactoring the test suite



This patch prepares test suite for a following update.  It introduces
test_string_check_buf() helper which checks the result and dumps an error.

Signed-off-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: "John W . Linville" <linville@tuxdriver.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d295634e
Loading
Loading
Loading
Loading
+27 −12
Original line number Original line Diff line number Diff line
@@ -10,6 +10,26 @@
#include <linux/string.h>
#include <linux/string.h>
#include <linux/string_helpers.h>
#include <linux/string_helpers.h>


static __init bool test_string_check_buf(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)
{
	if (q_real == q_test && !memcmp(out_test, out_real, q_test))
		return true;

	pr_warn("Test '%s' failed: flags = %u\n", name, flags);

	print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
		       in, p, true);
	print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
		       out_test, q_test, true);
	print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
		       out_real, q_real, true);

	return false;
}

struct test_string {
struct test_string {
	const char *in;
	const char *in;
	const char *out;
	const char *out;
@@ -39,7 +59,8 @@ static const struct test_string strings[] __initconst = {
	},
	},
};
};


static void __init test_string_unescape(unsigned int flags, bool inplace)
static void __init test_string_unescape(const char *name, unsigned int flags,
					bool inplace)
{
{
	char in[256];
	char in[256];
	char out_test[256];
	char out_test[256];
@@ -77,15 +98,8 @@ static void __init test_string_unescape(unsigned int flags, bool inplace)
		q_real = string_unescape(in, out_real, q_real, flags);
		q_real = string_unescape(in, out_real, q_real, flags);
	}
	}


	if (q_real != q_test || memcmp(out_test, out_real, q_test)) {
	test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
		pr_warn("Test failed: flags = %u\n", flags);
			      out_test, q_test);
		print_hex_dump(KERN_WARNING, "Input: ",
			       DUMP_PREFIX_NONE, 16, 1, in, p - 1, true);
		print_hex_dump(KERN_WARNING, "Expected: ",
			       DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true);
		print_hex_dump(KERN_WARNING, "Got: ",
			       DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true);
	}
}
}


static int __init test_string_helpers_init(void)
static int __init test_string_helpers_init(void)
@@ -94,8 +108,9 @@ static int __init test_string_helpers_init(void)


	pr_info("Running tests...\n");
	pr_info("Running tests...\n");
	for (i = 0; i < UNESCAPE_ANY + 1; i++)
	for (i = 0; i < UNESCAPE_ANY + 1; i++)
		test_string_unescape(i, false);
		test_string_unescape("unescape", i, false);
	test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true);
	test_string_unescape("unescape inplace",
			     get_random_int() % (UNESCAPE_ANY + 1), true);


	return -EINVAL;
	return -EINVAL;
}
}