summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorDima Tisnek <dimaqq@gmail.com>2018-12-17 13:07:55 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-12-17 13:07:55 (GMT)
commite991270363435da12049ecfe70bb69bd9c14b535 (patch)
tree538e8f22e498cee250e54230e304ae11668cfab0 /Lib
parent05c1b387f1cad3d3d005bb98ad42b1e31eb9e379 (diff)
downloadcpython-e991270363435da12049ecfe70bb69bd9c14b535.zip
cpython-e991270363435da12049ecfe70bb69bd9c14b535.tar.gz
cpython-e991270363435da12049ecfe70bb69bd9c14b535.tar.bz2
bpo-35415: validate fileno argument to socket.socket (GH-10917)
https://bugs.python.org/issue35415
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_socket.py49
1 files changed, 44 insertions, 5 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 626a077..bfbd1cc 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1700,7 +1700,6 @@ class GeneralModuleTests(unittest.TestCase):
s.setblocking(False)
self.assertEqual(s.type, socket.SOCK_STREAM)
- @unittest.skipIf(os.name == 'nt', 'Will not work on Windows')
def test_unknown_socket_family_repr(self):
# Test that when created with a family that's not one of the known
# AF_*/SOCK_* constants, socket.family just returns the number.
@@ -1708,10 +1707,8 @@ class GeneralModuleTests(unittest.TestCase):
# To do this we fool socket.socket into believing it already has an
# open fd because on this path it doesn't actually verify the family and
# type and populates the socket object.
- #
- # On Windows this trick won't work, so the test is skipped.
- fd, path = tempfile.mkstemp()
- self.addCleanup(os.unlink, path)
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ fd = sock.detach()
unknown_family = max(socket.AddressFamily.__members__.values()) + 1
unknown_type = max(
@@ -1785,6 +1782,48 @@ class GeneralModuleTests(unittest.TestCase):
s.bind(os.path.join(tmpdir, 'socket'))
self._test_socket_fileno(s, socket.AF_UNIX, socket.SOCK_STREAM)
+ def test_socket_fileno_rejects_float(self):
+ with self.assertRaisesRegex(TypeError, "integer argument expected"):
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=42.5)
+
+ def test_socket_fileno_rejects_other_types(self):
+ with self.assertRaisesRegex(TypeError, "integer is required"):
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno="foo")
+
+ def test_socket_fileno_rejects_invalid_socket(self):
+ with self.assertRaisesRegex(ValueError, "negative file descriptor"):
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=-1)
+
+ @unittest.skipIf(os.name == "nt", "Windows disallows -1 only")
+ def test_socket_fileno_rejects_negative(self):
+ with self.assertRaisesRegex(ValueError, "negative file descriptor"):
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=-42)
+
+ def test_socket_fileno_requires_valid_fd(self):
+ WSAENOTSOCK = 10038
+ with self.assertRaises(OSError) as cm:
+ socket.socket(fileno=support.make_bad_fd())
+ self.assertIn(cm.exception.errno, (errno.EBADF, WSAENOTSOCK))
+
+ with self.assertRaises(OSError) as cm:
+ socket.socket(
+ socket.AF_INET,
+ socket.SOCK_STREAM,
+ fileno=support.make_bad_fd())
+ self.assertIn(cm.exception.errno, (errno.EBADF, WSAENOTSOCK))
+
+ def test_socket_fileno_requires_socket_fd(self):
+ with tempfile.NamedTemporaryFile() as afile:
+ with self.assertRaises(OSError):
+ socket.socket(fileno=afile.fileno())
+
+ with self.assertRaises(OSError) as cm:
+ socket.socket(
+ socket.AF_INET,
+ socket.SOCK_STREAM,
+ fileno=afile.fileno())
+ self.assertEqual(cm.exception.errno, errno.ENOTSOCK)
+
@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
class BasicCANTest(unittest.TestCase):