18fa2394bSMichał Górnyimport lldb
28fa2394bSMichał Górnyfrom lldbsuite.test.lldbtest import *
38fa2394bSMichał Górnyfrom lldbsuite.test.decorators import *
48fa2394bSMichał Górnyfrom gdbclientutils import *
58fa2394bSMichał Górny
68fa2394bSMichał Górny
78fa2394bSMichał Górny@skipIfWindows
88fa2394bSMichał Górnyclass TestPty(GDBRemoteTestBase):
98fa2394bSMichał Górny    mydir = TestBase.compute_mydir(__file__)
108fa2394bSMichał Górny    server_socket_class = PtyServerSocket
118fa2394bSMichał Górny
124a7b4beaSMichał Górny    def get_term_attrs(self):
134a7b4beaSMichał Górny        import termios
14*52a3ed5bSQuinn Pham        return termios.tcgetattr(self.server._socket._secondary)
154a7b4beaSMichał Górny
164a7b4beaSMichał Górny    def setUp(self):
174a7b4beaSMichał Górny        super().setUp()
184a7b4beaSMichał Górny        self.orig_attr = self.get_term_attrs()
194a7b4beaSMichał Górny
204a7b4beaSMichał Górny    def assert_raw_mode(self, current_attr):
214a7b4beaSMichał Górny        import termios
224a7b4beaSMichał Górny        self.assertEqual(current_attr[0] & (termios.BRKINT |
234a7b4beaSMichał Górny                                            termios.PARMRK |
244a7b4beaSMichał Górny                                            termios.ISTRIP | termios.INLCR |
254a7b4beaSMichał Górny                                            termios.IGNCR | termios.ICRNL |
264a7b4beaSMichał Górny                                            termios.IXON),
274a7b4beaSMichał Górny                         0)
284a7b4beaSMichał Górny        self.assertEqual(current_attr[1] & termios.OPOST, 0)
294a7b4beaSMichał Górny        self.assertEqual(current_attr[2] & termios.CSIZE, termios.CS8)
304a7b4beaSMichał Górny        self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
314a7b4beaSMichał Górny                                            termios.ISIG | termios.IEXTEN),
324a7b4beaSMichał Górny                         0)
334a7b4beaSMichał Górny        self.assertEqual(current_attr[6][termios.VMIN], 1)
344a7b4beaSMichał Górny        self.assertEqual(current_attr[6][termios.VTIME], 0)
354a7b4beaSMichał Górny
364a7b4beaSMichał Górny    def get_parity_flags(self, attr):
374a7b4beaSMichał Górny        import termios
384a7b4beaSMichał Górny        return attr[2] & (termios.PARENB | termios.PARODD)
394a7b4beaSMichał Górny
404a7b4beaSMichał Górny    def get_stop_bit_flags(self, attr):
414a7b4beaSMichał Górny        import termios
424a7b4beaSMichał Górny        return attr[2] & termios.CSTOPB
434a7b4beaSMichał Górny
448fa2394bSMichał Górny    def test_process_connect_sync(self):
458fa2394bSMichał Górny        """Test the process connect command in synchronous mode"""
468fa2394bSMichał Górny        try:
478fa2394bSMichał Górny            self.dbg.SetAsync(False)
488fa2394bSMichał Górny            self.expect("platform select remote-gdb-server",
498fa2394bSMichał Górny                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
508fa2394bSMichał Górny            self.expect("process connect " + self.server.get_connect_url(),
518fa2394bSMichał Górny                        substrs=['Process', 'stopped'])
524a7b4beaSMichał Górny
534a7b4beaSMichał Górny            current_attr = self.get_term_attrs()
544a7b4beaSMichał Górny            # serial:// should set raw mode
554a7b4beaSMichał Górny            self.assert_raw_mode(current_attr)
564a7b4beaSMichał Górny            # other parameters should be unmodified
574a7b4beaSMichał Górny            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
584a7b4beaSMichał Górny            self.assertEqual(self.get_parity_flags(current_attr),
594a7b4beaSMichał Górny                             self.get_parity_flags(self.orig_attr))
604a7b4beaSMichał Górny            self.assertEqual(self.get_stop_bit_flags(current_attr),
614a7b4beaSMichał Górny                             self.get_stop_bit_flags(self.orig_attr))
628fa2394bSMichał Górny        finally:
6345f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
644a7b4beaSMichał Górny        # original mode should be restored on exit
654a7b4beaSMichał Górny        self.assertEqual(self.get_term_attrs(), self.orig_attr)
668fa2394bSMichał Górny
678fa2394bSMichał Górny    def test_process_connect_async(self):
688fa2394bSMichał Górny        """Test the process connect command in asynchronous mode"""
698fa2394bSMichał Górny        try:
708fa2394bSMichał Górny            self.dbg.SetAsync(True)
718fa2394bSMichał Górny            self.expect("platform select remote-gdb-server",
728fa2394bSMichał Górny                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
738fa2394bSMichał Górny            self.expect("process connect " + self.server.get_connect_url(),
748fa2394bSMichał Górny                        matching=False,
758fa2394bSMichał Górny                        substrs=['Process', 'stopped'])
768fa2394bSMichał Górny            lldbutil.expect_state_changes(self, self.dbg.GetListener(),
778fa2394bSMichał Górny                                          self.process(), [lldb.eStateStopped])
784a7b4beaSMichał Górny
794a7b4beaSMichał Górny            current_attr = self.get_term_attrs()
804a7b4beaSMichał Górny            # serial:// should set raw mode
814a7b4beaSMichał Górny            self.assert_raw_mode(current_attr)
824a7b4beaSMichał Górny            # other parameters should be unmodified
834a7b4beaSMichał Górny            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
844a7b4beaSMichał Górny            self.assertEqual(self.get_parity_flags(current_attr),
854a7b4beaSMichał Górny                             self.get_parity_flags(self.orig_attr))
864a7b4beaSMichał Górny            self.assertEqual(self.get_stop_bit_flags(current_attr),
874a7b4beaSMichał Górny                             self.get_stop_bit_flags(self.orig_attr))
888fa2394bSMichał Górny        finally:
8945f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
9045f97950SMichał Górny        lldbutil.expect_state_changes(self, self.dbg.GetListener(),
9145f97950SMichał Górny                                      self.process(), [lldb.eStateExited])
924a7b4beaSMichał Górny        # original mode should be restored on exit
934a7b4beaSMichał Górny        self.assertEqual(self.get_term_attrs(), self.orig_attr)
944a7b4beaSMichał Górny
954a7b4beaSMichał Górny    def test_connect_via_file(self):
964a7b4beaSMichał Górny        """Test connecting via the legacy file:// URL"""
974a7b4beaSMichał Górny        import termios
984a7b4beaSMichał Górny        try:
994a7b4beaSMichał Górny            self.expect("platform select remote-gdb-server",
1004a7b4beaSMichał Górny                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
1014a7b4beaSMichał Górny            self.expect("process connect file://" +
1024a7b4beaSMichał Górny                        self.server.get_connect_address(),
1034a7b4beaSMichał Górny                        substrs=['Process', 'stopped'])
1044a7b4beaSMichał Górny
1054a7b4beaSMichał Górny            # file:// sets baud rate and some raw-related flags
1064a7b4beaSMichał Górny            current_attr = self.get_term_attrs()
1074a7b4beaSMichał Górny            self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
1084a7b4beaSMichał Górny                                                termios.ECHOE | termios.ISIG),
1094a7b4beaSMichał Górny                             0)
1104a7b4beaSMichał Górny            self.assertEqual(current_attr[4], termios.B115200)
1114a7b4beaSMichał Górny            self.assertEqual(current_attr[5], termios.B115200)
1124a7b4beaSMichał Górny            self.assertEqual(current_attr[6][termios.VMIN], 1)
1134a7b4beaSMichał Górny            self.assertEqual(current_attr[6][termios.VTIME], 0)
1144a7b4beaSMichał Górny        finally:
1154a7b4beaSMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
1164a7b4beaSMichał Górny
1174a7b4beaSMichał Górny    def test_process_connect_params(self):
1184a7b4beaSMichał Górny        """Test serial:// URL with parameters"""
1194a7b4beaSMichał Górny        import termios
1204a7b4beaSMichał Górny        try:
1214a7b4beaSMichał Górny            self.expect("platform select remote-gdb-server",
1224a7b4beaSMichał Górny                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
1234a7b4beaSMichał Górny            self.expect("process connect " + self.server.get_connect_url() +
1244a7b4beaSMichał Górny                        "?baud=115200&stop-bits=2",
1254a7b4beaSMichał Górny                        substrs=['Process', 'stopped'])
1264a7b4beaSMichał Górny
1274a7b4beaSMichał Górny            current_attr = self.get_term_attrs()
1284a7b4beaSMichał Górny            self.assert_raw_mode(current_attr)
1294a7b4beaSMichał Górny            self.assertEqual(current_attr[4:6], 2 * [termios.B115200])
1304a7b4beaSMichał Górny            self.assertEqual(self.get_parity_flags(current_attr),
1314a7b4beaSMichał Górny                             self.get_parity_flags(self.orig_attr))
1324a7b4beaSMichał Górny            self.assertEqual(self.get_stop_bit_flags(current_attr),
1334a7b4beaSMichał Górny                             termios.CSTOPB)
1344a7b4beaSMichał Górny        finally:
1354a7b4beaSMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
1364a7b4beaSMichał Górny        # original mode should be restored on exit
1374a7b4beaSMichał Górny        self.assertEqual(self.get_term_attrs(), self.orig_attr)
138