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