[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- SocketTest.cpp ----------------------------------------------------===//
|
2015-01-16 08:47:08 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-01-16 08:47:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-05-29 07:26:32 +08:00
|
|
|
#include "SocketTestUtilities.h"
|
2019-12-23 17:38:12 +08:00
|
|
|
#include "TestingSupport/SubsystemRAII.h"
|
2019-12-13 02:00:45 +08:00
|
|
|
#include "lldb/Host/Config.h"
|
2019-05-31 07:30:35 +08:00
|
|
|
#include "lldb/Utility/UriParser.h"
|
2015-01-16 08:47:08 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
struct SocketTestParams {
|
|
|
|
bool is_ipv6;
|
|
|
|
std::string localhost_ip;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SocketTest : public testing::TestWithParam<SocketTestParams> {
|
2015-03-14 12:19:32 +08:00
|
|
|
public:
|
2019-12-23 17:38:12 +08:00
|
|
|
SubsystemRAII<Socket> subsystems;
|
2020-10-01 01:47:48 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool HostSupportsProtocol() const {
|
|
|
|
if (GetParam().is_ipv6)
|
|
|
|
return HostSupportsIPv6();
|
|
|
|
return HostSupportsIPv4();
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
};
|
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, DecodeHostAndPort) {
|
2015-01-16 08:47:08 +08:00
|
|
|
std::string host_str;
|
|
|
|
std::string port_str;
|
|
|
|
int32_t port;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2015-01-16 08:47:08 +08:00
|
|
|
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);
|
2016-05-03 22:07:41 +08:00
|
|
|
EXPECT_TRUE(error.Success());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-16 08:47:08 +08:00
|
|
|
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:65536", host_str, port_str,
|
|
|
|
port, &error));
|
|
|
|
EXPECT_TRUE(error.Fail());
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("invalid host:port specification: 'google.com:65536'",
|
|
|
|
error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-16 08:47:08 +08:00
|
|
|
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:-1138", host_str, port_str,
|
|
|
|
port, &error));
|
|
|
|
EXPECT_TRUE(error.Fail());
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("invalid host:port specification: 'google.com:-1138'",
|
|
|
|
error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-16 08:47:08 +08:00
|
|
|
EXPECT_FALSE(Socket::DecodeHostAndPort("google.com:65536", host_str, port_str,
|
|
|
|
port, &error));
|
|
|
|
EXPECT_TRUE(error.Fail());
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("invalid host:port specification: 'google.com:65536'",
|
|
|
|
error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_TRUE(
|
2015-01-16 08:47:08 +08:00
|
|
|
Socket::DecodeHostAndPort("12345", host_str, port_str, port, &error));
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("", host_str.c_str());
|
2015-01-16 08:47:08 +08:00
|
|
|
EXPECT_STREQ("12345", port_str.c_str());
|
|
|
|
EXPECT_EQ(12345, port);
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_TRUE(error.Success());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_TRUE(
|
2015-01-16 08:47:08 +08:00
|
|
|
Socket::DecodeHostAndPort("*:0", host_str, port_str, port, &error));
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("*", host_str.c_str());
|
2015-01-16 08:47:08 +08:00
|
|
|
EXPECT_STREQ("0", port_str.c_str());
|
|
|
|
EXPECT_EQ(0, port);
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_TRUE(error.Success());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_TRUE(
|
2015-01-16 08:47:08 +08:00
|
|
|
Socket::DecodeHostAndPort("*:65535", host_str, port_str, port, &error));
|
2016-02-03 19:12:23 +08:00
|
|
|
EXPECT_STREQ("*", host_str.c_str());
|
|
|
|
EXPECT_STREQ("65535", port_str.c_str());
|
|
|
|
EXPECT_EQ(65535, port);
|
|
|
|
EXPECT_TRUE(error.Success());
|
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());
|
2015-01-16 08:47:08 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 02:00:45 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, DomainListenConnectAccept) {
|
2018-12-18 23:33:50 +08:00
|
|
|
llvm::SmallString<64> Path;
|
|
|
|
std::error_code EC = llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
|
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
llvm::sys::path::append(Path, "test");
|
2019-09-25 03:34:50 +08:00
|
|
|
|
|
|
|
// Skip the test if the $TMPDIR is too long to hold a domain socket.
|
|
|
|
if (Path.size() > 107u)
|
|
|
|
return;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-05-03 22:07:41 +08:00
|
|
|
std::unique_ptr<DomainSocket> socket_a_up;
|
|
|
|
std::unique_ptr<DomainSocket> socket_b_up;
|
2019-05-29 07:26:32 +08:00
|
|
|
CreateDomainConnectedSockets(Path, &socket_a_up, &socket_b_up);
|
2015-01-16 08:47:08 +08:00
|
|
|
}
|
2015-10-16 07:54:09 +08:00
|
|
|
#endif
|
2015-01-16 08:47:08 +08:00
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, TCPListen0ConnectAccept) {
|
|
|
|
if (!HostSupportsProtocol())
|
|
|
|
return;
|
2016-05-03 22:07:41 +08:00
|
|
|
std::unique_ptr<TCPSocket> socket_a_up;
|
|
|
|
std::unique_ptr<TCPSocket> socket_b_up;
|
2020-10-01 01:47:48 +08:00
|
|
|
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
|
|
|
|
&socket_b_up);
|
2015-10-16 07:54:09 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, TCPGetAddress) {
|
2015-10-16 07:54:09 +08:00
|
|
|
std::unique_ptr<TCPSocket> socket_a_up;
|
|
|
|
std::unique_ptr<TCPSocket> socket_b_up;
|
2020-10-01 01:47:48 +08:00
|
|
|
if (!HostSupportsProtocol())
|
2019-05-29 07:26:32 +08:00
|
|
|
return;
|
2020-10-01 01:47:48 +08:00
|
|
|
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
|
|
|
|
&socket_b_up);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-16 08:47:08 +08:00
|
|
|
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());
|
2020-10-01 01:47:48 +08:00
|
|
|
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());
|
2015-01-16 08:47:08 +08:00
|
|
|
}
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, UDPConnect) {
|
|
|
|
// UDPSocket::Connect() creates sockets with AF_INET (IPv4).
|
|
|
|
if (!HostSupportsIPv4())
|
|
|
|
return;
|
2020-04-23 19:54:11 +08:00
|
|
|
llvm::Expected<std::unique_ptr<UDPSocket>> socket =
|
|
|
|
UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-04-23 19:54:11 +08:00
|
|
|
ASSERT_THAT_EXPECTED(socket, llvm::Succeeded());
|
|
|
|
EXPECT_TRUE(socket.get()->IsValid());
|
2015-10-16 07:54:09 +08:00
|
|
|
}
|
2017-08-30 00:13:41 +08:00
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, TCPListen0GetPort) {
|
|
|
|
if (!HostSupportsIPv4())
|
|
|
|
return;
|
2017-08-30 00:13:41 +08:00
|
|
|
Predicate<uint16_t> port_predicate;
|
|
|
|
port_predicate.SetValue(0, eBroadcastNever);
|
2020-04-23 19:54:11 +08:00
|
|
|
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);
|
2017-08-30 00:13:41 +08:00
|
|
|
}
|
2019-05-31 07:30:35 +08:00
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, TCPGetConnectURI) {
|
2019-05-31 07:30:35 +08:00
|
|
|
std::unique_ptr<TCPSocket> socket_a_up;
|
|
|
|
std::unique_ptr<TCPSocket> socket_b_up;
|
2020-10-01 01:47:48 +08:00
|
|
|
if (!HostSupportsProtocol())
|
2019-05-31 07:30:35 +08:00
|
|
|
return;
|
2020-10-01 01:47:48 +08:00
|
|
|
CreateTCPConnectedSockets(GetParam().localhost_ip, &socket_a_up,
|
|
|
|
&socket_b_up);
|
2019-05-31 07:30:35 +08:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, UDPGetConnectURI) {
|
|
|
|
// UDPSocket::Connect() creates sockets with AF_INET (IPv4).
|
2020-04-27 23:15:36 +08:00
|
|
|
if (!HostSupportsIPv4())
|
2019-05-31 07:30:35 +08:00
|
|
|
return;
|
2020-04-23 19:54:11 +08:00
|
|
|
llvm::Expected<std::unique_ptr<UDPSocket>> socket =
|
|
|
|
UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
|
|
|
|
ASSERT_THAT_EXPECTED(socket, llvm::Succeeded());
|
2019-05-31 07:30:35 +08:00
|
|
|
|
|
|
|
llvm::StringRef scheme;
|
|
|
|
llvm::StringRef hostname;
|
|
|
|
int port;
|
|
|
|
llvm::StringRef path;
|
2020-04-23 19:54:11 +08:00
|
|
|
std::string uri = socket.get()->GetRemoteConnectionURI();
|
2019-05-31 07:30:35 +08:00
|
|
|
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
|
|
|
|
EXPECT_EQ(scheme, "udp");
|
|
|
|
}
|
|
|
|
|
2019-12-13 02:00:45 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
2020-10-01 01:47:48 +08:00
|
|
|
TEST_P(SocketTest, DomainGetConnectURI) {
|
2019-05-31 07:30:35 +08:00
|
|
|
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");
|
2019-09-25 03:34:50 +08:00
|
|
|
|
|
|
|
// Skip the test if the $TMPDIR is too long to hold a domain socket.
|
|
|
|
if (domain_path.size() > 107u)
|
|
|
|
return;
|
2019-05-31 07:30:35 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-06-28 00:45:23 +08:00
|
|
|
#endif
|
2020-10-01 01:47:48 +08:00
|
|
|
|
2021-05-15 01:15:20 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2020-10-01 01:47:48 +08:00
|
|
|
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";
|
|
|
|
});
|