664b6001d8daebfbbbb349dc91956de53b45d990
[openbsd] /
1 from __future__ import print_function
2
3 import gdbremote_testcase
4 import lldbgdbserverutils
5 import re
6 import select
7 import socket
8 from lldbsuite.test.decorators import *
9 from lldbsuite.test.lldbtest import *
10 from lldbsuite.test import lldbutil
11
12
13 class TestStubReverseConnect(gdbremote_testcase.GdbRemoteTestCaseBase):
14
15     mydir = TestBase.compute_mydir(__file__)
16
17     _DEFAULT_TIMEOUT = 20 * (10 if ('ASAN_OPTIONS' in os.environ) else 1)
18
19     def setUp(self):
20         # Set up the test.
21         gdbremote_testcase.GdbRemoteTestCaseBase.setUp(self)
22
23         # Create a listener on a local port.
24         self.listener_socket = self.create_listener_socket()
25         self.assertIsNotNone(self.listener_socket)
26         self.listener_port = self.listener_socket.getsockname()[1]
27
28     def create_listener_socket(self, timeout_seconds=_DEFAULT_TIMEOUT):
29         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30         self.assertIsNotNone(sock)
31
32         sock.settimeout(timeout_seconds)
33         sock.bind(("127.0.0.1", 0))
34         sock.listen(1)
35
36         def tear_down_listener():
37             try:
38                 sock.shutdown(socket.SHUT_RDWR)
39             except:
40                 # ignore
41                 None
42
43         self.addTearDownHook(tear_down_listener)
44         return sock
45
46     def reverse_connect_works(self):
47         # Indicate stub startup should do a reverse connect.
48         appended_stub_args = ["--reverse-connect"]
49         if self.debug_monitor_extra_args:
50             self.debug_monitor_extra_args += appended_stub_args
51         else:
52             self.debug_monitor_extra_args = appended_stub_args
53
54         self.stub_hostname = "127.0.0.1"
55         self.port = self.listener_port
56
57         triple = self.dbg.GetSelectedPlatform().GetTriple()
58         if re.match(".*-.*-.*-android", triple):
59             self.forward_adb_port(
60                 self.port,
61                 self.port,
62                 "reverse",
63                 self.stub_device)
64
65         # Start the stub.
66         server = self.launch_debug_monitor(logfile=sys.stdout)
67         self.assertIsNotNone(server)
68         self.assertTrue(
69             lldbgdbserverutils.process_is_running(
70                 server.pid, True))
71
72         # Listen for the stub's connection to us.
73         (stub_socket, address) = self.listener_socket.accept()
74         self.assertIsNotNone(stub_socket)
75         self.assertIsNotNone(address)
76         print("connected to stub {} on {}".format(
77             address, stub_socket.getsockname()))
78
79         # Verify we can do the handshake.  If that works, we'll call it good.
80         self.do_handshake(stub_socket, timeout_seconds=self._DEFAULT_TIMEOUT)
81
82         # Clean up.
83         stub_socket.shutdown(socket.SHUT_RDWR)
84
85     @debugserver_test
86     @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
87     def test_reverse_connect_works_debugserver(self):
88         self.init_debugserver_test(use_named_pipe=False)
89         self.set_inferior_startup_launch()
90         self.reverse_connect_works()
91
92     @llgs_test
93     @skipIfRemote  # reverse connect is not a supported use case for now
94     def test_reverse_connect_works_llgs(self):
95         self.init_llgs_test(use_named_pipe=False)
96         self.set_inferior_startup_launch()
97         self.reverse_connect_works()