1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4from gdbclientutils import *
5
6
7@skipIfWindows
8class TestPty(GDBRemoteTestBase):
9    mydir = TestBase.compute_mydir(__file__)
10    server_socket_class = PtyServerSocket
11
12    def get_term_attrs(self):
13        import termios
14        return termios.tcgetattr(self.server._socket._slave)
15
16    def setUp(self):
17        super().setUp()
18        self.orig_attr = self.get_term_attrs()
19
20    def assert_raw_mode(self, current_attr):
21        import termios
22        self.assertEqual(current_attr[0] & (termios.BRKINT |
23                                            termios.PARMRK |
24                                            termios.ISTRIP | termios.INLCR |
25                                            termios.IGNCR | termios.ICRNL |
26                                            termios.IXON),
27                         0)
28        self.assertEqual(current_attr[1] & termios.OPOST, 0)
29        self.assertEqual(current_attr[2] & termios.CSIZE, termios.CS8)
30        self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
31                                            termios.ISIG | termios.IEXTEN),
32                         0)
33        self.assertEqual(current_attr[6][termios.VMIN], 1)
34        self.assertEqual(current_attr[6][termios.VTIME], 0)
35
36    def get_parity_flags(self, attr):
37        import termios
38        return attr[2] & (termios.PARENB | termios.PARODD)
39
40    def get_stop_bit_flags(self, attr):
41        import termios
42        return attr[2] & termios.CSTOPB
43
44    def test_process_connect_sync(self):
45        """Test the process connect command in synchronous mode"""
46        try:
47            self.dbg.SetAsync(False)
48            self.expect("platform select remote-gdb-server",
49                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
50            self.expect("process connect " + self.server.get_connect_url(),
51                        substrs=['Process', 'stopped'])
52
53            current_attr = self.get_term_attrs()
54            # serial:// should set raw mode
55            self.assert_raw_mode(current_attr)
56            # other parameters should be unmodified
57            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
58            self.assertEqual(self.get_parity_flags(current_attr),
59                             self.get_parity_flags(self.orig_attr))
60            self.assertEqual(self.get_stop_bit_flags(current_attr),
61                             self.get_stop_bit_flags(self.orig_attr))
62        finally:
63            self.dbg.GetSelectedTarget().GetProcess().Kill()
64        # original mode should be restored on exit
65        self.assertEqual(self.get_term_attrs(), self.orig_attr)
66
67    def test_process_connect_async(self):
68        """Test the process connect command in asynchronous mode"""
69        try:
70            self.dbg.SetAsync(True)
71            self.expect("platform select remote-gdb-server",
72                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
73            self.expect("process connect " + self.server.get_connect_url(),
74                        matching=False,
75                        substrs=['Process', 'stopped'])
76            lldbutil.expect_state_changes(self, self.dbg.GetListener(),
77                                          self.process(), [lldb.eStateStopped])
78
79            current_attr = self.get_term_attrs()
80            # serial:// should set raw mode
81            self.assert_raw_mode(current_attr)
82            # other parameters should be unmodified
83            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
84            self.assertEqual(self.get_parity_flags(current_attr),
85                             self.get_parity_flags(self.orig_attr))
86            self.assertEqual(self.get_stop_bit_flags(current_attr),
87                             self.get_stop_bit_flags(self.orig_attr))
88        finally:
89            self.dbg.GetSelectedTarget().GetProcess().Kill()
90        lldbutil.expect_state_changes(self, self.dbg.GetListener(),
91                                      self.process(), [lldb.eStateExited])
92        # original mode should be restored on exit
93        self.assertEqual(self.get_term_attrs(), self.orig_attr)
94
95    def test_connect_via_file(self):
96        """Test connecting via the legacy file:// URL"""
97        import termios
98        try:
99            self.expect("platform select remote-gdb-server",
100                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
101            self.expect("process connect file://" +
102                        self.server.get_connect_address(),
103                        substrs=['Process', 'stopped'])
104
105            # file:// sets baud rate and some raw-related flags
106            current_attr = self.get_term_attrs()
107            self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
108                                                termios.ECHOE | termios.ISIG),
109                             0)
110            self.assertEqual(current_attr[4], termios.B115200)
111            self.assertEqual(current_attr[5], termios.B115200)
112            self.assertEqual(current_attr[6][termios.VMIN], 1)
113            self.assertEqual(current_attr[6][termios.VTIME], 0)
114        finally:
115            self.dbg.GetSelectedTarget().GetProcess().Kill()
116
117    def test_process_connect_params(self):
118        """Test serial:// URL with parameters"""
119        import termios
120        try:
121            self.expect("platform select remote-gdb-server",
122                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
123            self.expect("process connect " + self.server.get_connect_url() +
124                        "?baud=115200&stop-bits=2",
125                        substrs=['Process', 'stopped'])
126
127            current_attr = self.get_term_attrs()
128            self.assert_raw_mode(current_attr)
129            self.assertEqual(current_attr[4:6], 2 * [termios.B115200])
130            self.assertEqual(self.get_parity_flags(current_attr),
131                             self.get_parity_flags(self.orig_attr))
132            self.assertEqual(self.get_stop_bit_flags(current_attr),
133                             termios.CSTOPB)
134        finally:
135            self.dbg.GetSelectedTarget().GetProcess().Kill()
136        # original mode should be restored on exit
137        self.assertEqual(self.get_term_attrs(), self.orig_attr)
138