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 <sys/select.h>
21#include <string>
22#include <vector>
23#include <fcntl.h>
24#include <unistd.h>
25#include <malloc.h>
26#include <arpa/inet.h>
27#include <gtest/gtest.h>
28#include <netinet/in.h>
29#include <sys/stat.h>
30#include <sys/socket.h>
31#include <sys/syscall.h>
32#include <sys/timerfd.h>
33#include <sys/types.h>
34#include "securec.h"
35
36using namespace testing::ext;
37
38class HatsPselectTest : public testing::Test {
39public:
40    static void SetUpTestCase();
41    static void TearDownTestCase();
42    void SetUp();
43    void TearDown();
44private:
45};
46void HatsPselectTest::SetUp()
47{
48}
49
50void HatsPselectTest::TearDown()
51{
52}
53
54void HatsPselectTest::SetUpTestCase()
55{
56}
57
58void HatsPselectTest::TearDownTestCase()
59{
60}
61
62/*
63 * @tc.number : SUB_KERNEL_SYSCALL_PSELECT_0100
64 * @tc.name   : PselectSuccess_0001
65 * @tc.desc   : Pselect monitors multiple file descriptors successfully.
66 * @tc.size   : MediumTest
67 * @tc.type   : Function
68 * @tc.level  : Level 1
69 */
70HWTEST_F(HatsPselectTest, PselectSuccess_0001, Function | MediumTest | Level1)
71{
72    int ret;
73    int numFds = 2;
74    fd_set readFds, writeFds;
75    FD_ZERO(&readFds);
76    FD_ZERO(&writeFds);
77
78    FD_SET(0, &readFds);
79    FD_SET(1, &writeFds);
80
81    timespec timeout;
82    timeout.tv_sec = 0;
83    timeout.tv_nsec = 800 * 1000 * 1000;
84
85    sigset_t sigMask;
86    ret = sigemptyset(&sigMask);
87    EXPECT_TRUE(ret >= 0);
88
89    int retNum = pselect(numFds + 1, &readFds, &writeFds, nullptr, &timeout, &sigMask);
90    EXPECT_TRUE(retNum > 0);
91}
92
93/*
94 * @tc.number : SUB_KERNEL_SYSCALL_PSELECT_0200
95 * @tc.name   : PselectTimeoutFailed_0002
96 * @tc.desc   : Pselect return 0 for no data ready within the timeout period.
97 * @tc.size   : MediumTest
98 * @tc.type   : Function
99 * @tc.level  : Level 2
100 */
101HWTEST_F(HatsPselectTest, PselectTimeoutFailed_0002, Function | MediumTest | Level2)
102{
103    fd_set readFds;
104    FD_ZERO(&readFds);
105
106    timespec timeout = {0, 0};
107
108    int invalidFdSet[10];
109    memset_s(invalidFdSet, sizeof(invalidFdSet), 0, sizeof(invalidFdSet));
110
111    int retNum = pselect(10, reinterpret_cast<fd_set*>(invalidFdSet), nullptr, nullptr, &timeout, nullptr);
112    EXPECT_EQ(retNum, 0);
113    EXPECT_EQ(errno, ENOENT);
114}
115
116/*
117 * @tc.number : SUB_KERNEL_SYSCALL_PSELECT_0300
118 * @tc.name   : PselectInvalidArgFailed_0003
119 * @tc.desc   : Pselect is failed for invalid argument.
120 * @tc.size   : MediumTest
121 * @tc.type   : Function
122 * @tc.level  : Level 2
123 */
124HWTEST_F(HatsPselectTest, PselectInvalidArgFailed_0003, Function | MediumTest | Level2)
125{
126    fd_set readFds;
127    FD_ZERO(&readFds);
128
129    timespec timeout = {0, 0};
130
131    int invalidFdSet[10];
132    memset_s(invalidFdSet, sizeof(invalidFdSet), 0, sizeof(invalidFdSet));
133
134    int retNum = pselect(-1, reinterpret_cast<fd_set*>(invalidFdSet), nullptr, nullptr, &timeout, nullptr);
135    EXPECT_EQ(retNum, -1);
136    EXPECT_EQ(errno, EINVAL);
137}