[PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator

Zongmin Zhou posted 1 patch 1 week, 6 days ago
lib/tests/string_kunit.c | 11 +++++++++++
1 file changed, 11 insertions(+)
[PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator
Posted by Zongmin Zhou 1 week, 6 days ago
From: Zongmin Zhou <zhouzongmin@kylinos.cn>

The existing strnlen() tests only cover strings with a NUL terminator.
Add test cases for strings without a NUL terminator to verify that
strnlen() correctly returns the maximum length when no NUL is found
within the given bound.

Co-developed-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 lib/tests/string_kunit.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index 0819ace5b027..b0d5626a79c8 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -155,6 +155,16 @@ static void string_test_strnlen(struct kunit *test)
 
 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
 		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
+			/* Test strings without NUL terminator */
+			s = buf + buf_size - offset - len;
+			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);
+
+			/* Test strings with NUL terminator */
 			s = buf + buf_size - 1 - offset - len;
 			s[len] = '\0';
 
@@ -169,6 +179,7 @@ static void string_test_strnlen(struct kunit *test)
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len);
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len);
 
+			/* Restore buffer */
 			s[len] = 'A';
 		}
 	}
-- 
2.34.1