Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 1 | from test.support import verbose, run_unittest, import_module, reap_children |
R. David Murray | eb3615d | 2009-04-22 02:24:39 +0000 | [diff] [blame] | 2 | |
| 3 | #Skip these tests if either fcntl or termios is not available |
| 4 | fcntl = import_module('fcntl') |
| 5 | import_module('termios') |
| 6 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 7 | import errno |
R. David Murray | eb3615d | 2009-04-22 02:24:39 +0000 | [diff] [blame] | 8 | import pty |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 9 | import os |
| 10 | import sys |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 11 | import select |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 12 | import signal |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 13 | import socket |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 14 | import unittest |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 15 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 16 | TEST_STRING_1 = b"I wish to buy a fish license.\n" |
| 17 | TEST_STRING_2 = b"For my pet fish, Eric.\n" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 18 | |
| 19 | if verbose: |
| 20 | def debug(msg): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 21 | print(msg) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 22 | else: |
| 23 | def debug(msg): |
| 24 | pass |
| 25 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | def normalize_output(data): |
| 28 | # Some operating systems do conversions on newline. We could possibly |
| 29 | # fix that by doing the appropriate termios.tcsetattr()s. I couldn't |
| 30 | # figure out the right combo on Tru64 and I don't have an IRIX box. |
| 31 | # So just normalize the output and doc the problem O/Ses by allowing |
| 32 | # certain combinations for some platforms, but avoid allowing other |
| 33 | # differences (like extra whitespace, trailing garbage, etc.) |
| 34 | |
| 35 | # This is about the best we can do without getting some feedback |
| 36 | # from someone more knowledgable. |
| 37 | |
| 38 | # OSF/1 (Tru64) apparently turns \n into \r\r\n. |
Walter Dörwald | 812d834 | 2007-05-29 18:57:42 +0000 | [diff] [blame] | 39 | if data.endswith(b'\r\r\n'): |
| 40 | return data.replace(b'\r\r\n', b'\n') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | |
| 42 | # IRIX apparently turns \n into \r\n. |
Walter Dörwald | 812d834 | 2007-05-29 18:57:42 +0000 | [diff] [blame] | 43 | if data.endswith(b'\r\n'): |
| 44 | return data.replace(b'\r\n', b'\n') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | |
| 46 | return data |
| 47 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 48 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 49 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 50 | # because pty code is not too portable. |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 51 | # XXX(nnorwitz): these tests leak fds when there is an error. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 52 | class PtyTest(unittest.TestCase): |
| 53 | def setUp(self): |
| 54 | # isatty() and close() can hang on some platforms. Set an alarm |
| 55 | # before running the test to make sure we don't hang forever. |
| 56 | self.old_alarm = signal.signal(signal.SIGALRM, self.handle_sig) |
| 57 | signal.alarm(10) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | def tearDown(self): |
| 60 | # remove alarm, restore old alarm handler |
| 61 | signal.alarm(0) |
| 62 | signal.signal(signal.SIGALRM, self.old_alarm) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 64 | def handle_sig(self, sig, frame): |
| 65 | self.fail("isatty hung") |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 67 | def test_basic(self): |
| 68 | try: |
| 69 | debug("Calling master_open()") |
| 70 | master_fd, slave_name = pty.master_open() |
| 71 | debug("Got master_fd '%d', slave_name '%s'" % |
| 72 | (master_fd, slave_name)) |
| 73 | debug("Calling slave_open(%r)" % (slave_name,)) |
| 74 | slave_fd = pty.slave_open(slave_name) |
| 75 | debug("Got slave_fd '%d'" % slave_fd) |
| 76 | except OSError: |
| 77 | # " An optional feature could not be imported " ... ? |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 78 | raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.") |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 80 | self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 82 | # Solaris requires reading the fd before anything is returned. |
| 83 | # My guess is that since we open and close the slave fd |
| 84 | # in master_open(), we need to read the EOF. |
| 85 | |
| 86 | # Ensure the fd is non-blocking in case there's nothing to read. |
| 87 | orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL) |
| 88 | fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) |
| 89 | try: |
| 90 | s1 = os.read(master_fd, 1024) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 91 | self.assertEqual(b'', s1) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 92 | except OSError as e: |
| 93 | if e.errno != errno.EAGAIN: |
| 94 | raise |
| 95 | # Restore the original flags. |
| 96 | fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags) |
| 97 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 98 | debug("Writing to slave_fd") |
| 99 | os.write(slave_fd, TEST_STRING_1) |
| 100 | s1 = os.read(master_fd, 1024) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 101 | self.assertEqual(b'I wish to buy a fish license.\n', |
| 102 | normalize_output(s1)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | debug("Writing chunked output") |
| 105 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 106 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 107 | s2 = os.read(master_fd, 1024) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 108 | self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | os.close(slave_fd) |
| 111 | os.close(master_fd) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 112 | |
| 113 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 114 | def test_fork(self): |
| 115 | debug("calling pty.fork()") |
| 116 | pid, master_fd = pty.fork() |
| 117 | if pid == pty.CHILD: |
| 118 | # stdout should be connected to a tty. |
| 119 | if not os.isatty(1): |
| 120 | debug("Child's fd 1 is not a tty?!") |
| 121 | os._exit(3) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 123 | # After pty.fork(), the child should already be a session leader. |
| 124 | # (on those systems that have that concept.) |
| 125 | debug("In child, calling os.setsid()") |
| 126 | try: |
| 127 | os.setsid() |
| 128 | except OSError: |
| 129 | # Good, we already were session leader |
| 130 | debug("Good: OSError was raised.") |
| 131 | pass |
| 132 | except AttributeError: |
| 133 | # Have pty, but not setsid()? |
| 134 | debug("No setsid() available?") |
| 135 | pass |
| 136 | except: |
| 137 | # We don't want this error to propagate, escaping the call to |
| 138 | # os._exit() and causing very peculiar behavior in the calling |
| 139 | # regrtest.py ! |
| 140 | # Note: could add traceback printing here. |
| 141 | debug("An unexpected error was raised.") |
| 142 | os._exit(1) |
| 143 | else: |
| 144 | debug("os.setsid() succeeded! (bad!)") |
| 145 | os._exit(2) |
| 146 | os._exit(4) |
| 147 | else: |
| 148 | debug("Waiting for child (%d) to finish." % pid) |
| 149 | # In verbose mode, we have to consume the debug output from the |
| 150 | # child or the child will block, causing this test to hang in the |
| 151 | # parent's waitpid() call. The child blocks after a |
| 152 | # platform-dependent amount of data is written to its fd. On |
| 153 | # Linux 2.6, it's 4000 bytes and the child won't block, but on OS |
| 154 | # X even the small writes in the child above will block it. Also |
| 155 | # on Linux, the read() will throw an OSError (input/output error) |
| 156 | # when it tries to read past the end of the buffer but the child's |
| 157 | # already exited, so catch and discard those exceptions. It's not |
| 158 | # worth checking for EIO. |
| 159 | while True: |
| 160 | try: |
| 161 | data = os.read(master_fd, 80) |
| 162 | except OSError: |
| 163 | break |
| 164 | if not data: |
| 165 | break |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 166 | sys.stdout.write(str(data.replace(b'\r\n', b'\n'), |
| 167 | encoding='ascii')) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 168 | |
| 169 | ##line = os.read(master_fd, 80) |
| 170 | ##lines = line.replace('\r\n', '\n').split('\n') |
| 171 | ##if False and lines != ['In child, calling os.setsid()', |
| 172 | ## 'Good: OSError was raised.', '']: |
| 173 | ## raise TestFailed("Unexpected output from child: %r" % line) |
| 174 | |
| 175 | (pid, status) = os.waitpid(pid, 0) |
| 176 | res = status >> 8 |
| 177 | debug("Child (%d) exited with status %d (%d)." % (pid, res, status)) |
| 178 | if res == 1: |
| 179 | self.fail("Child raised an unexpected exception in os.setsid()") |
| 180 | elif res == 2: |
| 181 | self.fail("pty.fork() failed to make child a session leader.") |
| 182 | elif res == 3: |
| 183 | self.fail("Child spawned by pty.fork() did not have a tty as stdout") |
| 184 | elif res != 4: |
| 185 | self.fail("pty.fork() failed for unknown reasons.") |
| 186 | |
| 187 | ##debug("Reading from master_fd now that the child has exited") |
| 188 | ##try: |
| 189 | ## s1 = os.read(master_fd, 1024) |
| 190 | ##except os.error: |
| 191 | ## pass |
| 192 | ##else: |
| 193 | ## raise TestFailed("Read from master_fd did not raise exception") |
| 194 | |
| 195 | os.close(master_fd) |
| 196 | |
| 197 | # pty.fork() passed. |
| 198 | |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 199 | |
| 200 | class SmallPtyTests(unittest.TestCase): |
| 201 | """These tests don't spawn children or hang.""" |
| 202 | |
| 203 | def setUp(self): |
| 204 | self.orig_stdin_fileno = pty.STDIN_FILENO |
| 205 | self.orig_stdout_fileno = pty.STDOUT_FILENO |
| 206 | self.orig_pty_select = pty.select |
| 207 | self.fds = [] # A list of file descriptors to close. |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 208 | self.files = [] |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 209 | self.select_rfds_lengths = [] |
| 210 | self.select_rfds_results = [] |
| 211 | |
| 212 | def tearDown(self): |
| 213 | pty.STDIN_FILENO = self.orig_stdin_fileno |
| 214 | pty.STDOUT_FILENO = self.orig_stdout_fileno |
| 215 | pty.select = self.orig_pty_select |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 216 | for file in self.files: |
| 217 | try: |
| 218 | file.close() |
| 219 | except OSError: |
| 220 | pass |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 221 | for fd in self.fds: |
| 222 | try: |
| 223 | os.close(fd) |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 224 | except OSError: |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 225 | pass |
| 226 | |
| 227 | def _pipe(self): |
| 228 | pipe_fds = os.pipe() |
| 229 | self.fds.extend(pipe_fds) |
| 230 | return pipe_fds |
| 231 | |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 232 | def _socketpair(self): |
| 233 | socketpair = socket.socketpair() |
| 234 | self.files.extend(socketpair) |
| 235 | return socketpair |
| 236 | |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 237 | def _mock_select(self, rfds, wfds, xfds): |
| 238 | # This will raise IndexError when no more expected calls exist. |
| 239 | self.assertEqual(self.select_rfds_lengths.pop(0), len(rfds)) |
| 240 | return self.select_rfds_results.pop(0), [], [] |
| 241 | |
| 242 | def test__copy_to_each(self): |
| 243 | """Test the normal data case on both master_fd and stdin.""" |
| 244 | read_from_stdout_fd, mock_stdout_fd = self._pipe() |
| 245 | pty.STDOUT_FILENO = mock_stdout_fd |
| 246 | mock_stdin_fd, write_to_stdin_fd = self._pipe() |
| 247 | pty.STDIN_FILENO = mock_stdin_fd |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 248 | socketpair = self._socketpair() |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 249 | masters = [s.fileno() for s in socketpair] |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 250 | |
| 251 | # Feed data. Smaller than PIPEBUF. These writes will not block. |
| 252 | os.write(masters[1], b'from master') |
| 253 | os.write(write_to_stdin_fd, b'from stdin') |
| 254 | |
| 255 | # Expect two select calls, the last one will cause IndexError |
| 256 | pty.select = self._mock_select |
| 257 | self.select_rfds_lengths.append(2) |
| 258 | self.select_rfds_results.append([mock_stdin_fd, masters[0]]) |
| 259 | self.select_rfds_lengths.append(2) |
| 260 | |
| 261 | with self.assertRaises(IndexError): |
| 262 | pty._copy(masters[0]) |
| 263 | |
| 264 | # Test that the right data went to the right places. |
| 265 | rfds = select.select([read_from_stdout_fd, masters[1]], [], [], 0)[0] |
Gregory P. Smith | 5b791fb | 2012-02-16 00:35:43 -0800 | [diff] [blame] | 266 | self.assertEqual([read_from_stdout_fd, masters[1]], rfds) |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 267 | self.assertEqual(os.read(read_from_stdout_fd, 20), b'from master') |
| 268 | self.assertEqual(os.read(masters[1], 20), b'from stdin') |
| 269 | |
| 270 | def test__copy_eof_on_all(self): |
| 271 | """Test the empty read EOF case on both master_fd and stdin.""" |
| 272 | read_from_stdout_fd, mock_stdout_fd = self._pipe() |
| 273 | pty.STDOUT_FILENO = mock_stdout_fd |
| 274 | mock_stdin_fd, write_to_stdin_fd = self._pipe() |
| 275 | pty.STDIN_FILENO = mock_stdin_fd |
Victor Stinner | b1f7f63 | 2012-03-06 02:04:58 +0100 | [diff] [blame] | 276 | socketpair = self._socketpair() |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 277 | masters = [s.fileno() for s in socketpair] |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 278 | |
| 279 | os.close(masters[1]) |
| 280 | socketpair[1].close() |
| 281 | os.close(write_to_stdin_fd) |
| 282 | |
| 283 | # Expect two select calls, the last one will cause IndexError |
| 284 | pty.select = self._mock_select |
| 285 | self.select_rfds_lengths.append(2) |
| 286 | self.select_rfds_results.append([mock_stdin_fd, masters[0]]) |
| 287 | # We expect that both fds were removed from the fds list as they |
| 288 | # both encountered an EOF before the second select call. |
| 289 | self.select_rfds_lengths.append(0) |
| 290 | |
| 291 | with self.assertRaises(IndexError): |
| 292 | pty._copy(masters[0]) |
| 293 | |
| 294 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 295 | def test_main(verbose=None): |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 296 | try: |
Gregory P. Smith | 05f5953 | 2012-02-16 00:29:12 -0800 | [diff] [blame] | 297 | run_unittest(SmallPtyTests, PtyTest) |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 298 | finally: |
| 299 | reap_children() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 300 | |
| 301 | if __name__ == "__main__": |
| 302 | test_main() |