
    h5                        S r SSKrSSKrSSKrSSKJr  SSKJrJrJ	r	J
r
JrJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  SS	KJr   " S
 S\R0                  5      r\" \
R4                  " \S5      (       + S5       " S S\R6                  5      5       r " S S\R:                  5      r " S S\R>                  5      r \" \
RB                  " \S5      (       + S5       " S S\R6                  5      5       r"g)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                        \ rS rSrS rS rSrg)FailedConnectionClientFactory   c                     Xl         g NonFail)selfr   s     ]/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_unix.py__init__&FailedConnectionClientFactory.__init__   s        c                 :    U R                   R                  U5        g r   )r   errback)r   	connectorreasons      r   clientConnectionFailed4FailedConnectionClientFactory.clientConnectionFailed   s    F#r   r   N)__name__
__module____qualname____firstlineno__r   r!   __static_attributes__ r   r   r   r      s    $r   r   1This reactor does not support UNIX domain socketsc                       \ rS rSrSr\R                  " \S5      (       d  SrS r	S r
S rS rS	 rS
 rS rS rS rSrg)UnixSocketTests   z
Test unix sockets.
Nr)   c                   ^ ^^ T R                  5       nT R                  5       m[        5       m[        R                  " 5       =nTl        [
        R                  " UT5      nT R                  UR                  5        [        R                  " [        R                  [        R                  5      nT R                  UR                  5        UR                  T5        UR                  U5        UU U4S jnUR                  U5        U$ )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                    > [         R                  " T5      nTR                  TR                  U/5        TR                  U R                  R                  5       U5        g r   )r   UNIXAddressassertEqualpeerAddresses	transportgetPeer)protoexpectedpeernamer   serverFactorys     r   
cbConnMade1UnixSocketTests.test_peerBind.<locals>.cbConnMade;   sH    **84H]888*EU__446Ar   )mktempr   r   DeferredprotocolConnectionMader	   
listenUNIX
addCleanupstopListeningsocketAF_UNIXSOCK_STREAMclosebindconnectaddCallback)r   filenameconnMadeunixPort
unixSocketr8   r6   r7   s   `     @@r   test_peerBindUnixSocketTests.test_peerBind*   s     ;;=;;='):?..:JJ=7%%h>../]]6>>63E3EF

(()!8$	B
 	Z(r   c                   ^ ^^ T R                  5       m[        5       n[        R                  " 5       nX!l        [
        R                  " TU5      nT R                  UR                  5        [        5       m[        R                  " 5       nUTl        [
        R                  " TT5        [        R                  " X$/5      nUUU 4S jnUR                  U5        U$ )zr
L{IReactorUNIX.connectUNIX} can be used to connect a client to a server
started with L{IReactorUNIX.listenUNIX}.
c                    > U u  pTR                  TR                  [        R                  " T5      /5        UR                  R                  5         UR                  R                  5         g r   )r0   r1   r   r/   r2   loseConnection)argsserverProtocolclientProtocolclientFactoryrG   r   s      r   allConnected1UnixSocketTests.test_dumber.<locals>.allConnectedT   sZ    -1*N ++g.A.A(.K-L $$335$$335r   )r:   r   r   r;   r<   r	   r=   r>   r?   r   connectUNIXgatherResultsrF   )	r   r7   serverConnMaderI   clientConnMadedrT   rS   rG   s	   `      @@r   test_dumberUnixSocketTests.test_dumberC   s    
 ;;='))/=,%%h>../'))/=,Hm4 @A		6 	
l#r   c                   ^ ^^^	 T R                  5       m[        5       n[        R                  " 5       nX!l        [
        R                  " TUSS9m	T R                  [        R                  " TS-   5      5        [        5       m[        R                  " 5       nUTl        [
        R                  " TTSS9  [        R                  " X#/5      nUUU U	4S jnUR                  U5        UU 4S jnUR                  U5        U$ )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                    > U u  pTR                  TR                  [        R                  " T5      /5        UR                  R                  5         UR                  R                  5         TR                  5       $ r   )r0   r1   r   r/   r2   rO   r?   )rP   rQ   clientProtorS   rG   r   rI   s      r   
_portStuff0UnixSocketTests.test_pidFile.<locals>._portStuffy   si    *.'N ++g.A.A(.K-L !!002$$335))++r   c                 X   > TR                  [        R                  " TS-   5      S5        g )Nr`   locked)assertFalser   isLocked)ignoredrG   r   s    r   _check,UnixSocketTests.test_pidFile.<locals>._check   s#    X..x'/ABHMr   )r:   r   r   r;   r<   r	   r=   
assertTruer   rk   r   rV   rW   rF   )
r   r7   rX   rY   rZ   rf   rm   rS   rG   rI   s
   `      @@@r   test_pidFileUnixSocketTests.test_pidFileb   s     ;;='))/=,%%htL))(W*<=>
 ())/=,Hma@ @A	, 	, 	
j!	N 	
fr   c                   ^^ U R                  5       m[        5       m[        R                  " TTSS9nU R	                  [
        R                  [        R                  TTSS9  UU4S jnUR                  5       R                  U5      $ )z
L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed
the name of a file on which a server is already listening.
Tr^   c                 P   > [         R                  " TTSS9nUR                  5       $ NTr^   )r	   r=   r?   )ignrI   rG   r7   s     r   stoppedListening<UnixSocketTests.test_socketLocking.<locals>.stoppedListening   s&    ))(M4PH))++r   )	r:   r   r	   r=   assertRaisesr   CannotListenErrorr?   rF   )r   rI   rv   rG   r7   s      @@r   test_socketLocking"UnixSocketTests.test_socketLocking   s|    
 ;;=')%%htL## 	 	
	, %%'334DEEr   c                    U R                  5       U l        [        SU R                  < S35      nS[        [        R
                  R                  [        R                  5      5      R                  5       R                  0n[        [        R                  5      R                  5       R                  n[        R                  " USSU4US9nUR                  U5        U$ )NzBfrom twisted.internet import protocol, reactor
reactor.listenUNIX(z), protocol.ServerFactory(),wantPID=True)
s
   PYTHONPATHs   -us   -c)env)r:   rG   r   r   ospathsepjoinsyspathasBytesMode
executabler
   getProcessValuerF   )r   callbacksourcer}   pyExerZ   s         r   _uncleanSocketTest"UnixSocketTests._uncleanSocketTest   s     }}
 hrzzsxx'@AMMOTTU(446;;!!%%)?SI	hr   c                 2   ^  U 4S jnT R                  U5      $ )z
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   > [         R                  " TR                  [        5       SS9nUR	                  5       $ rt   )r	   r=   rG   r   r?   )ru   pr   s     r   ranStupidChildGUnixSocketTests.test_uncleanServerSocketLocking.<locals>.ranStupidChild   s+    ""4==/2CTRA??$$r   r   r   r   s   ` r   test_uncleanServerSocketLocking/UnixSocketTests.test_uncleanServerSocketLocking   s    	%
 &&~66r   c                 2   ^  U 4S jnT R                  U5      $ )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                    > [         R                  " 5       n[        U5      n[        R                  " TR
                  USS9  TR                  U[        R                  5      $ )NTrb   )	r   r;   r   r	   rV   rG   assertFailurer   BadFileError)ru   rZ   fr   s      r   r   CUnixSocketTests.test_connectToUncleanServer.<locals>.ranStupidChild   sH     A-a0Aq4@%%a););<<r   r   r   s   ` r   test_connectToUncleanServer+UnixSocketTests.test_connectToUncleanServer   s    	= &&~66r   c                 J  ^ ^^ T R                  5       n[        R                  " X15      mST SU< S3nT R                  [	        T5      U5        T R                  [        T5      U5        [        R                  " TR                  5      nUU U4S jnUR                  U5        U$ )zf
Test the C{__str__} and C{__repr__} implementations of a UNIX port when
used with the given factory.
< on >c                 ~   > ST S3nTR                  [        T5      U5        TR                  [        T5      U5        g Nr   z (not listening)>r0   reprstr)ru   unconnectedStringfactoryNamer   rI   s     r   rv   3UnixSocketTests._reprTest.<locals>.stoppedListening   s=    "#K=0A BT(^->?S],=>r   )
r:   r	   r=   r0   r   r   r   maybeDeferredr?   rF   )r   r7   r   rG   connectedStringrZ   rv   rI   s   ` `    @r   	_reprTestUnixSocketTests._reprTest   s    
 ;;=%%h>k]$xl!<h9X8 6 67	?
 	
&'r   c                 p     " S S5      nU R                  U[        5        U R                  U" 5       S5      $ )z
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                        \ rS rSrS rS rSrg)EUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory   c                     g r   r(   r   s    r   doStartMUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStart       r   c                     g r   r(   r   s    r   doStopLUnixSocketTests.test_reprWithNewStyleFactory.<locals>.NewStyleFactory.doStop   r   r   r(   N)r#   r$   r%   r&   r   r   r'   r(   r   r   NewStyleFactoryr          r   r   z&twisted.test.test_unix.NewStyleFactoryassertIsInstancetyper   )r   r   s     r   test_reprWithNewStyleFactory,UnixSocketTests.test_reprWithNewStyleFactory   s8    	 	 	ot4~~G
 	
r   )rG   )r#   r$   r%   r&   __doc__r   IReactorUNIXr	   skiprK   r[   rp   rz   r   r   r   r   r   r'   r(   r   r   r+   r+      sP    
 ""7D11B2>*XF."77,
r   r+   c                   8    \ rS rSrS=rrSrS rS rS r	S r
Srg)	ClientProtoi  FNc                 l    [         R                  " 5       U l        [         R                  " 5       U l        g r   )r   r;   deferredStarteddeferredGotBackr   s    r   r   ClientProto.__init__       $~~/$~~/r   c                     SU l         g NTstoppedr   s    r   stopProtocolClientProto.stopProtocol	  	    r   c                 H    SU l         U R                  R                  S 5        g r   startedr   r   r   s    r   startProtocolClientProto.startProtocol      %%d+r   c                 F    Xl         U R                  R                  S 5        g r   )gotbackr   r   )r   datas     r   datagramReceivedClientProto.datagramReceived  s    %%d+r   )r   r   r   r   r   )r#   r$   r%   r&   r   r   r   r   r   r   r   r'   r(   r   r   r   r     s%    GgG0,,r   r   c                   <    \ rS rSrS=rrS=rrS rS r	S r
S rSrg)	ServerProtoi  FNc                 l    [         R                  " 5       U l        [         R                  " 5       U l        g r   )r   r;   r   deferredGotWhatr   s    r   r   ServerProto.__init__  r   r   c                     SU l         g r   r   r   s    r   r   ServerProto.stopProtocol  r   r   c                 H    SU l         U R                  R                  S 5        g r   r   r   s    r   r   ServerProto.startProtocol   r   r   c                     X l         U R                  R                  SU5        Xl        U R                  R                  S 5        g )N   hi back)gotfromr2   writegotwhatr   r   )r   r   addrs      r   r   ServerProto.datagramReceived$  s4    Z.%%d+r   )r   r   r   r   r   r   )r#   r$   r%   r&   r   r   r   r   r   r   r   r   r'   r(   r   r   r   r     s*    GgGg0,,r   r   z3This reactor does not support UNIX datagram socketsc                   0    \ rS rSrSrS rS rS rS rSr	g)	DatagramUnixSocketTestsi+  z
Test datagram UNIX sockets.
c                   ^ ^^^	 T R                  5       mT R                  5       n[        5       m	[        5       m[        R                  " UT	5      nT R                  UR                  5        [        R                  " UTTS9nT R                  UR                  5        [        R                  " T	R                  TR                  /5      nUU	4S jnUUU U	4S jnUR                  U5        UR                  U5        U$ )zN
Test that a datagram can be sent to and received by a server and vice
versa.
)bindAddressc                    > TR                   R                  S5        [        R                  " TR                  TR
                  /5      $ )N   hi)r2   r   r   rW   r   r   )rl   cpsps    r   r   4DatagramUnixSocketTests.test_exchange.<locals>.writeD  s7    LLu%&&(:(:B<N<N'OPPr   c                    > TR                  STR                  5        TR                  TTR                  5        TR                  STR                  5        g )Nr   r   )r0   r   r   r   )rl   
clientaddrr   r   r   s    r   _cbTestExchange>DatagramUnixSocketTests.test_exchange.<locals>._cbTestExchangeH  s?    UBJJ/Z4Z4r   )r:   r   r   r	   listenUNIXDatagramr>   r?   connectUNIXDatagramr   rW   r   rF   )
r   
serveraddrscrZ   r   r   r   r   r   s
   `      @@@r   test_exchange%DatagramUnixSocketTests.test_exchange4  s    
 [[]
[[]
]]&&z26(''
BJO(!3!3R5G5G HI	Q	5 	5
 	
e	o&r   c                    U R                  5       n[        5       n[        R                  " X5      nU R	                  [
        R                  [        R                  X5        UR                  5         [        R                  " U5        g)z~
L{IReactorUNIXDatagram.listenUNIXDatagram} raises
L{error.CannotListenError} if the unix socket specified is already in
use.
N)
r:   r   r	   r   rx   r   ry   r?   r~   unlink)r   r   r   r   s       r   test_cannotListen)DatagramUnixSocketTests.test_cannotListenQ  sY     {{}M&&t/%1173M3MtW	
		$r   c                 J  ^ ^^ T R                  5       n[        R                  " X15      mST SU< S3nT R                  [	        T5      U5        T R                  [        T5      U5        [        R                  " TR                  5      nUU U4S jnUR                  U5        U$ )zp
Test the C{__str__} and C{__repr__} implementations of a UNIX datagram
port when used with the given protocol.
r   r   r   c                 ~   > ST S3nTR                  [        T5      U5        TR                  [        T5      U5        g r   r   )ru   r   protocolNamer   rI   s     r   rv   ;DatagramUnixSocketTests._reprTest.<locals>.stoppedListeningn  s=    "#L>1B CT(^->?S],=>r   )
r:   r	   r   r0   r   r   r   r   r?   rF   )r   serverProtor  rG   r   stopDeferredrv   rI   s   ` `    @r   r   !DatagramUnixSocketTests._reprTest`  s    
 ;;=--hDl^4|1=h9X8**8+A+AB	?
 	  !12r   c                 p     " S S5      nU R                  U[        5        U R                  U" 5       S5      $ )a
  
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                        \ rS rSrS rS rSrg)ODatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocoli~  c                     g r   r(   )r   r2   s     r   makeConnection^DatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.makeConnection  r   r   c                     g r   r(   r   s    r   r   VDatagramUnixSocketTests.test_reprWithNewStyleProtocol.<locals>.NewStyleProtocol.doStop  r   r   r(   N)r#   r$   r%   r&   r  r   r'   r(   r   r   NewStyleProtocolr  ~  r   r   r  z'twisted.test.test_unix.NewStyleProtocolr   )r   r  s     r   test_reprWithNewStyleProtocol5DatagramUnixSocketTests.test_reprWithNewStyleProtocolv  s9    	 	 	.5~~ I
 	
r   r(   N)
r#   r$   r%   r&   r   r   r  r   r  r'   r(   r   r   r   r   +  s    
:,
r   r   )#r   r~   r@   r   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      s   
 
  
  X X X # / , B "$H$:$: $ ..7\
h'' \
	\
~,(44 ,(,(++ ,, ''669[
h// [
	[
r   