U
    
W[5;  ã                   @   s*  d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ G dd„ dejƒZG dd„ dejƒZG dd„ dej ƒZ!G dd„ dej"ƒZ#G dd„ dejƒZ$e	 %e
d¡sde_&e	 'e
d¡s&de$_&dS )zK
Tests for implementations of L{IReactorUNIX} and L{IReactorUNIXDatagram}.
é    )ÚdivisionÚabsolute_importN)Ú
interfacesÚreactorÚprotocolÚerrorÚaddress)ÚdeferÚutils)Úlockfile)Ú_PY3ÚnetworkString)ÚFilePath)Úunittest)ÚMyServerFactoryÚMyClientFactoryc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚFailedConnectionClientFactoryc                 C   s
   || _ d S ©N)ÚonFail)Úselfr   © r   ú8/usr/lib/python3/dist-packages/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   c                   @   sj   e Zd Z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er^de_dd„ ZdS )ÚUnixSocketTestsz
    Test unix sockets.
    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)ÚprotoZexpected©Zpeernamer   ÚserverFactoryr   r   Ú
cbConnMade6   s    
z1UnixSocketTests.test_peerBind.<locals>.cbConnMade)Úmktempr   r	   ÚDeferredÚprotocolConnectionMader   Ú
listenUNIXÚ
addCleanupÚstopListeningÚsocketZAF_UNIXZSOCK_STREAMÚcloseZbindZ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©ÚclientFactoryr0   r   r   r   ÚallConnectedN   s    
ÿ
z1UnixSocketTests.test_dumber.<locals>.allConnected)r'   r   r	   r(   r)   r   r*   r+   r,   r   ÚconnectUNIXÚgatherResultsr/   )r   r%   ÚserverConnMader1   ÚclientConnMadeÚdr8   r   r6   r   Útest_dumber>   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"   r3   r,   )r4   r5   ZclientProto©r7   r0   r   r1   r   r   Ú
_portStuffq   s    
ÿ

z0UnixSocketTests.test_pidFile.<locals>._portStuffc                    s   ˆ  t ˆ d ¡d¡ d S )Nr@   Úlocked)ZassertFalser   ÚisLocked©Zignored)r0   r   r   r   Ú_check~   s    z,UnixSocketTests.test_pidFile.<locals>._check)r'   r   r	   r(   r)   r   r*   Z
assertTruer   rF   r   r9   r:   r/   )r   r%   r;   r<   r=   rD   rH   r   rC   r   Útest_pidFile[   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.
        Tr?   c                    s   t jˆ ˆdd}| ¡ S ©NTr?   )r   r*   r,   )Úignr1   ©r0   r%   r   r   ÚstoppedListening‘   s    z<UnixSocketTests.test_socketLocking.<locals>.stoppedListening)	r'   r   r   r*   ÚassertRaisesr   ÚCannotListenErrorr,   r/   )r   r1   rM   r   rL   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'   r0   r   r   ÚosÚpathsepÚjoinÚsysÚpathZasBytesModeÚ
executabler
   ZgetProcessValuer/   )r   ÚcallbackÚsourcerQ   ZpyExer=   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 rJ   )r   r*   r0   r   r,   )rK   Úp©r   r   r   ÚranStupidChild®   s    zGUnixSocketTests.test_uncleanServerSocketLocking.<locals>.ranStupidChild©rZ   ©r   r]   r   r\   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 )NTrB   )	r	   r(   r   r   r9   r0   ZassertFailurer   ZBadFileError)rK   r=   Úfr\   r   r   r]   »   s    zCUnixSocketTests.test_connectToUncleanServer.<locals>.ranStupidChildr^   r_   r   r\   r   Útest_connectToUncleanServerµ   s    z+UnixSocketTests.test_connectToUncleanServerc                    sj   ˆ  ¡ }t ||¡‰dˆ |f }ˆ 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.
        ú
<%s on %r>c                    s.   dˆ f }ˆ  tˆƒ|¡ ˆ  tˆƒ|¡ d S ©Nz<%s (not listening)>©r    ÚreprÚstr©rK   ZunconnectedString©ÚfactoryNamer   r1   r   r   rM   Ð   s    
z3UnixSocketTests._reprTest.<locals>.stoppedListening)
r'   r   r*   r    rf   rg   r	   ÚmaybeDeferredr,   r/   )r   r%   rj   r0   ÚconnectedStringr=   rM   r   ri   r   Ú	_reprTestÃ   s    
zUnixSocketTests._reprTestc                 C   s*   G dd„ dƒ}|   |tj¡ |  |ƒ d¡S )a  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIX.listenUNIX} contains the name of the classic 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 )zCUnixSocketTests.test_reprWithClassicFactory.<locals>.ClassicFactoryc                 S   s   d S r   r   r\   r   r   r   ÚdoStartà   s    zKUnixSocketTests.test_reprWithClassicFactory.<locals>.ClassicFactory.doStartc                 S   s   d S r   r   r\   r   r   r   ÚdoStopã   s    zJUnixSocketTests.test_reprWithClassicFactory.<locals>.ClassicFactory.doStopN©r   r   r   rn   ro   r   r   r   r   ÚClassicFactoryß   s   rq   z%twisted.test.test_unix.ClassicFactory©ÚassertIsInstanceÚtypesZ	ClassTyperm   )r   rq   r   r   r   Útest_reprWithClassicFactoryØ   s     ÿz+UnixSocketTests.test_reprWithClassicFactoryú)Classic classes do not exist on Python 3.c                 C   s*   G dd„ dt ƒ}|  |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   r\   r   r   r   rn   ù   s    zMUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStartc                 S   s   d S r   r   r\   r   r   r   ro   ü   s    zLUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStopNrp   r   r   r   r   ÚNewStyleFactoryø   s   rw   z&twisted.test.test_unix.NewStyleFactory©Úobjectrs   Útyperm   )r   rw   r   r   r   Útest_reprWithNewStyleFactoryñ   s     ÿz,UnixSocketTests.test_reprWithNewStyleFactoryN)r   r   r   Ú__doc__r2   r>   rI   rP   rZ   r`   rb   rm   ru   r   Úskipr{   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ÚdeferredGotBackr\   r   r   r   r     s    
zClientProto.__init__c                 C   s
   d| _ d S ©NT©Ústoppedr\   r   r   r   ÚstopProtocol  s    zClientProto.stopProtocolc                 C   s   d| _ | j d ¡ d S r   ©Ústartedr   rX   r\   r   r   r   ÚstartProtocol  s    zClientProto.startProtocolc                 C   s   || _ | j d ¡ d S r   )Úgotbackr€   rX   )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   ÚdeferredGotWhatr\   r   r   r   r      s    
zServerProto.__init__c                 C   s
   d| _ d S r   r‚   r\   r   r   r   r„   $  s    zServerProto.stopProtocolc                 C   s   d| _ | j d ¡ d S r   r…   r\   r   r   r   r‡   '  s    zServerProto.startProtocolc                 C   s*   || _ | j d|¡ || _| j d ¡ d S )Nó   hi back)Úgotfromr"   ÚwriteÚgotwhatrŒ   rX   )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‹   c                   @   sB   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zer6d
e_	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€   rG   )ÚcpÚspr   r   r   F  s    ÿz4DatagramUnixSocketTests.test_exchange.<locals>.writec                    s.   ˆ  dˆj¡ ˆ  ˆ ˆj¡ ˆ  dˆj¡ d S )Nr“   r   )r    r   rŽ   rˆ   rG   ©Z
clientaddrr”   r   r•   r   r   Ú_cbTestExchangeK  s    z>DatagramUnixSocketTests.test_exchange.<locals>._cbTestExchange)r'   r‹   r~   r   ÚlistenUNIXDatagramr+   r,   ZconnectUNIXDatagramr	   r:   r   r/   )r   Z
serveraddrÚsÚcr=   r   r—   r   r–   r   Útest_exchange7  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˜   rN   r   rO   r,   rR   Úunlink)r   r‘   r[   r™   r   r   r   Útest_cannotListenU  s      ÿz)DatagramUnixSocketTests.test_cannotListenc                    sj   ˆ  ¡ }t ||¡‰dˆ |f }ˆ 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.
        rc   c                    s.   dˆ f }ˆ  tˆƒ|¡ ˆ  tˆƒ|¡ d S rd   re   rh   ©ÚprotocolNamer   r1   r   r   rM   r  s    
z;DatagramUnixSocketTests._reprTest.<locals>.stoppedListening)
r'   r   r˜   r    rf   rg   r	   rk   r,   r/   )r   ZserverProtorŸ   r0   rl   ZstopDeferredrM   r   rž   r   rm   e  s    
z!DatagramUnixSocketTests._reprTestc                 C   s*   G dd„ dƒ}|   |tj¡ |  |ƒ d¡S )a0  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the
        classic 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 )zMDatagramUnixSocketTests.test_reprWithClassicProtocol.<locals>.ClassicProtocolc                 S   s   d S r   r   ©r   r"   r   r   r   ÚmakeConnection‚  s    z\DatagramUnixSocketTests.test_reprWithClassicProtocol.<locals>.ClassicProtocol.makeConnectionc                 S   s   d S r   r   r\   r   r   r   ro   …  s    zTDatagramUnixSocketTests.test_reprWithClassicProtocol.<locals>.ClassicProtocol.doStopN©r   r   r   r¡   ro   r   r   r   r   ÚClassicProtocol  s   r£   z&twisted.test.test_unix.ClassicProtocolrr   )r   r£   r   r   r   Útest_reprWithClassicProtocolz  s     ÿz4DatagramUnixSocketTests.test_reprWithClassicProtocolrv   c                 C   s*   G dd„ dt ƒ}|  |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¡   ›  s    z^DatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.makeConnectionc                 S   s   d S r   r   r\   r   r   r   ro   ž  s    zVDatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.doStopNr¢   r   r   r   r   ÚNewStyleProtocolš  s   r¥   z'twisted.test.test_unix.NewStyleProtocolrx   )r   r¥   r   r   r   Útest_reprWithNewStyleProtocol“  s     ÿz5DatagramUnixSocketTests.test_reprWithNewStyleProtocolN)r   r   r   r|   r›   r   rm   r¤   r   r}   r¦   r   r   r   r   r’   3  s   ÿr’   z1This reactor does not support UNIX domain socketsz3This reactor does not support UNIX datagram sockets)(r|   Z
__future__r   r   rR   rU   rt   r-   Ztwisted.internetr   r   r   r   r   r	   r
   Ztwisted.pythonr   Ztwisted.python.compatr   r   Ztwisted.python.filepathr   Ztwisted.trialr   Ztwisted.test.test_tcpr   r   ZClientFactoryr   ZTestCaser   ZConnectedDatagramProtocolr~   ZDatagramProtocolr‹   r’   ZIReactorUNIXr}   ZIReactorUNIXDatagramr   r   r   r   Ú<module>   s,   	 fv