blob: d4ac333d2f9423775debf075bc530f4ac69a5ed3 [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
26TEST(sys_select, fd_set_smoke) {
27 fd_set fds;
28 FD_ZERO(&fds);
29
30 for (size_t i = 0; i < 1024; ++i) {
31 EXPECT_FALSE(FD_ISSET(i, &fds));
32 }
33
34 FD_SET(0, &fds);
35 EXPECT_TRUE(FD_ISSET(0, &fds));
36 EXPECT_FALSE(FD_ISSET(1, &fds));
37 FD_SET(1, &fds);
38 EXPECT_TRUE(FD_ISSET(0, &fds));
39 EXPECT_TRUE(FD_ISSET(1, &fds));
40 FD_CLR(0, &fds);
41 EXPECT_FALSE(FD_ISSET(0, &fds));
42 EXPECT_TRUE(FD_ISSET(1, &fds));
43 FD_CLR(1, &fds);
44 EXPECT_FALSE(FD_ISSET(0, &fds));
45 EXPECT_FALSE(FD_ISSET(1, &fds));
46}
Elliott Hughes11952072013-10-24 15:15:14 -070047
Christopher Ferrise3bb0252014-03-31 22:51:27 -070048#define DELAY_MSG "1234"
49
50static void DelayedWrite(int* pid, int* fd) {
51 int fds[2];
52 ASSERT_EQ(0, pipe(fds));
53
54 if ((*pid = fork()) == 0) {
55 close(fds[0]);
56 usleep(5000);
57 EXPECT_EQ(5, write(fds[1], DELAY_MSG, sizeof(DELAY_MSG)));
58 close(fds[1]);
59 exit(0);
60 }
61 ASSERT_LT(0, *pid);
62 close(fds[1]);
63
64 *fd = fds[0];
65}
66
67static void DelayedWriteCleanup(int pid, int fd) {
68 char buf[sizeof(DELAY_MSG)];
69 ASSERT_EQ(static_cast<ssize_t>(sizeof(DELAY_MSG)), read(fd, buf, sizeof(DELAY_MSG)));
70 ASSERT_STREQ(DELAY_MSG, buf);
71 ASSERT_EQ(pid, waitpid(pid, NULL, 0));
72}
73
Elliott Hughes11952072013-10-24 15:15:14 -070074TEST(sys_select, select_smoke) {
75 fd_set r;
76 FD_ZERO(&r);
77 fd_set w;
78 FD_ZERO(&w);
79 fd_set e;
80 FD_ZERO(&e);
81
82 FD_SET(STDIN_FILENO, &r);
83 FD_SET(STDOUT_FILENO, &w);
84 FD_SET(STDERR_FILENO, &w);
85
86 int max = STDERR_FILENO + 1;
87
88 // Invalid max fd.
89 ASSERT_EQ(-1, select(-1, &r, &w, &e, NULL));
90 ASSERT_EQ(EINVAL, errno);
91
Christopher Ferrisdd926b52016-01-13 22:38:09 -080092 int num_fds = select(max, &r, &w, &e, NULL);
Christopher Ferrise8efb962016-01-14 11:12:38 -080093 // If there is data to be read on STDIN, then the number of
94 // fds ready will be 3 instead of 2. Allow this case, but verify
95 // every fd that is set.
Christopher Ferrisdd926b52016-01-13 22:38:09 -080096 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
97 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
98 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
99 if (num_fds == 3) {
100 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
101 }
Elliott Hughes11952072013-10-24 15:15:14 -0700102
103 // Invalid timeout.
104 timeval tv;
105 tv.tv_sec = -1;
106 tv.tv_usec = 0;
107 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
108 ASSERT_EQ(EINVAL, errno);
109
110 // Valid timeout...
111 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700112 int pid, fd;
113 DelayedWrite(&pid, &fd);
114
115 FD_ZERO(&r);
116 FD_SET(fd, &r);
117 ASSERT_EQ(1, select(fd+1, &r, NULL, NULL, &tv));
118 // Both tv_sec and tv_nsec should have been updated.
119 ASSERT_EQ(0, tv.tv_sec);
120 ASSERT_NE(0, tv.tv_usec);
121
122 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700123}
124
125TEST(sys_select, pselect_smoke) {
126 sigset_t ss;
127 sigemptyset(&ss);
128 sigaddset(&ss, SIGPIPE);
129
130 fd_set r;
131 FD_ZERO(&r);
132 fd_set w;
133 FD_ZERO(&w);
134 fd_set e;
135 FD_ZERO(&e);
136
137 FD_SET(STDIN_FILENO, &r);
138 FD_SET(STDOUT_FILENO, &w);
139 FD_SET(STDERR_FILENO, &w);
140
141 int max = STDERR_FILENO + 1;
142
143 // Invalid max fd.
144 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
145 ASSERT_EQ(EINVAL, errno);
146
Christopher Ferrise8efb962016-01-14 11:12:38 -0800147 // If there is data to be read on STDIN, then the number of
148 // fds ready will be 3 instead of 2. Allow this case, but verify
149 // every fd that is set.
Christopher Ferrisdd926b52016-01-13 22:38:09 -0800150 int num_fds = pselect(max, &r, &w, &e, NULL, &ss);
151 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
152 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
153 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
154 if (num_fds == 3) {
155 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
156 }
Elliott Hughes11952072013-10-24 15:15:14 -0700157
158 // Invalid timeout.
159 timespec tv;
160 tv.tv_sec = -1;
161 tv.tv_nsec = 0;
162 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
163 ASSERT_EQ(EINVAL, errno);
164
165 // Valid timeout...
166 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700167 int pid, fd;
168 DelayedWrite(&pid, &fd);
169
170 FD_ZERO(&r);
171 FD_SET(fd, &r);
172 ASSERT_EQ(1, pselect(fd+1, &r, NULL, NULL, &tv, NULL));
173 // Neither tv_sec nor tv_nsec should have been updated.
174 ASSERT_EQ(1, tv.tv_sec);
175 ASSERT_EQ(0, tv.tv_nsec);
176
177 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700178}