1 /*
2 * Copyright (C) 2023 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 <sys/epoll.h>
20 #include <unistd.h>
21
TEST(EPoll,Pipe)22 TEST(EPoll, Pipe) {
23 int pipefd[2];
24 ASSERT_EQ(pipe(pipefd), 0);
25
26 int epfd = epoll_create(1);
27 ASSERT_NE(epfd, -1);
28
29 epoll_event event;
30 event.events = EPOLLIN | EPOLLOUT;
31
32 const uint64_t kData0 = 0x0123456701234567ULL;
33 event.data.u64 = kData0;
34 ASSERT_EQ(epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event), 0);
35
36 const uint64_t kData1 = 0x7654321076543210ULL;
37 event.data.u64 = kData1;
38 ASSERT_EQ(epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[1], &event), 0);
39
40 epoll_event events[2];
41 ASSERT_EQ(epoll_wait(epfd, events, 2, -1), 1);
42 ASSERT_EQ(events[0].data.u64, kData1);
43
44 char buf = ' ';
45 ASSERT_EQ(write(pipefd[1], &buf, 1), 1);
46
47 ASSERT_EQ(epoll_ctl(epfd, EPOLL_CTL_DEL, pipefd[1], nullptr), 0);
48
49 ASSERT_EQ(epoll_wait(epfd, events, 2, -1), 1);
50 ASSERT_EQ(events[0].data.u64, kData0);
51
52 close(epfd);
53 close(pipefd[0]);
54 close(pipefd[1]);
55 }
56