• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <fortify/string.h>
3 
4 #define MAX_SIZE 1024
5 constexpr int CANCEL_NUMBER = 100;
6 
7 using namespace testing::ext;
8 
9 class FortifyStrrchrchkTest : public testing::Test {
SetUp()10     void SetUp() override {}
TearDown()11     void TearDown() override {}
12 };
13 
14 extern "C" char* __strrchr_chk(const char* s, int c, size_t s_len);
15 
16 /**
17  * @tc.name: __strrchr_chk_001
18  * @tc.desc: Verify if the __strrchr_chk is functioning properly.
19  * @tc.type: FUNC
20  * */
21 HWTEST_F(FortifyStrrchrchkTest, __strrchr_chk_001, TestSize.Level1)
22 {
23     char srcChar[MAX_SIZE];
24 
25     int pos, obj;
26     char* excepted;
27     for (int i = 0; i < CANCEL_NUMBER; i++) {
28         int dest = 'A';
29         memset(srcChar, ~dest, MAX_SIZE);
30         pos = random() % (MAX_SIZE - 1);
31         obj = random() % (MAX_SIZE - 1);
32         if (pos > obj) {
33             excepted = nullptr;
34         } else {
35             srcChar[pos] = dest;
36             excepted = srcChar + pos;
37         }
38         char* result = __strrchr_chk(srcChar, dest, obj);
39         EXPECT_STREQ(excepted, result);
40     }
41 }
42