blob: 57afc9b8dafa202156ee7907d6562e3f252406f7 [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>
20#include <stdlib.h>
21#include <sys/select.h>
22
23TEST(sys_select, fd_set_smoke) {
24 fd_set fds;
25 FD_ZERO(&fds);
26
27 for (size_t i = 0; i < 1024; ++i) {
28 EXPECT_FALSE(FD_ISSET(i, &fds));
29 }
30
31 FD_SET(0, &fds);
32 EXPECT_TRUE(FD_ISSET(0, &fds));
33 EXPECT_FALSE(FD_ISSET(1, &fds));
34 FD_SET(1, &fds);
35 EXPECT_TRUE(FD_ISSET(0, &fds));
36 EXPECT_TRUE(FD_ISSET(1, &fds));
37 FD_CLR(0, &fds);
38 EXPECT_FALSE(FD_ISSET(0, &fds));
39 EXPECT_TRUE(FD_ISSET(1, &fds));
40 FD_CLR(1, &fds);
41 EXPECT_FALSE(FD_ISSET(0, &fds));
42 EXPECT_FALSE(FD_ISSET(1, &fds));
43}
Elliott Hughes11952072013-10-24 15:15:14 -070044
45TEST(sys_select, select_smoke) {
46 fd_set r;
47 FD_ZERO(&r);
48 fd_set w;
49 FD_ZERO(&w);
50 fd_set e;
51 FD_ZERO(&e);
52
53 FD_SET(STDIN_FILENO, &r);
54 FD_SET(STDOUT_FILENO, &w);
55 FD_SET(STDERR_FILENO, &w);
56
57 int max = STDERR_FILENO + 1;
58
59 // Invalid max fd.
60 ASSERT_EQ(-1, select(-1, &r, &w, &e, NULL));
61 ASSERT_EQ(EINVAL, errno);
62
63 ASSERT_EQ(2, select(max, &r, &w, &e, NULL));
64
65 // Invalid timeout.
66 timeval tv;
67 tv.tv_sec = -1;
68 tv.tv_usec = 0;
69 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
70 ASSERT_EQ(EINVAL, errno);
71
72 // Valid timeout...
73 tv.tv_sec = 1;
74 ASSERT_EQ(2, select(max, &r, &w, &e, &tv));
75 ASSERT_NE(0, tv.tv_usec); // ...which got updated.
76}
77
78TEST(sys_select, pselect_smoke) {
79 sigset_t ss;
80 sigemptyset(&ss);
81 sigaddset(&ss, SIGPIPE);
82
83 fd_set r;
84 FD_ZERO(&r);
85 fd_set w;
86 FD_ZERO(&w);
87 fd_set e;
88 FD_ZERO(&e);
89
90 FD_SET(STDIN_FILENO, &r);
91 FD_SET(STDOUT_FILENO, &w);
92 FD_SET(STDERR_FILENO, &w);
93
94 int max = STDERR_FILENO + 1;
95
96 // Invalid max fd.
97 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
98 ASSERT_EQ(EINVAL, errno);
99
100 ASSERT_EQ(2, pselect(max, &r, &w, &e, NULL, &ss));
101
102 // Invalid timeout.
103 timespec tv;
104 tv.tv_sec = -1;
105 tv.tv_nsec = 0;
106 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
107 ASSERT_EQ(EINVAL, errno);
108
109 // Valid timeout...
110 tv.tv_sec = 1;
111 ASSERT_EQ(2, pselect(max, &r, &w, &e, &tv, &ss));
112 ASSERT_EQ(0, tv.tv_nsec); // ...which did _not_ get updated.
113}