U
    
W[q$  ã                   @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZ dd	lmZ dd
lmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeƒ  e ¡ ¡ eƒ  e ¡ ¡ eƒ  e ¡ ¡ eƒ  e ¡ ¡ dS )aD  
Tests for implementations of L{IReactorSocket}.

Generally only tests for failure cases are found here.  Success cases for
this interface are tested elsewhere.  For example, the success case for
I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
behave exactly the same as L{IReactorTCP.listenTCP}.
é    N)Úverify)Úerr)ÚIReactorSocket)ÚUnsupportedAddressFamily)ÚDatagramProtocolÚServerFactory)ÚReactorBuilderÚneedsRunningReactor)Ú_PY3)Úplatformc                   @   s   e Zd ZdZegZdd„ ZdS )Ú&IReactorSocketVerificationTestsBuilderaO  
    Builder for testing L{IReactorSocket} implementations for required
    methods and method signatures.

    L{ReactorBuilder} already runs L{IReactorSocket.providedBy} to
    ensure that these tests will only be run on reactor classes that
    claim to implement L{IReactorSocket}.

    These tests ensure that reactors which claim to provide the
    L{IReactorSocket} interface actually have all the required methods
    and that those methods have the expected number of arguments.

    These tests will be skipped for reactors which do not claim to
    provide L{IReactorSocket}.
    c                 C   s   |   ¡ }|  t t|¡¡ dS )zf
        The reactor instance returned by C{buildReactor} provides
        L{IReactorSocket}.
        N)ÚbuildReactorZ
assertTruer   ZverifyObjectr   )ÚselfÚreactor© r   úC/usr/lib/python3/dist-packages/twisted/internet/test/test_socket.pyÚtest_provider/   s    
ÿz4IReactorSocketVerificationTestsBuilder.test_providerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚrequiredInterfacesr   r   r   r   r   r      s   r   c                   @   s.   e Zd ZdZegZdd„ Zdd„ Zdd„ ZdS )	Ú!AdoptStreamPortErrorsTestsBuilderas  
    Builder for testing L{IReactorSocket.adoptStreamPort} implementations.

    Generally only tests for failure cases are found here.  Success cases for
    this interface are tested elsewhere.  For example, the success case for
    I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
    behave exactly the same as L{IReactorTCP.listenTCP}.
    c                 C   st   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ r\tr\|  |jd tj¡ n|  |jd tj¡ dS )z«
        An implementation of L{IReactorSocket.adoptStreamPort} raises
        L{socket.error} if passed an integer which is not associated with a
        socket.
        r   N)r   ÚsocketÚfilenoÚcloseÚassertRaisesÚerrorÚadoptStreamPortÚAF_INETr   r   Ú	isWindowsr
   ÚassertEqualÚargsÚerrnoÚWSAENOTSOCKÚEBADF©r   r   Zprober   Úexcr   r   r   Útest_invalidDescriptorE   s       þz8AdoptStreamPortErrorsTestsBuilder.test_invalidDescriptorc                 C   sR   |   ¡ }t ¡ }| d¡ | d¡ |  |j¡ d}|  t|j| 	¡ |t
ƒ ¡ dS )z¬
        An implementation of L{IReactorSocket.adoptStreamPort} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        ©z	127.0.0.1r   é   é  N)r   r   ÚbindÚlistenÚ
addCleanupr   r   r   r   r   r   ©r   r   ÚportÚ	arbitraryr   r   r   Útest_invalidAddressFamilyZ   s    

   þz;AdoptStreamPortErrorsTestsBuilder.test_invalidAddressFamilyc                    sœ   ˆ  ¡ ‰t ¡ ‰ˆ ˆj¡ ˆ d¡ ˆ d¡ ˆ d¡ ˆ ˆ ¡ ˆj	t
ƒ ¡}| ¡ ‰ ‡‡fdd„}ˆ  |¡ ˆ  td¡ tˆ‡ ‡fdd„ƒ ˆ ¡  d	S )
a9  
        When the L{IListeningPort} returned by
        L{IReactorSocket.adoptStreamPort} is stopped using
        C{stopListening}, the underlying socket is closed but not
        shutdown.  This allows another process which still has a
        reference to it to continue accepting connections over it.
        r)   r*   Fc                    sJ   ˆ  tjˆ j¡}t ¡ r2tr2ˆ |jd t	j
¡ nˆ |jd t	j¡ d S )Nr   )r   r   r   Zacceptr   r    r
   r!   r"   r#   ÚWSAEWOULDBLOCKÚEAGAIN©Úignoredr'   ©Ú
portSocketr   r   r   Ústoppedƒ   s    zFAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz"Failed to accept on original port.c                      s   ˆ   ‡fdd„¡S )Nc                    s   ˆ   ¡ S ©N©Ústop©r6   ©r   r   r   Ú<lambda>‘   ó    zYAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>©ÚaddCallbackr   ©Údr   r   r   r?   ‘   r@   zGAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r.   r   r,   r-   Úsetblockingr   r   Úfamilyr   ÚstopListeningrB   Ú
addErrbackr   r	   Úrun©r   r0   r9   r   ©rD   r8   r   r   r   Útest_stopOnlyClosesn   s(    


  ÿ	
þz5AdoptStreamPortErrorsTestsBuilder.test_stopOnlyClosesN©	r   r   r   r   r   r   r(   r2   rL   r   r   r   r   r   :   s
   r   c                   @   s   e Zd ZdZegZdd„ ZdS )Ú'AdoptStreamConnectionErrorsTestsBuildera}  
    Builder for testing L{IReactorSocket.adoptStreamConnection}
    implementations.

    Generally only tests for failure cases are found here.  Success cases for
    this interface are tested elsewhere.  For example, the success case for
    I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
    behave exactly the same as L{IReactorTCP.listenTCP}.
    c                 C   s>   |   ¡ }t ¡ }|  |j¡ d}|  t|j| ¡ |tƒ ¡ dS )z²
        An implementation of L{IReactorSocket.adoptStreamConnection} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r+   N)	r   r   r.   r   r   r   ZadoptStreamConnectionr   r   )r   r   Z
connectionr1   r   r   r   r2   £   s      ýzAAdoptStreamConnectionErrorsTestsBuilder.test_invalidAddressFamilyN)r   r   r   r   r   r   r2   r   r   r   r   rN   —   s   	rN   c                   @   s.   e Zd ZdZegZdd„ Zdd„ Zdd„ ZdS )	Ú#AdoptDatagramPortErrorsTestsBuilderzR
    Builder for testing L{IReactorSocket.adoptDatagramPort} implementations.
    c                 C   st   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ r\tr\|  |jd tj¡ n|  |jd tj¡ dS )z­
        An implementation of L{IReactorSocket.adoptDatagramPort} raises
        L{socket.error} if passed an integer which is not associated with a
        socket.
        r   N)r   r   r   r   r   r   ÚadoptDatagramPortr   r   r   r    r
   r!   r"   r#   r$   r%   r&   r   r   r   r(   ¾   s      ýz:AdoptDatagramPortErrorsTestsBuilder.test_invalidDescriptorc                 C   sF   |   ¡ }t tjtj¡}|  |j¡ d}|  t|j| 	¡ |t
ƒ ¡ dS )z®
        An implementation of L{IReactorSocket.adoptDatagramPort} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r+   N)r   r   r   Ú
SOCK_DGRAMr.   r   r   r   rP   r   r   r/   r   r   r   r2   Ô   s      ýz=AdoptDatagramPortErrorsTestsBuilder.test_invalidAddressFamilyc                    sš   ˆ  ¡ ‰t tjtj¡‰ˆ ˆj¡ ˆ d¡ ˆ d¡ ˆ ˆ 	¡ ˆj
tƒ ¡}| ¡ ‰ ‡‡fdd„}ˆ  |¡ ˆ  td¡ tˆ‡ ‡fdd„ƒ ˆ ¡  dS )	a7  
        When the L{IListeningPort} returned by
        L{IReactorSocket.adoptDatagramPort} is stopped using
        C{stopListening}, the underlying socket is closed but not
        shutdown.  This allows another process which still has a
        reference to it to continue reading and writing to it.
        r)   Fc                    sL   ˆ  tjˆ jd¡}t ¡ r4tr4ˆ |jd t	j
¡ nˆ |jd t	j¡ d S )Nr*   r   )r   r   r   Zrecvfromr   r    r
   r!   r"   r#   r3   r4   r5   r7   r   r   r9   û   s    zHAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz Failed to read on original port.c                      s   ˆ   ‡fdd„¡S )Nc                    s   ˆ   ¡ S r:   r;   r=   r>   r   r   r?     r@   z[AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>rA   r   rC   r   r   r?     r@   zIAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r   rQ   r.   r   r,   rE   rP   r   rF   r   rG   rB   rH   r   r	   rI   rJ   r   rK   r   rL   ç   s&    

  ÿ
þz7AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyClosesNrM   r   r   r   r   rO   ·   s
   rO   )r   r#   r   Zzope.interfacer   Ztwisted.python.logr   Ztwisted.internet.interfacesr   Ztwisted.internet.errorr   Ztwisted.internet.protocolr   r   Z#twisted.internet.test.reactormixinsr   r	   Ztwisted.python.compatr
   Ztwisted.python.runtimer   r   r   rN   rO   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s"   	] W