
    Vh$                        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e      Z G d de      Z G d de      Z G d de      Z e       j1                  ej3                                 e       j1                  ej3                                 e       j1                  ej3                                 e       j1                  ej3                                y)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 Zy)&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                 v    | j                         }| j                  t        j                  t        |             y)zf
        The reactor instance returned by C{buildReactor} provides
        L{IReactorSocket}.
        N)buildReactor
assertTruer   verifyObjectr   )selfreactors     Q/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_socket.pytest_providerz4IReactorSocketVerificationTestsBuilder.test_provider-   s+    
 ##%++NGDE    N)__name__
__module____qualname____doc__r   requiredInterfacesr    r   r   r   r      s      ))Fr   r   c                   (    e Zd ZdZegZd Zd Zd Zy)!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                    | j                         }t        j                         }|j                         }|j                          | j	                  t        j
                  |j                  |t        j                  t                     }t        j                         r.| j                  |j                  d   t        j                         y| j                  |j                  d   t        j                         y)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!   excs        r   test_invalidDescriptorz8AdoptStreamPortErrorsTestsBuilder.test_invalidDescriptorB   s     ##%LL##NNO
 SXXa[%*;*;<SXXa[%++6r   c                 <   | j                         }t        j                         }|j                  d       |j                  d       | j	                  |j
                         d}| j                  t        |j                  |j                         |t                      y)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	arbitrarys       r   test_invalidAddressFamilyz;AdoptStreamPortErrorsTestsBuilder.test_invalidAddressFamilyZ   su     ##%}}		"#A

#	$##KKMO	
r   c                      j                         t        j                          j                  j                         j	                  d       j                  d       j                  d       j                  j                         j                  t                     }|j                          fd}j                  |       j                  t        d       t        fd       j!                          y)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.
        r2   r3   Fc                 8   j                  t        j                  j                        }t	        j
                         r.j                  |j                  d   t        j                         y j                  |j                  d   t        j                         y )Nr   )r#   r    r$   acceptr   r'   r(   r)   r*   WSAEWOULDBLOCKEAGAINignoredr/   
portSocketr   s     r   stoppedzFAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stopped   sg     ##FLL*2C2CDC!!#  !e.B.BC  !ell;r   z"Failed to accept on original port.c                  ,     j                  fd      S )Nc                 $    j                         S NstoprB   r   s    r   <lambda>zYAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>       7<<> r   addCallbackdr   s   r   rK   zGAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>       Q]]+IJ r   N)r   r    r7   r"   r5   r6   setblockingr%   r!   familyr   stopListeningrN   
addErrbackr
   r	   runr   r9   rD   rP   rC   r   s   `  @@@r   test_stopOnlyClosesz5AdoptStreamPortErrorsTestsBuilder.test_stopOnlyClosesq   s     ##%]]_

(()()!u% &&!2!2MO
  	< 	
g	S>?J	
 	r   N	r   r   r   r   r   r   r0   r;   rX   r   r   r   r   r   6   s!     ))70
.(r   r   c                       e Zd ZdZegZd Zy)'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                     | j                         }t        j                         }| j                  |j                         d}| j	                  t
        |j                  |j                         |t                      y)z
        An implementation of L{IReactorSocket.adoptStreamConnection} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r4   N)	r   r    r7   r"   r#   r   adoptStreamConnectionr!   r   )r   r   
connectionr:   s       r   r;   zAAdoptStreamConnectionErrorsTestsBuilder.test_invalidAddressFamily   sb     ##%]]_

(()	$))O	
r   N)r   r   r   r   r   r   r;   r   r   r   r[   r[      s     ))
r   r[   c                   (    e Zd ZdZegZd Zd Zd Zy)#AdoptDatagramPortErrorsTestsBuilderzR
    Builder for testing L{IReactorSocket.adoptDatagramPort} implementations.
    c                    | j                         }t        j                         }|j                         }|j                          | j	                  t        j
                  |j                  |t        j                  t                     }t        j                         r.| j                  |j                  d   t        j                         y| j                  |j                  d   t        j                         y)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-   s        r   r0   z:AdoptDatagramPortErrorsTestsBuilder.test_invalidDescriptor   s     ##%LL%%NN
 SXXa[%*;*;<SXXa[%++6r   c                 4   | j                         }t        j                  t        j                  t        j                        }| j	                  |j
                         d}| j                  t        |j                  |j                         |t                      y)z
        An implementation of L{IReactorSocket.adoptDatagramPort} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r4   N)r   r    r&   
SOCK_DGRAMr7   r"   r#   r   rb   r!   r   r8   s       r   r;   z=AdoptDatagramPortErrorsTestsBuilder.test_invalidAddressFamily   sm     ##%}}V^^V->->?

#	$%%KKM	
r   c                 *     j                         t        j                  t        j                  t        j                         j	                  j
                         j                  d       j                  d       j                  j                         j                  t                     }|j                          fd}j                  |       j                  t        d       t!        fd       j#                          y)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.
        r2   Fc                 :   j                  t        j                  j                  d      }t	        j
                         r.j                  |j                  d   t        j                         y j                  |j                  d   t        j                         y )Nr3   r   )r#   r    r$   recvfromr   r'   r(   r)   r*   r?   r@   rA   s     r   rD   zHAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stopped	  si     ##FLL*2E2EqIC!!#  !e.B.BC  !ell;r   z Failed to read on original port.c                  ,     j                  fd      S )Nc                 $    j                         S rG   rH   rJ   s    r   rK   z[AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>  rL   r   rM   rO   s   r   rK   zIAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>  rQ   r   N)r   r    r&   rd   r7   r"   r5   rR   rb   r!   rS   r   rT   rN   rU   r
   r	   rV   rW   s   `  @@@r   rX   z7AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses   s     ##%]]6>>63D3DE

(()()u% ((!2!24D4F
  	< 	
g	S<=J	
 	r   NrY   r   r   r   r`   r`      s!     ))70
*&r   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   r[   r`   globalsupdatemakeTestCaseClassesr   r   r   <module>rt      s      ! ; 6 E S " +F^ F8c cL 
n  
FZ. Zz 	  7KKM N 	  2FFH I 	  8LLN O 	  4HHJ Kr   