blob: d1b411a276f4e08ac5293a68f797ea8eb903e0ff [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).
36 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, NULL));
37
38 // epoll_pwait with a sigset.
39 sigset_t ss;
40 sigemptyset(&ss);
41 sigaddset(&ss, SIGPIPE);
42 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
43}
Philip Hatcher9ded07c2014-02-25 14:16:37 +010044
Greg Hackmanna03c62b2016-03-24 13:41:17 -070045TEST(sys_epoll, epoll_create_invalid_size) {
46 errno = 0;
47 ASSERT_EQ(-1, epoll_create(0));
48 ASSERT_EQ(EINVAL, errno);
49}
50
Philip Hatcher9ded07c2014-02-25 14:16:37 +010051TEST(sys_epoll, epoll_event_data) {
52 int epoll_fd = epoll_create(1);
53 ASSERT_NE(-1, epoll_fd) << strerror(errno);
54
55 int fds[2];
56 ASSERT_NE(-1, pipe(fds));
57
58 const uint64_t expected = 0x123456789abcdef0;
59
60 // Get ready to poll on read end of pipe.
61 epoll_event ev;
62 ev.events = EPOLLIN;
63 ev.data.u64 = expected;
64 ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev));
65
66 // Ensure there's something in the pipe.
67 ASSERT_EQ(1, write(fds[1], "\n", 1));
68
69 // Poll.
70 epoll_event events[1];
71 ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1));
72 ASSERT_EQ(expected, events[0].data.u64);
73
74 close(fds[0]);
75 close(fds[1]);
76}
Elliott Hughesa7f12942017-12-15 13:55:53 -080077
78TEST(sys_epoll, epoll_create1) {
79 int fd;
80 fd = epoll_create(1);
81 AssertCloseOnExec(fd, false);
82 close(fd);
83
84 fd = epoll_create1(0);
85 AssertCloseOnExec(fd, false);
86 close(fd);
87
88 fd = epoll_create1(EPOLL_CLOEXEC);
89 AssertCloseOnExec(fd, true);
90 close(fd);
91}