+
    DfjRF                        R t ^ RIHt ^ RIHt  ^ RIt]t^ RIH	t	 ^ RI
Ht ^ RIHtHt ^ RIHt ^ RIHtHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHt ^ RIHtHtHtH t H!t! ^ RI"H#t# ^ RI$H%t% ^ RI&H't'H(t(  ! R R] PR                  4      t* ! R R]PV                  4      t+ ! R R],4      t- ! R R]4      t. ! R R4      t/ ! R R]4      t0R#   ] d    Rt Li ; i)z#
Tests for L{twisted.web.distrib}.
)abspath)parseStringN)skipIf)verifyObject)deferreactor)globalLogPublisher)failurefilepath)pb
SIZE_LIMIT)proto_helpers)TestCase)clientdistribresourceserverstatic)Headers)_render)DummyChannelDummyRequestc                       ] tR t^#tRtR# )MySite N)__name__
__module____qualname____firstlineno____static_attributes__r       ?/usr/lib/python3/dist-packages/twisted/web/test/test_distrib.pyr   r   #   s    r!   r   c                   .   a  ] tR t^'t o RtRtR tRtV tR# )PBServerFactoryz
A PB server factory which keeps track of the most recent protocol it
created.

@ivar proto: L{None} or the L{Broker} instance most recently returned
    from C{buildProtocol}.
Nc                b    \         P                  P                  W4      V n        V P                  # N)r   r$   buildProtocolproto)selfaddrs   &&r"   r'   PBServerFactory.buildProtocol2   s$    ''55dA
zzr!   )r(   )	r   r   r   r   __doc__r(   r'   r    __classdictcell____classdict__s   @r"   r$   r$   '   s      E r!   r$   c                       ] tR t^7tRtRtR# )ArbitraryErrorz
An exception for this test.
r   N)r   r   r   r   r,   r    r   r!   r"   r1   r1   7   s    r!   r1   c                   ~   a  ] tR t^=t o RtRtRtRtR tR t	R t
R tR tR tR tR	 tR
 tR tR tR tR tRtV tR# )DistribTestsNc                  a \         P                  ! 4       \         P                  ! 4       .oV P                  eC   V P                  P                  e+   V P                  P                  P	                  V3R l4       MS^ ,          P                  R4       V P                  e   V P                  P                  em   V P                  P                  P                  P	                  V3R l4       V P                  P                  P                  P                  P                  4        MS^,          P                  R4       V P                  e*   SP                  V P                  P                  4       4       V P                  e*   SP                  V P                  P                  4       4       \         P                  ! S4      # )zs
Clean up all the event sources left behind by either directly by
test methods or indirectly via some distrib API.
Nc                  4   < S ^ ,          P                  R4      # )    Ncallbackdls   r"   <lambda>'DistribTests.tearDown.<locals>.<lambda>J   s    RU^^D5Ir!   c                  4   < S ^,          P                  R4      # )   Nr7   r9   s   r"   r;   r<   N   s    APTAUr!   )r   Deferredf1r(   notifyOnDisconnectr8   sub	publisherbroker	transportloseConnectionport1appendstopListeningport2gatherResults)r)   r:   s   &@r"   tearDownDistribTests.tearDownC   s   
 nn 0177477==#<GGMM,,-IJqENN4 88DHH$6$6$BHH%%889UVHH%%//>>@qENN4 ::!IIdjj..01::!IIdjj..01""2&&r!   c                   \         P                  ! 4       pVP                  R \        P                  ! RR4      4       \
        P                  ! V4      p\        \        P                  ! V4      4      V n
        \        P                  ! ^ V P                  4      V n        \        P                  ! RV P                  P                  4       P                   4      V n        \         P                  ! 4       pVP                  RV P"                  4       \%        V4      p\        P                  ! ^ V4      V n        \(        P*                  ! \        4      pRV P&                  P                  4       P                    R2pVP-                  R4      pVP/                  RV4      pVP1                  \(        P2                  4       VP1                  V P4                  R4       V# )	s   theres   rootz
text/plain	127.0.0.1s   herezhttp://127.0.0.1:z/here/thereascii   GET)r   ResourceputChildr   Datar   Siter$   r   ResourcePublisherr@   r   	listenTCPrG   ResourceSubscriptiongetHostportrB   r   rJ   r   AgentencoderequestaddCallbackreadBodyassertEqual)r)   r1site1r2f2agenturlds   &       r"   testDistribDistribTests.testDistribX   s@    
Hfkk'<@AB!'";";E"BC&&q$''2
//TZZ=O=O=Q=V=VW 
GTXX&BZ&&q"-
W%!$**"4"4"6";";!<KHjj!MM&#&	foo&	d&&0r!   c                n   \         P                  ! 4       pVP                  RV4       \        P                  ! V4      p\        \        P                  ! V4      4      ;V n        p\        P                  ! ^ VRR7      pV P                  VP                  4       VP                  4       p\        P                  ! VP                  VP                   4      ;V n        p\        P                  ! V4      p\        P                  ! ^ VRR7      p	V P                  V	P                  4       V	P                  4       p
W3# )z
Set up a resource on a distrib site using L{ResourcePublisher}.

@param child: The resource to publish using distrib.

@return: A tuple consisting of the host and port on which to contact
    the created site.
s   childrO   )	interface)r   rR   rS   r   rU   r$   r   rV   r@   r   rW   
addCleanuprI   rY   rX   hostrZ   rB   )r)   childdistribRootdistribSitedistribFactorydistribPortr*   mainRootmainSitemainPortmainAddrs   &&         r"   _setupDistribServer DistribTests._setupDistribServerl   s     '')Xu-kk+.#2%%k2$
 	
. ''>[Q112""$%::499diiPP8;;x($$QKH../##%!!r!   c                "   V P                  V4      w  r4\        P                  ! \        4      pRVP                   RVP
                   R2pVP                  R4      pVP                  ! RV3/ VB pVP                  \        P                  4       V# )ai  
Set up a resource on a distrib site using L{ResourcePublisher} and
then retrieve it from a L{ResourceSubscription} via an HTTP client.

@param child: The resource to publish using distrib.
@param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
    requesting the resource.

@return: A L{Deferred} which fires with the result of the request.
http://:/childrP   rQ   )
rw   r   r[   r   rm   rZ   r\   r]   r^   r_   )r)   rn   kwargsru   rv   re   rf   rg   s   &&,     r"   _requestTestDistribTests._requestTest   sy     "55e<W%af=jj!MM&#00	foo&r!   c                   V P                  V4      w  r4RVP                   RVP                   R2pVP                  R4      p\        P
                  ! \        4      P                  ! RV3/ VB pR pVP                  V4       V# )a  
Set up a resource on a distrib site using L{ResourcePublisher} and
then retrieve it from a L{ResourceSubscription} via an HTTP client.

@param child: The resource to publish using distrib.
@param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
    requesting the resource.

@return: A L{Deferred} which fires with a tuple consisting of a
    L{twisted.test.proto_helpers.AccumulatingProtocol} containing the
    body of the response and an L{IResponse} with the response itself.
rz   r{   r|   rP   rQ   c                    a a \         P                  ! 4       oS P                  S4       \        P                  ! 4       ;pSn        VP                  VV 3R  l4       V# )c                    < SS3# r&   r   )_protocolresponses   &r"   r;   GDistribTests._requestAgentTest.<locals>.cbCollectBody.<locals>.<lambda>   s
    Xx$8r!   )r   AccumulatingProtocoldeliverBodyr   r?   closedDeferredr^   )r   rg   r   s   f @r"   cbCollectBody5DistribTests._requestAgentTest.<locals>.cbCollectBody   sE    $99;H  **/..*::A'MM89Hr!   )	rw   rm   rZ   r\   r   r[   r   r]   r^   )r)   rn   r}   ru   rv   rf   rg   r   s   &&,     r"   _requestAgentTestDistribTests._requestAgentTest   sy     "55e<af=jj!LL!))&#@@	 	
m$r!   c                4  a aaaa / o\         P                  ! 4       o\        P                  ! S4       R.o ! VV3R lR\        P
                  4      pVVV 3R loS P                  V! 4       \        RR./4      R7      pVVV 3R lpVP                  V4       V# )	zn
The request headers are available on the request object passed to a
distributed resource's C{render} method.
Nc                   0   <a  ] tR t^t o VV3R ltRtV tR# )>DistribTests.test_requestHeaders.<locals>.ReportRequestHeadersc                v   < VS^ &   SP                  \        VP                  P                  4       4      4       R# )r6   r!   )updatedictrequestHeadersgetAllRawHeaders)r)   r]   reqr   s   &&r"   renderEDistribTests.test_requestHeaders.<locals>.ReportRequestHeaders.render   s1     A%%d7+A+A+R+R+T&UVr!   r   Nr   r   r   r   r   r    r-   )r/   r   r   s   @r"   ReportRequestHeadersr      s      r!   r   c                     < S U u. uF  q R ,          NK  	  pp SP                  RV4       SP                  RV4       SP                  S^ ,          V4       \        P                  ! S4       R# u up i )
log_formatzconnected to publisherz3could not connect to distributed web service: {msg}N)assertInr   removeObserver)emsgslogObserverr   r)   s     r"   
check_logs4DistribTests.test_requestHeaders.<locals>.check_logs   s_    -89[lOO[D9MM2D9MMOQUVMM#a&$'--k:	 :s   A.foobar)headersc                    < SP                   P                  P                  S4       SP                  SR ,          R.4       R# )s   Foos   barN)r@   r(   rA   r`   )resultr   r   r)   s   &r"   cbRequested5DistribTests.test_requestHeaders.<locals>.cbRequested   s0    GGMM,,Z8^F3fX>r!   )	r   EventLoggingObserverr   addObserverr   rR   r~   r   r^   )r)   r   r]   r   r   r   r   r   s   f   @@@@r"   test_requestHeaders DistribTests.test_requestHeaders   s    
 #88:&&{3f	 	8#4#4 		; ## "GUUG4D,E $ 
	? 	K(r!   c                   a   ! R R\         P                  4      pS P                  V! 4       4      pV 3R lpVP                  V4       V# )zi
The response code can be set by the request object passed to a
distributed resource's C{render} method.
c                   &   a  ] tR t^t o R tRtV tR# )>DistribTests.test_requestResponseCode.<locals>.SetResponseCodec                (    VP                  ^4       R# )    setResponseCoder)   r]   s   &&r"   r   EDistribTests.test_requestResponseCode.<locals>.SetResponseCode.render   s    '',r!   r   Nr   r.   s   @r"   SetResponseCoder            r!   r   c                    < SP                  V ^ ,          P                  R4       SP                  V ^,          P                  ^4       SP                  V ^,          P                  R4       R# )r6   r!   s   OKNr`   datacodephraser   r)   s   &r"   r   :DistribTests.test_requestResponseCode.<locals>.cbRequested   sM    VAY^^S1VAY^^S1VAY--u5r!   r   rR   r   r^   r)   r   r]   r   s   f   r"   test_requestResponseCode%DistribTests.test_requestResponseCode   sD    	h// 	
 (():;	6
 	K(r!   c                   a   ! R R\         P                  4      pS P                  V! 4       4      pV 3R lpVP                  V4       V# )zu
The response code and message can be set by the request object passed to
a distributed resource's C{render} method.
c                   &   a  ] tR t^t o R tRtV tR# )EDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCodec                *    VP                  ^R4       R# )r      some-messager   r   r   s   &&r"   r   LDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCode.render   s    ''_=r!   r   Nr   r.   s   @r"   r   r      r   r!   r   c                    < SP                  V ^ ,          P                  R4       SP                  V ^,          P                  ^4       SP                  V ^,          P                  R4       R# )r6   r!   r   Nr   r   s   &r"   r   ADistribTests.test_requestResponseCodeMessage.<locals>.cbRequested   sM    VAY^^S1VAY^^S1VAY--?r!   r   r   s   f   r"   test_requestResponseCodeMessage,DistribTests.test_requestResponseCodeMessage   sE    	h// 	
 (():;	@
 	K(r!   c                     ! R R\         P                  4      pV P                  V! 4       4      pVP                  V P                  R\
        ,          R,           4       V# )z
If a string longer than the Banana size limit is passed to the
L{distrib.Request} passed to the remote resource, it is broken into
smaller strings to be transported over the PB connection.
c                   &   a  ] tR tRt o R tRtV tR# )0DistribTests.test_largeWrite.<locals>.LargeWritei
  c                    VP                  R \        ,          R,           4       VP                  4        \        P                  #    x   y)writer   finishr   NOT_DONE_YETr   s   &&r"   r   7DistribTests.test_largeWrite.<locals>.LargeWrite.render  s.    dZ/$67 ***r!   r   Nr   r.   s   @r"   
LargeWriter   
  s     + +r!   r   r   r   r   rR   r~   r^   r`   r   )r)   r   r]   s   &  r"   test_largeWriteDistribTests.test_largeWrite  sM    	+** 	+ ##JL1D,,dZ.?$.FGr!   c                     ! R R\         P                  4      pV P                  V! 4       4      pVP                  V P                  R\
        ,          R,           4       V# )z
Like L{test_largeWrite}, but for the case where C{render} returns a
long string rather than explicitly passing it to L{Request.write}.
c                   &   a  ] tR tRt o R tRtV tR# )2DistribTests.test_largeReturn.<locals>.LargeReturni  c                *    R \         ,          R,           # r   r   r   s   &&r"   r   9DistribTests.test_largeReturn.<locals>.LargeReturn.render  s    j(4//r!   r   Nr   r.   s   @r"   LargeReturnr     s     0 0r!   r   r   r   r   )r)   r   r]   s   &  r"   test_largeReturnDistribTests.test_largeReturn  sM    	0(++ 	0 ##KM2D,,dZ.?$.FGr!   c                \  a a \        \        P                  ! 4       4      ;S n        p\        P
                  ! ^ V4      ;S n        p\        P                  ! RVP                  4       P                  4      ;S n        p\        R.4      o\        VS4      pVV 3R lpVP                  V4       V# )zb
If there is an error issuing the request to the remote publisher, an
error response is returned.
rO   r!   c                   < SP                  SP                  R 4       SP                  \        P                  4      pSP                  \        V4      ^4       . ROpSP                  RP                  V4      .SP                  4       R# )i     
N)
r!   s   <html>s:     <head><title>500 - Server Connection Lost</title></head>s     <body>s#       <h1>Server Connection Lost</h1>s       <p>Connection to distributed server lost:<pre>[Failure instance: Traceback from remote host -- twisted.spread.flavors.NoSuchMethod: No such method: remote_requests   ]</pre></p>s	     </body>s   </html>r!   )r`   responseCodeflushLoggedErrorsr   NoSuchMethodlenjoinwritten)ignorederrorsexpectedr]   r)   s   &  r"   
cbRendered4DistribTests.test_connectionLost.<locals>.cbRendered2  sl    W1137 ++BOO<FS[!,H  ejj23W__Er!   )r$   r   Rootr@   r   rW   rG   r   rX   rY   rZ   rB   r   r   r^   )r)   serverFactory
serverPortsubscriptionrg   r   r]   s   f     @r"   test_connectionLost DistribTests.test_connectionLost"  s     #2"'')"<<-")"3"3A}"EE
Z")">">++-22#
 	
< u%L'*	F2 	
j!r!   c                f   \         P                  P                  V \        4      p\        P
                  ! \        4       4      p\        R.4      p\        P                  ! V4      pVP                  V4       V P                  ^\        V4      4       V P                  RV^ ,          R,          4       R# )zA
When a request fails, the string form of the failure is logged.
r!   zFailure instancer   N)r   r   createWithCleanupr   r	   Failurer1   r   r   IssuefailedassertEqualsr   r   )r)   r   fr]   issues   &    r"   test_logFailedDistribTests.test_logFailedN  s     $88JJ$
 OON,-u%g&Q!S-.(+a.*FGr!   c                   \         P                  P                  V \        4      p\	        4       p\
        P                  ! V4      p\        P                  ! \        4       4      pVP                  V4       V P                  \        4       V P                  ^\        V4      4       V P                  V^ ,          R,          V4       R# )zN
When L{twisted.web.distrib.Request}'s fail is called, the failure
is logged.
log_failureN)r   r   r   r   r1   r	   r   r   Requestr   failr   r   r   assertIs)r)   r   errr  r   s   &    r"   test_requestFailDistribTests.test_requestFail]  s    
 $88JJ$
 OOC ooln-~.!S-.k!n]3Q7r!   )r@   rG   rJ   rB   )r   r   r   r   rG   rJ   rB   r@   rL   rh   rw   r~   r   r   r   r   r   r   r   r  r  r    r-   r.   s   @r"   r3   r3   =   sb     EE
C	B'*("6&: D**"*XH8 8r!   r3   c                   2   a  ] tR tRt o R tR tR tRtV tR# )_PasswordDatabasein  c                    Wn         R # r&   _users)r)   userss   &&r"   __init___PasswordDatabase.__init__o  s    r!   c                ,    \        V P                  4      # r&   )iterr  r)   s   &r"   getpwall_PasswordDatabase.getpwallr  s    DKK  r!   c                `    V P                    F  pV^ ,          V8X  g   K  Vu # 	  \        4       h)r6   )r  KeyError)r)   usernameusers   && r"   getpwnam_PasswordDatabase.getpwnamu  s+    KKDAw("   jr!   r  N)	r   r   r   r   r  r  r  r    r-   r.   s   @r"   r  r  n  s     ! r!   r  c                      a  ] tR tRt o RtR tR tV 3R lR ltR tR t	R	 t
R
 tR tR t]! ]'       * R4      R 4       tRtV tR# )UserDirectoryTestsi|  z^
Tests for L{UserDirectory}, a resource for listing all user resources
available on a system.
c                   R R^{RRV P                  4       R3V n        RR^RRV P                  4       R3V n        \        V P                  V P                  .4      V n        \
        P                  ! V P                  4      V n        R# )	alicexi  zAlice,,,z/bin/shbobi7  zBob,,,N)mktempr#  r%  r  databaser   UserDirectory	directoryr  s   &r"   setUpUserDirectoryTests.setUp  si    sCj$++-S
3S(DKKM9M)4::txx*@A ..t}}=r!   c                l    V P                  \        \        P                  V P                  4      4       R# )z;
L{UserDirectory} instances provide L{resource.IResource}.
N)
assertTruer   r   	IResourcer)  r  s   &r"   test_interface!UserDirectoryTests.test_interface  s!     	X%7%7HIr!   c                $   < V ^8  d   QhRS[ RR/# )   namereturnN)bytes)formatr/   s   "r"   __annotate__UserDirectoryTests.__annotate__  s     	4 	45 	4T 	4r!   c                   "   \        V.4      pV P                  P                  W4      p\        W24      pVG Rj  xL
  V P	                  VP
                  R4       R#  L"5i)zZ
Verify that requesting the C{name} child of C{self.directory} results
in a 404 response.
Ni  )r   r)  getChildr   r`   r   )r)   r3  r]   r   rg   s   &&   r"   _404TestUserDirectoryTests._404Test  sS     
 v&((7F$--s3 	s   8AA#Ac                D   "   V P                  R4      G Rj  xL
  R#  L5i)z
L{UserDirectory.getChild} returns a resource which renders a 404
response when passed a string which does not correspond to any known
user.
s   carolNr;  r  s   &r"   test_getInvalidUser&UserDirectoryTests.test_getInvalidUser        mmH%%%     c                D   "   V P                  R4      G Rj  xL
  R#  L5i)z
L{UserDirectory.getChild} returns a resource which renders a 404
response when passed a string which corresponds to a known user who has
neither a user directory nor a user distrib socket.
s   aliceNr>  r  s   &r"   test_getUserWithoutResource.UserDirectoryTests.test_getUserWithoutResource  rA  rB  c                r   \         P                  ! V P                  R,          4      pVP                  R4      pVP	                  4        \        R.4      pV P                  P                  RV4      pV P                  V\        P                  4       V P                  VP                  VP                  4       R# )z
L{UserDirectory.getChild} returns a L{static.File} instance when passed
the name of a user with a home directory containing a I{public_html}
directory.
public_htmlr%  s   bobN)r
   FilePathr%  rn   makedirsr   r)  r:  assertIsInstancer   Filer`   path)r)   homerG  r]   r   s   &    r"   test_getPublicHTMLChild*UserDirectoryTests.test_getPublicHTMLChild  s       ".jj/w'((9ffkk2k&6&67r!   c                   \         P                  ! V P                  R,          4      pVP                  4        VP	                  R4      p\        R.4      pV P                  P                  RV4      pV P                  V\        P                  4       V P                  VP                  R4       V P                  \        VP                  4      VP                  4       R# )z
L{UserDirectory.getChild} returns a L{ResourceSubscription} instance
when passed the name of a user suffixed with C{".twistd"} who has a
home directory containing a I{.twistd-web-pb} socket.
.twistd-web-pbr%  s
   bob.twistdunixNrH  )r
   rI  r%  rJ  rn   r   r)  r:  rK  r   rX   r`   rm   r   rZ   rM  )r)   rN  webr]   r   s   &    r"   test_getDistribChild'UserDirectoryTests.test_getDistribChild  s       ".jj)*w'((@fg&B&BCf--sxx8r!   c                    \        R.4      pRVn        V P                  \        P                  V P
                  P                  V4       R# )zZ
L{UserDirectory.render} raises L{UnsupportedMethod} in response to a
non-I{GET} request.
r   POSTN)r   methodassertRaisesr   UnsupportedMethodr)  r   r   s   & r"   test_invalidMethod%UserDirectoryTests.test_invalidMethod  s9    
 t$&22DNN4I4I7Sr!   c                  a a \         P                  ! S P                  R,          4      P                  R4      pVP	                  4        \         P                  ! S P
                  R,          4      pVP	                  4        VP                  R4      P                  R4       \        R.4      o\        S P                  S4      pVV 3R lpVP                  V4       V# )ze
L{UserDirectory} renders a list of links to available user content
in response to a I{GET} request.
rG  rR  r!   r   c                 z  < \        R P                  SP                  4      4      pVP                  R4      w  r#SP	                  VP
                  P                  R4       SP	                  VP
                  P                  R4      R4       SP	                  VP
                  P
                  P                  R4       SP	                  VP
                  P                  R4       SP	                  VP
                  P                  R4      R4       SP	                  VP
                  P
                  P                  R4       R# )	r!   liahrefzalice/zAlice (file)zbob.twistd/zBob (twistd)N)	r   r   r   getElementsByTagNamer`   
firstChildtagNamegetAttributer   )r   documentr#  r%  r]   r)   s   &   r"   r   2UserDirectoryTests.test_render.<locals>.cbRendered  s    "388GOO#<=H $88>LUU--55s;U--::6BHMU--88==~NS^^33S9S^^88@-PS^^66;;^Lr!   rH  )r
   rI  r#  rn   rJ  r%  
setContentr   r   r)  r^   )r)   rG  rT  r   r   r]   s   f    @r"   test_renderUserDirectoryTests.test_render  s    
 ''

27==mL- 			"#..s3t$1
	M 	:&r!   zpwd module requiredc                p    \         P                  ! 4       pV P                  VP                  \        4       R# )zl
If L{UserDirectory} is instantiated with no arguments, it uses the
L{pwd} module as its password database.
N)r   r(  assertIdentical_pwdpwd)r)   r)  s   & r"   test_passwordDatabase(UserDirectoryTests.test_passwordDatabase  s&     ))+	Y^^S1r!   )r#  r%  r'  r)  N)r   r   r   r   r,   r*  r/  r;  r?  rD  rO  rU  r\  rj  r   ro  rp  r    r-   r.   s   @r"   r!  r!  |  s]     
>J	4 	4&&89T@ G*+2 ,2r!   r!  )1r,   os.pathr   xml.dom.minidomr   ro  rn  ImportErrorunittestr   zope.interface.verifyr   twisted.internetr   r   twisted.loggerr   twisted.pythonr	   r
   twisted.spreadr   twisted.spread.bananar   twisted.testr   twisted.trial.unittestr   twisted.webr   r   r   r   r   twisted.web.http_headersr   twisted.web.test._utilr   twisted.web.test.requesthelperr   r   rU   r   r$   	Exceptionr1   r3   r  r!  r   r!   r"   <module>r     s     ' C  . + - ,  , & + A A , * E	V[[ 	b((  Y n88 n8b	 z2 z2_  
Cs   C	 		CC