• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <gtest/gtest.h>
3 #include <sys/stat.h>
4 #define __FORTIFY_COMPILATION
5 #include <fortify/string.h>
6 
7 constexpr int MAX_SIZE = 1024;
8 constexpr int FOR_SIZE = 100;
9 
10 using namespace testing::ext;
11 
12 class FortifyMemchrchkTest : public testing::Test {
SetUp()13     void SetUp() override {}
TearDown()14     void TearDown() override {}
15 };
16 
17 /**
18  * @tc.name: __memchr_chk_001
19  * @tc.desc: Verify if the __memchr_chk is functioning properly.
20  * @tc.type: FUNC
21  * */
22 HWTEST_F(FortifyMemchrchkTest, __memchr_chk_001, TestSize.Level1)
23 {
24     char srcChar[MAX_SIZE];
25     int seekChar = 'A';
26     char* expected;
27     int pos, obj;
28     for (int i = 0; i < FOR_SIZE; i++) {
29         memset(srcChar, ~seekChar, MAX_SIZE);
30         pos = random() % MAX_SIZE;
31         obj = random() % MAX_SIZE;
32         if (pos >= obj) {
33             expected = nullptr;
34         } else {
35             srcChar[pos] = seekChar;
36             expected = srcChar + pos;
37         }
38         EXPECT_TRUE(__memchr_chk(srcChar, seekChar, obj, sizeof(srcChar)) == expected);
39     }
40 }