2015-01-16 08:47:08 +08:00
|
|
|
//===-- SocketTest.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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"
|
2015-01-16 08:47:08 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
class SocketTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
void SetUp() override {
|
2019-04-10 12:57:18 +08:00
|
|
|
ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-01-16 08:47:08 +08:00
|
|
|
|
2019-04-10 12:57:18 +08:00
|
|
|
void TearDown() override { Socket::Terminate(); }
|
2016-09-07 04:57:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SocketTest, DecodeHostAndPort) {
|
|
|
|
std::string host_str;
|
|
|
|
std::string port_str;
|
|
|
|
int32_t port;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-09-07 04:57:50 +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);
|
|
|
|
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());
|
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
|
|
|
}
|
|
|
|
|
2015-10-16 07:54:09 +08:00
|
|
|
#ifndef LLDB_DISABLE_POSIX
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(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");
|
2016-09-07 04:57:50 +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
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(SocketTest, TCPListen0ConnectAccept) {
|
|
|
|
std::unique_ptr<TCPSocket> socket_a_up;
|
|
|
|
std::unique_ptr<TCPSocket> socket_b_up;
|
2019-05-29 07:26:32 +08:00
|
|
|
CreateTCPConnectedSockets("127.0.0.1", &socket_a_up, &socket_b_up);
|
2015-10-16 07:54:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(SocketTest, TCPGetAddress) {
|
|
|
|
std::unique_ptr<TCPSocket> socket_a_up;
|
|
|
|
std::unique_ptr<TCPSocket> socket_b_up;
|
2019-05-29 07:26:32 +08:00
|
|
|
if (!IsAddressFamilySupported("127.0.0.1")) {
|
|
|
|
GTEST_LOG_(WARNING) << "Skipping test due to missing IPv4 support.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CreateTCPConnectedSockets("127.0.0.1", &socket_a_up, &socket_b_up);
|
2016-09-07 04:57:50 +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());
|
|
|
|
EXPECT_STREQ("127.0.0.1", socket_a_up->GetRemoteIPAddress().c_str());
|
|
|
|
EXPECT_STREQ("127.0.0.1", socket_b_up->GetRemoteIPAddress().c_str());
|
2015-01-16 08:47:08 +08:00
|
|
|
}
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(SocketTest, UDPConnect) {
|
2017-04-06 11:16:44 +08:00
|
|
|
Socket *socket;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
bool child_processes_inherit = false;
|
|
|
|
auto error = UDPSocket::Connect("127.0.0.1:0", child_processes_inherit,
|
2017-04-06 11:16:44 +08:00
|
|
|
socket);
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2017-04-06 11:16:44 +08:00
|
|
|
std::unique_ptr<Socket> socket_up(socket);
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
EXPECT_TRUE(error.Success());
|
2017-04-06 11:16:44 +08:00
|
|
|
EXPECT_TRUE(socket_up->IsValid());
|
2015-10-16 07:54:09 +08:00
|
|
|
}
|
2017-08-30 00:13:41 +08:00
|
|
|
|
|
|
|
TEST_F(SocketTest, TCPListen0GetPort) {
|
|
|
|
Socket *server_socket;
|
|
|
|
Predicate<uint16_t> port_predicate;
|
|
|
|
port_predicate.SetValue(0, eBroadcastNever);
|
|
|
|
Status err =
|
|
|
|
Socket::TcpListen("10.10.12.3:0", false, server_socket, &port_predicate);
|
|
|
|
std::unique_ptr<TCPSocket> socket_up((TCPSocket*)server_socket);
|
|
|
|
EXPECT_TRUE(socket_up->IsValid());
|
|
|
|
EXPECT_NE(socket_up->GetLocalPortNumber(), 0);
|
|
|
|
}
|