PageRenderTime 282ms CodeModel.GetById 5ms app.highlight 239ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_socket.py

https://bitbucket.org/benoitbryon/cpython
Python | 4817 lines | 4524 code | 149 blank | 144 comment | 41 complexity | a3f8f50fd66dcdf387667daf7e8058e9 MD5 | raw file
   1#!/usr/bin/env python3
   2
   3import unittest
   4from test import support
   5
   6import errno
   7import io
   8import socket
   9import select
  10import tempfile
  11import _testcapi
  12import time
  13import traceback
  14import queue
  15import sys
  16import os
  17import array
  18import platform
  19import contextlib
  20from weakref import proxy
  21import signal
  22import math
  23import pickle
  24import struct
  25try:
  26    import fcntl
  27except ImportError:
  28    fcntl = False
  29try:
  30    import multiprocessing
  31except ImportError:
  32    multiprocessing = False
  33
  34HOST = support.HOST
  35MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return
  36
  37try:
  38    import _thread as thread
  39    import threading
  40except ImportError:
  41    thread = None
  42    threading = None
  43
  44def _have_socket_can():
  45    """Check whether CAN sockets are supported on this host."""
  46    try:
  47        s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
  48    except (AttributeError, socket.error, OSError):
  49        return False
  50    else:
  51        s.close()
  52    return True
  53
  54def _have_socket_rds():
  55    """Check whether RDS sockets are supported on this host."""
  56    try:
  57        s = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0)
  58    except (AttributeError, OSError):
  59        return False
  60    else:
  61        s.close()
  62    return True
  63
  64HAVE_SOCKET_CAN = _have_socket_can()
  65
  66HAVE_SOCKET_RDS = _have_socket_rds()
  67
  68# Size in bytes of the int type
  69SIZEOF_INT = array.array("i").itemsize
  70
  71class SocketTCPTest(unittest.TestCase):
  72
  73    def setUp(self):
  74        self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75        self.port = support.bind_port(self.serv)
  76        self.serv.listen(1)
  77
  78    def tearDown(self):
  79        self.serv.close()
  80        self.serv = None
  81
  82class SocketUDPTest(unittest.TestCase):
  83
  84    def setUp(self):
  85        self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  86        self.port = support.bind_port(self.serv)
  87
  88    def tearDown(self):
  89        self.serv.close()
  90        self.serv = None
  91
  92class ThreadSafeCleanupTestCase(unittest.TestCase):
  93    """Subclass of unittest.TestCase with thread-safe cleanup methods.
  94
  95    This subclass protects the addCleanup() and doCleanups() methods
  96    with a recursive lock.
  97    """
  98
  99    if threading:
 100        def __init__(self, *args, **kwargs):
 101            super().__init__(*args, **kwargs)
 102            self._cleanup_lock = threading.RLock()
 103
 104        def addCleanup(self, *args, **kwargs):
 105            with self._cleanup_lock:
 106                return super().addCleanup(*args, **kwargs)
 107
 108        def doCleanups(self, *args, **kwargs):
 109            with self._cleanup_lock:
 110                return super().doCleanups(*args, **kwargs)
 111
 112class SocketCANTest(unittest.TestCase):
 113
 114    """To be able to run this test, a `vcan0` CAN interface can be created with
 115    the following commands:
 116    # modprobe vcan
 117    # ip link add dev vcan0 type vcan
 118    # ifconfig vcan0 up
 119    """
 120    interface = 'vcan0'
 121    bufsize = 128
 122
 123    def setUp(self):
 124        self.s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
 125        self.addCleanup(self.s.close)
 126        try:
 127            self.s.bind((self.interface,))
 128        except socket.error:
 129            self.skipTest('network interface `%s` does not exist' %
 130                           self.interface)
 131
 132
 133class SocketRDSTest(unittest.TestCase):
 134
 135    """To be able to run this test, the `rds` kernel module must be loaded:
 136    # modprobe rds
 137    """
 138    bufsize = 8192
 139
 140    def setUp(self):
 141        self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0)
 142        self.addCleanup(self.serv.close)
 143        try:
 144            self.port = support.bind_port(self.serv)
 145        except OSError:
 146            self.skipTest('unable to bind RDS socket')
 147
 148
 149class ThreadableTest:
 150    """Threadable Test class
 151
 152    The ThreadableTest class makes it easy to create a threaded
 153    client/server pair from an existing unit test. To create a
 154    new threaded class from an existing unit test, use multiple
 155    inheritance:
 156
 157        class NewClass (OldClass, ThreadableTest):
 158            pass
 159
 160    This class defines two new fixture functions with obvious
 161    purposes for overriding:
 162
 163        clientSetUp ()
 164        clientTearDown ()
 165
 166    Any new test functions within the class must then define
 167    tests in pairs, where the test name is preceeded with a
 168    '_' to indicate the client portion of the test. Ex:
 169
 170        def testFoo(self):
 171            # Server portion
 172
 173        def _testFoo(self):
 174            # Client portion
 175
 176    Any exceptions raised by the clients during their tests
 177    are caught and transferred to the main thread to alert
 178    the testing framework.
 179
 180    Note, the server setup function cannot call any blocking
 181    functions that rely on the client thread during setup,
 182    unless serverExplicitReady() is called just before
 183    the blocking call (such as in setting up a client/server
 184    connection and performing the accept() in setUp().
 185    """
 186
 187    def __init__(self):
 188        # Swap the true setup function
 189        self.__setUp = self.setUp
 190        self.__tearDown = self.tearDown
 191        self.setUp = self._setUp
 192        self.tearDown = self._tearDown
 193
 194    def serverExplicitReady(self):
 195        """This method allows the server to explicitly indicate that
 196        it wants the client thread to proceed. This is useful if the
 197        server is about to execute a blocking routine that is
 198        dependent upon the client thread during its setup routine."""
 199        self.server_ready.set()
 200
 201    def _setUp(self):
 202        self.server_ready = threading.Event()
 203        self.client_ready = threading.Event()
 204        self.done = threading.Event()
 205        self.queue = queue.Queue(1)
 206        self.server_crashed = False
 207
 208        # Do some munging to start the client test.
 209        methodname = self.id()
 210        i = methodname.rfind('.')
 211        methodname = methodname[i+1:]
 212        test_method = getattr(self, '_' + methodname)
 213        self.client_thread = thread.start_new_thread(
 214            self.clientRun, (test_method,))
 215
 216        try:
 217            self.__setUp()
 218        except:
 219            self.server_crashed = True
 220            raise
 221        finally:
 222            self.server_ready.set()
 223        self.client_ready.wait()
 224
 225    def _tearDown(self):
 226        self.__tearDown()
 227        self.done.wait()
 228
 229        if self.queue.qsize():
 230            exc = self.queue.get()
 231            raise exc
 232
 233    def clientRun(self, test_func):
 234        self.server_ready.wait()
 235        self.clientSetUp()
 236        self.client_ready.set()
 237        if self.server_crashed:
 238            self.clientTearDown()
 239            return
 240        if not hasattr(test_func, '__call__'):
 241            raise TypeError("test_func must be a callable function")
 242        try:
 243            test_func()
 244        except unittest._ExpectedFailure:
 245            # We deliberately ignore expected failures
 246            pass
 247        except BaseException as e:
 248            self.queue.put(e)
 249        finally:
 250            self.clientTearDown()
 251
 252    def clientSetUp(self):
 253        raise NotImplementedError("clientSetUp must be implemented.")
 254
 255    def clientTearDown(self):
 256        self.done.set()
 257        thread.exit()
 258
 259class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest):
 260
 261    def __init__(self, methodName='runTest'):
 262        SocketTCPTest.__init__(self, methodName=methodName)
 263        ThreadableTest.__init__(self)
 264
 265    def clientSetUp(self):
 266        self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 267
 268    def clientTearDown(self):
 269        self.cli.close()
 270        self.cli = None
 271        ThreadableTest.clientTearDown(self)
 272
 273class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest):
 274
 275    def __init__(self, methodName='runTest'):
 276        SocketUDPTest.__init__(self, methodName=methodName)
 277        ThreadableTest.__init__(self)
 278
 279    def clientSetUp(self):
 280        self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 281
 282    def clientTearDown(self):
 283        self.cli.close()
 284        self.cli = None
 285        ThreadableTest.clientTearDown(self)
 286
 287class ThreadedCANSocketTest(SocketCANTest, ThreadableTest):
 288
 289    def __init__(self, methodName='runTest'):
 290        SocketCANTest.__init__(self, methodName=methodName)
 291        ThreadableTest.__init__(self)
 292
 293    def clientSetUp(self):
 294        self.cli = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
 295        try:
 296            self.cli.bind((self.interface,))
 297        except socket.error:
 298            # skipTest should not be called here, and will be called in the
 299            # server instead
 300            pass
 301
 302    def clientTearDown(self):
 303        self.cli.close()
 304        self.cli = None
 305        ThreadableTest.clientTearDown(self)
 306
 307class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest):
 308
 309    def __init__(self, methodName='runTest'):
 310        SocketRDSTest.__init__(self, methodName=methodName)
 311        ThreadableTest.__init__(self)
 312
 313    def clientSetUp(self):
 314        self.cli = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0)
 315        try:
 316            # RDS sockets must be bound explicitly to send or receive data
 317            self.cli.bind((HOST, 0))
 318            self.cli_addr = self.cli.getsockname()
 319        except OSError:
 320            # skipTest should not be called here, and will be called in the
 321            # server instead
 322            pass
 323
 324    def clientTearDown(self):
 325        self.cli.close()
 326        self.cli = None
 327        ThreadableTest.clientTearDown(self)
 328
 329class SocketConnectedTest(ThreadedTCPSocketTest):
 330    """Socket tests for client-server connection.
 331
 332    self.cli_conn is a client socket connected to the server.  The
 333    setUp() method guarantees that it is connected to the server.
 334    """
 335
 336    def __init__(self, methodName='runTest'):
 337        ThreadedTCPSocketTest.__init__(self, methodName=methodName)
 338
 339    def setUp(self):
 340        ThreadedTCPSocketTest.setUp(self)
 341        # Indicate explicitly we're ready for the client thread to
 342        # proceed and then perform the blocking call to accept
 343        self.serverExplicitReady()
 344        conn, addr = self.serv.accept()
 345        self.cli_conn = conn
 346
 347    def tearDown(self):
 348        self.cli_conn.close()
 349        self.cli_conn = None
 350        ThreadedTCPSocketTest.tearDown(self)
 351
 352    def clientSetUp(self):
 353        ThreadedTCPSocketTest.clientSetUp(self)
 354        self.cli.connect((HOST, self.port))
 355        self.serv_conn = self.cli
 356
 357    def clientTearDown(self):
 358        self.serv_conn.close()
 359        self.serv_conn = None
 360        ThreadedTCPSocketTest.clientTearDown(self)
 361
 362class SocketPairTest(unittest.TestCase, ThreadableTest):
 363
 364    def __init__(self, methodName='runTest'):
 365        unittest.TestCase.__init__(self, methodName=methodName)
 366        ThreadableTest.__init__(self)
 367
 368    def setUp(self):
 369        self.serv, self.cli = socket.socketpair()
 370
 371    def tearDown(self):
 372        self.serv.close()
 373        self.serv = None
 374
 375    def clientSetUp(self):
 376        pass
 377
 378    def clientTearDown(self):
 379        self.cli.close()
 380        self.cli = None
 381        ThreadableTest.clientTearDown(self)
 382
 383
 384# The following classes are used by the sendmsg()/recvmsg() tests.
 385# Combining, for instance, ConnectedStreamTestMixin and TCPTestBase
 386# gives a drop-in replacement for SocketConnectedTest, but different
 387# address families can be used, and the attributes serv_addr and
 388# cli_addr will be set to the addresses of the endpoints.
 389
 390class SocketTestBase(unittest.TestCase):
 391    """A base class for socket tests.
 392
 393    Subclasses must provide methods newSocket() to return a new socket
 394    and bindSock(sock) to bind it to an unused address.
 395
 396    Creates a socket self.serv and sets self.serv_addr to its address.
 397    """
 398
 399    def setUp(self):
 400        self.serv = self.newSocket()
 401        self.bindServer()
 402
 403    def bindServer(self):
 404        """Bind server socket and set self.serv_addr to its address."""
 405        self.bindSock(self.serv)
 406        self.serv_addr = self.serv.getsockname()
 407
 408    def tearDown(self):
 409        self.serv.close()
 410        self.serv = None
 411
 412
 413class SocketListeningTestMixin(SocketTestBase):
 414    """Mixin to listen on the server socket."""
 415
 416    def setUp(self):
 417        super().setUp()
 418        self.serv.listen(1)
 419
 420
 421class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase,
 422                              ThreadableTest):
 423    """Mixin to add client socket and allow client/server tests.
 424
 425    Client socket is self.cli and its address is self.cli_addr.  See
 426    ThreadableTest for usage information.
 427    """
 428
 429    def __init__(self, *args, **kwargs):
 430        super().__init__(*args, **kwargs)
 431        ThreadableTest.__init__(self)
 432
 433    def clientSetUp(self):
 434        self.cli = self.newClientSocket()
 435        self.bindClient()
 436
 437    def newClientSocket(self):
 438        """Return a new socket for use as client."""
 439        return self.newSocket()
 440
 441    def bindClient(self):
 442        """Bind client socket and set self.cli_addr to its address."""
 443        self.bindSock(self.cli)
 444        self.cli_addr = self.cli.getsockname()
 445
 446    def clientTearDown(self):
 447        self.cli.close()
 448        self.cli = None
 449        ThreadableTest.clientTearDown(self)
 450
 451
 452class ConnectedStreamTestMixin(SocketListeningTestMixin,
 453                               ThreadedSocketTestMixin):
 454    """Mixin to allow client/server stream tests with connected client.
 455
 456    Server's socket representing connection to client is self.cli_conn
 457    and client's connection to server is self.serv_conn.  (Based on
 458    SocketConnectedTest.)
 459    """
 460
 461    def setUp(self):
 462        super().setUp()
 463        # Indicate explicitly we're ready for the client thread to
 464        # proceed and then perform the blocking call to accept
 465        self.serverExplicitReady()
 466        conn, addr = self.serv.accept()
 467        self.cli_conn = conn
 468
 469    def tearDown(self):
 470        self.cli_conn.close()
 471        self.cli_conn = None
 472        super().tearDown()
 473
 474    def clientSetUp(self):
 475        super().clientSetUp()
 476        self.cli.connect(self.serv_addr)
 477        self.serv_conn = self.cli
 478
 479    def clientTearDown(self):
 480        self.serv_conn.close()
 481        self.serv_conn = None
 482        super().clientTearDown()
 483
 484
 485class UnixSocketTestBase(SocketTestBase):
 486    """Base class for Unix-domain socket tests."""
 487
 488    # This class is used for file descriptor passing tests, so we
 489    # create the sockets in a private directory so that other users
 490    # can't send anything that might be problematic for a privileged
 491    # user running the tests.
 492
 493    def setUp(self):
 494        self.dir_path = tempfile.mkdtemp()
 495        self.addCleanup(os.rmdir, self.dir_path)
 496        super().setUp()
 497
 498    def bindSock(self, sock):
 499        path = tempfile.mktemp(dir=self.dir_path)
 500        sock.bind(path)
 501        self.addCleanup(support.unlink, path)
 502
 503class UnixStreamBase(UnixSocketTestBase):
 504    """Base class for Unix-domain SOCK_STREAM tests."""
 505
 506    def newSocket(self):
 507        return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 508
 509
 510class InetTestBase(SocketTestBase):
 511    """Base class for IPv4 socket tests."""
 512
 513    host = HOST
 514
 515    def setUp(self):
 516        super().setUp()
 517        self.port = self.serv_addr[1]
 518
 519    def bindSock(self, sock):
 520        support.bind_port(sock, host=self.host)
 521
 522class TCPTestBase(InetTestBase):
 523    """Base class for TCP-over-IPv4 tests."""
 524
 525    def newSocket(self):
 526        return socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 527
 528class UDPTestBase(InetTestBase):
 529    """Base class for UDP-over-IPv4 tests."""
 530
 531    def newSocket(self):
 532        return socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 533
 534class SCTPStreamBase(InetTestBase):
 535    """Base class for SCTP tests in one-to-one (SOCK_STREAM) mode."""
 536
 537    def newSocket(self):
 538        return socket.socket(socket.AF_INET, socket.SOCK_STREAM,
 539                             socket.IPPROTO_SCTP)
 540
 541
 542class Inet6TestBase(InetTestBase):
 543    """Base class for IPv6 socket tests."""
 544
 545    # Don't use "localhost" here - it may not have an IPv6 address
 546    # assigned to it by default (e.g. in /etc/hosts), and if someone
 547    # has assigned it an IPv4-mapped address, then it's unlikely to
 548    # work with the full IPv6 API.
 549    host = "::1"
 550
 551class UDP6TestBase(Inet6TestBase):
 552    """Base class for UDP-over-IPv6 tests."""
 553
 554    def newSocket(self):
 555        return socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
 556
 557
 558# Test-skipping decorators for use with ThreadableTest.
 559
 560def skipWithClientIf(condition, reason):
 561    """Skip decorated test if condition is true, add client_skip decorator.
 562
 563    If the decorated object is not a class, sets its attribute
 564    "client_skip" to a decorator which will return an empty function
 565    if the test is to be skipped, or the original function if it is
 566    not.  This can be used to avoid running the client part of a
 567    skipped test when using ThreadableTest.
 568    """
 569    def client_pass(*args, **kwargs):
 570        pass
 571    def skipdec(obj):
 572        retval = unittest.skip(reason)(obj)
 573        if not isinstance(obj, type):
 574            retval.client_skip = lambda f: client_pass
 575        return retval
 576    def noskipdec(obj):
 577        if not (isinstance(obj, type) or hasattr(obj, "client_skip")):
 578            obj.client_skip = lambda f: f
 579        return obj
 580    return skipdec if condition else noskipdec
 581
 582
 583def requireAttrs(obj, *attributes):
 584    """Skip decorated test if obj is missing any of the given attributes.
 585
 586    Sets client_skip attribute as skipWithClientIf() does.
 587    """
 588    missing = [name for name in attributes if not hasattr(obj, name)]
 589    return skipWithClientIf(
 590        missing, "don't have " + ", ".join(name for name in missing))
 591
 592
 593def requireSocket(*args):
 594    """Skip decorated test if a socket cannot be created with given arguments.
 595
 596    When an argument is given as a string, will use the value of that
 597    attribute of the socket module, or skip the test if it doesn't
 598    exist.  Sets client_skip attribute as skipWithClientIf() does.
 599    """
 600    err = None
 601    missing = [obj for obj in args if
 602               isinstance(obj, str) and not hasattr(socket, obj)]
 603    if missing:
 604        err = "don't have " + ", ".join(name for name in missing)
 605    else:
 606        callargs = [getattr(socket, obj) if isinstance(obj, str) else obj
 607                    for obj in args]
 608        try:
 609            s = socket.socket(*callargs)
 610        except socket.error as e:
 611            # XXX: check errno?
 612            err = str(e)
 613        else:
 614            s.close()
 615    return skipWithClientIf(
 616        err is not None,
 617        "can't create socket({0}): {1}".format(
 618            ", ".join(str(o) for o in args), err))
 619
 620
 621#######################################################################
 622## Begin Tests
 623
 624class GeneralModuleTests(unittest.TestCase):
 625
 626    def test_repr(self):
 627        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 628        self.addCleanup(s.close)
 629        self.assertTrue(repr(s).startswith("<socket.socket object"))
 630
 631    def test_weakref(self):
 632        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 633        p = proxy(s)
 634        self.assertEqual(p.fileno(), s.fileno())
 635        s.close()
 636        s = None
 637        try:
 638            p.fileno()
 639        except ReferenceError:
 640            pass
 641        else:
 642            self.fail('Socket proxy still exists')
 643
 644    def testSocketError(self):
 645        # Testing socket module exceptions
 646        msg = "Error raising socket exception (%s)."
 647        with self.assertRaises(socket.error, msg=msg % 'socket.error'):
 648            raise socket.error
 649        with self.assertRaises(socket.error, msg=msg % 'socket.herror'):
 650            raise socket.herror
 651        with self.assertRaises(socket.error, msg=msg % 'socket.gaierror'):
 652            raise socket.gaierror
 653
 654    def testSendtoErrors(self):
 655        # Testing that sendto doens't masks failures. See #10169.
 656        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 657        self.addCleanup(s.close)
 658        s.bind(('', 0))
 659        sockname = s.getsockname()
 660        # 2 args
 661        with self.assertRaises(TypeError) as cm:
 662            s.sendto('\u2620', sockname)
 663        self.assertEqual(str(cm.exception),
 664                         "'str' does not support the buffer interface")
 665        with self.assertRaises(TypeError) as cm:
 666            s.sendto(5j, sockname)
 667        self.assertEqual(str(cm.exception),
 668                         "'complex' does not support the buffer interface")
 669        with self.assertRaises(TypeError) as cm:
 670            s.sendto(b'foo', None)
 671        self.assertIn('not NoneType',str(cm.exception))
 672        # 3 args
 673        with self.assertRaises(TypeError) as cm:
 674            s.sendto('\u2620', 0, sockname)
 675        self.assertEqual(str(cm.exception),
 676                         "'str' does not support the buffer interface")
 677        with self.assertRaises(TypeError) as cm:
 678            s.sendto(5j, 0, sockname)
 679        self.assertEqual(str(cm.exception),
 680                         "'complex' does not support the buffer interface")
 681        with self.assertRaises(TypeError) as cm:
 682            s.sendto(b'foo', 0, None)
 683        self.assertIn('not NoneType', str(cm.exception))
 684        with self.assertRaises(TypeError) as cm:
 685            s.sendto(b'foo', 'bar', sockname)
 686        self.assertIn('an integer is required', str(cm.exception))
 687        with self.assertRaises(TypeError) as cm:
 688            s.sendto(b'foo', None, None)
 689        self.assertIn('an integer is required', str(cm.exception))
 690        # wrong number of args
 691        with self.assertRaises(TypeError) as cm:
 692            s.sendto(b'foo')
 693        self.assertIn('(1 given)', str(cm.exception))
 694        with self.assertRaises(TypeError) as cm:
 695            s.sendto(b'foo', 0, sockname, 4)
 696        self.assertIn('(4 given)', str(cm.exception))
 697
 698    def testCrucialConstants(self):
 699        # Testing for mission critical constants
 700        socket.AF_INET
 701        socket.SOCK_STREAM
 702        socket.SOCK_DGRAM
 703        socket.SOCK_RAW
 704        socket.SOCK_RDM
 705        socket.SOCK_SEQPACKET
 706        socket.SOL_SOCKET
 707        socket.SO_REUSEADDR
 708
 709    def testHostnameRes(self):
 710        # Testing hostname resolution mechanisms
 711        hostname = socket.gethostname()
 712        try:
 713            ip = socket.gethostbyname(hostname)
 714        except socket.error:
 715            # Probably name lookup wasn't set up right; skip this test
 716            return
 717        self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.")
 718        try:
 719            hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
 720        except socket.error:
 721            # Probably a similar problem as above; skip this test
 722            return
 723        all_host_names = [hostname, hname] + aliases
 724        fqhn = socket.getfqdn(ip)
 725        if not fqhn in all_host_names:
 726            self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
 727
 728    @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()")
 729    @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()")
 730    def test_sethostname(self):
 731        oldhn = socket.gethostname()
 732        try:
 733            socket.sethostname('new')
 734        except socket.error as e:
 735            if e.errno == errno.EPERM:
 736                self.skipTest("test should be run as root")
 737            else:
 738                raise
 739        try:
 740            # running test as root!
 741            self.assertEqual(socket.gethostname(), 'new')
 742            # Should work with bytes objects too
 743            socket.sethostname(b'bar')
 744            self.assertEqual(socket.gethostname(), 'bar')
 745        finally:
 746            socket.sethostname(oldhn)
 747
 748    @unittest.skipUnless(hasattr(socket, 'if_nameindex'),
 749                         'socket.if_nameindex() not available.')
 750    def testInterfaceNameIndex(self):
 751        interfaces = socket.if_nameindex()
 752        for index, name in interfaces:
 753            self.assertIsInstance(index, int)
 754            self.assertIsInstance(name, str)
 755            # interface indices are non-zero integers
 756            self.assertGreater(index, 0)
 757            _index = socket.if_nametoindex(name)
 758            self.assertIsInstance(_index, int)
 759            self.assertEqual(index, _index)
 760            _name = socket.if_indextoname(index)
 761            self.assertIsInstance(_name, str)
 762            self.assertEqual(name, _name)
 763
 764    @unittest.skipUnless(hasattr(socket, 'if_nameindex'),
 765                         'socket.if_nameindex() not available.')
 766    def testInvalidInterfaceNameIndex(self):
 767        # test nonexistent interface index/name
 768        self.assertRaises(socket.error, socket.if_indextoname, 0)
 769        self.assertRaises(socket.error, socket.if_nametoindex, '_DEADBEEF')
 770        # test with invalid values
 771        self.assertRaises(TypeError, socket.if_nametoindex, 0)
 772        self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
 773
 774    def testRefCountGetNameInfo(self):
 775        # Testing reference count for getnameinfo
 776        if hasattr(sys, "getrefcount"):
 777            try:
 778                # On some versions, this loses a reference
 779                orig = sys.getrefcount(__name__)
 780                socket.getnameinfo(__name__,0)
 781            except TypeError:
 782                if sys.getrefcount(__name__) != orig:
 783                    self.fail("socket.getnameinfo loses a reference")
 784
 785    def testInterpreterCrash(self):
 786        # Making sure getnameinfo doesn't crash the interpreter
 787        try:
 788            # On some versions, this crashes the interpreter.
 789            socket.getnameinfo(('x', 0, 0, 0), 0)
 790        except socket.error:
 791            pass
 792
 793    def testNtoH(self):
 794        # This just checks that htons etc. are their own inverse,
 795        # when looking at the lower 16 or 32 bits.
 796        sizes = {socket.htonl: 32, socket.ntohl: 32,
 797                 socket.htons: 16, socket.ntohs: 16}
 798        for func, size in sizes.items():
 799            mask = (1<<size) - 1
 800            for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
 801                self.assertEqual(i & mask, func(func(i&mask)) & mask)
 802
 803            swapped = func(mask)
 804            self.assertEqual(swapped & mask, mask)
 805            self.assertRaises(OverflowError, func, 1<<34)
 806
 807    def testNtoHErrors(self):
 808        good_values = [ 1, 2, 3, 1, 2, 3 ]
 809        bad_values = [ -1, -2, -3, -1, -2, -3 ]
 810        for k in good_values:
 811            socket.ntohl(k)
 812            socket.ntohs(k)
 813            socket.htonl(k)
 814            socket.htons(k)
 815        for k in bad_values:
 816            self.assertRaises(OverflowError, socket.ntohl, k)
 817            self.assertRaises(OverflowError, socket.ntohs, k)
 818            self.assertRaises(OverflowError, socket.htonl, k)
 819            self.assertRaises(OverflowError, socket.htons, k)
 820
 821    def testGetServBy(self):
 822        eq = self.assertEqual
 823        # Find one service that exists, then check all the related interfaces.
 824        # I've ordered this by protocols that have both a tcp and udp
 825        # protocol, at least for modern Linuxes.
 826        if (sys.platform.startswith(('freebsd', 'netbsd'))
 827            or sys.platform in ('linux', 'darwin')):
 828            # avoid the 'echo' service on this platform, as there is an
 829            # assumption breaking non-standard port/protocol entry
 830            services = ('daytime', 'qotd', 'domain')
 831        else:
 832            services = ('echo', 'daytime', 'domain')
 833        for service in services:
 834            try:
 835                port = socket.getservbyname(service, 'tcp')
 836                break
 837            except socket.error:
 838                pass
 839        else:
 840            raise socket.error
 841        # Try same call with optional protocol omitted
 842        port2 = socket.getservbyname(service)
 843        eq(port, port2)
 844        # Try udp, but don't barf it it doesn't exist
 845        try:
 846            udpport = socket.getservbyname(service, 'udp')
 847        except socket.error:
 848            udpport = None
 849        else:
 850            eq(udpport, port)
 851        # Now make sure the lookup by port returns the same service name
 852        eq(socket.getservbyport(port2), service)
 853        eq(socket.getservbyport(port, 'tcp'), service)
 854        if udpport is not None:
 855            eq(socket.getservbyport(udpport, 'udp'), service)
 856        # Make sure getservbyport does not accept out of range ports.
 857        self.assertRaises(OverflowError, socket.getservbyport, -1)
 858        self.assertRaises(OverflowError, socket.getservbyport, 65536)
 859
 860    def testDefaultTimeout(self):
 861        # Testing default timeout
 862        # The default timeout should initially be None
 863        self.assertEqual(socket.getdefaulttimeout(), None)
 864        s = socket.socket()
 865        self.assertEqual(s.gettimeout(), None)
 866        s.close()
 867
 868        # Set the default timeout to 10, and see if it propagates
 869        socket.setdefaulttimeout(10)
 870        self.assertEqual(socket.getdefaulttimeout(), 10)
 871        s = socket.socket()
 872        self.assertEqual(s.gettimeout(), 10)
 873        s.close()
 874
 875        # Reset the default timeout to None, and see if it propagates
 876        socket.setdefaulttimeout(None)
 877        self.assertEqual(socket.getdefaulttimeout(), None)
 878        s = socket.socket()
 879        self.assertEqual(s.gettimeout(), None)
 880        s.close()
 881
 882        # Check that setting it to an invalid value raises ValueError
 883        self.assertRaises(ValueError, socket.setdefaulttimeout, -1)
 884
 885        # Check that setting it to an invalid type raises TypeError
 886        self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
 887
 888    def testIPv4_inet_aton_fourbytes(self):
 889        if not hasattr(socket, 'inet_aton'):
 890            return  # No inet_aton, nothing to check
 891        # Test that issue1008086 and issue767150 are fixed.
 892        # It must return 4 bytes.
 893        self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0'))
 894        self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255'))
 895
 896    def testIPv4toString(self):
 897        if not hasattr(socket, 'inet_pton'):
 898            return # No inet_pton() on this platform
 899        from socket import inet_aton as f, inet_pton, AF_INET
 900        g = lambda a: inet_pton(AF_INET, a)
 901
 902        assertInvalid = lambda func,a: self.assertRaises(
 903            (socket.error, ValueError), func, a
 904        )
 905
 906        self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0'))
 907        self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0'))
 908        self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170'))
 909        self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4'))
 910        self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255'))
 911        assertInvalid(f, '0.0.0.')
 912        assertInvalid(f, '300.0.0.0')
 913        assertInvalid(f, 'a.0.0.0')
 914        assertInvalid(f, '1.2.3.4.5')
 915        assertInvalid(f, '::1')
 916
 917        self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0'))
 918        self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0'))
 919        self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170'))
 920        self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255'))
 921        assertInvalid(g, '0.0.0.')
 922        assertInvalid(g, '300.0.0.0')
 923        assertInvalid(g, 'a.0.0.0')
 924        assertInvalid(g, '1.2.3.4.5')
 925        assertInvalid(g, '::1')
 926
 927    def testIPv6toString(self):
 928        if not hasattr(socket, 'inet_pton'):
 929            return # No inet_pton() on this platform
 930        try:
 931            from socket import inet_pton, AF_INET6, has_ipv6
 932            if not has_ipv6:
 933                return
 934        except ImportError:
 935            return
 936        f = lambda a: inet_pton(AF_INET6, a)
 937        assertInvalid = lambda a: self.assertRaises(
 938            (socket.error, ValueError), f, a
 939        )
 940
 941        self.assertEqual(b'\x00' * 16, f('::'))
 942        self.assertEqual(b'\x00' * 16, f('0::0'))
 943        self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::'))
 944        self.assertEqual(
 945            b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
 946            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
 947        )
 948        self.assertEqual(
 949            b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02',
 950            f('ad42:abc::127:0:254:2')
 951        )
 952        self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::'))
 953        assertInvalid('0x20::')
 954        assertInvalid(':::')
 955        assertInvalid('::0::')
 956        assertInvalid('1::abc::')
 957        assertInvalid('1::abc::def')
 958        assertInvalid('1:2:3:4:5:6:')
 959        assertInvalid('1:2:3:4:5:6')
 960        assertInvalid('1:2:3:4:5:6:7:8:')
 961        assertInvalid('1:2:3:4:5:6:7:8:0')
 962
 963        self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40',
 964            f('::254.42.23.64')
 965        )
 966        self.assertEqual(
 967            b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40',
 968            f('42::a29b:254.42.23.64')
 969        )
 970        self.assertEqual(
 971            b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40',
 972            f('42:a8b9:0:2:ffff:a29b:254.42.23.64')
 973        )
 974        assertInvalid('255.254.253.252')
 975        assertInvalid('1::260.2.3.0')
 976        assertInvalid('1::0.be.e.0')
 977        assertInvalid('1:2:3:4:5:6:7:1.2.3.4')
 978        assertInvalid('::1.2.3.4:0')
 979        assertInvalid('0.100.200.0:3:4:5:6:7:8')
 980
 981    def testStringToIPv4(self):
 982        if not hasattr(socket, 'inet_ntop'):
 983            return # No inet_ntop() on this platform
 984        from socket import inet_ntoa as f, inet_ntop, AF_INET
 985        g = lambda a: inet_ntop(AF_INET, a)
 986        assertInvalid = lambda func,a: self.assertRaises(
 987            (socket.error, ValueError), func, a
 988        )
 989
 990        self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00'))
 991        self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55'))
 992        self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff'))
 993        self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04'))
 994        assertInvalid(f, b'\x00' * 3)
 995        assertInvalid(f, b'\x00' * 5)
 996        assertInvalid(f, b'\x00' * 16)
 997
 998        self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00'))
 999        self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55'))
1000        self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff'))
1001        assertInvalid(g, b'\x00' * 3)
1002        assertInvalid(g, b'\x00' * 5)
1003        assertInvalid(g, b'\x00' * 16)
1004
1005    def testStringToIPv6(self):
1006        if not hasattr(socket, 'inet_ntop'):
1007            return # No inet_ntop() on this platform
1008        try:
1009            from socket import inet_ntop, AF_INET6, has_ipv6
1010            if not has_ipv6:
1011                return
1012        except ImportError:
1013            return
1014        f = lambda a: inet_ntop(AF_INET6, a)
1015        assertInvalid = lambda a: self.assertRaises(
1016            (socket.error, ValueError), f, a
1017        )
1018
1019        self.assertEqual('::', f(b'\x00' * 16))
1020        self.assertEqual('::1', f(b'\x00' * 15 + b'\x01'))
1021        self.assertEqual(
1022            'aef:b01:506:1001:ffff:9997:55:170',
1023            f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
1024        )
1025
1026        assertInvalid(b'\x12' * 15)
1027        assertInvalid(b'\x12' * 17)
1028        assertInvalid(b'\x12' * 4)
1029
1030    # XXX The following don't test module-level functionality...
1031
1032    def testSockName(self):
1033        # Testing getsockname()
1034        port = support.find_unused_port()
1035        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1036        self.addCleanup(sock.close)
1037        sock.bind(("0.0.0.0", port))
1038        name = sock.getsockname()
1039        # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
1040        # it reasonable to get the host's addr in addition to 0.0.0.0.
1041        # At least for eCos.  This is required for the S/390 to pass.
1042        try:
1043            my_ip_addr = socket.gethostbyname(socket.gethostname())
1044        except socket.error:
1045            # Probably name lookup wasn't set up right; skip this test
1046            return
1047        self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0])
1048        self.assertEqual(name[1], port)
1049
1050    def testGetSockOpt(self):
1051        # Testing getsockopt()
1052        # We know a socket should start without reuse==0
1053        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1054        self.addCleanup(sock.close)
1055        reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
1056        self.assertFalse(reuse != 0, "initial mode is reuse")
1057
1058    def testSetSockOpt(self):
1059        # Testing setsockopt()
1060        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1061        self.addCleanup(sock.close)
1062        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1063        reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
1064        self.assertFalse(reuse == 0, "failed to set reuse mode")
1065
1066    def testSendAfterClose(self):
1067        # testing send() after close() with timeout
1068        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1069        sock.settimeout(1)
1070        sock.close()
1071        self.assertRaises(socket.error, sock.send, b"spam")
1072
1073    def testNewAttributes(self):
1074        # testing .family, .type and .protocol
1075        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1076        self.assertEqual(sock.family, socket.AF_INET)
1077        self.assertEqual(sock.type, socket.SOCK_STREAM)
1078        self.assertEqual(sock.proto, 0)
1079        sock.close()
1080
1081    def test_getsockaddrarg(self):
1082        host = '0.0.0.0'
1083        port = support.find_unused_port()
1084        big_port = port + 65536
1085        neg_port = port - 65536
1086        sock = socket.socket()
1087        try:
1088            self.assertRaises(OverflowError, sock.bind, (host, big_port))
1089            self.assertRaises(OverflowError, sock.bind, (host, neg_port))
1090            sock.bind((host, port))
1091        finally:
1092            sock.close()
1093
1094    @unittest.skipUnless(os.name == "nt", "Windows specific")
1095    def test_sock_ioctl(self):
1096        self.assertTrue(hasattr(socket.socket, 'ioctl'))
1097        self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
1098        self.assertTrue(hasattr(socket, 'RCVALL_ON'))
1099        self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
1100        self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS'))
1101        s = socket.socket()
1102        self.addCleanup(s.close)
1103        self.assertRaises(ValueError, s.ioctl, -1, None)
1104        s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
1105
1106    def testGetaddrinfo(self):
1107        try:
1108            socket.getaddrinfo('localhost', 80)
1109        except socket.gaierror as err:
1110            if err.errno == socket.EAI_SERVICE:
1111                # see http://bugs.python.org/issue1282647
1112                self.skipTest("buggy libc version")
1113            raise
1114        # len of every sequence is supposed to be == 5
1115        for info in socket.getaddrinfo(HOST, None):
1116            self.assertEqual(len(info), 5)
1117        # host can be a domain name, a string representation of an
1118        # IPv4/v6 address or None
1119        socket.getaddrinfo('localhost', 80)
1120        socket.getaddrinfo('127.0.0.1', 80)
1121        socket.getaddrinfo(None, 80)
1122        if support.IPV6_ENABLED:
1123            socket.getaddrinfo('::1', 80)
1124        # port can be a string service name such as "http", a numeric
1125        # port number or None
1126        socket.getaddrinfo(HOST, "http")
1127        socket.getaddrinfo(HOST, 80)
1128        socket.getaddrinfo(HOST, None)
1129        # test family and socktype filters
1130        infos = socket.getaddrinfo(HOST, None, socket.AF_INET)
1131        for family, _, _, _, _ in infos:
1132            self.assertEqual(family, socket.AF_INET)
1133        infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
1134        for _, socktype, _, _, _ in infos:
1135            self.assertEqual(socktype, socket.SOCK_STREAM)
1136        # test proto and flags arguments
1137        socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
1138        socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
1139        # a server willing to support both IPv4 and IPv6 will
1140        # usually do this
1141        socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
1142                           socket.AI_PASSIVE)
1143        # test keyword arguments
1144        a = socket.getaddrinfo(HOST, None)
1145        b = socket.getaddrinfo(host=HOST, port=None)
1146        self.assertEqual(a, b)
1147        a = socket.getaddrinfo(HOST, None, socket.AF_INET)
1148        b = socket.getaddrinfo(HOST, None, family=socket.AF_INET)
1149        self.assertEqual(a, b)
1150        a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
1151        b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM)
1152        self.assertEqual(a, b)
1153        a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
1154        b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP)
1155        self.assertEqual(a, b)
1156        a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
1157        b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE)
1158        self.assertEqual(a, b)
1159        a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
1160                               socket.AI_PASSIVE)
1161        b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC,
1162                               type=socket.SOCK_STREAM, proto=0,
1163                               flags=socket.AI_PASSIVE)
1164        self.assertEqual(a, b)
1165        # Issue #6697.
1166        self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800')
1167
1168    def test_getnameinfo(self):
1169        # only IP addresses are allowed
1170        self.assertRaises(socket.error, socket.getnameinfo, ('mail.python.org',0), 0)
1171
1172    @unittest.skipUnless(support.is_resource_enabled('network'),
1173                         'network is not enabled')
1174    def test_idna(self):
1175        # Check for internet access before running test (issue #12804).
1176        try:
1177            socket.gethostbyname('python.org')
1178        except socket.gaierror as e:
1179            if e.errno == socket.EAI_NODATA:
1180                self.skipTest('internet access required for this test')
1181        # these should all be successful
1182        socket.gethostbyname('испытание.python.org')
1183        socket.gethostbyname_ex('испытание.python.org')
1184        socket.getaddrinfo('испытание.python.org',0,socket.AF_UNSPEC,socket.SOCK_STREAM)
1185        # this may not work if the forward lookup choses the IPv6 address, as that doesn't
1186        # have a reverse entry yet
1187        # socket.gethostbyaddr('испытание.python.org')
1188
1189    def check_sendall_interrupted(self, with_timeout):
1190        # socketpair() is not stricly required, but it makes things easier.
1191        if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'):
1192            self.skipTest("signal.alarm and socket.socketpair required for this test")
1193        # Our signal handlers clobber the C errno by calling a math function
1194        # with an invalid domain value.
1195        def ok_handler(*args):
1196            self.assertRaises(ValueError, math.acosh, 0)
1197        def raising_handler(*args):
1198            self.assertRaises(ValueError, math.acosh, 0)
1199            1 // 0
1200        c, s = socket.socketpair()
1201        old_alarm = signal.signal(signal.SIGALRM, raising_handler)
1202        try:
1203            if with_timeout:
1204                # Just above the one second minimum for signal.alarm
1205                c.settimeout(1.5)
1206            with self.assertRaises(ZeroDivisionError):
1207                signal.alarm(1)
1208                c.sendall(b"x" * (1024**2))
1209            if with_timeout:
1210                signal.signal(signal.SIGALRM, ok_handler)
1211                signal.alarm(1)
1212                self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2))
1213        finally:
1214            signal.signal(signal.SIGALRM, old_alarm)
1215            c.close()
1216            s.close()
1217
1218    def test_sendall_interrupted(self):
1219        self.check_sendall_interrupted(False)
1220
1221    def test_sendall_interrupted_with_timeout(self):
1222        self.check_sendall_interrupted(True)
1223
1224    def test_dealloc_warn(self):
1225        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1226        r = repr(sock)
1227        with self.assertWarns(ResourceWarning) as cm:
1228            sock = None
1229            support.gc_collect()
1230        self.assertIn(r, str(cm.warning.args[0]))
1231        # An open socket file object gets dereferenced after the socket
1232        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1233        f = sock.makefile('rb')
1234        r = repr(sock)
1235        sock = None
1236        support.gc_collect()
1237        with self.assertWarns(ResourceWarning):
1238            f = None
1239            support.gc_collect()
1240
1241    def test_name_closed_socketio(self):
1242        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
1243            fp = sock.makefile("rb")
1244            fp.close()
1245            self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>")
1246
1247    def test_pickle(self):
1248        sock = socket.socket()
1249        with sock:
1250            for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1251                self.assertRaises(TypeError, pickle.dumps, sock, protocol)
1252
1253    def test_listen_backlog0(self):
1254        srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1255        srv.bind((HOST, 0))
1256        # backlog = 0
1257        srv.listen(0)
1258        srv.close()
1259
1260    @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.')
1261    def test_flowinfo(self):
1262        self.assertRaises(OverflowError, socket.getnameinfo,
1263                          ('::1',0, 0xffffffff), 0)
1264        with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
1265            self.assertRaises(OverflowError, s.bind, ('::1', 0, -10))
1266
1267
1268@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
1269class BasicCANTest(unittest.TestCase):
1270
1271    def testCrucialConstants(self):
1272        socket.AF_CAN
1273        socket.PF_CAN
1274        socket.CAN_RAW
1275
1276    def testCreateSocket(self):
1277        with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s:
1278            pass
1279
1280    def testBindAny(self):
1281        with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s:
1282            s.bind(('', ))
1283
1284    def testTooLongInterfaceName(self):
1285        # most systems limit IFNAMSIZ to 16, take 1024 to be sure
1286        with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s:
1287            self.assertRaisesRegex(socket.error, 'interface name too long',
1288                                   s.bind, ('x' * 1024,))
1289
1290    @unittest.skipUnless(hasattr(socket, "CAN_RAW_LOOPBACK"),
1291                         'socket.CAN_RAW_LOOPBACK required for this test.')
1292    def testLoopback(self):
1293        with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s:
1294            for loopback in (0, 1):
1295                s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK,
1296                             loopback)
1297                self.assertEqual(loopback,
1298                    s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK))
1299
1300    @unittest.skipUnless(hasattr(socket, "CAN_RAW_FILTER"),
1301                         'socket.CAN_RAW_FILTER required for this test.')
1302    def testFilter(self):
1303        can_id, can_mask = 0x200, 0x700
1304        can_filter = struct.pack("=II", can_id, can_mask)
1305        with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s:
1306            s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, can_filter)
1307            self.assertEqual(can_filter,
1308                    s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, 8))
1309
1310
1311@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
1312@unittest.skipUnless(thread, 'Threading required for this test.')
1313class CANTest(ThreadedCANSocketTest):
1314
1315    """The CAN frame structure is defined in <linux/can.h>:
1316
1317    struct can_frame {
1318        canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
1319        __u8    can_dlc; /* data length code: 0 .. 8 */
1320        __u8    data[8] __attribute__((aligned(8)));
1321    };
1322    """
1323    can_frame_fmt = "=IB3x8s"
1324
1325    def __init__(self, methodName='runTest'):
1326        ThreadedCANSocketTest.__init__(self, methodName=methodName)
1327
1328    @classmethod
1329    def build_can_frame(cls, can_id, data):
1330        """Build a CAN frame."""
1331        can_dlc = len(data)
1332        data = data.ljust(8, b'\x00')
1333        return struct.pack(cls.can_frame_fmt, can_id, can_dlc, data)
1334
1335    @classmethod
1336    def dissect_can_frame(cls, frame):
1337        """Dissect a CAN frame."""
1338        can_id, can_dlc, data = struct.unpack(cls.can_frame_fmt, frame)
1339        return (can_id, can_dlc, data[:can_dlc])
1340
1341    def testSendFrame(self):
1342        cf, addr = self.s.recvfrom(self.bufsize)
1343        self.assertEqual(self.cf, cf)
1344        self.assertEqual(addr[0], self.interface)
1345        self.assertEqual(addr[1], socket.AF_CAN)
1346
1347    def _testSendFrame(self):
1348        self.cf = self.build_can_frame(0x00, b'\x01\x02\x03\x04\x05')
1349        self.cli.send(self.cf)
1350
1351    def testSendMaxFrame(self):
1352        cf, addr = self.s.recvfrom(self.bufsize)
1353        self.assertEqual(self.cf, cf)
1354
1355    def _testSendMaxFrame(self):
1356        self.cf = self.build_can_frame(0x00, b'\x07' * 8)
1357        self.cli.send(self.cf)
1358
1359    def testSendMultiFrames(self):
1360        cf, addr = self.s.recvfrom(self.bufsize)
1361        self.assertEqual(self.cf1, cf)
1362
1363        cf, addr = self.s.recvfrom(self.bufsize)
1364        self.assertEqual(self.cf2, cf)
1365
1366    def _testSendMultiFrames(self):
1367        self.cf1 = self.build_can_frame(0x07, b'\x44\x33\x22\x11')
1368        self.cli.send(self.cf1)
1369
1370        self.cf2 = self.build_can_frame(0x12, b'\x99\x22\x33')
1371        self.cli.send(self.cf2)
1372
1373
1374@unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.')
1375class BasicRDSTest(unittest.TestCase):
1376
1377    def testCrucialConstants(self):
1378        socket.AF_RDS
1379        socket.PF_RDS
1380
1381    def testCreateSocket(self):
1382        with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s:
1383            pass
1384
1385    def testSocketBufferSize(self):
1386        bufsize = 16384
1387        with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s:
1388            s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, bufsize)
1389            s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, bufsize)
1390
1391
1392@unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.')
1393@unittest.skipUnless(thread, 'Threading required for this test.')
1394class RDSTest(ThreadedRDSSocketTest):
1395
1396    def __init__(self, methodName='runTest'):
1397        ThreadedRDSSocketTest.__init__(self, methodName=methodName)
1398
1399    def setUp(self):
1400        super().setUp()
1401        self.evt = threading.Event()
1402
1403    def testSendAndRecv(self):
1404        data, addr = self.serv.recvfrom(self.bufsize)
1405        self.assertEqual(self.data, data)
1406        self.assertEqual(self.cli_addr, addr)
1407
1408    def _testSendAndRecv(self):
1409        self.data = b'spam'
1410        self.cli.sendto(self.data, 0, (HOST, self.port))
1411
1412    def testPeek(self):
1413        data, addr = self.serv.recvfrom(self.bufsize, socket.MSG_PEEK)
1414        self.assertEqual(self.data, data)
1415        data, addr = self.serv.recvfrom(self.bufsize)
1416        self.assertEqual(self.data, data)
1417
1418    def _testPeek(self):
1419        self.data = b'spam'
1420        self.cli.sendto(self.data, 0, (HOST, self.port))
1421
1422    @requireAttrs(socket.socket, 'recvmsg')
1423    def testSendAndRecvMsg(self):
1424        data, ancdata, msg_flags, addr = self.serv.recvmsg(self.bufsize)
1425        self.assertEqual(self.data, data)
1426
1427    @requireAttrs(socket.socket, 'sendmsg')
1428    def _testSendAndRecvMsg(self):
1429        self.data = b'hello ' * 10
1430        self.cli.sendmsg([self.data], (), 0, (HOST, self.port))
1431
1432    def testSendAndRecvMulti(self):
1433        data, addr = self.serv.recvfrom(self.bufsize)
1434        self.assertEqual(self.data1, data)
1435
1436        data, addr = self.serv.recvfrom(self.bufsize)
1437        self.assertEqual(self.data2, data)
1438
1439    def _testSendAndRecvMulti(self):
1440        self.data1 = b'bacon'
1441        self.cli.sendto(self.data1, 0, (HOST, self.port))
1442
1443        self.data2 = b'egg'
1444        self.cli.sendto(self.data2, 0, (HOST, self.port))
1445
1446    def testSelect(self):
1447        r, w, x = select.select([self.serv], [], [], 3.0)
1448        self.assertIn(self.serv, r)
1449        data, addr = self.serv.recvfrom(self.bufsize)
1450        self.assertEqual(self.data, data)
1451
1452    def _testSelect(self):
1453        self.data = b'select'
1454        self.cli.sendto(self.data, 0, (HOST, self.port))
1455
1456    def testCongestion(self):
1457        # wait until the sender is done
1458        self.evt.wait()
1459
1460    def _testCongestion(self):
1461        # test the behavior in case of congestion
1462        self.data = b'fill'
1463        self.cli.setblocking(False)
1464        try:
1465            # try to lower the receiver's socket buffer size
1466            self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384)
1467        except OSError:
1468            pass
1469        with self.assertRaises(OSError) as cm:
1470            try:
1471                # fill the receiver's socket buffer
1472                while True:
1473                    self.cli.sendto(self.data, 0, (HOST, self.port))
1474            finally:
1475                # signal the receiver we're done
1476                self.evt.set()
1477        # sendto() should have failed with ENOBUFS
1478        self.assertEqual(cm.exception.errno, errno.ENOBUFS)
1479        # and we should have received a congestion notification through poll
1480        r, w, x = select.select([self.serv], [], [], 3.0)
1481        self.assertIn(self.serv, r)
1482
1483
1484@unittest.skipUnless(thread, 'Threading required for this test.')
1485class BasicTCPTest(SocketConnectedTest):
1486
1487    def __init__(self, methodName='runTest'):
1488        SocketConnectedTest.__init__(self, methodName=methodName)
1489
1490    def testRecv(self):
1491        # Testing large receive over TCP
1492        msg = self.cli_conn.recv(1024)
1493        self.assertEqual(msg, MSG)
1494
1495    def _testRecv(self):
1496        self.serv_conn.send(MSG)
1497
1498    def testOverFlowRecv(self):
1499        # Testing receive in chunks over TCP
1500        seg1 = self.cli_conn.recv(len(MSG) - 3)
1501        seg2 = self.cli_conn.recv(1024)
1502        msg = seg1 + seg2
1503        self.assertEqual(msg, MSG)
1504
1505    def _testOverFlowRecv(self):
1506        self.serv_conn.send(MSG)
1507
1508    def testRecvFrom(self):
1509        # Testing large recvfrom() over TCP
1510        msg, addr = self.cli_conn.recvfrom(1024)
1511        self.assertEqual(msg, MSG)
1512
1513    def _testRecvFrom(self):
1514        self.serv_conn.send(MSG)
1515
1516    def testOverFlowRecvFrom(self):
1517        # Testing recvfrom() in chunks over TCP
1518        seg1, addr = self.cli_conn.recvfrom(len(MSG)-3)
1519        seg2, addr = self.cli_conn.recvfrom(1024)
1520        msg = seg1 + seg2
1521        self.assertEqual(msg, MSG)
1522
1523    def _testOverFlowRecvFrom(self):
1524        self.serv_conn.send(MSG)
1525
1526    def testSendAll(self):
1527        # Testing sendall() with a 2048 byte string over TCP
1528        msg = b''
1529        while 1:
1530            read = self.cli_conn.recv(1024)
1531            if not read:
1532                break
1533            msg += read
1534        self.assertEqual(msg, b'f' * 2048)
1535
1536    def _testSendAll(self):
1537        big_chunk = b'f' * 2048
1538        self.serv_conn.sendall(big_chunk)
1539
1540    def testFromFd(self):
1541        # Testing fromfd()
1542        fd = self.cli_conn.fileno()
1543        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
1544        self.addCleanup(sock.close)
1545        self.assertIsInstance(sock, socket.socket)
1546        msg = sock.recv(1024)
1547        self.assertEqual(msg, MSG)
1548
1549    def _testFromFd(self):
1550        self.serv_conn.send(MSG)
1551
1552    def testDup(self):
1553        # Testing dup()
1554        sock = self.cli_conn.dup()
1555        self.addCleanup(sock.close)
1556        msg = sock.recv(1024)
1557        self.assertEqual(msg, MSG)
1558
1559    def _testDup(self):
1560        self.serv_conn.send(MSG)
1561
1562    def testShutdown(self):
1563        # Testing shutdown()
1564        msg = self.cli_conn.recv(1024)
1565        self.assertEqual(msg, MSG)
1566        # wait for _testShutdown to finish: on OS X, when the server
1567        # closes the connection the client also becomes disconnected,
1568        # and the client's shutdown call will fail. (Issue #4397.)
1569        self.done.wait()
1570
1571    def _testShutdown(self):
1572        self.serv_conn.send(MSG)
1573        self.serv_conn.shutdown(2)
1574
1575    def testDetach(self):
1576        # Testing detach()
1577        fileno = self.cli_conn.fileno()
1578        f = self.cli_conn.detach()
1579        self.assertEqual(f, fileno)
1580        # cli_conn cannot be used anymore...
1581        self.assertTrue(self.cli_conn._closed)
1582        self.assertRaises(socket.error, self.cli_conn.recv, 1024)
1583        self.cli_conn.close()
1584        # ...but we can create another socket using the (still open)
1585        # file descriptor
1586        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f)
1587        self.addCleanup(sock.close)
1588        msg = sock.recv(1024)
1589        self.assertEqual(msg, MSG)
1590
1591    def _testDetach(self):
1592        self.serv_conn.send(MSG)
1593
1594@unittest.skipUnless(thread, 'Threading required for this test.')
1595class BasicUDPTest(ThreadedUDPSocketTest):
1596
1597    def __init__(self, methodName='runTest'):
1598        ThreadedUDPSocketTest.__init__(self, methodName=methodName)
1599
1600    def testSendtoAndRecv(self):
1601        # Testing sendto() and Recv() over UDP
1602        msg = self.serv.recv(len(MSG))
1603        self.assertEqual(msg, MSG)
1604
1605    def _testSendtoAndRecv(self):
1606        self.cli.sendto(MSG, 0, (HOST, self.port))
1607
1608    def testRecvFrom(self):
1609        # Testing recvfrom() over UDP
1610        msg, addr = self.serv.recvfrom(len(MSG))
1611        self.assertEqual(msg, MSG)
1612
1613    def _testRecvFrom(self):
1614        self.cli.sendto(MSG, 0, (HOST, self.port))
1615
1616    def testRecvFromNegative(self):
1617        # Negative lengths passed to recvfrom should give ValueError.
1618        self.assertRaises(ValueError, self.serv.recvfrom, -1)
1619
1620    def _testRecvFromNegative(self):
1621        self.cli.sendto(MSG, 0, (HOST, self.port))
1622
1623# Tests for the sendmsg()/recvmsg() interface.  Where possible, the
1624# same test code is used with different families and types of socket
1625# (e.g. stream, datagram), and tests using recvmsg() are repeated
1626# using recvmsg_into().
1627#
1628# The generic test classes such as SendmsgTests and
1629# RecvmsgGenericTests inherit from SendrecvmsgBase and expect to be
1630# supplied with sockets cli_sock and serv_sock representing the
1631# client's and the server's end of the connection respectively, and
1632# attributes cli_addr and serv_addr holding their (numeric where
1633# appropriate) addresses.
1634#
1635# The final concrete test classes combine these with subclasses of
1636# SocketTestBase which set up client and server sockets of a specific
1637# type, and with subclasses of SendrecvmsgBase such as
1638# SendrecvmsgDgramBase and SendrecvmsgConnectedBase which map these
1639# sockets to cli_sock and serv_sock and override the methods and
1640# attributes of SendrecvmsgBase to fill in destination addresses if
1641# needed when sending, check for specific flags in msg_flags, etc.
1642#
1643# RecvmsgIntoMixin provides a version of doRecvmsg() implemented using
1644# recvmsg_into().
1645
1646# XXX: like the other datagram (UDP) tests in this module, the code
1647# here assumes that datagram delivery on the local machine will be
1648# reliable.
1649
1650class SendrecvmsgBase(ThreadSafeCleanupTestCase):
1651    # Base class for sendmsg()/recvmsg() tests.
1652
1653    # Time in seconds to wait before considering a test failed, or
1654    # None for no timeout.  Not all tests actually set a timeout.
1655    fail_timeout = 3.0
1656
1657    def setUp(self):
1658        self.misc_event = threading.Event()
1659        super().setUp()
1660
1661    def sendToServer(self, msg):
1662        # Send msg to the server.
1663        return self.cli_sock.send(msg)
1664
1665    # Tuple of alternative default arguments for sendmsg() when called
1666    # via sendmsgToServer() (e.g. to include a destination address).
1667    sendmsg_to_server_defaults = ()
1668
1669    def sendmsgToServer(self, *args):
1670        # Call sendmsg() on self.cli_sock with the given arguments,
1671        # filling in any arguments which are not supplied with the
1672        # corresponding items of self.sendmsg_to_server_defaults, if
1673        # any.
1674        return self.cli_sock.sendmsg(
1675            *(args + self.sendmsg_to_server_defaults[len(args):]))
1676
1677    def doRecvmsg(self, sock, bufsize, *args):
1678        # Call recvmsg() on sock with given arguments and return its
1679        # result.  Should be used for tests which can use either
1680        # recvmsg() or recvmsg_into() - RecvmsgIntoMixin overrides
1681        # this method with one which emulates it using recvmsg_into(),
1682        # thus allowing the same test to be used for both methods.
1683        result = sock.recvmsg(bufsize, *args)
1684        self.registerRecvmsgResult(result)
1685        return result
1686
1687    def registerRecvmsgResult(self, result):
1688        # Called by doRecvmsg() with the return value of recvmsg() or
1689        # recvmsg_into().  Can be overridden to arrange cleanup based
1690        # on the returned ancillary data, for instance.
1691        pass
1692
1693    def checkRecvmsgAddress(self, addr1, addr2):
1694        # Called to compare the received address with the address of
1695        # the peer.
1696        self.assertEqual(addr1, addr2)
1697
1698    # Flags that are normally unset in msg_flags
1699    msg_flags_common_unset = 0
1700    for name in ("MSG_CTRUNC", "MSG_OOB"):
1701        msg_flags_common_unset |= getattr(socket, name, 0)
1702
1703    # Flags that are normally set
1704    msg_flags_common_set = 0
1705
1706    # Flags set when a complete record has been received (e.g. MSG_EOR
1707    # for SCTP)
1708    msg_flags_eor_indicator = 0
1709
1710    # Flags set when a complete record has not been received
1711    # (e.g. MSG_TRUNC for datagram sockets)
1712    msg_flags_non_eor_indicator = 0
1713
1714    def checkFlags(self, flags, eor=None, checkset=0, checkunset=0, ignore=0):
1715        # Method to check the value of msg_flags returned by recvmsg[_into]().
1716        #
1717        # Checks that all bits in msg_flags_common_set attribute are
1718        # set in "flags" and all bits in msg_flags_common_unset are
1719        # unset.
1720        #
1721        # The "eor" argument specifies whether the flags should
1722        # indicate that a full record (or datagram) has been received.
1723        # If "eor" is None, no checks are done; otherwise, checks
1724        # that:
1725        #
1726        #  * if "eor" is true, all bits in msg_flags_eor_indicator are
1727        #    set and all bits in msg_flags_non_eor_indicator are unset
1728        #
1729        #  * if "eor" is false, all bits in msg_flags_non_eor_indicator
1730        #    are set and all bits in msg_flags_eor_indicator are unset
1731        #
1732        # If "checkset" and/or "checkunset" are supplied, they require
1733        # the given bits to be set or unset respectively, overriding
1734        # what the attributes require for those bits.
1735        #
1736        # If any bits are set in "ignore", they will not be checked,
1737        # regardless of the other inputs.
1738        #
1739        # Will raise Exception if the inputs require a bit to be both
1740        # set and unset, and it is not ignored.
1741
1742        defaultset = self.msg_flags_common_set
1743        defaultunset = self.msg_flags_common_unset
1744
1745        if eor:
1746            defaultset |= self.msg_flags_eor_indicator
1747            defaultunset |= self.msg_flags_non_eor_indicator
1748        elif eor is not None:
1749            defaultset |= self.msg_flags_non_eor_indicator
1750            defaultunset |= self.msg_flags_eor_indicator
1751
1752        # Function arguments override defaults
1753        defaultset &= ~checkunset
1754        defaultunset &= ~checkset
1755
1756        # Merge arguments with remaining defaults, and check for conflicts
1757        checkset |= defaultset
1758        checkunset |= defaultunset
1759        inboth = checkset & checkunset & ~ignore
1760        if inboth:
1761            raise Exception("contradictory set, unset requirements for flags "
1762                            "{0:#x}".format(inboth))
1763
1764        # Compare with given msg_flags value
1765        mask = (checkset | checkunset) & ~ignore
1766        self.assertEqual(flags & mask, checkset & mask)
1767
1768
1769class RecvmsgIntoMixin(SendrecvmsgBase):
1770    # Mixin to implement doRecvmsg() using recvmsg_into().
1771
1772    def doRecvmsg(self, sock, bufsize, *args):
1773        buf = bytearray(bufsize)
1774        result = sock.recvmsg_into([buf], *args)
1775        self.registerRecvmsgResult(result)
1776        self.assertGreaterEqual(result[0], 0)
1777        self.assertLessEqual(result[0], bufsize)
1778        return (bytes(buf[:result[0]]),) + result[1:]
1779
1780
1781class SendrecvmsgDgramFlagsBase(SendrecvmsgBase):
1782    # Defines flags to be checked in msg_flags for datagram sockets.
1783
1784    @property
1785    def msg_flags_non_eor_indicator(self):
1786        return super().msg_flags_non_eor_indicator | socket.MSG_TRUNC
1787
1788
1789class SendrecvmsgSCTPFlagsBase(SendrecvmsgBase):
1790    # Defines flags to be checked in msg_flags for SCTP sockets.
1791
1792    @property
1793    def msg_flags_eor_indicator(self):
1794        return super().msg_flags_eor_indicator | socket.MSG_EOR
1795
1796
1797class SendrecvmsgConnectionlessBase(SendrecvmsgBase):
1798    # Base class for tests on connectionless-mode sockets.  Users must
1799    # supply sockets on attributes cli and serv to be mapped to
1800    # cli_sock and serv_sock respectively.
1801
1802    @property
1803    def serv_sock(self):
1804        return self.serv
1805
1806    @property
1807    def cli_sock(self):
1808        return self.cli
1809
1810    @property
1811    def sendmsg_to_server_defaults(self):
1812        return ([], [], 0, self.serv_addr)
1813
1814    def sendToServer(self, msg):
1815        return self.cli_sock.sendto(msg, self.serv_addr)
1816
1817
1818class SendrecvmsgConnectedBase(SendrecvmsgBase):
1819    # Base class for tests on connected sockets.  Users must supply
1820    # sockets on attributes serv_conn and cli_conn (representing the
1821    # connections *to* the server and the client), to be mapped to
1822    # cli_sock and serv_sock respectively.
1823
1824    @property
1825    def serv_sock(self):
1826        return self.cli_conn
1827
1828    @property
1829    def cli_sock(self):
1830        return self.serv_conn
1831
1832    def checkRecvmsgAddress(self, addr1, addr2):
1833        # Address is currently "unspecified" for a connected socket,
1834        # so we don't examine it
1835        pass
1836
1837
1838class SendrecvmsgServerTimeoutBase(SendrecvmsgBase):
1839    # Base class to set a timeout on server's socket.
1840
1841    def setUp(self):
1842        super().setUp()
1843        self.serv_sock.settimeout(self.fail_timeout)
1844
1845
1846class SendmsgTests(SendrecvmsgServerTimeoutBase):
1847    # Tests for sendmsg() which can use any socket type and do not
1848    # involve recvmsg() or recvmsg_into().
1849
1850    def testSendmsg(self):
1851        # Send a simple message with sendmsg().
1852        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1853
1854    def _testSendmsg(self):
1855        self.assertEqual(self.sendmsgToServer([MSG]), len(MSG))
1856
1857    def testSendmsgDataGenerator(self):
1858        # Send from buffer obtained from a generator (not a sequence).
1859        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1860
1861    def _testSendmsgDataGenerator(self):
1862        self.assertEqual(self.sendmsgToServer((o for o in [MSG])),
1863                         len(MSG))
1864
1865    def testSendmsgAncillaryGenerator(self):
1866        # Gather (empty) ancillary data from a generator.
1867        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1868
1869    def _testSendmsgAncillaryGenerator(self):
1870        self.assertEqual(self.sendmsgToServer([MSG], (o for o in [])),
1871                         len(MSG))
1872
1873    def testSendmsgArray(self):
1874        # Send data from an array instead of the usual bytes object.
1875        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1876
1877    def _testSendmsgArray(self):
1878        self.assertEqual(self.sendmsgToServer([array.array("B", MSG)]),
1879                         len(MSG))
1880
1881    def testSendmsgGather(self):
1882        # Send message data from more than one buffer (gather write).
1883        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1884
1885    def _testSendmsgGather(self):
1886        self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG))
1887
1888    def testSendmsgBadArgs(self):
1889        # Check that sendmsg() rejects invalid arguments.
1890        self.assertEqual(self.serv_sock.recv(1000), b"done")
1891
1892    def _testSendmsgBadArgs(self):
1893        self.assertRaises(TypeError, self.cli_sock.sendmsg)
1894        self.assertRaises(TypeError, self.sendmsgToServer,
1895                          b"not in an iterable")
1896        self.assertRaises(TypeError, self.sendmsgToServer,
1897                          object())
1898        self.assertRaises(TypeError, self.sendmsgToServer,
1899                          [object()])
1900        self.assertRaises(TypeError, self.sendmsgToServer,
1901                          [MSG, object()])
1902        self.assertRaises(TypeError, self.sendmsgToServer,
1903                          [MSG], object())
1904        self.assertRaises(TypeError, self.sendmsgToServer,
1905                          [MSG], [], object())
1906        self.assertRaises(TypeError, self.sendmsgToServer,
1907                          [MSG], [], 0, object())
1908        self.sendToServer(b"done")
1909
1910    def testSendmsgBadCmsg(self):
1911        # Check that invalid ancillary data items are rejected.
1912        self.assertEqual(self.serv_sock.recv(1000), b"done")
1913
1914    def _testSendmsgBadCmsg(self):
1915        self.assertRaises(TypeError, self.sendmsgToServer,
1916                          [MSG], [object()])
1917        self.assertRaises(TypeError, self.sendmsgToServer,
1918                          [MSG], [(object(), 0, b"data")])
1919        self.assertRaises(TypeError, self.sendmsgToServer,
1920                          [MSG], [(0, object(), b"data")])
1921        self.assertRaises(TypeError, self.sendmsgToServer,
1922                          [MSG], [(0, 0, object())])
1923        self.assertRaises(TypeError, self.sendmsgToServer,
1924                          [MSG], [(0, 0)])
1925        self.assertRaises(TypeError, self.sendmsgToServer,
1926                          [MSG], [(0, 0, b"data", 42)])
1927        self.sendToServer(b"done")
1928
1929    @requireAttrs(socket, "CMSG_SPACE")
1930    def testSendmsgBadMultiCmsg(self):
1931        # Check that invalid ancillary data items are rejected when
1932        # more than one item is present.
1933        self.assertEqual(self.serv_sock.recv(1000), b"done")
1934
1935    @testSendmsgBadMultiCmsg.client_skip
1936    def _testSendmsgBadMultiCmsg(self):
1937        self.assertRaises(TypeError, self.sendmsgToServer,
1938                          [MSG], [0, 0, b""])
1939        self.assertRaises(TypeError, self.sendmsgToServer,
1940                          [MSG], [(0, 0, b""), object()])
1941        self.sendToServer(b"done")
1942
1943    def testSendmsgExcessCmsgReject(self):
1944        # Check that sendmsg() rejects excess ancillary data items
1945        # when the number that can be sent is limited.
1946        self.assertEqual(self.serv_sock.recv(1000), b"done")
1947
1948    def _testSendmsgExcessCmsgReject(self):
1949        if not hasattr(socket, "CMSG_SPACE"):
1950            # Can only send one item
1951            with self.assertRaises(socket.error) as cm:
1952                self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")])
1953            self.assertIsNone(cm.exception.errno)
1954        self.sendToServer(b"done")
1955
1956    def testSendmsgAfterClose(self):
1957        # Check that sendmsg() fails on a closed socket.
1958        pass
1959
1960    def _testSendmsgAfterClose(self):
1961        self.cli_sock.close()
1962        self.assertRaises(socket.error, self.sendmsgToServer, [MSG])
1963
1964
1965class SendmsgStreamTests(SendmsgTests):
1966    # Tests for sendmsg() which require a stream socket and do not
1967    # involve recvmsg() or recvmsg_into().
1968
1969    def testSendmsgExplicitNoneAddr(self):
1970        # Check that peer address can be specified as None.
1971        self.assertEqual(self.serv_sock.recv(len(MSG)), MSG)
1972
1973    def _testSendmsgExplicitNoneAddr(self):
1974        self.assertEqual(self.sendmsgToServer([MSG], [], 0, None), len(MSG))
1975
1976    def testSendmsgTimeout(self):
1977        # Check that timeout works with sendmsg().
1978        self.assertEqual(self.serv_sock.recv(512), b"a"*512)
1979        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
1980
1981    def _testSendmsgTimeout(self):
1982        try:
1983            self.cli_sock.settimeout(0.03)
1984            with self.assertRaises(socket.timeout):
1985                while True:
1986                    self.sendmsgToServer([b"a"*512])
1987        finally:
1988            self.misc_event.set()
1989
1990    # XXX: would be nice to have more tests for sendmsg flags argument.
1991
1992    # Linux supports MSG_DONTWAIT when sending, but in general, it
1993    # only works when receiving.  Could add other platforms if they
1994    # support it too.
1995    @skipWithClientIf(sys.platform not in {"linux2"},
1996                      "MSG_DONTWAIT not known to work on this platform when "
1997                      "sending")
1998    def testSendmsgDontWait(self):
1999        # Check that MSG_DONTWAIT in flags causes non-blocking behaviour.
2000        self.assertEqual(self.serv_sock.recv(512), b"a"*512)
2001        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2002
2003    @testSendmsgDontWait.client_skip
2004    def _testSendmsgDontWait(self):
2005        try:
2006            with self.assertRaises(socket.error) as cm:
2007                while True:
2008                    self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT)
2009            self.assertIn(cm.exception.errno,
2010                          (errno.EAGAIN, errno.EWOULDBLOCK))
2011        finally:
2012            self.misc_event.set()
2013
2014
2015class SendmsgConnectionlessTests(SendmsgTests):
2016    # Tests for sendmsg() which require a connectionless-mode
2017    # (e.g. datagram) socket, and do not involve recvmsg() or
2018    # recvmsg_into().
2019
2020    def testSendmsgNoDestAddr(self):
2021        # Check that sendmsg() fails when no destination address is
2022        # given for unconnected socket.
2023        pass
2024
2025    def _testSendmsgNoDestAddr(self):
2026        self.assertRaises(socket.error, self.cli_sock.sendmsg,
2027                          [MSG])
2028        self.assertRaises(socket.error, self.cli_sock.sendmsg,
2029                          [MSG], [], 0, None)
2030
2031
2032class RecvmsgGenericTests(SendrecvmsgBase):
2033    # Tests for recvmsg() which can also be emulated using
2034    # recvmsg_into(), and can use any socket type.
2035
2036    def testRecvmsg(self):
2037        # Receive a simple message with recvmsg[_into]().
2038        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG))
2039        self.assertEqual(msg, MSG)
2040        self.checkRecvmsgAddress(addr, self.cli_addr)
2041        self.assertEqual(ancdata, [])
2042        self.checkFlags(flags, eor=True)
2043
2044    def _testRecvmsg(self):
2045        self.sendToServer(MSG)
2046
2047    def testRecvmsgExplicitDefaults(self):
2048        # Test recvmsg[_into]() with default arguments provided explicitly.
2049        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2050                                                   len(MSG), 0, 0)
2051        self.assertEqual(msg, MSG)
2052        self.checkRecvmsgAddress(addr, self.cli_addr)
2053        self.assertEqual(ancdata, [])
2054        self.checkFlags(flags, eor=True)
2055
2056    def _testRecvmsgExplicitDefaults(self):
2057        self.sendToServer(MSG)
2058
2059    def testRecvmsgShorter(self):
2060        # Receive a message smaller than buffer.
2061        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2062                                                   len(MSG) + 42)
2063        self.assertEqual(msg, MSG)
2064        self.checkRecvmsgAddress(addr, self.cli_addr)
2065        self.assertEqual(ancdata, [])
2066        self.checkFlags(flags, eor=True)
2067
2068    def _testRecvmsgShorter(self):
2069        self.sendToServer(MSG)
2070
2071    # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated
2072    # datagram is received (issue #13001).
2073    @support.requires_freebsd_version(8)
2074    def testRecvmsgTrunc(self):
2075        # Receive part of message, check for truncation indicators.
2076        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2077                                                   len(MSG) - 3)
2078        self.assertEqual(msg, MSG[:-3])
2079        self.checkRecvmsgAddress(addr, self.cli_addr)
2080        self.assertEqual(ancdata, [])
2081        self.checkFlags(flags, eor=False)
2082
2083    @support.requires_freebsd_version(8)
2084    def _testRecvmsgTrunc(self):
2085        self.sendToServer(MSG)
2086
2087    def testRecvmsgShortAncillaryBuf(self):
2088        # Test ancillary data buffer too small to hold any ancillary data.
2089        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2090                                                   len(MSG), 1)
2091        self.assertEqual(msg, MSG)
2092        self.checkRecvmsgAddress(addr, self.cli_addr)
2093        self.assertEqual(ancdata, [])
2094        self.checkFlags(flags, eor=True)
2095
2096    def _testRecvmsgShortAncillaryBuf(self):
2097        self.sendToServer(MSG)
2098
2099    def testRecvmsgLongAncillaryBuf(self):
2100        # Test large ancillary data buffer.
2101        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2102                                                   len(MSG), 10240)
2103        self.assertEqual(msg, MSG)
2104        self.checkRecvmsgAddress(addr, self.cli_addr)
2105        self.assertEqual(ancdata, [])
2106        self.checkFlags(flags, eor=True)
2107
2108    def _testRecvmsgLongAncillaryBuf(self):
2109        self.sendToServer(MSG)
2110
2111    def testRecvmsgAfterClose(self):
2112        # Check that recvmsg[_into]() fails on a closed socket.
2113        self.serv_sock.close()
2114        self.assertRaises(socket.error, self.doRecvmsg, self.serv_sock, 1024)
2115
2116    def _testRecvmsgAfterClose(self):
2117        pass
2118
2119    def testRecvmsgTimeout(self):
2120        # Check that timeout works.
2121        try:
2122            self.serv_sock.settimeout(0.03)
2123            self.assertRaises(socket.timeout,
2124                              self.doRecvmsg, self.serv_sock, len(MSG))
2125        finally:
2126            self.misc_event.set()
2127
2128    def _testRecvmsgTimeout(self):
2129        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2130
2131    @requireAttrs(socket, "MSG_PEEK")
2132    def testRecvmsgPeek(self):
2133        # Check that MSG_PEEK in flags enables examination of pending
2134        # data without consuming it.
2135
2136        # Receive part of data with MSG_PEEK.
2137        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2138                                                   len(MSG) - 3, 0,
2139                                                   socket.MSG_PEEK)
2140        self.assertEqual(msg, MSG[:-3])
2141        self.checkRecvmsgAddress(addr, self.cli_addr)
2142        self.assertEqual(ancdata, [])
2143        # Ignoring MSG_TRUNC here (so this test is the same for stream
2144        # and datagram sockets).  Some wording in POSIX seems to
2145        # suggest that it needn't be set when peeking, but that may
2146        # just be a slip.
2147        self.checkFlags(flags, eor=False,
2148                        ignore=getattr(socket, "MSG_TRUNC", 0))
2149
2150        # Receive all data with MSG_PEEK.
2151        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2152                                                   len(MSG), 0,
2153                                                   socket.MSG_PEEK)
2154        self.assertEqual(msg, MSG)
2155        self.checkRecvmsgAddress(addr, self.cli_addr)
2156        self.assertEqual(ancdata, [])
2157        self.checkFlags(flags, eor=True)
2158
2159        # Check that the same data can still be received normally.
2160        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG))
2161        self.assertEqual(msg, MSG)
2162        self.checkRecvmsgAddress(addr, self.cli_addr)
2163        self.assertEqual(ancdata, [])
2164        self.checkFlags(flags, eor=True)
2165
2166    @testRecvmsgPeek.client_skip
2167    def _testRecvmsgPeek(self):
2168        self.sendToServer(MSG)
2169
2170    @requireAttrs(socket.socket, "sendmsg")
2171    def testRecvmsgFromSendmsg(self):
2172        # Test receiving with recvmsg[_into]() when message is sent
2173        # using sendmsg().
2174        self.serv_sock.settimeout(self.fail_timeout)
2175        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG))
2176        self.assertEqual(msg, MSG)
2177        self.checkRecvmsgAddress(addr, self.cli_addr)
2178        self.assertEqual(ancdata, [])
2179        self.checkFlags(flags, eor=True)
2180
2181    @testRecvmsgFromSendmsg.client_skip
2182    def _testRecvmsgFromSendmsg(self):
2183        self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG))
2184
2185
2186class RecvmsgGenericStreamTests(RecvmsgGenericTests):
2187    # Tests which require a stream socket and can use either recvmsg()
2188    # or recvmsg_into().
2189
2190    def testRecvmsgEOF(self):
2191        # Receive end-of-stream indicator (b"", peer socket closed).
2192        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024)
2193        self.assertEqual(msg, b"")
2194        self.checkRecvmsgAddress(addr, self.cli_addr)
2195        self.assertEqual(ancdata, [])
2196        self.checkFlags(flags, eor=None) # Might not have end-of-record marker
2197
2198    def _testRecvmsgEOF(self):
2199        self.cli_sock.close()
2200
2201    def testRecvmsgOverflow(self):
2202        # Receive a message in more than one chunk.
2203        seg1, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2204                                                    len(MSG) - 3)
2205        self.checkRecvmsgAddress(addr, self.cli_addr)
2206        self.assertEqual(ancdata, [])
2207        self.checkFlags(flags, eor=False)
2208
2209        seg2, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024)
2210        self.checkRecvmsgAddress(addr, self.cli_addr)
2211        self.assertEqual(ancdata, [])
2212        self.checkFlags(flags, eor=True)
2213
2214        msg = seg1 + seg2
2215        self.assertEqual(msg, MSG)
2216
2217    def _testRecvmsgOverflow(self):
2218        self.sendToServer(MSG)
2219
2220
2221class RecvmsgTests(RecvmsgGenericTests):
2222    # Tests for recvmsg() which can use any socket type.
2223
2224    def testRecvmsgBadArgs(self):
2225        # Check that recvmsg() rejects invalid arguments.
2226        self.assertRaises(TypeError, self.serv_sock.recvmsg)
2227        self.assertRaises(ValueError, self.serv_sock.recvmsg,
2228                          -1, 0, 0)
2229        self.assertRaises(ValueError, self.serv_sock.recvmsg,
2230                          len(MSG), -1, 0)
2231        self.assertRaises(TypeError, self.serv_sock.recvmsg,
2232                          [bytearray(10)], 0, 0)
2233        self.assertRaises(TypeError, self.serv_sock.recvmsg,
2234                          object(), 0, 0)
2235        self.assertRaises(TypeError, self.serv_sock.recvmsg,
2236                          len(MSG), object(), 0)
2237        self.assertRaises(TypeError, self.serv_sock.recvmsg,
2238                          len(MSG), 0, object())
2239
2240        msg, ancdata, flags, addr = self.serv_sock.recvmsg(len(MSG), 0, 0)
2241        self.assertEqual(msg, MSG)
2242        self.checkRecvmsgAddress(addr, self.cli_addr)
2243        self.assertEqual(ancdata, [])
2244        self.checkFlags(flags, eor=True)
2245
2246    def _testRecvmsgBadArgs(self):
2247        self.sendToServer(MSG)
2248
2249
2250class RecvmsgIntoTests(RecvmsgIntoMixin, RecvmsgGenericTests):
2251    # Tests for recvmsg_into() which can use any socket type.
2252
2253    def testRecvmsgIntoBadArgs(self):
2254        # Check that recvmsg_into() rejects invalid arguments.
2255        buf = bytearray(len(MSG))
2256        self.assertRaises(TypeError, self.serv_sock.recvmsg_into)
2257        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2258                          len(MSG), 0, 0)
2259        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2260                          buf, 0, 0)
2261        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2262                          [object()], 0, 0)
2263        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2264                          [b"I'm not writable"], 0, 0)
2265        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2266                          [buf, object()], 0, 0)
2267        self.assertRaises(ValueError, self.serv_sock.recvmsg_into,
2268                          [buf], -1, 0)
2269        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2270                          [buf], object(), 0)
2271        self.assertRaises(TypeError, self.serv_sock.recvmsg_into,
2272                          [buf], 0, object())
2273
2274        nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf], 0, 0)
2275        self.assertEqual(nbytes, len(MSG))
2276        self.assertEqual(buf, bytearray(MSG))
2277        self.checkRecvmsgAddress(addr, self.cli_addr)
2278        self.assertEqual(ancdata, [])
2279        self.checkFlags(flags, eor=True)
2280
2281    def _testRecvmsgIntoBadArgs(self):
2282        self.sendToServer(MSG)
2283
2284    def testRecvmsgIntoGenerator(self):
2285        # Receive into buffer obtained from a generator (not a sequence).
2286        buf = bytearray(len(MSG))
2287        nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into(
2288            (o for o in [buf]))
2289        self.assertEqual(nbytes, len(MSG))
2290        self.assertEqual(buf, bytearray(MSG))
2291        self.checkRecvmsgAddress(addr, self.cli_addr)
2292        self.assertEqual(ancdata, [])
2293        self.checkFlags(flags, eor=True)
2294
2295    def _testRecvmsgIntoGenerator(self):
2296        self.sendToServer(MSG)
2297
2298    def testRecvmsgIntoArray(self):
2299        # Receive into an array rather than the usual bytearray.
2300        buf = array.array("B", [0] * len(MSG))
2301        nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf])
2302        self.assertEqual(nbytes, len(MSG))
2303        self.assertEqual(buf.tobytes(), MSG)
2304        self.checkRecvmsgAddress(addr, self.cli_addr)
2305        self.assertEqual(ancdata, [])
2306        self.checkFlags(flags, eor=True)
2307
2308    def _testRecvmsgIntoArray(self):
2309        self.sendToServer(MSG)
2310
2311    def testRecvmsgIntoScatter(self):
2312        # Receive into multiple buffers (scatter write).
2313        b1 = bytearray(b"----")
2314        b2 = bytearray(b"0123456789")
2315        b3 = bytearray(b"--------------")
2316        nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into(
2317            [b1, memoryview(b2)[2:9], b3])
2318        self.assertEqual(nbytes, len(b"Mary had a little lamb"))
2319        self.assertEqual(b1, bytearray(b"Mary"))
2320        self.assertEqual(b2, bytearray(b"01 had a 9"))
2321        self.assertEqual(b3, bytearray(b"little lamb---"))
2322        self.checkRecvmsgAddress(addr, self.cli_addr)
2323        self.assertEqual(ancdata, [])
2324        self.checkFlags(flags, eor=True)
2325
2326    def _testRecvmsgIntoScatter(self):
2327        self.sendToServer(b"Mary had a little lamb")
2328
2329
2330class CmsgMacroTests(unittest.TestCase):
2331    # Test the functions CMSG_LEN() and CMSG_SPACE().  Tests
2332    # assumptions used by sendmsg() and recvmsg[_into](), which share
2333    # code with these functions.
2334
2335    # Match the definition in socketmodule.c
2336    socklen_t_limit = min(0x7fffffff, _testcapi.INT_MAX)
2337
2338    @requireAttrs(socket, "CMSG_LEN")
2339    def testCMSG_LEN(self):
2340        # Test CMSG_LEN() with various valid and invalid values,
2341        # checking the assumptions used by recvmsg() and sendmsg().
2342        toobig = self.socklen_t_limit - socket.CMSG_LEN(0) + 1
2343        values = list(range(257)) + list(range(toobig - 257, toobig))
2344
2345        # struct cmsghdr has at least three members, two of which are ints
2346        self.assertGreater(socket.CMSG_LEN(0), array.array("i").itemsize * 2)
2347        for n in values:
2348            ret = socket.CMSG_LEN(n)
2349            # This is how recvmsg() calculates the data size
2350            self.assertEqual(ret - socket.CMSG_LEN(0), n)
2351            self.assertLessEqual(ret, self.socklen_t_limit)
2352
2353        self.assertRaises(OverflowError, socket.CMSG_LEN, -1)
2354        # sendmsg() shares code with these functions, and requires
2355        # that it reject values over the limit.
2356        self.assertRaises(OverflowError, socket.CMSG_LEN, toobig)
2357        self.assertRaises(OverflowError, socket.CMSG_LEN, sys.maxsize)
2358
2359    @requireAttrs(socket, "CMSG_SPACE")
2360    def testCMSG_SPACE(self):
2361        # Test CMSG_SPACE() with various valid and invalid values,
2362        # checking the assumptions used by sendmsg().
2363        toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1
2364        values = list(range(257)) + list(range(toobig - 257, toobig))
2365
2366        last = socket.CMSG_SPACE(0)
2367        # struct cmsghdr has at least three members, two of which are ints
2368        self.assertGreater(last, array.array("i").itemsize * 2)
2369        for n in values:
2370            ret = socket.CMSG_SPACE(n)
2371            self.assertGreaterEqual(ret, last)
2372            self.assertGreaterEqual(ret, socket.CMSG_LEN(n))
2373            self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0))
2374            self.assertLessEqual(ret, self.socklen_t_limit)
2375            last = ret
2376
2377        self.assertRaises(OverflowError, socket.CMSG_SPACE, -1)
2378        # sendmsg() shares code with these functions, and requires
2379        # that it reject values over the limit.
2380        self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig)
2381        self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize)
2382
2383
2384class SCMRightsTest(SendrecvmsgServerTimeoutBase):
2385    # Tests for file descriptor passing on Unix-domain sockets.
2386
2387    # Invalid file descriptor value that's unlikely to evaluate to a
2388    # real FD even if one of its bytes is replaced with a different
2389    # value (which shouldn't actually happen).
2390    badfd = -0x5555
2391
2392    def newFDs(self, n):
2393        # Return a list of n file descriptors for newly-created files
2394        # containing their list indices as ASCII numbers.
2395        fds = []
2396        for i in range(n):
2397            fd, path = tempfile.mkstemp()
2398            self.addCleanup(os.unlink, path)
2399            self.addCleanup(os.close, fd)
2400            os.write(fd, str(i).encode())
2401            fds.append(fd)
2402        return fds
2403
2404    def checkFDs(self, fds):
2405        # Check that the file descriptors in the given list contain
2406        # their correct list indices as ASCII numbers.
2407        for n, fd in enumerate(fds):
2408            os.lseek(fd, 0, os.SEEK_SET)
2409            self.assertEqual(os.read(fd, 1024), str(n).encode())
2410
2411    def registerRecvmsgResult(self, result):
2412        self.addCleanup(self.closeRecvmsgFDs, result)
2413
2414    def closeRecvmsgFDs(self, recvmsg_result):
2415        # Close all file descriptors specified in the ancillary data
2416        # of the given return value from recvmsg() or recvmsg_into().
2417        for cmsg_level, cmsg_type, cmsg_data in recvmsg_result[1]:
2418            if (cmsg_level == socket.SOL_SOCKET and
2419                    cmsg_type == socket.SCM_RIGHTS):
2420                fds = array.array("i")
2421                fds.frombytes(cmsg_data[:
2422                        len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
2423                for fd in fds:
2424                    os.close(fd)
2425
2426    def createAndSendFDs(self, n):
2427        # Send n new file descriptors created by newFDs() to the
2428        # server, with the constant MSG as the non-ancillary data.
2429        self.assertEqual(
2430            self.sendmsgToServer([MSG],
2431                                 [(socket.SOL_SOCKET,
2432                                   socket.SCM_RIGHTS,
2433                                   array.array("i", self.newFDs(n)))]),
2434            len(MSG))
2435
2436    def checkRecvmsgFDs(self, numfds, result, maxcmsgs=1, ignoreflags=0):
2437        # Check that constant MSG was received with numfds file
2438        # descriptors in a maximum of maxcmsgs control messages (which
2439        # must contain only complete integers).  By default, check
2440        # that MSG_CTRUNC is unset, but ignore any flags in
2441        # ignoreflags.
2442        msg, ancdata, flags, addr = result
2443        self.assertEqual(msg, MSG)
2444        self.checkRecvmsgAddress(addr, self.cli_addr)
2445        self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC,
2446                        ignore=ignoreflags)
2447
2448        self.assertIsInstance(ancdata, list)
2449        self.assertLessEqual(len(ancdata), maxcmsgs)
2450        fds = array.array("i")
2451        for item in ancdata:
2452            self.assertIsInstance(item, tuple)
2453            cmsg_level, cmsg_type, cmsg_data = item
2454            self.assertEqual(cmsg_level, socket.SOL_SOCKET)
2455            self.assertEqual(cmsg_type, socket.SCM_RIGHTS)
2456            self.assertIsInstance(cmsg_data, bytes)
2457            self.assertEqual(len(cmsg_data) % SIZEOF_INT, 0)
2458            fds.frombytes(cmsg_data)
2459
2460        self.assertEqual(len(fds), numfds)
2461        self.checkFDs(fds)
2462
2463    def testFDPassSimple(self):
2464        # Pass a single FD (array read from bytes object).
2465        self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock,
2466                                               len(MSG), 10240))
2467
2468    def _testFDPassSimple(self):
2469        self.assertEqual(
2470            self.sendmsgToServer(
2471                [MSG],
2472                [(socket.SOL_SOCKET,
2473                  socket.SCM_RIGHTS,
2474                  array.array("i", self.newFDs(1)).tobytes())]),
2475            len(MSG))
2476
2477    def testMultipleFDPass(self):
2478        # Pass multiple FDs in a single array.
2479        self.checkRecvmsgFDs(4, self.doRecvmsg(self.serv_sock,
2480                                               len(MSG), 10240))
2481
2482    def _testMultipleFDPass(self):
2483        self.createAndSendFDs(4)
2484
2485    @requireAttrs(socket, "CMSG_SPACE")
2486    def testFDPassCMSG_SPACE(self):
2487        # Test using CMSG_SPACE() to calculate ancillary buffer size.
2488        self.checkRecvmsgFDs(
2489            4, self.doRecvmsg(self.serv_sock, len(MSG),
2490                              socket.CMSG_SPACE(4 * SIZEOF_INT)))
2491
2492    @testFDPassCMSG_SPACE.client_skip
2493    def _testFDPassCMSG_SPACE(self):
2494        self.createAndSendFDs(4)
2495
2496    def testFDPassCMSG_LEN(self):
2497        # Test using CMSG_LEN() to calculate ancillary buffer size.
2498        self.checkRecvmsgFDs(1,
2499                             self.doRecvmsg(self.serv_sock, len(MSG),
2500                                            socket.CMSG_LEN(4 * SIZEOF_INT)),
2501                             # RFC 3542 says implementations may set
2502                             # MSG_CTRUNC if there isn't enough space
2503                             # for trailing padding.
2504                             ignoreflags=socket.MSG_CTRUNC)
2505
2506    def _testFDPassCMSG_LEN(self):
2507        self.createAndSendFDs(1)
2508
2509    # Issue #12958: The following test has problems on Mac OS X
2510    @support.anticipate_failure(sys.platform == "darwin")
2511    @requireAttrs(socket, "CMSG_SPACE")
2512    def testFDPassSeparate(self):
2513        # Pass two FDs in two separate arrays.  Arrays may be combined
2514        # into a single control message by the OS.
2515        self.checkRecvmsgFDs(2,
2516                             self.doRecvmsg(self.serv_sock, len(MSG), 10240),
2517                             maxcmsgs=2)
2518
2519    @testFDPassSeparate.client_skip
2520    @support.anticipate_failure(sys.platform == "darwin")
2521    def _testFDPassSeparate(self):
2522        fd0, fd1 = self.newFDs(2)
2523        self.assertEqual(
2524            self.sendmsgToServer([MSG], [(socket.SOL_SOCKET,
2525                                          socket.SCM_RIGHTS,
2526                                          array.array("i", [fd0])),
2527                                         (socket.SOL_SOCKET,
2528                                          socket.SCM_RIGHTS,
2529                                          array.array("i", [fd1]))]),
2530            len(MSG))
2531
2532    # Issue #12958: The following test has problems on Mac OS X
2533    @support.anticipate_failure(sys.platform == "darwin")
2534    @requireAttrs(socket, "CMSG_SPACE")
2535    def testFDPassSeparateMinSpace(self):
2536        # Pass two FDs in two separate arrays, receiving them into the
2537        # minimum space for two arrays.
2538        self.checkRecvmsgFDs(2,
2539                             self.doRecvmsg(self.serv_sock, len(MSG),
2540                                            socket.CMSG_SPACE(SIZEOF_INT) +
2541                                            socket.CMSG_LEN(SIZEOF_INT)),
2542                             maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC)
2543
2544    @testFDPassSeparateMinSpace.client_skip
2545    @support.anticipate_failure(sys.platform == "darwin")
2546    def _testFDPassSeparateMinSpace(self):
2547        fd0, fd1 = self.newFDs(2)
2548        self.assertEqual(
2549            self.sendmsgToServer([MSG], [(socket.SOL_SOCKET,
2550                                          socket.SCM_RIGHTS,
2551                                          array.array("i", [fd0])),
2552                                         (socket.SOL_SOCKET,
2553                                          socket.SCM_RIGHTS,
2554                                          array.array("i", [fd1]))]),
2555            len(MSG))
2556
2557    def sendAncillaryIfPossible(self, msg, ancdata):
2558        # Try to send msg and ancdata to server, but if the system
2559        # call fails, just send msg with no ancillary data.
2560        try:
2561            nbytes = self.sendmsgToServer([msg], ancdata)
2562        except socket.error as e:
2563            # Check that it was the system call that failed
2564            self.assertIsInstance(e.errno, int)
2565            nbytes = self.sendmsgToServer([msg])
2566        self.assertEqual(nbytes, len(msg))
2567
2568    def testFDPassEmpty(self):
2569        # Try to pass an empty FD array.  Can receive either no array
2570        # or an empty array.
2571        self.checkRecvmsgFDs(0, self.doRecvmsg(self.serv_sock,
2572                                               len(MSG), 10240),
2573                             ignoreflags=socket.MSG_CTRUNC)
2574
2575    def _testFDPassEmpty(self):
2576        self.sendAncillaryIfPossible(MSG, [(socket.SOL_SOCKET,
2577                                            socket.SCM_RIGHTS,
2578                                            b"")])
2579
2580    def testFDPassPartialInt(self):
2581        # Try to pass a truncated FD array.
2582        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2583                                                   len(MSG), 10240)
2584        self.assertEqual(msg, MSG)
2585        self.checkRecvmsgAddress(addr, self.cli_addr)
2586        self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC)
2587        self.assertLessEqual(len(ancdata), 1)
2588        for cmsg_level, cmsg_type, cmsg_data in ancdata:
2589            self.assertEqual(cmsg_level, socket.SOL_SOCKET)
2590            self.assertEqual(cmsg_type, socket.SCM_RIGHTS)
2591            self.assertLess(len(cmsg_data), SIZEOF_INT)
2592
2593    def _testFDPassPartialInt(self):
2594        self.sendAncillaryIfPossible(
2595            MSG,
2596            [(socket.SOL_SOCKET,
2597              socket.SCM_RIGHTS,
2598              array.array("i", [self.badfd]).tobytes()[:-1])])
2599
2600    @requireAttrs(socket, "CMSG_SPACE")
2601    def testFDPassPartialIntInMiddle(self):
2602        # Try to pass two FD arrays, the first of which is truncated.
2603        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2604                                                   len(MSG), 10240)
2605        self.assertEqual(msg, MSG)
2606        self.checkRecvmsgAddress(addr, self.cli_addr)
2607        self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC)
2608        self.assertLessEqual(len(ancdata), 2)
2609        fds = array.array("i")
2610        # Arrays may have been combined in a single control message
2611        for cmsg_level, cmsg_type, cmsg_data in ancdata:
2612            self.assertEqual(cmsg_level, socket.SOL_SOCKET)
2613            self.assertEqual(cmsg_type, socket.SCM_RIGHTS)
2614            fds.frombytes(cmsg_data[:
2615                    len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
2616        self.assertLessEqual(len(fds), 2)
2617        self.checkFDs(fds)
2618
2619    @testFDPassPartialIntInMiddle.client_skip
2620    def _testFDPassPartialIntInMiddle(self):
2621        fd0, fd1 = self.newFDs(2)
2622        self.sendAncillaryIfPossible(
2623            MSG,
2624            [(socket.SOL_SOCKET,
2625              socket.SCM_RIGHTS,
2626              array.array("i", [fd0, self.badfd]).tobytes()[:-1]),
2627             (socket.SOL_SOCKET,
2628              socket.SCM_RIGHTS,
2629              array.array("i", [fd1]))])
2630
2631    def checkTruncatedHeader(self, result, ignoreflags=0):
2632        # Check that no ancillary data items are returned when data is
2633        # truncated inside the cmsghdr structure.
2634        msg, ancdata, flags, addr = result
2635        self.assertEqual(msg, MSG)
2636        self.checkRecvmsgAddress(addr, self.cli_addr)
2637        self.assertEqual(ancdata, [])
2638        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC,
2639                        ignore=ignoreflags)
2640
2641    def testCmsgTruncNoBufSize(self):
2642        # Check that no ancillary data is received when no buffer size
2643        # is specified.
2644        self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)),
2645                                  # BSD seems to set MSG_CTRUNC only
2646                                  # if an item has been partially
2647                                  # received.
2648                                  ignoreflags=socket.MSG_CTRUNC)
2649
2650    def _testCmsgTruncNoBufSize(self):
2651        self.createAndSendFDs(1)
2652
2653    def testCmsgTrunc0(self):
2654        # Check that no ancillary data is received when buffer size is 0.
2655        self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0),
2656                                  ignoreflags=socket.MSG_CTRUNC)
2657
2658    def _testCmsgTrunc0(self):
2659        self.createAndSendFDs(1)
2660
2661    # Check that no ancillary data is returned for various non-zero
2662    # (but still too small) buffer sizes.
2663
2664    def testCmsgTrunc1(self):
2665        self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1))
2666
2667    def _testCmsgTrunc1(self):
2668        self.createAndSendFDs(1)
2669
2670    def testCmsgTrunc2Int(self):
2671        # The cmsghdr structure has at least three members, two of
2672        # which are ints, so we still shouldn't see any ancillary
2673        # data.
2674        self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
2675                                                 SIZEOF_INT * 2))
2676
2677    def _testCmsgTrunc2Int(self):
2678        self.createAndSendFDs(1)
2679
2680    def testCmsgTruncLen0Minus1(self):
2681        self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
2682                                                 socket.CMSG_LEN(0) - 1))
2683
2684    def _testCmsgTruncLen0Minus1(self):
2685        self.createAndSendFDs(1)
2686
2687    # The following tests try to truncate the control message in the
2688    # middle of the FD array.
2689
2690    def checkTruncatedArray(self, ancbuf, maxdata, mindata=0):
2691        # Check that file descriptor data is truncated to between
2692        # mindata and maxdata bytes when received with buffer size
2693        # ancbuf, and that any complete file descriptor numbers are
2694        # valid.
2695        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2696                                                   len(MSG), ancbuf)
2697        self.assertEqual(msg, MSG)
2698        self.checkRecvmsgAddress(addr, self.cli_addr)
2699        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
2700
2701        if mindata == 0 and ancdata == []:
2702            return
2703        self.assertEqual(len(ancdata), 1)
2704        cmsg_level, cmsg_type, cmsg_data = ancdata[0]
2705        self.assertEqual(cmsg_level, socket.SOL_SOCKET)
2706        self.assertEqual(cmsg_type, socket.SCM_RIGHTS)
2707        self.assertGreaterEqual(len(cmsg_data), mindata)
2708        self.assertLessEqual(len(cmsg_data), maxdata)
2709        fds = array.array("i")
2710        fds.frombytes(cmsg_data[:
2711                len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
2712        self.checkFDs(fds)
2713
2714    def testCmsgTruncLen0(self):
2715        self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0)
2716
2717    def _testCmsgTruncLen0(self):
2718        self.createAndSendFDs(1)
2719
2720    def testCmsgTruncLen0Plus1(self):
2721        self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1)
2722
2723    def _testCmsgTruncLen0Plus1(self):
2724        self.createAndSendFDs(2)
2725
2726    def testCmsgTruncLen1(self):
2727        self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT),
2728                                 maxdata=SIZEOF_INT)
2729
2730    def _testCmsgTruncLen1(self):
2731        self.createAndSendFDs(2)
2732
2733    def testCmsgTruncLen2Minus1(self):
2734        self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1,
2735                                 maxdata=(2 * SIZEOF_INT) - 1)
2736
2737    def _testCmsgTruncLen2Minus1(self):
2738        self.createAndSendFDs(2)
2739
2740
2741class RFC3542AncillaryTest(SendrecvmsgServerTimeoutBase):
2742    # Test sendmsg() and recvmsg[_into]() using the ancillary data
2743    # features of the RFC 3542 Advanced Sockets API for IPv6.
2744    # Currently we can only handle certain data items (e.g. traffic
2745    # class, hop limit, MTU discovery and fragmentation settings)
2746    # without resorting to unportable means such as the struct module,
2747    # but the tests here are aimed at testing the ancillary data
2748    # handling in sendmsg() and recvmsg() rather than the IPv6 API
2749    # itself.
2750
2751    # Test value to use when setting hop limit of packet
2752    hop_limit = 2
2753
2754    # Test value to use when setting traffic class of packet.
2755    # -1 means "use kernel default".
2756    traffic_class = -1
2757
2758    def ancillaryMapping(self, ancdata):
2759        # Given ancillary data list ancdata, return a mapping from
2760        # pairs (cmsg_level, cmsg_type) to corresponding cmsg_data.
2761        # Check that no (level, type) pair appears more than once.
2762        d = {}
2763        for cmsg_level, cmsg_type, cmsg_data in ancdata:
2764            self.assertNotIn((cmsg_level, cmsg_type), d)
2765            d[(cmsg_level, cmsg_type)] = cmsg_data
2766        return d
2767
2768    def checkHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0):
2769        # Receive hop limit into ancbufsize bytes of ancillary data
2770        # space.  Check that data is MSG, ancillary data is not
2771        # truncated (but ignore any flags in ignoreflags), and hop
2772        # limit is between 0 and maxhop inclusive.
2773        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
2774                                  socket.IPV6_RECVHOPLIMIT, 1)
2775        self.misc_event.set()
2776        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2777                                                   len(MSG), ancbufsize)
2778
2779        self.assertEqual(msg, MSG)
2780        self.checkRecvmsgAddress(addr, self.cli_addr)
2781        self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC,
2782                        ignore=ignoreflags)
2783
2784        self.assertEqual(len(ancdata), 1)
2785        self.assertIsInstance(ancdata[0], tuple)
2786        cmsg_level, cmsg_type, cmsg_data = ancdata[0]
2787        self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
2788        self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT)
2789        self.assertIsInstance(cmsg_data, bytes)
2790        self.assertEqual(len(cmsg_data), SIZEOF_INT)
2791        a = array.array("i")
2792        a.frombytes(cmsg_data)
2793        self.assertGreaterEqual(a[0], 0)
2794        self.assertLessEqual(a[0], maxhop)
2795
2796    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
2797    def testRecvHopLimit(self):
2798        # Test receiving the packet hop limit as ancillary data.
2799        self.checkHopLimit(ancbufsize=10240)
2800
2801    @testRecvHopLimit.client_skip
2802    def _testRecvHopLimit(self):
2803        # Need to wait until server has asked to receive ancillary
2804        # data, as implementations are not required to buffer it
2805        # otherwise.
2806        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2807        self.sendToServer(MSG)
2808
2809    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
2810    def testRecvHopLimitCMSG_SPACE(self):
2811        # Test receiving hop limit, using CMSG_SPACE to calculate buffer size.
2812        self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT))
2813
2814    @testRecvHopLimitCMSG_SPACE.client_skip
2815    def _testRecvHopLimitCMSG_SPACE(self):
2816        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2817        self.sendToServer(MSG)
2818
2819    # Could test receiving into buffer sized using CMSG_LEN, but RFC
2820    # 3542 says portable applications must provide space for trailing
2821    # padding.  Implementations may set MSG_CTRUNC if there isn't
2822    # enough space for the padding.
2823
2824    @requireAttrs(socket.socket, "sendmsg")
2825    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
2826    def testSetHopLimit(self):
2827        # Test setting hop limit on outgoing packet and receiving it
2828        # at the other end.
2829        self.checkHopLimit(ancbufsize=10240, maxhop=self.hop_limit)
2830
2831    @testSetHopLimit.client_skip
2832    def _testSetHopLimit(self):
2833        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2834        self.assertEqual(
2835            self.sendmsgToServer([MSG],
2836                                 [(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT,
2837                                   array.array("i", [self.hop_limit]))]),
2838            len(MSG))
2839
2840    def checkTrafficClassAndHopLimit(self, ancbufsize, maxhop=255,
2841                                     ignoreflags=0):
2842        # Receive traffic class and hop limit into ancbufsize bytes of
2843        # ancillary data space.  Check that data is MSG, ancillary
2844        # data is not truncated (but ignore any flags in ignoreflags),
2845        # and traffic class and hop limit are in range (hop limit no
2846        # more than maxhop).
2847        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
2848                                  socket.IPV6_RECVHOPLIMIT, 1)
2849        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
2850                                  socket.IPV6_RECVTCLASS, 1)
2851        self.misc_event.set()
2852        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2853                                                   len(MSG), ancbufsize)
2854
2855        self.assertEqual(msg, MSG)
2856        self.checkRecvmsgAddress(addr, self.cli_addr)
2857        self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC,
2858                        ignore=ignoreflags)
2859        self.assertEqual(len(ancdata), 2)
2860        ancmap = self.ancillaryMapping(ancdata)
2861
2862        tcdata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_TCLASS)]
2863        self.assertEqual(len(tcdata), SIZEOF_INT)
2864        a = array.array("i")
2865        a.frombytes(tcdata)
2866        self.assertGreaterEqual(a[0], 0)
2867        self.assertLessEqual(a[0], 255)
2868
2869        hldata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT)]
2870        self.assertEqual(len(hldata), SIZEOF_INT)
2871        a = array.array("i")
2872        a.frombytes(hldata)
2873        self.assertGreaterEqual(a[0], 0)
2874        self.assertLessEqual(a[0], maxhop)
2875
2876    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
2877                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
2878    def testRecvTrafficClassAndHopLimit(self):
2879        # Test receiving traffic class and hop limit as ancillary data.
2880        self.checkTrafficClassAndHopLimit(ancbufsize=10240)
2881
2882    @testRecvTrafficClassAndHopLimit.client_skip
2883    def _testRecvTrafficClassAndHopLimit(self):
2884        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2885        self.sendToServer(MSG)
2886
2887    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
2888                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
2889    def testRecvTrafficClassAndHopLimitCMSG_SPACE(self):
2890        # Test receiving traffic class and hop limit, using
2891        # CMSG_SPACE() to calculate buffer size.
2892        self.checkTrafficClassAndHopLimit(
2893            ancbufsize=socket.CMSG_SPACE(SIZEOF_INT) * 2)
2894
2895    @testRecvTrafficClassAndHopLimitCMSG_SPACE.client_skip
2896    def _testRecvTrafficClassAndHopLimitCMSG_SPACE(self):
2897        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2898        self.sendToServer(MSG)
2899
2900    @requireAttrs(socket.socket, "sendmsg")
2901    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
2902                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
2903    def testSetTrafficClassAndHopLimit(self):
2904        # Test setting traffic class and hop limit on outgoing packet,
2905        # and receiving them at the other end.
2906        self.checkTrafficClassAndHopLimit(ancbufsize=10240,
2907                                          maxhop=self.hop_limit)
2908
2909    @testSetTrafficClassAndHopLimit.client_skip
2910    def _testSetTrafficClassAndHopLimit(self):
2911        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2912        self.assertEqual(
2913            self.sendmsgToServer([MSG],
2914                                 [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS,
2915                                   array.array("i", [self.traffic_class])),
2916                                  (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT,
2917                                   array.array("i", [self.hop_limit]))]),
2918            len(MSG))
2919
2920    @requireAttrs(socket.socket, "sendmsg")
2921    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
2922                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
2923    def testOddCmsgSize(self):
2924        # Try to send ancillary data with first item one byte too
2925        # long.  Fall back to sending with correct size if this fails,
2926        # and check that second item was handled correctly.
2927        self.checkTrafficClassAndHopLimit(ancbufsize=10240,
2928                                          maxhop=self.hop_limit)
2929
2930    @testOddCmsgSize.client_skip
2931    def _testOddCmsgSize(self):
2932        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2933        try:
2934            nbytes = self.sendmsgToServer(
2935                [MSG],
2936                [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS,
2937                  array.array("i", [self.traffic_class]).tobytes() + b"\x00"),
2938                 (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT,
2939                  array.array("i", [self.hop_limit]))])
2940        except socket.error as e:
2941            self.assertIsInstance(e.errno, int)
2942            nbytes = self.sendmsgToServer(
2943                [MSG],
2944                [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS,
2945                  array.array("i", [self.traffic_class])),
2946                 (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT,
2947                  array.array("i", [self.hop_limit]))])
2948            self.assertEqual(nbytes, len(MSG))
2949
2950    # Tests for proper handling of truncated ancillary data
2951
2952    def checkHopLimitTruncatedHeader(self, ancbufsize, ignoreflags=0):
2953        # Receive hop limit into ancbufsize bytes of ancillary data
2954        # space, which should be too small to contain the ancillary
2955        # data header (if ancbufsize is None, pass no second argument
2956        # to recvmsg()).  Check that data is MSG, MSG_CTRUNC is set
2957        # (unless included in ignoreflags), and no ancillary data is
2958        # returned.
2959        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
2960                                  socket.IPV6_RECVHOPLIMIT, 1)
2961        self.misc_event.set()
2962        args = () if ancbufsize is None else (ancbufsize,)
2963        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
2964                                                   len(MSG), *args)
2965
2966        self.assertEqual(msg, MSG)
2967        self.checkRecvmsgAddress(addr, self.cli_addr)
2968        self.assertEqual(ancdata, [])
2969        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC,
2970                        ignore=ignoreflags)
2971
2972    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
2973    def testCmsgTruncNoBufSize(self):
2974        # Check that no ancillary data is received when no ancillary
2975        # buffer size is provided.
2976        self.checkHopLimitTruncatedHeader(ancbufsize=None,
2977                                          # BSD seems to set
2978                                          # MSG_CTRUNC only if an item
2979                                          # has been partially
2980                                          # received.
2981                                          ignoreflags=socket.MSG_CTRUNC)
2982
2983    @testCmsgTruncNoBufSize.client_skip
2984    def _testCmsgTruncNoBufSize(self):
2985        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2986        self.sendToServer(MSG)
2987
2988    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
2989    def testSingleCmsgTrunc0(self):
2990        # Check that no ancillary data is received when ancillary
2991        # buffer size is zero.
2992        self.checkHopLimitTruncatedHeader(ancbufsize=0,
2993                                          ignoreflags=socket.MSG_CTRUNC)
2994
2995    @testSingleCmsgTrunc0.client_skip
2996    def _testSingleCmsgTrunc0(self):
2997        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
2998        self.sendToServer(MSG)
2999
3000    # Check that no ancillary data is returned for various non-zero
3001    # (but still too small) buffer sizes.
3002
3003    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
3004    def testSingleCmsgTrunc1(self):
3005        self.checkHopLimitTruncatedHeader(ancbufsize=1)
3006
3007    @testSingleCmsgTrunc1.client_skip
3008    def _testSingleCmsgTrunc1(self):
3009        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3010        self.sendToServer(MSG)
3011
3012    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
3013    def testSingleCmsgTrunc2Int(self):
3014        self.checkHopLimitTruncatedHeader(ancbufsize=2 * SIZEOF_INT)
3015
3016    @testSingleCmsgTrunc2Int.client_skip
3017    def _testSingleCmsgTrunc2Int(self):
3018        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3019        self.sendToServer(MSG)
3020
3021    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
3022    def testSingleCmsgTruncLen0Minus1(self):
3023        self.checkHopLimitTruncatedHeader(ancbufsize=socket.CMSG_LEN(0) - 1)
3024
3025    @testSingleCmsgTruncLen0Minus1.client_skip
3026    def _testSingleCmsgTruncLen0Minus1(self):
3027        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3028        self.sendToServer(MSG)
3029
3030    @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT")
3031    def testSingleCmsgTruncInData(self):
3032        # Test truncation of a control message inside its associated
3033        # data.  The message may be returned with its data truncated,
3034        # or not returned at all.
3035        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
3036                                  socket.IPV6_RECVHOPLIMIT, 1)
3037        self.misc_event.set()
3038        msg, ancdata, flags, addr = self.doRecvmsg(
3039            self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1)
3040
3041        self.assertEqual(msg, MSG)
3042        self.checkRecvmsgAddress(addr, self.cli_addr)
3043        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
3044
3045        self.assertLessEqual(len(ancdata), 1)
3046        if ancdata:
3047            cmsg_level, cmsg_type, cmsg_data = ancdata[0]
3048            self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
3049            self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT)
3050            self.assertLess(len(cmsg_data), SIZEOF_INT)
3051
3052    @testSingleCmsgTruncInData.client_skip
3053    def _testSingleCmsgTruncInData(self):
3054        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3055        self.sendToServer(MSG)
3056
3057    def checkTruncatedSecondHeader(self, ancbufsize, ignoreflags=0):
3058        # Receive traffic class and hop limit into ancbufsize bytes of
3059        # ancillary data space, which should be large enough to
3060        # contain the first item, but too small to contain the header
3061        # of the second.  Check that data is MSG, MSG_CTRUNC is set
3062        # (unless included in ignoreflags), and only one ancillary
3063        # data item is returned.
3064        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
3065                                  socket.IPV6_RECVHOPLIMIT, 1)
3066        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
3067                                  socket.IPV6_RECVTCLASS, 1)
3068        self.misc_event.set()
3069        msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
3070                                                   len(MSG), ancbufsize)
3071
3072        self.assertEqual(msg, MSG)
3073        self.checkRecvmsgAddress(addr, self.cli_addr)
3074        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC,
3075                        ignore=ignoreflags)
3076
3077        self.assertEqual(len(ancdata), 1)
3078        cmsg_level, cmsg_type, cmsg_data = ancdata[0]
3079        self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
3080        self.assertIn(cmsg_type, {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT})
3081        self.assertEqual(len(cmsg_data), SIZEOF_INT)
3082        a = array.array("i")
3083        a.frombytes(cmsg_data)
3084        self.assertGreaterEqual(a[0], 0)
3085        self.assertLessEqual(a[0], 255)
3086
3087    # Try the above test with various buffer sizes.
3088
3089    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
3090                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
3091    def testSecondCmsgTrunc0(self):
3092        self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT),
3093                                        ignoreflags=socket.MSG_CTRUNC)
3094
3095    @testSecondCmsgTrunc0.client_skip
3096    def _testSecondCmsgTrunc0(self):
3097        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3098        self.sendToServer(MSG)
3099
3100    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
3101                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
3102    def testSecondCmsgTrunc1(self):
3103        self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1)
3104
3105    @testSecondCmsgTrunc1.client_skip
3106    def _testSecondCmsgTrunc1(self):
3107        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3108        self.sendToServer(MSG)
3109
3110    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
3111                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
3112    def testSecondCmsgTrunc2Int(self):
3113        self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) +
3114                                        2 * SIZEOF_INT)
3115
3116    @testSecondCmsgTrunc2Int.client_skip
3117    def _testSecondCmsgTrunc2Int(self):
3118        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3119        self.sendToServer(MSG)
3120
3121    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
3122                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
3123    def testSecondCmsgTruncLen0Minus1(self):
3124        self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) +
3125                                        socket.CMSG_LEN(0) - 1)
3126
3127    @testSecondCmsgTruncLen0Minus1.client_skip
3128    def _testSecondCmsgTruncLen0Minus1(self):
3129        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3130        self.sendToServer(MSG)
3131
3132    @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT",
3133                  "IPV6_RECVTCLASS", "IPV6_TCLASS")
3134    def testSecomdCmsgTruncInData(self):
3135        # Test truncation of the second of two control messages inside
3136        # its associated data.
3137        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
3138                                  socket.IPV6_RECVHOPLIMIT, 1)
3139        self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
3140                                  socket.IPV6_RECVTCLASS, 1)
3141        self.misc_event.set()
3142        msg, ancdata, flags, addr = self.doRecvmsg(
3143            self.serv_sock, len(MSG),
3144            socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)
3145
3146        self.assertEqual(msg, MSG)
3147        self.checkRecvmsgAddress(addr, self.cli_addr)
3148        self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
3149
3150        cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}
3151
3152        cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0)
3153        self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
3154        cmsg_types.remove(cmsg_type)
3155        self.assertEqual(len(cmsg_data), SIZEOF_INT)
3156        a = array.array("i")
3157        a.frombytes(cmsg_data)
3158        self.assertGreaterEqual(a[0], 0)
3159        self.assertLessEqual(a[0], 255)
3160
3161        if ancdata:
3162            cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0)
3163            self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
3164            cmsg_types.remove(cmsg_type)
3165            self.assertLess(len(cmsg_data), SIZEOF_INT)
3166
3167        self.assertEqual(ancdata, [])
3168
3169    @testSecomdCmsgTruncInData.client_skip
3170    def _testSecomdCmsgTruncInData(self):
3171        self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
3172        self.sendToServer(MSG)
3173
3174
3175# Derive concrete test classes for different socket types.
3176
3177class SendrecvmsgUDPTestBase(SendrecvmsgDgramFlagsBase,
3178                             SendrecvmsgConnectionlessBase,
3179                             ThreadedSocketTestMixin, UDPTestBase):
3180    pass
3181
3182@requireAttrs(socket.socket, "sendmsg")
3183@unittest.skipUnless(thread, 'Threading required for this test.')
3184class SendmsgUDPTest(SendmsgConnectionlessTests, SendrecvmsgUDPTestBase):
3185    pass
3186
3187@requireAttrs(socket.socket, "recvmsg")
3188@unittest.skipUnless(thread, 'Threading required for this test.')
3189class RecvmsgUDPTest(RecvmsgTests, SendrecvmsgUDPTestBase):
3190    pass
3191
3192@requireAttrs(socket.socket, "recvmsg_into")
3193@unittest.skipUnless(thread, 'Threading required for this test.')
3194class RecvmsgIntoUDPTest(RecvmsgIntoTests, SendrecvmsgUDPTestBase):
3195    pass
3196
3197
3198class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase,
3199                              SendrecvmsgConnectionlessBase,
3200                              ThreadedSocketTestMixin, UDP6TestBase):
3201    pass
3202
3203@requireAttrs(socket.socket, "sendmsg")
3204@unittest.skipUnless(socket.has_ipv6, "Python not built with IPv6 support")
3205@requireSocket("AF_INET6", "SOCK_DGRAM")
3206@unittest.skipUnless(thread, 'Threading required for this test.')
3207class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase):
3208    pass
3209
3210@requireAttrs(socket.socket, "recvmsg")
3211@unittest.skipUnless(socket.has_ipv6, "Python not built with IPv6 support")
3212@requireSocket("AF_INET6", "SOCK_DGRAM")
3213@unittest.skipUnless(thread, 'Threading required for this test.')
3214class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase):
3215    pass
3216
3217@requireAttrs(socket.socket, "recvmsg_into")
3218@unittest.skipUnless(socket.has_ipv6, "Python not built with IPv6 support")
3219@requireSocket("AF_INET6", "SOCK_DGRAM")
3220@unittest.skipUnless(thread, 'Threading required for this test.')
3221class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase):
3222    pass
3223
3224@requireAttrs(socket.socket, "recvmsg")
3225@unittest.skipUnless(socket.has_ipv6, "Python not built with IPv6 support")
3226@requireAttrs(socket, "IPPROTO_IPV6")
3227@requireSocket("AF_INET6", "SOCK_DGRAM")
3228@unittest.skipUnless(thread, 'Threading required for this test.')
3229class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest,
3230                                      SendrecvmsgUDP6TestBase):
3231    pass
3232
3233@requireAttrs(socket.socket, "recvmsg_into")
3234@unittest.skipUnless(socket.has_ipv6, "Python not built with IPv6 support")
3235@requireAttrs(socket, "IPPROTO_IPV6")
3236@requireSocket("AF_INET6", "SOCK_DGRAM")
3237@unittest.skipUnless(thread, 'Threading required for this test.')
3238class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin,
3239                                          RFC3542AncillaryTest,
3240                                          SendrecvmsgUDP6TestBase):
3241    pass
3242
3243
3244class SendrecvmsgTCPTestBase(SendrecvmsgConnectedBase,
3245                             ConnectedStreamTestMixin, TCPTestBase):
3246    pass
3247
3248@requireAttrs(socket.socket, "sendmsg")
3249@unittest.skipUnless(thread, 'Threading required for this test.')
3250class SendmsgTCPTest(SendmsgStreamTests, SendrecvmsgTCPTestBase):
3251    pass
3252
3253@requireAttrs(socket.socket, "recvmsg")
3254@unittest.skipUnless(thread, 'Threading required for this test.')
3255class RecvmsgTCPTest(RecvmsgTests, RecvmsgGenericStreamTests,
3256                     SendrecvmsgTCPTestBase):
3257    pass
3258
3259@requireAttrs(socket.socket, "recvmsg_into")
3260@unittest.skipUnless(thread, 'Threading required for this test.')
3261class RecvmsgIntoTCPTest(RecvmsgIntoTests, RecvmsgGenericStreamTests,
3262                         SendrecvmsgTCPTestBase):
3263    pass
3264
3265
3266class SendrecvmsgSCTPStreamTestBase(SendrecvmsgSCTPFlagsBase,
3267                                    SendrecvmsgConnectedBase,
3268                                    ConnectedStreamTestMixin, SCTPStreamBase):
3269    pass
3270
3271@requireAttrs(socket.socket, "sendmsg")
3272@requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP")
3273@unittest.skipUnless(thread, 'Threading required for this test.')
3274class SendmsgSCTPStreamTest(SendmsgStreamTests, SendrecvmsgSCTPStreamTestBase):
3275    pass
3276
3277@requireAttrs(socket.socket, "recvmsg")
3278@requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP")
3279@unittest.skipUnless(thread, 'Threading required for this test.')
3280class RecvmsgSCTPStreamTest(RecvmsgTests, RecvmsgGenericStreamTests,
3281                            SendrecvmsgSCTPStreamTestBase):
3282    pass
3283
3284@requireAttrs(socket.socket, "recvmsg_into")
3285@requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP")
3286@unittest.skipUnless(thread, 'Threading required for this test.')
3287class RecvmsgIntoSCTPStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests,
3288                                SendrecvmsgSCTPStreamTestBase):
3289    pass
3290
3291
3292class SendrecvmsgUnixStreamTestBase(SendrecvmsgConnectedBase,
3293                                    ConnectedStreamTestMixin, UnixStreamBase):
3294    pass
3295
3296@requireAttrs(socket.socket, "sendmsg")
3297@requireAttrs(socket, "AF_UNIX")
3298@unittest.skipUnless(thread, 'Threading required for this test.')
3299class SendmsgUnixStreamTest(SendmsgStreamTests, SendrecvmsgUnixStreamTestBase):
3300    pass
3301
3302@requireAttrs(socket.socket, "recvmsg")
3303@requireAttrs(socket, "AF_UNIX")
3304@unittest.skipUnless(thread, 'Threading required for this test.')
3305class RecvmsgUnixStreamTest(RecvmsgTests, RecvmsgGenericStreamTests,
3306                            SendrecvmsgUnixStreamTestBase):
3307    pass
3308
3309@requireAttrs(socket.socket, "recvmsg_into")
3310@requireAttrs(socket, "AF_UNIX")
3311@unittest.skipUnless(thread, 'Threading required for this test.')
3312class RecvmsgIntoUnixStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests,
3313                                SendrecvmsgUnixStreamTestBase):
3314    pass
3315
3316@requireAttrs(socket.socket, "sendmsg", "recvmsg")
3317@requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS")
3318@unittest.skipUnless(thread, 'Threading required for this test.')
3319class RecvmsgSCMRightsStreamTest(SCMRightsTest, SendrecvmsgUnixStreamTestBase):
3320    pass
3321
3322@requireAttrs(socket.socket, "sendmsg", "recvmsg_into")
3323@requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS")
3324@unittest.skipUnless(thread, 'Threading required for this test.')
3325class RecvmsgIntoSCMRightsStreamTest(RecvmsgIntoMixin, SCMRightsTest,
3326                                     SendrecvmsgUnixStreamTestBase):
3327    pass
3328
3329
3330# Test interrupting the interruptible send/receive methods with a
3331# signal when a timeout is set.  These tests avoid having multiple
3332# threads alive during the test so that the OS cannot deliver the
3333# signal to the wrong one.
3334
3335class InterruptedTimeoutBase(unittest.TestCase):
3336    # Base class for interrupted send/receive tests.  Installs an
3337    # empty handler for SIGALRM and removes it on teardown, along with
3338    # any scheduled alarms.
3339
3340    def setUp(self):
3341        super().setUp()
3342        orig_alrm_handler = signal.signal(signal.SIGALRM,
3343                                          lambda signum, frame: None)
3344        self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler)
3345        self.addCleanup(self.setAlarm, 0)
3346
3347    # Timeout for socket operations
3348    timeout = 4.0
3349
3350    # Provide setAlarm() method to schedule delivery of SIGALRM after
3351    # given number of seconds, or cancel it if zero, and an
3352    # appropriate time value to use.  Use setitimer() if available.
3353    if hasattr(signal, "setitimer"):
3354        alarm_time = 0.05
3355
3356        def setAlarm(self, seconds):
3357            signal.setitimer(signal.ITIMER_REAL, seconds)
3358    else:
3359        # Old systems may deliver the alarm up to one second early
3360        alarm_time = 2
3361
3362        def setAlarm(self, seconds):
3363            signal.alarm(seconds)
3364
3365
3366# Require siginterrupt() in order to ensure that system calls are
3367# interrupted by default.
3368@requireAttrs(signal, "siginterrupt")
3369@unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"),
3370                     "Don't have signal.alarm or signal.setitimer")
3371class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase):
3372    # Test interrupting the recv*() methods with signals when a
3373    # timeout is set.
3374
3375    def setUp(self):
3376        super().setUp()
3377        self.serv.settimeout(self.timeout)
3378
3379    def checkInterruptedRecv(self, func, *args, **kwargs):
3380        # Check that func(*args, **kwargs) raises socket.error with an
3381        # errno of EINTR when interrupted by a signal.
3382        self.setAlarm(self.alarm_time)
3383        with self.assertRaises(socket.error) as cm:
3384            func(*args, **kwargs)
3385        self.assertNotIsInstance(cm.exception, socket.timeout)
3386        self.assertEqual(cm.exception.errno, errno.EINTR)
3387
3388    def testInterruptedRecvTimeout(self):
3389        self.checkInterruptedRecv(self.serv.recv, 1024)
3390
3391    def testInterruptedRecvIntoTimeout(self):
3392        self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024))
3393
3394    def testInterruptedRecvfromTimeout(self):
3395        self.checkInterruptedRecv(self.serv.recvfrom, 1024)
3396
3397    def testInterruptedRecvfromIntoTimeout(self):
3398        self.checkInterruptedRecv(self.serv.recvfrom_into, bytearray(1024))
3399
3400    @requireAttrs(socket.socket, "recvmsg")
3401    def testInterruptedRecvmsgTimeout(self):
3402        self.checkInterruptedRecv(self.serv.recvmsg, 1024)
3403
3404    @requireAttrs(socket.socket, "recvmsg_into")
3405    def testInterruptedRecvmsgIntoTimeout(self):
3406        self.checkInterruptedRecv(self.serv.recvmsg_into, [bytearray(1024)])
3407
3408
3409# Require siginterrupt() in order to ensure that system calls are
3410# interrupted by default.
3411@requireAttrs(signal, "siginterrupt")
3412@unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"),
3413                     "Don't have signal.alarm or signal.setitimer")
3414@unittest.skipUnless(thread, 'Threading required for this test.')
3415class InterruptedSendTimeoutTest(InterruptedTimeoutBase,
3416                                 ThreadSafeCleanupTestCase,
3417                                 SocketListeningTestMixin, TCPTestBase):
3418    # Test interrupting the interruptible send*() methods with signals
3419    # when a timeout is set.
3420
3421    def setUp(self):
3422        super().setUp()
3423        self.serv_conn = self.newSocket()
3424        self.addCleanup(self.serv_conn.close)
3425        # Use a thread to complete the connection, but wait for it to
3426        # terminate before running the test, so that there is only one
3427        # thread to accept the signal.
3428        cli_thread = threading.Thread(target=self.doConnect)
3429        cli_thread.start()
3430        self.cli_conn, addr = self.serv.accept()
3431        self.addCleanup(self.cli_conn.close)
3432        cli_thread.join()
3433        self.serv_conn.settimeout(self.timeout)
3434
3435    def doConnect(self):
3436        self.serv_conn.connect(self.serv_addr)
3437
3438    def checkInterruptedSend(self, func, *args, **kwargs):
3439        # Check that func(*args, **kwargs), run in a loop, raises
3440        # socket.error with an errno of EINTR when interrupted by a
3441        # signal.
3442        with self.assertRaises(socket.error) as cm:
3443            while True:
3444                self.setAlarm(self.alarm_time)
3445                func(*args, **kwargs)
3446        self.assertNotIsInstance(cm.exception, socket.timeout)
3447        self.assertEqual(cm.exception.errno, errno.EINTR)
3448
3449    # Issue #12958: The following tests have problems on Mac OS X
3450    @support.anticipate_failure(sys.platform == "darwin")
3451    def testInterruptedSendTimeout(self):
3452        self.checkInterruptedSend(self.serv_conn.send, b"a"*512)
3453
3454    @support.anticipate_failure(sys.platform == "darwin")
3455    def testInterruptedSendtoTimeout(self):
3456        # Passing an actual address here as Python's wrapper for
3457        # sendto() doesn't allow passing a zero-length one; POSIX
3458        # requires that the address is ignored since the socket is
3459        # connection-mode, however.
3460        self.checkInterruptedSend(self.serv_conn.sendto, b"a"*512,
3461                                  self.serv_addr)
3462
3463    @support.anticipate_failure(sys.platform == "darwin")
3464    @requireAttrs(socket.socket, "sendmsg")
3465    def testInterruptedSendmsgTimeout(self):
3466        self.checkInterruptedSend(self.serv_conn.sendmsg, [b"a"*512])
3467
3468
3469@unittest.skipUnless(thread, 'Threading required for this test.')
3470class TCPCloserTest(ThreadedTCPSocketTest):
3471
3472    def testClose(self):
3473        conn, addr = self.serv.accept()
3474        conn.close()
3475
3476        sd = self.cli
3477        read, write, err = select.select([sd], [], [], 1.0)
3478        self.assertEqual(read, [sd])
3479        self.assertEqual(sd.recv(1), b'')
3480
3481        # Calling close() many times should be safe.
3482        conn.close()
3483        conn.close()
3484
3485    def _testClose(self):
3486        self.cli.connect((HOST, self.port))
3487        time.sleep(1.0)
3488
3489@unittest.skipUnless(thread, 'Threading required for this test.')
3490class BasicSocketPairTest(SocketPairTest):
3491
3492    def __init__(self, methodName='runTest'):
3493        SocketPairTest.__init__(self, methodName=methodName)
3494
3495    def _check_defaults(self, sock):
3496        self.assertIsInstance(sock, socket.socket)
3497        if hasattr(socket, 'AF_UNIX'):
3498            self.assertEqual(sock.family, socket.AF_UNIX)
3499        else:
3500            self.assertEqual(sock.family, socket.AF_INET)
3501        self.assertEqual(sock.type, socket.SOCK_STREAM)
3502        self.assertEqual(sock.proto, 0)
3503
3504    def _testDefaults(self):
3505        self._check_defaults(self.cli)
3506
3507    def testDefaults(self):
3508        self._check_defaults(self.serv)
3509
3510    def testRecv(self):
3511        msg = self.serv.recv(1024)
3512        self.assertEqual(msg, MSG)
3513
3514    def _testRecv(self):
3515        self.cli.send(MSG)
3516
3517    def testSend(self):
3518        self.serv.send(MSG)
3519
3520    def _testSend(self):
3521        msg = self.cli.recv(1024)
3522        self.assertEqual(msg, MSG)
3523
3524@unittest.skipUnless(thread, 'Threading required for this test.')
3525class NonBlockingTCPTests(ThreadedTCPSocketTest):
3526
3527    def __init__(self, methodName='runTest'):
3528        ThreadedTCPSocketTest.__init__(self, methodName=methodName)
3529
3530    def testSetBlocking(self):
3531        # Testing whether set blocking works
3532        self.serv.setblocking(0)
3533        start = time.time()
3534        try:
3535            self.serv.accept()
3536        except socket.error:
3537            pass
3538        end = time.time()
3539        self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.")
3540
3541    def _testSetBlocking(self):
3542        pass
3543
3544    if hasattr(socket, "SOCK_NONBLOCK"):
3545        @support.requires_linux_version(2, 6, 28)
3546        def testInitNonBlocking(self):
3547            # reinit server socket
3548            self.serv.close()
3549            self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM |
3550                                                      socket.SOCK_NONBLOCK)
3551            self.port = support.bind_port(self.serv)
3552            self.serv.listen(1)
3553            # actual testing
3554            start = time.time()
3555            try:
3556                self.serv.accept()
3557            except socket.error:
3558                pass
3559            end = time.time()
3560            self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.")
3561
3562        def _testInitNonBlocking(self):
3563            pass
3564
3565    def testInheritFlags(self):
3566        # Issue #7995: when calling accept() on a listening socket with a
3567        # timeout, the resulting socket should not be non-blocking.
3568        self.serv.settimeout(10)
3569        try:
3570            conn, addr = self.serv.accept()
3571            message = conn.recv(len(MSG))
3572        finally:
3573            conn.close()
3574            self.serv.settimeout(None)
3575
3576    def _testInheritFlags(self):
3577        time.sleep(0.1)
3578        self.cli.connect((HOST, self.port))
3579        time.sleep(0.5)
3580        self.cli.send(MSG)
3581
3582    def testAccept(self):
3583        # Testing non-blocking accept
3584        self.serv.setblocking(0)
3585        try:
3586            conn, addr = self.serv.accept()
3587        except socket.error:
3588            pass
3589        else:
3590            self.fail("Error trying to do non-blocking accept.")
3591        read, write, err = select.select([self.serv], [], [])
3592        if self.serv in read:
3593            conn, addr = self.serv.accept()
3594            conn.close()
3595        else:
3596            self.fail("Error trying to do accept after select.")
3597
3598    def _testAccept(self):
3599        time.sleep(0.1)
3600        self.cli.connect((HOST, self.port))
3601
3602    def testConnect(self):
3603        # Testing non-blocking connect
3604        conn, addr = self.serv.accept()
3605        conn.close()
3606
3607    def _testConnect(self):
3608        self.cli.settimeout(10)
3609        self.cli.connect((HOST, self.port))
3610
3611    def testRecv(self):
3612        # Testing non-blocking recv
3613        conn, addr = self.serv.accept()
3614        conn.setblocking(0)
3615        try:
3616            msg = conn.recv(len(MSG))
3617        except socket.error:
3618            pass
3619        else:
3620            self.fail("Error trying to do non-blocking recv.")
3621        read, write, err = select.select([conn], [], [])
3622        if conn in read:
3623            msg = conn.recv(len(MSG))
3624            conn.close()
3625            self.assertEqual(msg, MSG)
3626        else:
3627            self.fail("Error during select call to non-blocking socket.")
3628
3629    def _testRecv(self):
3630        self.cli.connect((HOST, self.port))
3631        time.sleep(0.1)
3632        self.cli.send(MSG)
3633
3634@unittest.skipUnless(thread, 'Threading required for this test.')
3635class FileObjectClassTestCase(SocketConnectedTest):
3636    """Unit tests for the object returned by socket.makefile()
3637
3638    self.read_file is the io object returned by makefile() on
3639    the client connection.  You can read from this file to
3640    get output from the server.
3641
3642    self.write_file is the io object returned by makefile() on the
3643    server connection.  You can write to this file to send output
3644    to the client.
3645    """
3646
3647    bufsize = -1 # Use default buffer size
3648    encoding = 'utf-8'
3649    errors = 'strict'
3650    newline = None
3651
3652    read_mode = 'rb'
3653    read_msg = MSG
3654    write_mode = 'wb'
3655    write_msg = MSG
3656
3657    def __init__(self, methodName='runTest'):
3658        SocketConnectedTest.__init__(self, methodName=methodName)
3659
3660    def setUp(self):
3661        self.evt1, self.evt2, self.serv_finished, self.cli_finished = [
3662            threading.Event() for i in range(4)]
3663        SocketConnectedTest.setUp(self)
3664        self.read_file = self.cli_conn.makefile(
3665            self.read_mode, self.bufsize,
3666            encoding = self.encoding,
3667            errors = self.errors,
3668            newline = self.newline)
3669
3670    def tearDown(self):
3671        self.serv_finished.set()
3672        self.read_file.close()
3673        self.assertTrue(self.read_file.closed)
3674        self.read_file = None
3675        SocketConnectedTest.tearDown(self)
3676
3677    def clientSetUp(self):
3678        SocketConnectedTest.clientSetUp(self)
3679        self.write_file = self.serv_conn.makefile(
3680            self.write_mode, self.bufsize,
3681            encoding = self.encoding,
3682            errors = self.errors,
3683            newline = self.newline)
3684
3685    def clientTearDown(self):
3686        self.cli_finished.set()
3687        self.write_file.close()
3688        self.assertTrue(self.write_file.closed)
3689        self.write_file = None
3690        SocketConnectedTest.clientTearDown(self)
3691
3692    def testReadAfterTimeout(self):
3693        # Issue #7322: A file object must disallow further reads
3694        # after a timeout has occurred.
3695        self.cli_conn.settimeout(1)
3696        self.read_file.read(3)
3697        # First read raises a timeout
3698        self.assertRaises(socket.timeout, self.read_file.read, 1)
3699        # Second read is disallowed
3700        with self.assertRaises(IOError) as ctx:
3701            self.read_file.read(1)
3702        self.assertIn("cannot read from timed out object", str(ctx.exception))
3703
3704    def _testReadAfterTimeout(self):
3705        self.write_file.write(self.write_msg[0:3])
3706        self.write_file.flush()
3707        self.serv_finished.wait()
3708
3709    def testSmallRead(self):
3710        # Performing small file read test
3711        first_seg = self.read_file.read(len(self.read_msg)-3)
3712        second_seg = self.read_file.read(3)
3713        msg = first_seg + second_seg
3714        self.assertEqual(msg, self.read_msg)
3715
3716    def _testSmallRead(self):
3717        self.write_file.write(self.write_msg)
3718        self.write_file.flush()
3719
3720    def testFullRead(self):
3721        # read until EOF
3722        msg = self.read_file.read()
3723        self.assertEqual(msg, self.read_msg)
3724
3725    def _testFullRead(self):
3726        self.write_file.write(self.write_msg)
3727        self.write_file.close()
3728
3729    def testUnbufferedRead(self):
3730        # Performing unbuffered file read test
3731        buf = type(self.read_msg)()
3732        while 1:
3733            char = self.read_file.read(1)
3734            if not char:
3735                break
3736            buf += char
3737        self.assertEqual(buf, self.read_msg)
3738
3739    def _testUnbufferedRead(self):
3740        self.write_file.write(self.write_msg)
3741        self.write_file.flush()
3742
3743    def testReadline(self):
3744        # Performing file readline test
3745        line = self.read_file.readline()
3746        self.assertEqual(line, self.read_msg)
3747
3748    def _testReadline(self):
3749        self.write_file.write(self.write_msg)
3750        self.write_file.flush()
3751
3752    def testCloseAfterMakefile(self):
3753        # The file returned by makefile should keep the socket open.
3754        self.cli_conn.close()
3755        # read until EOF
3756        msg = self.read_file.read()
3757        self.assertEqual(msg, self.read_msg)
3758
3759    def _testCloseAfterMakefile(self):
3760        self.write_file.write(self.write_msg)
3761        self.write_file.flush()
3762
3763    def testMakefileAfterMakefileClose(self):
3764        self.read_file.close()
3765        msg = self.cli_conn.recv(len(MSG))
3766        if isinstance(self.read_msg, str):
3767            msg = msg.decode()
3768        self.assertEqual(msg, self.read_msg)
3769
3770    def _testMakefileAfterMakefileClose(self):
3771        self.write_file.write(self.write_msg)
3772        self.write_file.flush()
3773
3774    def testClosedAttr(self):
3775        self.assertTrue(not self.read_file.closed)
3776
3777    def _testClosedAttr(self):
3778        self.assertTrue(not self.write_file.closed)
3779
3780    def testAttributes(self):
3781        self.assertEqual(self.read_file.mode, self.read_mode)
3782        self.assertEqual(self.read_file.name, self.cli_conn.fileno())
3783
3784    def _testAttributes(self):
3785        self.assertEqual(self.write_file.mode, self.write_mode)
3786        self.assertEqual(self.write_file.name, self.serv_conn.fileno())
3787
3788    def testRealClose(self):
3789        self.read_file.close()
3790        self.assertRaises(ValueError, self.read_file.fileno)
3791        self.cli_conn.close()
3792        self.assertRaises(socket.error, self.cli_conn.getsockname)
3793
3794    def _testRealClose(self):
3795        pass
3796
3797
3798class FileObjectInterruptedTestCase(unittest.TestCase):
3799    """Test that the file object correctly handles EINTR internally."""
3800
3801    class MockSocket(object):
3802        def __init__(self, recv_funcs=()):
3803            # A generator that returns callables that we'll call for each
3804            # call to recv().
3805            self._recv_step = iter(recv_funcs)
3806
3807        def recv_into(self, buffer):
3808            data = next(self._recv_step)()
3809            assert len(buffer) >= len(data)
3810            buffer[:len(data)] = data
3811            return len(data)
3812
3813        def _decref_socketios(self):
3814            pass
3815
3816        def _textiowrap_for_test(self, buffering=-1):
3817            raw = socket.SocketIO(self, "r")
3818            if buffering < 0:
3819                buffering = io.DEFAULT_BUFFER_SIZE
3820            if buffering == 0:
3821                return raw
3822            buffer = io.BufferedReader(raw, buffering)
3823            text = io.TextIOWrapper(buffer, None, None)
3824            text.mode = "rb"
3825            return text
3826
3827    @staticmethod
3828    def _raise_eintr():
3829        raise socket.error(errno.EINTR, "interrupted")
3830
3831    def _textiowrap_mock_socket(self, mock, buffering=-1):
3832        raw = socket.SocketIO(mock, "r")
3833        if buffering < 0:
3834            buffering = io.DEFAULT_BUFFER_SIZE
3835        if buffering == 0:
3836            return raw
3837        buffer = io.BufferedReader(raw, buffering)
3838        text = io.TextIOWrapper(buffer, None, None)
3839        text.mode = "rb"
3840        return text
3841
3842    def _test_readline(self, size=-1, buffering=-1):
3843        mock_sock = self.MockSocket(recv_funcs=[
3844                lambda : b"This is the first line\nAnd the sec",
3845                self._raise_eintr,
3846                lambda : b"ond line is here\n",
3847                lambda : b"",
3848                lambda : b"",  # XXX(gps): io library does an extra EOF read
3849            ])
3850        fo = mock_sock._textiowrap_for_test(buffering=buffering)
3851        self.assertEqual(fo.readline(size), "This is the first line\n")
3852        self.assertEqual(fo.readline(size), "And the second line is here\n")
3853
3854    def _test_read(self, size=-1, buffering=-1):
3855        mock_sock = self.MockSocket(recv_funcs=[
3856                lambda : b"This is the first line\nAnd the sec",
3857                self._raise_eintr,
3858                lambda : b"ond line is here\n",
3859                lambda : b"",
3860                lambda : b"",  # XXX(gps): io library does an extra EOF read
3861            ])
3862        expecting = (b"This is the first line\n"
3863                     b"And the second line is here\n")
3864        fo = mock_sock._textiowrap_for_test(buffering=buffering)
3865        if buffering == 0:
3866            data = b''
3867        else:
3868            data = ''
3869            expecting = expecting.decode('utf-8')
3870        while len(data) != len(expecting):
3871            part = fo.read(size)
3872            if not part:
3873                break
3874            data += part
3875        self.assertEqual(data, expecting)
3876
3877    def test_default(self):
3878        self._test_readline()
3879        self._test_readline(size=100)
3880        self._test_read()
3881        self._test_read(size=100)
3882
3883    def test_with_1k_buffer(self):
3884        self._test_readline(buffering=1024)
3885        self._test_readline(size=100, buffering=1024)
3886        self._test_read(buffering=1024)
3887        self._test_read(size=100, buffering=1024)
3888
3889    def _test_readline_no_buffer(self, size=-1):
3890        mock_sock = self.MockSocket(recv_funcs=[
3891                lambda : b"a",
3892                lambda : b"\n",
3893                lambda : b"B",
3894                self._raise_eintr,
3895                lambda : b"b",
3896                lambda : b"",
3897            ])
3898        fo = mock_sock._textiowrap_for_test(buffering=0)
3899        self.assertEqual(fo.readline(size), b"a\n")
3900        self.assertEqual(fo.readline(size), b"Bb")
3901
3902    def test_no_buffer(self):
3903        self._test_readline_no_buffer()
3904        self._test_readline_no_buffer(size=4)
3905        self._test_read(buffering=0)
3906        self._test_read(size=100, buffering=0)
3907
3908
3909class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
3910
3911    """Repeat the tests from FileObjectClassTestCase with bufsize==0.
3912
3913    In this case (and in this case only), it should be possible to
3914    create a file object, read a line from it, create another file
3915    object, read another line from it, without loss of data in the
3916    first file object's buffer.  Note that http.client relies on this
3917    when reading multiple requests from the same socket."""
3918
3919    bufsize = 0 # Use unbuffered mode
3920
3921    def testUnbufferedReadline(self):
3922        # Read a line, create a new file object, read another line with it
3923        line = self.read_file.readline() # first line
3924        self.assertEqual(line, b"A. " + self.write_msg) # first line
3925        self.read_file = self.cli_conn.makefile('rb', 0)
3926        line = self.read_file.readline() # second line
3927        self.assertEqual(line, b"B. " + self.write_msg) # second line
3928
3929    def _testUnbufferedReadline(self):
3930        self.write_file.write(b"A. " + self.write_msg)
3931        self.write_file.write(b"B. " + self.write_msg)
3932        self.write_file.flush()
3933
3934    def testMakefileClose(self):
3935        # The file returned by makefile should keep the socket open...
3936        self.cli_conn.close()
3937        msg = self.cli_conn.recv(1024)
3938        self.assertEqual(msg, self.read_msg)
3939        # ...until the file is itself closed
3940        self.read_file.close()
3941        self.assertRaises(socket.error, self.cli_conn.recv, 1024)
3942
3943    def _testMakefileClose(self):
3944        self.write_file.write(self.write_msg)
3945        self.write_file.flush()
3946
3947    def testMakefileCloseSocketDestroy(self):
3948        refcount_before = sys.getrefcount(self.cli_conn)
3949        self.read_file.close()
3950        refcount_after = sys.getrefcount(self.cli_conn)
3951        self.assertEqual(refcount_before - 1, refcount_after)
3952
3953    def _testMakefileCloseSocketDestroy(self):
3954        pass
3955
3956    # Non-blocking ops
3957    # NOTE: to set `read_file` as non-blocking, we must call
3958    # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp).
3959
3960    def testSmallReadNonBlocking(self):
3961        self.cli_conn.setblocking(False)
3962        self.assertEqual(self.read_file.readinto(bytearray(10)), None)
3963        self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None)
3964        self.evt1.set()
3965        self.evt2.wait(1.0)
3966        first_seg = self.read_file.read(len(self.read_msg) - 3)
3967        if first_seg is None:
3968            # Data not arrived (can happen under Windows), wait a bit
3969            time.sleep(0.5)
3970            first_seg = self.read_file.read(len(self.read_msg) - 3)
3971        buf = bytearray(10)
3972        n = self.read_file.readinto(buf)
3973        self.assertEqual(n, 3)
3974        msg = first_seg + buf[:n]
3975        self.assertEqual(msg, self.read_msg)
3976        self.assertEqual(self.read_file.readinto(bytearray(16)), None)
3977        self.assertEqual(self.read_file.read(1), None)
3978
3979    def _testSmallReadNonBlocking(self):
3980        self.evt1.wait(1.0)
3981        self.write_file.write(self.write_msg)
3982        self.write_file.flush()
3983        self.evt2.set()
3984        # Avoid cloding the socket before the server test has finished,
3985        # otherwise system recv() will return 0 instead of EWOULDBLOCK.
3986        self.serv_finished.wait(5.0)
3987
3988    def testWriteNonBlocking(self):
3989        self.cli_finished.wait(5.0)
3990        # The client thread can't skip directly - the SkipTest exception
3991        # would appear as a failure.
3992        if self.serv_skipped:
3993            self.skipTest(self.serv_skipped)
3994
3995    def _testWriteNonBlocking(self):
3996        self.serv_skipped = None
3997        self.serv_conn.setblocking(False)
3998        # Try to saturate the socket buffer pipe with repeated large writes.
3999        BIG = b"x" * (1024 ** 2)
4000        LIMIT = 10
4001        # The first write() succeeds since a chunk of data can be buffered
4002        n = self.write_file.write(BIG)
4003        self.assertGreater(n, 0)
4004        for i in range(LIMIT):
4005            n = self.write_file.write(BIG)
4006            if n is None:
4007                # Succeeded
4008                break
4009            self.assertGreater(n, 0)
4010        else:
4011            # Let us know that this test didn't manage to establish
4012            # the expected conditions. This is not a failure in itself but,
4013            # if it happens repeatedly, the test should be fixed.
4014            self.serv_skipped = "failed to saturate the socket buffer"
4015
4016
4017class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
4018
4019    bufsize = 1 # Default-buffered for reading; line-buffered for writing
4020
4021
4022class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
4023
4024    bufsize = 2 # Exercise the buffering code
4025
4026
4027class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase):
4028    """Tests for socket.makefile() in text mode (rather than binary)"""
4029
4030    read_mode = 'r'
4031    read_msg = MSG.decode('utf-8')
4032    write_mode = 'wb'
4033    write_msg = MSG
4034    newline = ''
4035
4036
4037class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase):
4038    """Tests for socket.makefile() in text mode (rather than binary)"""
4039
4040    read_mode = 'rb'
4041    read_msg = MSG
4042    write_mode = 'w'
4043    write_msg = MSG.decode('utf-8')
4044    newline = ''
4045
4046
4047class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase):
4048    """Tests for socket.makefile() in text mode (rather than binary)"""
4049
4050    read_mode = 'r'
4051    read_msg = MSG.decode('utf-8')
4052    write_mode = 'w'
4053    write_msg = MSG.decode('utf-8')
4054    newline = ''
4055
4056
4057class NetworkConnectionTest(object):
4058    """Prove network connection."""
4059
4060    def clientSetUp(self):
4061        # We're inherited below by BasicTCPTest2, which also inherits
4062        # BasicTCPTest, which defines self.port referenced below.
4063        self.cli = socket.create_connection((HOST, self.port))
4064        self.serv_conn = self.cli
4065
4066class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
4067    """Tests that NetworkConnection does not break existing TCP functionality.
4068    """
4069
4070class NetworkConnectionNoServer(unittest.TestCase):
4071
4072    class MockSocket(socket.socket):
4073        def connect(self, *args):
4074            raise socket.timeout('timed out')
4075
4076    @contextlib.contextmanager
4077    def mocked_socket_module(self):
4078        """Return a socket which times out on connect"""
4079        old_socket = socket.socket
4080        socket.socket = self.MockSocket
4081        try:
4082            yield
4083        finally:
4084            socket.socket = old_socket
4085
4086    def test_connect(self):
4087        port = support.find_unused_port()
4088        cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4089        self.addCleanup(cli.close)
4090        with self.assertRaises(socket.error) as cm:
4091            cli.connect((HOST, port))
4092        self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
4093
4094    def test_create_connection(self):
4095        # Issue #9792: errors raised by create_connection() should have
4096        # a proper errno attribute.
4097        port = support.find_unused_port()
4098        with self.assertRaises(socket.error) as cm:
4099            socket.create_connection((HOST, port))
4100        self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
4101
4102    def test_create_connection_timeout(self):
4103        # Issue #9792: create_connection() should not recast timeout errors
4104        # as generic socket errors.
4105        with self.mocked_socket_module():
4106            with self.assertRaises(socket.timeout):
4107                socket.create_connection((HOST, 1234))
4108
4109
4110@unittest.skipUnless(thread, 'Threading required for this test.')
4111class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
4112
4113    def __init__(self, methodName='runTest'):
4114        SocketTCPTest.__init__(self, methodName=methodName)
4115        ThreadableTest.__init__(self)
4116
4117    def clientSetUp(self):
4118        self.source_port = support.find_unused_port()
4119
4120    def clientTearDown(self):
4121        self.cli.close()
4122        self.cli = None
4123        ThreadableTest.clientTearDown(self)
4124
4125    def _justAccept(self):
4126        conn, addr = self.serv.accept()
4127        conn.close()
4128
4129    testFamily = _justAccept
4130    def _testFamily(self):
4131        self.cli = socket.create_connection((HOST, self.port), timeout=30)
4132        self.addCleanup(self.cli.close)
4133        self.assertEqual(self.cli.family, 2)
4134
4135    testSourceAddress = _justAccept
4136    def _testSourceAddress(self):
4137        self.cli = socket.create_connection((HOST, self.port), timeout=30,
4138                source_address=('', self.source_port))
4139        self.addCleanup(self.cli.close)
4140        self.assertEqual(self.cli.getsockname()[1], self.source_port)
4141        # The port number being used is sufficient to show that the bind()
4142        # call happened.
4143
4144    testTimeoutDefault = _justAccept
4145    def _testTimeoutDefault(self):
4146        # passing no explicit timeout uses socket's global default
4147        self.assertTrue(socket.getdefaulttimeout() is None)
4148        socket.setdefaulttimeout(42)
4149        try:
4150            self.cli = socket.create_connection((HOST, self.port))
4151            self.addCleanup(self.cli.close)
4152        finally:
4153            socket.setdefaulttimeout(None)
4154        self.assertEqual(self.cli.gettimeout(), 42)
4155
4156    testTimeoutNone = _justAccept
4157    def _testTimeoutNone(self):
4158        # None timeout means the same as sock.settimeout(None)
4159        self.assertTrue(socket.getdefaulttimeout() is None)
4160        socket.setdefaulttimeout(30)
4161        try:
4162            self.cli = socket.create_connection((HOST, self.port), timeout=None)
4163            self.addCleanup(self.cli.close)
4164        finally:
4165            socket.setdefaulttimeout(None)
4166        self.assertEqual(self.cli.gettimeout(), None)
4167
4168    testTimeoutValueNamed = _justAccept
4169    def _testTimeoutValueNamed(self):
4170        self.cli = socket.create_connection((HOST, self.port), timeout=30)
4171        self.assertEqual(self.cli.gettimeout(), 30)
4172
4173    testTimeoutValueNonamed = _justAccept
4174    def _testTimeoutValueNonamed(self):
4175        self.cli = socket.create_connection((HOST, self.port), 30)
4176        self.addCleanup(self.cli.close)
4177        self.assertEqual(self.cli.gettimeout(), 30)
4178
4179@unittest.skipUnless(thread, 'Threading required for this test.')
4180class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
4181
4182    def __init__(self, methodName='runTest'):
4183        SocketTCPTest.__init__(self, methodName=methodName)
4184        ThreadableTest.__init__(self)
4185
4186    def clientSetUp(self):
4187        pass
4188
4189    def clientTearDown(self):
4190        self.cli.close()
4191        self.cli = None
4192        ThreadableTest.clientTearDown(self)
4193
4194    def testInsideTimeout(self):
4195        conn, addr = self.serv.accept()
4196        self.addCleanup(conn.close)
4197        time.sleep(3)
4198        conn.send(b"done!")
4199    testOutsideTimeout = testInsideTimeout
4200
4201    def _testInsideTimeout(self):
4202        self.cli = sock = socket.create_connection((HOST, self.port))
4203        data = sock.recv(5)
4204        self.assertEqual(data, b"done!")
4205
4206    def _testOutsideTimeout(self):
4207        self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
4208        self.assertRaises(socket.timeout, lambda: sock.recv(5))
4209
4210
4211class TCPTimeoutTest(SocketTCPTest):
4212
4213    def testTCPTimeout(self):
4214        def raise_timeout(*args, **kwargs):
4215            self.serv.settimeout(1.0)
4216            self.serv.accept()
4217        self.assertRaises(socket.timeout, raise_timeout,
4218                              "Error generating a timeout exception (TCP)")
4219
4220    def testTimeoutZero(self):
4221        ok = False
4222        try:
4223            self.serv.settimeout(0.0)
4224            foo = self.serv.accept()
4225        except socket.timeout:
4226            self.fail("caught timeout instead of error (TCP)")
4227        except socket.error:
4228            ok = True
4229        except:
4230            self.fail("caught unexpected exception (TCP)")
4231        if not ok:
4232            self.fail("accept() returned success when we did not expect it")
4233
4234    def testInterruptedTimeout(self):
4235        # XXX I don't know how to do this test on MSWindows or any other
4236        # plaform that doesn't support signal.alarm() or os.kill(), though
4237        # the bug should have existed on all platforms.
4238        if not hasattr(signal, "alarm"):
4239            return                  # can only test on *nix
4240        self.serv.settimeout(5.0)   # must be longer than alarm
4241        class Alarm(Exception):
4242            pass
4243        def alarm_handler(signal, frame):
4244            raise Alarm
4245        old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
4246        try:
4247            signal.alarm(2)    # POSIX allows alarm to be up to 1 second early
4248            try:
4249                foo = self.serv.accept()
4250            except socket.timeout:
4251                self.fail("caught timeout instead of Alarm")
4252            except Alarm:
4253                pass
4254            except:
4255                self.fail("caught other exception instead of Alarm:"
4256                          " %s(%s):\n%s" %
4257                          (sys.exc_info()[:2] + (traceback.format_exc(),)))
4258            else:
4259                self.fail("nothing caught")
4260            finally:
4261                signal.alarm(0)         # shut off alarm
4262        except Alarm:
4263            self.fail("got Alarm in wrong place")
4264        finally:
4265            # no alarm can be pending.  Safe to restore old handler.
4266            signal.signal(signal.SIGALRM, old_alarm)
4267
4268class UDPTimeoutTest(SocketUDPTest):
4269
4270    def testUDPTimeout(self):
4271        def raise_timeout(*args, **kwargs):
4272            self.serv.settimeout(1.0)
4273            self.serv.recv(1024)
4274        self.assertRaises(socket.timeout, raise_timeout,
4275                              "Error generating a timeout exception (UDP)")
4276
4277    def testTimeoutZero(self):
4278        ok = False
4279        try:
4280            self.serv.settimeout(0.0)
4281            foo = self.serv.recv(1024)
4282        except socket.timeout:
4283            self.fail("caught timeout instead of error (UDP)")
4284        except socket.error:
4285            ok = True
4286        except:
4287            self.fail("caught unexpected exception (UDP)")
4288        if not ok:
4289            self.fail("recv() returned success when we did not expect it")
4290
4291class TestExceptions(unittest.TestCase):
4292
4293    def testExceptionTree(self):
4294        self.assertTrue(issubclass(socket.error, Exception))
4295        self.assertTrue(issubclass(socket.herror, socket.error))
4296        self.assertTrue(issubclass(socket.gaierror, socket.error))
4297        self.assertTrue(issubclass(socket.timeout, socket.error))
4298
4299class TestLinuxAbstractNamespace(unittest.TestCase):
4300
4301    UNIX_PATH_MAX = 108
4302
4303    def testLinuxAbstractNamespace(self):
4304        address = b"\x00python-test-hello\x00\xff"
4305        with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1:
4306            s1.bind(address)
4307            s1.listen(1)
4308            with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2:
4309                s2.connect(s1.getsockname())
4310                with s1.accept()[0] as s3:
4311                    self.assertEqual(s1.getsockname(), address)
4312                    self.assertEqual(s2.getpeername(), address)
4313
4314    def testMaxName(self):
4315        address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1)
4316        with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
4317            s.bind(address)
4318            self.assertEqual(s.getsockname(), address)
4319
4320    def testNameOverflow(self):
4321        address = "\x00" + "h" * self.UNIX_PATH_MAX
4322        with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
4323            self.assertRaises(socket.error, s.bind, address)
4324
4325    def testStrName(self):
4326        # Check that an abstract name can be passed as a string.
4327        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
4328        try:
4329            s.bind("\x00python\x00test\x00")
4330            self.assertEqual(s.getsockname(), b"\x00python\x00test\x00")
4331        finally:
4332            s.close()
4333
4334class TestUnixDomain(unittest.TestCase):
4335
4336    def setUp(self):
4337        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
4338
4339    def tearDown(self):
4340        self.sock.close()
4341
4342    def encoded(self, path):
4343        # Return the given path encoded in the file system encoding,
4344        # or skip the test if this is not possible.
4345        try:
4346            return os.fsencode(path)
4347        except UnicodeEncodeError:
4348            self.skipTest(
4349                "Pathname {0!a} cannot be represented in file "
4350                "system encoding {1!r}".format(
4351                    path, sys.getfilesystemencoding()))
4352
4353    def bind(self, sock, path):
4354        # Bind the socket
4355        try:
4356            sock.bind(path)
4357        except OSError as e:
4358            if str(e) == "AF_UNIX path too long":
4359                self.skipTest(
4360                    "Pathname {0!a} is too long to serve as a AF_UNIX path"
4361                    .format(path))
4362            else:
4363                raise
4364
4365    def testStrAddr(self):
4366        # Test binding to and retrieving a normal string pathname.
4367        path = os.path.abspath(support.TESTFN)
4368        self.bind(self.sock, path)
4369        self.addCleanup(support.unlink, path)
4370        self.assertEqual(self.sock.getsockname(), path)
4371
4372    def testBytesAddr(self):
4373        # Test binding to a bytes pathname.
4374        path = os.path.abspath(support.TESTFN)
4375        self.bind(self.sock, self.encoded(path))
4376        self.addCleanup(support.unlink, path)
4377        self.assertEqual(self.sock.getsockname(), path)
4378
4379    def testSurrogateescapeBind(self):
4380        # Test binding to a valid non-ASCII pathname, with the
4381        # non-ASCII bytes supplied using surrogateescape encoding.
4382        path = os.path.abspath(support.TESTFN_UNICODE)
4383        b = self.encoded(path)
4384        self.bind(self.sock, b.decode("ascii", "surrogateescape"))
4385        self.addCleanup(support.unlink, path)
4386        self.assertEqual(self.sock.getsockname(), path)
4387
4388    def testUnencodableAddr(self):
4389        # Test binding to a pathname that cannot be encoded in the
4390        # file system encoding.
4391        if support.TESTFN_UNENCODABLE is None:
4392            self.skipTest("No unencodable filename available")
4393        path = os.path.abspath(support.TESTFN_UNENCODABLE)
4394        self.bind(self.sock, path)
4395        self.addCleanup(support.unlink, path)
4396        self.assertEqual(self.sock.getsockname(), path)
4397
4398@unittest.skipUnless(thread, 'Threading required for this test.')
4399class BufferIOTest(SocketConnectedTest):
4400    """
4401    Test the buffer versions of socket.recv() and socket.send().
4402    """
4403    def __init__(self, methodName='runTest'):
4404        SocketConnectedTest.__init__(self, methodName=methodName)
4405
4406    def testRecvIntoArray(self):
4407        buf = bytearray(1024)
4408        nbytes = self.cli_conn.recv_into(buf)
4409        self.assertEqual(nbytes, len(MSG))
4410        msg = buf[:len(MSG)]
4411        self.assertEqual(msg, MSG)
4412
4413    def _testRecvIntoArray(self):
4414        buf = bytes(MSG)
4415        self.serv_conn.send(buf)
4416
4417    def testRecvIntoBytearray(self):
4418        buf = bytearray(1024)
4419        nbytes = self.cli_conn.recv_into(buf)
4420        self.assertEqual(nbytes, len(MSG))
4421        msg = buf[:len(MSG)]
4422        self.assertEqual(msg, MSG)
4423
4424    _testRecvIntoBytearray = _testRecvIntoArray
4425
4426    def testRecvIntoMemoryview(self):
4427        buf = bytearray(1024)
4428        nbytes = self.cli_conn.recv_into(memoryview(buf))
4429        self.assertEqual(nbytes, len(MSG))
4430        msg = buf[:len(MSG)]
4431        self.assertEqual(msg, MSG)
4432
4433    _testRecvIntoMemoryview = _testRecvIntoArray
4434
4435    def testRecvFromIntoArray(self):
4436        buf = bytearray(1024)
4437        nbytes, addr = self.cli_conn.recvfrom_into(buf)
4438        self.assertEqual(nbytes, len(MSG))
4439        msg = buf[:len(MSG)]
4440        self.assertEqual(msg, MSG)
4441
4442    def _testRecvFromIntoArray(self):
4443        buf = bytes(MSG)
4444        self.serv_conn.send(buf)
4445
4446    def testRecvFromIntoBytearray(self):
4447        buf = bytearray(1024)
4448        nbytes, addr = self.cli_conn.recvfrom_into(buf)
4449        self.assertEqual(nbytes, len(MSG))
4450        msg = buf[:len(MSG)]
4451        self.assertEqual(msg, MSG)
4452
4453    _testRecvFromIntoBytearray = _testRecvFromIntoArray
4454
4455    def testRecvFromIntoMemoryview(self):
4456        buf = bytearray(1024)
4457        nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf))
4458        self.assertEqual(nbytes, len(MSG))
4459        msg = buf[:len(MSG)]
4460        self.assertEqual(msg, MSG)
4461
4462    _testRecvFromIntoMemoryview = _testRecvFromIntoArray
4463
4464
4465TIPC_STYPE = 2000
4466TIPC_LOWER = 200
4467TIPC_UPPER = 210
4468
4469def isTipcAvailable():
4470    """Check if the TIPC module is loaded
4471
4472    The TIPC module is not loaded automatically on Ubuntu and probably
4473    other Linux distros.
4474    """
4475    if not hasattr(socket, "AF_TIPC"):
4476        return False
4477    if not os.path.isfile("/proc/modules"):
4478        return False
4479    with open("/proc/modules") as f:
4480        for line in f:
4481            if line.startswith("tipc "):
4482                return True
4483    if support.verbose:
4484        print("TIPC module is not loaded, please 'sudo modprobe tipc'")
4485    return False
4486
4487class TIPCTest(unittest.TestCase):
4488    def testRDM(self):
4489        srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
4490        cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
4491        self.addCleanup(srv.close)
4492        self.addCleanup(cli.close)
4493
4494        srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
4495        srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
4496                TIPC_LOWER, TIPC_UPPER)
4497        srv.bind(srvaddr)
4498
4499        sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
4500                TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
4501        cli.sendto(MSG, sendaddr)
4502
4503        msg, recvaddr = srv.recvfrom(1024)
4504
4505        self.assertEqual(cli.getsockname(), recvaddr)
4506        self.assertEqual(msg, MSG)
4507
4508
4509class TIPCThreadableTest(unittest.TestCase, ThreadableTest):
4510    def __init__(self, methodName = 'runTest'):
4511        unittest.TestCase.__init__(self, methodName = methodName)
4512        ThreadableTest.__init__(self)
4513
4514    def setUp(self):
4515        self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
4516        self.addCleanup(self.srv.close)
4517        self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
4518        srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
4519                TIPC_LOWER, TIPC_UPPER)
4520        self.srv.bind(srvaddr)
4521        self.srv.listen(5)
4522        self.serverExplicitReady()
4523        self.conn, self.connaddr = self.srv.accept()
4524        self.addCleanup(self.conn.close)
4525
4526    def clientSetUp(self):
4527        # The is a hittable race between serverExplicitReady() and the
4528        # accept() call; sleep a little while to avoid it, otherwise
4529        # we could get an exception
4530        time.sleep(0.1)
4531        self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
4532        self.addCleanup(self.cli.close)
4533        addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
4534                TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
4535        self.cli.connect(addr)
4536        self.cliaddr = self.cli.getsockname()
4537
4538    def testStream(self):
4539        msg = self.conn.recv(1024)
4540        self.assertEqual(msg, MSG)
4541        self.assertEqual(self.cliaddr, self.connaddr)
4542
4543    def _testStream(self):
4544        self.cli.send(MSG)
4545        self.cli.close()
4546
4547
4548@unittest.skipUnless(thread, 'Threading required for this test.')
4549class ContextManagersTest(ThreadedTCPSocketTest):
4550
4551    def _testSocketClass(self):
4552        # base test
4553        with socket.socket() as sock:
4554            self.assertFalse(sock._closed)
4555        self.assertTrue(sock._closed)
4556        # close inside with block
4557        with socket.socket() as sock:
4558            sock.close()
4559        self.assertTrue(sock._closed)
4560        # exception inside with block
4561        with socket.socket() as sock:
4562            self.assertRaises(socket.error, sock.sendall, b'foo')
4563        self.assertTrue(sock._closed)
4564
4565    def testCreateConnectionBase(self):
4566        conn, addr = self.serv.accept()
4567        self.addCleanup(conn.close)
4568        data = conn.recv(1024)
4569        conn.sendall(data)
4570
4571    def _testCreateConnectionBase(self):
4572        address = self.serv.getsockname()
4573        with socket.create_connection(address) as sock:
4574            self.assertFalse(sock._closed)
4575            sock.sendall(b'foo')
4576            self.assertEqual(sock.recv(1024), b'foo')
4577        self.assertTrue(sock._closed)
4578
4579    def testCreateConnectionClose(self):
4580        conn, addr = self.serv.accept()
4581        self.addCleanup(conn.close)
4582        data = conn.recv(1024)
4583        conn.sendall(data)
4584
4585    def _testCreateConnectionClose(self):
4586        address = self.serv.getsockname()
4587        with socket.create_connection(address) as sock:
4588            sock.close()
4589        self.assertTrue(sock._closed)
4590        self.assertRaises(socket.error, sock.sendall, b'foo')
4591
4592
4593@unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"),
4594                     "SOCK_CLOEXEC not defined")
4595@unittest.skipUnless(fcntl, "module fcntl not available")
4596class CloexecConstantTest(unittest.TestCase):
4597    @support.requires_linux_version(2, 6, 28)
4598    def test_SOCK_CLOEXEC(self):
4599        with socket.socket(socket.AF_INET,
4600                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
4601            self.assertTrue(s.type & socket.SOCK_CLOEXEC)
4602            self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
4603
4604
4605@unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"),
4606                     "SOCK_NONBLOCK not defined")
4607class NonblockConstantTest(unittest.TestCase):
4608    def checkNonblock(self, s, nonblock=True, timeout=0.0):
4609        if nonblock:
4610            self.assertTrue(s.type & socket.SOCK_NONBLOCK)
4611            self.assertEqual(s.gettimeout(), timeout)
4612        else:
4613            self.assertFalse(s.type & socket.SOCK_NONBLOCK)
4614            self.assertEqual(s.gettimeout(), None)
4615
4616    @support.requires_linux_version(2, 6, 28)
4617    def test_SOCK_NONBLOCK(self):
4618        # a lot of it seems silly and redundant, but I wanted to test that
4619        # changing back and forth worked ok
4620        with socket.socket(socket.AF_INET,
4621                           socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s:
4622            self.checkNonblock(s)
4623            s.setblocking(1)
4624            self.checkNonblock(s, False)
4625            s.setblocking(0)
4626            self.checkNonblock(s)
4627            s.settimeout(None)
4628            self.checkNonblock(s, False)
4629            s.settimeout(2.0)
4630            self.checkNonblock(s, timeout=2.0)
4631            s.setblocking(1)
4632            self.checkNonblock(s, False)
4633        # defaulttimeout
4634        t = socket.getdefaulttimeout()
4635        socket.setdefaulttimeout(0.0)
4636        with socket.socket() as s:
4637            self.checkNonblock(s)
4638        socket.setdefaulttimeout(None)
4639        with socket.socket() as s:
4640            self.checkNonblock(s, False)
4641        socket.setdefaulttimeout(2.0)
4642        with socket.socket() as s:
4643            self.checkNonblock(s, timeout=2.0)
4644        socket.setdefaulttimeout(None)
4645        with socket.socket() as s:
4646            self.checkNonblock(s, False)
4647        socket.setdefaulttimeout(t)
4648
4649
4650@unittest.skipUnless(os.name == "nt", "Windows specific")
4651@unittest.skipUnless(multiprocessing, "need multiprocessing")
4652class TestSocketSharing(SocketTCPTest):
4653    # This must be classmethod and not staticmethod or multiprocessing
4654    # won't be able to bootstrap it.
4655    @classmethod
4656    def remoteProcessServer(cls, q):
4657        # Recreate socket from shared data
4658        sdata = q.get()
4659        message = q.get()
4660
4661        s = socket.fromshare(sdata)
4662        s2, c = s.accept()
4663
4664        # Send the message
4665        s2.sendall(message)
4666        s2.close()
4667        s.close()
4668
4669    def testShare(self):
4670        # Transfer the listening server socket to another process
4671        # and service it from there.
4672
4673        # Create process:
4674        q = multiprocessing.Queue()
4675        p = multiprocessing.Process(target=self.remoteProcessServer, args=(q,))
4676        p.start()
4677
4678        # Get the shared socket data
4679        data = self.serv.share(p.pid)
4680
4681        # Pass the shared socket to the other process
4682        addr = self.serv.getsockname()
4683        self.serv.close()
4684        q.put(data)
4685
4686        # The data that the server will send us
4687        message = b"slapmahfro"
4688        q.put(message)
4689
4690        # Connect
4691        s = socket.create_connection(addr)
4692        #  listen for the data
4693        m = []
4694        while True:
4695            data = s.recv(100)
4696            if not data:
4697                break
4698            m.append(data)
4699        s.close()
4700        received = b"".join(m)
4701        self.assertEqual(received, message)
4702        p.join()
4703
4704    def testShareLength(self):
4705        data = self.serv.share(os.getpid())
4706        self.assertRaises(ValueError, socket.fromshare, data[:-1])
4707        self.assertRaises(ValueError, socket.fromshare, data+b"foo")
4708
4709    def compareSockets(self, org, other):
4710        # socket sharing is expected to work only for blocking socket
4711        # since the internal python timout value isn't transfered.
4712        self.assertEqual(org.gettimeout(), None)
4713        self.assertEqual(org.gettimeout(), other.gettimeout())
4714
4715        self.assertEqual(org.family, other.family)
4716        self.assertEqual(org.type, other.type)
4717        # If the user specified "0" for proto, then
4718        # internally windows will have picked the correct value.
4719        # Python introspection on the socket however will still return
4720        # 0.  For the shared socket, the python value is recreated
4721        # from the actual value, so it may not compare correctly.
4722        if org.proto != 0:
4723            self.assertEqual(org.proto, other.proto)
4724
4725    def testShareLocal(self):
4726        data = self.serv.share(os.getpid())
4727        s = socket.fromshare(data)
4728        try:
4729            self.compareSockets(self.serv, s)
4730        finally:
4731            s.close()
4732
4733    def testTypes(self):
4734        families = [socket.AF_INET, socket.AF_INET6]
4735        types = [socket.SOCK_STREAM, socket.SOCK_DGRAM]
4736        for f in families:
4737            for t in types:
4738                try:
4739                    source = socket.socket(f, t)
4740                except OSError:
4741                    continue # This combination is not supported
4742                try:
4743                    data = source.share(os.getpid())
4744                    shared = socket.fromshare(data)
4745                    try:
4746                        self.compareSockets(source, shared)
4747                    finally:
4748                        shared.close()
4749                finally:
4750                    source.close()
4751
4752
4753def test_main():
4754    tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
4755             TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ]
4756
4757    tests.extend([
4758        NonBlockingTCPTests,
4759        FileObjectClassTestCase,
4760        FileObjectInterruptedTestCase,
4761        UnbufferedFileObjectClassTestCase,
4762        LineBufferedFileObjectClassTestCase,
4763        SmallBufferedFileObjectClassTestCase,
4764        UnicodeReadFileObjectClassTestCase,
4765        UnicodeWriteFileObjectClassTestCase,
4766        UnicodeReadWriteFileObjectClassTestCase,
4767        NetworkConnectionNoServer,
4768        NetworkConnectionAttributesTest,
4769        NetworkConnectionBehaviourTest,
4770        ContextManagersTest,
4771        CloexecConstantTest,
4772        NonblockConstantTest
4773    ])
4774    if hasattr(socket, "socketpair"):
4775        tests.append(BasicSocketPairTest)
4776    if hasattr(socket, "AF_UNIX"):
4777        tests.append(TestUnixDomain)
4778    if sys.platform == 'linux':
4779        tests.append(TestLinuxAbstractNamespace)
4780    if isTipcAvailable():
4781        tests.append(TIPCTest)
4782        tests.append(TIPCThreadableTest)
4783    tests.extend([BasicCANTest, CANTest])
4784    tests.extend([BasicRDSTest, RDSTest])
4785    tests.extend([
4786        CmsgMacroTests,
4787        SendmsgUDPTest,
4788        RecvmsgUDPTest,
4789        RecvmsgIntoUDPTest,
4790        SendmsgUDP6Test,
4791        RecvmsgUDP6Test,
4792        RecvmsgRFC3542AncillaryUDP6Test,
4793        RecvmsgIntoRFC3542AncillaryUDP6Test,
4794        RecvmsgIntoUDP6Test,
4795        SendmsgTCPTest,
4796        RecvmsgTCPTest,
4797        RecvmsgIntoTCPTest,
4798        SendmsgSCTPStreamTest,
4799        RecvmsgSCTPStreamTest,
4800        RecvmsgIntoSCTPStreamTest,
4801        SendmsgUnixStreamTest,
4802        RecvmsgUnixStreamTest,
4803        RecvmsgIntoUnixStreamTest,
4804        RecvmsgSCMRightsStreamTest,
4805        RecvmsgIntoSCMRightsStreamTest,
4806        # These are slow when setitimer() is not available
4807        InterruptedRecvTimeoutTest,
4808        InterruptedSendTimeoutTest,
4809        TestSocketSharing,
4810    ])
4811
4812    thread_info = support.threading_setup()
4813    support.run_unittest(*tests)
4814    support.threading_cleanup(*thread_info)
4815
4816if __name__ == "__main__":
4817    test_main()