Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 1 | import pty |
| 2 | import os |
| 3 | import signal |
| 4 | from test.test_support import verbose, TestSkipped, run_unittest |
| 5 | import unittest |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 7 | TEST_STRING_1 = "I wish to buy a fish license.\n" |
| 8 | TEST_STRING_2 = "For my pet fish, Eric.\n" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 9 | |
| 10 | if verbose: |
| 11 | def debug(msg): |
| 12 | print msg |
| 13 | else: |
| 14 | def debug(msg): |
| 15 | pass |
| 16 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 17 | |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 18 | def normalize_output(data): |
| 19 | # Some operating systems do conversions on newline. We could possibly |
| 20 | # fix that by doing the appropriate termios.tcsetattr()s. I couldn't |
| 21 | # 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] | 22 | # 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] | 23 | # certain combinations for some platforms, but avoid allowing other |
| 24 | # differences (like extra whitespace, trailing garbage, etc.) |
| 25 | |
| 26 | # This is about the best we can do without getting some feedback |
| 27 | # from someone more knowledgable. |
| 28 | |
| 29 | # OSF/1 (Tru64) apparently turns \n into \r\r\n. |
| 30 | if data.endswith('\r\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 31 | return data.replace('\r\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 32 | |
| 33 | # IRIX apparently turns \n into \r\n. |
| 34 | if data.endswith('\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 35 | return data.replace('\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 36 | |
| 37 | return data |
| 38 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 39 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 40 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 41 | # because pty code is not too portable. |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 42 | class PtyTest(unittest.TestCase): |
| 43 | def setUp(self): |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 44 | # isatty() and close() can hang on some platforms. Set an alarm |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 45 | # before running the test to make sure we don't hang forever. |
| 46 | self.old_alarm = signal.signal(signal.SIGALRM, self.handle_sig) |
| 47 | signal.alarm(10) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 48 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 49 | def tearDown(self): |
| 50 | # remove alarm, restore old alarm handler |
| 51 | signal.alarm(0) |
| 52 | signal.signal(signal.SIGALRM, self.old_alarm) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 53 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 54 | def handle_sig(self, sig, frame): |
| 55 | self.fail("isatty hung") |
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 test_basic(self): |
| 58 | try: |
| 59 | debug("Calling master_open()") |
| 60 | master_fd, slave_name = pty.master_open() |
| 61 | debug("Got master_fd '%d', slave_name '%s'" % |
| 62 | (master_fd, slave_name)) |
| 63 | debug("Calling slave_open(%r)" % (slave_name,)) |
| 64 | slave_fd = pty.slave_open(slave_name) |
| 65 | debug("Got slave_fd '%d'" % slave_fd) |
| 66 | except OSError: |
| 67 | # " An optional feature could not be imported " ... ? |
| 68 | raise TestSkipped, "Pseudo-terminals (seemingly) not functional." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 69 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 70 | self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 71 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 72 | debug("Writing to slave_fd") |
| 73 | os.write(slave_fd, TEST_STRING_1) |
| 74 | s1 = os.read(master_fd, 1024) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 75 | self.assertEquals('I wish to buy a fish license.\n', |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 76 | normalize_output(s1)) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 78 | debug("Writing chunked output") |
| 79 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 80 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 81 | s2 = os.read(master_fd, 1024) |
| 82 | self.assertEquals('For my pet fish, Eric.\n', normalize_output(s2)) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 83 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 84 | os.close(slave_fd) |
| 85 | os.close(master_fd) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 86 | |
| 87 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 88 | def test_fork(self): |
| 89 | debug("calling pty.fork()") |
| 90 | pid, master_fd = pty.fork() |
| 91 | if pid == pty.CHILD: |
| 92 | # stdout should be connected to a tty. |
| 93 | if not os.isatty(1): |
| 94 | debug("Child's fd 1 is not a tty?!") |
| 95 | os._exit(3) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 96 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 97 | # After pty.fork(), the child should already be a session leader. |
| 98 | # (on those systems that have that concept.) |
| 99 | debug("In child, calling os.setsid()") |
| 100 | try: |
| 101 | os.setsid() |
| 102 | except OSError: |
| 103 | # Good, we already were session leader |
| 104 | debug("Good: OSError was raised.") |
| 105 | pass |
| 106 | except AttributeError: |
| 107 | # Have pty, but not setsid()? |
| 108 | debug("No setsid() available?") |
| 109 | pass |
| 110 | except: |
| 111 | # We don't want this error to propagate, escaping the call to |
| 112 | # os._exit() and causing very peculiar behavior in the calling |
| 113 | # regrtest.py ! |
| 114 | # Note: could add traceback printing here. |
| 115 | debug("An unexpected error was raised.") |
| 116 | os._exit(1) |
| 117 | else: |
| 118 | debug("os.setsid() succeeded! (bad!)") |
| 119 | os._exit(2) |
| 120 | os._exit(4) |
| 121 | else: |
| 122 | debug("Waiting for child (%d) to finish." % pid) |
| 123 | ##line = os.read(master_fd, 80) |
| 124 | ##lines = line.replace('\r\n', '\n').split('\n') |
| 125 | ##if False and lines != ['In child, calling os.setsid()', |
| 126 | ## 'Good: OSError was raised.', '']: |
| 127 | ## raise TestFailed("Unexpected output from child: %r" % line) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 129 | (pid, status) = os.waitpid(pid, 0) |
| 130 | res = status >> 8 |
| 131 | debug("Child (%d) exited with status %d (%d)." % (pid, res, status)) |
| 132 | if res == 1: |
| 133 | self.fail("Child raised an unexpected exception in os.setsid()") |
| 134 | elif res == 2: |
| 135 | self.fail("pty.fork() failed to make child a session leader.") |
| 136 | elif res == 3: |
| 137 | self.fail("Child spawned by pty.fork() did not have a tty as stdout") |
| 138 | elif res != 4: |
| 139 | self.fail("pty.fork() failed for unknown reasons.") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 141 | ##debug("Reading from master_fd now that the child has exited") |
| 142 | ##try: |
| 143 | ## s1 = os.read(master_fd, 1024) |
| 144 | ##except os.error: |
| 145 | ## pass |
| 146 | ##else: |
| 147 | ## raise TestFailed("Read from master_fd did not raise exception") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 148 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 149 | os.close(master_fd) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 150 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 151 | # pty.fork() passed. |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 152 | |
Georg Brandl | 9decc0d | 2007-03-07 11:37:42 +0000 | [diff] [blame] | 153 | def test_main(verbose=None): |
| 154 | run_unittest(PtyTest) |
| 155 | |
| 156 | if __name__ == "__main__": |
| 157 | test_main() |