xref: /llvm-project/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp (revision 9aa6a479738c7bf21128f9c45ea4ffcf82d80cbf)
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     std::string uri(connection_file_descriptor.GetURI());
30     EXPECT_EQ((URI{"connect", ip, socket->GetRemotePortNumber(), "/"}),
31               URI::Parse(uri).getValue());
32   }
33 
34   void TestConnect(std::string ip, std::string path) {
35     std::unique_ptr<TCPSocket> socket_a_up;
36     std::unique_ptr<TCPSocket> socket_b_up;
37     CreateTCPConnectedSockets(ip, &socket_a_up, &socket_b_up);
38     auto *socket = socket_a_up.release();
39     ConnectionFileDescriptor connection_file_descriptor(socket);
40     connection_file_descriptor.Connect(path, nullptr);
41   }
42 };
43 
44 TEST_F(ConnectionFileDescriptorTest, TCPGetURIv4) {
45   if (!HostSupportsIPv4())
46     return;
47   TestGetURI("127.0.0.1");
48 }
49 
50 TEST_F(ConnectionFileDescriptorTest, TCPGetURIv6) {
51   if (!HostSupportsIPv6())
52     return;
53   TestGetURI("::1");
54 }
55 
56 TEST_F(ConnectionFileDescriptorTest, Connectv4) {
57   if (!HostSupportsIPv4())
58     return;
59   TestConnect("127.0.0.1", "accept://127.0.0.1");
60 }
61 
62 TEST_F(ConnectionFileDescriptorTest, Connectv6) {
63   if (!HostSupportsIPv6())
64     return;
65   TestConnect("::1", "accept://::1");
66 }
67