• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <gtest/gtest.h>
3 #define __FORTIFY_COMPILATION
4 #include <fortify/unistd.h>
5 
6 using namespace testing::ext;
7 
8 constexpr int BUF_SIZE = 256;
9 constexpr size_t COUNT = 100;
10 
11 class FortifyPreadChkTest : public testing::Test {
SetUp()12     void SetUp() override {}
TearDown()13     void TearDown() override {}
14 };
15 
16 /**
17  * @tc.name: __pread_chk_001
18  * @tc.desc: Verify that the file can be opened, data can be pre-read using the __pread_chk function, and the file can
19  *           be closed properly.
20  * @tc.type: FUNC
21  */
22 HWTEST_F(FortifyPreadChkTest, __pread_chk_001, TestSize.Level1)
23 {
24     char buf[BUF_SIZE];
25     off_t offset = 0;
26     int file = open("test.txt", O_RDONLY | O_CREAT, 0644);
27     ASSERT_NE(file, -1);
28     size_t bufSize = sizeof(buf);
29     ssize_t result = __pread_chk(file, buf, COUNT, offset, bufSize);
30     close(file);
31     EXPECT_GE(result, 0);
32     EXPECT_EQ(0, remove("test.txt"));
33 }