blob: 5252e6bf7bc57bddc5a4e41b642a9776c55aa431 [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>
23
24TEST(sys_select, fd_set_smoke) {
25 fd_set fds;
26 FD_ZERO(&fds);
27
28 for (size_t i = 0; i < 1024; ++i) {
29 EXPECT_FALSE(FD_ISSET(i, &fds));
30 }
31
32 FD_SET(0, &fds);
33 EXPECT_TRUE(FD_ISSET(0, &fds));
34 EXPECT_FALSE(FD_ISSET(1, &fds));
35 FD_SET(1, &fds);
36 EXPECT_TRUE(FD_ISSET(0, &fds));
37 EXPECT_TRUE(FD_ISSET(1, &fds));
38 FD_CLR(0, &fds);
39 EXPECT_FALSE(FD_ISSET(0, &fds));
40 EXPECT_TRUE(FD_ISSET(1, &fds));
41 FD_CLR(1, &fds);
42 EXPECT_FALSE(FD_ISSET(0, &fds));
43 EXPECT_FALSE(FD_ISSET(1, &fds));
44}
Elliott Hughes11952072013-10-24 15:15:14 -070045
Christopher Ferrise3bb0252014-03-31 22:51:27 -070046#define DELAY_MSG "1234"
47
48static void DelayedWrite(int* pid, int* fd) {
49 int fds[2];
50 ASSERT_EQ(0, pipe(fds));
51
52 if ((*pid = fork()) == 0) {
53 close(fds[0]);
54 usleep(5000);
55 EXPECT_EQ(5, write(fds[1], DELAY_MSG, sizeof(DELAY_MSG)));
56 close(fds[1]);
57 exit(0);
58 }
59 ASSERT_LT(0, *pid);
60 close(fds[1]);
61
62 *fd = fds[0];
63}
64
65static void DelayedWriteCleanup(int pid, int fd) {
66 char buf[sizeof(DELAY_MSG)];
67 ASSERT_EQ(static_cast<ssize_t>(sizeof(DELAY_MSG)), read(fd, buf, sizeof(DELAY_MSG)));
68 ASSERT_STREQ(DELAY_MSG, buf);
69 ASSERT_EQ(pid, waitpid(pid, NULL, 0));
70}
71
Elliott Hughes11952072013-10-24 15:15:14 -070072TEST(sys_select, select_smoke) {
73 fd_set r;
74 FD_ZERO(&r);
75 fd_set w;
76 FD_ZERO(&w);
77 fd_set e;
78 FD_ZERO(&e);
79
80 FD_SET(STDIN_FILENO, &r);
81 FD_SET(STDOUT_FILENO, &w);
82 FD_SET(STDERR_FILENO, &w);
83
84 int max = STDERR_FILENO + 1;
85
86 // Invalid max fd.
87 ASSERT_EQ(-1, select(-1, &r, &w, &e, NULL));
88 ASSERT_EQ(EINVAL, errno);
89
90 ASSERT_EQ(2, select(max, &r, &w, &e, NULL));
91
92 // Invalid timeout.
93 timeval tv;
94 tv.tv_sec = -1;
95 tv.tv_usec = 0;
96 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
97 ASSERT_EQ(EINVAL, errno);
98
99 // Valid timeout...
100 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700101 int pid, fd;
102 DelayedWrite(&pid, &fd);
103
104 FD_ZERO(&r);
105 FD_SET(fd, &r);
106 ASSERT_EQ(1, select(fd+1, &r, NULL, NULL, &tv));
107 // Both tv_sec and tv_nsec should have been updated.
108 ASSERT_EQ(0, tv.tv_sec);
109 ASSERT_NE(0, tv.tv_usec);
110
111 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700112}
113
114TEST(sys_select, pselect_smoke) {
115 sigset_t ss;
116 sigemptyset(&ss);
117 sigaddset(&ss, SIGPIPE);
118
119 fd_set r;
120 FD_ZERO(&r);
121 fd_set w;
122 FD_ZERO(&w);
123 fd_set e;
124 FD_ZERO(&e);
125
126 FD_SET(STDIN_FILENO, &r);
127 FD_SET(STDOUT_FILENO, &w);
128 FD_SET(STDERR_FILENO, &w);
129
130 int max = STDERR_FILENO + 1;
131
132 // Invalid max fd.
133 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
134 ASSERT_EQ(EINVAL, errno);
135
136 ASSERT_EQ(2, pselect(max, &r, &w, &e, NULL, &ss));
137
138 // Invalid timeout.
139 timespec tv;
140 tv.tv_sec = -1;
141 tv.tv_nsec = 0;
142 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
143 ASSERT_EQ(EINVAL, errno);
144
145 // Valid timeout...
146 tv.tv_sec = 1;
Christopher Ferrise3bb0252014-03-31 22:51:27 -0700147 int pid, fd;
148 DelayedWrite(&pid, &fd);
149
150 FD_ZERO(&r);
151 FD_SET(fd, &r);
152 ASSERT_EQ(1, pselect(fd+1, &r, NULL, NULL, &tv, NULL));
153 // Neither tv_sec nor tv_nsec should have been updated.
154 ASSERT_EQ(1, tv.tv_sec);
155 ASSERT_EQ(0, tv.tv_nsec);
156
157 DelayedWriteCleanup(pid, fd);
Elliott Hughes11952072013-10-24 15:15:14 -0700158}