@@ -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+
527580static 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