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 
TEST(sys_epoll,smoke)25 TEST(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 
TEST(sys_epoll,epoll_create_invalid_size)43 TEST(sys_epoll, epoll_create_invalid_size) {
44   errno = 0;
45   ASSERT_EQ(-1, epoll_create(0));
46   ASSERT_EQ(EINVAL, errno);
47 }
48 
TEST(sys_epoll,epoll_event_data)49 TEST(sys_epoll, epoll_event_data) {
50   int epoll_fd = epoll_create(1);
51   ASSERT_NE(-1, epoll_fd) << strerror(errno);
52 
53   int fds[2];
54   ASSERT_NE(-1, pipe(fds));
55 
56   const uint64_t expected = 0x123456789abcdef0;
57 
58   // Get ready to poll on read end of pipe.
59   epoll_event ev;
60   ev.events = EPOLLIN;
61   ev.data.u64 = expected;
62   ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev));
63 
64   // Ensure there's something in the pipe.
65   ASSERT_EQ(1, write(fds[1], "\n", 1));
66 
67   // Poll.
68   epoll_event events[1];
69   ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1));
70   ASSERT_EQ(expected, events[0].data.u64);
71 
72   close(fds[0]);
73   close(fds[1]);
74 }
75