o
    >h                     @   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 reis  eq rejs  eq rens  eq reos  eq reps  G d>d? d?eHeEZteq rets  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   unixPort rS   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/internet/test/test_unix.py	_modeTestR   s
   zUNIXFamilyMixin._modeTestN)__name__
__module____qualname____doc__rU   rS   rS   rS   rT   rG   M   s    rG   c                 C   s   t td S )zI
    Return a new, unique abstract namespace path to be listened on.
    d   )r   r   	hexdigest)caserS   rS   rT   _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dir)r   r$   )rN   r   rP   rS   rS   rT   serverl      
zUNIXCreator.serverc                 C   s   t ||jS )z3
        Construct a UNIX client endpoint.
        )r#   name)rN   r   serverAddressrS   rS   rT   clientt   s   zUNIXCreator.clientN)rV   rW   rX   rY   r   r+   r_   r   r   r   r   __annotations__re   ri   rS   rS   rS   rT   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   rl   rm   rS   rS   rT   __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sendFileDescriptorrl   rm   writeloseConnectionrN   rS   rS   rT   connectionMade   s   z!SendFileDescriptor.connectionMadec                 C   s   t | | || _d S N)r0   connectionLostreasonrN   rw   rS   rS   rT   rv      s   
z!SendFileDescriptor.connectionLost)rV   rW   rX   rY   rw   rn   rt   rv   rS   rS   rS   rT   rk   {   s    
rk   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)rw   r    waitingr!   rs   rS   rS   rT   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)rz   callback)rN   
descriptorrS   rS   rT   fileDescriptorReceived   rf   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.)rz   errbackr<   	ExceptionrN   rm   rS   rS   rT   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   rv   rz   r   rw   rx   rS   rS   rT   rv      s
   

z$ReceiveFileDescriptor.connectionLost)
rV   rW   rX   rY   rw   rz   r{   r~   r   rv   rS   rS   rS   rT   ry      s    ry   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)rU   r   r.   rs   rS   rS   rT   	test_mode      zUNIXTestsBuilder.test_mode8Abstract 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   portrS   rS   rT   #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)OSErrorrs   rS   rS   rT   raiseSocketError  s   z=UNIXTestsBuilder.test_listenFailure.<locals>.raiseSocketErrorcreateInternetSocketznot-usedN)patchr   BasePortrI   assertRaisesr%   r   r.   )rN   r   r   rS   rS   rT   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   getDestinationr   )rN   rP   r   	connectorrS   rS   rT   $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	addressesrr   )rN   ro   rS   rS   rT   makeConnection#  s   zCUNIXTestsBuilder.test_addresses.<locals>.SaveAddress.makeConnectionN)rV   rW   rX   r   rS   rS   rS   rT   SaveAddress"      r   r   r   N)r0   r4   	endpointsrL   r   )rN   r   re   ri   rS   rS   rT   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 ru   )r	   r
   r   rL   getsocknameassertNotEqualfileno)r}   received)r   srN   rS   rT   checkDescriptor@  s   zAUNIXTestsBuilder.test_sendFileDescriptor.<locals>.checkDescriptorz-Sending file descriptor encountered a problemc                    
    j  S ru   ro   rr   ignoredre   rS   rT   <lambda>P     
 z:UNIXTestsBuilder.test_sendFileDescriptor.<locals>.<lambda>N)r   r   bindrk   r   ry   r{   addCallback
addErrbackr?   addBothr4   r   )rN   ri   dr   rS   )r   r   rN   re   rT   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 ru   )ro   pauseProducingrs   rS   rS   rT   rt   \  s   zbUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotRead.connectionMadeNrV   rW   rX   rt   rS   rS   rS   rT   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)ro   rp   r   r   rq   rS   rs   rS   rT   senderf  s   zUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMade.<locals>.senderr   zSend loop failure)
r   ro   registerProducerr/   taskr   clockstartr   r?   )rN   r   rS   rs   rT   rt   b  s   
zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMadec                 S      |    d S ru   _disconnectrs   rS   rS   rT   stopProducingn     znUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.stopProducingc                 S   r   ru   r   rs   rS   rS   rT   resumeProducingq  r   zpUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.resumeProducingc                 S   s   d| _ | j  |   d S )NT)pausedro   unregisterProducerr   rs   rS   rS   rT   r   t  s   
zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.pauseProducingc                 S   s$   | j   | j  | jj  d S ru   )r   stopro   abortConnectionotherrs   rS   rS   rT   r   y  s   

zlUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors._disconnectN)	rV   rW   rX   r   rt   r   r   r   r   rS   rS   rS   rT   SendsManyFileDescriptors_  s    r   z*sendFileDescriptor producer was not pausedN)r0   r   r4   r   
assertTruer   )rN   r   r   re   ri   rS   rS   rT   -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   ru   r   r   r   rS   rT   r     r   z=UNIXTestsBuilder.test_fileDescriptorOverrun.<locals>.<lambda>r   )r   rk   r   ry   r{   r   appendr4   r   assertIsInstancer<   trapr&   rw   valuer'   )rN   cargori   resultr   rS   r   rT   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 ru   )r   st_devst_ino)rl   fsrS   rS   rT   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 ru   )fdsdeviceInodesReceivedrs   rS   rS   rT   rn        
zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.__init__c                    s(   | j | | j | t| d S ru   )r   r   r   r   )rN   rl   r   rS   rT   r~     s   zgUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.fileDescriptorReceivedN)rV   rW   rX   rn   r~   rS   r   rS   rT   FakeProtocol  s    r   c                   @   r   )zPUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver   c                 S   s   || _ || _d S ru   )r   protocol)rN   sktprotorS   rS   rT   rn     r   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.__init__c                 S      d S ru   rS   r   rS   rS   rT   _dataReceived     z^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._dataReceivedc                 S   r   ru   rS   rs   rS   rS   rT   r     r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getHostc                 S   r   ru   rS   rs   rS   rS   rT   r     r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getPeerc                 S   r   ru   rS   )rN   orS   rS   rT   _getLogPrefix  r   z^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._getLogPrefixN)	rV   rW   rX   
bufferSizern   r   r   r   r   rS   rS   rS   rT   FakeReceiver  s    r   s   some data needs to be sentc                    s   g | ]} |qS rS   rS   .0rl   r   rS   rT   
<listcomp>  s    zNUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.<listcomp>N)r   r   twisted.internet.unixr   twisted.python.sendmsgr   r   r(   r0   r   r
   
addCleanupr   r   r   doReadrL   lenr   assertFalsesetintersectionr   )rN   ancillaryPackerr   r   r   r   r   
sendSocket
recvSocketr   receiver	fileOneFDfileOneName	fileTwoFDfileTwoName
dataToSend	fdsToSend	ancillaryexpectedCountdeviceInodesSentrS   r   rT   )_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  rS   rT   r   	  s   z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG.<locals>.ancillaryPackerNr   r  r
  rN   r   rS   r  rT   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  rS   rT   r     s    zpUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPacker.<locals>.<listcomp>r  rS   r  r  rS   rT   r     s   z\UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPackerNr  r  rS   r  rT   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   rS   r  rS   rS   rT   r   3  s   z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.ancillaryPackerc                    s   d}dg}d}  |||S )Ns	   some data)NN    r   )ReceivedMessage)r   argskwargsrm   r  flagsr   rS   rT   fakeRecvmsgUnsupportedAncillary8  s   zkUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.fakeRecvmsgUnsupportedAncillaryrecvmsgz#received unsupported ancillary datac                 3   s    | ]	} |d  v V  qdS )formatNrS   )r   e)expectedMessagerS   rT   	<genexpr>G  s    zUUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.<genexpr>z+Expected message not found in logged eventsN)
twisted.pythonr   r>   r   r   r@   r   r
  anyr   )rN   r   r  eventsfoundrS   )r   r   rT   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 nq[ dS qU| 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 ru   )ro   r   hostAddressr   peerAddressrk   rt   rs   rS   rS   rT   rt   _  s   zaUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddresses.connectionMadeNr   rS   rS   rS   rT   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(  protocolNamer  z3Expected event (%s) not found in logged events (%s)N)r   r   r>   r   r   r@   rk   r   r0   r4   r   r   setblockingrL   recvr   r(  r'  itemsgetr!   r   )rN   r   probeClientprobeServerr$  r)  re   ri   r  clsNameexpectedEventlogEventkvrS   rS   rT    test_avoidLeakingFileDescriptorsJ  sJ   


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 ru   )r0   rt   r$  rs   rS   rS   rT   rt     s   

zYUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.connectionMadec                    s      t| | jt| d S ru   )r   r   r$  r   type)	innerSelfr}   rs   rS   rT   r~     s   zaUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.fileDescriptorReceivedc                 S   s   | j | d S ru   )r$  extendr   rS   rS   rT   r     s   zWUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.dataReceivedN)rV   rW   rX   rt   r~   r   rS   rs   rS   rT   RecordEvents  s    r:  r   r      N)r   r(   r0   r   rk   r   r4   r   rL   intr$  bytes)rN   r:  r   re   ri   rS   rs   rT   #test_descriptorDeliveredBeforeBytes  s   z4UNIXTestsBuilder.test_descriptorDeliveredBeforeBytesN)rV   rW   rX   rY   r+   r_   r^   r   r   r   rB   isLinuxr   r   r   r   r   sendmsgSkipReasonr   r   r   r
  r  isMacOSXr  r&  r6  r>  rS   rS   rS   rT   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)rU   r   r-   rs   rS   rS   rT   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   rC  r-   rL   r   r   r   rS   rS   rT   r     s   
z<UNIXDatagramTestsBuilder.test_listenOnLinuxAbstractNamespaceN)rV   rW   rX   rY   r   IReactorUNIXDatagramr_   rD  r   rB   r?  r   rS   rS   rS   rT   rB    s    rB  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`   ra   rb      F)
r   r   r   r   listenr+  adoptStreamPortr   familyr   )rN   r   rQ   portSockrP   rS   rS   rT   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
        r   rg   rN   r   addressrQ   rS   rS   rT   connectToListener     z!SocketUNIXMixin.connectToListenerN)rV   rW   rX   rY   r+   r*   r_   r   r   r   r   rj   rL  rR  rS   rS   rS   rT   rF    s   
 rF  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`   ra   rb   )r   r   )rN   r   rQ   rP   rS   rS   rT   rL  	  s   z ListenUNIXMixin.getListeningPortc                 C   rM  rN  rO  rP  rS   rS   rT   rR    rS  z!ListenUNIXMixin.connectToListenerN)rV   rW   rX   rY   rL  rR  rS   rS   rS   rT   rT    s    rT  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 r;   r   rg   )rN   r   rQ   rS   rS   rT   #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)rV  )rN   r   rS   rS   rT   getExpectedConnectionLostLogMsg,  s   z2UNIXPortTestsMixin.getExpectedConnectionLostLogMsgN)rV   rW   rX   r+   r_   r   r   r   r   rj   rW  rX  rS   rS   rS   rT   rU  #  s   
 rU  c                   @      e Zd ZdZdS )UNIXPortTestsBuilderz.
    Tests for L{IReactorUNIX.listenUnix}
    NrV   rW   rX   rY   rS   rS   rS   rT   rZ  3      rZ  c                   @   rY  )UNIXFDPortTestsBuilderz3
    Tests for L{IReactorUNIX.adoptStreamPort}
    Nr[  rS   rS   rS   rT   r]  >  r\  r]  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   ru   rS   )rN   rQ  rS   rS   rT   buildProtocol_  r   zfUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactory.buildProtocolN)rV   rW   rX   r_  rS   rS   rS   rT   NoneFactory^  r   r`  FN)rI   r   r   r.   r   r
   r+  r   r   r   adoptStreamConnectionassertIsNone)	rN   r   r   r`  s1s2s1FDrQ   r   rS   rS   rT   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   rg   rL   ro   	sessionnostrlogstrrQ   peerAddressesr   r   rr   )	protocolsri   re   r   portPathr(  rs   rS   rT   	connectedq  s$   


zOUNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIX.<locals>.connectedN)	interfaceaddressFamily)rI   getConnectedClientAndServerr   
runReactor)rN   rm  r   r   rS   rs   rT   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`   ra   rb   c                    s0     | j  | j  | j t d S ru   )removeReaderro   removeWriterra  r   r   )r   )r   re   rS   rT   firstServerConnected  s   z_UNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.firstServerConnectedc                    s    j r   | S ru   )runningr   )r   )r   rS   rT   r     s   zOUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.stopc                    s   | \}}  ||f d S ru   )r|   )rk  ri   re   )deferredr   rS   rT   r     s   zPUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.start)r8   r    protocolConnectionMadeprotocolConnectionLostr7   r   r   r   r"   r   r   r   r   rg   )rN   r   rn  ro  firstServerri   rP   ru  lostDeferredr   startDeferredr   rS   )rw  r   r   re   rT   rp    s4   




zAUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServerN)
rV   rW   rX   r)   r*   r+   r_   rf  rr  rp  rS   rS   rS   rT   r^  I  s    "r^  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]   rs   rS   rS   rT   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.
        )r   rP   rN   r   rQ   rS   rS   rT   rH       
zUnixClientTestsBuilder.listenc                 C   r  )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.
        )r   rP   r  rS   rS   rT   connect  r  zUnixClientTestsBuilder.connect)rV   rW   rX   rY   r+   r_   r~  propertyrP   rH  r  rS   rS   rS   rT   r}    s    

r}  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.
    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   Portr   r   cleanUpr7   r   rw  lastProtocolr   madedisconnected
lostReasonr   r   ConnectionDone)rN   fr   clientFrS   rS   rT   test_closePortInProtocolFactory  s   
z/RealSocketTests.test_closePortInProtocolFactoryr|   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+   r  r   r8   r    rx  r   rP   r   rK   r7   r   r"   ro   rr   )	rN   r|   serverFactoryserverConnMader   r  clientConnMadeserverProtocolclientProtocolrS   rS   rT   _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.
        r  r  r  Nc                    sn   | |fD ]0}t j|jsJ |j} | d |d  | d |d  | d qd S )Nr   TF)r   ITCPTransportr  ro   rL   getTcpNoDelaysetTcpNoDelayr  r  pro   rs   rS   rT   checkA     

z.RealSocketTests.test_tcpNoDelay.<locals>.checkrD   r  rN   r  rS   rs   rT   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}.
        r  r  r  Nc                    sn   | |fD ]0}t j|jsJ |j} | d |d  | d |d  | d qd S )NFT)r   r  r  ro   rL   getTcpKeepAlivesetTcpKeepAliver  rs   rS   rT   r  W  r  z0RealSocketTests.test_tcpKeepAlive.<locals>.checkr  r  rS   rs   rT   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+   r  r   r7   r   failDeferredrw   r   r   ConnectError)rN   r  rS   rS   rT   test_Failinge  s   zRealSocketTests.test_Failing)rV   rW   rX   rY   rP   r  r   rD   r  r  r  r  rS   rS   rS   rT   r    s    
r  )vrY   hashlibr   osr   r   r   r   r   pprintr   r   r	   r
   r   r   structr   tempfiler   r   typingr   r   r   r   r   unittestr   r   _AF_UNIXImportErrorzope.interfacer   r   twisted.internetr   r   r   r   r   twisted.internet.addressr   twisted.internet.deferr    r!   r"   twisted.internet.endpointsr#   r$   twisted.internet.errorr%   r&   r'   twisted.internet.interfacesr(   r)   r*   r+   twisted.internet.protocolr,   r-   r.   twisted.internet.taskr/   &twisted.internet.test.connectionmixinsr0   r1   r2   r3   r4   #twisted.internet.test.reactormixinsr5   twisted.internet.test.test_tcpr6   r7   r8   r9   r:   twisted.python.compatr;   twisted.python.failurer<   twisted.python.filepathr=   twisted.python.logr>   r?   r@   twisted.python.reflectrA   twisted.python.runtimerB   rC   twisted.test.test_tcprD   twisted.trial.unittestrE   r   r@  rG   r]   r^   rk   ry   r   rB  rF  rT  rU  rZ  r]  r^  globalsupdatemakeTestCaseClassesr}  r  rS   rS   rS   rT   <module>   s   %>   Y!- 

~-