• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <string.h>
3 
4 constexpr int BUF_SIZE = 10;
5 
6 using namespace testing::ext;
7 
8 class FortifyStrncpychkTest : public testing::Test {
SetUp()9     void SetUp() override {}
TearDown()10     void TearDown() override {}
11 };
12 
13 extern "C" void __strncpy_chk(char* dest, const char* src, size_t len, size_t dst_len);
14 
15 /**
16  * @tc.name: __strncpy_chk_001
17  * @tc.desc: Verify if the function is functioning properly.
18  * @tc.type: FUNC
19  * */
20 
21 HWTEST_F(FortifyStrncpychkTest, __strncpy_chk_001, TestSize.Level1)
22 {
23     const char* srcChar = "012345678";
24     char dstChar[BUF_SIZE];
25     __strncpy_chk(dstChar, srcChar, BUF_SIZE, BUF_SIZE);
26     EXPECT_STREQ("012345678", dstChar);
27     srcChar = nullptr;
28 }
29 
30 /**
31  * @tc.name: __strncpy_chk_002
32  * @tc.desc: Verify whether the __strncpy_chk can correctly process the result and avoid buffer overflow if the
33  *           specified buffer size is an invalid value (-1) when connecting strings.
34  * @tc.type: FUNC
35  * */
36 
37 HWTEST_F(FortifyStrncpychkTest, __strncpy_chk_002, TestSize.Level1)
38 {
39     const char* srcChar = "012345678";
40     char dstChar[BUF_SIZE];
41     __strncpy_chk(dstChar, srcChar, BUF_SIZE, static_cast<size_t>(-1));
42     EXPECT_STREQ("012345678", dstChar);
43     srcChar = nullptr;
44 }