xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp (revision 56d7262b698024ba95afe5df5355e7cc23933049)
1 //===-- GDBRemoteTestUtils.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
11 // Workaround for MSVC standard library bug, which fails to include <thread> when
12 // exceptions are disabled.
13 #include <eh.h>
14 #endif
15 
16 #include "GDBRemoteTestUtils.h"
17 #include "gtest/gtest.h"
18 
19 #include "lldb/Host/common/TCPSocket.h"
20 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
21 
22 #include <future>
23 
24 namespace lldb_private
25 {
26 namespace process_gdb_remote
27 {
28 
29 void
30 GDBRemoteTest::SetUpTestCase()
31 {
32 #if defined(_MSC_VER)
33     WSADATA data;
34     ::WSAStartup(MAKEWORD(2, 2), &data);
35 #endif
36 }
37 
38 void
39 GDBRemoteTest::TearDownTestCase()
40 {
41 #if defined(_MSC_VER)
42     ::WSACleanup();
43 #endif
44 }
45 
46 void
47 Connect(GDBRemoteCommunication &client, GDBRemoteCommunication &server)
48 {
49     bool child_processes_inherit = false;
50     Error error;
51     TCPSocket listen_socket(child_processes_inherit, error);
52     ASSERT_FALSE(error.Fail());
53     error = listen_socket.Listen("127.0.0.1:0", 5);
54     ASSERT_FALSE(error.Fail());
55 
56     Socket *accept_socket;
57     std::future<Error> accept_error = std::async(std::launch::async, [&] {
58         return listen_socket.Accept("127.0.0.1:0", child_processes_inherit, accept_socket);
59     });
60 
61     char connect_remote_address[64];
62     snprintf(connect_remote_address, sizeof(connect_remote_address), "connect://localhost:%u",
63              listen_socket.GetLocalPortNumber());
64 
65     std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
66     ASSERT_EQ(conn_ap->Connect(connect_remote_address, nullptr), lldb::eConnectionStatusSuccess);
67 
68     client.SetConnection(conn_ap.release());
69     ASSERT_TRUE(accept_error.get().Success());
70     server.SetConnection(new ConnectionFileDescriptor(accept_socket));
71 }
72 
73 } // namespace process_gdb_remote
74 } // namespace lldb_private
75