blob: bfa624fdfd27e13f928d04bc35c6074a8804d74a [file] [log] [blame]
Georg Brandl9decc0d2007-03-07 11:37:42 +00001import pty
2import os
3import signal
4from test.test_support import verbose, TestSkipped, run_unittest
5import unittest
Fred Drake4c136ee2000-06-30 23:22:35 +00006
Thomas Woutersb0dbeef2001-03-22 14:50:24 +00007TEST_STRING_1 = "I wish to buy a fish license.\n"
8TEST_STRING_2 = "For my pet fish, Eric.\n"
Fred Drake4c136ee2000-06-30 23:22:35 +00009
10if verbose:
11 def debug(msg):
12 print msg
13else:
14 def debug(msg):
15 pass
16
Georg Brandl9decc0d2007-03-07 11:37:42 +000017
Neal Norwitz84c95b92006-04-03 05:28:31 +000018def 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 Baxtera2a26b92006-04-05 17:30:38 +000022 # So just normalize the output and doc the problem O/Ses by allowing
Neal Norwitz84c95b92006-04-03 05:28:31 +000023 # 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 Norwitz9cc3b1c2006-04-26 06:26:12 +000031 return data.replace('\r\r\n', '\n')
Neal Norwitz84c95b92006-04-03 05:28:31 +000032
33 # IRIX apparently turns \n into \r\n.
34 if data.endswith('\r\n'):
Neal Norwitz9cc3b1c2006-04-26 06:26:12 +000035 return data.replace('\r\n', '\n')
Neal Norwitz84c95b92006-04-03 05:28:31 +000036
37 return data
38
Georg Brandl9decc0d2007-03-07 11:37:42 +000039
Fred Drake4c136ee2000-06-30 23:22:35 +000040# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
41# because pty code is not too portable.
Georg Brandl9decc0d2007-03-07 11:37:42 +000042class PtyTest(unittest.TestCase):
43 def setUp(self):
Tim Petersea5962f2007-03-12 18:07:52 +000044 # isatty() and close() can hang on some platforms. Set an alarm
Georg Brandl9decc0d2007-03-07 11:37:42 +000045 # 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 Petersea5962f2007-03-12 18:07:52 +000048
Georg Brandl9decc0d2007-03-07 11:37:42 +000049 def tearDown(self):
50 # remove alarm, restore old alarm handler
51 signal.alarm(0)
52 signal.signal(signal.SIGALRM, self.old_alarm)
Tim Petersea5962f2007-03-12 18:07:52 +000053
Georg Brandl9decc0d2007-03-07 11:37:42 +000054 def handle_sig(self, sig, frame):
55 self.fail("isatty hung")
Tim Petersea5962f2007-03-12 18:07:52 +000056
Georg Brandl9decc0d2007-03-07 11:37:42 +000057 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 Drake4c136ee2000-06-30 23:22:35 +000069
Georg Brandl9decc0d2007-03-07 11:37:42 +000070 self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty')
Tim Petersea5962f2007-03-12 18:07:52 +000071
Georg Brandl9decc0d2007-03-07 11:37:42 +000072 debug("Writing to slave_fd")
73 os.write(slave_fd, TEST_STRING_1)
74 s1 = os.read(master_fd, 1024)
Tim Petersea5962f2007-03-12 18:07:52 +000075 self.assertEquals('I wish to buy a fish license.\n',
Georg Brandl9decc0d2007-03-07 11:37:42 +000076 normalize_output(s1))
Tim Petersea5962f2007-03-12 18:07:52 +000077
Georg Brandl9decc0d2007-03-07 11:37:42 +000078 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 Petersea5962f2007-03-12 18:07:52 +000083
Georg Brandl9decc0d2007-03-07 11:37:42 +000084 os.close(slave_fd)
85 os.close(master_fd)
Tim Petersf733abb2007-01-30 03:03:46 +000086
87
Georg Brandl9decc0d2007-03-07 11:37:42 +000088 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 Petersea5962f2007-03-12 18:07:52 +000096
Georg Brandl9decc0d2007-03-07 11:37:42 +000097 # 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 Petersea5962f2007-03-12 18:07:52 +0000128
Georg Brandl9decc0d2007-03-07 11:37:42 +0000129 (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 Petersea5962f2007-03-12 18:07:52 +0000140
Georg Brandl9decc0d2007-03-07 11:37:42 +0000141 ##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 Petersea5962f2007-03-12 18:07:52 +0000148
Georg Brandl9decc0d2007-03-07 11:37:42 +0000149 os.close(master_fd)
Tim Petersea5962f2007-03-12 18:07:52 +0000150
Georg Brandl9decc0d2007-03-07 11:37:42 +0000151 # pty.fork() passed.
Fred Drake4c136ee2000-06-30 23:22:35 +0000152
Georg Brandl9decc0d2007-03-07 11:37:42 +0000153def test_main(verbose=None):
154 run_unittest(PtyTest)
155
156if __name__ == "__main__":
157 test_main()