• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 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/select.h>
17 #include <signal.h>
18 #include <time.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "functionalext.h"
22 
23 extern int __pselect_time64 (int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict,
24                     const struct timespec *__restrict, const sigset_t *__restrict);
25 
26 
27 /**
28  * @tc.name      : pselect_fdset_0100
29  * @tc.desc      : Determine whether the fdset macro is normal
30  * @tc.level     : Level 0
31  */
pselect_fdset_0100(void)32 void pselect_fdset_0100(void)
33 {
34     fd_set set;
35     FD_ZERO(&set);
36     for (size_t i = 0; i < 1024; ++i) {
37         EXPECT_FALSE("pselect_fdset_0100", FD_ISSET(i, &set));
38     }
39     FD_SET(STDIN_FILENO, &set);
40     EXPECT_TRUE("pselect_fdset_0100", FD_ISSET(STDIN_FILENO, &set));
41     EXPECT_FALSE("pselect_fdset_0100", FD_ISSET(STDOUT_FILENO, &set));
42     FD_SET(STDOUT_FILENO, &set);
43     EXPECT_TRUE("pselect_fdset_0100", FD_ISSET(STDIN_FILENO, &set));
44     EXPECT_TRUE("pselect_fdset_0100", FD_ISSET(STDOUT_FILENO, &set));
45     FD_CLR(STDIN_FILENO, &set);
46     EXPECT_FALSE("pselect_fdset_0100", FD_ISSET(STDIN_FILENO, &set));
47     EXPECT_TRUE("pselect_fdset_0100", FD_ISSET(STDOUT_FILENO, &set));
48     FD_CLR(STDOUT_FILENO, &set);
49     EXPECT_FALSE("pselect_fdset_0100", FD_ISSET(STDIN_FILENO, &set));
50     EXPECT_FALSE("pselect_fdset_0100", FD_ISSET(STDOUT_FILENO, &set));
51 }
52 
53 /**
54  * @tc.name      : pselect_normal_0100
55  * @tc.desc      : Monitor stdout file for writability (normal)
56  * @tc.level     : Level 0
57  */
pselect_normal_0100(void)58 void pselect_normal_0100(void)
59 {
60     fd_set set;
61     struct timespec timeout;
62     timeout.tv_sec = 5;
63     timeout.tv_nsec = 0;
64     FD_ZERO(&set);
65     FD_SET(STDOUT_FILENO, &set);
66 
67     int ret = pselect(STDOUT_FILENO + 1, NULL, &set, NULL, &timeout, NULL);
68     EXPECT_EQ("pselect_normal_0100", ret, 1);
69     EXPECT_TRUE("pselect_normal_0100", FD_ISSET(STDOUT_FILENO, &set));
70 }
71 
72 /**
73  * @tc.name      : pselect_error_0100
74  * @tc.desc      : Incoming illegal file descriptor size to monitor stdout file writability
75  * @tc.level     : Level 2
76  */
pselect_error_0100(void)77 void pselect_error_0100(void)
78 {
79     fd_set set;
80     struct timespec timeout;
81     timeout.tv_sec = 5;
82     timeout.tv_nsec = 0;
83     FD_ZERO(&set);
84     FD_SET(STDOUT_FILENO, &set);
85 
86     int ret = pselect(-1, NULL, &set, NULL, &timeout, NULL);
87     EXPECT_EQ("pselect_error_0100", ret, ERREXPECT);
88 }
89 
90 /**
91  * @tc.name      : pselect_timeout_0100
92  * @tc.desc      : Monitor stdout file for readability (timeout)
93  * @tc.level     : Level 2
94  */
pselect_timeout_0100(void)95 void pselect_timeout_0100(void)
96 {
97     fd_set set;
98     sigset_t sigmask;
99     struct timespec timeout;
100     timeout.tv_sec = 2;
101     timeout.tv_nsec = 0;
102     FD_ZERO(&set);
103     FD_SET(STDOUT_FILENO, &set);
104     sigemptyset(&sigmask);
105     sigaddset(&sigmask, SIGINT);
106     int ret = pselect(STDOUT_FILENO + 1, &set, NULL, NULL, &timeout, &sigmask);
107     if (ret == 0) {
108         EXPECT_FALSE("pselect_timeout_0100", FD_ISSET(STDOUT_FILENO, &set));
109     }
110 }
111 
112 /**
113  * @tc.name      : pselect_time64_normal_0100
114  * @tc.desc      : Monitor stdout file for writability (normal)
115  * @tc.level     : Level 0
116  */
pselect_time64_normal_0100(void)117 void pselect_time64_normal_0100(void)
118 {
119     fd_set set;
120     struct timespec timeout;
121     timeout.tv_sec = 5;
122     timeout.tv_nsec = 0;
123     FD_ZERO(&set);
124     FD_SET(STDOUT_FILENO, &set);
125 
126     int ret = __pselect_time64(STDOUT_FILENO + 1, NULL, &set, NULL, &timeout, NULL);
127     EXPECT_EQ("pselect_time64_normal_0100", ret, 1);
128     EXPECT_TRUE("pselect_time64_normal_0100", FD_ISSET(STDOUT_FILENO, &set));
129 }
130 
main(void)131 int main(void)
132 {
133     pselect_fdset_0100();
134     pselect_normal_0100();
135     pselect_error_0100();
136     pselect_timeout_0100();
137     pselect_time64_normal_0100();
138 
139     return t_status;
140 }
141