
    \d5                        d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ  G d
 dej                  Z e e
j        ed           d           G d dej                              Z G d dej                  Z G d dej                  Z  e e
j!        ed           d           G d dej                              Z"dS )zK
Tests for implementations of L{IReactorUNIX} and L{IReactorUNIXDatagram}.
    N)skipIf)addressdefererror
interfacesprotocolreactorutils)lockfile)networkString)FilePath)MyClientFactoryMyServerFactory)unittestc                       e Zd Zd Zd ZdS )FailedConnectionClientFactoryc                     || _         d S N)onFail)selfr   s     6lib/python3.11/site-packages/twisted/test/test_unix.py__init__z&FailedConnectionClientFactory.__init__   s        c                 :    | j                             |           d S r   )r   errback)r   	connectorreasons      r   clientConnectionFailedz4FailedConnectionClientFactory.clientConnectionFailed   s    F#####r   N)__name__
__module____qualname__r   r    r   r   r   r      s2          $ $ $ $ $r   r   1This reactor does not support UNIX domain socketsc                   n    e Zd ZdZ ej        ed          sdZd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd ZdS )UnixSocketTestsz
    Test unix sockets.
    Nr#   c                                                       }                                  t                      t          j                    x}_        t          j        |          }                     |j                   t          j	        t          j
        t          j                  }                     |j                   |                               |                    |            fd}|                    |           |S )z
        The address passed to the server factory's C{buildProtocol} method and
        the address returned by the connected protocol's transport's C{getPeer}
        method match the address the client socket is bound to.
        c                     t          j                  }                    j        |g                               | j                                        |           d S r   )r   UNIXAddressassertEqualpeerAddresses	transportgetPeer)protoexpectedpeernamer   serverFactorys     r   
cbConnMadez1UnixSocketTests.test_peerBind.<locals>.cbConnMade;   sX    *844H]88*EEEU_4466AAAAAr   )mktempr   r   DeferredprotocolConnectionMader	   
listenUNIX
addCleanupstopListeningsocketAF_UNIXSOCK_STREAMclosebindconnectaddCallback)r   filenameconnMadeunixPort
unixSocketr1   r/   r0   s   `     @@r   test_peerBindzUnixSocketTests.test_peerBind*   s     ;;==;;==')):?.:J:JJ=7%h>>.///]6>63EFF

()))!!!8$$$	B 	B 	B 	B 	B 	B 	B
 	Z(((r   c                                                       t                      }t          j                    }||_        t          j        |          }                     |j                   t                      t          j                    }|_        t          j
                   t          j        ||g          } fd}|                    |           |S )z
        L{IReactorUNIX.connectUNIX} can be used to connect a client to a server
        started with L{IReactorUNIX.listenUNIX}.
        c                     | \  }}                     j        t          j                  g           |j                                         |j                                         d S r   )r)   r*   r   r(   r+   loseConnection)argsserverProtocolclientProtocolclientFactoryr?   r   s      r   allConnectedz1UnixSocketTests.test_dumber.<locals>.allConnectedT   so    -1*NN +g.A(.K.K-L   $33555$3355555r   )r2   r   r   r3   r4   r	   r5   r6   r7   r   connectUNIXgatherResultsr>   )	r   r0   serverConnMaderA   clientConnMadedrK   rJ   r?   s	   `      @@r   test_dumberzUnixSocketTests.test_dumberC   s    
 ;;=='))))/=,%h>>.///'))))/=,Hm444 @AA		6 		6 		6 		6 		6 		6 		6 	
l###r   c                 "   	                                   t                      }t          j                    }||_        t          j        |d          	                     t          j	        dz                        t                      t          j                    }|_        t          j        d           t          j        ||g          } 	fd}|                    |            fd}|                    |           |S )z
        A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is
        called and released when the Deferred returned by the L{IListeningPort}
        provider's C{stopListening} method is called back.
        TwantPID.lock   checkPIDc                     | \  }}                     j        t          j                  g           |j                                         |j                                                                         S r   )r)   r*   r   r(   r+   rF   r7   )rG   rH   clientProtorJ   r?   r   rA   s      r   
_portStuffz0UnixSocketTests.test_pidFile.<locals>._portStuffy   s{    *.'NK +g.A(.K.K-L   !00222$33555))+++r   c                 ^                         t          j        dz             d           d S )NrU   locked)assertFalser   isLocked)ignoredr?   r   s    r   _checkz,UnixSocketTests.test_pidFile.<locals>._check   s/    X.x'/ABBHMMMMMr   )r2   r   r   r3   r4   r	   r5   
assertTruer   r_   r   rL   rM   r>   )
r   r0   rN   rO   rP   r[   ra   rJ   r?   rA   s
   `      @@@r   test_pidFilezUnixSocketTests.test_pidFileb   s6    ;;=='))))/=,%htLLL)(W*<==>>>
 ())))/=,Hma@@@@ @AA	, 	, 	, 	, 	, 	, 	, 	, 	
j!!!	N 	N 	N 	N 	N 	N 	
fr   c                 .   |                                  t                      t          j        d          }|                     t
          j        t          j        d           fd}|                                                    |          S )z
        L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed
        the name of a file on which a server is already listening.
        TrS   c                 Z    t          j        d          }|                                S NTrS   )r	   r5   r7   )ignrA   r?   r0   s     r   stoppedListeningz<UnixSocketTests.test_socketLocking.<locals>.stoppedListening   s,    )(M4PPPH))+++r   )	r2   r   r	   r5   assertRaisesr   CannotListenErrorr7   r>   )r   rA   rh   r?   r0   s      @@r   test_socketLockingz"UnixSocketTests.test_socketLocking   s    
 ;;=='))%htLLL# 	 	
 	
 	
	, 	, 	, 	, 	, 	, %%''334DEEEr   c                    |                                  | _        t          d| j        d          }dt          t          j                            t          j                            	                                j        i}t          t          j
                  	                                j        }t          j        |dd|f|          }|                    |           |S )NzBfrom twisted.internet import protocol, reactor
reactor.listenUNIX(z), protocol.ServerFactory(),wantPID=True)
s
   PYTHONPATHs   -us   -c)env)r2   r?   r   r   ospathsepjoinsyspathasBytesMode
executabler
   getProcessValuer>   )r   callbacksourcerm   pyExerP   s         r   _uncleanSocketTestz"UnixSocketTests._uncleanSocketTest   s     }}}
 
 hrzsx'@'@AAMMOOTU((4466;!%%)?SIII	hr   c                 8      fd}                      |          S )a"  
        If passed C{True} for the C{wantPID} parameter, a server can be started
        listening with L{IReactorUNIX.listenUNIX} when passed the name of a
        file on which a previous server which has not exited cleanly has been
        listening using the C{wantPID} option.
        c                 |    t          j        j        t                      d          }|                                S rf   )r	   r5   r?   r   r7   )rg   pr   s     r   ranStupidChildzGUnixSocketTests.test_uncleanServerSocketLocking.<locals>.ranStupidChild   s2    "4=/2C2CTRRRA??$$$r   ry   r   r}   s   ` r   test_uncleanServerSocketLockingz/UnixSocketTests.test_uncleanServerSocketLocking   s2    	% 	% 	% 	% 	%
 &&~666r   c                 8      fd}                      |          S )z
        If passed C{True} for the C{checkPID} parameter, a client connection
        attempt made with L{IReactorUNIX.connectUNIX} fails with
        L{error.BadFileError}.
        c                     t          j                    }t          |          }t          j        j        |d                               |t          j                  S )NTrW   )	r   r3   r   r	   rL   r?   assertFailurer   BadFileError)rg   rP   fr   s      r   r}   zCUnixSocketTests.test_connectToUncleanServer.<locals>.ranStupidChild   sP      A-a00Aq4@@@@%%a);<<<r   r~   r   s   ` r   test_connectToUncleanServerz+UnixSocketTests.test_connectToUncleanServer   s2    	= 	= 	= 	= 	= &&~666r   c                 f                                      }t          j        ||          d d|d}                     t	                    |                                t                    |           t          j        j                  } fd}|	                    |           |S )z~
        Test the C{__str__} and C{__repr__} implementations of a UNIX port when
        used with the given factory.
        < on >c                     d d}                     t                    |                                t                    |           d S Nr   z (not listening)>r)   reprstr)rg   unconnectedStringfactoryNamer   rA   s     r   rh   z3UnixSocketTests._reprTest.<locals>.stoppedListening   sT     BK B B BT(^^->???S]],=>>>>>r   )
r2   r	   r5   r)   r   r   r   maybeDeferredr7   r>   )r   r0   r   r?   connectedStringrP   rh   rA   s   ` `    @r   	_reprTestzUnixSocketTests._reprTest   s    
 ;;==%h>><k<<x<<<h999X888 677	? 	? 	? 	? 	? 	? 	?
 	
&'''r   c                      G d d          }|                      |t                     |                      |            d          S )a!  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIX.listenUNIX} contains the name of the new-style factory
        class being used and the filename on which the port is listening or
        indicates that the port is not listening.
        c                       e Zd Zd Zd ZdS )EUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactoryc                     d S r   r"   r   s    r   doStartzMUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStart       r   c                     d S r   r"   r   s    r   doStopzLUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStop   r   r   N)r   r    r!   r   r   r"   r   r   NewStyleFactoryr      2              r   r   z&twisted.test.test_unix.NewStyleFactoryassertIsInstancetyper   )r   r   s     r   test_reprWithNewStyleFactoryz,UnixSocketTests.test_reprWithNewStyleFactory   sf    	 	 	 	 	 	 	 	 	ot444~~OG
 
 	
r   )r   r    r!   __doc__r   IReactorUNIXr	   skiprC   rQ   rc   rk   ry   r   r   r   r   r"   r   r   r%   r%      s        
  #:"7D11 CB  2  >* * *XF F F.  "7 7 77 7 7  ,
 
 
 
 
r   r%   c                   2    e Zd ZdxZZdZd Zd Zd Zd Z	dS )ClientProtoFNc                 f    t          j                    | _        t          j                    | _        d S r   )r   r3   deferredStarteddeferredGotBackr   s    r   r   zClientProto.__init__  (    $~//$~//r   c                     d| _         d S NTstoppedr   s    r   stopProtocolzClientProto.stopProtocol	      r   c                 H    d| _         | j                            d            d S r   startedr   rv   r   s    r   startProtocolzClientProto.startProtocol  &    %%d+++++r   c                 H    || _         | j                            d            d S r   )gotbackr   rv   )r   datas     r   datagramReceivedzClientProto.datagramReceived  r   r   )
r   r    r!   r   r   r   r   r   r   r   r"   r   r   r   r     s_        GgG0 0 0  , , ,, , , , ,r   r   c                   6    e Zd ZdxZZdxZZd Zd Zd Z	d Z
dS )ServerProtoFNc                 f    t          j                    | _        t          j                    | _        d S r   )r   r3   r   deferredGotWhatr   s    r   r   zServerProto.__init__  r   r   c                     d| _         d S r   r   r   s    r   r   zServerProto.stopProtocol  r   r   c                 H    d| _         | j                            d            d S r   r   r   s    r   r   zServerProto.startProtocol   r   r   c                     || _         | j                            d|           || _        | j                            d            d S )N   hi back)gotfromr+   writegotwhatr   rv   )r   r   addrs      r   r   zServerProto.datagramReceived$  sE    Z...%%d+++++r   )r   r    r!   r   r   r   r   r   r   r   r   r"   r   r   r   r     sd        GgGg0 0 0  , , ,, , , , ,r   r   z3This reactor does not support UNIX datagram socketsc                   *    e Zd ZdZd Zd Zd Zd ZdS )DatagramUnixSocketTestsz%
    Test datagram UNIX sockets.
    c                    	                                                                     }t                      	t                      t          j        |	          }                     |j                   t          j        |          }                     |j                   t          j	        	j
        j
        g          }	fd} 	fd}|                    |           |                    |           |S )zf
        Test that a datagram can be sent to and received by a server and vice
        versa.
        )bindAddressc                 x    j                             d           t          j        j        j        g          S )N   hi)r+   r   r   rM   r   r   )r`   cpsps    r   r   z4DatagramUnixSocketTests.test_exchange.<locals>.writeD  s5    Lu%%%&(:B<N'OPPPr   c                                          dj                                        j                                        dj                   d S )Nr   r   )r)   r   r   r   )r`   
clientaddrr   r   r   s    r   _cbTestExchangez>DatagramUnixSocketTests.test_exchange.<locals>._cbTestExchangeH  sQ    UBJ///Z444Z44444r   )r2   r   r   r	   listenUNIXDatagramr6   r7   connectUNIXDatagramr   rM   r   r>   )
r   
serveraddrscrP   r   r   r   r   r   s
   `      @@@r   test_exchangez%DatagramUnixSocketTests.test_exchange4  s   
 [[]]
[[]]
]]]]&z266((('
BJOOO(((!3R5G HII	Q 	Q 	Q 	Q 	Q 	Q	5 	5 	5 	5 	5 	5 	5 	5
 	
e	o&&&r   c                    |                                  }t                      }t          j        ||          }|                     t
          j        t          j        ||           |                                 t          j	        |           dS )z
        L{IReactorUNIXDatagram.listenUNIXDatagram} raises
        L{error.CannotListenError} if the unix socket specified is already in
        use.
        N)
r2   r   r	   r   ri   r   rj   r7   rn   unlink)r   r   r|   r   s       r   test_cannotListenz)DatagramUnixSocketTests.test_cannotListenQ  sn     {{}}MM&tQ//%173MtUVWWW	
	$r   c                 f                                      }t          j        ||          d d|d}                     t	                    |                                t                    |           t          j        j                  } fd}|	                    |           |S )z
        Test the C{__str__} and C{__repr__} implementations of a UNIX datagram
        port when used with the given protocol.
        r   r   r   c                     d d}                     t                    |                                t                    |           d S r   r   )rg   r   protocolNamer   rA   s     r   rh   z;DatagramUnixSocketTests._reprTest.<locals>.stoppedListeningn  sT     CL C C CT(^^->???S]],=>>>>>r   )
r2   r	   r   r)   r   r   r   r   r7   r>   )r   serverProtor   r?   r   stopDeferredrh   rA   s   ` `    @r   r   z!DatagramUnixSocketTests._reprTest`  s    
 ;;==-hDD=l=====h999X888*8+ABB	? 	? 	? 	? 	? 	? 	?
 	  !1222r   c                      G d d          }|                      |t                     |                      |            d          S )a2  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the
        new-style protocol class being used and the filename on which the port
        is listening or indicates that the port is not listening.
        c                       e Zd Zd Zd ZdS )ODatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocolc                     d S r   r"   )r   r+   s     r   makeConnectionz^DatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.makeConnection  r   r   c                     d S r   r"   r   s    r   r   zVDatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.doStop  r   r   N)r   r    r!   r   r   r"   r   r   NewStyleProtocolr   ~  r   r   r   z'twisted.test.test_unix.NewStyleProtocolr   )r   r   s     r   test_reprWithNewStyleProtocolz5DatagramUnixSocketTests.test_reprWithNewStyleProtocolv  sh    	 	 	 	 	 	 	 	 	.555~~ I
 
 	
r   N)r   r    r!   r   r   r   r   r   r"   r   r   r   r   +  sZ        
   :    ,
 
 
 
 
r   r   )#r   rn   r8   rq   r   r   twisted.internetr   r   r   r   r   r	   r
   twisted.pythonr   twisted.python.compatr   twisted.python.filepathr   twisted.test.test_tcpr   r   twisted.trialClientFactoryr   r   TestCaser%   ConnectedDatagramProtocolr   DatagramProtocolr   IReactorUNIXDatagramr   r"   r   r   <module>r      s8   
 
			  



       X X X X X X X X X X X X X X X X X X # # # # # # / / / / / / , , , , , , B B B B B B B B " " " " " "$ $ $ $ $H$: $ $ $ 
...7 \
 \
 \
 \
 \
h' \
 \
	 \
~, , , , ,(4 , , ,(, , , , ,(+ , , ,, '
'6669 [
 [
 [
 [
 [
h/ [
 [
	 [
 [
 [
r   