1 #include <gtest/gtest.h> 2 #include <string.h> 3 4 constexpr int BUF_SIZE = 20; 5 6 using namespace testing::ext; 7 8 extern "C" void* __memcpy_chk(void* dest, const void* src, size_t count, size_t dst_len); 9 10 class FortifyMemcpychkTest : public testing::Test { SetUp()11 void SetUp() override {} TearDown()12 void TearDown() override {} 13 }; 14 15 /** 16 * @tc.name: __memcpy_chk_001 17 * @tc.desc: Verify whether the __memcpy_chk can correctly process the result and avoid buffer overflow if the specified 18 * buffer size is an invalid value (-1) when connecting strings. 19 * @tc.type: FUNC 20 * */ 21 HWTEST_F(FortifyMemcpychkTest, __memcpy_chk_001, TestSize.Level1) 22 { 23 const char* src = "Hello, World!"; 24 char dest[BUF_SIZE]; 25 26 EXPECT_TRUE(__memcpy_chk(dest, src, strlen(src), static_cast<size_t>(-1))); 27 EXPECT_STREQ(src, dest); 28 } 29 30 /** 31 * @tc.name: __memcpy_chk_002 32 * @tc.desc: Verify that the __memcpy_chk function performs copy operations correctly given parameters and prevents 33 buffer overflows. 34 * @tc.type: FUNC 35 * */ 36 HWTEST_F(FortifyMemcpychkTest, __memcpy_chk_002, TestSize.Level1) 37 { 38 const char* src = "Hello, World!"; 39 char dest[BUF_SIZE]; 40 41 EXPECT_TRUE(__memcpy_chk(dest, src, strlen(src), sizeof(dest))); 42 EXPECT_STREQ(src, dest); 43 }