2015-09-16 07:27:09 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdevent.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
#include <array>
|
2015-09-16 07:27:09 +08:00
|
|
|
#include <limits>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_io.h"
|
2016-02-11 06:49:00 +08:00
|
|
|
#include "fdevent_test.h"
|
2015-09-16 07:27:09 +08:00
|
|
|
#include "socket.h"
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
|
|
|
struct ThreadArg {
|
|
|
|
int first_read_fd;
|
|
|
|
int last_write_fd;
|
|
|
|
size_t middle_pipe_count;
|
|
|
|
};
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
class LocalSocketTest : public FdeventTest {};
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
static void FdEventThreadFunc(void*) {
|
2015-09-16 07:27:09 +08:00
|
|
|
fdevent_loop();
|
|
|
|
}
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
TEST_F(LocalSocketTest, smoke) {
|
|
|
|
// Join two socketpairs with a chain of intermediate socketpairs.
|
|
|
|
int first[2];
|
|
|
|
std::vector<std::array<int, 2>> intermediates;
|
|
|
|
int last[2];
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
constexpr size_t INTERMEDIATE_COUNT = 50;
|
|
|
|
constexpr size_t MESSAGE_LOOP_COUNT = 100;
|
|
|
|
const std::string MESSAGE = "socket_test";
|
|
|
|
|
|
|
|
intermediates.resize(INTERMEDIATE_COUNT);
|
|
|
|
ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
|
|
|
|
ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
|
|
|
|
asocket* prev_tail = create_local_socket(first[1]);
|
|
|
|
ASSERT_NE(nullptr, prev_tail);
|
|
|
|
|
|
|
|
auto connect = [](asocket* tail, asocket* head) {
|
|
|
|
tail->peer = head;
|
|
|
|
head->peer = tail;
|
|
|
|
tail->ready(tail);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& intermediate : intermediates) {
|
|
|
|
ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
|
|
|
|
|
|
|
|
asocket* head = create_local_socket(intermediate[0]);
|
|
|
|
ASSERT_NE(nullptr, head);
|
|
|
|
|
|
|
|
asocket* tail = create_local_socket(intermediate[1]);
|
|
|
|
ASSERT_NE(nullptr, tail);
|
|
|
|
|
|
|
|
connect(prev_tail, head);
|
|
|
|
prev_tail = tail;
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
asocket* end = create_local_socket(last[0]);
|
|
|
|
ASSERT_NE(nullptr, end);
|
|
|
|
connect(prev_tail, end);
|
|
|
|
|
|
|
|
PrepareThread();
|
|
|
|
adb_thread_t thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(FdEventThreadFunc, nullptr, &thread));
|
|
|
|
|
2015-09-16 07:27:09 +08:00
|
|
|
for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
|
|
|
|
std::string read_buffer = MESSAGE;
|
|
|
|
std::string write_buffer(MESSAGE.size(), 'a');
|
2016-02-11 06:49:00 +08:00
|
|
|
ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
|
|
|
|
ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(read_buffer, write_buffer);
|
|
|
|
}
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
ASSERT_EQ(0, adb_close(first[0]));
|
|
|
|
ASSERT_EQ(0, adb_close(last[1]));
|
|
|
|
|
|
|
|
// Wait until the local sockets are closed.
|
|
|
|
adb_sleep_ms(100);
|
|
|
|
TerminateThread(thread);
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CloseWithPacketArg {
|
|
|
|
int socket_fd;
|
|
|
|
size_t bytes_written;
|
|
|
|
int cause_close_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
|
|
|
|
asocket* s = create_local_socket(arg->socket_fd);
|
|
|
|
ASSERT_TRUE(s != nullptr);
|
|
|
|
arg->bytes_written = 0;
|
|
|
|
while (true) {
|
|
|
|
apacket* p = get_apacket();
|
|
|
|
p->len = sizeof(p->data);
|
|
|
|
arg->bytes_written += p->len;
|
|
|
|
int ret = s->enqueue(s, p);
|
|
|
|
if (ret == 1) {
|
|
|
|
// The writer has one packet waiting to send.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
|
|
|
|
ASSERT_TRUE(cause_close_s != nullptr);
|
|
|
|
cause_close_s->peer = s;
|
|
|
|
s->peer = cause_close_s;
|
|
|
|
cause_close_s->ready(cause_close_s);
|
|
|
|
|
|
|
|
fdevent_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can close local socket in the following situation:
|
|
|
|
// The socket is closing but having some packets, so it is not closed. Then
|
|
|
|
// some write error happens in the socket's file handler, e.g., the file
|
|
|
|
// handler is closed.
|
2015-09-30 03:25:33 +08:00
|
|
|
TEST_F(LocalSocketTest, close_socket_with_packet) {
|
2015-09-16 07:27:09 +08:00
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
|
|
|
arg.socket_fd = socket_fd[1];
|
|
|
|
arg.cause_close_fd = cause_close_fd[1];
|
2016-02-11 06:49:00 +08:00
|
|
|
|
|
|
|
PrepareThread();
|
|
|
|
adb_thread_t thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
|
|
|
|
&arg, &thread));
|
2015-09-16 07:27:09 +08:00
|
|
|
// Wait until the fdevent_loop() starts.
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(100);
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(100);
|
|
|
|
EXPECT_EQ(2u, fdevent_installed_count());
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
TerminateThread(thread);
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can read packets from a closing local socket.
|
2015-09-30 03:25:33 +08:00
|
|
|
TEST_F(LocalSocketTest, read_from_closing_socket) {
|
2015-09-16 07:27:09 +08:00
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
|
|
|
arg.socket_fd = socket_fd[1];
|
|
|
|
arg.cause_close_fd = cause_close_fd[1];
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
PrepareThread();
|
|
|
|
adb_thread_t thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
|
|
|
|
&arg, &thread));
|
2015-09-16 07:27:09 +08:00
|
|
|
// Wait until the fdevent_loop() starts.
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(100);
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(100);
|
|
|
|
EXPECT_EQ(2u, fdevent_installed_count());
|
2015-09-16 07:27:09 +08:00
|
|
|
|
|
|
|
// Verify if we can read successfully.
|
|
|
|
std::vector<char> buf(arg.bytes_written);
|
2016-02-11 06:49:00 +08:00
|
|
|
ASSERT_NE(0u, arg.bytes_written);
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
|
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
TerminateThread(thread);
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can close local socket in the following situation:
|
|
|
|
// The socket is not closed and has some packets. When it fails to write to
|
|
|
|
// the socket's file handler because the other end is closed, we check if the
|
|
|
|
// socket is closed.
|
|
|
|
TEST_F(LocalSocketTest, write_error_when_having_packets) {
|
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
|
|
|
arg.socket_fd = socket_fd[1];
|
|
|
|
arg.cause_close_fd = cause_close_fd[1];
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
PrepareThread();
|
|
|
|
adb_thread_t thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
|
|
|
|
&arg, &thread));
|
|
|
|
|
2015-09-16 07:27:09 +08:00
|
|
|
// Wait until the fdevent_loop() starts.
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(100);
|
|
|
|
EXPECT_EQ(3u, fdevent_installed_count());
|
2015-09-16 07:27:09 +08:00
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
TerminateThread(thread);
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:25:33 +08:00
|
|
|
#if defined(__linux__)
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2015-09-30 03:25:33 +08:00
|
|
|
static void ClientThreadFunc() {
|
|
|
|
std::string error;
|
|
|
|
int fd = network_loopback_client(5038, SOCK_STREAM, &error);
|
|
|
|
ASSERT_GE(fd, 0) << error;
|
2016-02-11 06:49:00 +08:00
|
|
|
adb_sleep_ms(200);
|
2015-09-30 03:25:33 +08:00
|
|
|
ASSERT_EQ(0, adb_close(fd));
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:25:33 +08:00
|
|
|
struct CloseRdHupSocketArg {
|
2016-02-11 06:49:00 +08:00
|
|
|
int socket_fd;
|
2015-09-30 03:25:33 +08:00
|
|
|
};
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2015-09-30 03:25:33 +08:00
|
|
|
static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
|
2016-02-11 06:49:00 +08:00
|
|
|
asocket* s = create_local_socket(arg->socket_fd);
|
|
|
|
ASSERT_TRUE(s != nullptr);
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2016-02-11 06:49:00 +08:00
|
|
|
fdevent_loop();
|
2015-09-30 03:25:33 +08:00
|
|
|
}
|
2015-09-16 07:27:09 +08:00
|
|
|
|
2015-09-30 03:25:33 +08:00
|
|
|
// This test checks if we can close sockets in CLOSE_WAIT state.
|
|
|
|
TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
|
2016-02-11 06:49:00 +08:00
|
|
|
std::string error;
|
|
|
|
int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
|
|
|
|
ASSERT_GE(listen_fd, 0);
|
|
|
|
|
|
|
|
adb_thread_t client_thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(ClientThreadFunc), nullptr,
|
|
|
|
&client_thread));
|
|
|
|
|
|
|
|
struct sockaddr addr;
|
|
|
|
socklen_t alen;
|
|
|
|
alen = sizeof(addr);
|
|
|
|
int accept_fd = adb_socket_accept(listen_fd, &addr, &alen);
|
|
|
|
ASSERT_GE(accept_fd, 0);
|
|
|
|
CloseRdHupSocketArg arg;
|
|
|
|
arg.socket_fd = accept_fd;
|
|
|
|
|
|
|
|
PrepareThread();
|
|
|
|
adb_thread_t thread;
|
|
|
|
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseRdHupSocketThreadFunc),
|
|
|
|
&arg, &thread));
|
|
|
|
|
|
|
|
// Wait until the fdevent_loop() starts.
|
|
|
|
adb_sleep_ms(100);
|
|
|
|
EXPECT_EQ(2u, fdevent_installed_count());
|
|
|
|
|
|
|
|
// Wait until the client closes its socket.
|
|
|
|
ASSERT_TRUE(adb_thread_join(client_thread));
|
|
|
|
|
|
|
|
TerminateThread(thread);
|
2015-09-16 07:27:09 +08:00
|
|
|
}
|
2015-09-30 03:25:33 +08:00
|
|
|
|
|
|
|
#endif // defined(__linux__)
|