
    Vh                     ~   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 	 ddlmZ 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"Zc eZd!      d#Zc G d$ d%      Zdd& Ze G d' d(eD      Zf G d) d*eB      Zg e"e7       G d+ d,eB             Zh G d- d.edeHeC      Zi G d/ d0edeH      Zj G d1 d2      Zk G d3 d4      Zl G d5 d6      Zm G d7 d8elemeHeM      Zn G d9 d:ekemeHeM      Zo G d; d<eNeH      Zp eq       j                  eij                                 eq       j                  ejj                                 eq       j                  enj                                 eq       j                  eoj                                 eq       j                  epj                                 G d= d>eHeE      Zt eq       j                  etj                                 ee]d?k7  d@       G dA dBea             Zuy# e$ r dZY aw xY w)Cz/
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                       e Zd ZdZd Zy)UNIXFamilyMixinzK
    Test-helper defining mixin for things related to AF_UNIX sockets.
    c                     d}| j                         } t        ||      |||      }|j                          | j                  t	        t        |      j                        |       y)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factoryrK   r   unixPorts          O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_unix.py	_modeTestzUNIXFamilyMixin._modeTestR   sY    
 ##%/77J/gDI d!3!34d;    N)__name__
__module____qualname____doc__rW    rX   rV   rI   rI   M   s    	<rX   rI   c                 F    t        t        d            j                         S )zI
    Return a new, unique abstract namespace path to be listened on.
    d   )r   r   	hexdigest)cases    rV   _abstractPathrb   ^   s     ws|&&((rX   c                   T    e Zd ZU dZej
                  fZeee	e
         ed<   d Zd Zy)UNIXCreatorz(
    Create UNIX socket end points.
    requiredInterfacesc                 4    t        dd      }t        ||      S )z3
        Construct a UNIX server endpoint.
        .sock.suffixdir)r   r%   )rQ   r   rS   s      rV   serverzUNIXCreator.serverl   s    
 W#.!'400rX   c                 .    t        ||j                        S )z3
        Construct a UNIX client endpoint.
        )r$   name)rQ   r   serverAddresss      rV   clientzUNIXCreator.clientt   s     "'=+=+=>>rX   N)rY   rZ   r[   r\   r   r,   re   r   r   r   r   __annotations__rl   rp   r]   rX   rV   rd   rd   e   s7     @J?V?V>X$y/!:;X1?rX   rd   c                   &    e Zd ZdZdZd Zd Zd Zy)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                      || _         || _        y)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)rQ   ru   rv   s      rV   __init__zSendFileDescriptor.__init__   s     	rX   c                     | j                   j                  | j                         | j                  r%| j                   j	                  | j                         | j                   j                          y)zn
        Send C{self.fd} and, if it is not L{None}, C{self.data}.  Then close the
        connection.
        N)	transportsendFileDescriptorru   rv   writeloseConnectionrQ   s    rV   connectionMadez!SendFileDescriptor.connectionMade   sG    
 	))$''299NN  +%%'rX   c                 >    t        j                  | |       || _        y N)r1   connectionLostreasonrQ   r   s     rV   r   z!SendFileDescriptor.connectionLost   s    **48rX   )rY   rZ   r[   r\   r   rw   r~   r   r]   rX   rV   rs   rs   {   s     F	(rX   rs   c                   0    e Zd ZdZdZdZd Zd Zd Zd Z	y)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                 z    | 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.
        )r   r!   waitingr"   r}   s    rV   waitForDescriptorz'ReceiveFileDescriptor.waitForDescriptor   s0     ;;#:DL<<$$rX   c                 H    | j                   j                  |       d| _         y)z
        Fire the waiting Deferred, initialized by C{waitForDescriptor}, with the
        file descriptor just received.
        N)r   callback)rQ   
descriptors     rV   fileDescriptorReceivedz,ReceiveFileDescriptor.fileDescriptorReceived   s    
 	j)rX   c           	          | j                   9| j                   j                  t        t        d|d                   d| _         yy)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   errbackr=   	ExceptionrQ   rv   s     rV   dataReceivedz"ReceiveFileDescriptor.dataReceived   sF     <<#LL  	$4TH<P"QRS  DL	 $rX   c                     t        j                  | |       | j                  "| j                  j                  |       d| _        || _        y)zj
        Fail the waiting Deferred, initialized by C{waitForDescriptor}, if there
        is one.
        N)r1   r   r   r   r   r   s     rV   r   z$ReceiveFileDescriptor.connectionLost   s>    
 	**48<<#LL  (DLrX   )
rY   rZ   r[   r\   r   r   r   r   r   r   r]   rX   rV   r   r      s'    
 FG
% 	rX   r   c                      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y)UNIXTestsBuilderz=
    Builder defining tests relating to L{IReactorUNIX}.
    c                 V    | j                  d| j                         t                      y)zs
        The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with
        the mode specified.
        
listenUNIXN)rW   r   r/   r}   s    rV   	test_modezUNIXTestsBuilder.test_mode   s    
 	|T[[]MODrX   8Abstract namespace UNIX sockets only supported on Linux.c                     t        |       }| j                         } |j                  d|z   t                     }| j	                  |j                         t        d|z                y)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)rb   rL   r   r/   rO   getHostr    rQ   rS   r   ports       rV   #test_listenOnLinuxAbstractNamespacez4UNIXTestsBuilder.test_listenOnLinuxAbstractNamespace   sU     T"##%!w!!$+}?TD[)ABrX   c                     d }| j                  t        j                  d|       | j                         }| j	                  t
              5   |j                  dt                      ddd       y# 1 sw Y   yxY w)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)OSErrorr}   s    rV   raiseSocketErrorz=UNIXTestsBuilder.test_listenFailure.<locals>.raiseSocketError  s    <==rX   createInternetSocketznot-usedN)patchr   BasePortrL   assertRaisesr&   r   r/   )rQ   r   r   s      rV   test_listenFailurez#UNIXTestsBuilder.test_listenFailure   se    	> 	

4=="8:JK##%01 	<Gz=?;	< 	< 	<s   
A//A8c                     t        |       }| j                         } |j                  d|z   t                     }| j	                  |j                         t        d|z                y)zc
        L{IReactorUNIX.connectUNIX} also accepts a Linux abstract namespace
        path.
        r   N)rb   rL   connectUNIXr-   rO   getDestinationr    )rQ   rS   r   	connectors       rV   $test_connectToLinuxAbstractNamespacez5UNIXTestsBuilder.test_connectToLinuxAbstractNamespace  sX     T"##%'G''t]_E	113[5MNrX   c                     G d dt               } |       } |       }t        | ||| j                         | j                  |j                  d   |j                  d          | j                  |j                  d   |j                  d          y)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y)4UNIXTestsBuilder.test_addresses.<locals>.SaveAddressc                     t        |j                         |j                               | _        |j	                          y )N)hostpeer)dictr   getPeer	addressesr|   )rQ   ry   s     rV   makeConnectionzCUNIXTestsBuilder.test_addresses.<locals>.SaveAddress.makeConnection#  s3    !%"**,93D3D3F" ((*rX   N)rY   rZ   r[   r   r]   rX   rV   SaveAddressr   "  s    +rX   r   r   r   N)r1   r5   	endpointsrO   r   )rQ   r   rl   rp   s       rV   test_addresseszUNIXTestsBuilder.test_addresses  s}    	+- 	+ ffdnnE))&163C3CF3KL))&163C3CF3KLrX   c                 j    ddl m t               j                  d       t        j	                         d      t               }|j                         } fd}|j                  |       |j                  t        d       |j                  fd       t         | j                         y)	z
        L{IUNIXTransport.sendFileDescriptor} accepts an integer file descriptor
        and sends a copy of it to the process reading from the connection.
        r   )fromfd)rG   r      junkc                      | t         t              }t        |        j                  j	                         |j	                                j                  j                         |j                                y r   )r
   r   r   rO   getsocknameassertNotEqualfileno)r   receivedr   srQ   s     rV   checkDescriptorzAUNIXTestsBuilder.test_sendFileDescriptor.<locals>.checkDescriptor@  sY    j';?H* Q]]_h.B.B.DE
 
HOO,=>rX   z-Sending file descriptor encountered a problemc                 8    j                   j                         S r   ry   r|   ignoredrl   s    rV   <lambda>z:UNIXTestsBuilder.test_sendFileDescriptor.<locals>.<lambda>P      &"2"2"A"A"C rX   N)r   r   bindrs   r   r   r   addCallback
addErrbackr@   addBothr5   r   )rQ   rp   dr   r   r   rl   s   `   @@@rV   test_sendFileDescriptorz(UNIXTestsBuilder.test_sendFileDescriptor1  s     	"H	w#AHHJ8&($$&	? 	
o&	SIJ			CDffdnnErX   c                      G d dt               } G d dt               } |       } |       }||_        t        | ||| j                         | j	                  |j
                  d       y)z
        If a L{IUNIXTransport.sendFileDescriptor} call fills up
        the send buffer, any registered producer is paused.
        c                       e Zd Zd Zy)SUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotReadc                 8    | j                   j                          y r   )ry   pauseProducingr}   s    rV   r~   zbUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.DoesNotRead.connectionMade\  s    --/rX   NrY   rZ   r[   r~   r]   rX   rV   DoesNotReadr   [  s    0rX   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)`UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptorsFc                 .    t                _          j                  j                   d        fd}t        |       _         j                  j
                   j                  _         j                  j                  d      j                  t        d       y )NTc                       j                   j                   j                  j                                 j                   j	                  d       y )N   x)ry   rz   r   r   r{   r}   s   rV   senderzUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMade.<locals>.senderf  s4    NN55dkk6H6H6JKNN((.rX   r   zSend loop failure)
r   ry   registerProducerr0   taskr   clockstartr   r@   )rQ   r   s   ` rV   r~   zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.connectionMadeb  sf    $h//d;/ (/	"&.."8"8				"--c3FGrX   c                 $    | j                          y r   _disconnectr}   s    rV   stopProducingznUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.stopProducingn        "rX   c                 $    | j                          y r   r   r}   s    rV   resumeProducingzpUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.resumeProducingq  r   rX   c                 f    d| _         | j                  j                          | j                          y )NT)pausedry   unregisterProducerr   r}   s    rV   r   zoUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors.pauseProducingt  s%    "113  "rX   c                     | j                   j                          | j                  j                          | j                  j                  j                          y r   )r   stopry   abortConnectionotherr}   s    rV   r   zlUNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducing.<locals>.SendsManyFileDescriptors._disconnecty  s6    		 ..0

$$446rX   N)	rY   rZ   r[   r   r~   r   r   r   r   r]   rX   rV   SendsManyFileDescriptorsr   _  s!    F
H###
7rX   r   z*sendFileDescriptor producer was not pausedN)r1   r   r5   r   
assertTruer   )rQ   r   r   rl   rp   s        rV   -test_sendFileDescriptorTriggersPauseProducingz>UNIXTestsBuilder.test_sendFileDescriptorTriggersPauseProducingT  sY    	0- 	0	7': 	7> *+ffdnnE'STrX   c                    t               }t        |j                         d      t               }g }|j	                         }|j                  |j                         |j                  fd       t        | || j                         | j                  |d   t               |d   j                  t               | j                  j                  j                  t               y)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                 8    j                   j                         S r   r   r   s    rV   r   z=UNIXTestsBuilder.test_fileDescriptorOverrun.<locals>.<lambda>  r   rX   r   )r   rs   r   r   r   r   appendr5   r   assertIsInstancer=   trapr'   r   valuer(   )rQ   cargorp   resultr   rl   s        @rV   test_fileDescriptorOverrunz+UNIXTestsBuilder.test_fileDescriptorOverrun  s     #ELLND9&($$&			&-- 			CDffdnnEfQi1q	'(fmm113HIrX   c                 j   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                         | j                  |j                          |       }	 |||	      }
t               \  }}t               \  }}| j                  t        |       | j                  t        |       d
}||g} ||      \  }} ||||       |
j                          | j!                  t#        |	j$                        |       | j'                  t)        |      j+                  t)        |	j$                                     |	j$                  r2|D cg c]
  } |       }}| j!                  ||	j,                         yyc c}w )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                 H    t        |       }|j                  |j                  fS r   )r   st_devst_ino)ru   fss     rV   deviceInodeTuplezTUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.deviceInodeTuple  s    rBIIryy))rX   c                       e Zd Zd Z fdZy)PUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocolc                      g | _         g | _        y r   )fdsdeviceInodesReceivedr}   s    rV   rw   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.__init__  s    ,.)rX   c                     | j                   j                  |       | j                  j                   |             t        |       y r   )r	  r   r
  r   )rQ   ru   r  s     rV   r   zgUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeProtocol.fileDescriptorReceived  s4    #))001A"1EFb	rX   N)rY   rZ   r[   rw   r   )r  s   rV   FakeProtocolr    s    /rX   r  c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)PUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver   c                      || _         || _        y r   )r   protocol)rQ   sktprotos      rV   rw   zYUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.__init__  s    ! %rX   c                      y r   r]   r   s     rV   _dataReceivedz^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._dataReceived      rX   c                      y r   r]   r}   s    rV   r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getHost  r  rX   c                      y r   r]   r}   s    rV   r   zXUNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver.getPeer  r  rX   c                      y r   r]   )rQ   os     rV   _getLogPrefixz^UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver.<locals>.FakeReceiver._getLogPrefix  r  rX   N)	rY   rZ   r[   
bufferSizerw   r  r   r   r  r]   rX   rV   FakeReceiverr    s     J&rX   r  s   some data needs to be sentN)r   r   twisted.internet.unixr   twisted.python.sendmsgr   r   r)   r1   r   r   
addCleanupr   r   r   doReadrO   lenr	  assertFalsesetintersectionr
  )rQ   ancillaryPackerr   r   r   r  r  
sendSocket
recvSocketr  receiver	fileOneFDfileOneName	fileTwoFDfileTwoName
dataToSend	fdsToSend	ancillaryexpectedCountru   deviceInodesSentr  s                        @rV   )_sendmsgMixinFileDescriptorReceivedDriverz:UNIXTestsBuilder._sendmsgMixinFileDescriptorReceivedDriver  sx   : 	&72	* 
,	-	. 	 
.		= 	& ",G[!A
J
(()
(()
E2 ")	;!(	;,,2
	*	#29#= 	=
J	2 	UYY7 	Y44S^DE 99?HI 0 4II-u/I/IJ Is   F0c                 >    ddl m fd}| j                  |       y)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                 8    t         t        dg|  fg}d}||fS )Nii   r   r   )r/  r0  r1  r6  s      rV   r&  z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG.<locals>.ancillaryPacker	  s,    $j$t2Hi2HIJIMm++rX   Nr  r6  r3  rQ   r&  r6  s     @rV   1test_multiFileDescriptorReceivedPerRecvmsgOneCMSGzBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgOneCMSG   s     	6	,
 	66GrX   z=Multi control message ancillary sendmsg not supported on Mac.c                 >    ddl m fd}| j                  |       y)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   r5  c           	      Z    | D cg c]  }t         t        d|      f }}d}||fS c c}w )Nir9  r:  )r/  ru   r0  r1  r6  s       rV   r&  z\UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs.<locals>.ancillaryPacker  s9    KTUR*j$sB-@UIUMm++ Vs   (Nr;  r<  s     @rV   2test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGszCUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgTwoCMSGs  s     	6	,
 	66GrX   c                 *   ddl m d }fd}g }t        |j                         | j	                  t
        |j                         | j                  d|       | j                  |       dt        fd|D              }| j                  |d       y	)
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   r]   )r/  r0  r1  s      rV   r&  z[UNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.ancillaryPacker3  s    IMm++rX   c                 8    d}dg}d}j                  |||      S )Ns	   some data)NNrX   r   )ReceivedMessage)r  argskwargsrv   r0  flagsr   s         rV   fakeRecvmsgUnsupportedAncillaryzkUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.fakeRecvmsgUnsupportedAncillary8  s*    D*+IE**4EBBrX   recvmsgz#received unsupported ancillary datac              3   ,   K   | ]  }|d    v   yw)formatNr]   ).0eexpectedMessages     rV   	<genexpr>zUUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG.<locals>.<genexpr>G  s     CqOq{2Cs   z+Expected message not found in logged eventsN)
twisted.pythonr   r?   r   r   rA   r   r3  anyr   )rQ   r&  rI  eventsfoundrO  r   s        @@rV   1test_multiFileDescriptorReceivedPerRecvmsgBadCMSGzBUNIXTestsBuilder.test_multiFileDescriptorReceivedPerRecvmsgBadCMSG$  s~     	+	,
	C FMM"6

7I'FG66G @CFCCLMrX   c                    ddl m}  |       \  }}g }t        |j                         | j	                  t
        |j                          G d dt              } ||j                         d      }t               }t        | ||| j                         |j                          |j                  d       | j                  d|j                  d             d	}d
}	t        |j                   |j"                  |	|      }
|D ]1  }|
j%                         D ]  \  }}||j'                  |      k7  s 0  y | j)                  d|
dt+        |      d       y)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y)RUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddressesc                     | j                   j                         | _        | j                   j                         | _        t
        j                  |        y r   )ry   r   hostAddressr   peerAddressrs   r~   r}   s    rV   r~   zaUNIXTestsBuilder.test_avoidLeakingFileDescriptors.<locals>.RecordEndpointAddresses.connectionMade_  s:    #'>>#9#9#; #'>>#9#9#; "11$7rX   Nr   r]   rX   rV   RecordEndpointAddressesrX  ^  s    8rX   r\  r   FrX   r  z%(protocolName)s (on %(hostAddress)r) does not provide IFileDescriptorReceiver; closing file descriptor received (from %(peerAddress)r).r1   )rZ  r[  protocolNamerL  zExpected event (z) not found in logged events ()N)r   r   r?   r   r   rA   rs   r   r1   r5   r   r   setblockingrO   recvr   r[  rZ  itemsgetr"   r	   )rQ   r   probeClientprobeServerrS  r\  rl   rp   rL  clsNameexpectedEventlogEventkvs                 rV    test_avoidLeakingFileDescriptorsz1UNIXTestsBuilder.test_avoidLeakingFileDescriptorsJ  sJ    	&#-< [FMM"6	8&8 	8 )););)=wG$&ffdnnE 	 	&k..t45: 	
 ( **** 	
  	H%++- 1Q'
 	 II "rX   c                 h    t        t               G  fddt                     }t               }t	        |j                         d      } |       }t         || j                          j                  t        |j                  d           j                  dt        |j                  dd              y)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y)JUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEventsc                 <    t        j                  |        g | _        y r   )r1   r~   rS  r}   s    rV   r~   zYUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.connectionMade  s    #2248 rX   c                 z    j                  t        |       | j                  j                  t	        |             y r   )r   r   rS  r   type)	innerSelfr   rQ   s     rV   r   zaUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.fileDescriptorReceived  s*    z2  ''Z(89rX   c                 :    | j                   j                  |       y r   )rS  extendr   s     rV   r   zWUNIXTestsBuilder.test_descriptorDeliveredBeforeBytes.<locals>.RecordEvents.dataReceived  s    ""4(rX   N)rY   rZ   r[   r~   r   r   r}   s   rV   RecordEventsrm    s    !:)rX   rt  r   r      N)r   r)   r1   r   rs   r   r5   r   rO   intrS  bytes)rQ   rt  r   rl   rp   s   `    rV   #test_descriptorDeliveredBeforeBytesz4UNIXTestsBuilder.test_descriptorDeliveredBeforeBytes  s     
,	-
	). 
	) 
.
	) #ELLNG<ffdnnEfmmA./%ab(9":;rX   N)rY   rZ   r[   r\   r,   re   rd   r   r   r   rC   isLinuxr   r   r   r   r   sendmsgSkipReasonr   r   r   r3  r=  isMacOSXrA  rU  rj  rx  r]   rX   rV   r   r      s    'IE HE
C	
C< HEO	OM, K*+ F , FD K*+.U ,.U` K*+J ,J.aKF K*+H ,H G K*+H ,	
H K*+#N ,#NJ K*+J ,JX K*+< ,<rX   r   c                   n    e Zd ZdZej
                  fZd Z e e	j                          d      d        Zy)UNIXDatagramTestsBuilderzE
    Builder defining tests relating to L{IReactorUNIXDatagram}.
    c                 V    | j                  d| j                         t                      y)z
        The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
        is created with the mode specified.
        listenUNIXDatagramN)rW   r   r.   r}   s    rV   test_listenModez(UNIXDatagramTestsBuilder.test_listenMode  s    
 	+T[[]<L<NOrX   r   c                     t        |       }| j                         } |j                  d|z   t                     }| j	                  |j                         t        d|z                y)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)rb   rL   r  r.   rO   r   r    r   s       rV   r   z<UNIXDatagramTestsBuilder.test_listenOnLinuxAbstractNamespace  sW     T"##%)w))$+7G7IJTD[)ABrX   N)rY   rZ   r[   r\   r   IReactorUNIXDatagramre   r  r   rC   ry  r   r]   rX   rV   r}  r}    sM     %99;
P HE	C		CrX   r}  c                   B    e Zd ZU dZeefZeee	e
         ed<   d Zd Zy)SocketUNIXMixinzb
    Mixin which uses L{IReactorSocket.adoptStreamPort} to hand out listening
    UNIX ports.
    re   c                 D   t        t              }t        dd      }|j                  |       |j	                  d       |j                  d       	  |j                  |j                         |j                  |      |j                          S # |j                          w xY w)zj
        Get a UNIX port from a reactor, wrapping an already-initialized file
        descriptor.
        rg   rh   ri      F)
r   r   r   r   listenr_  adoptStreamPortr   familyr   )rQ   r   rT   portSockrS   s        rV   getListeningPortz SocketUNIXMixin.getListeningPort  sx    
 '?W#.dU#	*7**8??+<hoowWNNHNNs   +B Bc                 <     |j                   |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   rn   rQ   r   addressrT   s       rV   connectToListenerz!SocketUNIXMixin.connectToListener       #w""7<<99rX   N)rY   rZ   r[   r\   r,   r+   re   r   r   r   r   rq   r  r  r]   rX   rV   r  r    s6     	?$y/!:; 
 :rX   r  c                       e Zd ZdZd Zd Zy)ListenUNIXMixinzZ
    Mixin which uses L{IReactorTCP.listenUNIX} to hand out listening UNIX
    ports.
    c                 B    t        dd      } |j                  ||      S )z0
        Get a UNIX port from a reactor
        rg   rh   ri   )r   r   )rQ   r   rT   rS   s       rV   r  z ListenUNIXMixin.getListeningPort	  s%    
 W#.!w!!$00rX   c                 <     |j                   |j                  |      S r  r  r  s       rV   r  z!ListenUNIXMixin.connectToListener  r  rX   N)rY   rZ   r[   r\   r  r  r]   rX   rV   r  r    s    
1:rX   r  c                   <    e Zd ZU efZeeee         e	d<   d Z
d Zy)UNIXPortTestsMixinre   c                 R    | dt        |j                         j                        S )zZ
        Get the message expected to be logged when a UNIX port starts listening.
        z starting on r<   r   rn   )rQ   r   rT   s      rV   #getExpectedStartListeningLogMessagez6UNIXPortTestsMixin.getExpectedStartListeningLogMessage&  s'     -T\\^5H5H(I'LMMrX   c                 P    dt        |j                         j                         dS )zJ
        Get the expected connection lost message for a UNIX port
        z(UNIX Port z Closed)r  )rQ   r   s     rV   getExpectedConnectionLostLogMsgz2UNIXPortTestsMixin.getExpectedConnectionLostLogMsg,  s%     \$,,.*=*=>?xHHrX   N)rY   rZ   r[   r,   re   r   r   r   r   rq   r  r  r]   rX   rV   r  r  #  s)    ?Ko$y/!:;MNIrX   r  c                       e Zd ZdZy)UNIXPortTestsBuilderz.
    Tests for L{IReactorUNIX.listenUnix}
    NrY   rZ   r[   r\   r]   rX   rV   r  r  3      rX   r  c                       e Zd ZdZy)UNIXFDPortTestsBuilderz3
    Tests for L{IReactorUNIX.adoptStreamPort}
    Nr  r]   rX   rV   r  r  >  r  rX   r  c                   (    e Zd ZeeefZd Zd Zd Z	y)%UNIXAdoptStreamConnectionTestsBuilderc                    | j                         }ddlm}  G d dt              } |t        t
              \  }}|j                  d       | j                  |j                         | j                  |j                         |j                         } |       } |j                  |t        |      }| j                  |       y)z
        {IReactorSocket.adoptStreamConnection} returns None if the given
        factory's buildProtocol returns None.
        r   r   c                       e Zd Zd Zy)XUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactoryc                      y r   r]   )rQ   r  s     rV   buildProtocolzfUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNone.<locals>.NoneFactory.buildProtocol_  s    rX   N)rY   rZ   r[   r  r]   rX   rV   NoneFactoryr  ^  s    rX   r  FN)rL   r   r   r/   r   r   r_  r   r   r   adoptStreamConnectionassertIsNone)	rQ   r   r   r  s1s2s1FDrT   r   s	            rV   test_buildProtocolReturnsNonezCUNIXAdoptStreamConnectionTestsBuilder.test_buildProtocolReturnsNoneP  s     ##%%	- 	 G[1B
u!!yy{-...tWgF&!rX   c                       fd} j                         } j                  |dd      }|j                  |        j                  |       y)z>
        Helper method to test UNIX server addresses.
        c                 ,   | \  }}}	 t        d|j                         j                        }j                  d|j                  j
                  d|dt        |j                               j                  d|j                  j
                  d||j                  j                         |j                  j                  d   }j                  |t               |j                  j                          y # |j                  j                          w xY w)NrG   z<AccumulatingProtocol #z on >zAccumulatingProtocol,,r   )r>   r   rn   rO   ry   	sessionnostrlogstrrT   peerAddressesr   r    r|   )	protocolsrp   rl   r   portPathr[  rQ   s         rV   	connectedzOUNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIX.<locals>.connectedq  s    #, FFD26r4<<>;N;NO  ''118=(()   ''118=$$++ %nn::1=%%k;?   //1  //1s   CC7 7DN)	interfaceaddressFamily)rL   getConnectedClientAndServerr   
runReactor)rQ   r  r   r   s   `   rV   test_ServerAddressUNIXz<UNIXAdoptStreamConnectionTestsBuilder.test_ServerAddressUNIXl  sN    
	2, ##%,,t4 - 
 	
i  rX   c                    t               }t               |_        t               t               _        t               _        t	               }t               |_        t               |_        t        dd      } j                  ||      fd}|j                  j                  |       t        |j                  j                  g      }fd}	|j                  |	       t               j                  |	       t        |j                  j                  g      }
fd}|
j                  |        j                  j                         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.
        rg   rh   ri   c                      j                   | j                          j                  | j                          j                  | j                  j	                         t
               y r   )removeReaderry   removeWriterr  r   r   )r  r   rl   s    rV   firstServerConnectedz_UNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.firstServerConnected  sL     G  1 G  1)G))%//*@*@*BGVTrX   c                 B    j                   r j                          | S r   )runningr   )r   r   s    rV   r   zOUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.stop  s    MrX   c                 8    | \  }}j                  ||f       y r   )r   )r  rp   rl   deferredr   s      rV   r   zPUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer.<locals>.start  s!    &NFFvvt45rX   )r9   r!   protocolConnectionMadeprotocolConnectionLostr8   r   r   r   r#   r   r   r   r   rn   )rQ   r   r  r  firstServerrp   rS   r  lostDeferredr   startDeferredr   r  r   rl   s    `          @@@rV   r  zAUNIXAdoptStreamConnectionTestsBuilder.getConnectedClientAndServer  s,    &'-5Z* "(0
%(0
% "(0
%(0
% W#.!w!!$4	U
 	**667KL$**F,I,IJ
	
 	T":D!%**F,I,IJ
	6 	!!%(DLLN//8rX   N)
rY   rZ   r[   r*   r+   r,   re   r  r  r  r]   rX   rV   r  r  I  s#    "8 !D6rX   r  c                   6    e Zd ZdZefZdZed        Zd Z	d Z
y)UnixClientTestsBuilderz7
    Define tests for L{IReactorUNIX.connectUNIX}.
    Nc                 R    | j                   t        |       | _         | j                   S )z
        Return a path usable by C{connectUNIX} and C{listenUNIX}.

        @return: A path instance, built with C{_abstractPath}.
        )_pathrb   r}   s    rV   rS   zUnixClientTestsBuilder.path  s$     ::&t,DJzzrX   c                 <     |j                   | 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   rS   rQ   r   rT   s      rV   r  zUnixClientTestsBuilder.listen  s     "w!!$))W55rX   c                 <     |j                   | 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   rS   r  s      rV   connectzUnixClientTestsBuilder.connect  s     #w""499g66rX   )rY   rZ   r[   r\   r,   re   r  propertyrS   r  r  r]   rX   rV   r  r    s2     'E 
6
7rX   r  posixz%UNIX sockets only supported on POSIX.c                   J    e Zd ZdZdZd Zdeeegdf   ddfdZd Z	d	 Z
d
 Zy)RealSocketTestsz,
    Test real UNIX socket connections.
    unixpathc                   K   t         j                  j                  t              sJ t	               }t        j
                  | j                  |      }t        |t        j                        sJ ||_
        | j                  |j                         t               }t        j                  | j                  |       |j                   d{    |j                   sJ | j#                  |j                   j$                         | j#                  |j&                         |j(                  j+                  t,        j.                         y7 |w)z|
        A port created with L{IReactorTCP.listenUNIX} can be connected to with
        L{IReactorTCP.connectUNIX}.
        N)r   r,   
providedByr   r7   r   rS   
isinstancer   Portr   r   cleanUpr8   r   r  lastProtocolr   madedisconnected
lostReasonr   r   ConnectionDone)rQ   fr   clientFs       rV   test_closePortInProtocolFactoryz/RealSocketTests.test_closePortInProtocolFactory  s     
 &&11':::!!$))Q/$)))		"!#DIIw/####,,112))* 4 45 	s   CE	E
A=Er   Nreturnc                 ^  K   t         j                  j                  t              sJ t	               }t               }||_        t        j                  | j                  |      }| j                  |j                         t               }t               }||_        t        j                  | j                  |       t        ||g       d{   \  }} |||       |j                  sJ |j                  sJ |j                  j                          |j                  j                          y7 aw)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   r9   r!   r  r   rS   r   rN   r8   r   r#   ry   r|   )	rQ   r   serverFactoryserverConnMader   r  clientConnMadeserverProtocolclientProtocols	            rV   _connectedClientAndServerTestz-RealSocketTests._connectedClientAndServerTest  s      &&11':::')9A/=,!!$))];**+!#(0
)7&DIIw//<^,0
 *
& 	0''''''''  //1  //1*
s   CD-	D+
A"D-c                 l    K   dt         dt         ddf fd} j                  |       d{    y7 w)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                    | |fD ]  }t         j                  j                  |j                        sJ |j                  }j	                  |j                         d       |j                  d       j	                  |j                         d       |j                  d       j	                  |j                         d        y )Nr   TF)r   ITCPTransportr  ry   rO   getTcpNoDelaysetTcpNoDelayr  r  pry   rQ   s       rV   checkz.RealSocketTests.test_tcpNoDelay.<locals>.checkA  s     %n5 ?!//::1;;GGGKK	  !8!8!:A>''-  !8!8!:A>''.  !8!8!:A>?rX   rE   r  rQ   r  s   ` rV   test_tcpNoDelayzRealSocketTests.test_tcpNoDelay:  s:     
	?0
	?BV
	?
	? 00777   )424c                 l    K   dt         dt         ddf fd} j                  |       d{    y7 w)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                    | |fD ]  }t         j                  j                  |j                        sJ |j                  }j	                  |j                         d       |j                  d       j	                  |j                         d       |j                  d       j	                  |j                         d        y )NFT)r   r  r  ry   rO   getTcpKeepAlivesetTcpKeepAliver  s       rV   r  z0RealSocketTests.test_tcpKeepAlive.<locals>.checkW  s     %n5 E!//::1;;GGGKK	  !:!:!<eD))$/  !:!:!<dC))%0  !:!:!<eDErX   r  r  s   ` rV   test_tcpKeepAlivez!RealSocketTests.test_tcpKeepAliveO  s>     
	E0
	EBV
	E
	E 00777r  c                   K   t         j                  j                  t              sJ t	               }t        j
                  d|d       |j                   d{    |j                  j                  t        j                         y7 .w)zb
        Tests whether attempting to establish connection to non-existing UNIX path fails
        znon-existing   )timeoutN)r   r,   r  r   r8   r   failDeferredr   r   r   ConnectError)rQ   r  s     rV   test_FailingzRealSocketTests.test_Failinge  sh      &&11':::!#NGQ?""""E../ 	#s   AB
B/B
)rY   rZ   r[   r\   rS   r  r   rE   r  r  r  r  r]   rX   rV   r  r    sH     D6,2 "68L!Mt!ST2	2<8*8,
0rX   r  )vr\   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.taskr0   &twisted.internet.test.connectionmixinsr1   r2   r3   r4   r5   #twisted.internet.test.reactormixinsr6   twisted.internet.test.test_tcpr7   r8   r9   r:   r;   twisted.python.compatr<   twisted.python.failurer=   twisted.python.filepathr>   twisted.python.logr?   r@   rA   twisted.python.reflectrB   twisted.python.runtimerC   rD   twisted.test.test_tcprE   twisted.trial.unittestrF   r   rz  rI   rb   rd   rs   r   r   r}  r  r  r  r  r  r  globalsupdatemakeTestCaseClassesr  r  r]   rX   rV   <module>r)     s  
  2 2  ; ;   $ : : * G 1 B B 0 @ @ M 
  U T -  ?  / * ? ? ? 0 9 6 +
0
1 )*6K 
< <")?/ ?,", "J $%;/ ; &;|S<8L S<lC CB*: *:Z: :@I I 		{,C^ {| 	  !557 8 	  )==? @ 	  %99; < 	  ';;= > 	  6JJL M*7^-C *7Z 	  ';;= > !HIp0h p0 Jp0U  Gs   J1 1J<;J<