blob: 96e76630bdddef0586d95ba0095679408e5c4b71 [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);
93 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
94 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
95 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
96 if (num_fds == 3) {
97 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
98 }
Elliott Hughes11952072013-10-24 15:15:14 -070099
100 // Invalid timeout.
101 timeval tv;
102 tv.tv_sec = -1;
103 tv.tv_usec = 0;
104 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
105 ASSERT_EQ(EINVAL, errno);
106
107 // Valid timeout...
108 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700109 int pid, fd;
110 DelayedWrite(&pid, &fd);
111
112 FD_ZERO(&r);
113 FD_SET(fd, &r);
114 ASSERT_EQ(1, select(fd+1, &r, NULL, NULL, &tv));
115 // Both tv_sec and tv_nsec should have been updated.
116 ASSERT_EQ(0, tv.tv_sec);
117 ASSERT_NE(0, tv.tv_usec);
118
119 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700120}
121
122TEST(sys_select, pselect_smoke) {
123 sigset_t ss;
124 sigemptyset(&ss);
125 sigaddset(&ss, SIGPIPE);
126
127 fd_set r;
128 FD_ZERO(&r);
129 fd_set w;
130 FD_ZERO(&w);
131 fd_set e;
132 FD_ZERO(&e);
133
134 FD_SET(STDIN_FILENO, &r);
135 FD_SET(STDOUT_FILENO, &w);
136 FD_SET(STDERR_FILENO, &w);
137
138 int max = STDERR_FILENO + 1;
139
140 // Invalid max fd.
141 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
142 ASSERT_EQ(EINVAL, errno);
143
Christopher Ferrisdd926b52016-01-13 22:38:09 -0800144 int num_fds = pselect(max, &r, &w, &e, NULL, &ss);
145 ASSERT_TRUE(num_fds == 2 || num_fds == 3) << "Num fds returned " << num_fds;
146 ASSERT_TRUE(FD_ISSET(STDOUT_FILENO, &w));
147 ASSERT_TRUE(FD_ISSET(STDERR_FILENO, &w));
148 if (num_fds == 3) {
149 ASSERT_TRUE(FD_ISSET(STDIN_FILENO, &r));
150 }
Elliott Hughes11952072013-10-24 15:15:14 -0700151
152 // Invalid timeout.
153 timespec tv;
154 tv.tv_sec = -1;
155 tv.tv_nsec = 0;
156 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
157 ASSERT_EQ(EINVAL, errno);
158
159 // Valid timeout...
160 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700161 int pid, fd;
162 DelayedWrite(&pid, &fd);
163
164 FD_ZERO(&r);
165 FD_SET(fd, &r);
166 ASSERT_EQ(1, pselect(fd+1, &r, NULL, NULL, &tv, NULL));
167 // Neither tv_sec nor tv_nsec should have been updated.
168 ASSERT_EQ(1, tv.tv_sec);
169 ASSERT_EQ(0, tv.tv_nsec);
170
171 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700172}