PageRenderTime 286ms CodeModel.GetById 7ms app.highlight 245ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_socket.py

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