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
28using namespace testing::ext;
29using namespace std;
30
31static const char *TEST_FILE = "/data/local/tmp/read_test.txt";
32static const char *TEST_DATA = "Hello World!";
33static const int TEST_LEN = strlen(TEST_DATA);
34static const int MAX_LEN = 128;
35
36class ReadApiTest : public testing::Test {
37public:
38    static void SetUpTestCase();
39    static void TearDownTestCase();
40    void SetUp();
41    void TearDown();
42private:
43};
44void 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}
50void ReadApiTest::TearDown()
51{
52    (void)remove(TEST_FILE);
53}
54void ReadApiTest::SetUpTestCase()
55{
56}
57void 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 */
69HWTEST_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 */
90HWTEST_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 */
107HWTEST_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