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