blob: f6508c25d23d310223d4e9fa73e928e806a65251 [file] [log] [blame]
Michael W. Hudsonf0089982003-03-03 12:29:42 +00001import unittest
Benjamin Petersone549ead2009-03-28 21:42:05 +00002from test.support import run_unittest
Michael W. Hudsonf0089982003-03-03 12:29:42 +00003import os, struct
4try:
5 import fcntl, termios
6except ImportError:
Benjamin Petersone549ead2009-03-28 21:42:05 +00007 raise unittest.SkipTest("No fcntl or termios module")
Michael W. Hudsonf0089982003-03-03 12:29:42 +00008if not hasattr(termios,'TIOCGPGRP'):
Benjamin Petersone549ead2009-03-28 21:42:05 +00009 raise unittest.SkipTest("termios module doesn't have TIOCGPGRP")
Michael W. Hudsonf0089982003-03-03 12:29:42 +000010
Neal Norwitz26f42f62003-03-20 04:33:16 +000011try:
12 tty = open("/dev/tty", "r")
13 tty.close()
14except IOError:
Benjamin Petersone549ead2009-03-28 21:42:05 +000015 raise unittest.SkipTest("Unable to open /dev/tty")
Neal Norwitz26f42f62003-03-20 04:33:16 +000016
Christian Heimese25f35e2008-03-20 10:49:03 +000017try:
18 import pty
19except ImportError:
20 pty = None
21
Michael W. Hudsonf0089982003-03-03 12:29:42 +000022class IoctlTests(unittest.TestCase):
23 def test_ioctl(self):
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000024 # 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. Hudsonf0089982003-03-03 12:29:42 +000027 tty = open("/dev/tty", "r")
28 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000029 rpgrp = struct.unpack("i", r)[0]
30 self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000031
32 def test_ioctl_mutate(self):
33 import array
34 buf = array.array('i', [0])
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000035 ids = (os.getpgrp(), os.getsid(0))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000036 tty = open("/dev/tty", "r")
37 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000038 rpgrp = buf[0]
Michael W. Hudsonf0089982003-03-03 12:29:42 +000039 self.assertEquals(r, 0)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000040 self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000041
Christian Heimese25f35e2008-03-20 10:49:03 +000042 def test_ioctl_signed_unsigned_code_param(self):
43 if not pty:
Benjamin Petersone549ead2009-03-28 21:42:05 +000044 raise unittest.SkipTest('pty module required')
Christian Heimese25f35e2008-03-20 10:49:03 +000045 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
Christian Heimese25f35e2008-03-20 10:49:03 +000055 our_winsz = struct.pack("HHHH",80,25,0,0)
56 # test both with a positive and potentially negative ioctl code
57 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
58 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
Christian Heimese25f35e2008-03-20 10:49:03 +000059 finally:
60 os.close(mfd)
61 os.close(sfd)
62
Michael W. Hudsonf0089982003-03-03 12:29:42 +000063def test_main():
64 run_unittest(IoctlTests)
65
66if __name__ == "__main__":
67 test_main()