1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdio.h>
17570af302Sopenharmony_ci#include <stdlib.h>
18570af302Sopenharmony_ci#include <stdint.h>
19570af302Sopenharmony_ci#include <unistd.h>
20570af302Sopenharmony_ci#include <sys/time.h>
21570af302Sopenharmony_ci#include <sys/select.h>
22570af302Sopenharmony_ci#include <signal.h>
23570af302Sopenharmony_ci#include <time.h>
24570af302Sopenharmony_ci#include <fcntl.h>
25570af302Sopenharmony_ci#include "functionalext.h"
26570af302Sopenharmony_ci
27570af302Sopenharmony_ciextern int __select_time64(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict);
28570af302Sopenharmony_ci
29570af302Sopenharmony_ciconst int32_t COUNT_ZERO = 0;
30570af302Sopenharmony_ciconst int32_t COUNT_FAILED = -1;
31570af302Sopenharmony_ci
32570af302Sopenharmony_ci/**
33570af302Sopenharmony_ci * @tc.name      : select_0100
34570af302Sopenharmony_ci * @tc.desc      : Verify that the monitoring descriptor is successful (valid parameters)
35570af302Sopenharmony_ci * @tc.level     : Level 0
36570af302Sopenharmony_ci */
37570af302Sopenharmony_civoid select_0100(void)
38570af302Sopenharmony_ci{
39570af302Sopenharmony_ci    struct timeval timeout;
40570af302Sopenharmony_ci    timeout.tv_sec = 5;
41570af302Sopenharmony_ci    timeout.tv_usec = 0;
42570af302Sopenharmony_ci    fd_set readfds, writefds;
43570af302Sopenharmony_ci    FD_ZERO(&readfds);
44570af302Sopenharmony_ci    FD_ZERO(&writefds);
45570af302Sopenharmony_ci    FD_SET(0, &readfds);
46570af302Sopenharmony_ci    FD_SET(1, &writefds);
47570af302Sopenharmony_ci    int result;
48570af302Sopenharmony_ci    result = select(2, &readfds, &writefds, 0, &timeout);
49570af302Sopenharmony_ci    EXPECT_TRUE("select_0100", result > 0);
50570af302Sopenharmony_ci}
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci/**
53570af302Sopenharmony_ci * @tc.name      : select_0200
54570af302Sopenharmony_ci * @tc.desc      : Failed to validate monitoring descriptor
55570af302Sopenharmony_ci *                 (invalid parameter, microsecond invalid value in timeout structure)
56570af302Sopenharmony_ci * @tc.level     : Level 2
57570af302Sopenharmony_ci */
58570af302Sopenharmony_civoid select_0200(void)
59570af302Sopenharmony_ci{
60570af302Sopenharmony_ci    struct timeval timeout;
61570af302Sopenharmony_ci    timeout.tv_sec = -50;
62570af302Sopenharmony_ci    fd_set readfds, writefds;
63570af302Sopenharmony_ci    FD_ZERO(&readfds);
64570af302Sopenharmony_ci    FD_ZERO(&writefds);
65570af302Sopenharmony_ci    FD_SET(0, &readfds);
66570af302Sopenharmony_ci    FD_SET(1, &writefds);
67570af302Sopenharmony_ci    int result;
68570af302Sopenharmony_ci    result = select(2, &readfds, &writefds, 0, &timeout);
69570af302Sopenharmony_ci    EXPECT_EQ("select_0200", result, COUNT_FAILED);
70570af302Sopenharmony_ci}
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci/**
73570af302Sopenharmony_ci * @tc.name      : select_0300
74570af302Sopenharmony_ci * @tc.desc      : Validation monitoring descriptor failed
75570af302Sopenharmony_ci *                 (invalid parameter, invalid parameter added to descriptor set)
76570af302Sopenharmony_ci * @tc.level     : Level 1
77570af302Sopenharmony_ci */
78570af302Sopenharmony_civoid select_0300(void)
79570af302Sopenharmony_ci{
80570af302Sopenharmony_ci    struct timeval timeout;
81570af302Sopenharmony_ci    timeout.tv_sec = 0;
82570af302Sopenharmony_ci    fd_set readfds, writefds;
83570af302Sopenharmony_ci    int fd1, fd2;
84570af302Sopenharmony_ci    FD_ZERO(&readfds);
85570af302Sopenharmony_ci    FD_ZERO(&writefds);
86570af302Sopenharmony_ci    FD_SET(2, &readfds);
87570af302Sopenharmony_ci    FD_SET(2, &writefds);
88570af302Sopenharmony_ci    int result;
89570af302Sopenharmony_ci    result = select(2, &readfds, &writefds, 0, &timeout);
90570af302Sopenharmony_ci    EXPECT_EQ("select_0300", result, COUNT_ZERO);
91570af302Sopenharmony_ci}
92570af302Sopenharmony_ci
93570af302Sopenharmony_ci/**
94570af302Sopenharmony_ci * @tc.name      : select_time64_0100
95570af302Sopenharmony_ci * @tc.desc      : Verify that the monitoring descriptor is successful (valid parameters)
96570af302Sopenharmony_ci * @tc.level     : Level 0
97570af302Sopenharmony_ci */
98570af302Sopenharmony_civoid select_time64_0100(void)
99570af302Sopenharmony_ci{
100570af302Sopenharmony_ci    struct timeval timeout;
101570af302Sopenharmony_ci    timeout.tv_sec = 5;
102570af302Sopenharmony_ci    timeout.tv_usec = 0;
103570af302Sopenharmony_ci    fd_set readfds, writefds;
104570af302Sopenharmony_ci    FD_ZERO(&readfds);
105570af302Sopenharmony_ci    FD_ZERO(&writefds);
106570af302Sopenharmony_ci    FD_SET(0, &readfds);
107570af302Sopenharmony_ci    FD_SET(1, &writefds);
108570af302Sopenharmony_ci    int result;
109570af302Sopenharmony_ci    result = __select_time64(2, &readfds, &writefds, 0, &timeout);
110570af302Sopenharmony_ci    EXPECT_TRUE("select_time64_0100", result > 0);
111570af302Sopenharmony_ci}
112570af302Sopenharmony_ci
113570af302Sopenharmony_ciint main(void)
114570af302Sopenharmony_ci{
115570af302Sopenharmony_ci    select_0100();
116570af302Sopenharmony_ci    select_0200();
117570af302Sopenharmony_ci    select_0300();
118570af302Sopenharmony_ci    select_time64_0100();
119570af302Sopenharmony_ci    return t_status;
120570af302Sopenharmony_ci}
121