U
    c6{i5  ã                   @   s  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ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 G d
d„ dejƒZee
 ed¡ dƒG dd„ dejƒƒZG dd„ dejƒZG dd„ dejƒZ ee
 !ed¡ dƒG dd„ dejƒƒZ"dS )zK
Tests for implementations of L{IReactorUNIX} and L{IReactorUNIXDatagram}.
é    N)ÚskipIf)ÚaddressÚdeferÚerrorÚ
interfacesÚprotocolÚreactorÚutils)Úlockfile)ÚnetworkString)ÚFilePath)ÚMyClientFactoryÚMyServerFactory)Úunittestc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚFailedConnectionClientFactoryc                 C   s
   || _ d S ©N)ÚonFail)Úselfr   © r   ú:/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/test/test_unix.pyÚ__init__   s    z&FailedConnectionClientFactory.__init__c                 C   s   | j  |¡ d S r   )r   Zerrback)r   Z	connectorÚreasonr   r   r   ÚclientConnectionFailed   s    z4FailedConnectionClientFactory.clientConnectionFailedN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s   r   ú1This reactor does not support UNIX domain socketsc                   @   sh   e Zd ZdZe ed¡sdZdd„ Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚUnixSocketTestsz
    Test unix sockets.
    Nr   c                    sŠ   ˆ  ¡ }ˆ  ¡ ‰ tƒ ‰t ¡  }ˆ_t |ˆ¡}ˆ |j¡ t	 	t	j
t	j¡}ˆ |j¡ | ˆ ¡ | |¡ ‡ ‡‡fdd„}| |¡ |S )zè
        The address passed to the server factory's C{buildProtocol} method and
        the address returned by the connected protocol's transport's C{getPeer}
        method match the address the client socket is bound to.
        c                    s0   t  ˆ ¡}ˆ ˆj|g¡ ˆ | j ¡ |¡ d S r   )r   ÚUNIXAddressÚassertEqualÚpeerAddressesÚ	transportZgetPeer)ÚprotoÚexpected©Zpeernamer   ÚserverFactoryr   r   Ú
cbConnMade;   s    
z1UnixSocketTests.test_peerBind.<locals>.cbConnMade)Úmktempr   r   ÚDeferredÚprotocolConnectionMader   Ú
listenUNIXÚ
addCleanupÚstopListeningÚsocketÚAF_UNIXÚSOCK_STREAMÚcloseÚbindÚconnectÚaddCallback)r   ÚfilenameZconnMadeÚunixPortZ
unixSocketr&   r   r$   r   Útest_peerBind*   s    


zUnixSocketTests.test_peerBindc                    s€   ˆ  ¡ ‰tƒ }t ¡ }||_t ˆ|¡}ˆ |j¡ t	ƒ ‰ t ¡ }|ˆ _t 
ˆˆ ¡ t ||g¡}‡ ‡‡fdd„}| |¡ |S )zŠ
        L{IReactorUNIX.connectUNIX} can be used to connect a client to a server
        started with L{IReactorUNIX.listenUNIX}.
        c                    s6   | \}}ˆ  ˆ jt ˆ¡g¡ |j ¡  |j ¡  d S r   )r   r    r   r   r!   ÚloseConnection)ÚargsÚserverProtocolZclientProtocol©ÚclientFactoryr4   r   r   r   ÚallConnectedT   s     
ÿ
z1UnixSocketTests.test_dumber.<locals>.allConnected)r'   r   r   r(   r)   r   r*   r+   r,   r   ÚconnectUNIXÚgatherResultsr3   )r   r%   ÚserverConnMader5   ÚclientConnMadeÚdr<   r   r:   r   Útest_dumberC   s    
zUnixSocketTests.test_dumberc                    sª   ˆ  ¡ ‰tƒ }t ¡ }||_tjˆ|dd�‰ˆ t 	ˆd ¡¡ t
ƒ ‰ t ¡ }|ˆ _tjˆˆ dd� t ||g¡}‡ ‡‡‡fdd„}| |¡ ‡‡fdd	„}| |¡ |S )
zà
        A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is
        called and released when the Deferred returned by the L{IListeningPort}
        provider's C{stopListening} method is called back.
        T©ZwantPIDú.locké   ©ZcheckPIDc                    s:   | \}}ˆ  ˆ jt ˆ¡g¡ |j ¡  |j ¡  ˆ ¡ S r   )r   r    r   r   r!   r7   r,   )r8   r9   ZclientProto©r;   r4   r   r5   r   r   Ú
_portStuffy   s     
ÿ

z0UnixSocketTests.test_pidFile.<locals>._portStuffc                    s   ˆ  t ˆ d ¡d¡ d S )NrD   Úlocked)ZassertFalser
   ÚisLocked©Zignored)r4   r   r   r   Ú_checkˆ   s    z,UnixSocketTests.test_pidFile.<locals>._check)r'   r   r   r(   r)   r   r*   Ú
assertTruer
   rJ   r   r=   r>   r3   )r   r%   r?   r@   rA   rH   rL   r   rG   r   Útest_pidFileb   s     

zUnixSocketTests.test_pidFilec                    sR   |   ¡ ‰ tƒ ‰tjˆ ˆdd�}| jtjtjˆ ˆdd� ‡ ‡fdd„}| ¡  |¡S )z›
        L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed
        the name of a file on which a server is already listening.
        TrC   c                    s   t jˆ ˆdd�}| ¡ S ©NTrC   )r   r*   r,   )Úignr5   ©r4   r%   r   r   ÚstoppedListeningŸ   s    z<UnixSocketTests.test_socketLocking.<locals>.stoppedListening)	r'   r   r   r*   ÚassertRaisesr   ÚCannotListenErrorr,   r3   )r   r5   rR   r   rQ   r   Útest_socketLockingŽ   s    ûz"UnixSocketTests.test_socketLockingc                 C   sj   |   ¡ | _td| jf ƒ}dttj tj¡ƒ 	¡ ji}ttj
ƒ 	¡ j}tj|dd|f|d�}| |¡ |S )Nzmfrom twisted.internet import protocol, reactor
reactor.listenUNIX(%r, protocol.ServerFactory(),wantPID=True)
s
   PYTHONPATHs   -us   -c)Úenv)r'   r4   r   r   ÚosÚpathsepÚjoinÚsysÚpathZasBytesModeÚ
executabler	   ZgetProcessValuer3   )r   ÚcallbackÚsourcerV   ZpyExerA   r   r   r   Ú_uncleanSocketTest¥   s    
ûÿ
z"UnixSocketTests._uncleanSocketTestc                    s   ‡ fdd„}ˆ   |¡S )a"  
        If passed C{True} for the C{wantPID} parameter, a server can be started
        listening with L{IReactorUNIX.listenUNIX} when passed the name of a
        file on which a previous server which has not exited cleanly has been
        listening using the C{wantPID} option.
        c                    s   t jˆ jtƒ dd�}| ¡ S rO   )r   r*   r4   r   r,   )rP   Úp©r   r   r   ÚranStupidChild¾   s    zGUnixSocketTests.test_uncleanServerSocketLocking.<locals>.ranStupidChild©r_   ©r   rb   r   ra   r   Útest_uncleanServerSocketLocking¶   s    z/UnixSocketTests.test_uncleanServerSocketLockingc                    s   ‡ fdd„}ˆ   |¡S )z¶
        If passed C{True} for the C{checkPID} parameter, a client connection
        attempt made with L{IReactorUNIX.connectUNIX} fails with
        L{error.BadFileError}.
        c                    s0   t  ¡ }t|ƒ}tjˆ j|dd� ˆ  |tj¡S )NTrF   )	r   r(   r   r   r=   r4   ZassertFailurer   ZBadFileError)rP   rA   Úfra   r   r   rb   Ì   s    zCUnixSocketTests.test_connectToUncleanServer.<locals>.ranStupidChildrc   rd   r   ra   r   Útest_connectToUncleanServerÅ   s    z+UnixSocketTests.test_connectToUncleanServerc                    sp   ˆ  ¡ }t ||¡‰dˆ › d|›d�}ˆ tˆƒ|¡ ˆ tˆƒ|¡ t ˆj¡}‡ ‡‡fdd„}| 	|¡ |S )z~
        Test the C{__str__} and C{__repr__} implementations of a UNIX port when
        used with the given factory.
        ú<ú on ú>c                    s0   dˆ › d�}ˆ  tˆƒ|¡ ˆ  tˆƒ|¡ d S ©Nrh   z (not listening)>©r   ÚreprÚstr©rP   ZunconnectedString©ÚfactoryNamer   r5   r   r   rR   â   s    z3UnixSocketTests._reprTest.<locals>.stoppedListening)
r'   r   r*   r   rm   rn   r   ÚmaybeDeferredr,   r3   )r   r%   rq   r4   ÚconnectedStringrA   rR   r   rp   r   Ú	_reprTestÔ   s    
zUnixSocketTests._reprTestc                 C   s(   G dd„ dƒ}|   |t¡ |  |ƒ d¡S )a!  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIX.listenUNIX} contains the name of the new-style factory
        class being used and the filename on which the port is listening or
        indicates that the port is not listening.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )zEUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactoryc                 S   s   d S r   r   ra   r   r   r   ÚdoStartó   s    zMUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStartc                 S   s   d S r   r   ra   r   r   r   ÚdoStopö   s    zLUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStopN)r   r   r   ru   rv   r   r   r   r   ÚNewStyleFactoryò   s   rw   z&twisted.test.test_unix.NewStyleFactory©ZassertIsInstanceÚtypert   )r   rw   r   r   r   Útest_reprWithNewStyleFactoryê   s     ÿz,UnixSocketTests.test_reprWithNewStyleFactory)r   r   r   Ú__doc__r   ÚIReactorUNIXr   Úskipr6   rB   rN   rU   r_   re   rg   rt   rz   r   r   r   r   r      s   ,r   c                   @   s8   e Zd Zd ZZdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚClientProtoFNc                 C   s   t  ¡ | _t  ¡ | _d S r   )r   r(   ÚdeferredStartedÚdeferredGotBackra   r   r   r   r     s    
zClientProto.__init__c                 C   s
   d| _ d S ©NT©Ústoppedra   r   r   r   ÚstopProtocol	  s    zClientProto.stopProtocolc                 C   s   d| _ | j d ¡ d S r�   ©Ústartedr   r]   ra   r   r   r   ÚstartProtocol  s    zClientProto.startProtocolc                 C   s   || _ | j d ¡ d S r   )Úgotbackr€   r]   )r   Údatar   r   r   ÚdatagramReceived  s    zClientProto.datagramReceived)
r   r   r   r†   rƒ   rˆ   r   r„   r‡   rŠ   r   r   r   r   r~     s   r~   c                   @   s<   e Zd Zd ZZd ZZdd„ Zdd„ Zdd„ Z	d	d
„ Z
dS )ÚServerProtoFNc                 C   s   t  ¡ | _t  ¡ | _d S r   )r   r(   r   ÚdeferredGotWhatra   r   r   r   r     s    
zServerProto.__init__c                 C   s
   d| _ d S r�   r‚   ra   r   r   r   r„     s    zServerProto.stopProtocolc                 C   s   d| _ | j d ¡ d S r�   r…   ra   r   r   r   r‡      s    zServerProto.startProtocolc                 C   s*   || _ | j d|¡ || _| j d ¡ d S )Nó   hi back)Úgotfromr!   ÚwriteÚgotwhatrŒ   r]   )r   r‰   Úaddrr   r   r   rŠ   $  s    zServerProto.datagramReceived)r   r   r   r†   rƒ   r�   rŽ   r   r„   r‡   rŠ   r   r   r   r   r‹     s   r‹   z3This reactor does not support UNIX datagram socketsc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚDatagramUnixSocketTestsz%
    Test datagram UNIX sockets.
    c                    sš   ˆ  ¡ ‰ ˆ  ¡ }tƒ ‰tƒ ‰t |ˆ¡}ˆ |j¡ tj|ˆˆ d�}ˆ |j¡ t 	ˆj
ˆj
g¡}‡‡fdd„}‡ ‡‡‡fdd„}| |¡ | |¡ |S )zf
        Test that a datagram can be sent to and received by a server and vice
        versa.
        )ZbindAddressc                    s   ˆ j  d¡ t ˆjˆ jg¡S )Nó   hi)r!   r�   r   r>   rŒ   r€   rK   )ÚcpÚspr   r   r�   D  s    z4DatagramUnixSocketTests.test_exchange.<locals>.writec                    s.   ˆ  dˆj¡ ˆ  ˆ ˆj¡ ˆ  dˆj¡ d S )Nr“   r�   )r   r�   rŽ   rˆ   rK   ©Z
clientaddrr”   r   r•   r   r   Ú_cbTestExchangeH  s    z>DatagramUnixSocketTests.test_exchange.<locals>._cbTestExchange)r'   r‹   r~   r   ÚlistenUNIXDatagramr+   r,   ZconnectUNIXDatagramr   r>   r   r3   )r   Z
serveraddrÚsÚcrA   r�   r—   r   r–   r   Útest_exchange4  s    

z%DatagramUnixSocketTests.test_exchangec                 C   sD   |   ¡ }tƒ }t ||¡}|  tjtj||¡ | ¡  t 	|¡ dS )zž
        L{IReactorUNIXDatagram.listenUNIXDatagram} raises
        L{error.CannotListenError} if the unix socket specified is already in
        use.
        N)
r'   r‹   r   r˜   rS   r   rT   r,   rW   Úunlink)r   r‘   r`   r™   r   r   r   Útest_cannotListenQ  s    z)DatagramUnixSocketTests.test_cannotListenc                    sp   ˆ  ¡ }t ||¡‰dˆ › d|›d�}ˆ tˆƒ|¡ ˆ tˆƒ|¡ t ˆj¡}‡ ‡‡fdd„}| 	|¡ |S )zˆ
        Test the C{__str__} and C{__repr__} implementations of a UNIX datagram
        port when used with the given protocol.
        rh   ri   rj   c                    s0   dˆ › d�}ˆ  tˆƒ|¡ ˆ  tˆƒ|¡ d S rk   rl   ro   ©ÚprotocolNamer   r5   r   r   rR   n  s    z;DatagramUnixSocketTests._reprTest.<locals>.stoppedListening)
r'   r   r˜   r   rm   rn   r   rr   r,   r3   )r   ZserverProtorŸ   r4   rs   ZstopDeferredrR   r   rž   r   rt   `  s    
z!DatagramUnixSocketTests._reprTestc                 C   s(   G dd„ dƒ}|   |t¡ |  |ƒ d¡S )a2  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the
        new-style protocol class being used and the filename on which the port
        is listening or indicates that the port is not listening.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )zODatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocolc                 S   s   d S r   r   )r   r!   r   r   r   ÚmakeConnection  s    z^DatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.makeConnectionc                 S   s   d S r   r   ra   r   r   r   rv   ‚  s    zVDatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.doStopN)r   r   r   r    rv   r   r   r   r   ÚNewStyleProtocol~  s   r¡   z'twisted.test.test_unix.NewStyleProtocolrx   )r   r¡   r   r   r   Útest_reprWithNewStyleProtocolv  s     ÿz5DatagramUnixSocketTests.test_reprWithNewStyleProtocolN)r   r   r   r{   r›   r�   rt   r¢   r   r   r   r   r’   +  s
   r’   )#r{   rW   r-   rZ   r   r   Ztwisted.internetr   r   r   r   r   r   r	   Ztwisted.pythonr
   Ztwisted.python.compatr   Ztwisted.python.filepathr   Ztwisted.test.test_tcpr   r   Ztwisted.trialZClientFactoryr   r|   ZTestCaser   ZConnectedDatagramProtocolr~   ZDatagramProtocolr‹   ZIReactorUNIXDatagramr’   r   r   r   r   Ú<module>   s0   $þ `þ