• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <gtest/gtest.h>
3 #include <stdlib.h>
4 #include <sys/auxv.h>
5 #include <sys/uio.h>
6 #include <unistd.h>
7 
8 using namespace testing::ext;
9 
10 class UnistdWriteTest : public testing::Test {
SetUp()11     void SetUp() override {}
TearDown()12     void TearDown() override {}
13 };
14 
15 constexpr size_t BUF_SIZE = 2;
16 
17 /**
18  * @tc.name: write_001
19  * @tc.desc: checks whether the write function behaves correctly when writing data to a file opened in write-only
20  *           mode and when attempting to write data to a file opened in read-only mode.
21  * @tc.type: FUNC
22  * */
23 HWTEST_F(UnistdWriteTest, write_001, TestSize.Level1)
24 {
25     char buf[1];
26     int fd = open("/data/write.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
27     ASSERT_NE(-1, fd);
28     EXPECT_EQ(BUF_SIZE, write(fd, buf, BUF_SIZE));
29     close(fd);
30     fd = open("/data/write.txt", O_RDONLY);
31     ASSERT_NE(-1, fd);
32     int ret = write(fd, buf, BUF_SIZE);
33     EXPECT_EQ(-1, ret);
34     close(fd);
35     remove("/data/write.txt");
36 }