• 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 class FortifyWriteChkTest : public testing::Test {
SetUp()9     void SetUp() override {}
TearDown()10     void TearDown() override {}
11 };
12 
13 /**
14  * @tc.name: __write_chk_001
15  * @tc.desc: Verify the basic functionality of writing data to a file using the __write_chk function, including creating
16  *           a temporary file, writing data, and validating the correctness of the write results.
17  * @tc.type: FUNC
18  */
19 HWTEST_F(FortifyWriteChkTest, __write_chk_001, TestSize.Level1)
20 {
21     const char* data = "Hello, this is a test.";
22     size_t count = strlen(data);
23     int fd = open("test.txt", O_RDWR | O_CREAT, 0644);
24     ASSERT_NE(fd, -1);
25     ssize_t result = __write_chk(fd, data, count, count);
26     EXPECT_EQ(result, count);
27     close(fd);
28     EXPECT_EQ(0, remove("test.txt"));
29 }