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 */ 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 */ 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 */ 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 char testBuffer[arrSize]; 111 memset_s(testBuffer, sizeof(testBuffer), '1', sizeof(testBuffer)); 112 113 int ret = mkfifo(FIFO_PATH, 0666); 114 EXPECT_EQ(ret, 0) << "> parent: mkfifo errno = " << errno; 115 116 pid_t pid = fork(); 117 ASSERT_TRUE(pid >= 0) << "> parent : fork : error"; 118 if (pid == 0) { 119 char readBuffer[arrSize]; 120 memset_s(readBuffer, sizeof(readBuffer), 0, sizeof(readBuffer)); 121 fd = open(FIFO_PATH, O_RDONLY, S_IRUSR|S_IWUSR); 122 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 123 LOG("> fcntl errno = %d", errno); 124 } 125 126 Msleep(150); 127 LOG("> child read"); 128 tmpInt = read(fd, readBuffer, arrSize); 129 if (tmpInt != MAX_PIPE_BUFFER) { 130 LOG("> child: tmpInt = %d", tmpInt); 131 LOG("> child: errno = %d", errno); 132 close(fd); 133 exit(1); 134 } 135 if (strncmp(testBuffer, readBuffer, MAX_PIPE_BUFFER) != 0) { 136 close(fd); 137 exit(1); 138 } 139 close(fd); 140 exit(0); 141 } 142 // parent 143 char writeBuffer[arrSize]; 144 memset_s(writeBuffer, sizeof(writeBuffer), '1', sizeof(writeBuffer)); 145 fd = open(FIFO_PATH, O_WRONLY, S_IRUSR|S_IWUSR); 146 EXPECT_NE(fd, -1) << "> open faild errno = " << errno; 147 EXPECT_NE(fcntl(fd, F_SETFL, O_NONBLOCK), -1) << "> fcntl errno = " << errno; 148 149 Msleep(50); 150 LOG("> parent write"); 151 tmpInt = write(fd, writeBuffer, arrSize); 152 EXPECT_EQ(tmpInt, MAX_PIPE_BUFFER) << "> parent: errno = " <<errno; 153 LOG("> parent: write num = %d", tmpInt); 154 close(fd); 155 156 Msleep(50); 157 WaitProcExitedOK(pid); 158 } 159 160 /** 161 * @tc.number SUB_KERNEL_IPC_FIFO_0900 162 * @tc.name test BLOCK FIFO 163 * @tc.desc [C- SOFTWARE -0200] 164 */ 165 HWTEST_F(FifoTest, testFifoBlock, Function | MediumTest | Level1) 166 { 167 const int arrSize = MAX_PIPE_BUFFER + 1000; 168 int fd = -1; 169 int tmpInt; 170 char testBuffer[arrSize]; 171 memset_s(testBuffer, sizeof(testBuffer), '1', sizeof(testBuffer)); 172 173 int ret = mkfifo(FIFO_PATH, 0666); 174 EXPECT_EQ(ret, 0) << "> parent: mkfifo errno = " << errno; 175 176 pid_t pid = fork(); 177 ASSERT_TRUE(pid >= 0) << "> parent : fork : error"; 178 if (pid == 0) { 179 char readBuffer[arrSize]; 180 memset_s(readBuffer, sizeof(readBuffer), 0, sizeof(readBuffer)); 181 fd = open(FIFO_PATH, O_RDONLY); 182 183 Msleep(60); 184 tmpInt = read(fd, readBuffer, arrSize); 185 if (tmpInt != arrSize) { 186 LOG("> child: error : read %d bytes", tmpInt); 187 LOG("> child: errno = %d", errno); 188 close(fd); 189 exit(1); 190 } 191 if (strncmp(testBuffer, readBuffer, MAX_PIPE_BUFFER) != 0) { 192 close(fd); 193 exit(1); 194 } 195 close(fd); 196 exit(0); 197 } 198 // parent 199 char writeBuffer[arrSize]; 200 memset_s(writeBuffer, sizeof(writeBuffer), '1', sizeof(writeBuffer)); 201 fd = open(FIFO_PATH, O_WRONLY, S_IRUSR|S_IWUSR); 202 EXPECT_NE(fd, -1) << "> open faild errno = " << errno; 203 204 Msleep(30); 205 tmpInt = write(fd, writeBuffer, arrSize); 206 EXPECT_EQ(tmpInt, arrSize) << "> parent: errno = " <<errno; 207 LOG("> parent: write : = %d bytes", tmpInt); 208 close(fd); 209 210 Msleep(50); 211 WaitProcExitedOK(pid); 212 } 213