• 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 size_t BUF_SIZE = 5;
9 
10 class FortifyReadChkTest : public testing::Test {
SetUp()11     void SetUp() override {}
TearDown()12     void TearDown() override {}
13 };
14 
15 /**
16  * @tc.name: __read_chk_001
17  * @tc.desc: Verify that the file can be opened, data can be read using the __read_chk function, and the correctness of
18  *           the read results can be validated.
19  * @tc.type: FUNC
20  */
21 HWTEST_F(FortifyReadChkTest, __read_chk_001, TestSize.Level1)
22 {
23     int fd = open("/proc/version", O_RDONLY);
24     ASSERT_NE(fd, -1);
25     char buf[BUF_SIZE];
26     ssize_t result = __read_chk(fd, buf, BUF_SIZE, sizeof(buf));
27     EXPECT_EQ(result, BUF_SIZE);
28     EXPECT_EQ(buf[0], 'L');
29     EXPECT_EQ(buf[1], 'i');
30     EXPECT_EQ(buf[2], 'n');
31     EXPECT_EQ(buf[3], 'u');
32     EXPECT_EQ(buf[4], 'x');
33     close(fd);
34 }