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 <fcntl.h>
21#include <signal.h>
22#include <sys/epoll.h>
23#include <unistd.h>
24
25TEST(sys_epoll, smoke) {
26  int epoll_fd = epoll_create(1);
27  ASSERT_NE(-1, epoll_fd) << strerror(errno);
28  epoll_event events[1];
29
30  // Regular epoll_wait.
31  ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1));
32
33  // epoll_pwait without a sigset (which is equivalent to epoll_wait).
34  ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, NULL));
35
36  // epoll_pwait with a sigset.
37  sigset_t ss;
38  sigemptyset(&ss);
39  sigaddset(&ss, SIGPIPE);
40  ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
41}
42
43TEST(sys_epoll, epoll_event_data) {
44  int epoll_fd = epoll_create(1);
45  ASSERT_NE(-1, epoll_fd) << strerror(errno);
46
47  int fds[2];
48  ASSERT_NE(-1, pipe(fds));
49
50  const uint64_t expected = 0x123456789abcdef0;
51
52  // Get ready to poll on read end of pipe.
53  epoll_event ev;
54  ev.events = EPOLLIN;
55  ev.data.u64 = expected;
56  ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev));
57
58  // Ensure there's something in the pipe.
59  ASSERT_EQ(1, write(fds[1], "\n", 1));
60
61  // Poll.
62  epoll_event events[1];
63  ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1));
64  ASSERT_EQ(expected, events[0].data.u64);
65
66  close(fds[0]);
67  close(fds[1]);
68}
69