• 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 constexpr size_t N_BITES = 2;
11 constexpr size_t READ_COUNT = 5;
12 constexpr size_t READ_SIZE = 6;
13 
14 class UnistdReadTest : public testing::Test {
SetUp()15     void SetUp() override {}
TearDown()16     void TearDown() override {}
17 };
18 
19 /**
20  * @tc.name: read_001
21  * @tc.desc: checks whether the read function behaves correctly when reading data from a file opened in read-only
22  *           mode and when attempting to read data from a file opened in write-only mode.
23  * @tc.type: FUNC
24  * */
25 HWTEST_F(UnistdReadTest, read_001, TestSize.Level1)
26 {
27     char buf[1];
28     int fd = open("/dev/null", O_RDONLY);
29     ASSERT_NE(-1, fd);
30     EXPECT_EQ(0, read(fd, buf, N_BITES));
31     close(fd);
32     fd = open("/dev/null", O_WRONLY);
33     ASSERT_NE(-1, fd);
34     EXPECT_EQ(-1, read(fd, buf, N_BITES));
35     close(fd);
36 }
37 
38 /**
39  * @tc.name: read_002
40  * @tc.desc: Verify whether the read content matches the actual situation.
41  * @tc.type: FUNC
42  * */
43 HWTEST_F(UnistdReadTest, read_002, TestSize.Level1)
44 {
45     int fd = open("/proc/version", O_RDONLY);
46     ASSERT_NE(-1, fd);
47     char buf[READ_SIZE];
48     EXPECT_EQ(READ_COUNT, read(fd, buf, READ_COUNT));
49     EXPECT_STREQ("Linux", buf);
50     close(fd);
51 }
52 
53 /**
54  * @tc.name: read_003
55  * @tc.desc: Validation function error returned
56  * @tc.type: FUNC
57  * */
58 HWTEST_F(UnistdReadTest, read_003, TestSize.Level1)
59 {
60     char buf[1];
61     EXPECT_EQ(-1, read(-1, buf, sizeof(buf)));
62 }