llvm-project/lldb/unittests/Host/SocketTest.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

240 lines
8.0 KiB
C++
Raw Normal View History

//===-- SocketTest.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "SocketTestUtilities.h"
[lldb] Add a SubsystemRAII that takes care of calling Initialize and Terminate in the unit tests Summary: Many of our tests need to initialize certain subsystems/plugins of LLDB such as `FileSystem` or `HostInfo` by calling their static `Initialize` functions before the test starts and then calling `::Terminate` after the test is done (in reverse order). This adds a lot of error-prone boilerplate code to our testing code. This patch adds a RAII called SubsystemRAII that ensures that we always call ::Initialize and then call ::Terminate after the test is done (and that the Terminate calls are always in the reverse order of the ::Initialize calls). It also gets rid of all of the boilerplate that we had for these calls. Per-fixture initialization is still not very nice with this approach as it would require some kind of static unique_ptr that gets manually assigned/reseted from the gtest SetUpTestCase/TearDownTestCase functions. Because of that I changed all per-fixture setup to now do per-test setup which can be done by just having the SubsystemRAII as a member of the test fixture. This change doesn't influence our normal test runtime as LIT anyway runs each test case separately (and the Initialize/Terminate calls are anyway not very expensive). It will however make running all tests in a single executable slightly slower. Reviewers: labath, JDevlieghere, martong, espindola, shafik Reviewed By: labath Subscribers: mgorny, rnkovacs, emaste, MaskRay, abidh, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D71630
2019-12-23 17:38:12 +08:00
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/Config.h"
#include "lldb/Utility/UriParser.h"
#include "gtest/gtest.h"
using namespace lldb_private;
struct SocketTestParams {
bool is_ipv6;
std::string localhost_ip;
};
class SocketTest : public testing::TestWithParam<SocketTestParams> {
public:
[lldb] Add a SubsystemRAII that takes care of calling Initialize and Terminate in the unit tests Summary: Many of our tests need to initialize certain subsystems/plugins of LLDB such as `FileSystem` or `HostInfo` by calling their static `Initialize` functions before the test starts and then calling `::Terminate` after the test is done (in reverse order). This adds a lot of error-prone boilerplate code to our testing code. This patch adds a RAII called SubsystemRAII that ensures that we always call ::Initialize and then call ::Terminate after the test is done (and that the Terminate calls are always in the reverse order of the ::Initialize calls). It also gets rid of all of the boilerplate that we had for these calls. Per-fixture initialization is still not very nice with this approach as it would require some kind of static unique_ptr that gets manually assigned/reseted from the gtest SetUpTestCase/TearDownTestCase functions. Because of that I changed all per-fixture setup to now do per-test setup which can be done by just having the SubsystemRAII as a member of the test fixture. This change doesn't influence our normal test runtime as LIT anyway runs each test case separately (and the Initialize/Terminate calls are anyway not very expensive). It will however make running all tests in a single executable slightly slower. Reviewers: labath, JDevlieghere, martong, espindola, shafik Reviewed By: labath Subscribers: mgorny, rnkovacs, emaste, MaskRay, abidh, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D71630
2019-12-23 17:38:12 +08:00
SubsystemRAII<Socket> subsystems;
protected:
bool HostSupportsProtocol() const {
if (GetParam().is_ipv6)
return HostSupportsIPv6();
return HostSupportsIPv4();
}
};
TEST_P(SocketTest, DecodeHostAndPort) {
std::string host_str;
std::string port_str;
int32_t port;
Status error;
EXPECT_TRUE(Socket::DecodeHostAndPort("localhost:1138", host_str, port_str,
port, &error));
EXPECT_STREQ("localhost", host_str.c_str());
EXPECT_STREQ("1138", port_str.c_str());
EXPECT_EQ(1138, port);
EXPECT_TRUE(error.Success());
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:65536", host_str, port_str,
port, &error));
EXPECT_TRUE(error.Fail());
EXPECT_STREQ("invalid host:port specification: 'google.com:65536'",
error.AsCString());
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:-1138", host_str, port_str,
port, &error));
EXPECT_TRUE(error.Fail());
EXPECT_STREQ("invalid host:port specification: 'google.com:-1138'",
error.AsCString());
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:65536", host_str, port_str,
port, &error));
EXPECT_TRUE(error.Fail());
EXPECT_STREQ("invalid host:port specification: 'google.com:65536'",
error.AsCString());
EXPECT_TRUE(
Socket::DecodeHostAndPort("12345", host_str, port_str, port, &error));
EXPECT_STREQ("", host_str.c_str());
EXPECT_STREQ("12345", port_str.c_str());
EXPECT_EQ(12345, port);
EXPECT_TRUE(error.Success());
EXPECT_TRUE(
Socket::DecodeHostAndPort("*:0", host_str, port_str, port, &error));
EXPECT_STREQ("*", host_str.c_str());
EXPECT_STREQ("0", port_str.c_str());
EXPECT_EQ(0, port);
EXPECT_TRUE(error.Success());
EXPECT_TRUE(
Socket::DecodeHostAndPort("*:65535", host_str, port_str, port, &error));
EXPECT_STREQ("*", host_str.c_str());
EXPECT_STREQ("65535", port_str.c_str());
EXPECT_EQ(65535, port);
EXPECT_TRUE(error.Success());
Re-landing IPv6 support for LLDB Host This support was landed in r300579, and reverted in r300669 due to failures on the bots. The failures were caused by sockets not being properly closed, and this updated version of the patches should resolve that. Summary from the original change: This patch adds IPv6 support to LLDB/Host's TCP socket implementation. Supporting IPv6 involved a few significant changes to the implementation of the socket layers, and I have performed some significant code cleanup along the way. This patch changes the Socket constructors for all types of sockets to not create sockets until first use. This is required for IPv6 support because the socket type will vary based on the address you are connecting to. This also has the benefit of removing code that could have errors from the Socket subclass constructors (which seems like a win to me). The patch also slightly changes the API and behaviors of the Listen/Accept pattern. Previously both Listen and Accept calls took an address specified as a string. Now only listen does. This change was made because the Listen call can result in opening more than one socket. In order to support listening for both IPv4 and IPv6 connections we need to open one AF_INET socket and one AF_INET6 socket. During the listen call we construct a map of file descriptors to addrin structures which represent the allowable incoming connection address. This map removes the need for taking an address into the Accept call. This does have a change in functionality. Previously you could Listen for connections based on one address, and Accept connections from a different address. This is no longer supported. I could not find anywhere in LLDB where we actually used the APIs in that way. The new API does still support AnyAddr for allowing incoming connections from any address. The Listen implementation is implemented using kqueue on FreeBSD and Darwin, WSAPoll on Windows and poll(2) everywhere else. https://reviews.llvm.org/D31823 llvm-svn: 301492
2017-04-27 07:17:20 +08:00
EXPECT_TRUE(
Socket::DecodeHostAndPort("[::1]:12345", host_str, port_str, port, &error));
EXPECT_STREQ("::1", host_str.c_str());
EXPECT_STREQ("12345", port_str.c_str());
EXPECT_EQ(12345, port);
EXPECT_TRUE(error.Success());
EXPECT_TRUE(
Socket::DecodeHostAndPort("[abcd:12fg:AF58::1]:12345", host_str, port_str, port, &error));
EXPECT_STREQ("abcd:12fg:AF58::1", host_str.c_str());
EXPECT_STREQ("12345", port_str.c_str());
EXPECT_EQ(12345, port);
EXPECT_TRUE(error.Success());
}
#if LLDB_ENABLE_POSIX
TEST_P(SocketTest, DomainListenConnectAccept) {
llvm::SmallString<64> Path;
std::error_code EC = llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
ASSERT_FALSE(EC);
llvm::sys::path::append(Path, "test");
// Skip the test if the $TMPDIR is too long to hold a domain socket.
if (Path.size() > 107u)
return;
std::unique_ptr<DomainSocket> socket_a_up;
std::unique_ptr<DomainSocket> socket_b_up;
CreateDomainConnectedSockets(Path, &socket_a_up, &socket_b_up);
}
#endif
TEST_P(SocketTest, TCPListen0ConnectAccept) {
if (!HostSupportsProtocol())
return;
std::unique_ptr<TCPSocket> socket_a_up;
std::unique_ptr<TCPSocket> socket_b_up;
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
&socket_b_up);
}
TEST_P(SocketTest, TCPGetAddress) {
std::unique_ptr<TCPSocket> socket_a_up;
std::unique_ptr<TCPSocket> socket_b_up;
if (!HostSupportsProtocol())
return;
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
&socket_b_up);
EXPECT_EQ(socket_a_up->GetLocalPortNumber(),
socket_b_up->GetRemotePortNumber());
EXPECT_EQ(socket_b_up->GetLocalPortNumber(),
socket_a_up->GetRemotePortNumber());
EXPECT_NE(socket_a_up->GetLocalPortNumber(),
socket_b_up->GetLocalPortNumber());
EXPECT_STREQ(GetParam().localhost_ip.c_str(),
socket_a_up->GetRemoteIPAddress().c_str());
EXPECT_STREQ(GetParam().localhost_ip.c_str(),
socket_b_up->GetRemoteIPAddress().c_str());
}
TEST_P(SocketTest, UDPConnect) {
// UDPSocket::Connect() creates sockets with AF_INET (IPv4).
if (!HostSupportsIPv4())
return;
llvm::Expected<std::unique_ptr<UDPSocket>> socket =
UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
ASSERT_THAT_EXPECTED(socket, llvm::Succeeded());
EXPECT_TRUE(socket.get()->IsValid());
}
TEST_P(SocketTest, TCPListen0GetPort) {
if (!HostSupportsIPv4())
return;
Predicate<uint16_t> port_predicate;
port_predicate.SetValue(0, eBroadcastNever);
llvm::Expected<std::unique_ptr<TCPSocket>> sock =
Socket::TcpListen("10.10.12.3:0", false, &port_predicate);
ASSERT_THAT_EXPECTED(sock, llvm::Succeeded());
ASSERT_TRUE(sock.get()->IsValid());
EXPECT_NE(sock.get()->GetLocalPortNumber(), 0);
}
TEST_P(SocketTest, TCPGetConnectURI) {
std::unique_ptr<TCPSocket> socket_a_up;
std::unique_ptr<TCPSocket> socket_b_up;
if (!HostSupportsProtocol())
return;
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
&socket_b_up);
llvm::StringRef scheme;
llvm::StringRef hostname;
int port;
llvm::StringRef path;
std::string uri(socket_a_up->GetRemoteConnectionURI());
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
EXPECT_EQ(scheme, "connect");
EXPECT_EQ(port, socket_a_up->GetRemotePortNumber());
}
TEST_P(SocketTest, UDPGetConnectURI) {
// UDPSocket::Connect() creates sockets with AF_INET (IPv4).
if (!HostSupportsIPv4())
return;
llvm::Expected<std::unique_ptr<UDPSocket>> socket =
UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
ASSERT_THAT_EXPECTED(socket, llvm::Succeeded());
llvm::StringRef scheme;
llvm::StringRef hostname;
int port;
llvm::StringRef path;
std::string uri = socket.get()->GetRemoteConnectionURI();
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
EXPECT_EQ(scheme, "udp");
}
#if LLDB_ENABLE_POSIX
TEST_P(SocketTest, DomainGetConnectURI) {
llvm::SmallString<64> domain_path;
std::error_code EC =
llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", domain_path);
ASSERT_FALSE(EC);
llvm::sys::path::append(domain_path, "test");
// Skip the test if the $TMPDIR is too long to hold a domain socket.
if (domain_path.size() > 107u)
return;
std::unique_ptr<DomainSocket> socket_a_up;
std::unique_ptr<DomainSocket> socket_b_up;
CreateDomainConnectedSockets(domain_path, &socket_a_up, &socket_b_up);
llvm::StringRef scheme;
llvm::StringRef hostname;
int port;
llvm::StringRef path;
std::string uri(socket_a_up->GetRemoteConnectionURI());
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
EXPECT_EQ(scheme, "unix-connect");
EXPECT_EQ(path, domain_path);
}
#endif
2021-05-15 01:15:20 +08:00
INSTANTIATE_TEST_SUITE_P(
SocketTests, SocketTest,
testing::Values(SocketTestParams{/*is_ipv6=*/false,
/*localhost_ip=*/"127.0.0.1"},
SocketTestParams{/*is_ipv6=*/true, /*localhost_ip=*/"::1"}),
// Prints "SocketTests/SocketTest.DecodeHostAndPort/ipv4" etc. in test logs.
[](const testing::TestParamInfo<SocketTestParams> &info) {
return info.param.is_ipv6 ? "ipv6" : "ipv4";
});