| 1 |
import array |
| 2 |
import os |
| 3 |
import struct |
| 4 |
import sys |
| 5 |
import threading |
| 6 |
import unittest |
| 7 |
from test import support |
| 8 |
from test.support import os_helper, threading_helper |
| 9 |
from test.support.import_helper import import_module |
| 10 |
fcntl = import_module('fcntl') |
| 11 |
termios = import_module('termios') |
| 12 |
|
| 13 |
class IoctlTestsTty(unittest.TestCase): |
| 14 |
@classmethod |
| 15 |
def setUpClass(cls): |
| 16 |
TIOCGPGRP = support.get_attribute(termios, 'TIOCGPGRP') |
| 17 |
try: |
| 18 |
tty = open("/dev/tty", "rb") |
| 19 |
except OSError: |
| 20 |
raise unittest.SkipTest("Unable to open /dev/tty") |
| 21 |
with tty: |
| 22 |
# Skip if another process is in foreground |
| 23 |
r = fcntl.ioctl(tty, TIOCGPGRP, struct.pack("i", 0)) |
| 24 |
rpgrp = struct.unpack("i", r)[0] |
| 25 |
if rpgrp not in (os.getpgrp(), os.getsid(0)): |
| 26 |
raise unittest.SkipTest("Neither the process group nor the session " |
| 27 |
"are attached to /dev/tty") |
| 28 |
|
| 29 |
def test_ioctl_immutable_buf(self): |
| 30 |
# If this process has been put into the background, TIOCGPGRP returns |
| 31 |
# the session ID instead of the process group id. |
| 32 |
ids = (os.getpgrp(), os.getsid(0)) |
| 33 |
with open("/dev/tty", "rb") as tty: |
| 34 |
# string |
| 35 |
buf = " "*8 |
| 36 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf) |
| 37 |
self.assertIsInstance(r, bytes) |
| 38 |
rpgrp = memoryview(r).cast('i')[0] |
| 39 |
self.assertIn(rpgrp, ids) |
| 40 |
|
| 41 |
# bytes |
| 42 |
buf = b" "*8 |
| 43 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf) |
| 44 |
self.assertIsInstance(r, bytes) |
| 45 |
rpgrp = memoryview(r).cast('i')[0] |
| 46 |
self.assertIn(rpgrp, ids) |
| 47 |
|
| 48 |
# read-only buffer |
| 49 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, memoryview(buf)) |
| 50 |
self.assertIsInstance(r, bytes) |
| 51 |
rpgrp = memoryview(r).cast('i')[0] |
| 52 |
self.assertIn(rpgrp, ids) |
| 53 |
|
| 54 |
def test_ioctl_mutable_buf(self): |
| 55 |
ids = (os.getpgrp(), os.getsid(0)) |
| 56 |
with open("/dev/tty", "rb") as tty: |
| 57 |
buf = bytearray(b" "*8) |
| 58 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf) |
| 59 |
self.assertEqual(r, 0) |
| 60 |
rpgrp = memoryview(buf).cast('i')[0] |
| 61 |
self.assertIn(rpgrp, ids) |
| 62 |
|
| 63 |
def test_ioctl_no_mutate_buf(self): |
| 64 |
ids = (os.getpgrp(), os.getsid(0)) |
| 65 |
with open("/dev/tty", "rb") as tty: |
| 66 |
buf = bytearray(b" "*8) |
| 67 |
save_buf = bytes(buf) |
| 68 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, False) |
| 69 |
self.assertEqual(bytes(buf), save_buf) |
| 70 |
self.assertIsInstance(r, bytes) |
| 71 |
rpgrp = memoryview(r).cast('i')[0] |
| 72 |
self.assertIn(rpgrp, ids) |
| 73 |
|
| 74 |
def _create_int_buf(self, nbytes=None): |
| 75 |
buf = array.array('i') |
| 76 |
intsize = buf.itemsize |
| 77 |
# A fill value unlikely to be in `ids` |
| 78 |
fill = -12345 |
| 79 |
if nbytes is not None: |
| 80 |
# Extend the buffer so that it is exactly `nbytes` bytes long |
| 81 |
buf.extend([fill] * (nbytes // intsize)) |
| 82 |
self.assertEqual(len(buf) * intsize, nbytes) # sanity check |
| 83 |
else: |
| 84 |
buf.append(fill) |
| 85 |
return buf |
| 86 |
|
| 87 |
def _check_ioctl_mutate_len(self, nbytes=None): |
| 88 |
ids = (os.getpgrp(), os.getsid(0)) |
| 89 |
buf = self._create_int_buf(nbytes) |
| 90 |
with open("/dev/tty", "rb") as tty: |
| 91 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf) |
| 92 |
rpgrp = buf[0] |
| 93 |
self.assertEqual(r, 0) |
| 94 |
self.assertIn(rpgrp, ids) |
| 95 |
|
| 96 |
def _check_ioctl_not_mutate_len(self, nbytes=None): |
| 97 |
ids = (os.getpgrp(), os.getsid(0)) |
| 98 |
buf = self._create_int_buf(nbytes) |
| 99 |
save_buf = bytes(buf) |
| 100 |
with open("/dev/tty", "rb") as tty: |
| 101 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, False) |
| 102 |
self.assertIsInstance(r, bytes) |
| 103 |
self.assertEqual(len(r), len(save_buf)) |
| 104 |
self.assertEqual(bytes(buf), save_buf) |
| 105 |
rpgrp = array.array('i', r)[0] |
| 106 |
rpgrp = memoryview(r).cast('i')[0] |
| 107 |
self.assertIn(rpgrp, ids) |
| 108 |
|
| 109 |
buf = bytes(buf) |
| 110 |
with open("/dev/tty", "rb") as tty: |
| 111 |
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, True) |
| 112 |
self.assertIsInstance(r, bytes) |
| 113 |
self.assertEqual(len(r), len(save_buf)) |
| 114 |
self.assertEqual(buf, save_buf) |
| 115 |
rpgrp = array.array('i', r)[0] |
| 116 |
rpgrp = memoryview(r).cast('i')[0] |
| 117 |
self.assertIn(rpgrp, ids) |
| 118 |
|
| 119 |
def test_ioctl_mutate(self): |
| 120 |
self._check_ioctl_mutate_len() |
| 121 |
self._check_ioctl_not_mutate_len() |
| 122 |
|
| 123 |
def test_ioctl_mutate_1024(self): |
| 124 |
# Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be |
| 125 |
# copied back after the system call. |
| 126 |
self._check_ioctl_mutate_len(1024) |
| 127 |
self._check_ioctl_not_mutate_len(1024) |
| 128 |
|
| 129 |
def test_ioctl_mutate_2048(self): |
| 130 |
# Test with a larger buffer, just for the record. |
| 131 |
self._check_ioctl_mutate_len(2048) |
| 132 |
self.assertRaises(ValueError, self._check_ioctl_not_mutate_len, 2048) |
| 133 |
|
| 134 |
|
| 135 |
@unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()") |
| 136 |
class IoctlTestsPty(unittest.TestCase): |
| 137 |
def setUp(self): |
| 138 |
self.master_fd, self.slave_fd = os.openpty() |
| 139 |
self.addCleanup(os.close, self.slave_fd) |
| 140 |
self.addCleanup(os.close, self.master_fd) |
| 141 |
|
| 142 |
@unittest.skipUnless(hasattr(termios, 'TCFLSH'), 'requires termios.TCFLSH') |
| 143 |
def test_ioctl_clear_input_or_output(self): |
| 144 |
wfd = self.slave_fd |
| 145 |
rfd = self.master_fd |
| 146 |
# The data is buffered in the input buffer on Linux, and in |
| 147 |
# the output buffer on other platforms. |
| 148 |
inbuf = sys.platform in ('linux', 'android') |
| 149 |
|
| 150 |
os.write(wfd, b'abcdef') |
| 151 |
self.assertEqual(os.read(rfd, 2), b'ab') |
| 152 |
if inbuf: |
| 153 |
# don't flush input |
| 154 |
fcntl.ioctl(rfd, termios.TCFLSH, termios.TCOFLUSH) |
| 155 |
else: |
| 156 |
# don't flush output |
| 157 |
fcntl.ioctl(wfd, termios.TCFLSH, termios.TCIFLUSH) |
| 158 |
self.assertEqual(os.read(rfd, 2), b'cd') |
| 159 |
if inbuf: |
| 160 |
# flush input |
| 161 |
fcntl.ioctl(rfd, termios.TCFLSH, termios.TCIFLUSH) |
| 162 |
else: |
| 163 |
# flush output |
| 164 |
fcntl.ioctl(wfd, termios.TCFLSH, termios.TCOFLUSH) |
| 165 |
os.write(wfd, b'ABCDEF') |
| 166 |
self.assertEqual(os.read(rfd, 1024), b'ABCDEF') |
| 167 |
|
| 168 |
@support.skip_android_selinux('tcflow') |
| 169 |
@unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux') |
| 170 |
@unittest.skipUnless(hasattr(termios, 'TCXONC'), 'requires termios.TCXONC') |
| 171 |
def test_ioctl_suspend_and_resume_output(self): |
| 172 |
wfd = self.slave_fd |
| 173 |
rfd = self.master_fd |
| 174 |
write_suspended = threading.Event() |
| 175 |
write_finished = threading.Event() |
| 176 |
|
| 177 |
def writer(): |
| 178 |
os.write(wfd, b'abc') |
| 179 |
self.assertTrue(write_suspended.wait(support.SHORT_TIMEOUT)) |
| 180 |
os.write(wfd, b'def') |
| 181 |
write_finished.set() |
| 182 |
|
| 183 |
with threading_helper.start_threads([threading.Thread(target=writer)]): |
| 184 |
self.assertEqual(os.read(rfd, 3), b'abc') |
| 185 |
try: |
| 186 |
try: |
| 187 |
fcntl.ioctl(wfd, termios.TCXONC, termios.TCOOFF) |
| 188 |
finally: |
| 189 |
write_suspended.set() |
| 190 |
self.assertFalse(write_finished.wait(0.5), |
| 191 |
'output was not suspended') |
| 192 |
finally: |
| 193 |
fcntl.ioctl(wfd, termios.TCXONC, termios.TCOON) |
| 194 |
self.assertTrue(write_finished.wait(support.SHORT_TIMEOUT), |
| 195 |
'output was not resumed') |
| 196 |
self.assertEqual(os.read(rfd, 1024), b'def') |
| 197 |
|
| 198 |
def test_ioctl_set_window_size(self): |
| 199 |
# (rows, columns, xpixel, ypixel) |
| 200 |
our_winsz = struct.pack("HHHH", 20, 40, 0, 0) |
| 201 |
result = fcntl.ioctl(self.master_fd, termios.TIOCSWINSZ, our_winsz) |
| 202 |
new_winsz = struct.unpack("HHHH", result) |
| 203 |
self.assertEqual(new_winsz[:2], (20, 40)) |
| 204 |
|
| 205 |
@unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE') |
| 206 |
def test_bad_fd(self): |
| 207 |
# gh-134744: Test error handling |
| 208 |
fd = os_helper.make_bad_fd() |
| 209 |
with self.assertRaises(OSError): |
| 210 |
fcntl.ioctl(fd, fcntl.FICLONE, fd) |
| 211 |
with self.assertRaises(OSError): |
| 212 |
fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 1024) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
unittest.main() |