PageRenderTime 303ms CodeModel.GetById 6ms app.highlight 261ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_socket.py

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