PageRenderTime 303ms CodeModel.GetById 5ms app.highlight 249ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_socket.py

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