1/*
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
21#include <limits>
22#include <queue>
23#include <string>
24#include <vector>
25
26#include "adb_io.h"
27#include "fdevent_test.h"
28
29class FdHandler {
30  public:
31    FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
32        fdevent_install(&read_fde_, read_fd_, FdEventCallback, this);
33        fdevent_add(&read_fde_, FDE_READ);
34        fdevent_install(&write_fde_, write_fd_, FdEventCallback, this);
35    }
36
37    ~FdHandler() {
38        fdevent_remove(&read_fde_);
39        fdevent_remove(&write_fde_);
40    }
41
42  private:
43    static void FdEventCallback(int fd, unsigned events, void* userdata) {
44        FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
45        ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
46        if (events & FDE_READ) {
47            ASSERT_EQ(fd, handler->read_fd_);
48            char c;
49            ASSERT_EQ(1, adb_read(fd, &c, 1));
50            handler->queue_.push(c);
51            fdevent_add(&handler->write_fde_, FDE_WRITE);
52        }
53        if (events & FDE_WRITE) {
54            ASSERT_EQ(fd, handler->write_fd_);
55            ASSERT_FALSE(handler->queue_.empty());
56            char c = handler->queue_.front();
57            handler->queue_.pop();
58            ASSERT_EQ(1, adb_write(fd, &c, 1));
59            if (handler->queue_.empty()) {
60              fdevent_del(&handler->write_fde_, FDE_WRITE);
61            }
62        }
63    }
64
65  private:
66    const int read_fd_;
67    const int write_fd_;
68    fdevent read_fde_;
69    fdevent write_fde_;
70    std::queue<char> queue_;
71};
72
73struct ThreadArg {
74    int first_read_fd;
75    int last_write_fd;
76    size_t middle_pipe_count;
77};
78
79TEST_F(FdeventTest, fdevent_terminate) {
80    adb_thread_t thread;
81    PrepareThread();
82    ASSERT_TRUE(adb_thread_create([](void*) { fdevent_loop(); }, nullptr, &thread));
83    TerminateThread(thread);
84}
85
86static void FdEventThreadFunc(ThreadArg* arg) {
87    std::vector<int> read_fds;
88    std::vector<int> write_fds;
89
90    read_fds.push_back(arg->first_read_fd);
91    for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
92        int fds[2];
93        ASSERT_EQ(0, adb_socketpair(fds));
94        read_fds.push_back(fds[0]);
95        write_fds.push_back(fds[1]);
96    }
97    write_fds.push_back(arg->last_write_fd);
98
99    std::vector<std::unique_ptr<FdHandler>> fd_handlers;
100    for (size_t i = 0; i < read_fds.size(); ++i) {
101        fd_handlers.push_back(std::unique_ptr<FdHandler>(new FdHandler(read_fds[i], write_fds[i])));
102    }
103
104    fdevent_loop();
105}
106
107TEST_F(FdeventTest, smoke) {
108    const size_t PIPE_COUNT = 10;
109    const size_t MESSAGE_LOOP_COUNT = 100;
110    const std::string MESSAGE = "fdevent_test";
111    int fd_pair1[2];
112    int fd_pair2[2];
113    ASSERT_EQ(0, adb_socketpair(fd_pair1));
114    ASSERT_EQ(0, adb_socketpair(fd_pair2));
115    adb_thread_t thread;
116    ThreadArg thread_arg;
117    thread_arg.first_read_fd = fd_pair1[0];
118    thread_arg.last_write_fd = fd_pair2[1];
119    thread_arg.middle_pipe_count = PIPE_COUNT;
120    int writer = fd_pair1[1];
121    int reader = fd_pair2[0];
122
123    PrepareThread();
124    ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(FdEventThreadFunc), &thread_arg,
125                                  &thread));
126
127    for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
128        std::string read_buffer = MESSAGE;
129        std::string write_buffer(MESSAGE.size(), 'a');
130        ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
131        ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
132        ASSERT_EQ(read_buffer, write_buffer);
133    }
134
135    TerminateThread(thread);
136    ASSERT_EQ(0, adb_close(writer));
137    ASSERT_EQ(0, adb_close(reader));
138}
139
140struct InvalidFdArg {
141    fdevent fde;
142    unsigned expected_events;
143    size_t* happened_event_count;
144};
145
146static void InvalidFdEventCallback(int fd, unsigned events, void* userdata) {
147    InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
148    ASSERT_EQ(arg->expected_events, events);
149    fdevent_remove(&arg->fde);
150    if (++*(arg->happened_event_count) == 2) {
151        fdevent_terminate_loop();
152    }
153}
154
155static void InvalidFdThreadFunc(void*) {
156    const int INVALID_READ_FD = std::numeric_limits<int>::max() - 1;
157    size_t happened_event_count = 0;
158    InvalidFdArg read_arg;
159    read_arg.expected_events = FDE_READ | FDE_ERROR;
160    read_arg.happened_event_count = &happened_event_count;
161    fdevent_install(&read_arg.fde, INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
162    fdevent_add(&read_arg.fde, FDE_READ);
163
164    const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
165    InvalidFdArg write_arg;
166    write_arg.expected_events = FDE_READ | FDE_ERROR;
167    write_arg.happened_event_count = &happened_event_count;
168    fdevent_install(&write_arg.fde, INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
169    fdevent_add(&write_arg.fde, FDE_WRITE);
170    fdevent_loop();
171}
172
173TEST_F(FdeventTest, invalid_fd) {
174    adb_thread_t thread;
175    ASSERT_TRUE(adb_thread_create(InvalidFdThreadFunc, nullptr, &thread));
176    ASSERT_TRUE(adb_thread_join(thread));
177}
178