1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 using namespace std;
30 
31 static const char *TEST_FILE = "/data/local/tmp/read_test.txt";
32 static const char *TEST_DATA = "Hello World!";
33 static const int TEST_LEN = strlen(TEST_DATA);
34 static const int MAX_LEN = 128;
35 
36 class ReadApiTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 private:
43 };
SetUp()44 void ReadApiTest::SetUp()
45 {
46     int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
47     write(fd, TEST_DATA, strlen(TEST_DATA));
48     close(fd);
49 }
TearDown()50 void ReadApiTest::TearDown()
51 {
52     (void)remove(TEST_FILE);
53 }
SetUpTestCase()54 void ReadApiTest::SetUpTestCase()
55 {
56 }
TearDownTestCase()57 void ReadApiTest::TearDownTestCase()
58 {
59 }
60 
61 /*
62  * @tc.number : SUB_KERNEL_SYSCALL_READ_0100
63  * @tc.name   : ReadFileSuccess_0001
64  * @tc.desc   : read file success.
65  * @tc.size   : MediumTest
66  * @tc.type   : Function
67  * @tc.level  : Level 1
68  */
HWTEST_F(ReadApiTest, ReadFileSuccess_0001, Function | MediumTest | Level1)69 HWTEST_F(ReadApiTest, ReadFileSuccess_0001, Function | MediumTest | Level1)
70 {
71     char buffer[MAX_LEN] = { 0 };
72     int fd = open(TEST_FILE, O_RDONLY);
73 
74     ssize_t size = read(fd, nullptr, 0);
75     EXPECT_EQ(size, 0);
76     size = read(fd, buffer, MAX_LEN);
77     EXPECT_EQ(size, TEST_LEN);
78 
79     close(fd);
80 }
81 
82 /*
83  * @tc.number : SUB_KERNEL_SYSCALL_READ_0200
84  * @tc.name   : ReadUseInvalidParamFailed_0002
85  * @tc.desc   : read use invalid param failed.
86  * @tc.size   : MediumTest
87  * @tc.type   : Function
88  * @tc.level  : Level 2
89  */
HWTEST_F(ReadApiTest, ReadUseInvalidParamFailed_0002, Function | MediumTest | Level2)90 HWTEST_F(ReadApiTest, ReadUseInvalidParamFailed_0002, Function | MediumTest | Level2)
91 {
92     char buffer[MAX_LEN] = { 0 };
93     errno = 0;
94     ssize_t size = read(-1, buffer, MAX_LEN);
95     EXPECT_EQ(size, -1);
96     EXPECT_EQ(errno, EBADF);
97 }
98 
99 /*
100  * @tc.number : SUB_KERNEL_SYSCALL_READ_0300
101  * @tc.name   : ReadUseErrorPermissionFdFailed_0003
102  * @tc.desc   : read use write only fd failed, errno .
103  * @tc.size   : MediumTest
104  * @tc.type   : Function
105  * @tc.level  : Level 2
106  */
HWTEST_F(ReadApiTest, ReadUseErrorPermissionFdFailed_0003, Function | MediumTest | Level2)107 HWTEST_F(ReadApiTest, ReadUseErrorPermissionFdFailed_0003, Function | MediumTest | Level2)
108 {
109     char buffer[MAX_LEN] = { 0 };
110     int fd = open(TEST_FILE, O_WRONLY);
111 
112     ssize_t size = read(fd, buffer, MAX_LEN);
113     EXPECT_EQ(size, -1);
114     EXPECT_EQ(errno, EBADF);
115 
116     close(fd);
117 }
118