blob: 18cf380b01c97a4071cb210281fbf39992541a86 [file] [log] [blame]
Elliott Hughes11952072013-10-24 15:15:14 -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>
Philip Hatcher9ded07c2014-02-25 14:16:37 +010020#include <fcntl.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080021#include <signal.h>
Elliott Hughes11952072013-10-24 15:15:14 -070022#include <sys/epoll.h>
Philip Hatcher9ded07c2014-02-25 14:16:37 +010023#include <unistd.h>
Elliott Hughes11952072013-10-24 15:15:14 -070024
Elliott Hughesa7f12942017-12-15 13:55:53 -080025#include "utils.h"
26
Elliott Hughes11952072013-10-24 15:15:14 -070027TEST(sys_epoll, smoke) {
28 int epoll_fd = epoll_create(1);
29 ASSERT_NE(-1, epoll_fd) << strerror(errno);
30 epoll_event events[1];
31
32 // Regular epoll_wait.
33 ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1));
34
35 // epoll_pwait without a sigset (which is equivalent to epoll_wait).
Yi Kong32bc0fc2018-08-02 17:31:13 -070036 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, nullptr));
Elliott Hughes11952072013-10-24 15:15:14 -070037
Elliott Hughes5905d6f2018-01-30 15:09:51 -080038#if defined(__BIONIC__)
39 // epoll_pwait64 without a sigset (which is equivalent to epoll_wait).
Yi Kong32bc0fc2018-08-02 17:31:13 -070040 ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, nullptr));
Elliott Hughes5905d6f2018-01-30 15:09:51 -080041#endif
42
Elliott Hughes11952072013-10-24 15:15:14 -070043 // epoll_pwait with a sigset.
44 sigset_t ss;
45 sigemptyset(&ss);
46 sigaddset(&ss, SIGPIPE);
47 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
48}
Philip Hatcher9ded07c2014-02-25 14:16:37 +010049
Greg Hackmanna03c62b2016-03-24 13:41:17 -070050TEST(sys_epoll, epoll_create_invalid_size) {
51 errno = 0;
52 ASSERT_EQ(-1, epoll_create(0));
53 ASSERT_EQ(EINVAL, errno);
54}
55
Philip Hatcher9ded07c2014-02-25 14:16:37 +010056TEST(sys_epoll, epoll_event_data) {
57 int epoll_fd = epoll_create(1);
58 ASSERT_NE(-1, epoll_fd) << strerror(errno);
59
60 int fds[2];
61 ASSERT_NE(-1, pipe(fds));
62
63 const uint64_t expected = 0x123456789abcdef0;
64
65 // Get ready to poll on read end of pipe.
66 epoll_event ev;
67 ev.events = EPOLLIN;
68 ev.data.u64 = expected;
69 ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev));
70
71 // Ensure there's something in the pipe.
72 ASSERT_EQ(1, write(fds[1], "\n", 1));
73
74 // Poll.
75 epoll_event events[1];
76 ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1));
77 ASSERT_EQ(expected, events[0].data.u64);
78
79 close(fds[0]);
80 close(fds[1]);
81}
Elliott Hughesa7f12942017-12-15 13:55:53 -080082
83TEST(sys_epoll, epoll_create1) {
84 int fd;
85 fd = epoll_create(1);
86 AssertCloseOnExec(fd, false);
87 close(fd);
88
89 fd = epoll_create1(0);
90 AssertCloseOnExec(fd, false);
91 close(fd);
92
93 fd = epoll_create1(EPOLL_CLOEXEC);
94 AssertCloseOnExec(fd, true);
95 close(fd);
96}