blob: 4bb2d180ca3de6f4ac4f1b736ef07abf0d58d432 [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
46TEST(sys_select, select_smoke) {
47 fd_set r;
48 FD_ZERO(&r);
49 fd_set w;
50 FD_ZERO(&w);
51 fd_set e;
52 FD_ZERO(&e);
53
54 FD_SET(STDIN_FILENO, &r);
55 FD_SET(STDOUT_FILENO, &w);
56 FD_SET(STDERR_FILENO, &w);
57
58 int max = STDERR_FILENO + 1;
59
60 // Invalid max fd.
61 ASSERT_EQ(-1, select(-1, &r, &w, &e, NULL));
62 ASSERT_EQ(EINVAL, errno);
63
64 ASSERT_EQ(2, select(max, &r, &w, &e, NULL));
65
66 // Invalid timeout.
67 timeval tv;
68 tv.tv_sec = -1;
69 tv.tv_usec = 0;
70 ASSERT_EQ(-1, select(max, &r, &w, &e, &tv));
71 ASSERT_EQ(EINVAL, errno);
72
73 // Valid timeout...
74 tv.tv_sec = 1;
75 ASSERT_EQ(2, select(max, &r, &w, &e, &tv));
76 ASSERT_NE(0, tv.tv_usec); // ...which got updated.
77}
78
79TEST(sys_select, pselect_smoke) {
80 sigset_t ss;
81 sigemptyset(&ss);
82 sigaddset(&ss, SIGPIPE);
83
84 fd_set r;
85 FD_ZERO(&r);
86 fd_set w;
87 FD_ZERO(&w);
88 fd_set e;
89 FD_ZERO(&e);
90
91 FD_SET(STDIN_FILENO, &r);
92 FD_SET(STDOUT_FILENO, &w);
93 FD_SET(STDERR_FILENO, &w);
94
95 int max = STDERR_FILENO + 1;
96
97 // Invalid max fd.
98 ASSERT_EQ(-1, pselect(-1, &r, &w, &e, NULL, &ss));
99 ASSERT_EQ(EINVAL, errno);
100
101 ASSERT_EQ(2, pselect(max, &r, &w, &e, NULL, &ss));
102
103 // Invalid timeout.
104 timespec tv;
105 tv.tv_sec = -1;
106 tv.tv_nsec = 0;
107 ASSERT_EQ(-1, pselect(max, &r, &w, &e, &tv, &ss));
108 ASSERT_EQ(EINVAL, errno);
109
110 // Valid timeout...
111 tv.tv_sec = 1;
112 ASSERT_EQ(2, pselect(max, &r, &w, &e, &tv, &ss));
113 ASSERT_EQ(0, tv.tv_nsec); // ...which did _not_ get updated.
114}