
    h)                       S r SSKrSSKrSSKJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJrJr  SSK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  SSKJr  SSK J!r!   SSKJ"r#  \#RH                  (       d  Sr"O\#r" \"c  Sr&OSr&\RN                  RQ                  \5      (       d  Sr)OSr)\RT                  RV                  r, " S S\RZ                  5      r. " S S\R^                  5      r0 " S S\15      r2 " S S\15      r3 " S S\15      r4 " S S\Rj                  5      r6 " S S\Rn                  5      r8 " S S \85      r9 " S! S"\Rn                  5      r: " S# S$\Rn                  5      r; " S% S&\Rn                  5      r< " S' S(\Rn                  5      r= " S) S*\Rn                  5      r> " S+ S,\Rn                  5      r? " S- S.\Rn                  5      r@ " S/ S0\Rn                  5      rA " S1 S2\R                  5      rC " S3 S4\R                  5      rES5rF " S6 S7\15      rG " S8 S9\R^                  5      rH " S: S;\H5      rI " S< S=\I5      rJ " S> S?\I5      rK " S@ SA\I5      rL\0\04SB jrM " SC SD\RZ                  5      rN " SE SF\R^                  5      rO " SG SH\!5      rP " SI SJ\!5      rQ " SK SL5      rR " SM SN5      rS " SO SP\!5      rT " SQ SR\Rn                  5      rU " SS ST\R                  5      rW " SU SV\W5      rX " SW SX\X5      rY " SY SZ\R^                  5      rZ " S[ S\\!5      r[S]r\S^r] " S_ S`\!5      r^ " Sa Sb\!5      r_ " Sc Sd5      r` " Se Sf5      ra " Sg Sh\a5      rb " Si Sj\a5      rc " Sk Sl\a5      rd " Sm Sn\H5      re\	" \&So5      \	" \)Sp5       " Sq Sr\!5      5       5       rf " Ss St\!5      rg " Su Sv\15      rh " Sw Sx\15      ri " Sy Sz\Rn                  5      rj " S{ S|\j5      rk " S} S~\j5      rl " S S\R^                  5      rm " S S\R^                  5      rn " S S\R^                  5      ro " S S\!5      rpS rq " S S5      rrS rsS rt\"b  \t" 5       ru\	" \&So5      \	" \)Sp5       " S S\r\!5      5       5       rv " S S\I5      rw\	" \&So5      \	" \)Sp5       " S S\r\!5      5       5       rx\	" \&So5      \	" \)Sp5       " S S\r\!5      5       5       ry " S S\Rj                  5      rz " S S\Rn                  5      r{ " S S\Rn                  5      r| " S S\R^                  5      r}\" S5      r~\" S5      r " S S\\~\4   5      r " S S\{5      r " S S\!5      r " S S5      r " S S\!\5      r " S S\!\5      r " S S\!\5      r " S S\!\5      r " S S\!\5      r " S S\!5      r " S S\!5      r " S S\!\5      r " S S\!5      r\" \GR                  5       " S S5      5       r\" \GR                  \5        \" \GR                  \5         " S S\!5      r " S S\!5      r " S S\!5      r " S S\!5      rg! \% a    Sr" GNf = f)z%
Tests for L{twisted.protocols.amp}.
    N)ClassVarDictTypeTypeVar)skipIf)implementer)verifyClassverifyObject)addressdefererror
interfacesprotocolreactor)StringTransport)amp)filepath)Failure)iosim)TestCasesslTFc                   B    \ rS rSrSrSrS rS rS rS r	S\
4S	 jrS
rg)	TestProto2   z
A trivial protocol for use in testing where a L{Protocol} is expected.

@ivar instanceId: the id of this instance
@ivar onConnLost: deferred that will fired when the connection is lost
@ivar dataToSend: data to send on the protocol
r   c                     [        U[        5      (       d   [        U5      5       eXl        X l        [
        R                  U l        [
        R                  S-   [
        l        g )N   )
isinstancebytesrepr
onConnLost
dataToSendr   instanceCount
instanceId)selfr!   r"   s      \/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_amp.py__init__TestProto.__init__=   sI    *e,,>d:.>>,$$#11"+"9"9A"=	    c                 \    / U l         U R                  R                  U R                  5        g N)data	transportwriter"   r%   s    r&   connectionMadeTestProto.connectionMadeD   s    	T__-r)   c                 :    U R                   R                  U5        g r+   )r,   append)r%   r   s     r&   dataReceivedTestProto.dataReceivedH   s    		r)   c                 N    U R                   R                  U R                  5        g r+   )r!   callbackr,   r%   reasons     r&   connectionLostTestProto.connectionLostK   s      +r)   returnc                 "    SU R                   4-  $ )z
Custom repr for testing to avoid coupling amp tests with repr from
L{Protocol}

Returns a string which contains a unique identifier that can be looked
up using the instanceId property::

    <TestProto #3>
z<TestProto #%d>)r$   r/   s    r&   __repr__TestProto.__repr__N   s     !DOO#555r)   )r,   r"   r$   r!   N)__name__
__module____qualname____firstlineno____doc__r#   r'   r0   r4   r:   strr>   __static_attributes__ r)   r&   r   r   2   s.     M>. ,
6# 
6r)   r   c                        \ rS rSrS rS rSrg)SimpleSymmetricProtocol[   c                 "    U R                  SUS9$ N   hellohello)callRemoteStringr%   texts     r&   	sendHello!SimpleSymmetricProtocol.sendHello\   s    $$XT$::r)   c                 0    [         R                  " US   S9$ rL   r   Boxr%   boxs     r&   	amp_HELLO!SimpleSymmetricProtocol.amp_HELLO_   s    wwS]++r)   rG   N)r@   rA   rB   rC   rS   rZ   rF   rG   r)   r&   rI   rI   [   s    ;,r)   rI   c                       \ rS rSrSrSrg)UnfriendlyGreetingc   !Greeting was insufficiently kind.rG   Nr@   rA   rB   rC   rD   rF   rG   r)   r&   r]   r]   c       +r)   r]   c                       \ rS rSrSrSrg)DeathThreatg   r_   rG   Nr`   rG   r)   r&   rc   rc   g   ra   r)   rc   c                       \ rS rSrSrSrg)UnknownProtocolk   z&Asked to switch to the wrong protocol.rG   Nr`   rG   r)   r&   rf   rf   k   s    0r)   rf   c                   &    \ rS rSrS rS rS rSrg)TransportPeero   c                     g)Nr)   rG   )r%   dnameprotos       r&   retrieveTransportPeer.retriever   s    r)   c                 6    UR                   R                  5       $ r+   )r-   getPeer)r%   
notAStringrn   s      r&   fromStringProtoTransportPeer.fromStringProtou   s    &&((r)   c                     g r+   rG   )r%   rm   stringsobjectsrn   s        r&   toBoxTransportPeer.toBoxx   s    r)   rG   N)r@   rA   rB   rC   ro   rt   ry   rF   rG   r)   r&   ri   ri   o   s    )r)   ri   c                      \ rS rSr% SrS\R                  " 5       4S\R                  " SS94S\R                  " SS94S\	" SS94S\R                  " SS94S	\R                  " SS94S
\R                  " SS94/r
S\R                  " 5       4S\R                  " SS94/r\S0r\\\\   \4      \S'   \S0r\\\\   \4      \S'   Srg)Hello|   rM      optionalToptional   print   from	   mixedCase   dash-arg   underscore_arg
   UNFRIENDLYerrorss   DEADfatalErrorsrG   N)r@   rA   rB   rC   commandNamer   StringBooleanUnicoderi   	argumentsresponser]   r   r   r   r   	Exceptionr   __annotations__rc   r   rF   rG   r)   r&   r|   r|   |   s    K 
3::< 	ckk401	3;;-.	-./	szz401	cjj$/0	CJJ56I 3::<(8S[[$5O*PQH6H-5XFHT$y/5012X;F:PK$tI567Pr)   r|   c                   0    \ rS rSr\R
                  rSrSrg)NoAnswerHello   FrG   N)r@   rA   rB   rC   r|   r   requiresAnswerrF   rG   r)   r&   r   r      s    ##KNr)   r   c            	          \ rS rSrSrS\R                  " 5       4S\R                  " SS94S\R                  " SS94S\	" SS94S\R                  " SS94/r
S\R                  " 5       4S\R                  " SS94/r\S	0rS
rg)FutureHello   rM   r~   Tr   r   r   s   bonusr   rG   N)r@   rA   rB   rC   r   r   r   r   r   ri   r   r   r]   r   rF   rG   r)   r&   r   r      s    K 
3::< 	ckk401	3;;-.	-./	3::t,-	I 3::<(8S[[$5O*PQH -0Fr)   r   c                       \ rS rSrSrSrg)WTF   z#
An example of an invalid command.
rG   Nr`   rG   r)   r&   r   r          r)   r   c                       \ rS rSrSrSrSrg)BrokenReturn   zSAn example of a perfectly good command, but the handler is going to return
None...
s   broken_returnrG   N)r@   rA   rB   rC   rD   r   rF   rG   r)   r&   r   r      s     #Kr)   r   c                   T    \ rS rSrS\R
                  " 5       4/r\R                  rSr	g)Goodbye   s   goodbyerG   N)
r@   rA   rB   rC   r   r   r   QuitBoxresponseTyperF   rG   r)   r&   r   r      s    SZZ\*+H;;Lr)   r   c                       \ rS rSrSrSrg)WaitForever   s   wait_foreverrG   N)r@   rA   rB   rC   r   rF   rG   r)   r&   r   r      s    !Kr)   r   c                       \ rS rSrSrS\R                  " 5       4/rS\R                  " S\R                  " 5       4/5      4/r	Sr
g)GetList   s   getlists   length   body   xrG   N)r@   rA   rB   rC   r   r   Integerr   AmpListr   rF   rG   r)   r&   r   r      s>    KS[[]+,I#++ckkm'<&=>?@Hr)   r   c                       \ rS rSrSrS\R                  " 5       4S\R                  " S\R                  " 5       4/SS94/rS\R                  " 5       4/r	S	r
g
)DontRejectMe   s   dontrejectmes	   magicWords   list   nameTr   s   responserG   N)r@   rA   rB   rC   r   r   r   r   r   r   rF   rG   r)   r&   r   r      sR    !K	s{{}%	#++784HII ckkm,-Hr)   r   c                   <    \ rS rSrS\R
                  " 5       4/rSrg)SecuredPing   s   pingedrG   N)r@   rA   rB   rC   r   r   r   rF   rG   r)   r&   r   r      s    CKKM*+Hr)   r   c                   H    \ rS rSrSrS\R                  " 5       4/r\S0r	Sr
g)TestSwitchProto   s   Switch-Protor   s   UNKNOWNrG   N)r@   rA   rB   rC   r   r   r   r   rf   r   rF   rG   r)   r&   r   r      s*    !K 
#**,I z*Fr)   r   c                   *    \ rS rSrS rS rSrS rSrg)SingleUseFactory   c                 0    Xl         X R                   l        g r+   )rn   factory)r%   rn   s     r&   r'   SingleUseFactory.__init__   s    
!

r)   c                 ,    U R                   S so l         U$ r+   )rn   )r%   addrps      r&   buildProtocolSingleUseFactory.buildProtocol   s    

D:r)   Nc                     X l         g r+   )reasonFailedr%   	connectorr9   s      r&   clientConnectionFailed'SingleUseFactory.clientConnectionFailed   s    "r)   )rn   r   )	r@   rA   rB   rC   r'   r   r   r   rF   rG   r)   r&   r   r      s    " Lr)   r   s   gwebol nargoc                       \ rS rSrSrg)ThingIDontUnderstandError   rG   N)r@   rA   rB   rC   rF   rG   r)   r&   r   r      s    r)   r   c                   F    \ rS rSrSrS rS r\R                  \5        Sr	g)FactoryNotifier   Nc                     U R                   bR  X R                   l        [        U R                   S5      (       a&  U R                   R                  R	                  S 5        g g g )NonMade)r   theProtohasattrr   r7   r/   s    r&   r0   FactoryNotifier.connectionMade   sI    <<#$(LL!t||X..##,,T2 / $r)   c                 l    SSK Jn  UR                  U R                  5      (       d  [	        S5      eSS0$ )Nr   )ISSLTransportz+only send secure pings over secure channelspingedT)twisted.internet.interfacesr   
providedByr-   rc   )r%   r   s     r&   emitpongFactoryNotifier.emitpong   s1    =''77KLL$r)   rG   )
r@   rA   rB   rC   r   r0   r   r   	responderrF   rG   r)   r&   r   r      s!    G3  (#r)   r   c                   f   \ rS rSrSrSS jrS rS rSr     SS jr	\
R                  \	5        S r\R                  \5        SS	 jr\R                  \5        S
 r\R                  \5        S r\R                  \5        SS jrS r\R                  \5        S r\R                  \5        Srg)SimpleSymmetricCommandProtocoli  Nc                 N    [         R                  R                  U 5        Xl        g r+   )r   AMPr'   r!   )r%   r!   s     r&   r'   'SimpleSymmetricCommandProtocol.__init__
  s    $r)   c                 *    U R                  [        US9$ )NrN   
callRemoter|   rQ   s     r&   rS   (SimpleSymmetricCommandProtocol.sendHello  s    uD11r)   c                 *    U R                  [        XS9$ )NrO   Printr   )r%   rR   translations      r&   sendUnicodeHello/SimpleSymmetricCommandProtocol.sendUnicodeHello  s    uDDDr)   Fc                 &   X R                   R                  5       :X  d   eU[        :X  a
  [        5       eUR	                  S5      (       a  [        S5      eUS:X  a  [        S5      e[        US9nUb  UR                  [        US95        SU l	        U$ )Ns   fuckDon't be a dick.   dieaieeeeeeeeerN   )r   T)
r-   rr   THING_I_DONT_UNDERSTANDr   
startswithr]   rc   dictupdategreeted)	r%   rO   Fromr   r   	mixedCasedash_argunderscore_argresults	            r&   cmdHello'SimpleSymmetricCommandProtocol.cmdHello  s     ~~--////+++--G$$$%788F?m,,E"MM$U+,r)   c                      S[        SS9/U-  0$ )Nbodyr   )xr   )r%   lengths     r&   
cmdGetlist)SimpleSymmetricCommandProtocol.cmdGetlist/  s    f,--r)   c                 6    Uc  SnOSUS   S   -  n[        US9$ )Nlist omittedz%s acceptedr   rm   )r   r   )r%   	magicWordlistr   s       r&   okiwont&SimpleSymmetricCommandProtocol.okiwont4  s)    <%H$Q8HX&&r)   c                 N    [         R                  " 5       U l        U R                  $ r+   )r   Deferredwaitingr/   s    r&   	waitforit(SimpleSymmetricCommandProtocol.waitforit=  s    ~~'||r)   c                     [        SS9$ )N   everyone)goodbyer   r/   s    r&   saybye%SimpleSymmetricCommandProtocol.saybyeC  s    K((r)   c                    ^ U(       a  SnOSn[        U R                  [        5      mU R                  [        [        T5      US9R                  U4S j5      $ )Ns   no-proto
   test-proto)rm   c                    > T$ r+   rG   )ignr   s    r&   <lambda>ESimpleSymmetricCommandProtocol.switchToTestProtocol.<locals>.<lambda>P  s    !r)   )r   r!   SWITCH_CLIENT_DATAr   r   r   addCallback)r%   failrm   r   s      @r&   switchToTestProtocol3SimpleSymmetricCommandProtocol.switchToTestProtocolH  sN    D Ddoo'9:-a0t  

+m
$	%r)   c                 X    US:X  a  [        U R                  [        5      $ [        U5      eNr  )r   r!   SWITCH_SERVER_DATArf   r%   rm   s     r&   switchit'SimpleSymmetricCommandProtocol.switchitR  s'    = T__.@AAd##r)   c                     g r+   rG   r/   s    r&   	donothing(SimpleSymmetricCommandProtocol.donothingY      r)   )r   r!   r
  r+   NNNNN)F)r@   rA   rB   rC   
maybeLaterr'   rS   r   r   r   r|   r   r   r   r  r   r  r   r  r   r  r!  r   r$  r   rF   rG   r)   r&   r   r     s    J%2E G . 
OOH. j!' 7# )$) f%$
 h' 9%r)   r   c                   <    \ rS rSrS r\R                  \5        Srg) DeferredSymmetricCommandProtocoli_  c                     US:X  aE  [        U R                  [        5      U l        [        R
                  " 5       U l        U R                  $ g r  )r   r!   r  maybeLaterProtor   r	  r(  r   s     r&   r!  )DeferredSymmetricCommandProtocol.switchit`  s;    = #,T__>P#QD #nn.DO??" !r)   )r(  r,  N)r@   rA   rB   rC   r!  r   r   rF   rG   r)   r&   r*  r*  _  s    # h'r)   r*  c                   J    \ rS rSr     SS jr\R                  \5        Srg)BadNoAnswerCommandProtocolii  Nc                     g)zA
This responder does nothing and forgets to return a dictionary.
NrG   r%   rO   r   r   r   r   r   r   s           r&   badResponder'BadNoAnswerCommandProtocol.badResponderj  s    r)   rG   r'  )r@   rA   rB   rC   r2  r   r   rF   rG   r)   r&   r/  r/  i  s(    
  L)r)   r/  c                   J    \ rS rSr     SS jr\R                  \5        Srg)NoAnswerCommandProtocoli{  Nc                     [        US-   S9$ )Ns	   -noanswerrN   r   r1  s           r&   goodNoAnswerResponder-NoAnswerCommandProtocol.goodNoAnswerResponder|  s     %,.//r)   rG   r'  )r@   rA   rB   rC   r7  r   r   rF   rG   r)   r&   r5  r5  {  s)    
 
0 12r)   r5  c                 6    [         R                  " X/UQ70 UD6$ )z)Returns a 3-tuple: (client, server, pump))r   connectedServerAndClient)ServerClassClientClassakws       r&   r:  r:    s     ))+MQM"MMr)   c                       \ rS rSrSrS rSrg)TotallyDumbProtocoli  r)   c                 .    U =R                   U-  sl         g r+   )bufr%   r,   s     r&   r4    TotallyDumbProtocol.dataReceived  s    Dr)   rG   N)r@   rA   rB   rC   rB  r4   rF   rG   r)   r&   r@  r@    s    
Cr)   r@  c                        \ rS rSrS rS rSrg)
LiteralAmpi  c                     / U l         g r+   boxesr/   s    r&   r'   LiteralAmp.__init__  s	    
r)   c                 :    U R                   R                  U5        g r+   rI  r3   rX   s     r&   ampBoxReceivedLiteralAmp.ampBoxReceived  s    

#r)   rH  N)r@   rA   rB   rC   r'   rM  rF   rG   r)   r&   rF  rF    s    r)   rF  c                   *    \ rS rSrSrS rS rS rSrg)AmpBoxTestsi  za
Test a few essential properties of AMP boxes, mostly with respect to
serialization correctness.
c                     [         R                  " SS9nU R                  [        UR	                  5       5      [
        5        g)z(
Make sure that strs serialize to strs.
s   valuekeyN)r   AmpBoxassertEqualtype	serializer   r%   r=  s     r&   test_serializeStrAmpBoxTests.test_serializeStr  s-     JJ8$akkm,e4r)   c                 t    [         R                  " S0 SS0D6nU R                  [        UR                  5        g)zH
Verify that TypeError is raised when trying to serialize Unicode keys.
rS  valueNrG   r   rT  assertRaises	TypeErrorrW  rX  s     r&   test_serializeUnicodeKeyRaises*AmpBoxTests.test_serializeUnicodeKeyRaises  s.     JJ*%)*)Q[[1r)   c                 l    [         R                  " SS9nU R                  [        UR                  5        g)zJ
Verify that TypeError is raised when trying to serialize Unicode
values.
r\  rR  Nr]  rX  s     r&    test_serializeUnicodeValueRaises,AmpBoxTests.test_serializeUnicodeValueRaises  s&    
 JJ7#)Q[[1r)   rG   N)	r@   rA   rB   rC   rD   rY  r`  rc  rF   rG   r)   r&   rP  rP    s    
522r)   rP  c                   ,    \ rS rSrS rS rS rS rSrg)ParsingTestsi  c                    [         R                  " 5       nU R                  UR                  S5      5        U R	                  UR                  S5      5        U R                  [        UR                  S5        U R                  [        UR                  S5        U R                  [        UR                  S5        U R                  UR                  S5      S5        U R                  UR                  S5      S5        g)	zM
Verify that the Boolean parser parses 'True' and 'False', but nothing
else.
s   Trues   Falses   ninjas   trues   TRUETFN)	r   r   
assertTrue
fromStringassertFalser^  r_  rU  toString)r%   bs     r&   test_booleanValuesParsingTests.test_booleanValues  s    
 KKMW-.h/0)Q\\8<)Q\\7;)Q\\7;D)73E*H5r)   c                     [         R                  " U R                  5       5      n[        R                  " 5       nUR                  U5      nUR                  U5      nU R                  X5        U R                  X5        g)z<
Verify the 'Path' argument can parse and emit a file path.
N)	r   FilePathmktempr   Pathrk  ri  assertIsNotrU  )r%   fpr   svs        r&   test_pathValueRoundTrip$ParsingTests.test_pathValueRoundTrip  s[     t{{}-HHJJJrNLLOr)   c                     [         R                  " 5       nU R                  [         R                  UR                  [         R
                  " 5       5      $ )zS
Test that empty boxes raise an error; they aren't supposed to be sent
on purpose.
)r   r   r^  NoEmptyBoxesrM  rW   rX  s     r&   test_sillyEmptyThing!ParsingTests.test_sillyEmptyThing  s6    
 GGI  !1!113C3CSWWYOOr)   c           
      f   [        [        [        S9u  pnSnSnSnSnSnSn	Sn
U/XJ/XE/XF/XEXg/XVU/XHXY/XU//nU Hr  n[        R                  " 5       nUR	                  [        U5      5        UR                  U5        UR                  5         U R                  UR                  S	   U5        Mt     g
)zY
Verify that various kinds of data make it through the encode/parse
round-trip unharmed.
)r<  r;  )   simples   test)s   ceqs   : )s   crtests   test)s   lftests   hello
)s   newlines   test
one
two)s   newline2s   test
one
 two)r   s   blah

testtestN)
r:  rF  r   rW   r   r   _sendToflushrU  rI  )r%   cru  r   SIMPLECECRLFNEWLINENEWLINE2BODYTESTtestDatatestjbs                 r&   test_ParsingRoundTrip"ParsingTests.test_ParsingRoundTrip  s    
 +"

a &#$585 HLL RLb+w'	
 DBIId4j!JJqMGGIQWWR["- r)   rG   N)	r@   rA   rB   rC   rm  rw  r{  r  rF   rG   r)   r&   rf  rf    s    6	 P!.r)   rf  c                   $    \ rS rSrSrS rS rSrg)FakeLocatori  zN
This is a fake implementation of the interface implied by
L{CommandLocator}.
c                     0 U l         g)z>
Remember the given keyword arguments as a set of responders.
Ncommandsr/   s    r&   r'   FakeLocator.__init__  s     r)   c                      U R                   U   $ )zb
Look up and return a function passed as a keyword argument of the given
name to the constructor.
r  )r%   r   s     r&   locateResponderFakeLocator.locateResponder  s    
 }}[))r)   r  N)r@   rA   rB   rC   rD   r'   r  rF   rG   r)   r&   r  r    s    
*r)   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	
FakeSenderi  zP
This is a fake implementation of the 'box sender' interface implied by
L{AMP}.
c                 .    / U l         / U l        SU l        g)zV
Create a fake sender and initialize the list of received boxes and
unhandled errors.
r   N)	sentBoxesunhandledErrorsexpectedErrorsr/   s    r&   r'   FakeSender.__init__  s    
 !r)   c                 .    U =R                   S-  sl         g)z2
Expect one error, so that the test doesn't fail.
r   N)r  r/   s    r&   expectErrorFakeSender.expectError(  s     	q r)   c                 :    U R                   R                  U5        g)z&
Accept a box, but don't do anything.
N)r  r3   rX   s     r&   sendBoxFakeSender.sendBox.  s     	c"r)   c                     U =R                   S-  sl         U R                   S:  a  UR                  5         gU R                  R                  U5        g)zG
Deal with failures by instantly re-raising them for easier debugging.
r   r   N)r  raiseExceptionr  r3   )r%   failures     r&   unhandledErrorFakeSender.unhandledError4  sC     	q """$  ''0r)   )r  r  r  N)
r@   rA   rB   rC   rD   r'   r  r  r  rF   rG   r)   r&   r  r    s    
 !#1r)   r  c                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)CommandDispatchTestsi?  a  
The AMP CommandDispatcher class dispatches converts AMP boxes into commands
and responses using Command.responder decorator.

Note: Originally, AMP's factoring was such that many tests for this
functionality are now implemented as full round-trip tests in L{AMPTests}.
Future tests should be written at this level instead, to ensure API
compatibility and to provide more granular, readable units of test
coverage.
c                     [        5       U l        [        5       U l        [        R
                  " U R                  5      U l        U R                  R                  U R                  5        g)z
Create a dispatcher to use.
N)r  locatorr  senderr   BoxDispatcher
dispatcherstartReceivingBoxesr/   s    r&   setUpCommandDispatchTests.setUpK  sB     #} l++DLL9++DKK8r)   c                    ^ / mU4S jn[         R                  " SSSS9nXR                  R                  S'   U R                  R                  U5        U R                  TU/5        g)z}
L{CommandDispatcher.ampBoxReceived} should locate the appropriate
command in its responder lookup, based on the '_ask' key.
c                 V   > TR                  U 5        [        R                  " SS05      $ )NrO   r  )r3   r   rW   )rY   receiveds    r&   thunk4CommandDispatchTests.test_receivedAsk.<locals>.thunk[  s#    OOC 77GY/00r)   rO   test-command-idworld_command_askrO   N)r   rW   r  r  r  rM  rU  )r%   r  inputr  s      @r&   test_receivedAsk%CommandDispatchTests.test_receivedAskT  sZ    
 	1 /@P).g&&&u-E7+r)   c                 Z   [        S5      nU R                  R                  5         U R                  R	                  [        U5      5        U R                  [        U R                  R                  5      S5        U R                  U R                  R                  S   R                  U5        g)za
L{CommandDispatcher} should relay its unhandled errors in responding to
boxes to its boxSender.
zsomething went wrong, oh nor   r   N)
RuntimeErrorr  r  r  r  r   rU  lenr  r\  )r%   errs     r&   test_sendUnhandledError,CommandDispatchTests.test_sendUnhandledErrord  sy    
 89!&&ws|4T[[8891=44Q7==sCr)   c                   ^ [        S5      mU4S jnXR                  R                  S'   [        R                  " SSSS9nU R
                  R                  5         U R                  R                  U5        U R                  [        U R
                  R                  5      S5        U R                  U R
                  R                  S   R                  T5        g	)
zX
Errors during serialization ought to be relayed to the sender's
unhandledError method.
zsomething undefined went wrongc                 J   >  " U4S jS[         R                  5      nU" 5       $ )Nc                   "   > \ rS rSrU 4S jrSrg)WCommandDispatchTests.test_unhandledSerializationError.<locals>.thunk.<locals>.BrokenBoxiw  c                    > Ter+   rG   )r%   rn   r  s     r&   r  _CommandDispatchTests.test_unhandledSerializationError.<locals>.thunk.<locals>.BrokenBox._sendTox  s    Ir)   rG   N)r@   rA   rB   rC   r  rF   )r  s   r&   	BrokenBoxr  w  s     r)   r  rV   )r   r  r  s     r&   r  DCommandDispatchTests.test_unhandledSerializationError.<locals>.thunkv  s    CGG  ;r)   rO   r  r  r  r   r   N)r  r  r  r   rW   r  r  r  rM  rU  r  r  r\  )r%   r  r  r  s      @r&    test_unhandledSerializationError5CommandDispatchTests.test_unhandledSerializationErroro  s    
 ;<	 */g&/@P!&&u-T[[8891=44Q7==sCr)   c           	         U R                   R                  [        SS9nU R                  U R                  R
                  [        R                  " SSSS9/5        / nUR                  UR                  5        U R                  U/ 5        U R                   R                  [        R                  " SSSS.5      5        U R                  U[        SS	S
9/5        g)a+  
L{CommandDispatcher.callRemote} should emit a properly formatted '_ask'
box to its boxSender and record an outstanding L{Deferred}.  When a
corresponding '_answer' packet is received, the L{Deferred} should be
fired, and the results translated via the given L{Command}'s response
de-serialization.
   worldrN   rM      1r     yay   ignoredrM   r   s   _answerignoredr   N)r  r   r|   rU  r  r  r   rT  r  r3   rM  r   )r%   Danswerss      r&   test_callRemote$CommandDispatchTests.test_callRemote  s     OO&&uH&=KK!!ZZHEF	
 	gnn%"%&&JJ&JDQR	
 	4fI#F"GHr)   c                 0   / nUR                  UR                  5        U R                  S[        U R                  R
                  5      5        U R                  U R                  R
                  S   R                  [        5        U R                  S/U5        g)zh
Verify that C{callResult} completes with a L{None} result and that an
unhandled error has been logged.
r   r   N)	addBothr3   rU  r  r  r  assertIsInstancer\  ZeroDivisionError)r%   
callResultfinalResults      r&   _localCallbackErrorLoggingTest3CommandDispatchTests._localCallbackErrorLoggingTest  st    
 ;--.C ; ;<=dkk99!<BBDUV$-r)   c                     U R                   R                  5         U R                  R                  [        SS9nUR                  S 5        U R                  R                  [        R                  " SSSS.5      5        U R                  U5        g)	z
If the last callback on the L{Deferred} returned by C{callRemote} (added
by application code calling C{callRemote}) fails, the failure is passed
to the sender's C{unhandledError} method.
r  rN   c                     SS-  $ Nr   r   rG   r   s    r&   r  VCommandDispatchTests.test_callRemoteSuccessLocalCallbackErrorLogging.<locals>.<lambda>  s    a1fr)   r  r  r  r  N)
r  r  r  r   r|   r  rM  r   rT  r  r%   r  s     r&   /test_callRemoteSuccessLocalCallbackErrorLoggingDCommandDispatchTests.test_callRemoteSuccessLocalCallbackErrorLogging  sq     	!__//X/F
45&&JJ&JDQR	
 	++J7r)   c                     U R                   R                  5         U R                  R                  [        SS9nUR                  S 5        U R                  R                  [        R                  " SSSS.5      5        U R                  U5        g)	z
Like L{test_callRemoteSuccessLocalCallbackErrorLogging}, but for the
case where the L{Deferred} returned by C{callRemote} fails.
r  rN   c                     SS-  $ r  rG   r  s    r&   r  TCommandDispatchTests.test_callRemoteErrorLocalCallbackErrorLogging.<locals>.<lambda>  s    Q!Vr)   r  s   bugss   stuff)   _error   _error_code   _error_descriptionN)
r  r  r  r   r|   
addErrbackrM  r   rT  r  r  s     r&   -test_callRemoteErrorLocalCallbackErrorLoggingBCommandDispatchTests.test_callRemoteErrorLocalCallbackErrorLogging  sx    
 	!__//X/F
34&&JJ#$++3	
 	++J7r)   )r  r  r  N)r@   rA   rB   rC   rD   r  r  r  r  r  r  r  r  rF   rG   r)   r&   r  r  ?  s2    	9, 	DD*I*.8"8r)   r  c                       \ rS rSrSrSrS\R                  " 5       4S\R                  " 5       4/r	S\R                  " 5       4/r
Srg)	SimpleGreetingi  zF
A very simple greeting command that uses a few basic argument types.
r~  s   greetings   cookies
   cookieplusrG   N)r@   rA   rB   rC   rD   r   r   r   r   r   r   rF   rG   r)   r&   r  r    sA     Ks{{}-	3;;=/IJI./Hr)   r  c                   F    \ rS rSrSrS rS r\R                  \5      rSr	g)TestLocatori  zA
A locator which implements a responder to the 'simple' command.
c                     / U l         g r+   	greetingsr/   s    r&   r'   TestLocator.__init__  s	    r)   c                 R    U R                   R                  X45        [        US-   S9$ N   
cookieplusr  r3   r   r%   greetingcookies      r&   greetingResponderTestLocator.greetingResponder  &    x01vz**r)   r  N)
r@   rA   rB   rC   rD   r'   r  r  r   rF   rG   r)   r&   r  r    s%    + '001BCr)   r  c                   @    \ rS rSrSrS r\R                  \5      rSrg)OverridingLocatori  zB
A locator which overrides the responder to the 'simple' command.
c                 R    U R                   R                  X45        [        US-   S9$ )zF
Return a different cookieplus than L{TestLocator.greetingResponder}.
   r   r  r  s      r&   r  #OverridingLocator.greetingResponder  s(     	x01vz**r)   rG   N)	r@   rA   rB   rC   rD   r  r  r   rF   rG   r)   r&   r
  r
    s     + '001BCr)   r
  c                       \ rS rSrSrSrg)InheritingLocatori  zF
This locator should inherit the responder from L{OverridingLocator}.
rG   Nr`   rG   r)   r&   r  r    r   r)   r  c                   V   ^  \ rS rSrS rU 4S jrS r\R                  \5      rSr	U =r
$ )OverrideLocatorAMPi  c                     [         R                  R                  U 5        [        5       U l        SU R                  0U l        / U l        g Ns   custom)r   r   r'   objectcustomResponderexpectationsr  r/   s    r&   r'   OverrideLocatorAMP.__init__  s8    %x&(<(<=r)   c                 b   > XR                   ;   a  U R                   U   nU$ [        TU ]	  U5      $ )z2
Override the deprecated lookupFunction function.
)r  superlookupFunction)r%   rm   r   	__class__s      r&   r  !OverrideLocatorAMP.lookupFunction  s6     $$$&&t,FM7)$//r)   c                 R    U R                   R                  X45        [        US-   S9$ r  r  r  s      r&   r  $OverrideLocatorAMP.greetingResponder  r  r)   )r  r  r  )r@   rA   rB   rC   r'   r  r  r  r   rF   __classcell__)r  s   @r&   r  r    s&    0+ '001BCr)   r  c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)CommandLocatorTestsi  z
The CommandLocator should enable users to specify responders to commands as
functions that take structured objects, annotated with metadata.
c                    ^ ^ U" 5       nUR                  S5      nU" [        R                  " SSS95      nUU 4S jnUR                  U5      $ )z
Check that a locator of type C{locatorClass} finds a responder
for command named I{simple} and that the found responder answers
with the C{expected} result to a C{SimpleGreeting<"ni hao", 5>}
command.
r~     ni hao   5r  r  c                 V   > TR                  U [        R                  " ST4-  S95        g )Ns   %dr   rU  r   rT  )valuesexpectedr%   s    r&   done6CommandLocatorTests._checkSimpleGreeting.<locals>.done%  s#    VSZZ5H;;N%OPr)   )r  r   rW   r  )r%   locatorClassr)  r  responderCallabler   r*  s   ` `    r&   _checkSimpleGreeting(CommandLocatorTests._checkSimpleGreeting  sK     .#33I>"377Id#KL	Q !!$''r)   c                 .    U R                  [        S5      $ )z
A method on a L{CommandLocator} subclass decorated with a L{Command}
subclass's L{responder} decorator should be returned from
locateResponder, wrapped in logic to serialize and deserialize its
arguments.
   )r.  r  r/   s    r&   test_responderDecorator+CommandLocatorTests.test_responderDecorator*  s     ((a88r)   c                 .    U R                  [        S5      $ )z
L{CommandLocator} subclasses can override a responder inherited from
a base class by using the L{Command.responder} decorator to register
a new responder method.
	   )r.  r
  r/   s    r&   test_responderOverriding,CommandLocatorTests.test_responderOverriding3  s     (():A>>r)   c                 .    U R                  [        S5      $ )zr
Responder lookup follows the same rules as normal method lookup
rules, particularly with respect to inheritance.
r5  )r.  r  r/   s    r&   test_responderInheritance-CommandLocatorTests.test_responderInheritance;  s    
 (():A>>r)   c                 6  ^ ^ [        5       mT R                  [        S[        U4S j5      nT R	                  TR
                  U5        T R                  [        S[        U4S j5      nU" [        R                  " SSS95      nU 4S jnUR                  U5      $ )a  
Subclasses which override locateResponder under its old name,
lookupFunction, should have the override invoked instead.  (This tests
an AMP subclass, because in the version of the code that could invoke
this deprecated code path, there was no L{CommandLocator}.)
z-Override locateResponder, not lookupFunction.c                  &   > T R                  S5      $ r  r  r  s   r&   r  KCommandLocatorTests.test_lookupFunctionDeprecatedOverride.<locals>.<lambda>N      G++I6r)   c                  &   > T R                  S5      $ Nr~  r=  r>  s   r&   r  r?  V  r@  r)   r#  r$  r%  c                 N   > TR                  U [        R                  " SS95        g N   8r   r'  r(  r%   s    r&   r*  GCommandLocatorTests.test_lookupFunctionDeprecatedOverride.<locals>.doneZ      VSZZ4%@Ar)   )	r  assertWarnsPendingDeprecationWarning__file__rU  r  r   rW   r  )r%   customResponderObjectnormalResponderObjectr   r*  r  s   `    @r&   %test_lookupFunctionDeprecatedOverride9CommandLocatorTests.test_lookupFunctionDeprecatedOverrideB  s     %& $ 0 0%;6	!
 	002GH $ 0 0%;6	!
 'sww	$'OP	B !!$''r)   c                    ^ ^ [        5       mT R                  [        S[        U4S j5      nU" [        R
                  " SSS95      nU 4S jnUR                  U5      $ )zx
Invoking locateResponder under its old name, lookupFunction, should
emit a deprecation warning, but do the same thing.
z)Call locateResponder, not lookupFunction.c                  &   > T R                  S5      $ rB  )r  r>  s   r&   r  ICommandLocatorTests.test_lookupFunctionDeprecatedInvoke.<locals>.<lambda>i  s    G**95r)   r#  r$  r%  c                 N   > TR                  U [        R                  " SS95        g rD  r'  rF  s    r&   r*  ECommandLocatorTests.test_lookupFunctionDeprecatedInvoke.<locals>.donem  rH  r)   )r  rI  rJ  rK  r   rW   r  )r%   r-  r   r*  r  s   `   @r&   #test_lookupFunctionDeprecatedInvoke7CommandLocatorTests.test_lookupFunctionDeprecatedInvoke_  sY    
 - ,,%75	
 #377Id#KL	B !!$''r)   rG   N)r@   rA   rB   rC   rD   r.  r2  r6  r9  rN  rU  rF   rG   r)   r&   r!  r!    s%    
( 9??(:(r)   r!  s   Success!s   No, really.  Success.c                       \ rS rSrSrS rS rS rSrS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)BinaryProtocolTestsiw  z
Tests for L{amp.BinaryBoxProtocol}.

@ivar _boxSender: After C{startReceivingBoxes} is called, the L{IBoxSender}
    which was passed to it.
c                      / U l         / U l        g)zb
Keep track of all boxes received by this test in its capacity as an
L{IBoxReceiver} implementor.
N)rI  r,   r/   s    r&   r  BinaryProtocolTests.setUp  s    
 
	r)   c                     Xl         g)zU
Implement L{IBoxReceiver.startReceivingBoxes} to just remember the
value passed in.
N)
_boxSenderr%   r  s     r&   r  'BinaryProtocolTests.startReceivingBoxes  s	    
 !r)   c                 :    U R                   R                  U5        g)z%
A box was received by the protocol.
NrL  rX   s     r&   rM  "BinaryProtocolTests.ampBoxReceived  s     	

#r)   Nc                     Xl         g)z4
Record the reason that we stopped receiving boxes.
N)
stopReasonr8   s     r&   stopReceivingBoxes&BinaryProtocolTests.stopReceivingBoxes  s	     !r)   c                     g)Nzno peerrG   r/   s    r&   rr   BinaryProtocolTests.getPeer      r)   c                     g)Nzno hostrG   r/   s    r&   getHostBinaryProtocolTests.getHost  rg  r)   c                 f    U R                  U[        5        U R                  R                  U5        g r+   )r  r   r,   r3   rC  s     r&   r.   BinaryProtocolTests.write  s$    dE*		r)   c                     [         R                  " U 5      nUR                  " S5        U R                  U R                  U5        g)z
When L{amp.BinaryBoxProtocol} is connected to a transport, it calls
C{startReceivingBoxes} on its L{IBoxReceiver} with itself as the
L{IBoxSender} parameter.
N)r   BinaryBoxProtocolmakeConnectionassertIsr\  r%   r   s     r&   test_startReceivingBoxes,BinaryProtocolTests.test_startReceivingBoxes  s4     ((.%doox0r)   c                      " S S5      n[        5       n[        R                  " U" 5       5      nUR                  " U5        U R	                  UR                  5       S5        g)z
The L{IBoxReceiver} which is started when L{amp.BinaryBoxProtocol} is
connected to a transport can call C{sendBox} on the L{IBoxSender}
passed to it before C{startReceivingBoxes} returns and have that box
sent.
c                       \ rS rSrS rSrg)[BinaryProtocolTests.test_sendBoxInStartReceivingBoxes.<locals>.SynchronouslySendingReceiveri  c                 R    UR                  [        R                  " SS05      5        g )N   foo   bar)r  r   rW   r]  s     r&   r  oBinaryProtocolTests.test_sendBoxInStartReceivingBoxes.<locals>.SynchronouslySendingReceiver.startReceivingBoxes  s    sww'789r)   rG   N)r@   rA   rB   rC   r  rF   rG   r)   r&   SynchronouslySendingReceiverrv    s    :r)   r{  s    foo bar  N)r   r   rn  ro  rU  r\  )r%   r{  r-   r   s       r&   !test_sendBoxInStartReceivingBoxes5BinaryProtocolTests.test_sendBoxInStartReceivingBoxes  sP    	: 	: $%	(()E)GH	**,MNr)   c                     [         R                  " U 5      nUR                  S5        UR                  S5        UR                  S5        U R                  U R                  [         R
                  " SS9/5        g)z
When a binary box protocol receives:
    * a key
    * a value
    * an empty string
it should emit a box and send it to its boxReceiver.
rM   r  r)   rN   N)r   rn  stringReceivedrU  rI  rT  rX  s     r&   test_receiveBoxStateMachine/BinaryProtocolTests.test_receiveBoxStateMachine  s^     !!$'	"	"	cjjx&@%ABr)   c                     [        5       n[        R                  " U 5      nUR                  " U5        UR                  " S5        U R                  UR                  5        g)zx
L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
the first a key it receives is larger than 255.
    N)r   r   rn  ro  r4   rh  disconnectingr%   r-   r   s      r&   $test_firstBoxFirstKeyExcessiveLength8BinaryProtocolTests.test_firstBoxFirstKeyExcessiveLength  sJ    
 $%	((.	*k*	//0r)   c                    [        5       n[        R                  " U 5      nUR                  " U5        UR                  " S5        U R                  UR                  5        UR                  " S5        U R                  UR                  5        g)z
L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
a subsequent key in the first box it receives is larger than 255.
s    k vr  Nr   r   rn  ro  r4   rj  r  rh  r  s      r&   )test_firstBoxSubsequentKeyExcessiveLength=BinaryProtocolTests.test_firstBoxSubsequentKeyExcessiveLength  sm    
 $%	((.	*34001k*	//0r)   c                    [        5       n[        R                  " U 5      nUR                  " U5        UR                  " S5        U R                  UR                  5        UR                  " S5        U R                  UR                  5        g)z
L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
the first key in a subsequent box it receives is larger than 255.
s    k v  r  Nr  r  s      r&   )test_subsequentBoxFirstKeyExcessiveLength=BinaryProtocolTests.test_subsequentBoxFirstKeyExcessiveLength  sm    
 $%	((.	*;<001k*	//0r)   c                    [         R                  " U 5      nUR                  " [        5       5        UR                  " S5        UR
                  " [        [        R                  " S5      5      5        U R                  R                  [         R                  5        U R                  U R                  R                  R                  5        U R                  U R                  R                  R                   5        U R#                  U R                  R                  R                  5        U R#                  U R                  R                  R$                  5        g)z
If L{amp.BinaryBoxProtocol} disconnects because it received a key
length prefix which was too large, the L{IBoxReceiver}'s
C{stopReceivingBoxes} method is called with a L{TooLong} failure.
r  zsimulated connection doneN)r   rn  ro  r   r4   r:   r   r   ConnectionDonerb  trapTooLongrh  r\  isKeyrj  isLocalassertIsNonekeyNamerq  s     r&   test_excessiveKeyFailure,BinaryProtocolTests.test_excessiveKeyFailure  s     ((. 12k*E(()DEF	
 	S[[)--334..667$////556$////778r)   c                 B   [        5       n[        R                  " U 5      nUR                  " U5        UR                  " [        [        S5      5      5        U R                  S[        U R                  [        5      5      5        U R                  UR                  5        g)zf
L{amp.BinaryBoxProtocol.unhandledError} logs the failure passed to it
and disconnects its transport.

Fake errorr   N)r   r   rn  ro  r  r   r  rU  r  flushLoggedErrorsrh  r  r  s      r&    test_unhandledErrorWithTransport4BinaryProtocolTests.test_unhandledErrorWithTransport  st    
 $%	((.	*\(B CDC 6 6| DEF	//0r)   c                 P   [         R                  " U 5      nUR                  " [        5       5        UR                  " [        [        S5      5      5        UR                  " [        [        S5      5      5        U R                  S[        U R                  [        5      5      5        g)zh
L{amp.BinaryBoxProtocol.unhandledError} completes without error when
there is no associated transport.
	Simulatedr  r   N)r   rn  ro  r   r:   r   r   r  r  rU  r  r  rq  s     r&   #test_unhandledErrorWithoutTransport7BinaryProtocolTests.test_unhandledErrorWithoutTransport  su    
 ((. 12	+(> ?@\(B CDC 6 6| DEFr)   c                    [         R                  " U 5      nUR                  [         R                  " SSS.5      R	                  5       5        U R                  U R                  [         R                  " SSS.5      /5        g)zy
When a binary box protocol receives the serialized form of an AMP box,
it should emit a similar box to its boxReceiver.
	   valueTests   anotherValue)   testKeys
   anotherKeyN)r   rn  r4   rW   rW  rU  rI  rX  s     r&   test_receiveBoxData'BinaryProtocolTests.test_receiveBoxData  sf    
 !!$'	GG)/Jik	

 	JJWW,OPQ	
r)   c                 ~   SnSU-  n[        5       n[        R                  " U 5      nUR                  " U5        UR                  " [        R
                  " SU05      R                  5       5        U R                  U R                  [        R
                  " SU05      /5        U R                  UR                  5        g)zh
An L{amp.BinaryBoxProtocol} can receive serialized AMP boxes with
values of up to (2 ** 16 - 1) bytes.
i  r   kN)r   r   rn  ro  r4   rW   rW  rU  rI  rj  r  )r%   r   r\  r-   r   s        r&   test_receiveLongerBoxData-BinaryProtocolTests.test_receiveLongerBoxData.  s    
 v#%	((.	*cggsEl3==?@cggsEl&;%<=001r)   c                 :   [         R                  " U 5      nUR                  U 5        [         R                  " SSS.5      nUR                  U 5        UR	                  U5        U R                  SR                  U R                  5      UR                  5       5        g)zk
When a binary box protocol sends a box, it should emit the serialized
bytes of that box to its transport.
r  rM   )r  s   someDatar)   N)	r   rn  ro  rW   r  rU  joinr,   rW  )r%   r=  aBoxs      r&   test_sendBox BinaryProtocolTests.test_sendBox<  sr    
 !!$'	wwLxHI				$$)),dnn.>?r)   c                     [         R                  " U 5      nUR                  U 5        [        [	        5       5      nUR                  U5        U R                  U R                  U5        g)zy
When a binary box protocol loses its connection, it should notify its
box receiver that it has stopped receiving boxes.
N)r   rn  ro  r   r  r:   rp  rb  )r%   r=  connectionFailures      r&   #test_connectionLostStopSendingBoxes7BinaryProtocolTests.test_connectionLostStopSendingBoxesH  sQ    
 !!$'	#LN3	*+doo'89r)   c                   ^^^ [        SS5      mU m " UUU4S jS5      n[        R                  " U" 5       5      m[        R                  " SSS.5      nTR	                  U 5        UR                  5       S-   nTR                  U5        U R                  TR                  U 5        U R                  S	R                  TR                  5      S5        U R                  U R                  S/5        TR                  S
5        U R                  S	R                  TR                  5      S5        U R                  [        R                  TR                  U5        g)z
L{BinaryBoxProtocol} has the capacity to switch to a different protocol
on a box boundary.  When a protocol is in the process of switching, it
cannot receive traffic.
N   outgoing datac                   0   > \ rS rSrSrS rU UU4S jrSrg)@BinaryProtocolTests.test_protocolSwitch.<locals>.SwitchyReceiveri\  Fc                     g r+   rG   r]  s     r&   r  TBinaryProtocolTests.test_protocolSwitch.<locals>.SwitchyReceiver.startReceivingBoxes_  s    r)   c                    > TR                  U R                  S5        SU l        TR                  5         TR                  T5        g )NzShould only receive one box!T)rj  switched_lockForSwitch	_switchTo)r%   rY   r=  
otherProtor  s     r&   rM  OBinaryProtocolTests.test_protocolSwitch.<locals>.SwitchyReceiver.ampBoxReceivedb  s7      0NO $  "J'r)   )r  N)r@   rA   rB   rC   r  r  rM  rF   )r=  r  r  s   r&   SwitchyReceiverr  \  s    H( (r)   r  s   lots   data)s   includes   ofs     Hello, world!r)   s	   more datas     Hello, world!more data)r   r   rn  rW   ro  rW  r4   rp  r-   rU  r  r,   r^  ProtocolSwitchedr  )r%   r  	anyOldBoxmoreThanOneBoxr=  r  r  s       @@@r&   test_protocolSwitch'BinaryProtocolTests.test_protocolSwitchS  s	    t%56

	( 
	( !!/"34GGAB		 #,,.1II	~&j**D1*//24LM%5$67	|$*//24UV#..		9Er)   c                     [         R                  " U 5      nUR                  U 5        [        SS5      nUR	                  U5        U R                  UR                  / 5        g)z
After switching to a different protocol, if no extra bytes beyond
the switch box were delivered, an empty string is not passed to the
switched protocol's C{dataReceived} method.
Nr)   )r   rn  ro  r   r  rU  r,   )r%   r=  r  s      r&   test_protocolSwitchEmptyBuffer2BinaryProtocolTests.test_protocolSwitchEmptyBufferw  sN     !!$'	tS)
	J"-r)   c                 Z   [         R                  " U 5      nUR                  U 5        [         R                  " SS05      nUR	                  5         U R                  [         R                  UR                  U5        UR                  5         UR                  U5        U R                  SR                  U R                  5      UR                  5       5        UR	                  5         [        SS5      nUR                  U5        U R                  [         R                  UR                  5        g)a  
In order to make sure the protocol never gets any invalid data sent
into the middle of a box, it must be locked for switching before it is
switched.  It can only be unlocked if the switch failed, and attempting
to send a box while it is locked should raise an exception.
s   somer  r)   Nr  )r   rn  ro  rW   r  r^  r  r  _unlockFromSwitchrU  r  r,   rW  r   r  )r%   r=  	sampleBoxr  s       r&    test_protocolSwitchInvalidStates4BinaryProtocolTests.test_protocolSwitchInvalidStates  s     !!$'	GGWg./		#..		9E				)$)),i.A.A.CD	t%56
	J#..0C0CDr)   c                 T    " S S[         R                  5      nU" 5       n[        R                  " U 5      nUR	                  U 5        UR                  5         UR                  U5        [        [        5       5      nUR                  U5        U R                  UR                  U5        g)zW
When the protocol is switched, it should notify its nested protocol of
disconnection.
c                       \ rS rSrSrS rSrg)DBinaryProtocolTests.test_protocolSwitchLoseConnection.<locals>.Loseri  Nc                     Xl         g r+   r9   r8   s     r&   r:   SBinaryProtocolTests.test_protocolSwitchLoseConnection.<locals>.Loser.connectionLost      $r)   r  )r@   rA   rB   rC   r9   r:   rF   rG   r)   r&   Loserr        F%r)   r  N)r   Protocolr   rn  ro  r  r  r   r  r:   rU  r9   )r%   r  connectionLoserr=  r  s        r&   !test_protocolSwitchLoseConnection5BinaryProtocolTests.test_protocolSwitchLoseConnection  s    	%H%% 	%  '!!$'			O$#LN3	*+//1BCr)   c                 `    " S S5      n[         R                  " U 5      n[        R                  " 5       nU" 5       nUR	                  U 5        UR                  5         UR                  X45        [        [        5       5      nUR                  U5        U R                  UR                  U5        g)zf
When the protocol is switched, it should notify its nested client
protocol factory of disconnection.
c                       \ rS rSrSrS rSrg)PBinaryProtocolTests.test_protocolSwitchLoseClientConnection.<locals>.ClientLoseri  Nc                     X l         g r+   r  r   s      r&   clientConnectionLosteBinaryProtocolTests.test_protocolSwitchLoseClientConnection.<locals>.ClientLoser.clientConnectionLost  r  r)   r  )r@   rA   rB   rC   r9   r  rF   rG   r)   r&   ClientLoserr    r  r)   r  N)r   rn  r   r  ro  r  r  r   r  r:   rU  r9   )r%   r  r=  r  clientLoserr  s         r&   'test_protocolSwitchLoseClientConnection;BinaryProtocolTests.test_protocolSwitchLoseClientConnection  s    	% 	% !!$'"++-!m			O1#LN3	*+++->?r)   )r\  rI  r,   rb  ) r@   rA   rB   rC   rD   r  r  rM  rb  rc  rr   ri  r.   rr  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rF   rG   r)   r&   rX  rX  w  s    ! J!1O"C	1119$
1	G
 2
@	:"FH
.E(D*@r)   rX  c                       \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS r\" \S5      S 5       rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S  r#\$S!S!4S" jr%S# r&\$4S$ jr'S% r(S& r)S' r*S( r+S) r,S*r-g+),AMPTestsi  c                    [         R                  [         R                  4[         R                  [         R                  4[         R
                  [         R                  4[         R
                  [         R                  4[         R                  [         R                  4[         R                  [         R                  4[         R
                  [         R                  44 H,  u  pU R                  UR                  U5      U SU S35        M.     g)zf
The classes in the amp module ought to implement the interfaces that
are declared for their benefit.
z does not implements()N)r   
IBoxSenderrn  IBoxReceiverr  IResponderLocatorCommandLocatorSimpleStringLocatorr   rh  implementedBy)r%   	interfaceimplementations      r&   test_interfaceDeclarations#AMPTests.test_interfaceDeclarations  s     ^^S223s001""C$6$67""C$;$;<^^SWW%sww'""CGG,*
%I OO''7!""7	{!D*
r)   c                     [        5       u  pn/ nSnUR                  U5      R                  UR                  5        UR	                  5         U R                  US   S   U5        g)zp
Verify that a simple command can be sent and its response received with
the simple low-level string-based API.
r  r   rM   Nr:  rS   r  r3   r  rU  r%   r  ru  r   LHELLOs         r&   test_helloWorldAMPTests.test_helloWorld  V    
 +,a	E&&qxx0		1h/r)   c                     [        5       u  pn/ nSnUR                  U5      R                  UR                  5        UR	                  5         U R                  US   S   U5        g)ze
Verify that mixed-case, underscored and dashed arguments are mapped to
their python names properly.
r  r   rM   Nr  r  s         r&   test_wireFormatRoundTrip!AMPTests.test_wireFormatRoundTrip  r  r)   c                    [        [        [        S9u  pn/ nSnSnUR                  XV5      R                  UR                  5        UR                  5         U R                  US   S   U5        U R                  US   S   U5        g)z;
Verify that unicode arguments can be encoded and decoded.
r;  r<  r  u   worሴldr   rO   r   N)r:  r   r   r  r3   r  rU  )r%   r  ru  r   r  r  HELLO_UNICODEs          r&   test_helloWorldUnicodeAMPTests.test_helloWorldUnicode  s     +66
a %	50<<QXXF		1g.1g6r)   c                 `    [        5       u  pnUR                  SSS9nU R                  U5        g)zU
L{BoxDispatcher.callRemoteString} returns L{None} if C{requiresAnswer}
is C{False}.
   WTFF)r   N)r:  rP   r  )r%   r  ru  r   rets        r&   (test_callRemoteStringRequiresAnswerFalse1AMPTests.test_callRemoteStringRequiresAnswerFalse  s3    
 +,a   >#r)   c                    [        5       u  pn/ nS nUR                  S5      R                  U5      R                  UR                  5        UR                  5         U R                  UR                  5       S5        SnUR                  U5      R                  UR                  5        UR                  5         U R                  US   S   U5        g)zz
Verify that unknown commands using low-level APIs will be rejected with an
error, but will NOT terminate the connection.
c                 B    U R                  [        R                  5        gz"
You can't propagate the error...
OKr  r   UnhandledCommandes    r&   clearAndAdd4AMPTests.test_unknownCommandLow.<locals>.clearAndAdd       FF3''(r)   r  r  r  r   rM   N)	r:  rP   r  r  r3   r  rU  poprS   r%   r  ru  r   r  r  r  s          r&   test_unknownCommandLowAMPTests.test_unknownCommandLow  s    
 +,a	 	
6"--k:FFqxxP		$'	E&&qxx0		1h/r)   c                    [        5       u  pn/ nS nUR                  [        5      R                  U5      R	                  UR
                  5        UR                  5         U R                  UR                  5       S5        SnUR                  U5      R	                  UR
                  5        UR                  5         U R                  US   S   U5        g)z{
Verify that unknown commands using high-level APIs will be rejected with an
error, but will NOT terminate the connection.
c                 B    U R                  [        R                  5        gr  r  r  s    r&   r  5AMPTests.test_unknownCommandHigh.<locals>.clearAndAdd'  r  r)   r  r  r   rM   N)
r:  r   r   r  r  r3   r  rU  r  rS   r  s          r&   test_unknownCommandHigh AMPTests.test_unknownCommandHigh  s    
 +,a	 	
S$$[1==ahhG		$'	E&&qxx0		1h/r)   c                 @   / n[        5       R                  [        R                  " [        R
                  S95      R                  UR                  5        US   R                  [        R                  5        U R                  S[        US   R                  5      5        g)z
It can be very confusing if you write some code which responds to a
command, but gets the return value wrong.  Most commonly you end up
returning None instead of a dictionary.

Verify that if that happens, the framework logs a useful error.
)r  r   NoneN)r   dispatchCommandr   rT  r   r   r  r3   r  BadLocalReturnfailUnlessInr    r\  )r%   r  s     r&   test_brokenReturnValueAMPTests.test_brokenReturnValue6  sm     &(88JJ 8 89	

*QXX
	!		#$$%&$qtzz"23r)   c                     [        [        [        S9u  pn/ nSnUR                  [        USS9R	                  UR
                  5        UR                  5         U R                  US   S   U5        g)zi
Verify that unknown arguments are ignored, and not passed to a Python
function which can't accept them.
r   r  s   I'm not in the book!)rO   bonusr   rO   N)r:  r   r   r   r  r3   r  rU  r  s         r&   test_unknownArgumentAMPTests.test_unknownArgumentE  sl    
 +66
a 	u,C 	 	

+ahh
		1g.r)   c           	         U R                  [        [        [        R                  " S5      5      5      [
        5        U R                  [        [        [        R                  " 5       5      5      [
        5        U R                  [        [        [        R                  " 5       5      5      [
        5        U R                  S[        [        R                  " 5       5      5        g)zC
Verify that the various Box objects repr properly, for debugging.
r=  rT  N)	rU  rV  r    r   
_SwitchBoxrE   r   rT  assertInr/   s    r&   test_simpleReprsAMPTests.test_simpleReprsW  s     	d3>>##678#>d3;;=12C8d3::<0137hSZZ\ 23r)   c                     [        SS5      n[        R                  " 5       nXl        U R	                  [        U5      SUR                  [        U5      4-  5        g)zA
Verify that L{AMP} objects output their innerProtocol when set.
Nr  z#<AMP inner <TestProto #%d> at 0x%x>)r   r   r   innerProtocolrU  r    r$   id)r%   r  r=  s      r&   test_innerProtocolInRepr!AMPTests.test_innerProtocolInRepr`  sM     t%56
GGI$G1Z5J5JBqE4RR	
r)   c                     [         R                  " 5       nU R                  [        U5      S[	        U5      S S35        g)zP
Verify that L{AMP} objects do not output 'inner' when no innerProtocol
is set.
z
<AMP at 0xr   >N)r   r   rU  r    r/  rX  s     r&   test_innerProtocolNotInRepr$AMPTests.test_innerProtocolNotInReprm  s2    
 GGIaJr!uQiq"9:r)   SSL not availablec                 z    U R                  [        [        [        R                  " 5       5      5      [
        5        g)z+
L{amp._TLSBox.__repr__} returns a string.
N)rU  rV  r    r   _TLSBoxrE   r/   s    r&   test_simpleSSLReprAMPTests.test_simpleSSLRepru  s$    
 	d3;;=12C8r)   c                    [        5       u  pnSnU R                  " [        R                  UR                  S40 US0D6nU R                  UR                  5        U R                  UR                  5        U R                  UR                  5        U R                  UR                  UR                  S5      5        U R                  [        [        U5      5      [!        U5      5        U R                  S[!        U5      5        g)zT
Verify that a key that is too long will immediately raise a synchronous
exception.
   HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHs   Hellos   hiasciirS  N)r:  r^  r   r  rP   rh  r  r  r  r  rU  r\  encoder*  rE   r  r    r%   r  ru  r   r   tls         r&   test_keyTooLongAMPTests.test_keyTooLong|  s    
 +,as{{A,>,>WQPUJW!

#"**%188G#45c#a&k48,eT"X&r)   c                 @   [        5       u  pnSS-  nU R                  [        R                  UR                  U5      nUR                  5         U R                  UR                  5        U R                  UR                  5        U R                  UR                  S5        U R                  UR                  U5        U R                  [        [!        U5      5      [#        U5      5        U R                  S[#        U5      5        U R                  S[#        U5      5        g)z[
Verify that attempting to send value longer than 64k will immediately
raise an exception.
   Hi   rM   r\  rO   N)r:  r^  r   r  rS   r  rj  r  rh  r  rU  r  failUnlessIdenticalr\  r*  rE   r  r    r?  s         r&   test_valueTooLongAMPTests.test_valueTooLong  s    
 +,aJs{{AKK;		"

#X.  1-c#a&k48,gtBx(gtBx(r)   c                     [        [        [        S9u  pn/ nSnUR                  U5      R                  UR                  5        UR                  5         U R                  US   S   U5        g)zk
Verify that a simple command can be sent and its response received with
the high-level value parsing API.
r   r  r   rO   N)r:  r   rS   r  r3   r  rU  r  s         r&   test_helloWorldCommandAMPTests.test_helloWorldCommand  s`    
 +66
a 	E&&qxx0		1g.r)   c                 *   / n[        [        [        S9u  p#nSnUR                  U5      R                  UR                  5        UR                  5         US   R                  [        5        U R                  [        US   R                  5      S5        g)z
Verify that if a known error type is raised and handled, it will be
properly relayed to the other end of the connection and translated into
an exception, and no error will be logged.
r      fuck your   r   N)r:  r   rS   r  r3   r  r  r]   rU  rE   r\  r%   r  r  ru  r   r  s         r&   test_helloErrorHandling AMPTests.test_helloErrorHandling  sv     *66
a 	E%%ahh/			!		$%QqTZZ*<=r)   c                    / n[        [        [        S9u  p#nSnUR                  U5      R                  UR                  5        UR                  5         UR                  5       R                  [        5        UR                  U5      R                  UR                  5        UR                  5         UR                  5       R                  [        R                  5        g)z
Verify that if a known, fatal error type is raised and handled, it will
be properly relayed to the other end of the connection and translated
into an exception, no error will be logged, and the connection will be
terminated.
r   r   N)r:  r   rS   r  r3   r  r  r  rc   r   r  rM  s         r&   test_helloFatalErrorHandling%AMPTests.test_helloFatalErrorHandling  s     *66
a 	E%%ahh/			[!	E%%ahh/			U))*r)   c                 
   / n[        [        [        S9u  p#n[        nUR                  U5      R	                  UR
                  5        UR                  5         UR                  5       nUR                  [        R                  5        UR                  U5      R	                  UR
                  5        UR                  5       nUR                  [        R                  5        U R                  U R                  [        5      5        g)z
Verify that if an unknown error type is raised, it will be relayed to
the other end of the connection and translated into an exception, it
will be logged, and then the connection will be dropped.
r   N)r:  r   r   rS   r  r3   r  r  r  r   UnknownRemoteErrorr   r  rh  r  r   )r%   r  r  ru  r   r  urecls           r&   test_helloNoErrorHandling"AMPTests.test_helloNoErrorHandling  s     *66
a (	E%%ahh/		eeg''(	E%%ahh/UUW
$$%../HIJr)   c                    [        [        [        S9u  pn/ nUR                  [        5      R	                  UR
                  5        UR                  5         U R                  U/ 5        UR                  R                  5         UR                  5         UR                  5       R                  [        R                  5        UR                  R                  0 5        UR                  $ )zs
Verify that a command that does not get answered until after the
connection terminates will not cause any errors.
r   )r:  r   r   r   r  r3   r  rU  r-   loseConnectionr  r  r   r  r
  r7   r%   r  ru  r   r  s        r&   test_lateAnswerAMPTests.test_lateAnswer  s    
 +66
a 	[!,,QXX6		B	""$			U))*			2yyr)   c                     [        [        [        S9u  pnSnUR                  [        US9  UR	                  5         U R                  UR                  5        g)z7
Verify that a command that requires no answer is run.
r   r  rN   N)r:  r   r   r   r  rh  r   )r%   r  ru  r   r  s        r&   test_requiresNoAnswerAMPTests.test_requiresNoAnswer  sH     +66
a 	]%0				"r)   c                    / n[        [        [        S9u  p#nSnUR                  [        US9  UR	                  5         U R                  U R                  [        R                  5      5        SnUR                  [        US9R                  UR                  5        UR	                  5         UR                  5       R                  [        R                  5        U R!                  UR"                  5        g)zM
Verify that commands sent after a failed no-answer request do not complete.
r   rL  rN   r  N)r:  r   r   r   r  rh  r  r   RemoteAmpErrorr|   r  r3   r  r  r   r  rj  r   rM  s         r&   test_requiresNoAnswerFail"AMPTests.test_requiresNoAnswerFail  s     *66
a 	]%0		..s/A/ABC	U%(33AHH=			U))*#r)   c                     [        [        [        S9u  pnUR                  R                  5         UR	                  5         UR                  [        SS9nU R                  US5        g)z^
No-answer commands sent after the connection has been torn down do not
return a L{Deferred}.
r   r  rN   N)r:  r   r-   rZ  r  r   r   rp  )r%   r  ru  r   r   s        r&   test_requiresNoAnswerAfterFail'AMPTests.test_requiresNoAnswerAfterFail  sS    
 +66
a 	
""$		m:>fd#r)   c                     [        [        [        S9u  pnUR                  [        SS9  UR                  5         U R                  [        R                  5      nU R                  [        U5      S5        g)z
Verify that responders of requiresAnswer=False commands have to return
a dictionary anyway.

(requiresAnswer is a hint from the _client_ - the server may be called
upon to answer commands in any case, if the client wants to know when
they complete.)
r   rM   rN   r   N)r:  r/  r   r   r   r  r  r   r   rU  r  )r%   r  ru  r   les        r&   test_noAnswerResponderBadAnswer(AMPTests.test_noAnswerResponderBadAnswer)  s]     +26
a 	
](3		##C$6$67R!$r)   c                    [        [        [        S9u  pn/ nUR                  [        SS9R                  UR                  5        UR                  5         U R                  [        U5      S5        U R                  U[        SSS9/5        g)z
Verify that responders with requiresAnswer=False will actually respond
if the client sets requiresAnswer=True.  In other words, verify that
requiresAnswer is a hint honored only by the client.
r   s   Hello!rN   r   s   Hello!-noanswerNr   )r:  r5  r   r   r|   r  r3   r  rU  r  r   r[  s        r&   $test_noAnswerResponderAskedForAnswer-AMPTests.test_noAnswerResponderAskedForAnswer;  sy     +/6
a 	U),88B		Q#-T:;	
r)   c                    [        [        [        S9u  pn/ nUR                  [        SS9R	                  UR
                  5        UR                  5         UR                  5       R                  S5      nU R                  USS0/S-  5        g)z>
Test encoding of an argument that uses the AmpList encoding.
r   
   )r   r   r   r   N)
r:  r   r   r   r  r3   r  r  getrU  )r%   r  ru  r   r  r(  s         r&   test_ampListCommandAMPTests.test_ampListCommandM  su     +66
a 	WR(44QXX>		V$3(b1r)   c                    [        [        [        S9u  pn/ nUR                  [        SS9R	                  UR
                  5        UR                  5         UR                  5       R                  S5      nU R                  US5        g)z}
Sending a command with an omitted AmpList argument that is
designated as optional does not raise an InvalidSignature error.
r   please)r  r   r  N
r:  r   r   r   r  r3   r  r  rq  rU  r%   r  ru  r   r  r   s         r&   test_optionalAmpListOmitted$AMPTests.test_optionalAmpListOmitted[  sk    
 +66
a 	\X6BB188L		557;;z*>2r)   c                    [        [        [        S9u  pn/ nUR                  [        SSS0/S9R	                  UR
                  5        UR                  5         UR                  5       R                  S5      nU R                  US5        g)	zF
Sanity check that optional AmpList arguments are processed normally.
r   ru  rm   foo)r  r  r   zfoo acceptedNrv  rw  s         r&   test_optionalAmpListPresent$AMPTests.test_optionalAmpListPresentj  sy     +66
a 	HVUO3D 	 	

+ahh
		557;;z*>2r)   c                 L    U R                  [        R                  [        5        g)zf
Verify that if we pass an invalid argument list (omitting an argument),
an exception will be raised.
N)r^  r   InvalidSignaturer|   r/   s    r&   test_failEarlyOnArgSending#AMPTests.test_failEarlyOnArgSendingz  s    
 	#..6r)   c                 X  ^ ^^^ [         R                  " 5       n[        U5      m[         R                  " 5       n[        U5      m[        U4S jU4S jS9u  mp4UU 4S jnTR	                  5       R                  U5        UR                  5         T R                  T R                  5        g)z
As a debugging aid, a protocol system should raise a
L{ProtocolSwitched} exception when asked to switch a protocol that is
already switched.
c                     > T $ r+   rG   serverProtos   r&   r  4AMPTests.test_doubleProtocolSwitch.<locals>.<lambda>      r)   c                     > T $ r+   rG   clientProtos   r&   r  r        r)   r   c                 h   > TR                  [        R                  TR                  5        STl        g NT)r^  r   r  r  testSucceeded)r   r  r%   s    r&   r  4AMPTests.test_doubleProtocolSwitch.<locals>.switched  s'    c22A4J4JK!%Dr)   N)	r   r	  r   r:  r  r  r  rh  r  )	r%   serverDeferredclientDeferredru  r   r  r  r  r  s	   `     @@@r&   test_doubleProtocolSwitch"AMPTests.test_doubleProtocolSwitch  s~     )4^D)4^D*+9L
1	& 	
 ,,X6		**+r)   Fc                   ^ ^
^^^^ ST l         [        R                  " 5       mU" T5      m[        R                  " 5       mU" T5      m[        U4S jU4S jS9u  pEnU(       a0  / nUR	                  [
        5      R                  UR                  5        UR                  5       nU(       a+  T R                  [        R                  UR                  S5        U 4S jm
U
UU4S jn	UR                  U	5        UR                  5         TR                  b5  TR                  R!                  TR"                  5        UR                  5         U(       ac  U(       a1  UR$                  R'                  [        R(                  " S	S
5      5        OUR$                  R!                  0 5        UR                  5         UR*                  R-                  5         UR                  5         T R/                  T R                   5        g)zk
Verify that it is possible to switch to another protocol mid-connection and
send data to it successfully.
Fc                     > T $ r+   rG   r  s   r&   r  .AMPTests.test_protocolSwitch.<locals>.<lambda>  r  r)   c                     > T $ r+   rG   r  s   r&   r  r    r  r)   r   r  c                    > U u  u  pu  p4TR                  U5        TR                  U5        TR                  SR                  U5      [        5        TR                  SR                  U5      [        5        STl        g )Nr)   T)rh  rU  r  r  r  r  )infoserverSuccess
serverDataclientSuccess
clientDatar%   s        r&   cbConnsLost1AMPTests.test_protocolSwitch.<locals>.cbConnsLost  sg    IMF(m*E=OOM*OOM*SXXj13EFSXXj13EF!%Dr)   c                 R   > [         R                  " TT/5      R                  T5      $ r+   )r   DeferredListr  )rn   r  r  r  s    r&   cbSwitch.AMPTests.test_protocolSwitch.<locals>.cbSwitch  s)    %%~~&FGSS r)   Ns   SPURIOUSz,Here's some traffic in the form of an error.)r  r   r	  r:  r   r   r  r3   r  r^  r   r  rS   r  r  r(  r7   r,  r
  errbackrb  r-   rZ  rh  )r%   switcherspuriousTrafficspuriousErrorr  ru  r   wfdrswitchDeferredr  r  r  r  r  r  s   `         @@@@@r&   r  AMPTests.test_protocolSwitch  sg    #)~.)~.*+9L
a DLL%00=//1c22AKKJ	&	
 	""8,		!!-""++K,G,GHGGI 		!!&&#%S 		""2&GGI	""$		**+r)   c                 (    U R                  [        S9$ )zt
Verify that protocol-switching even works if the value returned from
the command that does the switch is deferred.
)r  )r  r*  r/   s    r&   test_protocolSwitchDeferred$AMPTests.test_protocolSwitchDeferred  s    
 ''1Q'RRr)   c                 ^  ^^	 SU l         [        R                  " 5       nU" U5      m	[        R                  " 5       nU" U5      m[        U	4S jU4S jS9u  pEn/ nUR	                  SS9R                  UR                  5        UR                  5         UR                  5       R                  [        5        U R                  U R                   5        UR                  S5      R                  UR                  5        UR                  5         U R                  UR                  5       S   S5        g	)
zu
Verify that if we try to switch protocols and it fails, the connection
stays up and we can go back to speaking AMP.
Fc                     > T $ r+   rG   r  s   r&   r  2AMPTests.test_protocolSwitchFail.<locals>.<lambda>  r  r)   c                     > T $ r+   rG   r  s   r&   r  r    r  r)   r   T)r  r  rO   N)r  r   r	  r:  r  r  r3   r  r  r  rf   rj  rS   r  rU  )
r%   r  r  r  r  ru  r   r  r  r  s
           @@r&   test_protocolSwitchFail AMPTests.test_protocolSwitchFail  s    
 #)~.)~.*+9L
a 	D)44QXX>			_%++, 	
H))!((3		)84r)   c                      U R                  SS9$ )z[
Verify that attempts to send traffic after a switch will not corrupt
the nested protocol.
T)r  r  r/   s    r&   test_trafficAfterSwitch AMPTests.test_trafficAfterSwitch  s    
 '''==r)   c                 "    U R                  SSS9$ )zP
Returning an error after a protocol switch should record the underlying
error.
T)r  r  r  r/   s    r&   test_errorAfterSwitchAMPTests.test_errorAfterSwitch  s    
 ''D'QQr)   c                 d   [        [        [        S9u  pn/ nSnSnUR                  U5      R                  UR                  5        UR                  5         U R                  UR                  5       S   U5        UR                  [        5      R                  UR                  5        UR                  5         U R                  UR                  5       S   U5        UR                  U5      R                  UR                  5        UR                  5       R                  [        R                  5        g)z\
Verify that commands with a responseType of QuitBox will in fact
terminate the connection.
r   r  r  rO   r  N)r:  r   rS   r  r3   r  rU  r  r   r   r  r  r   r  )r%   r  ru  r   r  r  GOODBYEs          r&   test_quitBoxQuitsAMPTests.test_quitBoxQuits  s    
 +66
a
 	E&&qxx0		)51	W))!((3		+W5	E%%ahh/	U))*r)   c                    [        5       u  pn/ nUR                  Ul        UR                  [        SSSSS9  UR                  5         U R                  [        U5      S5        S[        R                  4SS	S
S4 H)  u  pVU R                  US   R                  U5      U5        M+     US   R                  S5        U R                  US   0 5        g)zh
Verify that the command dictionaries for a callRemoteN look correct
after being serialized and parsed.

   hello test   mixed case arg testr      yrO   r   r   r   r      _command)rM   r  )r   r  )r   r   )r   r  r     _askN)
r:  r3   rM  r   r|   r  rU  r  r   r  )r%   r  ru  r   r  r  rv  s          r&   test_basicLiteralEmitAMPTests.test_basicLiteralEmit  s    
 +,a88	, 	 	
 	
	Q#%++,%2%
DA QrUYYq\1-
 	
"		'2#r)   c                   ^ / m " U4S jS[         R                  5      n[        US9u  p#nUR                  [        SSSSS9R                  TR                  5        UR                  5         U R                  [        T5      S	5        U R                  TS
   S[        SSSSUR                  R                  5       SSS945        U R                  TS   [        SSS95        g)z
Verify that a call similar to basicLiteralEmit's is handled properly with
high-level quoting and passing to Python methods, and that argument
names are correctly handled.
c                   D   > \ rS rSrU 4S jr\R                  \5        Srg):AMPTests.test_basicStructuredEmit.<locals>.StructuredHelloi<  c                 :   > TR                  X45        [        SS9$ )N   aaarN   )r3   r   )r%   r=  r  r  s      r&   h<AMPTests.test_basicStructuredEmit.<locals>.StructuredHello.h=  s    ! &))r)   rG   N)r@   rA   rB   rC   r  r|   r   rF   )r  s   r&   StructuredHellor  <  s    * OOAr)   r  )r;  r  r  r   r  r     r   rG   N)rO   r   r   r   r   r   r   r   r  )r   rO   )r   r   r:  r   r|   r  r3   r  rU  r  r   r-   rr   )r%   r  r  ru  r   r  s        @r&   test_basicStructuredEmit!AMPTests.test_basicStructuredEmit4  s     	cgg 	 +Ga	, 	 	
 +ahh
		Q#aD'4!#',,. !	
$ 	1t$f=>r)   )r  N).r@   rA   rB   rC   r  r  r  r  r  r  r  r"  r&  r+  r0  r4  r   skipSSLr9  rA  rF  rI  rN  rQ  rW  r\  r_  rc  rf  rj  rm  rr  rx  r|  r  r  r   r  r  r  r  r  r  r  r  rF   rG   r)   r&   r  r    s    &
0
07 0.0.4/$4
; G()9 *9')"/>"+(K,(#$($%$
$233 7,0 0	;,zS 0N 52>R+,$8+?r)   r  c                       \ rS rSrS rSrg)!PretendRemoteCertificateAuthorityib  c                     gr  rG   r/   s    r&   checkIsPretendRemote6PretendRemoteCertificateAuthority.checkIsPretendRemotec  r&  r)   rG   N)r@   rA   rB   rC   r  rF   rG   r)   r&   r  r  b  s    r)   r  c                   $    \ rS rSrSrS rS rSrg)	IOSimCertig  r   c                     U $ r+   rG   )r%   r  s     r&   optionsIOSimCert.optionsj  s    r)   c                 :    XL d   eU =R                   S-  sl         g)z
This isn't a real certificate, and wouldn't work on a real socket, but
iosim specifies a different API so that we don't have to do any crypto
math to demonstrate that the right functions get called in the right
places.
r   TverifyCountr%   	otherCerts     r&   iosimVerifyIOSimCert.iosimVerifym  s$        Ar)   rG   N)r@   rA   rB   rC   r  r  r  rF   rG   r)   r&   r  r  g  s    K	r)   r  c                       \ rS rSrS rSrg)OKCertiy  c                 4    UR                  5       (       d   eU $ r+   )r  )r%   r   s     r&   r  OKCert.optionsz  s    %%''''r)   rG   N)r@   rA   rB   rC   r  rF   rG   r)   r&   r  r  y  s    r)   r  c                       \ rS rSrS rSrg)
GrumpyCerti  c                 .    U =R                   S-  sl         g)Nr   Fr  r  s     r&   r  GrumpyCert.iosimVerify  s    Ar)   rG   N)r@   rA   rB   rC   r  rF   rG   r)   r&   r  r    s    r)   r  c                        \ rS rSrS rS rSrg)
DroppyCerti  c                     Xl         g r+   toDrop)r%   r  s     r&   r'   DroppyCert.__init__  s    r)   c                 b    U =R                   S-  sl         U R                  R                  5         g)Nr   T)r  r  rZ  r  s     r&   r  DroppyCert.iosimVerify  s&    A""$r)   r  N)r@   rA   rB   rC   r'   r  rF   rG   r)   r&   r  r    s    r)   r  c                   Z    \ rS rSrSrS rS r\R                  R                  \5        Sr
g)SecurableProtoi  Nc                     [        5       /$ r+   )r  r/   s    r&   verifyFactorySecurableProto.verifyFactory  s    1344r)   c                 T    U R                  5       nU R                  5       n[        XS9$ )Ntls_localCertificatetls_verifyAuthorities)certFactoryr  r   )r%   certverifys      r&   
getTLSVarsSecurableProto.getTLSVars  s)    !##%LLr)   rG   )r@   rA   rB   rC   r   r  r  r   StartTLSr   rF   rG   r)   r&   r  r    s&    G5M
 LL:&r)   r  r6  z2This test case requires SSL support in the reactorc                   ,    \ rS rSrS rS rS rS rSrg)TLSTestsi  c                   ^ [        [        [        S9u  pn[        5       mU4S jUl        UR	                  [
        R                  T[        5       /S9  / nUR	                  [        5      R                  UR                  5        UR                  5         U R                  TR                  S5        / nUR	                  [        5      R                  UR                  5        UR                  5         U R                  US   SS05        g)	zm
Verify that starting TLS and succeeding at handshaking sends all the
notifications to all the right places.
r   c                     > T $ r+   rG   okcs   r&   r  +TLSTests.test_startingTLS.<locals>.<lambda>      #r)   r  r  r   r   TN)r:  r  r  r  r   r   r  r  r   r  r3   r  rU  r  )r%   clisvrr   r  r	  s        @r&   test_startingTLSTLSTests.test_startingTLS  s    
 /&N
! h%LL!$#D#F"G 	 	
 {#//9		!,{#//9		1$/0r)   c                 \  ^ [        [        [        S9u  pn[        5       mU4S jUl        UR	                  [
        R                  T[        5       /S9  UR                  5         SUl	        U R                  [
        R                  UR                  [
        R                  T[        5       /S9  g)zb
Verify that the protocol will complain if we attempt to renegotiate TLS,
which we don't support.
r   c                     > T $ r+   rG   r  s   r&   r  1TLSTests.test_startTooManyTimes.<locals>.<lambda>  r  r)   r  TN)r:  r  r  r  r   r   r  r  r  noPeerCertificater^  
OnlyOneTLSr%   r  r  r   r	  s       @r&   test_startTooManyTimesTLSTests.test_startTooManyTimes  s    
 /&N
! h%LL!$#D#F"G 	 	

 	
	 $NNNNLL!$#D#F"G 	 	
r)   c                 x  ^ [        5       m[        [        [        S9u  pnU4S jUl        UR	                  [
        R                  TS9  UR                  5         U R                  TR                  S5        UR	                  [        5      nUR                  5         U R                  U[        R                  5        g)z
Verify that starting TLS and failing on both sides at handshaking sends
notifications to all the right places and terminates the connection.
r   c                     > T $ r+   rG   )badCerts   r&   r  1TLSTests.test_negotiationFailed.<locals>.<lambda>  s    'r)   r  r  N)r  r:  r  r  r   r   r  r  rU  r  r   assertFailurer   NativeOpenSSLError)r%   r  r  r   rl   r  s        @r&   test_negotiationFailedTLSTests.test_negotiationFailed  s     ,.&N
! *s||'B		,,a0NN;'		1e667r)   c                   ^ [        [        [        S9u  pn[        UR                  5      mU4S jUl        UR                  [        R                  TS9  UR                  5         U R                  TR                  S5        UR                  [        5      nUR                  5         U R                  U[        R                  5        g)zn
Verify that starting TLS and failing by way of a lost connection
notices that it is probably an SSL problem.
r   c                     > T $ r+   rG   )
droppyCerts   r&   r  :TLSTests.test_negotiationFailedByClosing.<locals>.<lambda>  s    *r)   r  r  N)r:  r  r  r-   r  r   r   r  r  rU  r  r   r  r   PeerVerifyError)r%   r  r  r   rl   r#  s        @r&   test_negotiationFailedByClosing(TLSTests.test_negotiationFailedByClosing  s     /&N
!  .
,s||*E		//3NN;'		 	1e334r)   rG   N)	r@   rA   rB   rC   r  r  r  r&  rF   rG   r)   r&   r  r    s    1:
68,5r)   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	TLSNotAvailableTestsi  zH
Tests what happened when ssl is not available in current installation.
c                 D    [         R                  U l        S[         l        g)z
Disable ssl in amp.
N)r   r   r/   s    r&   r  TLSNotAvailableTests.setUp  s     77r)   c                 .    U R                   [        l         g)z
Restore ssl module.
N)r   r   r/   s    r&   tearDownTLSNotAvailableTests.tearDown  s     ((r)   c                    ^ [        [        [        S9u  pn[        5       mU4S jUl        U R	                  UR                  [        R                  T[        5       /S9[        5      $ )zO
Check that callRemote raises an exception when called with a
L{amp.StartTLS}.
r   c                     > T $ r+   rG   r  s   r&   r  ;TLSNotAvailableTests.test_callRemoteError.<locals>.<lambda>$  r  r)   r  )
r:  r  r  r  r  r   r   r  r  r  r  s       @r&   test_callRemoteError)TLSNotAvailableTests.test_callRemoteError  sd    
 /&N
! h%!!NN%('H'J&K  
 
 	
r)   c                 *  ^ [        5       n[        5       mU4S jUl        [        R                  " 5       nSUS'   SUS'   / nUR
                  Ul        UR                  [        5       5        UR                  U5        U R                  USSSS./5        g	)
zd
When a client with SSL enabled talks to a server without SSL, it
should return a meaningful error.
c                     > T $ r+   rG   r  s   r&   r  @TLSNotAvailableTests.test_messageReceivedError.<locals>.<lambda>6  r  r)   s   StartTLSr  r  r  s	   TLS_ERRORs   TLS not available)r  r  r  N)r  r  r  r   rW   r3   r  ro  r   rM  rU  )r%   r  rY   rI  r	  s       @r&   test_messageReceivedError.TLSNotAvailableTests.test_messageReceivedError/  s    
 h%ggi&KGll?,-3 %1#+?		
r)   r   N)
r@   rA   rB   rC   rD   r  r-  r2  r7  rF   rG   r)   r&   r)  r)    s    
*
r)   r)  c                       \ rS rSrSrSrg)InheritedErroriJ  z*
This error is used to check inheritance.
rG   Nr`   rG   r)   r&   r:  r:  J  r   r)   r:  c                       \ rS rSrSrSrg)OtherInheritedErroriP  z4
This is a distinct error for checking inheritance.
rG   Nr`   rG   r)   r&   r<  r<  P  r   r)   r<  c                   B    \ rS rSr% Sr\S0r\\\	\
   \4      \S'   Srg)BaseCommandiV  z2
This provides a command that will be subclassed.
s   INHERITED_ERRORr   rG   N)r@   rA   rB   rC   rD   r:  r   r   r   r   r   r   r   rF   rG   r)   r&   r>  r>  V  s0    
 	*6FHT$y/5012 r)   r>  c                       \ rS rSrSrSrg)InheritedCommandi`  zT
This is a command which subclasses another command but does not override
anything.
rG   Nr`   rG   r)   r&   r@  r@  `  s    r)   r@  c                   j    \ rS rSr% SrS\R                  " 5       4/r\S0r	\
\\\   \4      \S'   Srg)AddErrorsCommandig  zQ
This is a command which subclasses another command but adds errors to the
list.
s   others   OTHER_INHERITED_ERRORr   rG   N)r@   rA   rB   rC   rD   r   r   r   r<  r   r   r   r   r   r   r   rF   rG   r)   r&   rB  rB  g  sC    
 CKKM*+I56FHT$y/5012 r)   rB  c                   @    \ rS rSrSrS r\R                  \5        Srg)NormalCommandProtocolis  z
This is a protocol which responds to L{BaseCommand}, and is used to test
that inheritance does not interfere with the normal handling of errors.
c                     [        5       er+   r:  r/   s    r&   respNormalCommandProtocol.respy      r)   rG   N)	r@   rA   rB   rC   rD   rG  r>  r   rF   rG   r)   r&   rD  rD  s  s    
 $r)   rD  c                   @    \ rS rSrSrS r\R                  \5        Srg)InheritedCommandProtocoli  z
This is a protocol which responds to L{InheritedCommand}, and is used to
test that inherited commands inherit their bases' errors if they do not
respond to any of their own.
c                     [        5       er+   rF  r/   s    r&   rG  InheritedCommandProtocol.resp  rI  r)   rG   N)	r@   rA   rB   rC   rD   rG  r@  r   rF   rG   r)   r&   rK  rK    s     t$r)   rK  c                   @    \ rS rSrSrS r\R                  \5        Srg)AddedCommandProtocoli  z
This is a protocol which responds to L{AddErrorsCommand}, and is used to
test that inherited commands can add their own new types of errors, but
still respond in the same way to their parents types of errors.
c                 8    U(       a
  [        5       e[        5       er+   )r<  r:  )r%   others     r&   rG  AddedCommandProtocol.resp  s    %'' ""r)   rG   N)	r@   rA   rB   rC   rD   rG  rB  r   rF   rG   r)   r&   rO  rO    s    # t$r)   rO  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
CommandInheritanceTestsi  zE
These tests verify that commands inherit error conditions properly.
c                     [        X"S9u  pVnUR                  " U40 UD6nU R                  X5      n	UR                  5         U	$ )zf
Check that the appropriate kind of error is raised when a given command
is sent to a given protocol.
r   )r:  r   failUnlessFailurer  )
r%   r  rn   cmdr>  r  ru  r   rl   d2s
             r&   
errorCheck"CommandInheritanceTests.errorCheck  sC    
 +uPaLL####A+			r)   c                 @    U R                  [        [        [        5      $ )z`
Verify that errors specified in a superclass are respected normally
even if it has subclasses.
)rY  r:  rD  r>  r/   s    r&   test_basicErrorPropagation2CommandInheritanceTests.test_basicErrorPropagation  s    
 ~/DkRRr)   c                 @    U R                  [        [        [        5      $ )zX
Verify that errors specified in a superclass command are propagated to
its subclasses.
)rY  r:  rK  r@  r/   s    r&   test_inheritedErrorPropagation6CommandInheritanceTests.test_inheritedErrorPropagation  s    
 46F
 	
r)   c                 >    U R                  [        [        [        SS9$ )z
Verify that new errors specified in a subclass of an existing command
are honored even if the superclass defines some errors.
TrQ  )rY  r<  rO  rB  r/   s    r&   test_inheritedErrorAddition3CommandInheritanceTests.test_inheritedErrorAddition  s%    
 !57Gt  
 	
r)   c                 >    U R                  [        [        [        SS9$ )zv
Verify that errors specified in a command's superclass are respected
even if that command defines new errors itself.
Frb  )rY  r:  rO  rB  r/   s    r&   test_additionWithOriginalError6CommandInheritanceTests.test_additionWithOriginalError  s%    
 02B%  
 	
r)   rG   N)r@   rA   rB   rC   rD   rY  r\  r_  rc  rf  rF   rG   r)   r&   rT  rT    s!    	S


r)   rT  c                     U R                  [        R                  [        R                  5        U?UR	                  U 5        g r+   )r  r   ConnectionLostr  r:   )r  rn   s     r&   _loseAndPassrj    s0    HHU!!5#7#78	r)   c                   $    \ rS rSrSrS rS rSrg)LiveFireBasei  z,
Utility for connected reactor-using tests.
c                   ^  SSK Jn  [        R                  " 5       T l        T R
                  T R                  l        [        R                  " 5       T l        T R                  T R                  l        [        R                  " 5       T R                  l        [        R                  " 5       T R                  l        UR                  " ST R                  5      T l        T R                  T R                  R                  5        UR                   " ST R                  R#                  5       R$                  T R                  5      T l        T R                  T R&                  R(                  5        U 4S jn[        R*                  " T R                  R                  T R                  R                  /5      nUR-                  U5      $ )z2
Create an amp server and connect a client to it.
r   )r   z	127.0.0.1c                 r   > TR                   R                  Tl        TR                  R                  Tl        g r+   )clientFactoryr   r  serverFactoryr  )rlstr%   s    r&   	getProtos%LiveFireBase.setUp.<locals>.getProtos  s)    ))22DH))22DHr)   )twisted.internetr   r   ServerFactoryrp  r  ClientFactoryro  r  r   r	  r   	listenTCP
serverPort
addCleanupstopListening
connectTCPri  port
clientConn
disconnectr  r  )r%   r   rr  dls   `   r&   r  LiveFireBase.setUp  s9    	-%335&*&6&6#%335&*&6&6#$)NN$4!$)NN$4!!++At/A/AB556!,,002779K9K
 	223	3 !3!3!:!:D<N<N<U<U VW~~i((r)   c                 x   / nU R                   U R                  4 Hw  nUR                  c  M  [        R                  " 5       R                  [        U5      nUR                  Ul        UR                  R                  5         UR                  U5        My     [        R                  " U5      R                  S 5      $ )zV
Cleanup client and server connections, and check the error got at
C{connectionLost}.
c                 .    U R                   R                  $ r+   )r\  
subFailure)firsts    r&   r  'LiveFireBase.tearDown.<locals>.<lambda>	  s    u{{?U?Ur)   )r  r  r-   r   r	  r  rj  r  r:   rZ  r3   gatherResults)r%   r  connrl   s       r&   r-  LiveFireBase.tearDown  s    
 HHdhh&D~~)NN$//dC&'ii#--/ ' ""1%001UVVr)   )r  r}  ro  rp  rx  r  N)r@   rA   rB   rC   rD   r  r-  rF   rG   r)   r&   rl  rl    s    )4Wr)   rl  c                 |    SS K nUR                  R                  U S-   5        UR                  R                  5         g )Nr   
)sysstdoutr.   r  )r   r  s     r&   showr  	  s+    JJQXJJr)   c                      SSK Jn   U R                  SS9nU R                  R	                  5       nUR                  U5      nUR                  XS S5      nUR                  U5      nU$ )Nr   r   shared)CNc                     gr  rG   )dns    r&   r   tempSelfSigned.<locals>.<lambda>	  s    r)   i )rt  r   DNKeyPairgeneratecertificateRequestsignCertificateRequestnewCertificate)r   sharedDNrS  crsscrdr  s         r&   tempSelfSignedr  	  s`    $vvv"H
++


 C				)B&&x_gNEe$DKr)   c                   "    \ rS rSr\r\rS rSrg)LiveFireTLSTestsi	  c                    ^ ^ [         mU4S jT R                  l        U4S jT R                  l        UU 4S jnT R                  R                  [        R                  TT/S9R                  U5      $ )a  
Using real, live TLS, actually negotiate a connection.

This also looks at the 'peerCertificate' attribute's correctness, since
that's actually loaded using OpenSSL calls, but the main purpose is to
make sure that we didn't miss anything obvious in iosim about TLS
negotiations.
c                  
   > T /$ r+   rG   r  s   r&   r  9LiveFireTLSTests.test_liveFireCustomTLS.<locals>.<lambda>-	  s    $r)   c                     > T $ r+   rG   r  s   r&   r  r  .	  s    tr)   c                    >^ TR                  5       mUU4S jnTR                  R                  [        5      R	                  U5      $ )Nc                   > TR                  TTR                  R                  R                  5       5        TR                  TTR                  R                  R                  5       5        TR                  TTR
                  R                  R                  5       5        TR                  TTR
                  R                  R                  5       5        g r+   )rU  r  hostCertificatedigestpeerCertificater  )rslt2r%   r   s    r&   r   HLiveFireTLSTests.test_liveFireCustomTLS.<locals>.secured.<locals>.pinged4	  s    
   DHH$<$<$C$C$EF  DHH$<$<$C$C$EF  DHH$<$<$C$C$EF  DHH$<$<$C$C$EFr)   )r  r  r   r   r  )rsltr   r   r  r%   s     @r&   secured8LiveFireTLSTests.test_liveFireCustomTLS.<locals>.secured1	  s6    AG 88&&{3??GGr)   r  )	tempcertr  r  r  r  r   r   r  r  )r%   r  r  s   ` @r&   test_liveFireCustomTLS'LiveFireTLSTests.test_liveFireCustomTLS!	  s]     !/+	H xx""LLtD6 # 

+g
	r)   rG   N)	r@   rA   rB   rC   r  r  r  r  rF   rG   r)   r&   r  r  	  s     !K K!r)   r  c                   T    \ rS rSrSrS r\R                  R                  \5        Sr	g)SlightlySmartTLSiE	  zS
Specific implementation of server side protocol with different
management of TLS.
c                     [        [        S9$ )zC
@return: the global C{tempcert} certificate as local certificate.
r  )r   r  r/   s    r&   r  SlightlySmartTLS.getTLSVarsK	  s     22r)   rG   N)
r@   rA   rB   rC   rD   r  r   r  r   rF   rG   r)   r&   r  r  E	  s     
3 LL:&r)   r  c                   "    \ rS rSr\r\rS rSrg)PlainVanillaLiveFireTestsiT	  c                    ^  U 4S jnT R                   R                  [        R                  5      R	                  U5      $ )z
Verify that out of the box, we can start TLS to at least encrypt the
connection, even if we don't have any certificates to use.
c                 B   > TR                   R                  [        5      $ r+   r  r   r   r   r%   s    r&   r  BPlainVanillaLiveFireTests.test_liveFireDefaultTLS.<locals>.secured`	      88&&{33r)   )r  r   r   r  r  r%   r  s   ` r&   test_liveFireDefaultTLS1PlainVanillaLiveFireTests.test_liveFireDefaultTLSZ	  s.    	4 xx""3<<0<<WEEr)   rG   N)	r@   rA   rB   rC   r   r  r  r  rF   rG   r)   r&   r  r  T	  s     1K0K	Fr)   r  c                   "    \ rS rSr\r\rS rSr	g)WithServerTLSVerificationTestsif	  c                    ^  U 4S jnT R                   R                  [        R                  [        /S9R                  U5      $ )z?
Verify that anonymous clients can verify server certificates.
c                 B   > TR                   R                  [        5      $ r+   r  r  s    r&   r  MWithServerTLSVerificationTests.test_anonymousVerifyingClient.<locals>.securedq	  r  r)   )r  )r  r   r   r  r  r  r  s   ` r&   test_anonymousVerifyingClient<WithServerTLSVerificationTests.test_anonymousVerifyingClientl	  s;    
	4 xx""LL
 # 

+g
	r)   rG   N)
r@   rA   rB   rC   r   r  r  r  r  rF   rG   r)   r&   r  r  f	  s     1K"K
r)   r  c                   $    \ rS rSrSrS rS rSrg)ProtocolIncludingArgumentiy	  z
An L{amp.Argument} which encodes its parser and serializer
arguments *including the protocol* into its parsed and serialized
forms.
c                     X4$ )zz
Don't decode anything; just return all possible information.

@return: A two-tuple of the input string and the protocol.
rG   )r%   stringr   s      r&   rt   )ProtocolIncludingArgument.fromStringProto	  s     !!r)   c                 V    S[        U5      [        U5      4-  nUR                  S5      $ )z
Encode identifying information about L{object} and protocol
into a string for later verification.

@type obj: L{object}
@type protocol: L{amp.AMP}
%d:%dr=  )r/  r>  )r%   objr   idents       r&   toStringProto'ProtocolIncludingArgument.toStringProto	  s*     2c7BxL11||G$$r)   rG   N)r@   rA   rB   rC   rD   rt   r  rF   rG   r)   r&   r  r  y	  s    "	%r)   r  c                   @    \ rS rSrSrS\" 5       4/rS\" 5       4/rSrg)ProtocolIncludingCommandi	  zZ
A command that has argument and response schemas which use
L{ProtocolIncludingArgument}.
   weirdrG   N)	r@   rA   rB   rC   rD   r  r   r   rF   rG   r)   r&   r  r  	  s+    
 5789I4678Hr)   r  c                   H    \ rS rSrSr\S 5       r\S 5       r\S 5       rSr	g)MagicSchemaCommandi	  zU
A command which overrides L{parseResponse}, L{parseArguments}, and
L{makeResponse}.
c                     X4Ul         U$ )z
Don't do any parsing, just jam the input strings and protocol
onto the C{protocol.parseResponseArguments} attribute as a
two-tuple. Return the original strings.
)parseResponseArguments)r%   rw   r   s      r&   parseResponse MagicSchemaCommand.parseResponse	       ,3*='r)   c                     X4Ul         U$ )z
Don't do any parsing, just jam the input strings and protocol
onto the C{protocol.parseArgumentsArguments} attribute as a
two-tuple. Return the original strings.
)parseArgumentsArguments)clsrw   r   s      r&   parseArguments!MagicSchemaCommand.parseArguments	  s     -4+>(r)   c                     X4Ul         U$ )z
Don't do any serializing, just jam the input strings and protocol
onto the C{protocol.makeArgumentsArguments} attribute as a
two-tuple. Return the original strings.
)makeArgumentsArguments)r  rx   r   s      r&   makeArguments MagicSchemaCommand.makeArguments	  r  r)   rG   N)
r@   rA   rB   rC   rD   classmethodr  r  r  rF   rG   r)   r&   r  r  	  sC    
      r)   r  c                   B    \ rS rSrSrS r\R                  S 5        Srg)NoNetworkProtocoli	  a  
An L{amp.AMP} subclass which overrides private methods to avoid
testing the network. It also provides a responder for
L{MagicSchemaCommand} that does nothing, so that tests can test
aspects of the interaction of L{amp.Command}s and L{amp.AMP}.

@ivar parseArgumentsArguments: Arguments that have been passed to any
L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
this protocol.

@ivar parseResponseArguments: Responses that have been returned from a
L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
this protocol.

@ivar makeArgumentsArguments: Arguments that have been serialized by any
L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
this protocol.
c                 .    [         R                  " U5      $ )z:
Return a Deferred which fires with the original strings.
)r   succeed)r%   r   rw   r   s       r&   _sendBoxCommand!NoNetworkProtocol._sendBoxCommand	  s     }}W%%r)   c                     0 $ r+   rG   )ru  weirds     r&   r  NoNetworkProtocol.<lambda>	  s    "r)   rG   N)	r@   rA   rB   rC   rD   r  r  r   rF   rG   r)   r&   r  r  	  s    &&   !45r)   r  _KT_VTc                       \ rS rSrSrSrg)MyBoxi	  z
A unique dict subclass.
rG   Nr`   rG   r)   r&   r  r  	  r   r)   r  c                       \ rS rSrSr\rSrg)0ProtocolIncludingCommandWithDifferentCommandTypei	  zF
A L{ProtocolIncludingCommand} subclass whose commandType is L{MyBox}
rG   N)r@   rA   rB   rC   rD   r  commandTyperF   rG   r)   r&   r  r  	  s     Kr)   r  c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rSrg)CommandTestsi	  z/
Tests for L{amp.Argument} and L{amp.Command}.
c                 |    U R                  [        [        R                  [        R                  " 5       5      5        g)z5
L{Argument} instances provide L{amp.IArgumentType}.
N)rh  r
   r   IArgumentTypeArgumentr/   s    r&   test_argumentInterface#CommandTests.test_argumentInterface	  s#     	S%6%6GHr)   c                 t    [        5       nSnSU0nU R                  [        R                  X15      SX!405        g)z
There should be a class method of Command which accepts a
mapping of argument names to serialized forms and returns a
similar mapping whose values have been parsed via the
Command's response schema.
   whateverr  r  N)r  rU  r  r  r%   r   r   rw   s       r&   test_parseResponseCommandTests.test_parseResponse	  sA     8V$$227Ev()	
r)   c                 ~   ^ ^^ [        5       mSmTR                  [        TS9nUU U4S jnUR                  U5        U$ )z
Making a remote call on a L{amp.Command} subclass which
overrides the C{parseResponse} method should call that
C{parseResponse} method to get the response.
s   weeoor  c                 F   > TR                  TR                  ST0T45        g Nr  )rU  r  )r  clientr%   thingys    r&   gotResponseCCommandTests.test_callRemoteCallsParseResponse.<locals>.gotResponse
  s$    V::gv=NPV<WXr)   )r  r   r  r  )r%   r   r	  r  r  s   `  @@r&   !test_callRemoteCallsParseResponse.CommandTests.test_callRemoteCallsParseResponse
  sC     #$$$%7v$F	Y 	[)r)   c                 t    [        5       nSnSU0nU R                  [        R                  X15      SX!405        g)z
There should be a class method of L{amp.Command} which accepts
a mapping of argument names to serialized forms and returns a
similar mapping whose values have been parsed via the
command's argument schema.
r  r  r  N)r  rU  r  r  r   s       r&   test_parseArguments CommandTests.test_parseArguments
  sA     8V$$33GFv()	
r)   c                    ^ ^^ [        5       mTR                  " [        R                  5      n[	        5       mU" [        TS95      nUR                  UUU 4S j5        U$ )z
Making a remote call on a L{amp.Command} subclass which
overrides the C{parseArguments} method should call that
C{parseArguments} method to get the arguments.
r  c                 D   > TR                  TR                  ST0T45      $ r  )rU  r  )r  argumentr   r%   s    r&   r  @CommandTests.test_responderCallsParseArguments.<locals>.<lambda>6
  s%    ((00GX3F2Qr)   )r  r  r  r   r  r   r  )r%   r   r   r  r   s   `  @@r&   !test_responderCallsParseArguments.CommandTests.test_responderCallsParseArguments+
  sT     %&,,-?-K-KL	8T12	

 r)   c                     [        5       n[        5       nSU0nS[        U5      [        U5      4-  nU R                  [        R	                  X15      SUR                  S5      05        g)z
There should be a class method of L{amp.Command} which accepts
a mapping of argument names to objects and returns a similar
mapping whose values have been serialized via the command's
argument schema.
r  r  r  r=  N)r  r/  rU  r  r  r>  )r%   r   r  rx   r  s        r&   test_makeArgumentsCommandTests.test_makeArguments<
  s`     88H%2h<H66$227Eu||G,-	
r)   c                     [        5       nSS0n[        R                  X!5      nU R                  [	        U5      [
        5        g)zl
L{amp.Command.makeArguments}'s return type should be the type
of the result of L{amp.Command.commandType}.
r  r  N)r  r  r  rp  rV  r  )r%   r   rx   r   s       r&   !test_makeArgumentsUsesCommandType.CommandTests.test_makeArgumentsUsesCommandTypeL
  s<    
 8K(AOO
 	d6lE*r)   c                    ^ ^^ [        5       m[        5       mTR                  [        TS9nUUU 4S jnUR	                  U5        U$ )z
Making a remote call on a L{amp.Command} subclass which
overrides the C{makeArguments} method should call that
C{makeArguments} method to get the response.
r  c                 F   > TR                  TR                  ST0T45        g r  )rU  r  )r  r  r  r%   s    r&   r	  CCommandTests.test_callRemoteCallsMakeArguments.<locals>.gotResponsec
  s&    --(0CV/Lr)   )r  r  r   r  r  )r%   r   r	  r  r  s   `  @@r&   !test_callRemoteCallsMakeArguments.CommandTests.test_callRemoteCallsMakeArgumentsY
  sD     #$8$$%7x$H	
 	[)r)   c           	          U R                  [        R                  [        R                  [        S[        5       S9S5        g)z
L{Command.makeArguments} raises L{amp.InvalidSignature} if the objects
dictionary passed to it includes a key which does not correspond to the
Python identifier for a defined argument.
rO   )rO   bogusArgumentN)r^  r   r  r|   r  r   r  r/   s    r&   test_extraArgumentsDisallowed*CommandTests.test_extraArgumentsDisallowedk
  s3     	  wfh7		
r)   c           
      ~    U R                  [        R                  [        R                  [        SSS0SS0D6S5        g)a  
If a command argument conflicts with a Python keyword, the
untransformed argument name is not allowed as a key in the dictionary
passed to L{Command.makeArguments}.  If it is supplied,
L{amp.InvalidSignature} is raised.

This may be a pointless implementation restriction which may be lifted.
The current behavior is tested to verify that such arguments are not
silently dropped on the floor (the previous behavior).
rO   requiredprintzprint valueNrG   )r^  r   r  r|   r  r   r/   s    r&   test_wireSpellingDisallowed(CommandTests.test_wireSpellingDisallowedx
  s<     	  >z>g}%=>		
r)   c                 n     " S S[         R                  5      nU R                  SUR                  5        g)zR
A L{Command} subclass without a defined C{commandName} that's
not a byte string.
c                       \ rS rSrSrSrg)PCommandTests.test_commandNameDefaultsToClassNameAsByteString.<locals>.NewCommandi
  z
A new command.
rG   Nr`   rG   r)   r&   
NewCommandr,  
  s    r)   r-  s
   NewCommandN)r   CommandrU  r   r%   r-  s     r&   /test_commandNameDefaultsToClassNameAsByteString<CommandTests.test_commandNameDefaultsToClassNameAsByteString
  s*    	 	
 	
(>(>?r)   c                     U R                  [        [        S[        R                  4SS05      nU R                  [        U5      S5        g)zY
A L{Command} subclass cannot be defined with a C{commandName} that's
not a byte string.
r-  r   FOOz2^Command names must be byte strings, got: u?'FOO'$Nr^  r_  rV  r   r.  assertRegexrE   r%   r   s     r&   !test_commandNameMustBeAByteString.CommandTests.test_commandNameMustBeAByteString
  sD    
 !!t\CKK>M5;Q
 	JL	
r)   c                     U R                  [        [        S[        R                  4SS/05      nU R                  [        U5      S5        g)zC
A L{Command} subclass's C{arguments} must have byte string names.
r-  r   r{  Nz3^Argument names must be byte strings, got: u?'foo'$Nr4  r6  s     r&   /test_commandArgumentsMustBeNamedWithByteStrings<CommandTests.test_commandArgumentsMustBeNamedWithByteStrings
  sJ     !![[N=/*
 	JM	
r)   c                     U R                  [        [        S[        R                  4SS/05      nU R                  [        U5      S5        g)zB
A L{Command} subclass's C{response} must have byte string names.
r-  r   r:  z3^Response names must be byte strings, got: u?'foo'$Nr4  r6  s     r&   .test_commandResponseMustBeNamedWithByteStrings;CommandTests.test_commandResponseMustBeNamedWithByteStrings
  sF     !!t\CKK>J;X
 	JM	
r)   c                 z     " S S[         R                  5      nU R                  [        S0UR                  5        g)z>
A L{Command} subclass's C{errors} is coerced into a C{dict}.
c                       \ rS rSr\S4/rSrg)DCommandTests.test_commandErrorsIsConvertedToDict.<locals>.NewCommandi
     ZDErG   N)r@   rA   rB   rC   r  r   rF   rG   r)   r&   r-  rB  
  s    (&12Fr)   r-  rC  N)r   r.  rU  r  r   r/  s     r&   #test_commandErrorsIsConvertedToDict0CommandTests.test_commandErrorsIsConvertedToDict
  s0    
	3 	3 	+V4j6G6GHr)   c           	          U R                  [        [        S[        R                  4S[
        S4/05      nU R                  [        U5      S5        g)zH
A L{Command} subclass's C{errors} must map exceptions to byte strings.
r-  r   r{  z0^Error names must be byte strings, got: u?'foo'$Nr^  r_  rV  r   r.  r  r5  rE   r6  s     r&   5test_commandErrorsMustUseBytesForOnWireRepresentationBCommandTests.test_commandErrorsMustUseBytesForOnWireRepresentation
  sN     !![[N*E234
 	U%WXr)   c                 z     " S S[         R                  5      nU R                  [        S0UR                  5        g)zC
A L{Command} subclass's C{fatalErrors} is coerced into a C{dict}.
c                       \ rS rSr\S4/rSrg)ICommandTests.test_commandFatalErrorsIsConvertedToDict.<locals>.NewCommandi
  rC  rG   N)r@   rA   rB   rC   r  r   rF   rG   r)   r&   r-  rL  
  s    -v67Kr)   r-  rC  N)r   r.  rU  r  r   r/  s     r&   (test_commandFatalErrorsIsConvertedToDict5CommandTests.test_commandFatalErrorsIsConvertedToDict
  s0    
	8 	8 	+V4j6L6LMr)   c           	          U R                  [        [        S[        R                  4S[
        S4/05      nU R                  [        U5      S5        g)zM
A L{Command} subclass's C{fatalErrors} must map exceptions to byte
strings.
r-  r   r{  z6^Fatal error names must be byte strings, got: u?'foo'$NrG  r6  s     r&   :test_commandFatalErrorsMustUseBytesForOnWireRepresentationGCommandTests.test_commandFatalErrorsMustUseBytesForOnWireRepresentation
  sQ    
 !![[N/789
 	JS	
r)   rG   N)r@   rA   rB   rC   rD   r  r  r  r  r  r  r  r  r#  r(  r0  r7  r;  r>  rD  rH  rM  rP  rF   rG   r)   r&   r  r  	  sf    I
 
"
 +$

$@


	
IYN
r)   r  c                   $    \ rS rSrSrS rS rSrg)ListOfTestsMixini
  a  
Base class for testing L{ListOf}, a parameterized zero-or-more argument
type.

@ivar elementType: Subclasses should set this to an L{Argument}
    instance.  The tests will make a L{ListOf} using this.

@ivar strings: Subclasses should set this to a dictionary mapping some
    number of keys -- as BYTE strings -- to the correct serialized form
    for some example values. These should agree with what L{elementType}
    produces/accepts.

@ivar objects: Subclasses should set this to a dictionary with the same
    keys as C{strings} -- as NATIVE strings -- and with values which are
    the lists which should serialize to the values in the C{strings}
    dictionary.
c                 >   [         R                  " U R                  5      n[         R                  " 5       nU R                   H=  nUR                  UR                  S5      X R                  R                  5       S5        M?     U R                  X R                  5        g)ai  
L{ListOf.toBox} extracts the list of objects from the C{objects}
dictionary passed to it, using the C{name} key also passed to it,
serializes each of the elements in that list using the L{Argument}
instance previously passed to its initializer, combines the serialized
results, and inserts the result into the C{strings} dictionary using
the same C{name} key.
r=  N)
r   ListOfelementTyperT  rx   ry   r>  copyrU  rw   )r%   
stringListrw   rS  s       r&   
test_toBoxListOfTestsMixin.test_toBox  sl     ZZ 0 01
**,<<CSZZ0'<<;L;L;NPTU  ,,/r)   c                     [         R                  " U R                  5      n0 nU R                   H.  nUR	                  X0R                  R                  5       US5        M0     U R                  X R                  5        g)H
L{ListOf.fromBox} reverses the operation performed by L{ListOf.toBox}.
N)r   rU  rV  rw   fromBoxrW  rU  rx   )r%   rX  rx   rS  s       r&   test_fromBoxListOfTestsMixin.test_fromBox  s\     ZZ 0 01
<<CsLL$5$5$7$G  ,,/r)   rG   N)r@   rA   rB   rC   rD   rY  r^  rF   rG   r)   r&   rS  rS  
  s    $00r)   rS  c                   X    \ rS rSrSr\R                  " 5       rSSSS.r/ S// SQS	.r	S
r
g)ListOfStringsTestsi  z2
Tests for L{ListOf} combined with L{amp.String}.
r)       foos    bar baz quux   empty   single   multiplerx  )ry  s   bazs   quuxemptysinglemultiplerG   N)r@   rA   rB   rC   rD   r   r   rV  rw   rx   rF   rG   r)   r&   ra  ra    s7     **,K !:G vh<UVGr)   ra  c                   v    \ rS rSrSr\R                  " 5       rSS-  rSSSS\4-  SS	.r	/ S
// SQ\/S/S.r
Srg)ListOfIntegersTestsi,  z3
Tests for L{ListOf} combined with L{amp.Integer}.
l   ]OSt7s
Xg&}r)   s    10s    1 20 500s    t%ds    -1)rd  re  rf  s   huges   negativerp  )r      i  r  )rh  ri  rj  hugenegativerG   N)r@   rA   rB   rC   rD   r   r   rV  rn  rw   rx   rF   rG   r)   r&   rl  rl  ,  se     ++-K 	C
D	E 	  6$("G $ DGr)   rl  c                   X    \ rS rSrSr\R                  " 5       rSSSS.r/ S// SQS	.r	S
r
g)ListOfUnicodeTestsiI  z3
Tests for L{ListOf} combined with L{amp.Unicode}.
r)   rb  s    ☃ Hello worldrc  r{  )u   ☃r|   r  rg  rG   N)r@   rA   rB   rC   rD   r   r   rV  rw   rx   rF   rG   r)   r&   rq  rq  I  s:     ++-K !FG '5Gr)   rq  c                      \ rS rSrSr\R                  " 5       rSSSSS\R                  " S5      R                  5       R                  S	5      -   S
-   \R                  " S5      R                  5       R                  S	5      -   S.r/ \R                  " S5      /\R                  " S5      \R                  " S5      /\R                  " S5      \R                  " S5      \R                  " S5      \R                  " S5      \R                  " S5      \R                  " S5      /\R                  " S5      \R                  " S5      /S.rSrg)ListOfDecimalTestsi]  z3
Tests for L{ListOf} combined with L{amp.Decimal}.
r)   s    1.1s    Infinity 	-InfinitysC    3.141E+5 
0.00003141 3.141E-7 	-3.141E+5 -0.00003141 	-3.141E-7s    0e6r=  s    z1.5E-9)rd  re  s   extremes	   scientists   engineerz1.1Infinityz	-Infinityz3.141E5z3.141e-5z3.141E-7z-3.141e5z	-3.141E-5z	-3.141e-7)rh  ri  extreme	scientistengineerrG   N)r@   rA   rB   rC   rD   r   DecimalrV  decimalto_eng_stringr>  rw   rx   rF   rG   r)   r&   rs  rs  ]  s-    ++-K !8A ooe$224;;GDE ooh'557>>wGHG ??5)*OOJ'OOK(
 OOI&OOJ'OOJ'OOJ'OOK(OOK(
 OOE"OOH%
'Gr)   rs  c                       \ rS rSrSr\R                  " 5       rSS0rS\	R                  " S5      \	R                  " S5      \	R                  " S5      \	R                  " S	5      /0r
S
 rSrg)ListOfDecimalNanTestsi  zK
Tests for L{ListOf} combined with L{amp.Decimal} for not-a-number values.
s   nans    NaN -NaN sNaN -sNaNnanNaNz-NaNsNaNz-sNaNc                 T   S nS nS n[         R                  " U R                  5      n0 nU R                   H.  nUR	                  X`R                  R                  5       US5        M0     US   nU R                  U" US   5      =(       a    U" US   5      (       + 5        U R                  U" US   5      =(       a    U" US   5      5        U R                  U" US   5      =(       a    U" US   5      (       + 5        U R                  U" US	   5      =(       a    U" US	   5      5        g)
r\  c                 H    S[        U 5      ;   =(       a    S[        U 5      ;  $ )Nr  r  rE   rz  s    r&   is_qnan3ListOfDecimalNanTests.test_fromBox.<locals>.is_qnan  s    CL(GV3w<-GGr)   c                     S[        U 5      ;   $ )Nr  r  r  s    r&   is_snan3ListOfDecimalNanTests.test_fromBox.<locals>.is_snan  s    S\))r)   c                     S[        U 5      ;   $ )N-r  r  s    r&   	is_signed5ListOfDecimalNanTests.test_fromBox.<locals>.is_signed  s    #g,&&r)   Nr~  r   r   r  r  )r   rU  rV  rw   r]  rW  rh  )r%   r  r  r  rX  rx   rS  ns           r&   r^  "ListOfDecimalNanTests.test_fromBox  s    	H	*	' ZZ 0 01
<<CsLL$5$5$7$G  EN!=i!o*=>!9)AaD/:!=i!o*=>!9)AaD/:r)   rG   N)r@   rA   rB   rC   rD   r   ry  rV  rw   rz  rx   r^  rF   rG   r)   r&   r}  r}    si     ++-K 	CG
 	OOE"OOF#OOF#OOG$	
G;r)   r}  c                       \ rS rSrSrS rSrg)DecimalTestsi  z
Tests for L{amp.Decimal}.
c                     [         R                  " 5       nU R                  [        UR                  S5        U R                  [        UR                  S5        U R                  [        UR                  S5        g)zr
L{amp.Decimal.toString} raises L{ValueError} if passed an object which
is not an instance of C{decimal.Decimal}.
1.234X9v?  N)r   ry  r^  
ValueErrorrk  r%   r  s     r&   test_nonDecimalDecimalTests.test_nonDecimal  sW    
 ;;=*h&7&7A*h&7&7?*h&7&7>r)   rG   N)r@   rA   rB   rC   rD   r  rF   rG   r)   r&   r  r    s    ?r)   r  c                   $    \ rS rSrSrS rS rSrg)
FloatTestsi  z
Tests for L{amp.Float}.
c                     [         R                  " 5       nU R                  [        UR                  S5        U R                  [        UR                  S5        U R                  [        UR                  S5        g)zY
L{amp.Float.toString} raises L{ValueError} if passed an object which
is not a L{float}.
r     1.234r  N)r   Floatr^  r  rk  r  s     r&   test_nonFloatFloatTests.test_nonFloat  sW    
 99;*h&7&7A*h&7&7B*h&7&7>r)   c                 p    [         R                  " 5       nU R                  UR                  S5      S5        g)zI
L{amp.Float.toString} returns a bytestring when it is given a L{float}.
r  r  N)r   r  rU  rk  r  s     r&   
test_floatFloatTests.test_float  s*     99;**518<r)   rG   N)r@   rA   rB   rC   rD   r  r  rF   rG   r)   r&   r  r    s    ?=r)   r  c                      \ rS rSrSr\R                  " 5       rSSSSSS.r\	R                  " S	S
SSSS\R                  S9\	R                  " S	S
SSSS\" SSS5      S9/\	R                  " S	S
SSSS\" SSS5      S9/\	R                  " S	S
SSSS\" SSS5      S9/\	R                  " S	S
SSSS\" SSS5      S9/\	R                  " S	S
SSSS\" SSS5      S9\	R                  " S	S
SSSS\" SSS5      S9/S.rSrg)ListOfDateTimeTestsi  z4
Tests for L{ListOf} combined with L{amp.DateTime}.
sD     2010-12-25T00:00:00.000000-00:00  2010-12-25T00:00:00.000000-00:00s"     2010-12-25T00:00:00.000000+01:00s"     2010-12-25T00:00:00.000000+03:30s"     2010-12-25T00:00:00.000000-05:00sD     2010-12-25T00:00:00.000000+03:19  2010-12-25T00:00:00.000000-06:59)s	   christmass   christmas in eus   christmas in irans   christmas in nycs   previous tests        r   )tzinfo+r   r     r           ;   )	christmaszchristmas in euzchristmas in iranzchristmas in nyczprevious testsrG   N)r@   rA   rB   rC   rD   r   DateTimerV  rw   datetimeutctzrx   rF   rG   r)   r&   r  r    sB    ,,.K4GIH4G dBAq!CGGDdBAq!BsAqMJ

 dBAq!BsAqMJ
 dBAq!BsArNK
 dBAq!BsAqMJ
 dBAq!BsArNKdBAq!BsArNK
Gr)   r  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
ListOfOptionalTestsi  zc
Tests to ensure L{ListOf} AMP arguments can be omitted from AMP commands
via the 'optional' flag.
c                     [         R                  " [         R                  " 5       5      nU R                  [        UR
                  S[         R                  " 5       SS0S5        g)zV
L{ListOf.toBox} raises C{TypeError} when passed a value of L{None}
for the argument.
   omittedomittedN)r   rU  r   r^  r_  ry   rT  r%   rX  s     r&   1test_requiredArgumentWithNoneValueRaisesTypeErrorEListOfOptionalTests.test_requiredArgumentWithNoneValueRaisesTypeError  sI    
 ZZ.
JJL	
r)   c                     [         R                  " [         R                  " 5       SS9n[         R                  " 5       nUR	                  SUSS0S5        U R                  U0 5        g)z
L{ListOf.toBox} silently omits serializing any argument with a
value of L{None} that is designated as optional for the protocol.
Tr   r  N)r   rU  r   rT  ry   rU  )r%   rX  rw   s      r&   )test_optionalArgumentWithNoneValueOmitted=ListOfOptionalTests.test_optionalArgumentWithNoneValueOmitted   sN    
 ZZ=
**,Wz4.@$G"%r)   c                     [         R                  " [         R                  " 5       5      nU R                  [        UR
                  S[         R                  " 5       SS0S5        g)zd
L{ListOf.toBox} raises C{KeyError} if the argument's key is not
present in the objects dictionary.
   ommitedsomeOtherKeyr   N)r   rU  r   r^  KeyErrorry   rT  r  s     r&   1test_requiredArgumentWithKeyMissingRaisesKeyErrorEListOfOptionalTests.test_requiredArgumentWithKeyMissingRaisesKeyError*  sI    
 ZZ.
JJLQ	
r)   c                     [         R                  " [         R                  " 5       SS9nUR                  S[         R                  " 5       SS0S5        g)z
L{ListOf.toBox} silently omits serializing any argument designated
as optional whose key is not present in the objects dictionary.
Tr   r  s   someOtherKeyr   N)r   rU  r   ry   rT  r  s     r&   *test_optionalArgumentWithKeyMissingOmitted>ListOfOptionalTests.test_optionalArgumentWithKeyMissingOmitted9  s9    
 ZZ=
SZZ\OQ3GNr)   c                     [         R                  " [         R                  " 5       SS9n0 nUR                  S0 US5        U R	                  USS05        g)zi
L{ListOf.fromBox} correctly reverses the operation performed by
L{ListOf.toBox} for optional arguments.
Tr   r  Nr  )r   rU  r   r]  rU  )r%   rX  rx   s      r&   .test_omittedOptionalArgumentDeserializesAsNoneBListOfOptionalTests.test_omittedOptionalArgumentDeserializesAsNoneA  sH    
 ZZ=
:r7D99d"34r)   rG   N)r@   rA   rB   rC   rD   r  r  r  r  r  rF   rG   r)   r&   r  r    s!    

&
O5r)   r  c                   B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)UNIXStringTransportiL  aZ  
An in-memory implementation of L{interfaces.IUNIXTransport} which collects
all data given to it for later inspection.

@ivar _queue: A C{list} of the data which has been given to this transport,
    eg via C{write} or C{sendFileDescriptor}.  Elements are two-tuples of a
    string (identifying the destination of the data) and the data itself.
c                     Xl         / U l        g)zX
@param descriptorFuzz: An offset to apply to descriptors.
@type descriptorFuzz: C{int}
N_fuzz_queue)r%   descriptorFuzzs     r&   r'   UNIXStringTransport.__init__W  s    
 $
r)   c                 V    U R                   R                  SXR                  -   45        g )NfileDescriptorReceived)r  r3   r  )r%   
descriptors     r&   sendFileDescriptor&UNIXStringTransport.sendFileDescriptor_  s!    4j::6MNOr)   c                 >    U R                   R                  SU45        g )Nr4   )r  r3   rC  s     r&   r.   UNIXStringTransport.writeb  s    ND12r)   c                 8    U H  nU R                  U5        M     g r+   )r.   )r%   seqr,   s      r&   writeSequence!UNIXStringTransport.writeSequencee  s    DJJt r)   c                 v    U R                   R                  S[        [        R                  " 5       5      45        g )Nr:   )r  r3   r   r   ri  r/   s    r&   rZ  "UNIXStringTransport.loseConnectioni  s(    ,ge6J6J6L.MNOr)   c                 .    [         R                  " S5      $ )Nz/tmp/some-pathr   UNIXAddressr/   s    r&   ri  UNIXStringTransport.getHostl  s    ""#344r)   c                 .    [         R                  " S5      $ )Nz/tmp/another-pathr  r/   s    r&   rr   UNIXStringTransport.getPeero  s    ""#677r)   r  N)r@   rA   rB   rC   rD   r'   r  r.   r  rZ  ri  rr   rF   rG   r)   r&   r  r  L  s,    P3P58r)   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	DescriptorTestsix  z
Tests for L{amp.Descriptor}, an argument type for passing a file descriptor
over an AMP connection over a UNIX domain socket.
c                    SU l         [        U R                   S9U l        [        R                  " [        R
                  " [        R                  " 5       5      5      U l        U R                  R                  U R                  5        g )Nr  )r  )	fuzzr  r-   r   rn  r  r  r   ro  r/   s    r&   r  DescriptorTests.setUp~  sU    	,DIIF--c.?.?@R@R@T.UV$$T^^4r)   c                 $   [         R                  " 5       nU R                  R                  S5        U R                  R                  S5        U R                  R                  S5        U R	                  SUR                  SU R                  5      5        U R	                  SUR                  SU R                  5      5        U R	                  SUR                  SU R                  5      5        U R	                  0 U R                  R                  5        g)aD  
L{Descriptor.fromStringProto} constructs a file descriptor value by
extracting a previously received file descriptor corresponding to the
wire value of the argument from the L{_DescriptorExchanger} state of the
protocol passed to it.

This is a whitebox test which involves direct L{_DescriptorExchanger}
state inspection.
r  r  r   012N)r   
Descriptorr   r  rU  rt   _descriptorsr  s     r&   test_fromStringProto$DescriptorTests.test_fromStringProto  s     >>#,,Q/,,Q/,,Q/H44S$--HIH44S$--HIH44S$--HIT]]778r)   c                    [         R                  " 5       nU R                  SUR                  SU R                  5      5        U R                  SSU R
                  -   4U R                  R                  R                  S5      5        U R                  SUR                  SU R                  5      5        U R                  SSU R
                  -   4U R                  R                  R                  S5      5        U R                  SUR                  SU R                  5      5        U R                  SSU R
                  -   4U R                  R                  R                  S5      5        U R                  0 U R                  R                  5        g	)
a  
To send a file descriptor, L{Descriptor.toStringProto} uses the
L{IUNIXTransport.sendFileDescriptor} implementation of the transport of
the protocol passed to it to copy the file descriptor.  Each subsequent
descriptor sent over a particular AMP connection is assigned the next
integer value, starting from 0.  The base ten string representation of
this value is the byte encoding of the argument.

This is a whitebox test which involves direct L{_DescriptorExchanger}
state inspection and mutation.
   0r  r  r   r  r     2r  N)
r   r  rU  r  r   r  r-   r  r  r  r  s     r&   test_toStringProto"DescriptorTests.test_toStringProto  s5    >>#x55aGH%q499}5t~~7L7L7P7PQR7S	
 	x55aGH%q499}5t~~7L7L7P7PQR7S	
 	x55aGH%q499}5t~~7L7L7P7PQR7S	
 	T]]778r)   c                    SnUR                  S5      n0 nSnX0n[        R                  " 5       nUR                  X#UR	                  5       U R
                  5        [        R                  " [        R                  " [        R                  " 5       5      5      nU R                  R                   H  n[        XxS   5      " USS 6   M     0 n	UR                  X#R	                  5       X5        U R                  X@R                  -   X   5        g)z
L{amp.Descriptor.fromBox} can interpret an L{amp.AmpBox} constructed by
L{amp.Descriptor.toBox} to reconstruct a file descriptor value.
alphar=     r   r   N)r>  r   r  ry   rW  r   rn  r  r  r-   r  getattrr]  rU  r  )
r%   rm   nameAsBytesrw   r  sendObjectsr  receivereventreceiveObjectss
             r&   test_roundTripDescriptorTests.test_roundTrip  s    
 kk'*
(>>#{[-=-=-?O(():):3;M;M;O)PQ^^**EHAh'qr3 + llnnO
 	ii/1EFr)   )r  r   r-   N)
r@   rA   rB   rC   rD   r  r  r  r  rF   rG   r)   r&   r  r  x  s    
59&96Gr)   r  c            
       z    \ rS rSrSrSr\" SSS5      r\R                  " SSSSS	S
S\5      r	S r
S rS rS rSrg)DateTimeTestsi  zG
Tests for L{amp.DateTime}, L{amp._FixedOffsetTZInfo}, and L{amp.utc}.
s    9876-01-23T12:34:56.054321-01:23r  r      i&  r  "   8   i1  c                 p    [         R                  " 5       nU R                  [        UR                  S5        g)z
L{amp.DateTime.fromString} raises L{ValueError} when passed a string
which does not represent a timestamp in the proper format.
abcN)r   r  r^  r  ri  r%   rl   s     r&   test_invalidString DateTimeTests.test_invalidString  s%    
 LLN*allE:r)   c                     [         R                  " 5       nU R                  [        UR                  [
        R
                  " SSSSSS5      5        g)zw
L{amp.DateTime.toString} raises L{ValueError} when passed a naive
datetime (a datetime with no timezone information).
r  r  r  r   N)r   r  r^  r  rk  r  r  s     r&   test_invalidDatetime"DateTimeTests.test_invalidDatetime  s=    
 LLN

H$5$5dBAq!$L	
r)   c                     [         R                  " 5       nUR                  U R                  5      nU R	                  X R
                  5        g)zz
L{amp.DateTime.fromString} returns a C{datetime.datetime} with all of
its fields populated from the string passed to it.
N)r   r  ri  r  rU  r  r%   r  r\  s      r&   test_fromStringDateTimeTests.test_fromString  s5    
 <<>##DKK0,r)   c                     [         R                  " 5       nUR                  U R                  5      nU R	                  X R
                  5        g)z
L{amp.DateTime.toString} returns a C{str} in the wire format including
all of the information from the C{datetime.datetime} passed into it,
including the timezone offset.
N)r   r  rk  r  rU  r  r  s      r&   test_toStringDateTimeTests.test_toString  s5     <<>!!$++.,r)   rG   N)r@   rA   rB   rC   rD   r  r  r  r  r  r  r  r  r  rF   rG   r)   r&   r  r    sM     1FQ^FtQBBvFF;
--r)   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	UTCTestsi  z
Tests for L{amp.utc}.
c                 b    U R                  [        R                  R                  S5      S5        g)z(
L{amp.utc.tzname} returns C{"+00:00"}.
Nz+00:00)rU  r   r  tznamer/   s    r&   test_tznameUTCTests.test_tzname  s!     	-x8r)   c                     U R                  [        R                  R                  S5      [        R
                  " S5      5        g)z*
L{amp.utc.dst} returns a zero timedelta.
Nr   )rU  r   r  dstr  	timedeltar/   s    r&   test_dstUTCTests.test_dst  s,     	T*H,>,>q,ABr)   c                     U R                  [        R                  R                  S5      [        R
                  " S5      5        g)z0
L{amp.utc.utcoffset} returns a zero timedelta.
Nr   )rU  r   r  	utcoffsetr  r   r/   s    r&   test_utcoffsetUTCTests.test_utcoffset  s.     	**40(2D2DQ2GHr)   c                 >    U R                  [        [        SSS5        g)z{
L{amp._FixedOffsetTZInfo.fromSignHoursMinutes} raises L{ValueError} if
passed an offset sign other than C{'+'} or C{'-'}.
?r   N)r^  r  r  r/   s    r&   test_badSignUTCTests.test_badSign  s    
 	*b#q!4r)   rG   N)
r@   rA   rB   rC   rD   r  r!  r%  r)  rF   rG   r)   r&   r  r    s    9CI5r)   r  c                   *    \ rS rSrSrS rS rS rSrg)RemoteAmpErrorTestsi  z"
Tests for L{amp.RemoteAmpError}.
c                 h    [         R                  " SS5      nU R                  S[        U5      5        g)zj
L{amp.RemoteAmpError} renders the given C{errorCode} (C{bytes}) and
C{description} into a native string.
   BROKENSomething has brokenz"Code<BROKEN>: Something has brokenNr   rb  rU  rE   r6  s     r&   test_stringMessage&RemoteAmpErrorTests.test_stringMessage   s,    
 ""9.DE=s5zJr)   c                 h    [         R                  " SS5      nU R                  S[        U5      5        g)zt
When C{errorCode} contains non-ASCII characters, L{amp.RemoteAmpError}
renders then as backslash-escape sequences.
s   BROKEN-r/  z'Code<BROKEN-\xff>: Something has brokenNr0  r6  s     r&   &test_stringMessageReplacesNonAsciiText:RemoteAmpErrorTests.test_stringMessageReplacesNonAsciiText(  s,    
 "">3IJCSZPr)   c                     [        [        S5      5      n[        R                  " SSUS9nU R	                  [        U5      S5        g)z[
L{amp.RemoteAmpError} renders local errors with a "(local)" marker and
a brief traceback.
zSomething came looser.  r/  )localz}^Code<BROKEN> [(]local[)]: Something has broken
Traceback [(]failure with no frames[)]: <.+Exception.>: Something came loose
N)r   r   r   rb  r5  rE   )r%   r  r   s      r&   "test_stringMessageWithLocalFailure6RemoteAmpErrorTests.test_stringMessageWithLocalFailure0  sC    
 )$:;<""9.DGTJ9	
r)   rG   N)	r@   rA   rB   rC   rD   r1  r4  r8  rF   rG   r)   r&   r,  r,    s    KQ
r)   r,  )rD   r  rz  typingr   r   r   r   unittestr   zope.interfacer   zope.interface.verifyr	   r
   rt  r   r   r   r   r   r   twisted.internet.testingr   twisted.protocolsr   twisted.pythonr   twisted.python.failurer   twisted.testr   twisted.trial.unittestr   r   _ssl	supportedImportErrorr  IReactorSSLr   reactorLacksSSL_FixedOffsetTZInfofromSignHoursMinutesr  r  r   r   rI   r   r]   rc   rf   r  ri   r.  r|   r   r   r   r   r   r   r   r   r   ProtocolSwitchCommandr   rv  r   r   r   r   r   r*  r/  r5  r:  r@  rF  rP  rf  r  r  r  r  r  r  r
  r  r  r!  r  r  rX  r  r  r  r  r  r  r  r  r)  r:  r<  r>  r@  rB  rD  rK  rO  rT  rj  rl  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rS  ra  rl  rq  rs  r}  r  r  r  r  IUNIXTransportr  
ITransportr  r  r  r,  rG   r)   r&   <module>rN     s  

   0 0  & ; Q Q 4 ! # *  +, >>;GG((11OO 	00&6!! &6R,cgg ,, ,,) ,1i 1
CLL 
QCKK Q(E 
1#++ 1&#++ #3;; #ckk "#++ "Ackk A.3;; .,#++ ,
+c// +x--   * 		 	$cgg $&U&_ U&p('E (*!? *$3< 3" (5LN(++  2( 2:C.8 C.L* *(#1 #1LL88 L8^0S[[ 0D#$$ DD D) D D0\(( \(~ ! - I@( I@X
\
?x \
?~ 
 $Y   '_ ' $%MNf5x f5 O &f5R?
8 ?
DY ) #++ { 	{ 		 CGG 	 
%sww 
%%377 % 0
h 0
f,W ,W^ ?H $%MN%|X % O &%P'5 ' $%MNFh F O &F  $%MN\8  O &"% %69s{{ 9" "J6 6: enenDcN 7O z
8 z
z*0 *0ZW#3 W ($4 :#3 (,#3 ,^/;H&6 /;d?8 ? = =.#($4 #L>5( >5B Z&&'#8 #8 (#8N J!!#6 7 J%%': ;RGh RGj,-H ,-^5x 5>#
( #
g  
Cs    X? ?Y
Y