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 <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <poll.h>
26 #include <arpa/inet.h>
27 #include <gtest/gtest.h>
28 #include <netinet/in.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/socket.h>
32 #include <sys/syscall.h>
33 #include <sys/timerfd.h>
34 #include <sys/types.h>
35 #include "securec.h"
36 
37 using namespace testing::ext;
38 
39 class HatsPpollTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 private:
46 };
SetUp()47 void HatsPpollTest::SetUp()
48 {
49 }
50 
TearDown()51 void HatsPpollTest::TearDown()
52 {
53 }
54 
SetUpTestCase()55 void HatsPpollTest::SetUpTestCase()
56 {
57 }
58 
TearDownTestCase()59 void HatsPpollTest::TearDownTestCase()
60 {
61 }
62 
63 /*
64  * @tc.number : SUB_KERNEL_SYSCALL_PPOLL_0100
65  * @tc.name   : PpollSuccess_0001
66  * @tc.desc   : Ppoll poll fds successfully.
67  * @tc.size   : MediumTest
68  * @tc.type   : Function
69  * @tc.level  : Level 1
70  */
HWTEST_F(HatsPpollTest, PpollSuccess_0001, Function | MediumTest | Level1)71 HWTEST_F(HatsPpollTest, PpollSuccess_0001, Function | MediumTest | Level1)
72 {
73     int ret;
74     struct pollfd pfdArray[2];
75     int numFds = 2;
76 
77     pfdArray[0].fd = 0;
78     pfdArray[0].events = POLLIN;
79 
80     pfdArray[1].fd = 1;
81     pfdArray[1].events = POLLOUT;
82 
83     struct timespec timeout;
84     timeout.tv_sec = 0;
85     timeout.tv_nsec = 200 * 1000 * 1000;
86 
87     sigset_t sigMask;
88     ret = sigemptyset(&sigMask);
89     EXPECT_TRUE(ret >= 0);
90 
91     int retNum = ppoll(pfdArray, numFds, &timeout, &sigMask);
92     EXPECT_TRUE(retNum > 0);
93 }
94 
95 /*
96  * @tc.number : SUB_KERNEL_SYSCALL_PPOLL_0200
97  * @tc.name   : PpollFdsNoReadyFailed_0002
98  * @tc.desc   : Ppoll poll is failed for no file descriptors were ready.
99  * @tc.size   : MediumTest
100  * @tc.type   : Function
101  * @tc.level  : Level 2
102  */
HWTEST_F(HatsPpollTest, PpollFdsNoReadyFailed_0002, Function | MediumTest | Level2)103 HWTEST_F(HatsPpollTest, PpollFdsNoReadyFailed_0002, Function | MediumTest | Level2)
104 {
105     struct pollfd* fds = nullptr;
106     struct timespec timeout;
107     timeout.tv_sec = 0;
108     timeout.tv_nsec = 200 * 1000 * 1000;
109 
110     int retNum = ppoll(fds, 0, &timeout, nullptr);
111     EXPECT_EQ(retNum, 0);
112 }
113