string_kunit: Move strtomem KUnit test to string_kunit.c
authorKees Cook <keescook@chromium.org>
Wed, 24 Apr 2024 16:01:30 +0000 (09:01 -0700)
committerKees Cook <keescook@chromium.org>
Wed, 24 Apr 2024 16:02:34 +0000 (09:02 -0700)
It is more logical to have the strtomem() test in string_kunit.c instead
of the memcpy() suite. Move it to live with memtostr().

Signed-off-by: Kees Cook <keescook@chromium.org>
lib/memcpy_kunit.c
lib/string_kunit.c

index fd16e6ce53d1dbc788ed7fd955cba58ef04ee517..20ea9038c3fff65f65c9f7e4e871331e3342db20 100644 (file)
@@ -493,58 +493,6 @@ static void memmove_overlap_test(struct kunit *test)
        }
 }
 
-static void strtomem_test(struct kunit *test)
-{
-       static const char input[sizeof(unsigned long)] = "hi";
-       static const char truncate[] = "this is too long";
-       struct {
-               unsigned long canary1;
-               unsigned char output[sizeof(unsigned long)] __nonstring;
-               unsigned long canary2;
-       } wrap;
-
-       memset(&wrap, 0xFF, sizeof(wrap));
-       KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
-                           "bad initial canary value");
-       KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
-                           "bad initial canary value");
-
-       /* Check unpadded copy leaves surroundings untouched. */
-       strtomem(wrap.output, input);
-       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
-       KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
-       KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
-       for (size_t i = 2; i < sizeof(wrap.output); i++)
-               KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
-       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
-
-       /* Check truncated copy leaves surroundings untouched. */
-       memset(&wrap, 0xFF, sizeof(wrap));
-       strtomem(wrap.output, truncate);
-       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
-       for (size_t i = 0; i < sizeof(wrap.output); i++)
-               KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
-       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
-
-       /* Check padded copy leaves only string padded. */
-       memset(&wrap, 0xFF, sizeof(wrap));
-       strtomem_pad(wrap.output, input, 0xAA);
-       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
-       KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
-       KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
-       for (size_t i = 2; i < sizeof(wrap.output); i++)
-               KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
-       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
-
-       /* Check truncated padded copy has no padding. */
-       memset(&wrap, 0xFF, sizeof(wrap));
-       strtomem(wrap.output, truncate);
-       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
-       for (size_t i = 0; i < sizeof(wrap.output); i++)
-               KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
-       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
-}
-
 static struct kunit_case memcpy_test_cases[] = {
        KUNIT_CASE(memset_test),
        KUNIT_CASE(memcpy_test),
@@ -552,7 +500,6 @@ static struct kunit_case memcpy_test_cases[] = {
        KUNIT_CASE_SLOW(memmove_test),
        KUNIT_CASE_SLOW(memmove_large_test),
        KUNIT_CASE_SLOW(memmove_overlap_test),
-       KUNIT_CASE(strtomem_test),
        {}
 };
 
index fadad46c1b05fda8f7d0ef350c7ac77cd7ea5d9b..2a812decf14bd3149d518d7970a41ef7407245ed 100644 (file)
@@ -524,6 +524,59 @@ static void string_test_strlcat(struct kunit *test)
        KUNIT_EXPECT_STREQ(test, dest, "fourABE");
 }
 
+static void string_test_strtomem(struct kunit *test)
+{
+       static const char input[sizeof(unsigned long)] = "hi";
+       static const char truncate[] = "this is too long";
+       struct {
+               unsigned long canary1;
+               unsigned char output[sizeof(unsigned long)] __nonstring;
+               unsigned long canary2;
+       } wrap;
+
+       memset(&wrap, 0xFF, sizeof(wrap));
+       KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
+                           "bad initial canary value");
+       KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
+                           "bad initial canary value");
+
+       /* Check unpadded copy leaves surroundings untouched. */
+       strtomem(wrap.output, input);
+       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
+       KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
+       KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
+       for (size_t i = 2; i < sizeof(wrap.output); i++)
+               KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
+       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
+
+       /* Check truncated copy leaves surroundings untouched. */
+       memset(&wrap, 0xFF, sizeof(wrap));
+       strtomem(wrap.output, truncate);
+       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
+       for (size_t i = 0; i < sizeof(wrap.output); i++)
+               KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
+       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
+
+       /* Check padded copy leaves only string padded. */
+       memset(&wrap, 0xFF, sizeof(wrap));
+       strtomem_pad(wrap.output, input, 0xAA);
+       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
+       KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
+       KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
+       for (size_t i = 2; i < sizeof(wrap.output); i++)
+               KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
+       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
+
+       /* Check truncated padded copy has no padding. */
+       memset(&wrap, 0xFF, sizeof(wrap));
+       strtomem(wrap.output, truncate);
+       KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
+       for (size_t i = 0; i < sizeof(wrap.output); i++)
+               KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
+       KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
+}
+
+
 static void string_test_memtostr(struct kunit *test)
 {
        char nonstring[7] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
@@ -568,6 +621,7 @@ static struct kunit_case string_test_cases[] = {
        KUNIT_CASE(string_test_strcat),
        KUNIT_CASE(string_test_strncat),
        KUNIT_CASE(string_test_strlcat),
+       KUNIT_CASE(string_test_strtomem),
        KUNIT_CASE(string_test_memtostr),
        {}
 };