1/*
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>
20#include <signal.h>
21#include <stdlib.h>
22#include <sys/select.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
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}
47
48#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
74TEST(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
92  ASSERT_EQ(2, select(max, &r, &w, &e, NULL));
93
94  // Invalid timeout.
95  timeval tv;
96  tv.tv_sec = -1;
97  tv.tv_usec = 0;
98  ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
99  ASSERT_EQ(EINVAL, errno);
100
101  // Valid timeout...
102  tv.tv_sec = 1;
103  int pid, fd;
104  DelayedWrite(&pid, &fd);
105
106  FD_ZERO(&r);
107  FD_SET(fd, &r);
108  ASSERT_EQ(1, select(fd+1, &r, NULL, NULL, &tv));
109  // Both tv_sec and tv_nsec should have been updated.
110  ASSERT_EQ(0, tv.tv_sec);
111  ASSERT_NE(0, tv.tv_usec);
112
113  DelayedWriteCleanup(pid, fd);
114}
115
116TEST(sys_select, pselect_smoke) {
117  sigset_t ss;
118  sigemptyset(&ss);
119  sigaddset(&ss, SIGPIPE);
120
121  fd_set r;
122  FD_ZERO(&r);
123  fd_set w;
124  FD_ZERO(&w);
125  fd_set e;
126  FD_ZERO(&e);
127
128  FD_SET(STDIN_FILENO, &r);
129  FD_SET(STDOUT_FILENO, &w);
130  FD_SET(STDERR_FILENO, &w);
131
132  int max = STDERR_FILENO + 1;
133
134  // Invalid max fd.
135  ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
136  ASSERT_EQ(EINVAL, errno);
137
138  ASSERT_EQ(2, pselect(max, &r, &w, &e, NULL, &ss));
139
140  // Invalid timeout.
141  timespec tv;
142  tv.tv_sec = -1;
143  tv.tv_nsec = 0;
144  ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
145  ASSERT_EQ(EINVAL, errno);
146
147  // Valid timeout...
148  tv.tv_sec = 1;
149  int pid, fd;
150  DelayedWrite(&pid, &fd);
151
152  FD_ZERO(&r);
153  FD_SET(fd, &r);
154  ASSERT_EQ(1, pselect(fd+1, &r, NULL, NULL, &tv, NULL));
155  // Neither tv_sec nor tv_nsec should have been updated.
156  ASSERT_EQ(1, tv.tv_sec);
157  ASSERT_EQ(0, tv.tv_nsec);
158
159  DelayedWriteCleanup(pid, fd);
160}
161