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 <stdio.h>
17#include <stdlib.h>
18#include <stdint.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/select.h>
22#include <signal.h>
23#include <time.h>
24#include <fcntl.h>
25#include "functionalext.h"
26
27extern int __select_time64(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict);
28
29const int32_t COUNT_ZERO = 0;
30const int32_t COUNT_FAILED = -1;
31
32/**
33 * @tc.name      : select_0100
34 * @tc.desc      : Verify that the monitoring descriptor is successful (valid parameters)
35 * @tc.level     : Level 0
36 */
37void select_0100(void)
38{
39    struct timeval timeout;
40    timeout.tv_sec = 5;
41    timeout.tv_usec = 0;
42    fd_set readfds, writefds;
43    FD_ZERO(&readfds);
44    FD_ZERO(&writefds);
45    FD_SET(0, &readfds);
46    FD_SET(1, &writefds);
47    int result;
48    result = select(2, &readfds, &writefds, 0, &timeout);
49    EXPECT_TRUE("select_0100", result > 0);
50}
51
52/**
53 * @tc.name      : select_0200
54 * @tc.desc      : Failed to validate monitoring descriptor
55 *                 (invalid parameter, microsecond invalid value in timeout structure)
56 * @tc.level     : Level 2
57 */
58void select_0200(void)
59{
60    struct timeval timeout;
61    timeout.tv_sec = -50;
62    fd_set readfds, writefds;
63    FD_ZERO(&readfds);
64    FD_ZERO(&writefds);
65    FD_SET(0, &readfds);
66    FD_SET(1, &writefds);
67    int result;
68    result = select(2, &readfds, &writefds, 0, &timeout);
69    EXPECT_EQ("select_0200", result, COUNT_FAILED);
70}
71
72/**
73 * @tc.name      : select_0300
74 * @tc.desc      : Validation monitoring descriptor failed
75 *                 (invalid parameter, invalid parameter added to descriptor set)
76 * @tc.level     : Level 1
77 */
78void select_0300(void)
79{
80    struct timeval timeout;
81    timeout.tv_sec = 0;
82    fd_set readfds, writefds;
83    int fd1, fd2;
84    FD_ZERO(&readfds);
85    FD_ZERO(&writefds);
86    FD_SET(2, &readfds);
87    FD_SET(2, &writefds);
88    int result;
89    result = select(2, &readfds, &writefds, 0, &timeout);
90    EXPECT_EQ("select_0300", result, COUNT_ZERO);
91}
92
93/**
94 * @tc.name      : select_time64_0100
95 * @tc.desc      : Verify that the monitoring descriptor is successful (valid parameters)
96 * @tc.level     : Level 0
97 */
98void select_time64_0100(void)
99{
100    struct timeval timeout;
101    timeout.tv_sec = 5;
102    timeout.tv_usec = 0;
103    fd_set readfds, writefds;
104    FD_ZERO(&readfds);
105    FD_ZERO(&writefds);
106    FD_SET(0, &readfds);
107    FD_SET(1, &writefds);
108    int result;
109    result = __select_time64(2, &readfds, &writefds, 0, &timeout);
110    EXPECT_TRUE("select_time64_0100", result > 0);
111}
112
113int main(void)
114{
115    select_0100();
116    select_0200();
117    select_0300();
118    select_time64_0100();
119    return t_status;
120}
121