PageRenderTime 25ms CodeModel.GetById 6ms app.highlight 235ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_socket.py

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