
    Yf&                        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 dd
lmZ 	 ddlmZ eZn# e$ r dZY nw xY wddlmZm Z  ddl!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZA ddlBmCZC ddlDmEZEmFZFmGZGmHZH ddlImJZJ ddlKmLZL ddlMmNZN ddlOmPZPmQZQmRZR ddlSmTZT ddlUmVZV  eTd          ZWd ZX eTd          d!ZX G d" d#          ZYd$ ZZ G d% d&e?          Z[ G d' d(e=          Z\ e e2           G d) d*e=                      Z] G d+ d,eYeCe>          Z^ G d- d.eYeC          Z_ G d/ d0          Z` G d1 d2          Za G d3 d4          Zb G d5 d6eaebeCeG          Zc G d7 d8e`ebeCeG          Zd G d9 d:eHeC          Ze ef            g                    e^h                                            ef            g                    e_h                                            ef            g                    ech                                            ef            g                    edh                                            ef            g                    eeh                                            G d; d<eCe@          Zi ef            g                    eih                                           dS )=z/
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)OptionalSequenceType)skipIf)AF_UNIXN)	Interfaceimplementer)base
interfaces)UNIXAddress)DeferredfailgatherResults)UNIXClientEndpointUNIXServerEndpoint)CannotListenErrorConnectionClosedFileDescriptorOverrun)IFileDescriptorReceiverIReactorFDSetIReactorSocketIReactorUNIX)ClientFactoryDatagramProtocolServerFactory)LoopingCall)ConnectableProtocolConnectionTestsMixinEndpointCreatorStreamClientTestsMixinrunProtocolsWithReactor)ReactorBuilder)MyClientFactoryMyServerFactoryStreamTransportTestsMixinWriteSequenceTestsMixin)nativeString)Failure)_coerceToFilesystemEncoding)addObservererrremoveObserver)requireModule)platformztwisted.python.sendmsg z>sendmsg extension unavailable, extended UNIX features disabledc                       e Zd ZdZd ZdS )UNIXFamilyMixinzK
    Test-helper defining mixin for things related to AF_UNIX sockets.
    c                     d}|                                  } t          ||          |||          }|                                 |                     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factoryrB   reactorunixPorts          Q/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_unix.py	_modeTestzUNIXFamilyMixin._modeTestO   sy    
 ##%%/77J//gDIII   d!344d;;;;;    N)__name__
__module____qualname____doc__rO    rP   rN   r@   r@   J   s-         	< 	< 	< 	< 	<rP   r@   c                 ^    t          t          d                                                    S )zI
    Return a new, unique abstract namespace path to be listened on.
    d   )r   r   	hexdigest)cases    rN   _abstractPathrZ   [   s$     ws||&&(((rP   c                   ^    e Zd ZU dZej        fZeee	e
                           ed<   d Zd ZdS )UNIXCreatorz(
    Create UNIX socket end points.
    requiredInterfacesc                 D    t          dd          }t          ||          S )z3
        Construct a UNIX server endpoint.
        .sock.suffixdir)r   r    )rH   rL   rJ   s      rN   serverzUNIXCreator.serveri   s&    
 W#...!'4000rP   c                 ,    t          ||j                  S )z3
        Construct a UNIX client endpoint.
        )r   name)rH   rL   serverAddresss      rN   clientzUNIXCreator.clientq   s     "'=+=>>>rP   N)rQ   rR   rS   rT   r   r'   r]   r   r   r   r   __annotations__rd   rh   rU   rP   rN   r\   r\   b   sg           @J?V>X$y/!:;XXX1 1 1? ? ? ? ?rP   r\   c                   (    e Zd ZdZdZd Zd Z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                 "    || _         || _        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)rH   rm   rn   s      rN   __init__zSendFileDescriptor.__init__   s     			rP   c                     | 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sendFileDescriptorrm   rn   writeloseConnectionrH   s    rN   connectionMadez!SendFileDescriptor.connectionMade   sX    
 	))$'2229 	,N  +++%%'''''rP   c                 >    t          j        | |           || _        d S N)r,   connectionLostreasonrH   rz   s     rN   ry   z!SendFileDescriptor.connectionLost   s     *4888rP   )rQ   rR   rS   rT   rz   ro   rv   ry   rU   rP   rN   rk   rk   x   sR          F	 	 	( ( (    rP   rk   c                   2    e Zd ZdZdZdZd Zd Zd Z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                 l    | j         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.
        )rz   r   waitingr   ru   s    rN   waitForDescriptorz'ReceiveFileDescriptor.waitForDescriptor   s/     ;#::DL<$$$rP   c                 H    | j                             |           d| _         dS )z
        Fire the waiting Deferred, initialized by C{waitForDescriptor}, with the
        file descriptor just received.
        N)r   callback)rH   
descriptors     rN   fileDescriptorReceivedz,ReceiveFileDescriptor.fileDescriptorReceived   s%    
 	j)))rP   c           	          | j         A| 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.)r   errbackr7   	ExceptionrH   rn   s     rN   dataReceivedz"ReceiveFileDescriptor.dataReceived   sZ     <#L  	"QT"Q"Q"QRRSS    DLLL	 $#rP   c                     t          j        | |           | j        !| j                            |           d| _        || _        dS )zj
        Fail the waiting Deferred, initialized by C{waitForDescriptor}, if there
        is one.
        N)r,   ry   r   r   rz   r{   s     rN   ry   z$ReceiveFileDescriptor.connectionLost   sG    
 	*4888<#L  (((DLrP   )
rQ   rR   rS   rT   rz   r   r   r   r   ry   rU   rP   rN   r}   r}      sf        
 
 FG
% 
% 
%       	 	 	 	 	rP   r}   c                   n   e Zd ZdZefZ e            Zd Z e	 e
j                     d          d             Zd Z e	 e
j                     d          d             Zd Z e	e e          d             Z e	e e          d	             Z e	e e          d
             Zd Z e	e e          d             Z e	 e
j                    d           e	e e          d                         Z e	e e          d             Z e	e e          d             Z e	e e          d             ZdS )UNIXTestsBuilderz=
    Builder defining tests relating to L{IReactorUNIX}.
    c                 p    |                      d|                                 t                                 dS )zs
        The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with
        the mode specified.
        
listenUNIXN)rO   r   r*   ru   s    rN   	test_modezUNIXTestsBuilder.test_mode   s,    
 	|T[[]]MOODDDDDrP   8Abstract namespace UNIX sockets only supported on Linux.c                    t          |           }|                                 }|                    d|z   t                                }|                     |                                t          d|z                        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)rZ   rC   r   r*   rF   getHostr   rH   rJ   rL   ports       rN   #test_listenOnLinuxAbstractNamespacez4UNIXTestsBuilder.test_listenOnLinuxAbstractNamespace   so     T""##%%!!$+}??TD[)A)ABBBBBrP   c                     d }|                      t          j        d|           |                                 }|                     t
                    5  |                    dt                                 ddd           dS # 1 swxY w Y   dS )z
        L{IReactorUNIX.listenUNIX} raises L{CannotListenError} if the
        underlying port's createInternetSocket raises a socket error.
        c                      t          d          )Nz FakeBasePort forced socket.error)OSErrorru   s    rN   raiseSocketErrorz=UNIXTestsBuilder.test_listenFailure.<locals>.raiseSocketError  s    <===rP   createInternetSocketznot-usedN)patchr   BasePortrC   assertRaisesr!   r   r*   )rH   r   rL   s      rN   test_listenFailurez#UNIXTestsBuilder.test_listenFailure   s    	> 	> 	> 	

4="8:JKKK##%%011 	< 	<z=??;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   #BB
Bc                    t          |           }|                                 }|                    d|z   t                                }|                     |                                t          d|z                        dS )zc
        L{IReactorUNIX.connectUNIX} also accepts a Linux abstract namespace
        path.
        r   N)rZ   rC   connectUNIXr(   rF   getDestinationr   )rH   rJ   rL   	connectors       rN   $test_connectToLinuxAbstractNamespacez5UNIXTestsBuilder.test_connectToLinuxAbstractNamespace
  ss     T""##%%''t]__EE	1133[5M5MNNNNNrP   c                 2    G 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 ZdS )4UNIXTestsBuilder.test_addresses.<locals>.SaveAddressc                     t          |                                |                                          | _        |                                 d S )N)hostpeer)dictr   getPeer	addressesrt   )rH   rq   s     rN   makeConnectionzCUNIXTestsBuilder.test_addresses.<locals>.SaveAddress.makeConnection   sO    !%"**,,93D3D3F3F" " " ((*****rP   N)rQ   rR   rS   r   rU   rP   rN   SaveAddressr     s#        + + + + +rP   r   r   r   N)r,   r0   	endpointsrF   r   )rH   r   rd   rh   s       rN   test_addresseszUNIXTestsBuilder.test_addresses  s    	+ 	+ 	+ 	+ 	+- 	+ 	+ 	+ ffdnEEE)&163CF3KLLL)&163CF3KLLLLLrP   c                     ddl m t                                          d           t                                          d          t                      }|                                } fd}|                    |           |                    t          d           |
                    f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)r>   r      junkc                 <    | t           t                    }t          |                                                                |                                                                                               |                                           d S rx   )r
   r   r   rF   getsocknameassertNotEqualfileno)r   receivedr   srH   s     rN   checkDescriptorzAUNIXTestsBuilder.test_sendFileDescriptor.<locals>.checkDescriptor=  s    vj';??H* Q]]__h.B.B.D.DEEE
 

HOO,=,=>>>>>rP   z-Sending file descriptor encountered a problemc                 6    j                                         S rx   rq   rt   ignoredrd   s    rN   <lambda>z:UNIXTestsBuilder.test_sendFileDescriptor.<locals>.<lambda>M      &"2"A"A"C"C rP   N)r   r   bindrk   r   r}   r   addCallback
addErrbackr:   addBothr0   r   )rH   rh   dr   r   r   rd   s   `   @@@rN   test_sendFileDescriptorz(UNIXTestsBuilder.test_sendFileDescriptor.  s     	"!!!!!HH	w#AHHJJ88&(($$&&	? 	? 	? 	? 	? 	? 	? 	
o&&&	SIJJJ			CCCCDDDffdnEEEEErP   c                      G d dt                     } G 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                       e Zd Zd ZdS )SUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotReadc                 8    | j                                          d S rx   )rq   pauseProducingru   s    rN   rv   zbUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotRead.connectionMadeY  s    --/////rP   NrQ   rR   rS   rv   rU   rP   rN   DoesNotReadr   X  s#        0 0 0 0 0rP   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )`UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptorsFc                 (    t                       _          j                             d            fd}t          |           _         j        j         j        _         j                            d                              t          d           d S )NTc                       j                              j                                                    j                             d           d S )N   x)rq   rr   r   r   rs   ru   s   rN   senderzUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMade.<locals>.senderc  sC    N55dk6H6H6J6JKKKN((.....rP   r   zSend loop failure)
r   rq   registerProducerr+   taskrL   clockstartr   r:   )rH   r   s   ` rN   rv   zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMade_  s    $hh//d;;;/ / / / / (//	"&."8		""--c3FGGGGGrP   c                 .    |                                   d S rx   _disconnectru   s    rN   stopProducingznUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.stopProducingk        """""rP   c                 .    |                                   d S rx   r   ru   s    rN   resumeProducingzpUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.resumeProducingn  r   rP   c                 n    d| _         | j                                         |                                  d S )NT)pausedrq   unregisterProducerr   ru   s    rN   r   zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.pauseProducingq  s5    "11333  """""rP   c                     | j                                          | j                                         | j        j                                         d S rx   )r   stoprq   abortConnectionotherru   s    rN   r   zlUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors._disconnectv  sE    	   ..000
$4466666rP   N)	rQ   rR   rS   r   rv   r   r   r   r   rU   rP   rN   SendsManyFileDescriptorsr   \  sg        F
H 
H 
H# # ## # ## # #
7 7 7 7 7rP   r   z*sendFileDescriptor producer was not pausedN)r,   r   r0   r   
assertTruer   )rH   r   r   rd   rh   s        rN   -test_sendFileDescriptorTriggersPauseProducingz>UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducingQ  s    	0 	0 	0 	0 	0- 	0 	0 	0	7 	7 	7 	7 	7': 	7 	7 	7> *)++ffdnEEE'STTTTTrP   c                    t                      }t          |                                d          t                      }g }|                                }|                    |j                   |                    f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                 6    j                                         S rx   r   r   s    rN   r   z=UNIXTestsBuilder.test_fileDescriptorOverrun.<locals>.<lambda>  r   rP   r   )r   rk   r   r}   r   r   appendr0   r   assertIsInstancer7   trapr"   rz   valuer#   )rH   cargorh   resultr   rd   s        @rN   test_fileDescriptorOverrunz+UNIXTestsBuilder.test_fileDescriptorOverrun  s     #ELLNND99&(($$&&			&-   			CCCCDDDffdnEEEfQi111q	'(((fm13HIIIIIrP   c                    ddl m} ddlm} ddlm} d t          t                     G fddt                                } G d d	|          } |t          t                    \  }}|                     |j                   |                     |j                    |            }	 |||	          }
t                      \  }}t                      \  }}|                     t          |           |                     t          |           d
}||g} ||          \  }} ||||           |
                                 |                     t#          |	j                  |           |                     t)          |                              t)          |	j                                       |	j        r+f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                 <    t          |           }|j        |j        fS rx   )r   st_devst_ino)rm   fss     rN   deviceInodeTuplezTUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.deviceInodeTuple  s    rBIry))rP   c                        e Zd Zd Z fdZdS )PUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocolc                 "    g | _         g | _        d S rx   )fdsdeviceInodesReceivedru   s    rN   ro   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.__init__  s    ,.)))rP   c                     | j                             |           | j                             |                     t          |           d S rx   )r  r   r  r   )rH   rm   r   s     rN   r   zgUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.fileDescriptorReceived  sI    ###)001A1A"1E1EFFFb					rP   N)rQ   rR   rS   ro   r   )r   s   rN   FakeProtocolr     s=        / / /      rP   r  c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )PUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver   c                 "    || _         || _        d S rx   )r   protocol)rH   sktprotos      rN   ro   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.__init__  s    ! %rP   c                     d S rx   rU   r   s     rN   _dataReceivedz^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._dataReceived      rP   c                     d S rx   rU   ru   s    rN   r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getHost  r  rP   c                     d S rx   rU   ru   s    rN   r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getPeer  r  rP   c                     d S rx   rU   )rH   os     rN   _getLogPrefixz^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._getLogPrefix  r  rP   N)	rQ   rR   rS   
bufferSizero   r  r   r   r  rU   rP   rN   FakeReceiverr    sd        J& & &          rP   r  s   some data needs to be sentc                 &    g | ]} |          S rU   rU   ).0rm   r   s     rN   
<listcomp>zNUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.<listcomp>  s%    III 0 0 4 4IIIrP   N)r   r   twisted.internet.unixr   twisted.python.sendmsgr   r   r$   r,   r   r   
addCleanupr   r   r   doReadrF   lenr  assertFalsesetintersectionr  )rH   ancillaryPackerr   r   r   r  r  
sendSocket
recvSocketr  receiver	fileOneFDfileOneName	fileTwoFDfileTwoName
dataToSend	fdsToSend	ancillaryexpectedCountdeviceInodesSentr   s                       @rN   )_sendmsgMixinFileDescriptorReceivedDriverz:UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver  sO   : 	&%%%%%777777222222	* 	* 	* 
,	-	-	 	 	 	 	 	 	. 	 	 
.	-		 	 	 	 	= 	 	 	& ",G[!A!A
J
()))
()))<
E22 ")	;!(	;,,,,,,2
	*	#2?9#=#= 	=
J	222 	UY777 	Y44S^^DDEEE 9 	KIIIIyIII-u/IJJJJJ	K 	KrP   c                 H    ddl m f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                 <    t           t          dg| R  fg}d}||fS )Nii   r   r   r*  r+  r,  r1  s      rN   r!  z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG.<locals>.ancillaryPacker  s2    $j$t2Hi2H2H2HIJIMm++rP   Nr  r1  r.  rH   r!  r1  s     @rN   1test_multiFileDescriptorReceivedPerRecvmsgOneCMSGzBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG  sL     	655555	, 	, 	, 	, 	,
 	66GGGGGrP   z=Multi control message ancillary sendmsg not supported on Mac.c                 H    ddl m fd}|                     |           dS )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   r0  c                 ,    fd| D             }d}||fS )Nc                 @    g | ]}t           t          d |          fS )ir5  )r  rm   r1  s     rN   r  zpUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPacker.<locals>.<listcomp>  s(    UUUR*j$sB--@UUUrP   r4  rU   r6  s      rN   r!  z\UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPacker  s,    UUUU9UUUIMm++rP   Nr7  r8  s     @rN   2test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGszCUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs  sL     	655555	, 	, 	, 	, 	,
 	66GGGGGrP   c                 P   ddl m d }fd}g }t          |j                   |                     t
          |j                   |                     d|           |                     |           dt          f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                     g }d}||fS )Nr   rU   )r*  r+  r,  s      rN   r!  z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.ancillaryPacker0  s    IMm++rP   c                 @    d}dg}d}                     |||          S )Ns	   some data)NNrP   r   )ReceivedMessage)r
  argskwargsrn   r+  flagsr   s         rN   fakeRecvmsgUnsupportedAncillaryzkUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.fakeRecvmsgUnsupportedAncillary5  s-    D*+IE**4EBBBrP   recvmsgz#received unsupported ancillary datac              3   ,   K   | ]}|d          v V  dS )formatNrU   )r  eexpectedMessages     rN   	<genexpr>zUUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.<genexpr>D  s,      CCqOq{2CCCCCCrP   z+Expected message not found in logged eventsN)
twisted.pythonr   r9   r   r  r;   r   r.  anyr   )rH   r!  rF  eventsfoundrK  r   s        @@rN   1test_multiFileDescriptorReceivedPerRecvmsgBadCMSGzBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG!  s     	+*****	, 	, 	,
	C 	C 	C 	C 	C FM"""666

7I'FGGG66GGG @CCCCFCCCCCLMMMMMrP   c                    ddl m}  |            \  }}g }t          |j                   |                     t
          |j                    G d dt                    } ||                                d          }t                      }t          | ||| j
                   |                                 |                    d           |                     d|                    d                     d	}d
}	t          |j        |j        |	|          }
|D ]:}|
                                D ] \  }}||                    |          k    r n! dS ;|                     d|
dt+          |          d           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                       e Zd Zd ZdS )RUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddressesc                     | j                                         | _        | j                                         | _        t
                              |            d S rx   )rq   r   hostAddressr   peerAddressrk   rv   ru   s    rN   rv   zaUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddresses.connectionMade\  sH    #'>#9#9#;#; #'>#9#9#;#; "11$77777rP   Nr   rU   rP   rN   RecordEndpointAddressesrT  [  s#        8 8 8 8 8rP   rX  r   FrP   r  z%(protocolName)s (on %(hostAddress)r) does not provide IFileDescriptorReceiver; closing file descriptor received (from %(peerAddress)r).r,   )rV  rW  protocolNamerI  zExpected event (z) not found in logged events ()N)r   r   r9   r   r  r;   rk   r   r,   r0   r   r   setblockingrF   recvr   rW  rV  itemsgetr   r	   )rH   r   probeClientprobeServerrO  rX  rd   rh   rI  clsNameexpectedEventlogEventkvs                 rN    test_avoidLeakingFileDescriptorsz1UNIXTestsBuilder.test_avoidLeakingFileDescriptorsG  s    	&%%%%%#-:<< [FM"""666	8 	8 	8 	8 	8&8 	8 	8 	8 )(););)=)=wGG$&&ffdnEEE 	 	&&&k..t44555: 	
 ( ** 	
 
 
  	 	H%++--  1Q''E (   III "MM       rP   c                     t          t                     G  f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                   &    e Zd Zd Z fdZd ZdS )JUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEventsc                 <    t          j        |            g | _        d S rx   )r,   rv   rO  ru   s    rN   rv   zYUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.connectionMade  s    #24888 rP   c                                          t          |           | j                            t	          |                     d S rx   )r  r   rO  r   type)	innerSelfr   rH   s     rN   r   zaUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.fileDescriptorReceived  s<    z222 ''Z(8(899999rP   c                 :    | j                             |           d S rx   )rO  extendr   s     rN   r   zWUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.dataReceived  s    ""4(((((rP   N)rQ   rR   rS   rv   r   r   ru   s   rN   RecordEventsri    sL        ! ! !: : : : :) ) ) ) )rP   rp  r   r      N)r   r$   r,   r   rk   r   r0   r   rF   intrO  bytes)rH   rp  r   rd   rh   s   `    rN   #test_descriptorDeliveredBeforeBytesz4UNIXTestsBuilder.test_descriptorDeliveredBeforeBytes  s     
,	-	-
	) 
	) 
	) 
	) 
	) 
	) 
	). 
	) 
	) 
.	-
	) #ELLNNG<<ffdnEEEfmA.///%abb(9":":;;;;;rP   N)rQ   rR   rS   rT   r'   r]   r\   r   r   r   r=   isLinuxr   r   r   r   r   sendmsgSkipReasonr   r   r   r.  r9  isMacOSXr>  rQ  rf  rt  rU   rP   rN   r   r      s         'IE E E VHE 
C 
C	 
C< < < VHE O O	 OM M M, VK*++ F  F ,+ FD VK*++.U .U ,+.U` VK*++J J ,+J.aK aK aKF VK*++H H ,+H VG  VK*++H H ,+	 
H VK*++#N #N ,+#NJ VK*++J J ,+JX VK*++< < ,+< < <rP   r   c                   n    e Zd ZdZej        fZd Z e e	j
                     d          d             ZdS )UNIXDatagramTestsBuilderzE
    Builder defining tests relating to L{IReactorUNIXDatagram}.
    c                 p    |                      d|                                 t                                 dS )z
        The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
        is created with the mode specified.
        listenUNIXDatagramN)rO   r   r)   ru   s    rN   test_listenModez(UNIXDatagramTestsBuilder.test_listenMode  s0    
 	+T[[]]<L<N<NOOOOOrP   r   c                    t          |           }|                                 }|                    d|z   t                                }|                     |                                t          d|z                        dS )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)rZ   rC   r{  r)   rF   r   r   r   s       rN   r   z<UNIXDatagramTestsBuilder.test_listenOnLinuxAbstractNamespace  sr     T""##%%))$+7G7I7IJJTD[)A)ABBBBBrP   N)rQ   rR   rS   rT   r   IReactorUNIXDatagramr]   r|  r   r=   ru  r   rU   rP   rN   ry  ry    s          %9;
P P P VHE 	C 	C	 	C 	C 	CrP   ry  c                   V    e Zd ZU dZeefZeee	e
                           ed<   d Zd ZdS )SocketUNIXMixinzb
    Mixin which uses L{IReactorSocket.adoptStreamPort} to hand out listening
    UNIX ports.
    r]   c                    t          t                    }t          dd          }|                    |           |                    d           |                    d           	 |                    |                                |j        |          |	                                 S # |	                                 w xY w)zj
        Get a UNIX port from a reactor, wrapping an already-initialized file
        descriptor.
        r_   r`   ra      F)
r   r   r   r   listenr[  adoptStreamPortr   familyr   )rH   rL   rK   portSockrJ   s        rN   getListeningPortz SocketUNIXMixin.getListeningPort  s    
 '??W#...dU###	**8??+<+<howWWNNHNNs   &-B( (B>c                 8    |                     |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   rf   rH   rL   addressrK   s       rN   connectToListenerz!SocketUNIXMixin.connectToListener       ""7<999rP   N)rQ   rR   rS   rT   r'   r&   r]   r   r   r   r   ri   r  r  rU   rP   rN   r  r    sm           	?$y/!:;   
   : : : : :rP   r  c                       e Zd ZdZd Zd ZdS )ListenUNIXMixinzZ
    Mixin which uses L{IReactorTCP.listenUNIX} to hand out listening UNIX
    ports.
    c                 P    t          dd          }|                    ||          S )z0
        Get a UNIX port from a reactor
        r_   r`   ra   )r   r   )rH   rL   rK   rJ   s       rN   r  z ListenUNIXMixin.getListeningPort  s,    
 W#...!!$000rP   c                 8    |                     |j        |          S r  r  r  s       rN   r  z!ListenUNIXMixin.connectToListener  r  rP   N)rQ   rR   rS   rT   r  r  rU   rP   rN   r  r     s<         
1 1 1: : : : :rP   r  c                   P    e Zd ZU efZeeee                           e	d<   d Z
d ZdS )UNIXPortTestsMixinr]   c                 X    | dt          |                                j                  S )zZ
        Get the message expected to be logged when a UNIX port starts listening.
        z starting on r6   r   rf   )rH   r   rK   s      rN   #getExpectedStartListeningLogMessagez6UNIXPortTestsMixin.getExpectedStartListeningLogMessage#  s+     MMT\\^^5H(I(IMMMrP   c                 V    dt          |                                j                   dS )zJ
        Get the expected connection lost message for a UNIX port
        z(UNIX Port z Closed)r  )rH   r   s     rN   getExpectedConnectionLostLogMsgz2UNIXPortTestsMixin.getExpectedConnectionLostLogMsg)  s)     I\$,,..*=>>HHHHrP   N)rQ   rR   rS   r'   r]   r   r   r   r   ri   r  r  rU   rP   rN   r  r     s]         ?Ko$y/!:;MMMN N NI I I I IrP   r  c                       e Zd ZdZdS )UNIXPortTestsBuilderz.
    Tests for L{IReactorUNIX.listenUnix}
    NrQ   rR   rS   rT   rU   rP   rN   r  r  0             rP   r  c                       e Zd ZdZdS )UNIXFDPortTestsBuilderz3
    Tests for L{IReactorUNIX.adoptStreamPort}
    Nr  rU   rP   rN   r  r  ;  r  rP   r  c                   *    e Zd ZeeefZd Zd Zd Z	dS )%UNIXAdoptStreamConnectionTestsBuilderc                    |                                  }ddlm}  G 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                       e Zd Zd ZdS )XUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactoryc                     d S rx   rU   )rH   r  s     rN   buildProtocolzfUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactory.buildProtocol\  s    trP   N)rQ   rR   rS   r  rU   rP   rN   NoneFactoryr  [  s#            rP   r  FN)rC   r   r   r*   r   r   r[  r  r   r   adoptStreamConnectionassertIsNone)	rH   rL   r   r  s1s2s1FDrK   r   s	            rN   test_buildProtocolReturnsNonezCUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNoneM  s     ##%%%%%%%%	 	 	 	 	- 	 	 	 G[11B
u!!!!!!yy{{+--..tWgFF&!!!!!rP   c                       fd}                                  }                     |dd          }|                    |                                |           dS )z>
        Helper method to test UNIX server addresses.
        c                    | \  }}}	 t          d|                                j                  }                    d|j        j        d|dt          |j                                                 d|j        j        d||j        j                   |j        j	        d         }
                    |t                     |j                                         d S # |j                                         w xY w)Nr>   z<AccumulatingProtocol #z on >zAccumulatingProtocol,,r   )r8   r   rf   rF   rq   	sessionnostrlogstrrK   peerAddressesr   r   rt   )	protocolsrh   rd   r   portPathrW  rH   s         rN   	connectedzOUNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIX.<locals>.connectedn  s   #, FFD26r4<<>>;NOO   '111888=())      '11188=$+   %n:1=%%k;???  //11111 //1111s   B=C! !C<N)	interfaceaddressFamily)rC   getConnectedClientAndServerr   
runReactor)rH   r  rL   r   s   `   rN   test_ServerAddressUNIXz<UNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIXi  s|    
	2 	2 	2 	2 	2, ##%%,,t4 - 
 
 	
i        rP   c                 $   t                      }t                      |_        t                      t                      _        t                      _        t	                      }t                      |_        t                      |_        t          dd          }                    ||          fd}|j                            |           t          |j        j        g          }fd}	|	                    |	           t                      
                    |	           t          |j        j        g          }
f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                                          | j                                       | j                                       | j                                        t
                     d S rx   )removeReaderrq   removeWriterr  r   r   )r  rL   rd   s    rN   firstServerConnectedz_UNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.firstServerConnected  s[      111  111))%/*@*@*B*BGVTTTTTrP   c                 >    j         r                                 | S rx   )runningr   )r   rL   s    rN   r   zOUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.stop  s     MrP   c                 B    | \  }}                     ||f           d S rx   )r   )r  rh   rd   deferredr   s      rN   r   zPUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.start  s-    &NFFvvt455555rP   )r3   r   protocolConnectionMadeprotocolConnectionLostr2   r   r   r   r   r   r   r   r   rf   )rH   rL   r  r  firstServerrh   rJ   r  lostDeferredr   startDeferredr   r  r   rd   s    `          @@@rN   r  zAUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer  s    &''-5ZZ* ""(0

%(0

% ""(0

%(0

% W#...!!$44	U 	U 	U 	U 	U 	U
 	*667KLLL$*F,IJ
 
	 	 	 	 	
 	T"""::D!!!%*F,IJ
 
	6 	6 	6 	6 	6 	6 	!!%(((DLLNN/888rP   N)
rQ   rR   rS   r%   r&   r'   r]   r  r  r  rU   rP   rN   r  r  F  sS        " " "8 !  !  !D6 6 6 6 6rP   r  c                   >    e Zd ZdZefZdZed             Zd Z	d Z
dS )UnixClientTestsBuilderz7
    Define tests for L{IReactorUNIX.connectUNIX}.
    Nc                 F    | j         t          |           | _         | j         S )z
        Return a path usable by C{connectUNIX} and C{listenUNIX}.

        @return: A path instance, built with C{_abstractPath}.
        )_pathrZ   ru   s    rN   rJ   zUnixClientTestsBuilder.path  s#     :&t,,DJzrP   c                 8    |                     | 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   rJ   rH   rL   rK   s      rN   r  zUnixClientTestsBuilder.listen  s     !!$)W555rP   c                 8    |                     | j        |          S )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   rJ   r  s      rN   connectzUnixClientTestsBuilder.connect  s     ""49g666rP   )rQ   rR   rS   rT   r'   r]   r  propertyrJ   r  r  rU   rP   rN   r  r    sd          'E  X
6 
6 
6
7 
7 
7 
7 
7rP   r  )jrT   hashlibr   osr   r   r   r   r   pprintr	   r   r
   r   r   r   structr   tempfiler   r   typingr   r   r   unittestr   r   _AF_UNIXImportErrorzope.interfacer   r   twisted.internetr   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.connectionmixinsr,   r-   r.   r/   r0   #twisted.internet.test.reactormixinsr1   twisted.internet.test.test_tcpr2   r3   r4   r5   twisted.python.compatr6   twisted.python.failurer7   twisted.python.filepathr8   twisted.python.logr9   r:   r;   twisted.python.reflectr<   twisted.python.runtimer=   r   rv  r@   rZ   r\   rk   r}   r   ry  r  r  r  r  r  r  globalsupdatemakeTestCaseClassesr  rU   rP   rN   <module>r     s   
       2 2 2 2 2 2 2 2 2 2 2 2 2 2       ; ; ; ; ; ; ; ; ; ; ; ;             $ $ $ $ $ $ $ $ + + + + + + + + + +      ****** GG    GGG
 2 1 1 1 1 1 1 1 - - - - - - - - 0 0 0 0 0 0 @ @ @ @ @ @ @ @ @ @ M M M M M M M M         
            U T T T T T T T T T - - - - - -              ? > > > > >            / . . . . . * * * * * * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 0 0 0 0 0 + + + + + +
-0
1
1 =)**6K 
< < < < < < < <") ) )? ? ? ? ?/ ? ? ?," " " " ", " " "J $%%; ; ; ; ;/ ; ; &%;|S< S< S< S< S<8L S< S< S<lC C C C C C C CB*: *: *: *: *: *: *: *:Z: : : : : : : :@I I I I I I I I     	      	  { { { { {,C^ { { {| 		  !5577 8 8 8 		  )==?? @ @ @ 		  %99;; < < < 		  ';;== > > > 		  6JJLL M M M*7 *7 *7 *7 *7^-C *7 *7 *7Z 		  ';;== > > > > >s   A A! A!