blob: 47e591152303ec90c45449e5519b51a6cb094f3b [file] [log] [blame]
Michael W. Hudsonf0089982003-03-03 12:29:42 +00001import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +00002from test.support import run_unittest, import_module, get_attribute
Michael W. Hudsonf0089982003-03-03 12:29:42 +00003import os, struct
R. David Murraya21e4ca2009-03-31 23:16:50 +00004fcntl = import_module('fcntl')
5termios = import_module('termios')
6get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
Michael W. Hudsonf0089982003-03-03 12:29:42 +00007
Neal Norwitz26f42f62003-03-20 04:33:16 +00008try:
9 tty = open("/dev/tty", "r")
10 tty.close()
11except IOError:
Benjamin Petersone549ead2009-03-28 21:42:05 +000012 raise unittest.SkipTest("Unable to open /dev/tty")
Neal Norwitz26f42f62003-03-20 04:33:16 +000013
Christian Heimese25f35e2008-03-20 10:49:03 +000014try:
15 import pty
16except ImportError:
17 pty = None
18
Michael W. Hudsonf0089982003-03-03 12:29:42 +000019class IoctlTests(unittest.TestCase):
20 def test_ioctl(self):
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000021 # If this process has been put into the background, TIOCGPGRP returns
22 # the session ID instead of the process group id.
23 ids = (os.getpgrp(), os.getsid(0))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000024 tty = open("/dev/tty", "r")
25 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000026 rpgrp = struct.unpack("i", r)[0]
Benjamin Peterson577473f2010-01-19 00:09:57 +000027 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000028
29 def test_ioctl_mutate(self):
30 import array
31 buf = array.array('i', [0])
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000032 ids = (os.getpgrp(), os.getsid(0))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000033 tty = open("/dev/tty", "r")
34 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000035 rpgrp = buf[0]
Michael W. Hudsonf0089982003-03-03 12:29:42 +000036 self.assertEquals(r, 0)
Benjamin Peterson577473f2010-01-19 00:09:57 +000037 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000038
Christian Heimese25f35e2008-03-20 10:49:03 +000039 def test_ioctl_signed_unsigned_code_param(self):
40 if not pty:
Benjamin Petersone549ead2009-03-28 21:42:05 +000041 raise unittest.SkipTest('pty module required')
Christian Heimese25f35e2008-03-20 10:49:03 +000042 mfd, sfd = pty.openpty()
43 try:
44 if termios.TIOCSWINSZ < 0:
45 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
46 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff
47 else:
48 set_winsz_opcode_pos = termios.TIOCSWINSZ
49 set_winsz_opcode_maybe_neg, = struct.unpack("i",
50 struct.pack("I", termios.TIOCSWINSZ))
51
Christian Heimese25f35e2008-03-20 10:49:03 +000052 our_winsz = struct.pack("HHHH",80,25,0,0)
53 # test both with a positive and potentially negative ioctl code
54 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
55 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
Christian Heimese25f35e2008-03-20 10:49:03 +000056 finally:
57 os.close(mfd)
58 os.close(sfd)
59
Michael W. Hudsonf0089982003-03-03 12:29:42 +000060def test_main():
61 run_unittest(IoctlTests)
62
63if __name__ == "__main__":
64 test_main()