Skip to content

Commit c01c41e

Browse files
committed
string_kunit: Move strtomem KUnit test to string_kunit.c
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>
1 parent 0efc599 commit c01c41e

2 files changed

Lines changed: 54 additions & 53 deletions

File tree

lib/memcpy_kunit.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -493,66 +493,13 @@ static void memmove_overlap_test(struct kunit *test)
493493
}
494494
}
495495

496-
static void strtomem_test(struct kunit *test)
497-
{
498-
static const char input[sizeof(unsigned long)] = "hi";
499-
static const char truncate[] = "this is too long";
500-
struct {
501-
unsigned long canary1;
502-
unsigned char output[sizeof(unsigned long)] __nonstring;
503-
unsigned long canary2;
504-
} wrap;
505-
506-
memset(&wrap, 0xFF, sizeof(wrap));
507-
KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
508-
"bad initial canary value");
509-
KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
510-
"bad initial canary value");
511-
512-
/* Check unpadded copy leaves surroundings untouched. */
513-
strtomem(wrap.output, input);
514-
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
515-
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
516-
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
517-
for (size_t i = 2; i < sizeof(wrap.output); i++)
518-
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
519-
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
520-
521-
/* Check truncated copy leaves surroundings untouched. */
522-
memset(&wrap, 0xFF, sizeof(wrap));
523-
strtomem(wrap.output, truncate);
524-
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
525-
for (size_t i = 0; i < sizeof(wrap.output); i++)
526-
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
527-
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
528-
529-
/* Check padded copy leaves only string padded. */
530-
memset(&wrap, 0xFF, sizeof(wrap));
531-
strtomem_pad(wrap.output, input, 0xAA);
532-
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
533-
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
534-
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
535-
for (size_t i = 2; i < sizeof(wrap.output); i++)
536-
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
537-
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
538-
539-
/* Check truncated padded copy has no padding. */
540-
memset(&wrap, 0xFF, sizeof(wrap));
541-
strtomem(wrap.output, truncate);
542-
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
543-
for (size_t i = 0; i < sizeof(wrap.output); i++)
544-
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
545-
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
546-
}
547-
548496
static struct kunit_case memcpy_test_cases[] = {
549497
KUNIT_CASE(memset_test),
550498
KUNIT_CASE(memcpy_test),
551499
KUNIT_CASE_SLOW(memcpy_large_test),
552500
KUNIT_CASE_SLOW(memmove_test),
553501
KUNIT_CASE_SLOW(memmove_large_test),
554502
KUNIT_CASE_SLOW(memmove_overlap_test),
555-
KUNIT_CASE(strtomem_test),
556503
{}
557504
};
558505

lib/string_kunit.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,59 @@ static void string_test_strlcat(struct kunit *test)
524524
KUNIT_EXPECT_STREQ(test, dest, "fourABE");
525525
}
526526

527+
static void string_test_strtomem(struct kunit *test)
528+
{
529+
static const char input[sizeof(unsigned long)] = "hi";
530+
static const char truncate[] = "this is too long";
531+
struct {
532+
unsigned long canary1;
533+
unsigned char output[sizeof(unsigned long)] __nonstring;
534+
unsigned long canary2;
535+
} wrap;
536+
537+
memset(&wrap, 0xFF, sizeof(wrap));
538+
KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
539+
"bad initial canary value");
540+
KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
541+
"bad initial canary value");
542+
543+
/* Check unpadded copy leaves surroundings untouched. */
544+
strtomem(wrap.output, input);
545+
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
546+
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
547+
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
548+
for (size_t i = 2; i < sizeof(wrap.output); i++)
549+
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
550+
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
551+
552+
/* Check truncated copy leaves surroundings untouched. */
553+
memset(&wrap, 0xFF, sizeof(wrap));
554+
strtomem(wrap.output, truncate);
555+
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
556+
for (size_t i = 0; i < sizeof(wrap.output); i++)
557+
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
558+
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
559+
560+
/* Check padded copy leaves only string padded. */
561+
memset(&wrap, 0xFF, sizeof(wrap));
562+
strtomem_pad(wrap.output, input, 0xAA);
563+
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
564+
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
565+
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
566+
for (size_t i = 2; i < sizeof(wrap.output); i++)
567+
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
568+
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
569+
570+
/* Check truncated padded copy has no padding. */
571+
memset(&wrap, 0xFF, sizeof(wrap));
572+
strtomem(wrap.output, truncate);
573+
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
574+
for (size_t i = 0; i < sizeof(wrap.output); i++)
575+
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
576+
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
577+
}
578+
579+
527580
static void string_test_memtostr(struct kunit *test)
528581
{
529582
char nonstring[7] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
@@ -568,6 +621,7 @@ static struct kunit_case string_test_cases[] = {
568621
KUNIT_CASE(string_test_strcat),
569622
KUNIT_CASE(string_test_strncat),
570623
KUNIT_CASE(string_test_strlcat),
624+
KUNIT_CASE(string_test_strtomem),
571625
KUNIT_CASE(string_test_memtostr),
572626
{}
573627
};

0 commit comments

Comments
 (0)