o
    Òó·iî™  ã                   @   s²  d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 ddlmZmZmZ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mZmZmZ dd
lmZ zddlmZ W n eyc   dZY nw eZddl m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z=m>Z> ddl?m@Z@ ddlAmBZBmCZCmDZDmEZEmFZF ddlGmHZH ddlImJZJmKZKmLZLmMZMmNZN ddlOmPZP ddlQmRZR ddlSmTZT ddlUmVZVmWZWmXZX ddlYmZZZ ddl[m\Z\m]Z] ddl^m_Z_ dd l`maZa eZd!ƒZbd"ZceZd!ƒdu�r!d#ZcG d$d%„ d%ƒZdd&d'„ ZeG d(d)„ d)eDƒZfG d*d+„ d+eBƒZge"e7ƒG d,d-„ d-eBƒƒZhG d.d/„ d/edeHeCƒZiG d0d1„ d1edeHƒZjG d2d3„ d3ƒZkG d4d5„ d5ƒZlG d6d7„ d7ƒZmG d8d9„ d9elemeHeMƒZnG d:d;„ d;ekemeHeMƒZoG d<d=„ d=eNeHƒZpeqƒ  rei s¡ ¡ eqƒ  rej s¡ ¡ eqƒ  ren s¡ ¡ eqƒ  reo s¡ ¡ eqƒ  rep s¡ ¡ G d>d?„ d?eHeEƒZteqƒ  ret s¡ ¡ ee]d@kdAƒG dBdC„ dCeaƒƒZudS )Dz/
Tests for implementations of L{IReactorUNIX}.
é    )Úmd5)ÚcloseÚfstatÚstatÚunlinkÚurandom)Úpformat)ÚAF_INETÚSOCK_STREAMÚ
SOL_SOCKETÚsocket)ÚS_IMODE)Úpack)ÚmkstempÚmktemp)ÚAnyÚCallableÚOptionalÚSequenceÚType)ÚskipIf)ÚAF_UNIXN)Ú	InterfaceÚimplementer)ÚbaseÚerrorÚ
interfacesÚreactorÚtcp)ÚUNIXAddress)ÚDeferredÚfailÚgatherResults)ÚUNIXClientEndpointÚUNIXServerEndpoint)ÚCannotListenErrorÚConnectionClosedÚFileDescriptorOverrun)ÚIFileDescriptorReceiverÚIReactorFDSetÚIReactorSocketÚIReactorUNIX)ÚClientFactoryÚDatagramProtocolÚServerFactory)ÚLoopingCall)ÚConnectableProtocolÚConnectionTestsMixinÚEndpointCreatorÚStreamClientTestsMixinÚrunProtocolsWithReactor)ÚReactorBuilder)ÚClosingFactoryÚMyClientFactoryÚMyServerFactoryÚStreamTransportTestsMixinÚWriteSequenceTestsMixin)ÚnativeString)ÚFailure)Ú_coerceToFilesystemEncoding)ÚaddObserverÚerrÚremoveObserver)ÚrequireModule)ÚplatformÚplatformType)ÚAccumulatingProtocol)ÚTestCaseztwisted.python.sendmsgÚ z>sendmsg extension unavailable, extended UNIX features disabledc                   @   s   e Zd ZdZdd„ ZdS )ÚUNIXFamilyMixinzK
    Test-helper defining mixin for things related to AF_UNIX sockets.
    c                 C   sB   d}|   ¡ }t||ƒ|||d�}| ¡  |  tt|ƒjƒ|¡ dS )z}
        Assert that the mode of the created unix socket is set to the mode
        specified to the reactor method.
        i€  )ÚmodeN)ÚbuildReactorÚgetattrÚstopListeningÚassertEqualr   r   Úst_mode)ÚselfÚ
methodNameÚpathÚfactoryrH   r   ZunixPort© rR   úeC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/internet/test/test_unix.pyÚ	_modeTestR   s
   zUNIXFamilyMixin._modeTestN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rT   rR   rR   rR   rS   rG   M   s    rG   c                 C   s   t tdƒƒ ¡ S )zI
    Return a new, unique abstract namespace path to be listened on.
    éd   N)r   r   Ú	hexdigest)ÚcaserR   rR   rS   Ú_abstractPath^   s   r\   c                   @   s>   e Zd ZU dZejfZeee	e
   ed< dd„ Zdd„ ZdS )ÚUNIXCreatorz(
    Create UNIX socket end points.
    ÚrequiredInterfacesc                 C   s   t ddd�}t||ƒS )z3
        Construct a UNIX server endpoint.
        ú.sockÚ.©ÚsuffixÚdirN)r   r$   )rN   r   rP   rR   rR   rS   Úserverl   ó   
zUNIXCreator.serverc                 C   s   t ||jƒS )z3
        Construct a UNIX client endpoint.
        N)r#   Úname)rN   r   ZserverAddressrR   rR   rS   Úclientt   s   zUNIXCreator.clientN)rU   rV   rW   rX   r   r+   r^   r   r   r   r   Ú__annotations__rd   rg   rR   rR   rR   rS   r]   e   s
   
 r]   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚSendFileDescriptorzæ
    L{SendFileDescriptorAndBytes} sends a file descriptor and optionally some
    normal bytes and then closes its connection.

    @ivar reason: The reason the connection was lost, after C{connectionLost}
        is called.
    Nc                 C   s   || _ || _dS )zà
        @param fd: A C{int} giving a file descriptor to send over the
            connection.

        @param data: A C{str} giving data to send over the connection, or
            L{None} if no data is to be sent.
        N)ÚfdÚdata)rN   rj   rk   rR   rR   rS   Ú__init__†   s   
zSendFileDescriptor.__init__c                 C   s0   | j  | j¡ | jr| j  | j¡ | j  ¡  dS )zn
        Send C{self.fd} and, if it is not L{None}, C{self.data}.  Then close the
        connection.
        N)Ú	transportÚsendFileDescriptorrj   rk   ÚwriteÚloseConnection©rN   rR   rR   rS   ÚconnectionMade‘   s   z!SendFileDescriptor.connectionMadec                 C   s   t  | |¡ || _d S ©N)r0   ÚconnectionLostÚreason©rN   ru   rR   rR   rS   rt   ›   s   
z!SendFileDescriptor.connectionLost)rU   rV   rW   rX   ru   rl   rr   rt   rR   rR   rR   rS   ri   {   s    
ri   c                   @   s8   e Zd ZdZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚReceiveFileDescriptora}  
    L{ReceiveFileDescriptor} provides an API for waiting for file descriptors to
    be received.

    @ivar reason: The reason the connection was lost, after C{connectionLost}
        is called.

    @ivar waiting: A L{Deferred} which fires with a file descriptor once one is
        received, or with a failure if the connection is lost with no descriptor
        arriving.
    Nc                 C   s"   | j du rtƒ | _| jS t| j ƒS )z¯
        Return a L{Deferred} which will fire with the next file descriptor
        received, or with a failure if the connection is or has already been
        lost.
        N)ru   r    Úwaitingr!   rq   rR   rR   rS   ÚwaitForDescriptor±   s   

z'ReceiveFileDescriptor.waitForDescriptorc                 C   s   | j  |¡ d| _ dS )z�
        Fire the waiting Deferred, initialized by C{waitForDescriptor}, with the
        file descriptor just received.
        N)rx   Úcallback)rN   Ú
descriptorrR   rR   rS   ÚfileDescriptorReceived½   re   z,ReceiveFileDescriptor.fileDescriptorReceivedc                 C   s4   | j dur| j  ttd|›d�ƒƒ¡ d| _ dS dS )a_  
        Fail the waiting Deferred, if it has not already been fired by
        C{fileDescriptorReceived}.  The bytes sent along with a file descriptor
        are guaranteed to be delivered to the protocol's C{dataReceived} method
        only after the file descriptor has been delivered to the protocol's
        C{fileDescriptorReceived}.
        NzReceived bytes (z) before descriptor.)rx   Úerrbackr<   Ú	Exception©rN   rk   rR   rR   rS   ÚdataReceivedÅ   s   
ÿ
üz"ReceiveFileDescriptor.dataReceivedc                 C   s2   t  | |¡ | jdur| j |¡ d| _|| _dS )zj
        Fail the waiting Deferred, initialized by C{waitForDescriptor}, if there
        is one.
        N)r0   rt   rx   r}   ru   rv   rR   rR   rS   rt   Ó   s
   

z$ReceiveFileDescriptor.connectionLost)
rU   rV   rW   rX   ru   rx   ry   r|   r€   rt   rR   rR   rR   rS   rw       s    rw   c                   @   s  e Zd ZdZefZeƒ Zdd„ Ze	e
 ¡  dƒdd„ ƒZdd„ Ze	e
 ¡  dƒd	d
„ ƒZdd„ Ze	e eƒdd„ ƒZe	e eƒdd„ ƒZe	e eƒdd„ ƒZdd„ Ze	e eƒdd„ ƒZe	e
 ¡ dƒe	e eƒdd„ ƒƒZe	e eƒdd„ ƒZe	e eƒdd„ ƒZe	e eƒdd„ ƒZd S )!ÚUNIXTestsBuilderz=
    Builder defining tests relating to L{IReactorUNIX}.
    c                 C   ó   |   d|  ¡ tƒ ¡ dS )zs
        The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with
        the mode specified.
        Ú
listenUNIXN)rT   r   r.   rq   rR   rR   rS   Ú	test_modeè   ó   zUNIXTestsBuilder.test_modeú8Abstract namespace UNIX sockets only supported on Linux.c                 C   ó>   t | ƒ}|  ¡ }| d| tƒ ¡}|  | ¡ td| ƒ¡ dS )zµ
        On Linux, a UNIX socket path may begin with C{' '} to indicate
        a socket in the abstract namespace.  L{IReactorUNIX.listenUNIX}
        accepts such a path.
        ú N)r\   rI   rƒ   r.   rL   ÚgetHostr   ©rN   rP   r   ÚportrR   rR   rS   Ú#test_listenOnLinuxAbstractNamespaceï   s   z4UNIXTestsBuilder.test_listenOnLinuxAbstractNamespacec                 C   s^   dd„ }|   tjd|¡ |  ¡ }|  t¡� | dtƒ ¡ W d  ƒ dS 1 s(w   Y  dS )z•
        L{IReactorUNIX.listenUNIX} raises L{CannotListenError} if the
        underlying port's createInternetSocket raises a socket error.
        c                 S   s   t dƒ‚)Nz FakeBasePort forced socket.error)ÚOSErrorrq   rR   rR   rS   ÚraiseSocketError  s   z=UNIXTestsBuilder.test_listenFailure.<locals>.raiseSocketErrorZcreateInternetSocketznot-usedN)Úpatchr   ZBasePortrI   ÚassertRaisesr%   rƒ   r.   )rN   rŽ   r   rR   rR   rS   Útest_listenFailureÿ   s   "ÿz#UNIXTestsBuilder.test_listenFailurec                 C   r‡   )zc
        L{IReactorUNIX.connectUNIX} also accepts a Linux abstract namespace
        path.
        rˆ   N)r\   rI   ÚconnectUNIXr,   rL   ZgetDestinationr   )rN   rP   r   Z	connectorrR   rR   rS   Ú$test_connectToLinuxAbstractNamespace  s   	z5UNIXTestsBuilder.test_connectToLinuxAbstractNamespacec                 C   s`   G dd„ dt ƒ}|ƒ }|ƒ }t| ||| jƒ |  |jd |jd ¡ |  |jd |jd ¡ dS )z¾
        A client's transport's C{getHost} and C{getPeer} return L{UNIXAddress}
        instances which have the filesystem path of the host and peer ends of
        the connection.
        c                   @   ó   e Zd Zdd„ ZdS )z4UNIXTestsBuilder.test_addresses.<locals>.SaveAddressc                 S   s"   t | ¡ | ¡ d�| _| ¡  d S )N)ÚhostÚpeer)Údictr‰   ÚgetPeerÚ	addressesrp   )rN   rm   rR   rR   rS   ÚmakeConnection#  s   ÿzCUNIXTestsBuilder.test_addresses.<locals>.SaveAddress.makeConnectionN)rU   rV   rW   rš   rR   rR   rR   rS   ÚSaveAddress"  ó    r›   r•   r–   N)r0   r4   Ú	endpointsrL   r™   )rN   r›   rd   rg   rR   rR   rS   Útest_addresses  s   zUNIXTestsBuilder.test_addressesc                    s„   ddl m‰  t ƒ ‰ˆ d¡ tˆ ¡ dƒ‰tƒ }| ¡ }‡ ‡‡fdd„}| |¡ | t	d¡ | 
‡fdd	„¡ tˆˆ|ˆjƒ d
S )z¤
        L{IUNIXTransport.sendFileDescriptor} accepts an integer file descriptor
        and sends a copy of it to the process reading from the connection.
        r   )Úfromfd)rF   r   ó   junkc                    s@   ˆ | t tƒ}t| ƒ ˆ ˆ ¡ | ¡ ¡ ˆ ˆ ¡ | ¡ ¡ d S rs   )r	   r
   r   rL   ÚgetsocknameZassertNotEqualÚfileno)r{   Zreceived)rŸ   ÚsrN   rR   rS   ÚcheckDescriptor@  s   zAUNIXTestsBuilder.test_sendFileDescriptor.<locals>.checkDescriptorz-Sending file descriptor encountered a problemc                    ó
   ˆ j  ¡ S rs   ©rm   rp   ©Zignored©rd   rR   rS   Ú<lambda>P  ó   
 z:UNIXTestsBuilder.test_sendFileDescriptor.<locals>.<lambda>N)r   rŸ   Úbindri   r¢   rw   ry   ÚaddCallbackÚ
addErrbackr?   ÚaddBothr4   r�   )rN   rg   Údr¤   rR   )rŸ   r£   rN   rd   rS   Útest_sendFileDescriptor1  s   

z(UNIXTestsBuilder.test_sendFileDescriptorc                 C   sT   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }||_t| ||| jƒ |  |jd¡ dS )z…
        If a L{IUNIXTransport.sendFileDescriptor} call fills up
        the send buffer, any registered producer is paused.
        c                   @   r”   )zSUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotReadc                 S   s   | j  ¡  d S rs   )rm   ÚpauseProducingrq   rR   rR   rS   rr   \  s   zbUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotRead.connectionMadeN©rU   rV   rW   rr   rR   rR   rR   rS   ÚDoesNotRead[  rœ   r³   c                   @   ó8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )z`UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptorsFc                    sP   t ƒ ˆ _ ˆ j ˆ d¡ ‡ fdd„}t|ƒˆ _ˆ jjˆ j_ˆ j d¡ t	d¡ d S )NTc                      s"   ˆ j  ˆ j ¡ ¡ ˆ j  d¡ d S )Nó   x)rm   rn   r   r¢   ro   rR   rq   rR   rS   Úsenderf  s   zUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMade.<locals>.senderr   zSend loop failure)
r   rm   ZregisterProducerr/   Útaskr   ZclockÚstartr­   r?   )rN   r¶   rR   rq   rS   rr   b  s   
zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMadec                 S   ó   |   ¡  d S rs   ©Ú_disconnectrq   rR   rR   rS   ÚstopProducingn  ó   znUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.stopProducingc                 S   r¹   rs   rº   rq   rR   rR   rS   ÚresumeProducingq  r½   zpUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.resumeProducingc                 S   s   d| _ | j ¡  |  ¡  d S )NT)Úpausedrm   ZunregisterProducerr»   rq   rR   rR   rS   r±   t  s   
zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.pauseProducingc                 S   s$   | j  ¡  | j ¡  | jj ¡  d S rs   )r·   Ústoprm   ZabortConnectionÚotherrq   rR   rR   rS   r»   y  s   

zlUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors._disconnectN)	rU   rV   rW   r¿   rr   r¼   r¾   r±   r»   rR   rR   rR   rS   ÚSendsManyFileDescriptors_  s    rÂ   z*sendFileDescriptor producer was not pausedN)r0   rÁ   r4   r�   Ú
assertTruer¿   )rN   r³   rÂ   rd   rg   rR   rR   rS   Ú-test_sendFileDescriptorTriggersPauseProducingT  s   z>UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducingc                    s†   t ƒ }t| ¡ dƒ‰ tƒ }g }| ¡ }| |j¡ | ‡ fdd„¡ t| ˆ || jƒ |  	|d t
¡ |d  t¡ |  	ˆ jjt¡ dS )ag  
        If L{IUNIXTransport.sendFileDescriptor} is used to queue a greater
        number of file descriptors than the number of bytes sent using
        L{ITransport.write}, the connection is closed and the protocol connected
        to the transport has its C{connectionLost} method called with a failure
        wrapping L{FileDescriptorOverrun}.
        Nc                    r¥   rs   r¦   r§   r¨   rR   rS   r©   •  rª   z=UNIXTestsBuilder.test_fileDescriptorOverrun.<locals>.<lambda>r   )r   ri   r¢   rw   ry   r®   Úappendr4   r�   ÚassertIsInstancer<   Útrapr&   ru   Úvaluer'   )rN   Úcargorg   Úresultr¯   rR   r¨   rS   Útest_fileDescriptorOverrun…  s   	z+UNIXTestsBuilder.test_fileDescriptorOverrunc                    s@  ddl m} ddlm} ddlm} dd„ ‰ ttƒG ‡ fdd„dtƒƒ}G d	d
„ d
|ƒ}|t	t
ƒ\}}|  |j¡ |  |j¡ |ƒ }	|||	ƒ}
tƒ \}}tƒ \}}|  t|¡ |  t|¡ d}||g}||ƒ\}}||||ƒ |
 ¡  |  t|	jƒ|¡ |  t|ƒ t|	jƒ¡¡ |	jrž‡ fdd„|D ƒ}|  ||	j¡ dS dS )a‰  
        Drive _SendmsgMixin via sendmsg socket calls to check that
        L{IFileDescriptorReceiver.fileDescriptorReceived} is called once
        for each file descriptor received in the ancillary messages.

        @param ancillaryPacker: A callable that will be given a list of
            two file descriptors and should return a two-tuple where:
            The first item is an iterable of zero or more (cmsg_level,
            cmsg_type, cmsg_data) tuples in the same order as the given
            list for actual sending via sendmsg; the second item is an
            integer indicating the expected number of FDs to be received.
        r   ©Ú
socketpair)Ú_SendmsgMixin©Úsendmsgc                 S   s   t | ƒ}|j|jfS rs   )r   Úst_devÚst_ino)rj   ÚfsrR   rR   rS   ÚdeviceInodeTuple¿  s   zTUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.deviceInodeTuplec                       s    e Zd Zdd„ Z‡ fdd„ZdS )zPUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocolc                 S   s   g | _ g | _d S rs   )ÚfdsÚdeviceInodesReceivedrq   rR   rR   rS   rl   Å  ó   
zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.__init__c                    s(   | j  |¡ | j ˆ |ƒ¡ t|ƒ d S rs   )rÕ   rÅ   rÖ   r   )rN   rj   ©rÔ   rR   rS   r|   É  s   zgUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.fileDescriptorReceivedN)rU   rV   rW   rl   r|   rR   rØ   rR   rS   ÚFakeProtocolÃ  s    rÙ   c                   @   r´   )zPUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiveré   c                 S   s   || _ || _d S rs   )r   Úprotocol)rN   ÚsktÚprotorR   rR   rS   rl   Ñ  r×   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.__init__c                 S   ó   d S rs   rR   r   rR   rR   rS   Ú_dataReceivedÕ  ó   z^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._dataReceivedc                 S   rÞ   rs   rR   rq   rR   rR   rS   r‰   Ø  rà   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getHostc                 S   rÞ   rs   rR   rq   rR   rR   rS   r˜   Û  rà   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getPeerc                 S   rÞ   rs   rR   )rN   ÚorR   rR   rS   Ú_getLogPrefixÞ  rà   z^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._getLogPrefixN)	rU   rV   rW   Z
bufferSizerl   rß   r‰   r˜   râ   rR   rR   rR   rS   ÚFakeReceiverÎ  s    rã   s   some data needs to be sentc                    s   g | ]}ˆ |ƒ‘qS rR   rR   ©Ú.0rj   rØ   rR   rS   Ú
<listcomp>ý  s    zNUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.<listcomp>N)r   rÍ   Ztwisted.internet.unixrÎ   Útwisted.python.sendmsgrÐ   r   r(   r0   r   r
   Ú
addCleanupr   r   r   ZdoReadrL   ÚlenrÕ   ZassertFalseÚsetÚintersectionrÖ   )rN   ÚancillaryPackerrÍ   rÎ   rÐ   rÙ   rã   Z
sendSocketZ
recvSocketrÝ   ZreceiverZ	fileOneFDZfileOneNameZ	fileTwoFDZfileTwoNameZ
dataToSendÚ	fdsToSendÚ	ancillaryÚexpectedCountZdeviceInodesSentrR   rØ   rS   Ú)_sendmsgMixinFileDescriptorReceivedDriver�  s6   



þz:UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriverc                    ó&   ddl m‰  ‡ fdd„}|  |¡ dS )zß
        _SendmsgMixin handles multiple file descriptors per recvmsg, calling
        L{IFileDescriptorReceiver.fileDescriptorReceived} once per received
        file descriptor. Scenario: single CMSG with two FDs.
        r   ©Ú
SCM_RIGHTSc                    s$   t ˆ tdg| ¢R Ž fg}d}||fS )NÚiié   ©r   r   ©rí   rî   rï   rò   rR   rS   rì   	  s   z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG.<locals>.ancillaryPackerN©rç   ró   rð   ©rN   rì   rR   rò   rS   Ú1test_multiFileDescriptorReceivedPerRecvmsgOneCMSG   s   zBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSGz=Multi control message ancillary sendmsg not supported on Mac.c                    rñ   )zá
        _SendmsgMixin handles multiple file descriptors per recvmsg, calling
        L{IFileDescriptorReceiver.fileDescriptorReceived} once per received
        file descriptor. Scenario: two CMSGs with one FD each.
        r   rò   c                    s   ‡ fdd„| D ƒ}d}||fS )Nc                    s   g | ]
}t ˆ td |ƒf‘qS )Úirö   rä   rò   rR   rS   ræ     s    zpUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPacker.<locals>.<listcomp>rõ   rR   r÷   rò   rR   rS   rì     s   z\UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPackerNrø   rù   rR   rò   rS   Ú2test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs  s   zCUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGsc                    s~   ddl m‰ dd„ }‡fdd„}g }t|jƒ |  t|j¡ |  ˆd|¡ |  |¡ d‰ t‡ fd	d
„|D ƒƒ}|  	|d¡ dS )zØ
        _SendmsgMixin handles multiple file descriptors per recvmsg, calling
        L{IFileDescriptorReceiver.fileDescriptorReceived} once per received
        file descriptor. Scenario: unsupported CMSGs.
        r   rÏ   c                 S   s   g }d}||fS )Nr   rR   r÷   rR   rR   rS   rì   3  s   z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.ancillaryPackerc                    s   d}dg}d}ˆ   |||¡S )Ns	   some data)NNó    r   )ZReceivedMessage)rÜ   ÚargsÚkwargsrk   rî   ÚflagsrÏ   rR   rS   ÚfakeRecvmsgUnsupportedAncillary8  s   zkUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.fakeRecvmsgUnsupportedAncillaryÚrecvmsgz#received unsupported ancillary datac                 3   s   � | ]	}ˆ |d  v V  qdS )ÚformatNrR   )rå   Úe)ÚexpectedMessagerR   rS   Ú	<genexpr>G  s   € zUUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.<genexpr>z+Expected message not found in logged eventsN)
Ztwisted.pythonrÐ   r>   rÅ   rè   r@   r�   rð   ÚanyrÃ   )rN   rì   r  ÚeventsÚfoundrR   )r  rÐ   rS   Ú1test_multiFileDescriptorReceivedPerRecvmsgBadCMSG$  s   

zBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSGc                 C   sò   ddl m} |ƒ \}}g }t|jƒ |  t|j¡ G dd„ dtƒ}|| ¡ dƒ}tƒ }t	| ||| j
ƒ | ¡  | d¡ |  d| d¡¡ d	}d
}	t|j|j|	|d�}
|D ]}|
 ¡ D ]\}}|| |¡krh qUq[ dS |  d|
t|ƒf ¡ dS )zÙ
        If associated with a protocol which does not provide
        L{IFileDescriptorReceiver}, file descriptors received by the
        L{IUNIXTransport} implementation are closed and a warning is emitted.
        r   rÌ   c                   @   r”   )zRUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddressesc                 S   s&   | j  ¡ | _| j  ¡ | _t | ¡ d S rs   )rm   r‰   ÚhostAddressr˜   ÚpeerAddressri   rr   rq   rR   rR   rS   rr   _  s   zaUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddresses.connectionMadeNr²   rR   rR   rR   rS   ÚRecordEndpointAddresses^  rœ   r  r    Frý   rÚ   zˆ%(protocolName)s (on %(hostAddress)r) does not provide IFileDescriptorReceiver; closing file descriptor received (from %(peerAddress)r).r0   )r  r  ZprotocolNamer  z3Expected event (%s) not found in logged events (%s)N)r   rÍ   r>   rÅ   rè   r@   ri   r¢   r0   r4   r�   r   ÚsetblockingrL   Úrecvr—   r  r  ÚitemsÚgetr!   r   )rN   rÍ   ZprobeClientZprobeServerr  r  rd   rg   r  ZclsNameZexpectedEventZlogEventÚkÚvrR   rR   rS   Ú test_avoidLeakingFileDescriptorsJ  sH   


ÿüÿÿþÿÿz1UNIXTestsBuilder.test_avoidLeakingFileDescriptorsc                    sv   t tƒG ‡ fdd„dtƒƒ}tƒ }t| ¡ dƒ}|ƒ }tˆ ||ˆ jƒ ˆ  t	|j
d ¡ ˆ  dt|j
dd… ƒ¡ dS )z„
        L{IUNIXTransport.sendFileDescriptor} sends file descriptors before
        L{ITransport.write} sends normal bytes.
        c                       s(   e Zd Zdd„ Z‡ fdd„Zdd„ ZdS )zJUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEventsc                 S   s   t  | ¡ g | _d S rs   )r0   rr   r  rq   rR   rR   rS   rr      s   

zYUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.connectionMadec                    s    ˆ   t|¡ | j t|ƒ¡ d S rs   )rè   r   r  rÅ   Útype)Z	innerSelfr{   rq   rR   rS   r|   ¤  s   zaUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.fileDescriptorReceivedc                 S   s   | j  |¡ d S rs   )r  Úextendr   rR   rR   rS   r€   ¨  s   zWUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.dataReceivedN)rU   rV   rW   rr   r|   r€   rR   rq   rR   rS   ÚRecordEventsž  s    r  r    r   é   N)r   r(   r0   r   ri   r¢   r4   r�   rL   Úintr  Úbytes)rN   r  rÉ   rd   rg   rR   rq   rS   Ú#test_descriptorDeliveredBeforeBytes—  s   z4UNIXTestsBuilder.test_descriptorDeliveredBeforeBytesN)rU   rV   rW   rX   r+   r^   r]   r�   r„   r   rB   ÚisLinuxrŒ   r‘   r“   rž   rÐ   ÚsendmsgSkipReasonr°   rÄ   rË   rð   rú   ZisMacOSXrü   r
  r  r  rR   rR   rR   rS   r�   ß   sL    þ
þ




"

0

c
þ



%

Lr�   c                   @   s8   e Zd ZdZejfZdd„ Zee	 
¡  dƒdd„ ƒZdS )ÚUNIXDatagramTestsBuilderzE
    Builder defining tests relating to L{IReactorUNIXDatagram}.
    c                 C   r‚   )zƒ
        The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
        is created with the mode specified.
        ÚlistenUNIXDatagramN)rT   r   r-   rq   rR   rR   rS   Útest_listenMode¿  r…   z(UNIXDatagramTestsBuilder.test_listenModer†   c                 C   r‡   )z½
        On Linux, a UNIX socket path may begin with C{' '} to indicate a
        socket in the abstract namespace.  L{IReactorUNIX.listenUNIXDatagram}
        accepts such a path.
        rˆ   N)r\   rI   r  r-   rL   r‰   r   rŠ   rR   rR   rS   rŒ   Æ  s   
z<UNIXDatagramTestsBuilder.test_listenOnLinuxAbstractNamespaceN)rU   rV   rW   rX   r   ZIReactorUNIXDatagramr^   r   r   rB   r  rŒ   rR   rR   rR   rS   r  µ  s    þr  c                   @   s>   e Zd ZU dZeefZeee	e
   ed< dd„ Zdd„ ZdS )ÚSocketUNIXMixinzb
    Mixin which uses L{IReactorSocket.adoptStreamPort} to hand out listening
    UNIX ports.
    r^   c                 C   s\   t tƒ}tddd�}| |¡ | d¡ | d¡ z| | ¡ |j|¡W | 	¡  S | 	¡  w )zj
        Get a UNIX port from a reactor, wrapping an already-initialized file
        descriptor.
        r_   r`   ra   é   FN)
r   r   r   r«   Úlistenr  ZadoptStreamPortr¢   Úfamilyr   )rN   r   rQ   ZportSockrP   rR   rR   rS   ÚgetListeningPortá  s   


z SocketUNIXMixin.getListeningPortc                 C   ó   |  |j|¡S ©aZ  
        Connect to a listening UNIX socket.

        @param reactor: The reactor under test.
        @type reactor: L{IReactorUNIX}

        @param address: The listening's address.
        @type address: L{UNIXAddress}

        @param factory: The client factory.
        @type factory: L{ClientFactory}

        @return: The connector
        N©r’   rf   ©rN   r   ÚaddressrQ   rR   rR   rS   ÚconnectToListenerñ  ó   z!SocketUNIXMixin.connectToListenerN)rU   rV   rW   rX   r+   r*   r^   r   r   r   r   rh   r%  r+  rR   rR   rR   rS   r!  Ö  s   
 þr!  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚListenUNIXMixinzZ
    Mixin which uses L{IReactorTCP.listenUNIX} to hand out listening UNIX
    ports.
    c                 C   s   t ddd�}| ||¡S )z0
        Get a UNIX port from a reactor
        r_   r`   ra   N)r   rƒ   )rN   r   rQ   rP   rR   rR   rS   r%  	  s   z ListenUNIXMixin.getListeningPortc                 C   r&  r'  r(  r)  rR   rR   rS   r+    r,  z!ListenUNIXMixin.connectToListenerN)rU   rV   rW   rX   r%  r+  rR   rR   rR   rS   r-    s    r-  c                   @   s8   e Zd ZU efZeeee   e	d< dd„ Z
dd„ ZdS )ÚUNIXPortTestsMixinr^   c                 C   s   |› dt | ¡ jƒ›�S )zZ
        Get the message expected to be logged when a UNIX port starts listening.
        z starting on N©r;   r‰   rf   )rN   r‹   rQ   rR   rR   rS   Ú#getExpectedStartListeningLogMessage&  s   z6UNIXPortTestsMixin.getExpectedStartListeningLogMessagec                 C   s   dt | ¡ jƒ› d�S )zJ
        Get the expected connection lost message for a UNIX port
        z(UNIX Port z Closed)Nr/  )rN   r‹   rR   rR   rS   ÚgetExpectedConnectionLostLogMsg,  s   z2UNIXPortTestsMixin.getExpectedConnectionLostLogMsgN)rU   rV   rW   r+   r^   r   r   r   r   rh   r0  r1  rR   rR   rR   rS   r.  #  s   
 r.  c                   @   ó   e Zd ZdZdS )ÚUNIXPortTestsBuilderz.
    Tests for L{IReactorUNIX.listenUnix}
    N©rU   rV   rW   rX   rR   rR   rR   rS   r3  3  ó    r3  c                   @   r2  )ÚUNIXFDPortTestsBuilderz3
    Tests for L{IReactorUNIX.adoptStreamPort}
    Nr4  rR   rR   rR   rS   r6  >  r5  r6  c                   @   s.   e Zd ZeeefZdd„ Zdd„ Zdd„ Z	dS )Ú%UNIXAdoptStreamConnectionTestsBuilderc           	      C   s~   |   ¡ }ddlm} G dd„ dtƒ}|ttƒ\}}| d¡ |  |j¡ |  |j¡ | 	¡ }|ƒ }| 
|t|¡}|  |¡ dS )z€
        {IReactorSocket.adoptStreamConnection} returns None if the given
        factory's buildProtocol returns None.
        r   rÌ   c                   @   r”   )zXUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactoryc                 S   rÞ   rs   rR   )rN   r*  rR   rR   rS   ÚbuildProtocol_  rà   zfUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactory.buildProtocolN)rU   rV   rW   r8  rR   rR   rR   rS   ÚNoneFactory^  rœ   r9  FN)rI   r   rÍ   r.   r   r
   r  rè   r   r¢   ÚadoptStreamConnectionZassertIsNone)	rN   r   rÍ   r9  Ús1Ús2Zs1FDrQ   rÊ   rR   rR   rS   Útest_buildProtocolReturnsNoneP  s   

zCUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNonec                    s<   ‡ fdd„}ˆ   ¡ }ˆ j|ddd�}| |¡ ˆ  |¡ dS )z>
        Helper method to test UNIX server addresses.
        c                    sŠ   | \}}}z9t d| ¡ jƒ}ˆ  d|jj|f t|jƒ¡ ˆ  d|jj|f |jj¡ |jj	d }ˆ  
|t¡ W |j ¡  d S |j ¡  w )NrF   z <AccumulatingProtocol #%s on %s>zAccumulatingProtocol,%s,%sr   )r=   r‰   rf   rL   rm   Z	sessionnoÚstrZlogstrrQ   ZpeerAddressesrÆ   r   rp   )Ú	protocolsrg   rd   r‹   ZportPathr  rq   rR   rS   Ú	connectedq  s$   

ÿý
ÿýzOUNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIX.<locals>.connectedN)Ú	interfaceÚaddressFamily)rI   ÚgetConnectedClientAndServerr¬   Z
runReactor)rN   r@  r   r¯   rR   rq   rS   Útest_ServerAddressUNIXl  s   ÿ
z<UNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIXc                    sà   t ƒ }tƒ |_t ƒ ‰tƒ ˆ_tƒ ˆ_tƒ }tƒ |_tƒ |_tddd�}ˆ ||¡‰‡‡fdd„}|j |¡ t|jˆjgƒ}‡fdd„}	| 	|	¡ tƒ ‰ ˆ  
|	¡ t|jˆjgƒ}
‡ ‡fdd	„}|
 |¡ ˆ ˆ ¡ j|¡ ˆ S )a0  
        Return a L{Deferred} firing with a L{MyClientFactory} and
        L{MyServerFactory} connected pair, and the listening C{Port}. The
        particularity is that the server protocol has been obtained after doing
        a C{adoptStreamConnection} against the original server connection.
        r_   r`   ra   c                    s0   ˆ   | j¡ ˆ  | j¡ ˆ  | j ¡ tˆ¡ d S rs   )ZremoveReaderrm   ZremoveWriterr:  r¢   r   )rÝ   )r   rd   rR   rS   ÚfirstServerConnected¤  s   z_UNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.firstServerConnectedc                    s   ˆ j rˆ  ¡  | S rs   )ÚrunningrÀ   )rÊ   )r   rR   rS   rÀ   ¯  s   zOUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.stopc                    s   | \}}ˆ   ||ˆf¡ d S rs   )rz   )r?  rg   rd   )Údeferredr‹   rR   rS   r¸   ½  s   zPUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.startN)r8   r    ÚprotocolConnectionMadeZprotocolConnectionLostr7   r   rƒ   r¬   r"   r®   r­   r’   r‰   rf   )rN   r   rA  rB  ZfirstServerrg   rP   rE  ZlostDeferredrÀ   ZstartDeferredr¸   rR   )rG  r‹   r   rd   rS   rC  Ž  s4   
ÿ


ÿ
zAUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServerN)
rU   rV   rW   r)   r*   r+   r^   r=  rD  rC  rR   rR   rR   rS   r7  I  s    ý"r7  c                   @   s6   e Zd ZdZefZdZedd„ ƒZdd„ Z	dd„ Z
dS )	ÚUnixClientTestsBuilderz7
    Define tests for L{IReactorUNIX.connectUNIX}.
    Nc                 C   s   | j du r
t| ƒ| _ | j S )z‹
        Return a path usable by C{connectUNIX} and C{listenUNIX}.

        @return: A path instance, built with C{_abstractPath}.
        N)Ú_pathr\   rq   rR   rR   rS   rP   ×  s   

zUnixClientTestsBuilder.pathc                 C   ó   |  | j|¡S )z×
        Start an UNIX server with the given C{factory}.

        @param reactor: The reactor to create the UNIX port in.

        @param factory: The server factory.

        @return: A UNIX port instance.
        N)rƒ   rP   ©rN   r   rQ   rR   rR   rS   r#  â  ó   
zUnixClientTestsBuilder.listenc                 C   rK  )zÝ
        Start an UNIX client with the given C{factory}.

        @param reactor: The reactor to create the connection in.

        @param factory: The client factory.

        @return: A UNIX connector instance.
        N)r’   rP   rL  rR   rR   rS   Úconnectî  rM  zUnixClientTestsBuilder.connect)rU   rV   rW   rX   r+   r^   rJ  ÚpropertyrP   r#  rN  rR   rR   rR   rS   rI  Î  s    

rI  Úposixz%UNIX sockets only supported on POSIX.c                   @   sR   e Zd ZdZdZdd„ Zdeeegdf ddfdd	„Zd
d„ Z	dd„ Z
dd„ ZdS )ÚRealSocketTestsz,
    Test real UNIX socket connections.
    Zunixpathc                 Ã   sž   �t j t¡s	J ‚tƒ }t | j|¡}t|tj	ƒsJ ‚||_
|  |j¡ tƒ }t | j|¡ |jI dH  |js9J ‚|  |jj¡ |  |j¡ |j tj¡ dS )z|
        A port created with L{IReactorTCP.listenUNIX} can be connected to with
        L{IReactorTCP.connectUNIX}.
        N)r   r+   Ú
providedByr   r6   rƒ   rP   Ú
isinstancer   ZPortr‹   rè   ZcleanUpr7   r’   rG  ZlastProtocolrÃ   ZmadeÚdisconnectedZ
lostReasonrÇ   r   ZConnectionDone)rN   Úfr‹   ÚclientFrR   rR   rS   Útest_closePortInProtocolFactory  s   €
z/RealSocketTests.test_closePortInProtocolFactoryrz   NÚreturnc           	      Ã   sª   �t j t¡s	J ‚tƒ }tƒ }||_t | j|¡}|  	|j
¡ tƒ }tƒ }||_t | j|¡ t||gƒI dH \}}|||ƒ |jsDJ ‚|jsIJ ‚|j ¡  |j ¡  dS )z‰
        Invoke the given callback with a client protocol and a server protocol
        which have been connected to each other.
        N)r   r+   rR  r   r8   r    rH  rƒ   rP   rè   rK   r7   r’   r"   rm   rp   )	rN   rz   ZserverFactoryZserverConnMader‹   rV  ZclientConnMadeÚserverProtocolÚclientProtocolrR   rR   rS   Ú_connectedClientAndServerTest  s&   €ÿ



z-RealSocketTests._connectedClientAndServerTestc                 ƒ   ó0   �dt dt ddf‡ fdd„}ˆ  |¡I dH  dS )zå
        The transport of a protocol connected with L{IReactorTCP.connectUNIX} or
        L{IReactor.TCP.listenUNIX} can have L{ITCPTransport.getTcpNoDelay} or
        L{ITCPTransport.setTcpNoDelay} called without error.
        rY  rZ  rX  Nc                    sn   | |fD ]0}t j |j¡sJ ‚|j}ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ qd S )Nr   TF)r   ÚITCPTransportrR  rm   rL   ZgetTcpNoDelayZsetTcpNoDelay©rY  rZ  Úprm   rq   rR   rS   ÚcheckA  ó   

ùz.RealSocketTests.test_tcpNoDelay.<locals>.check©rD   r[  ©rN   r`  rR   rq   rS   Útest_tcpNoDelay:  s   €ÿÿþzRealSocketTests.test_tcpNoDelayc                 ƒ   r\  )a  
        The transport of a protocol connected with L{IReactorTCP.connectUNIX} or
        L{IReactor.TCP.listenUNIX} can have its I{SO_KEEPALIVE} state inspected
        and manipulated with L{ITCPTransport.getTcpKeepAlive} and
        L{ITCPTransport.setTcpKeepAlive}.
        rY  rZ  rX  Nc                    sn   | |fD ]0}t j |j¡sJ ‚|j}ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ qd S )NFT)r   r]  rR  rm   rL   ZgetTcpKeepAliveZsetTcpKeepAliver^  rq   rR   rS   r`  W  ra  z0RealSocketTests.test_tcpKeepAlive.<locals>.checkrb  rc  rR   rq   rS   Útest_tcpKeepAliveO  s   €ÿÿþz!RealSocketTests.test_tcpKeepAlivec                 Ã   sF   �t j t¡s	J ‚tƒ }tjd|dd� |jI dH  |j t	j
¡ dS )zb
        Tests whether attempting to establish connection to non-existing UNIX path fails
        znon-existingé   )ÚtimeoutN)r   r+   rR  r   r7   r’   ZfailDeferredru   rÇ   r   ZConnectError)rN   rV  rR   rR   rS   Útest_Failinge  s   €zRealSocketTests.test_Failing)rU   rV   rW   rX   rP   rW  r   rD   r[  rd  re  rh  rR   rR   rR   rS   rQ  þ  s    ÿ
þrQ  )vrX   Úhashlibr   Úosr   r   r   r   r   Úpprintr   r   r	   r
   r   r   Ústructr   Útempfiler   r   Útypingr   r   r   r   r   Zunittestr   r   Z_AF_UNIXÚImportErrorZzope.interfacer   r   Ztwisted.internetr   r   r   r   r   Ztwisted.internet.addressr   Ztwisted.internet.deferr    r!   r"   Ztwisted.internet.endpointsr#   r$   Ztwisted.internet.errorr%   r&   r'   Ztwisted.internet.interfacesr(   r)   r*   r+   Ztwisted.internet.protocolr,   r-   r.   Ztwisted.internet.taskr/   Z&twisted.internet.test.connectionmixinsr0   r1   r2   r3   r4   Z#twisted.internet.test.reactormixinsr5   Ztwisted.internet.test.test_tcpr6   r7   r8   r9   r:   Ztwisted.python.compatr;   Ztwisted.python.failurer<   Ztwisted.python.filepathr=   Ztwisted.python.logr>   r?   r@   Ztwisted.python.reflectrA   Ztwisted.python.runtimerB   rC   Ztwisted.test.test_tcprD   Ztwisted.trial.unittestrE   rÐ   r  rG   r\   r]   ri   rw   r�   r  r!  r-  r.  r3  r6  r7  ÚglobalsÚupdateZmakeTestCaseClassesrI  rQ  rR   rR   rR   rS   Ú<module>   sš   ÿÿ%>   Y!- 
ü
ü~-