PageRenderTime 313ms CodeModel.GetById 5ms app.highlight 273ms RepoModel.GetById 1ms app.codeStats 2ms

/Lib/test/test_socket.py

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