blob: 0eab8768b117f9e16cc6a8ad37c0a0c21bd2fda5 [file] [log] [blame]
Elliott Hughes5b9310e2013-10-02 16:59:05 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080020#include <signal.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070021#include <stdlib.h>
22#include <sys/select.h>
Brian Carlstrom1021c052014-04-01 17:33:41 -070023#include <sys/types.h>
24#include <sys/wait.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070025
Elliott Hughes33697a02016-01-26 13:04:57 -080026#include "utils.h"
27
Elliott Hughes5b9310e2013-10-02 16:59:05 -070028TEST(sys_select, fd_set_smoke) {
29 fd_set fds;
30 FD_ZERO(&fds);
31
32 for (size_t i = 0; i < 1024; ++i) {
33 EXPECT_FALSE(FD_ISSET(i, &fds));
34 }
35
36 FD_SET(0, &fds);
37 EXPECT_TRUE(FD_ISSET(0, &fds));
38 EXPECT_FALSE(FD_ISSET(1, &fds));
39 FD_SET(1, &fds);
40 EXPECT_TRUE(FD_ISSET(0, &fds));
41 EXPECT_TRUE(FD_ISSET(1, &fds));
42 FD_CLR(0, &fds);
43 EXPECT_FALSE(FD_ISSET(0, &fds));
44 EXPECT_TRUE(FD_ISSET(1, &fds));
45 FD_CLR(1, &fds);
46 EXPECT_FALSE(FD_ISSET(0, &fds));
47 EXPECT_FALSE(FD_ISSET(1, &fds));
48}
Elliott Hughes11952072013-10-24 15:15:14 -070049
Christopher Ferrise3bb0252014-03-31 22:51:27 -070050#define DELAY_MSG "1234"
51
52static void DelayedWrite(int* pid, int* fd) {
53 int fds[2];
54 ASSERT_EQ(0, pipe(fds));
55
56 if ((*pid = fork()) == 0) {
57 close(fds[0]);
58 usleep(5000);
59 EXPECT_EQ(5, write(fds[1], DELAY_MSG, sizeof(DELAY_MSG)));
60 close(fds[1]);
61 exit(0);
62 }
63 ASSERT_LT(0, *pid);
64 close(fds[1]);
65
66 *fd = fds[0];
67}
68
69static void DelayedWriteCleanup(int pid, int fd) {
70 char buf[sizeof(DELAY_MSG)];
71 ASSERT_EQ(static_cast<ssize_t>(sizeof(DELAY_MSG)), read(fd, buf, sizeof(DELAY_MSG)));
72 ASSERT_STREQ(DELAY_MSG, buf);
Elliott Hughes33697a02016-01-26 13:04:57 -080073
74 AssertChildExited(pid, 0);
Christopher Ferrise3bb0252014-03-31 22:51:27 -070075}
76
Elliott Hughes11952072013-10-24 15:15:14 -070077TEST(sys_select, select_smoke) {
78 fd_set r;
79 FD_ZERO(&r);
80 fd_set w;
81 FD_ZERO(&w);
82 fd_set e;
83 FD_ZERO(&e);
84
85 FD_SET(STDIN_FILENO, &r);
86 FD_SET(STDOUT_FILENO, &w);
87 FD_SET(STDERR_FILENO, &w);
88
89 int max = STDERR_FILENO + 1;
90
91 // Invalid max fd.
92 ASSERT_EQ(-1, select(-1, &r, &w, &e, NULL));
93 ASSERT_EQ(EINVAL, errno);
94
Christopher Ferrisdd926b52016-01-13 22:38:09 -080095 int num_fds = select(max, &r, &w, &e, NULL);
Christopher Ferrise8efb962016-01-14 11:12:38 -080096 // If there is data to be read on STDIN, then the number of
97 // fds ready will be 3 instead of 2. Allow this case, but verify
98 // every fd that is set.
Christopher Ferrisdd926b52016-01-13 22:38:09 -080099 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
100 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
101 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
102 if (num_fds == 3) {
103 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
104 }
Elliott Hughes11952072013-10-24 15:15:14 -0700105
106 // Invalid timeout.
107 timeval tv;
108 tv.tv_sec = -1;
109 tv.tv_usec = 0;
110 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
111 ASSERT_EQ(EINVAL, errno);
112
113 // Valid timeout...
114 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700115 int pid, fd;
116 DelayedWrite(&pid, &fd);
117
118 FD_ZERO(&r);
119 FD_SET(fd, &r);
120 ASSERT_EQ(1, select(fd+1, &r, NULL, NULL, &tv));
121 // Both tv_sec and tv_nsec should have been updated.
122 ASSERT_EQ(0, tv.tv_sec);
123 ASSERT_NE(0, tv.tv_usec);
124
125 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700126}
127
128TEST(sys_select, pselect_smoke) {
129 sigset_t ss;
130 sigemptyset(&ss);
131 sigaddset(&ss, SIGPIPE);
132
133 fd_set r;
134 FD_ZERO(&r);
135 fd_set w;
136 FD_ZERO(&w);
137 fd_set e;
138 FD_ZERO(&e);
139
140 FD_SET(STDIN_FILENO, &r);
141 FD_SET(STDOUT_FILENO, &w);
142 FD_SET(STDERR_FILENO, &w);
143
144 int max = STDERR_FILENO + 1;
145
146 // Invalid max fd.
147 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
148 ASSERT_EQ(EINVAL, errno);
149
Christopher Ferrise8efb962016-01-14 11:12:38 -0800150 // If there is data to be read on STDIN, then the number of
151 // fds ready will be 3 instead of 2. Allow this case, but verify
152 // every fd that is set.
Christopher Ferrisdd926b52016-01-13 22:38:09 -0800153 int num_fds = pselect(max, &r, &w, &e, NULL, &ss);
154 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
155 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
156 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
157 if (num_fds == 3) {
158 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
159 }
Elliott Hughes11952072013-10-24 15:15:14 -0700160
161 // Invalid timeout.
162 timespec tv;
163 tv.tv_sec = -1;
164 tv.tv_nsec = 0;
165 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
166 ASSERT_EQ(EINVAL, errno);
167
168 // Valid timeout...
169 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700170 int pid, fd;
171 DelayedWrite(&pid, &fd);
172
173 FD_ZERO(&r);
174 FD_SET(fd, &r);
175 ASSERT_EQ(1, pselect(fd+1, &r, NULL, NULL, &tv, NULL));
176 // Neither tv_sec nor tv_nsec should have been updated.
177 ASSERT_EQ(1, tv.tv_sec);
178 ASSERT_EQ(0, tv.tv_nsec);
179
180 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700181}
Elliott Hughesa6714d12017-12-04 14:16:38 -0800182
183TEST(sys_select, FD_ISSET_const) {
184 const fd_set none = {};
185 ASSERT_FALSE(FD_ISSET(atoi("0"), &none));
186}