o
    ¯bã$  ã                   @   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
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)ÚUnsupportedAddressFamily)ÚIReactorSocket)ÚDatagramProtocolÚServerFactory)ÚReactorBuilderÚneedsRunningReactor)Úerr)Úplatformc                   @   ó   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)ÚbuildReactorÚ
assertTruer   Ú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                   @   ó.   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   ór   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ r-|  |jd tj¡ dS |  |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
   Ú	isWindowsÚassertEqualÚargsÚerrnoÚWSAENOTSOCKÚEBADF©r   r   Úprober   Úexcr   r   r   Útest_invalidDescriptorB   ó   û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/   r0   Fc                    sH   ˆ  tjˆ j¡}t ¡ rˆ |jd tj	¡ d S ˆ |jd tj
¡ d S )Nr   )r    r   r!   Úacceptr
   r$   r%   r&   r'   ÚWSAEWOULDBLOCKÚEAGAIN©Úignoredr,   ©Ú
portSocketr   r   r   Ústoppedˆ   s   zFAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz"Failed to accept on original port.c                      ó   ˆ   ‡fdd„¡S )Nc                    ó   ˆ   ¡ S ©N©Ústop©r=   ©r   r   r   Ú<lambda>–   ó    zYAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>©ÚaddCallbackr   ©Údr   r   r   rH   –   ó    zGAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r4   r   r2   r3   Úsetblockingr"   r   Úfamilyr   ÚstopListeningrK   Ú
addErrbackr	   r   Úrun©r   r6   r@   r   ©rM   r?   r   r   r   Útest_stopOnlyClosesq   s"   


ÿ

ÿz5AdoptStreamPortErrorsTestsBuilder.test_stopOnlyClosesN©	r   r   r   r   r   r   r-   r8   rV   r   r   r   r   r   6   s    	r   c                   @   r   )Ú'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.
        r1   N)	r   r   r4   r   r    r   ÚadoptStreamConnectionr   r   )r   r   Ú
connectionr7   r   r   r   r8   ©   s   ûzAAdoptStreamConnectionErrorsTestsBuilder.test_invalidAddressFamilyN)r   r   r   r   r   r   r8   r   r   r   r   rX   œ   s    
rX   c                   @   r   )	Ú#AdoptDatagramPortErrorsTestsBuilderzR
    Builder for testing L{IReactorSocket.adoptDatagramPort} implementations.
    c                 C   r   )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.   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.
        r1   N)r   r   r#   Ú
SOCK_DGRAMr4   r   r    r   r\   r   r   r5   r   r   r   r8   Þ   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                    sJ   ˆ  tjˆ jd¡}t ¡ rˆ |jd tj	¡ d S ˆ |jd tj
¡ d S )Nr0   r   )r    r   r!   Úrecvfromr
   r$   r%   r&   r'   r:   r;   r<   r>   r   r   r@   	  s   zHAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz Failed to read on original port.c                      rA   )Nc                    rB   rC   rD   rF   rG   r   r   rH     rI   z[AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>rJ   r   rL   r   r   rH     rN   zIAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r#   r]   r4   r   r2   rO   r\   r   rP   r   rQ   rK   rR   r	   r   rS   rT   r   rU   r   rV   ó   s    

ÿ
	ÿz7AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyClosesNrW   r   r   r   r   r[   ¿   s    r[   )r   r'   r   Úzope.interfacer   Útwisted.internet.errorr   Útwisted.internet.interfacesr   Útwisted.internet.protocolr   r   Ú#twisted.internet.test.reactormixinsr   r   Útwisted.python.logr	   Útwisted.python.runtimer
   r   r   rX   r[   ÚglobalsÚupdateÚmakeTestCaseClassesr   r   r   r   Ú<module>   s$   	f#]