• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <string.h>
3 
4 constexpr int BUF_SIZE = 20;
5 
6 extern "C" void* __mempcpy_chk(void* dest, const void* src, size_t count, size_t dst_len);
7 
8 using namespace testing::ext;
9 
10 class FortifyMempcpychkTest : public testing::Test {
SetUp()11     void SetUp() override {}
TearDown()12     void TearDown() override {}
13 };
14 
15 /**
16  * @tc.name: __mempcpy_chk_001
17  * @tc.desc: Verify if the function is functioning properly.
18  * @tc.type: FUNC
19  * */
20 HWTEST_F(FortifyMempcpychkTest, __mempcpy_chk_001, TestSize.Level1)
21 {
22     const char* src = "Hello, World!";
23     char dest[BUF_SIZE];
24 
25     EXPECT_TRUE(__mempcpy_chk(dest, src, strlen(src), sizeof(dest)));
26     EXPECT_STREQ(src, dest);
27 }
28 
29 /**
30  * @tc.name: __mempcpy_chk_002
31  * @tc.desc: We will check the length of the target buffer to avoid the risk of buffer overflow.
32  * @tc.type: FUNC
33  * */
34 HWTEST_F(FortifyMempcpychkTest, __mempcpy_chk_002, TestSize.Level1)
35 {
36     const char* src = "Hello, World!";
37     char dest[BUF_SIZE];
38 
39     EXPECT_TRUE(__mempcpy_chk(dest, src, strlen(src), static_cast<size_t>(-1)));
40     EXPECT_STREQ(src, dest);
41 }