blob: 07dac09d8660f372a9b92433d6afd711b8d841c8 [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")
Neal Norwitz26f42f62003-03-20 04:33:16 +000010except IOError:
Benjamin Petersone549ead2009-03-28 21:42:05 +000011 raise unittest.SkipTest("Unable to open /dev/tty")
Florent Xicluna39d795d2010-08-08 18:06:13 +000012else:
13 # Skip if another process is in foreground
14 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
15 tty.close()
16 rpgrp = struct.unpack("i", r)[0]
17 if rpgrp not in (os.getpgrp(), os.getsid(0)):
18 raise unittest.SkipTest("Neither the process group nor the session "
19 "are attached to /dev/tty")
20 del tty, r, rpgrp
Neal Norwitz26f42f62003-03-20 04:33:16 +000021
Christian Heimese25f35e2008-03-20 10:49:03 +000022try:
23 import pty
24except ImportError:
25 pty = None
26
Michael W. Hudsonf0089982003-03-03 12:29:42 +000027class IoctlTests(unittest.TestCase):
28 def test_ioctl(self):
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000029 # If this process has been put into the background, TIOCGPGRP returns
30 # the session ID instead of the process group id.
31 ids = (os.getpgrp(), os.getsid(0))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000032 tty = open("/dev/tty", "r")
33 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000034 rpgrp = struct.unpack("i", r)[0]
Benjamin Peterson577473f2010-01-19 00:09:57 +000035 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000036
37 def test_ioctl_mutate(self):
38 import array
39 buf = array.array('i', [0])
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000040 ids = (os.getpgrp(), os.getsid(0))
Michael W. Hudsonf0089982003-03-03 12:29:42 +000041 tty = open("/dev/tty", "r")
42 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000043 rpgrp = buf[0]
Michael W. Hudsonf0089982003-03-03 12:29:42 +000044 self.assertEquals(r, 0)
Benjamin Peterson577473f2010-01-19 00:09:57 +000045 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000046
Christian Heimese25f35e2008-03-20 10:49:03 +000047 def test_ioctl_signed_unsigned_code_param(self):
48 if not pty:
Benjamin Petersone549ead2009-03-28 21:42:05 +000049 raise unittest.SkipTest('pty module required')
Christian Heimese25f35e2008-03-20 10:49:03 +000050 mfd, sfd = pty.openpty()
51 try:
52 if termios.TIOCSWINSZ < 0:
53 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
54 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff
55 else:
56 set_winsz_opcode_pos = termios.TIOCSWINSZ
57 set_winsz_opcode_maybe_neg, = struct.unpack("i",
58 struct.pack("I", termios.TIOCSWINSZ))
59
Christian Heimese25f35e2008-03-20 10:49:03 +000060 our_winsz = struct.pack("HHHH",80,25,0,0)
61 # test both with a positive and potentially negative ioctl code
62 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
63 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
Christian Heimese25f35e2008-03-20 10:49:03 +000064 finally:
65 os.close(mfd)
66 os.close(sfd)
67
Michael W. Hudsonf0089982003-03-03 12:29:42 +000068def test_main():
69 run_unittest(IoctlTests)
70
71if __name__ == "__main__":
72 test_main()