move sections
[python/dscho.git] / Lib / test / test_ioctl.py
blob7fc9c083add73a76dc0707ea111ec7d7c26c7300
1 import unittest
2 from test.test_support import run_unittest, import_module, get_attribute
3 import os, struct
4 fcntl = import_module('fcntl')
5 termios = import_module('termios')
6 get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
8 try:
9 tty = open("/dev/tty", "r")
10 tty.close()
11 except IOError:
12 raise unittest.SkipTest("Unable to open /dev/tty")
14 try:
15 import pty
16 except ImportError:
17 pty = None
19 class IoctlTests(unittest.TestCase):
20 def test_ioctl(self):
21 # 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))
24 tty = open("/dev/tty", "r")
25 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
26 rpgrp = struct.unpack("i", r)[0]
27 self.assertIn(rpgrp, ids)
29 def test_ioctl_mutate(self):
30 import array
31 buf = array.array('i', [0])
32 ids = (os.getpgrp(), os.getsid(0))
33 tty = open("/dev/tty", "r")
34 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
35 rpgrp = buf[0]
36 self.assertEquals(r, 0)
37 self.assertIn(rpgrp, ids)
39 def test_ioctl_signed_unsigned_code_param(self):
40 if not pty:
41 raise unittest.SkipTest('pty module required')
42 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 & 0xffffffffL
47 else:
48 set_winsz_opcode_pos = termios.TIOCSWINSZ
49 set_winsz_opcode_maybe_neg, = struct.unpack("i",
50 struct.pack("I", termios.TIOCSWINSZ))
52 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)
56 finally:
57 os.close(mfd)
58 os.close(sfd)
60 def test_main():
61 run_unittest(IoctlTests)
63 if __name__ == "__main__":
64 test_main()