• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <string.h>
3 
4 constexpr size_t TEST_SIZE = 10;
5 constexpr int COMPER_NUMBER = 7;
6 constexpr int COMPER_NUMBER_TWO = 9;
7 
8 using namespace testing::ext;
9 
10 class FortifyStrcatchkTest : public testing::Test {
SetUp()11     void SetUp() override {}
TearDown()12     void TearDown() override {}
13 };
14 
15 extern "C" char* __strcat_chk(char* dest, const char* src, size_t dst_buf_size);
16 
17 /**
18  * @tc.name: __strcat_chk_001
19  * @tc.desc: Verify whether the __strcat_chk can correctly handle the results and avoid buffer overflow when connecting
20  *           strings.
21  * @tc.type: FUNC
22  * */
23 HWTEST_F(FortifyStrcatchkTest, __strcat_chk_001, TestSize.Level1)
24 {
25     char src[TEST_SIZE] = { 0 };
26     src[0] = 'a';
27     src[1] = '\0';
28     char* result = __strcat_chk(src, "aaaaa", sizeof(src));
29     EXPECT_STREQ("aaaaaa", result);
30     EXPECT_EQ('\0', src[COMPER_NUMBER]);
31 }
32 
33 /**
34  * @tc.name: __strcat_chk_002
35  * @tc.desc: Verify whether the __strcat_chk can correctly handle the results and avoid buffer overflow when connecting
36  *           long strings.
37  * @tc.type: FUNC
38  * */
39 HWTEST_F(FortifyStrcatchkTest, __strcat_chk_002, TestSize.Level1)
40 {
41     char src[TEST_SIZE] = { 0 };
42     src[0] = 'a';
43     src[1] = '\0';
44     EXPECT_STREQ("aaaaaaaaa", __strcat_chk(src, "aaaaaaaa", sizeof(src)));
45     EXPECT_EQ('\0', src[COMPER_NUMBER_TWO]);
46 }
47 
48 /**
49  * @tc.name: __strcat_chk_003
50  * @tc.desc: Verify whether the __strcat_chk can correctly process the result and avoid buffer overflow if the specified
51  *           buffer size is an invalid value (-1) when connecting strings.
52  * @tc.type: FUNC
53  * */
54 HWTEST_F(FortifyStrcatchkTest, __strcat_chk_003, TestSize.Level1)
55 {
56     char src[TEST_SIZE] = { 0 };
57     src[0] = 'a';
58     src[1] = '\0';
59     EXPECT_STREQ("aaaaaaaaa", __strcat_chk(src, "aaaaaaaa", static_cast<size_t>(-1)));
60     EXPECT_EQ('\0', src[COMPER_NUMBER_TWO]);
61 }