blob: 33034d8cd9eb9c36644928ff0c45a97146566b0a [file] [log] [blame]
Yabin Cuibec02fc2015-08-28 15:44:27 -07001/*
2 * Copyright (C) 2015 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 "fdevent.h"
18
19#include <gtest/gtest.h>
20
Yabin Cuia1080162015-09-04 16:19:56 -070021#include <limits>
Yabin Cuibec02fc2015-08-28 15:44:27 -070022#include <queue>
23#include <string>
24#include <vector>
25
26#include <pthread.h>
27#include <signal.h>
28
29#include "adb_io.h"
30
31class SignalHandlerRegister {
32 public:
33 SignalHandlerRegister(const std::vector<int>& signums, void (*handler)(int)) {
34 for (auto& sig : signums) {
35 sig_t old_handler = signal(sig, handler);
36 saved_signal_handlers_.push_back(std::make_pair(sig, old_handler));
37 }
38 }
39
40 ~SignalHandlerRegister() {
41 for (auto& pair : saved_signal_handlers_) {
42 signal(pair.first, pair.second);
43 }
44 }
45
46 private:
47 std::vector<std::pair<int, sig_t>> saved_signal_handlers_;
48};
49
50class FdHandler {
51 public:
52 FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
53 fdevent_install(&read_fde_, read_fd_, FdEventCallback, this);
Yabin Cuia1080162015-09-04 16:19:56 -070054 fdevent_add(&read_fde_, FDE_READ);
Yabin Cuibec02fc2015-08-28 15:44:27 -070055 fdevent_install(&write_fde_, write_fd_, FdEventCallback, this);
Yabin Cuia1080162015-09-04 16:19:56 -070056 }
57
58 ~FdHandler() {
59 fdevent_remove(&read_fde_);
60 fdevent_remove(&write_fde_);
Yabin Cuibec02fc2015-08-28 15:44:27 -070061 }
62
63 private:
64 static void FdEventCallback(int fd, unsigned events, void* userdata) {
65 FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
66 ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
67 if (events & FDE_READ) {
68 ASSERT_EQ(fd, handler->read_fd_);
69 char c;
70 ASSERT_EQ(1, read(fd, &c, 1));
71 handler->queue_.push(c);
72 fdevent_add(&handler->write_fde_, FDE_WRITE);
73 }
74 if (events & FDE_WRITE) {
75 ASSERT_EQ(fd, handler->write_fd_);
76 ASSERT_FALSE(handler->queue_.empty());
77 char c = handler->queue_.front();
78 handler->queue_.pop();
79 ASSERT_EQ(1, write(fd, &c, 1));
80 if (handler->queue_.empty()) {
81 fdevent_del(&handler->write_fde_, FDE_WRITE);
82 }
83 }
84 }
85
86 private:
87 const int read_fd_;
88 const int write_fd_;
89 fdevent read_fde_;
90 fdevent write_fde_;
91 std::queue<char> queue_;
92};
93
94static void signal_handler(int) {
95 pthread_exit(nullptr);
96}
97
98struct ThreadArg {
99 int first_read_fd;
100 int last_write_fd;
101 size_t middle_pipe_count;
102};
103
104static void FdEventThreadFunc(ThreadArg* arg) {
105 SignalHandlerRegister signal_handler_register({SIGUSR1}, signal_handler);
106
107 std::vector<int> read_fds;
108 std::vector<int> write_fds;
109
110 read_fds.push_back(arg->first_read_fd);
111 for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
112 int fds[2];
113 ASSERT_EQ(0, pipe(fds));
114 read_fds.push_back(fds[0]);
115 write_fds.push_back(fds[1]);
116 }
117 write_fds.push_back(arg->last_write_fd);
118
119 std::vector<std::unique_ptr<FdHandler>> fd_handlers;
120 for (size_t i = 0; i < read_fds.size(); ++i) {
121 fd_handlers.push_back(std::unique_ptr<FdHandler>(new FdHandler(read_fds[i], write_fds[i])));
122 }
123
124 fdevent_loop();
125}
126
127TEST(fdevent, smoke) {
128 const size_t PIPE_COUNT = 10;
129 const size_t MESSAGE_LOOP_COUNT = 100;
130 const std::string MESSAGE = "fdevent_test";
131 int fd_pair1[2];
132 int fd_pair2[2];
133 ASSERT_EQ(0, pipe(fd_pair1));
134 ASSERT_EQ(0, pipe(fd_pair2));
135 pthread_t thread;
136 ThreadArg thread_arg;
137 thread_arg.first_read_fd = fd_pair1[0];
138 thread_arg.last_write_fd = fd_pair2[1];
139 thread_arg.middle_pipe_count = PIPE_COUNT;
140 int writer = fd_pair1[1];
141 int reader = fd_pair2[0];
142
143 ASSERT_EQ(0, pthread_create(&thread, nullptr,
144 reinterpret_cast<void* (*)(void*)>(FdEventThreadFunc),
145 &thread_arg));
146
147 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
148 std::string read_buffer = MESSAGE;
149 std::string write_buffer(MESSAGE.size(), 'a');
150 ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
151 ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
152 ASSERT_EQ(read_buffer, write_buffer);
153 }
154
155 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
156 ASSERT_EQ(0, pthread_join(thread, nullptr));
157}
Yabin Cuia1080162015-09-04 16:19:56 -0700158
159struct InvalidFdArg {
160 fdevent fde;
161 unsigned expected_events;
162 size_t* happened_event_count;
163};
164
165static void InvalidFdEventCallback(int fd, unsigned events, void* userdata) {
166 InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
167 ASSERT_EQ(arg->expected_events, events);
168 fdevent_remove(&arg->fde);
169 if (++*(arg->happened_event_count) == 2) {
170 pthread_exit(nullptr);
171 }
172}
173
174void InvalidFdThreadFunc(void*) {
175 const int INVALID_READ_FD = std::numeric_limits<int>::max() - 1;
176 size_t happened_event_count = 0;
177 InvalidFdArg read_arg;
178 read_arg.expected_events = FDE_READ | FDE_ERROR;
179 read_arg.happened_event_count = &happened_event_count;
180 fdevent_install(&read_arg.fde, INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
181 fdevent_add(&read_arg.fde, FDE_READ);
182
183 const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
184 InvalidFdArg write_arg;
185 write_arg.expected_events = FDE_READ | FDE_ERROR;
186 write_arg.happened_event_count = &happened_event_count;
187 fdevent_install(&write_arg.fde, INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
188 fdevent_add(&write_arg.fde, FDE_WRITE);
189 fdevent_loop();
190}
191
192TEST(fdevent, invalid_fd) {
193 pthread_t thread;
194 ASSERT_EQ(0, pthread_create(&thread, nullptr,
195 reinterpret_cast<void* (*)(void*)>(InvalidFdThreadFunc),
196 nullptr));
197 ASSERT_EQ(0, pthread_join(thread, nullptr));
198}