ยปCore Development>Code coverage>Lib/test/test_ioctl.py

Python code coverage for Lib/test/test_ioctl.py

#countcontent
1n/aimport array
2n/aimport unittest
3n/afrom test.support import import_module, get_attribute
4n/aimport os, struct
5n/afcntl = import_module('fcntl')
6n/atermios = import_module('termios')
7n/aget_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
8n/a
9n/atry:
10n/a tty = open("/dev/tty", "rb")
11n/aexcept OSError:
12n/a raise unittest.SkipTest("Unable to open /dev/tty")
13n/aelse:
14n/a # Skip if another process is in foreground
15n/a r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
16n/a tty.close()
17n/a rpgrp = struct.unpack("i", r)[0]
18n/a if rpgrp not in (os.getpgrp(), os.getsid(0)):
19n/a raise unittest.SkipTest("Neither the process group nor the session "
20n/a "are attached to /dev/tty")
21n/a del tty, r, rpgrp
22n/a
23n/atry:
24n/a import pty
25n/aexcept ImportError:
26n/a pty = None
27n/a
28n/aclass IoctlTests(unittest.TestCase):
29n/a def test_ioctl(self):
30n/a # If this process has been put into the background, TIOCGPGRP returns
31n/a # the session ID instead of the process group id.
32n/a ids = (os.getpgrp(), os.getsid(0))
33n/a with open("/dev/tty", "rb") as tty:
34n/a r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
35n/a rpgrp = struct.unpack("i", r)[0]
36n/a self.assertIn(rpgrp, ids)
37n/a
38n/a def _check_ioctl_mutate_len(self, nbytes=None):
39n/a buf = array.array('i')
40n/a intsize = buf.itemsize
41n/a ids = (os.getpgrp(), os.getsid(0))
42n/a # A fill value unlikely to be in `ids`
43n/a fill = -12345
44n/a if nbytes is not None:
45n/a # Extend the buffer so that it is exactly `nbytes` bytes long
46n/a buf.extend([fill] * (nbytes // intsize))
47n/a self.assertEqual(len(buf) * intsize, nbytes) # sanity check
48n/a else:
49n/a buf.append(fill)
50n/a with open("/dev/tty", "rb") as tty:
51n/a r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
52n/a rpgrp = buf[0]
53n/a self.assertEqual(r, 0)
54n/a self.assertIn(rpgrp, ids)
55n/a
56n/a def test_ioctl_mutate(self):
57n/a self._check_ioctl_mutate_len()
58n/a
59n/a def test_ioctl_mutate_1024(self):
60n/a # Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be
61n/a # copied back after the system call.
62n/a self._check_ioctl_mutate_len(1024)
63n/a
64n/a def test_ioctl_mutate_2048(self):
65n/a # Test with a larger buffer, just for the record.
66n/a self._check_ioctl_mutate_len(2048)
67n/a
68n/a def test_ioctl_signed_unsigned_code_param(self):
69n/a if not pty:
70n/a raise unittest.SkipTest('pty module required')
71n/a mfd, sfd = pty.openpty()
72n/a try:
73n/a if termios.TIOCSWINSZ < 0:
74n/a set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
75n/a set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff
76n/a else:
77n/a set_winsz_opcode_pos = termios.TIOCSWINSZ
78n/a set_winsz_opcode_maybe_neg, = struct.unpack("i",
79n/a struct.pack("I", termios.TIOCSWINSZ))
80n/a
81n/a our_winsz = struct.pack("HHHH",80,25,0,0)
82n/a # test both with a positive and potentially negative ioctl code
83n/a new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
84n/a new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
85n/a finally:
86n/a os.close(mfd)
87n/a os.close(sfd)
88n/a
89n/a
90n/aif __name__ == "__main__":
91n/a unittest.main()