xref: /llvm-project/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp (revision ff569ed03092dba39effcc45e81d64beff800bb5)
1 //===-- ConnectionFileDescriptorTest.cpp ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SocketTestUtilities.h"
10 #include "gtest/gtest.h"
11 
12 #include "TestingSupport/SubsystemRAII.h"
13 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
14 #include "lldb/Utility/UriParser.h"
15 
16 using namespace lldb_private;
17 
18 class ConnectionFileDescriptorTest : public testing::Test {
19 public:
20   SubsystemRAII<Socket> subsystems;
21 
22   void TestGetURI(std::string ip) {
23     std::unique_ptr<TCPSocket> socket_a_up;
24     std::unique_ptr<TCPSocket> socket_b_up;
25     CreateTCPConnectedSockets(ip, &socket_a_up, &socket_b_up);
26     auto socket = socket_a_up.release();
27     ConnectionFileDescriptor connection_file_descriptor(socket);
28 
29     llvm::StringRef scheme;
30     llvm::StringRef hostname;
31     llvm::Optional<uint16_t> port;
32     llvm::StringRef path;
33     std::string uri(connection_file_descriptor.GetURI());
34     EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
35     EXPECT_EQ(ip, hostname);
36     EXPECT_EQ(socket->GetRemotePortNumber(), port.getValue());
37   }
38 };
39 
40 TEST_F(ConnectionFileDescriptorTest, TCPGetURIv4) {
41   if (!HostSupportsIPv4())
42     return;
43   TestGetURI("127.0.0.1");
44 }
45 
46 TEST_F(ConnectionFileDescriptorTest, TCPGetURIv6) {
47   if (!HostSupportsIPv6())
48     return;
49   TestGetURI("::1");
50 }
51