1 #include <fcntl.h> 2 #include <gtest/gtest.h> 3 #include <string.h> 4 #define __FORTIFY_COMPILATION 5 #include <fortify/unistd.h> 6 7 constexpr int BUF_SIZE = 2; 8 9 using namespace testing::ext; 10 11 class FortifyPwritechkTest : public testing::Test { SetUp()12 void SetUp() override {} TearDown()13 void TearDown() override {} 14 }; 15 16 /** 17 * @tc.name: __pwrite_chk_001 18 * @tc.desc: Check whether the pwrite64 function behaves correctly when writing data to a file opened in write-only 19 * mode and when attempting to write data to a file opened in read-only mode. 20 * @tc.type: FUNC 21 * */ 22 HWTEST_F(FortifyPwritechkTest, __pwrite_chk_001, TestSize.Level1) 23 { 24 char buf[1]; 25 int fd = open("/data/pwrite_chk.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); 26 EXPECT_EQ(BUF_SIZE, __pwrite_chk(fd, buf, BUF_SIZE, 0, BUF_SIZE)); 27 fd = open("/data/pwrite_chk.txt", O_RDONLY); 28 int ret = __pwrite_chk(fd, buf, BUF_SIZE, 0, BUF_SIZE); 29 EXPECT_EQ(-1, ret); 30 close(fd); 31 remove("/data/pwrite_chk.txt"); 32 } 33