924 lines · 29.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Test cases for string functions. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <kunit/test.h>#include <linux/ktime.h>#include <linux/math64.h>#include <linux/minmax.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/prandom.h>#include <linux/printk.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/time64.h>#include <linux/units.h>#include <linux/vmalloc.h> #define STRCMP_LARGE_BUF_LEN 2048#define STRCMP_CHANGE_POINT 1337#define STRCMP_TEST_EXPECT_EQUAL(test, fn, ...) KUNIT_EXPECT_EQ(test, fn(__VA_ARGS__), 0)#define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0)#define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0) #define STRING_TEST_MAX_LEN	128#define STRING_TEST_MAX_OFFSET	16 #define STRING_BENCH_SEED	888#define STRING_BENCH_WORKLOAD	(1 * MEGA) static void string_test_memset16(struct kunit *test){	unsigned i, j, k;	u16 v, *p; 	p = kunit_kzalloc(test, 256 * 2 * 2, GFP_KERNEL);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 	for (i = 0; i < 256; i++) {		for (j = 0; j < 256; j++) {			memset(p, 0xa1, 256 * 2 * sizeof(v));			memset16(p + i, 0xb1b2, j);			for (k = 0; k < 512; k++) {				v = p[k];				if (k < i) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,						"i:%d j:%d k:%d", i, j, k);				} else if (k < i + j) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2,						"i:%d j:%d k:%d", i, j, k);				} else {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,						"i:%d j:%d k:%d", i, j, k);				}			}		}	}} static void string_test_memset32(struct kunit *test){	unsigned i, j, k;	u32 v, *p; 	p = kunit_kzalloc(test, 256 * 2 * 4, GFP_KERNEL);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 	for (i = 0; i < 256; i++) {		for (j = 0; j < 256; j++) {			memset(p, 0xa1, 256 * 2 * sizeof(v));			memset32(p + i, 0xb1b2b3b4, j);			for (k = 0; k < 512; k++) {				v = p[k];				if (k < i) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,						"i:%d j:%d k:%d", i, j, k);				} else if (k < i + j) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4,						"i:%d j:%d k:%d", i, j, k);				} else {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,						"i:%d j:%d k:%d", i, j, k);				}			}		}	}} static void string_test_memset64(struct kunit *test){	unsigned i, j, k;	u64 v, *p; 	p = kunit_kzalloc(test, 256 * 2 * 8, GFP_KERNEL);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 	for (i = 0; i < 256; i++) {		for (j = 0; j < 256; j++) {			memset(p, 0xa1, 256 * 2 * sizeof(v));			memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);			for (k = 0; k < 512; k++) {				v = p[k];				if (k < i) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,						"i:%d j:%d k:%d", i, j, k);				} else if (k < i + j) {					KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4b5b6b7b8ULL,						"i:%d j:%d k:%d", i, j, k);				} else {					KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,						"i:%d j:%d k:%d", i, j, k);				}			}		}	}} static void string_test_strlen(struct kunit *test){	size_t buf_size;	char *buf, *s; 	buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);	buf = vmalloc(buf_size);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 	memset(buf, 'A', buf_size); 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {			s = buf + buf_size - 1 - offset - len;			s[len] = '\0';			KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,				"offset:%zu len:%zu", offset, len);			s[len] = 'A';		}	} 	vfree(buf);} static void string_test_strnlen(struct kunit *test){	size_t buf_size;	char *buf, *s; 	buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);	buf = vmalloc(buf_size);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 	memset(buf, 'A', buf_size); 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {			s = buf + buf_size - 1 - offset - len;			s[len] = '\0'; 			if (len > 0)				KUNIT_EXPECT_EQ(test, strnlen(s, len - 1), len - 1);			if (len > 1)				KUNIT_EXPECT_EQ(test, strnlen(s, len - 2), len - 2); 			KUNIT_EXPECT_EQ(test, strnlen(s, len), len); 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 1), len);			KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len);			KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len); 			s[len] = 'A';		}	} 	vfree(buf);} static void string_test_strchr(struct kunit *test){	const char *test_string = "abcdefghijkl";	const char *empty_string = "";	char *result;	int i; 	for (i = 0; i < strlen(test_string) + 1; i++) {		result = strchr(test_string, test_string[i]);		KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,				    "char:%c", 'a' + i);	} 	result = strchr(empty_string, '\0');	KUNIT_ASSERT_PTR_EQ(test, result, empty_string); 	result = strchr(empty_string, 'a');	KUNIT_ASSERT_NULL(test, result); 	result = strchr(test_string, 'z');	KUNIT_ASSERT_NULL(test, result);} static void string_test_strrchr(struct kunit *test){	size_t buf_size;	char *buf, *s; 	buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);	buf = vmalloc(buf_size);	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 	memset(buf, 'A', buf_size); 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {			s = buf + buf_size - 1 - offset - len;			s[len] = '\0'; 			KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'Z'), NULL); 			if (len > 0)				KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), s + len - 1);			else				KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), NULL); 			s[len] = 'A';		}	} 	vfree(buf);} static void string_test_strnchr(struct kunit *test){	const char *test_string = "abcdefghijkl";	const char *empty_string = "";	char *result;	int i, j; 	for (i = 0; i < strlen(test_string) + 1; i++) {		for (j = 0; j < strlen(test_string) + 2; j++) {			result = strnchr(test_string, j, test_string[i]);			if (j <= i) {				KUNIT_ASSERT_NULL_MSG(test, result,					"char:%c i:%d j:%d", 'a' + i, i, j);			} else {				KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,					"char:%c i:%d j:%d", 'a' + i, i, j);			}		}	} 	result = strnchr(empty_string, 0, '\0');	KUNIT_ASSERT_NULL(test, result); 	result = strnchr(empty_string, 1, '\0');	KUNIT_ASSERT_PTR_EQ(test, result, empty_string); 	result = strnchr(empty_string, 1, 'a');	KUNIT_ASSERT_NULL(test, result); 	result = strnchr(NULL, 0, '\0');	KUNIT_ASSERT_NULL(test, result);} static void string_test_strspn(struct kunit *test){	static const struct strspn_test {		const char str[16];		const char accept[16];		const char reject[16];		unsigned a;		unsigned r;	} tests[] = {		{ "foobar", "", "", 0, 6 },		{ "abba", "abc", "ABBA", 4, 4 },		{ "abba", "a", "b", 1, 1 },		{ "", "abc", "abc", 0, 0},	};	const struct strspn_test *s = tests;	size_t i; 	for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) {		KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept),			"i:%zu", i);		KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject),			"i:%zu", i);	}} static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN];static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN]; static void strcmp_fill_buffers(char fill1, char fill2){	memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN);	memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN);	strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0;	strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0;} static void string_test_strcmp(struct kunit *test){	/* Equal strings */	STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!");	/* First string is lexicographically less than the second */	STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!");	/* First string is lexicographically larger than the second */	STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!");	/* Empty string is always lexicographically less than any non-empty string */	STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string");	/* Two empty strings should be equal */	STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", "");	/* Compare two strings which have only one char difference */	STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba");	/* Compare two strings which have the same prefix*/	STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else");} static void string_test_strcmp_long_strings(struct kunit *test){	strcmp_fill_buffers('B', 'B');	STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';	STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';	STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2);} static void string_test_strncmp(struct kunit *test){	/* Equal strings */	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13);	/* First string is lexicographically less than the second */	STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13);	/* Result is always 'equal' when count = 0 */	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0);	/* Strings with common prefix are equal if count = length of prefix */	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3);	/* Strings with common prefix are not equal when count = length of prefix + 1 */	STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4);	/* If one string is a prefix of another, the shorter string is lexicographically smaller */	STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else",				 strlen("Just a string and something else"));	/*	 * If one string is a prefix of another, and we check first length	 * of prefix chars, the result is 'equal'	 */	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else",				 strlen("Just a string"));} static void string_test_strncmp_long_strings(struct kunit *test){	strcmp_fill_buffers('B', 'B');	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';	STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';	STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,				   strcmp_buffer2, STRCMP_LARGE_BUF_LEN);	/* the strings are equal up to STRCMP_CHANGE_POINT */	STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_CHANGE_POINT);	STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,				   strcmp_buffer2, STRCMP_CHANGE_POINT + 1);} static void string_test_strcasecmp(struct kunit *test){	/* Same strings in different case should be equal */	STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!");	/* Empty strings should be equal */	STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", "");	/* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */	STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B");	STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a");	/* Special symbols and numbers should be processed correctly */	STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^");} static void string_test_strcasecmp_long_strings(struct kunit *test){	strcmp_fill_buffers('b', 'B');	STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';	STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';	STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);} static void string_test_strncasecmp(struct kunit *test){	/* Same strings in different case should be equal */	STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba"));	/* strncasecmp should check 'count' chars only */	STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5);	STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1);	STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1);	/* Result is always 'equal' when count = 0 */	STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0);} static void string_test_strncasecmp_long_strings(struct kunit *test){	strcmp_fill_buffers('b', 'B');	STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';	STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 	strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';	STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,				   strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 	STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,				 strcmp_buffer2, STRCMP_CHANGE_POINT);	STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,				   strcmp_buffer2, STRCMP_CHANGE_POINT + 1);} /** * strscpy_check() - Run a specific test case. * @test: KUnit test context pointer * @src: Source string, argument to strscpy_pad() * @count: Size of destination buffer, argument to strscpy_pad() * @expected: Expected return value from call to strscpy_pad() * @chars: Number of characters from the src string expected to be *         written to the dst buffer. * @terminator: 1 if there should be a terminating null byte 0 otherwise. * @pad: Number of pad characters expected (in the tail of dst buffer). *       (@pad does not include the null terminator byte.) * * Calls strscpy_pad() and verifies the return value and state of the * destination buffer after the call returns. */static void strscpy_check(struct kunit *test, char *src, int count,			  int expected, int chars, int terminator, int pad){	int nr_bytes_poison;	int max_expected;	int max_count;	int written;	char buf[6];	int index, i;	const char POISON = 'z'; 	KUNIT_ASSERT_TRUE_MSG(test, src != NULL,			      "null source string not supported"); 	memset(buf, POISON, sizeof(buf));	/* Future proofing test suite, validate args */	max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */	max_expected = count - 1;    /* Space for the null */ 	KUNIT_ASSERT_LE_MSG(test, count, max_count,		"count (%d) is too big (%d) ... aborting", count, max_count);	KUNIT_EXPECT_LE_MSG(test, expected, max_expected,		"expected (%d) is bigger than can possibly be returned (%d)",		expected, max_expected); 	written = strscpy_pad(buf, src, count);	KUNIT_ASSERT_EQ(test, written, expected); 	if (count && written == -E2BIG) {		KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1),			"buffer state invalid for -E2BIG");		KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',			"too big string is not null terminated correctly");	} 	for (i = 0; i < chars; i++)		KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i],			"buf[i]==%c != src[i]==%c", buf[i], src[i]); 	if (terminator)		KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',			"string is not null terminated correctly"); 	for (i = 0; i < pad; i++) {		index = chars + terminator + i;		KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0',			"padding missing at index: %d", i);	} 	nr_bytes_poison = sizeof(buf) - chars - terminator - pad;	for (i = 0; i < nr_bytes_poison; i++) {		index = sizeof(buf) - 1 - i; /* Check from the end back */		KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON,			"poison value missing at index: %d", i);	}

Lines 1–500 of 924.