R. David Murray | 95fb46c | 2009-04-21 13:06:04 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, run_unittest, import_module |
| 2 | |
| 3 | #Skip these tests if either fcntl or termios is not available |
| 4 | fcntl = import_module('fcntl') |
| 5 | import_module('termios') |
| 6 | |
Neal Norwitz | 1b59d10 | 2007-04-27 06:45:32 +0000 | [diff] [blame] | 7 | import errno |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 8 | import pty |
| 9 | import os |
Barry Warsaw | 25a3864 | 2007-04-13 18:47:14 +0000 | [diff] [blame] | 10 | import sys |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 11 | import signal |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 12 | import unittest |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 14 | TEST_STRING_1 = "I wish to buy a fish license.\n" |
| 15 | TEST_STRING_2 = "For my pet fish, Eric.\n" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 16 | |
| 17 | if verbose: |
| 18 | def debug(msg): |
| 19 | print msg |
| 20 | else: |
| 21 | def debug(msg): |
| 22 | pass |
| 23 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 24 | |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 25 | def normalize_output(data): |
| 26 | # Some operating systems do conversions on newline. We could possibly |
| 27 | # fix that by doing the appropriate termios.tcsetattr()s. I couldn't |
| 28 | # figure out the right combo on Tru64 and I don't have an IRIX box. |
Anthony Baxter | a2a26b9 | 2006-04-05 17:30:38 +0000 | [diff] [blame] | 29 | # So just normalize the output and doc the problem O/Ses by allowing |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 30 | # certain combinations for some platforms, but avoid allowing other |
| 31 | # differences (like extra whitespace, trailing garbage, etc.) |
| 32 | |
| 33 | # This is about the best we can do without getting some feedback |
| 34 | # from someone more knowledgable. |
| 35 | |
| 36 | # OSF/1 (Tru64) apparently turns \n into \r\r\n. |
| 37 | if data.endswith('\r\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 38 | return data.replace('\r\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 39 | |
| 40 | # IRIX apparently turns \n into \r\n. |
| 41 | if data.endswith('\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 42 | return data.replace('\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 43 | |
| 44 | return data |
| 45 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 46 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 47 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 48 | # because pty code is not too portable. |
Neal Norwitz | 1b59d10 | 2007-04-27 06:45:32 +0000 | [diff] [blame] | 49 | # XXX(nnorwitz): these tests leak fds when there is an error. |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 50 | class PtyTest(unittest.TestCase): |
| 51 | def setUp(self): |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 52 | # isatty() and close() can hang on some platforms. Set an alarm |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 53 | # before running the test to make sure we don't hang forever. |
| 54 | self.old_alarm = signal.signal(signal.SIGALRM, self.handle_sig) |
| 55 | signal.alarm(10) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 56 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 57 | def tearDown(self): |
| 58 | # remove alarm, restore old alarm handler |
| 59 | signal.alarm(0) |
| 60 | signal.signal(signal.SIGALRM, self.old_alarm) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 62 | def handle_sig(self, sig, frame): |
| 63 | self.fail("isatty hung") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 64 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 65 | def test_basic(self): |
| 66 | try: |
| 67 | debug("Calling master_open()") |
| 68 | master_fd, slave_name = pty.master_open() |
| 69 | debug("Got master_fd '%d', slave_name '%s'" % |
| 70 | (master_fd, slave_name)) |
| 71 | debug("Calling slave_open(%r)" % (slave_name,)) |
| 72 | slave_fd = pty.slave_open(slave_name) |
| 73 | debug("Got slave_fd '%d'" % slave_fd) |
| 74 | except OSError: |
| 75 | # " An optional feature could not be imported " ... ? |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 76 | raise unittest.SkipTest, "Pseudo-terminals (seemingly) not functional." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 78 | self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 79 | |
Neal Norwitz | 1b59d10 | 2007-04-27 06:45:32 +0000 | [diff] [blame] | 80 | # Solaris requires reading the fd before anything is returned. |
| 81 | # My guess is that since we open and close the slave fd |
| 82 | # in master_open(), we need to read the EOF. |
| 83 | |
| 84 | # Ensure the fd is non-blocking in case there's nothing to read. |
| 85 | orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL) |
| 86 | fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) |
| 87 | try: |
| 88 | s1 = os.read(master_fd, 1024) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame^] | 89 | self.assertEqual('', s1) |
Neal Norwitz | 1b59d10 | 2007-04-27 06:45:32 +0000 | [diff] [blame] | 90 | except OSError, e: |
| 91 | if e.errno != errno.EAGAIN: |
| 92 | raise |
| 93 | # Restore the original flags. |
| 94 | fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags) |
| 95 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 96 | debug("Writing to slave_fd") |
| 97 | os.write(slave_fd, TEST_STRING_1) |
| 98 | s1 = os.read(master_fd, 1024) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame^] | 99 | self.assertEqual('I wish to buy a fish license.\n', |
| 100 | normalize_output(s1)) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 101 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 102 | debug("Writing chunked output") |
| 103 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 104 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 105 | s2 = os.read(master_fd, 1024) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame^] | 106 | self.assertEqual('For my pet fish, Eric.\n', normalize_output(s2)) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 108 | os.close(slave_fd) |
| 109 | os.close(master_fd) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 110 | |
| 111 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 112 | def test_fork(self): |
| 113 | debug("calling pty.fork()") |
| 114 | pid, master_fd = pty.fork() |
| 115 | if pid == pty.CHILD: |
| 116 | # stdout should be connected to a tty. |
| 117 | if not os.isatty(1): |
| 118 | debug("Child's fd 1 is not a tty?!") |
| 119 | os._exit(3) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 120 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 121 | # After pty.fork(), the child should already be a session leader. |
| 122 | # (on those systems that have that concept.) |
| 123 | debug("In child, calling os.setsid()") |
| 124 | try: |
| 125 | os.setsid() |
| 126 | except OSError: |
| 127 | # Good, we already were session leader |
| 128 | debug("Good: OSError was raised.") |
| 129 | pass |
| 130 | except AttributeError: |
| 131 | # Have pty, but not setsid()? |
| 132 | debug("No setsid() available?") |
| 133 | pass |
| 134 | except: |
| 135 | # We don't want this error to propagate, escaping the call to |
| 136 | # os._exit() and causing very peculiar behavior in the calling |
| 137 | # regrtest.py ! |
| 138 | # Note: could add traceback printing here. |
| 139 | debug("An unexpected error was raised.") |
| 140 | os._exit(1) |
| 141 | else: |
| 142 | debug("os.setsid() succeeded! (bad!)") |
| 143 | os._exit(2) |
| 144 | os._exit(4) |
| 145 | else: |
| 146 | debug("Waiting for child (%d) to finish." % pid) |
Barry Warsaw | 25a3864 | 2007-04-13 18:47:14 +0000 | [diff] [blame] | 147 | # In verbose mode, we have to consume the debug output from the |
| 148 | # child or the child will block, causing this test to hang in the |
| 149 | # parent's waitpid() call. The child blocks after a |
| 150 | # platform-dependent amount of data is written to its fd. On |
| 151 | # Linux 2.6, it's 4000 bytes and the child won't block, but on OS |
| 152 | # X even the small writes in the child above will block it. Also |
| 153 | # on Linux, the read() will throw an OSError (input/output error) |
| 154 | # when it tries to read past the end of the buffer but the child's |
| 155 | # already exited, so catch and discard those exceptions. It's not |
| 156 | # worth checking for EIO. |
| 157 | while True: |
| 158 | try: |
| 159 | data = os.read(master_fd, 80) |
| 160 | except OSError: |
| 161 | break |
| 162 | if not data: |
| 163 | break |
| 164 | sys.stdout.write(data.replace('\r\n', '\n')) |
| 165 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 166 | ##line = os.read(master_fd, 80) |
| 167 | ##lines = line.replace('\r\n', '\n').split('\n') |
| 168 | ##if False and lines != ['In child, calling os.setsid()', |
| 169 | ## 'Good: OSError was raised.', '']: |
| 170 | ## raise TestFailed("Unexpected output from child: %r" % line) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 172 | (pid, status) = os.waitpid(pid, 0) |
| 173 | res = status >> 8 |
| 174 | debug("Child (%d) exited with status %d (%d)." % (pid, res, status)) |
| 175 | if res == 1: |
| 176 | self.fail("Child raised an unexpected exception in os.setsid()") |
| 177 | elif res == 2: |
| 178 | self.fail("pty.fork() failed to make child a session leader.") |
| 179 | elif res == 3: |
| 180 | self.fail("Child spawned by pty.fork() did not have a tty as stdout") |
| 181 | elif res != 4: |
| 182 | self.fail("pty.fork() failed for unknown reasons.") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 183 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 184 | ##debug("Reading from master_fd now that the child has exited") |
| 185 | ##try: |
| 186 | ## s1 = os.read(master_fd, 1024) |
| 187 | ##except os.error: |
| 188 | ## pass |
| 189 | ##else: |
| 190 | ## raise TestFailed("Read from master_fd did not raise exception") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 191 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 192 | os.close(master_fd) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 193 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 194 | # pty.fork() passed. |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 195 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 196 | def test_main(verbose=None): |
| 197 | run_unittest(PtyTest) |
| 198 | |
| 199 | if __name__ == "__main__": |
| 200 | test_main() |