1import gdbremote_testcase
2import lldbgdbserverutils
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbgdbserverutils import *
6
7import xml.etree.ElementTree as ET
8
9
10@skipIfWindows
11class PtyServerTestCase(gdbremote_testcase.GdbRemoteTestCaseBase):
12
13    def setUp(self):
14        super().setUp()
15        import pty
16        import tty
17        primary, secondary = pty.openpty()
18        tty.setraw(primary)
19        self._primary = io.FileIO(primary, 'r+b')
20        self._secondary = io.FileIO(secondary, 'r+b')
21
22    def get_debug_monitor_command_line_args(self, attach_pid=None):
23        commandline_args = self.debug_monitor_extra_args
24        if attach_pid:
25            commandline_args += ["--attach=%d" % attach_pid]
26
27        libc = ctypes.CDLL(None)
28        libc.ptsname.argtypes = (ctypes.c_int,)
29        libc.ptsname.restype = ctypes.c_char_p
30        pty_path = libc.ptsname(self._primary.fileno()).decode()
31        commandline_args += ["serial://%s" % (pty_path,)]
32        return commandline_args
33
34    def connect_to_debug_monitor(self, attach_pid=None):
35        self.reverse_connect = False
36        server = self.launch_debug_monitor(attach_pid=attach_pid)
37        self.assertIsNotNone(server)
38
39        # TODO: make it into proper abstraction
40        class FakeSocket:
41            def __init__(self, fd):
42                self.fd = fd
43
44            def sendall(self, frame):
45                self.fd.write(frame)
46
47            def recv(self, count):
48                return self.fd.read(count)
49
50        self.sock = FakeSocket(self._primary)
51        self._server = Server(self.sock, server)
52        return server
53
54    @add_test_categories(["llgs"])
55    def test_pty_server(self):
56        self.build()
57        self.set_inferior_startup_launch()
58        self.prep_debug_monitor_and_inferior()
59
60        # target.xml transfer should trigger a large enough packet to check
61        # for partial write regression
62        self.test_sequence.add_log_lines([
63            "read packet: $qXfer:features:read:target.xml:0,200000#00",
64            {
65                "direction": "send",
66                "regex": re.compile("^\$l(.+)#[0-9a-fA-F]{2}$"),
67                "capture": {1: "target_xml"},
68            }],
69            True)
70        context = self.expect_gdbremote_sequence()
71        # verify that we have received a complete, non-malformed XML
72        self.assertIsNotNone(ET.fromstring(context.get("target_xml")))
73