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 <arpa/inet.h>
24#include <gtest/gtest.h>
25#include <netinet/in.h>
26#include <sys/stat.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29
30using namespace testing::ext;
31
32class HatsWriteTest : public testing::Test {
33public:
34static void SetUpTestCase();
35static void TearDownTestCase();
36void SetUp();
37void TearDown();
38private:
39};
40void HatsWriteTest::SetUp()
41{
42}
43void HatsWriteTest::TearDown()
44{
45}
46void HatsWriteTest::SetUpTestCase()
47{
48}
49void HatsWriteTest::TearDownTestCase()
50{
51}
52
53static const char *WRITE_TEST_FILE = "/data/local/tmp/tryWrite.txt";
54static const char *TEST_DATA = "Hello write!";
55static const int TEST_DATA_LEN = strlen(TEST_DATA);
56
57/*
58 * @tc.number : SUB_KERNEL_SYSCALL_WRITE_0100
59 * @tc.name   : WriteDataToFileSuccess_0001
60 * @tc.desc   : write data to file success.
61 * @tc.size   : MediumTest
62 * @tc.type   : Function
63 * @tc.level  : Level 1
64 */
65HWTEST_F(HatsWriteTest, WriteDataToFileSuccess_0001, Function | MediumTest | Level1)
66{
67    int ret;
68    int fd;
69    char *buf = nullptr;
70    buf = new char[TEST_DATA_LEN + 1];
71
72    fd = open(WRITE_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
73    EXPECT_TRUE(fd >= 3);
74
75    ret = write(fd, TEST_DATA, TEST_DATA_LEN);
76    EXPECT_TRUE(ret == TEST_DATA_LEN);
77    close(fd);
78
79    fd = open(WRITE_TEST_FILE, O_RDONLY);
80    EXPECT_TRUE(fd >= 3);
81    ret = read(fd, buf, TEST_DATA_LEN);
82    close(fd);
83    buf[TEST_DATA_LEN] = '\0';
84    ret = strcmp(buf, TEST_DATA);
85    EXPECT_TRUE(ret == 0);
86
87    remove(WRITE_TEST_FILE);
88}
89
90/*
91 * @tc.number : SUB_KERNEL_SYSCALL_WRITE_0200
92 * @tc.name   : WriteInvalidFdFail_0002
93 * @tc.desc   : write to invalid fd fail.
94 * @tc.size   : MediumTest
95 * @tc.type   : Function
96 * @tc.level  : Level 2
97 */
98HWTEST_F(HatsWriteTest, WriteInvalidFdFail_0002, Function | MediumTest | Level2)
99{
100    int ret;
101    int fd;
102    int pipefd[2];
103
104    fd = -1;
105    errno = 0;
106    ret = write(fd, TEST_DATA, TEST_DATA_LEN);
107    EXPECT_TRUE(ret == -1);
108    EXPECT_EQ(errno, EBADF);
109
110    ret = pipe(pipefd);
111    EXPECT_TRUE(ret == 0);
112    errno = 0;
113    ret = write(pipefd[0], TEST_DATA, TEST_DATA_LEN);
114    EXPECT_TRUE(ret == -1);
115    EXPECT_EQ(errno, EBADF);
116    close(pipefd[0]);
117    close(pipefd[1]);
118}
119
120/*
121 * @tc.number : SUB_KERNEL_SYSCALL_WRITE_0300
122 * @tc.name   : WriteFdIsNotOpenForWriteFail_0003
123 * @tc.desc   : write fd is not open for write fail.
124 * @tc.size   : MediumTest
125 * @tc.type   : Function
126 * @tc.level  : Level 2
127 */
128HWTEST_F(HatsWriteTest, WriteFdIsNotOpenForWriteFail_0003, Function | MediumTest | Level2)
129{
130    int ret;
131    int fd;
132
133    fd = open(WRITE_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
134    EXPECT_TRUE(fd >= 3);
135
136    errno = 0;
137    ret = write(fd, TEST_DATA, TEST_DATA_LEN);
138    EXPECT_TRUE(ret == -1);
139    EXPECT_EQ(errno, EBADF);
140    close(fd);
141
142    fd = open(WRITE_TEST_FILE, O_APPEND | O_CREAT | O_TRUNC, 0644);
143    EXPECT_TRUE(fd >= 3);
144
145    errno = 0;
146    ret = write(fd, TEST_DATA, TEST_DATA_LEN);
147    EXPECT_TRUE(ret == -1);
148    EXPECT_EQ(errno, EBADF);
149    close(fd);
150    remove(WRITE_TEST_FILE);
151}
152
153/*
154 * @tc.number : SUB_KERNEL_SYSCALL_WRITE_0400
155 * @tc.name   : WriteEmptyDataToFileSuccess_0004
156 * @tc.desc   : write empty data to file success.
157 * @tc.size   : MediumTest
158 * @tc.type   : Function
159 * @tc.level  : Level 1
160 */
161HWTEST_F(HatsWriteTest, WriteEmptyDataToFileSuccess_0004, Function | MediumTest | Level1)
162{
163    int ret;
164    int fd;
165
166    fd = open(WRITE_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
167    EXPECT_TRUE(fd >= 3);
168
169    ret = write(fd, "", 0);
170    EXPECT_TRUE(ret == 0);
171    close(fd);
172    remove(WRITE_TEST_FILE);
173}
174
175/*
176 * @tc.number : SUB_KERNEL_SYSCALL_WRITE_0500
177 * @tc.name   : WriteDataToPipeSuccess_0005
178 * @tc.desc   : Write data to pipe success.
179 * @tc.size   : MediumTest
180 * @tc.type   : Function
181 * @tc.level  : Level 1
182 */
183HWTEST_F(HatsWriteTest, WriteDataToPipeSuccess_0005, Function | MediumTest | Level1)
184{
185    int pipefd[2];
186    int ret;
187    char *buf = nullptr;
188    buf = new char[TEST_DATA_LEN + 1];
189
190    ret = pipe(pipefd);
191    EXPECT_TRUE(ret == 0);
192    ret = write(pipefd[1], TEST_DATA, TEST_DATA_LEN);
193    EXPECT_TRUE(ret == TEST_DATA_LEN);
194
195    ret = read(pipefd[0], buf, TEST_DATA_LEN);
196    EXPECT_TRUE(ret == TEST_DATA_LEN);
197    buf[TEST_DATA_LEN] = '\0';
198    ret = strcmp(buf, TEST_DATA);
199    EXPECT_TRUE(ret == 0);
200
201    close(pipefd[0]);
202    close(pipefd[1]);
203}
204