Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test.support import TestSkipped, run_unittest |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 3 | import os, struct |
| 4 | try: |
| 5 | import fcntl, termios |
| 6 | except ImportError: |
| 7 | raise TestSkipped("No fcntl or termios module") |
| 8 | if not hasattr(termios,'TIOCGPGRP'): |
| 9 | raise TestSkipped("termios module doesn't have TIOCGPGRP") |
| 10 | |
Neal Norwitz | 26f42f6 | 2003-03-20 04:33:16 +0000 | [diff] [blame] | 11 | try: |
| 12 | tty = open("/dev/tty", "r") |
| 13 | tty.close() |
| 14 | except IOError: |
| 15 | raise TestSkipped("Unable to open /dev/tty") |
| 16 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 17 | try: |
| 18 | import pty |
| 19 | except ImportError: |
| 20 | pty = None |
| 21 | |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 22 | class IoctlTests(unittest.TestCase): |
| 23 | def test_ioctl(self): |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 24 | # If this process has been put into the background, TIOCGPGRP returns |
| 25 | # the session ID instead of the process group id. |
| 26 | ids = (os.getpgrp(), os.getsid(0)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 27 | tty = open("/dev/tty", "r") |
| 28 | r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 29 | rpgrp = struct.unpack("i", r)[0] |
| 30 | self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 31 | |
| 32 | def test_ioctl_mutate(self): |
| 33 | import array |
| 34 | buf = array.array('i', [0]) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 35 | ids = (os.getpgrp(), os.getsid(0)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 36 | tty = open("/dev/tty", "r") |
| 37 | r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 38 | rpgrp = buf[0] |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 39 | self.assertEquals(r, 0) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 40 | self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 41 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 42 | def test_ioctl_signed_unsigned_code_param(self): |
| 43 | if not pty: |
| 44 | raise TestSkipped('pty module required') |
| 45 | mfd, sfd = pty.openpty() |
| 46 | try: |
| 47 | if termios.TIOCSWINSZ < 0: |
| 48 | set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ |
| 49 | set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff |
| 50 | else: |
| 51 | set_winsz_opcode_pos = termios.TIOCSWINSZ |
| 52 | set_winsz_opcode_maybe_neg, = struct.unpack("i", |
| 53 | struct.pack("I", termios.TIOCSWINSZ)) |
| 54 | |
| 55 | # We're just testing that these calls do not raise exceptions. |
| 56 | saved_winsz = fcntl.ioctl(mfd, termios.TIOCGWINSZ, "\0"*8) |
| 57 | our_winsz = struct.pack("HHHH",80,25,0,0) |
| 58 | # test both with a positive and potentially negative ioctl code |
| 59 | new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz) |
| 60 | new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz) |
| 61 | fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, saved_winsz) |
| 62 | finally: |
| 63 | os.close(mfd) |
| 64 | os.close(sfd) |
| 65 | |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 66 | def test_main(): |
| 67 | run_unittest(IoctlTests) |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | test_main() |