1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/resource.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <gtest/gtest.h>
22 #include "log.h"
23 #include "utils.h"
24 #include "KernelConstants.h"
25 #include <securec.h>
26
27 using namespace testing::ext;
28
29 #define FIFO_PATH "/dev/xtsTestFifo"
30
31 class FifoTest : public::testing::Test {
32 protected:
SetUp()33 void SetUp()
34 {
35 remove(FIFO_PATH);
36 }
TearDown()37 void TearDown()
38 {
39 remove(FIFO_PATH);
40 }
41 };
42
43 /**
44 * @tc.number SUB_KERNEL_IPC_FIFO_0100
45 * @tc.name basic function test : hello world
46 * @tc.desc [C- SOFTWARE -0200]
47 */
HWTEST_F(FifoTest, testHelloWorld, Function | MediumTest | Level0)48 HWTEST_F(FifoTest, testHelloWorld, Function | MediumTest | Level0)
49 {
50 char buffer[80];
51 int fd;
52 char sentence[] = "Hello World";
53
54 pid_t pid = fork();
55 ASSERT_TRUE(pid >= 0) << "> parent: fork : error";
56 if (pid == 0) {
57 Msleep(20);
58 fd = open(FIFO_PATH, O_WRONLY, S_IRUSR|S_IWUSR);
59 write(fd, sentence, sizeof(sentence));
60 LOG("> child: write: %s", sentence);
61 close(fd);
62 exit(0);
63 }
64 // parent
65 int ret = mkfifo(FIFO_PATH, 0666);
66 EXPECT_EQ(ret, 0) << "> parent: mkfifo errno = " << errno;
67 fd = open(FIFO_PATH, O_RDONLY, S_IRUSR|S_IWUSR);
68 EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
69 EXPECT_NE(read(fd, buffer, sizeof(buffer)), -1) << "> read errno = " << errno;
70 EXPECT_STREQ(sentence, buffer) << "> parent: read = " << buffer;
71 LOG("> parent: read: %s", buffer);
72 close(fd);
73
74 Msleep(20);
75 WaitProcExitedOK(pid);
76 }
77
78 /**
79 * @tc.number SUB_KERNEL_IPC_FIFO_0700
80 * @tc.name mkfifo Duplicate creation and delete
81 * @tc.desc [C- SOFTWARE -0200]
82 */
HWTEST_F(FifoTest, testFifoAddDelete, Function | MediumTest | Level0)83 HWTEST_F(FifoTest, testFifoAddDelete, Function | MediumTest | Level0)
84 {
85 char fifoPath[50];
86 const int loopNum = 32;
87
88 for (int i = 0; i < loopNum; i++) {
89 sprintf(fifoPath, "/dev/xtsTestFifo_%d", i);
90 remove(fifoPath);
91
92 LOG("> Create fifo %d", i);
93 EXPECT_EQ(mkfifo(fifoPath, 0666), 0) << "> mkfifo errno = " << errno;
94
95 LOG("> Delete fifo %d", i);
96 EXPECT_NE(remove(fifoPath), -1) << "> remove errno = " << errno;
97 }
98 }
99
100 /**
101 * @tc.number SUB_KERNEL_IPC_FIFO_0800
102 * @tc.name test O_NONBLOCK FIFO
103 * @tc.desc [C- SOFTWARE -0200]
104 */
HWTEST_F(FifoTest, testFifoNonblack, Function | MediumTest | Level1)105 HWTEST_F(FifoTest, testFifoNonblack, Function | MediumTest | Level1)
106 {
107 const int arrSize = MAX_PIPE_BUFFER + 10;
108 int fd = -1;
109 int tmpInt;
110 int memRet = -1;
111 char testBuffer[arrSize];
112 memRet = memset_s(testBuffer, sizeof(testBuffer), '1', sizeof(testBuffer));
113 EXPECT_EQ(0, memRet);
114
115 int ret = mkfifo(FIFO_PATH, 0666);
116 EXPECT_EQ(ret, 0) << "> parent: mkfifo errno = " << errno;
117
118 pid_t pid = fork();
119 ASSERT_TRUE(pid >= 0) << "> parent : fork : error";
120 if (pid == 0) {
121 char readBuffer[arrSize];
122 memRet = memset_s(readBuffer, sizeof(readBuffer), 0, sizeof(readBuffer));
123 EXPECT_EQ(0, memRet);
124 fd = open(FIFO_PATH, O_RDONLY, S_IRUSR|S_IWUSR);
125 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
126 LOG("> fcntl errno = %d", errno);
127 }
128
129 Msleep(150);
130 LOG("> child read");
131 tmpInt = read(fd, readBuffer, arrSize);
132 if (tmpInt != MAX_PIPE_BUFFER) {
133 LOG("> child: tmpInt = %d", tmpInt);
134 LOG("> child: errno = %d", errno);
135 close(fd);
136 exit(1);
137 }
138 if (strncmp(testBuffer, readBuffer, MAX_PIPE_BUFFER) != 0) {
139 close(fd);
140 exit(1);
141 }
142 close(fd);
143 exit(0);
144 }
145 // parent
146 char writeBuffer[arrSize];
147 memRet = memset_s(writeBuffer, sizeof(writeBuffer), '1', sizeof(writeBuffer));
148 EXPECT_EQ(0, memRet);
149 fd = open(FIFO_PATH, O_WRONLY, S_IRUSR|S_IWUSR);
150 EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
151 EXPECT_NE(fcntl(fd, F_SETFL, O_NONBLOCK), -1) << "> fcntl errno = " << errno;
152
153 Msleep(50);
154 LOG("> parent write");
155 tmpInt = write(fd, writeBuffer, arrSize);
156 EXPECT_EQ(tmpInt, MAX_PIPE_BUFFER) << "> parent: errno = " <<errno;
157 LOG("> parent: write num = %d", tmpInt);
158 close(fd);
159
160 Msleep(50);
161 WaitProcExitedOK(pid);
162 }
163
164 /**
165 * @tc.number SUB_KERNEL_IPC_FIFO_0900
166 * @tc.name test BLOCK FIFO
167 * @tc.desc [C- SOFTWARE -0200]
168 */
HWTEST_F(FifoTest, testFifoBlock, Function | MediumTest | Level1)169 HWTEST_F(FifoTest, testFifoBlock, Function | MediumTest | Level1)
170 {
171 const int arrSize = MAX_PIPE_BUFFER + 1000;
172 int fd = -1;
173 int tmpInt;
174 char testBuffer[arrSize];
175 int memRet = -1;
176 memRet = memset_s(testBuffer, sizeof(testBuffer), '1', sizeof(testBuffer));
177 EXPECT_EQ(0, memRet);
178
179 int ret = mkfifo(FIFO_PATH, 0666);
180 EXPECT_EQ(ret, 0) << "> parent: mkfifo errno = " << errno;
181
182 pid_t pid = fork();
183 ASSERT_TRUE(pid >= 0) << "> parent : fork : error";
184 if (pid == 0) {
185 char readBuffer[arrSize];
186 memRet = memset_s(readBuffer, sizeof(readBuffer), 0, sizeof(readBuffer));
187 EXPECT_EQ(0, memRet);
188 fd = open(FIFO_PATH, O_RDONLY);
189
190 Msleep(60);
191 tmpInt = read(fd, readBuffer, arrSize);
192 if (tmpInt != arrSize) {
193 LOG("> child: error : read %d bytes", tmpInt);
194 LOG("> child: errno = %d", errno);
195 close(fd);
196 exit(1);
197 }
198 if (strncmp(testBuffer, readBuffer, MAX_PIPE_BUFFER) != 0) {
199 close(fd);
200 exit(1);
201 }
202 close(fd);
203 exit(0);
204 }
205 // parent
206 char writeBuffer[arrSize];
207 memRet = memset_s(writeBuffer, sizeof(writeBuffer), '1', sizeof(writeBuffer));
208 EXPECT_EQ(0, memRet);
209 fd = open(FIFO_PATH, O_WRONLY, S_IRUSR|S_IWUSR);
210 EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
211
212 Msleep(30);
213 tmpInt = write(fd, writeBuffer, arrSize);
214 EXPECT_EQ(tmpInt, arrSize) << "> parent: errno = " <<errno;
215 LOG("> parent: write : = %d bytes", tmpInt);
216 close(fd);
217
218 Msleep(50);
219 WaitProcExitedOK(pid);
220 }
221