
    Vhc                        d Z ddlmZmZ ddlmZmZ ddlmZ ddl	m
Z
 	 ddlmZ ddlmZ d	d
j!                  dd      z   Zdj!                  dd      Zdj!                  dd      Zdj!                  dd      Zdj!                  dd      Zdj!                  dd      Zdj!                  dd      Zdj!                  dd      Z G d d      Z G d de
j4                        Z G d de      Z G d de
j4                        Z G d de
j4                        Z G d de
j4                        Z G d  d!e
j4                        Z  eejB                         G d" d#             Z" eejB                         G d$ d%             Z# G d& d'e
j4                        Z$ G d( d)e
j4                        Z% G d* d+ejL                        Z' G d, d-e
j4                        Z(y# e$ r dZY w xY w).z(
Session Initialization Protocol tests.
    )checkersportal)deferreactor)sip)unittest)AsyncioSelectorReactorN)implementerz


z?INVITE sip:foo SIP/2.0
From: mo
To: joe
Content-Length: 4

abcd
z
z-INVITE sip:foo SIP/2.0
From: mo
To: joe

1234zHINVITE sip:foo SIP/2.0
From: mo
To: joe
Content-Length: 4

1234

lalalalzINVITE sip:foo SIP/2.0
From: mo
To: joe
Content-Length: 0

INVITE sip:loop SIP/2.0
From: foo
To: bar
Content-Length: 4

abcdINVITE sip:loop SIP/2.0
From: foo
To: bar
Content-Length: 4

1234z4SIP/2.0 200 OK
From:  foo
To:bar
Content-Length: 0

z.INVITE sip:foo SIP/2.0
f: mo
t: joe
l: 4

abcdz=INVITE sip:foo SIP/2.0
Via: SIP/2.0/UDP 10.0.0.1:5060;rport

a  SIP/2.0 200 OK
Via: SIP/2.0/UDP server10.biloxi.com
    ;branch=z9hG4bKnashds8;received=192.0.2.3
Via: SIP/2.0/UDP bigbox3.site3.atlanta.com
    ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2
Via: SIP/2.0/UDP pc33.atlanta.com
    ;branch=z9hG4bK776asdhds ;received=192.0.2.1
To: Bob <sip:bob@biloxi.com>;tag=a6c85cf
From: Alice <sip:alice@atlanta.com>;tag=1928301774
Call-ID: a84b4c76e66710@pc33.atlanta.com
CSeq: 314159 INVITE
Contact: <sip:bob@192.0.2.4>
Content-Type: application/sdp
Content-Length: 0

c                       e Zd Zd Zy)	TestRealmc                 *    t         j                  d d fS )Nc                       y N r       E/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_sip.py<lambda>z)TestRealm.requestAvatar.<locals>.<lambda>   s    r   )r   IContact)selfavatarIdmind
interfacess       r   requestAvatarzTestRealm.requestAvatar   s    ||T<//r   N)__name__
__module____qualname__r   r   r   r   r   r   ~   s    0r   r   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)MessageParsingTestsc                 n    g | _         t        j                  | j                   j                        | _        y r   )lr   MessagesParserappendparserr   s    r   setUpzMessageParsingTests.setUp   s#    ((7r   c                 n    | j                   j                  |       | j                   j                          y r   r$   dataReceiveddataDone)r   messages     r   feedMessagezMessageParsingTests.feedMessage   s$      )r   c                 8   | j                  |j                  |       | j                  |j                  j                         |       | j                  |j                  |       | j                  |j
                  |       | j                  |j                  d       y)z$
        Validate Requests.
           N)assertEqualmethoduritoStringheadersbodyfinished)r   mr0   r1   r3   r4   s         r   validateMessagez#MessageParsingTests.validateMessage   so     	6*)3/G,&Q'r   c           	          | j                   }| j                  t               | j                  t	        |      d       | j                  |d   dddgdgdgdd	       y 
Nr.   r   INVITEsip:foomojoe4fromtozcontent-lengthabcd)r!   r,   request1r/   lenr7   r   r!   s     r   
testSimplezMessageParsingTests.testSimple   [    FF"Q#aDVE7seD	
r   c           	         | j                   }| j                  t               | j                  t               | j	                  t        |      d       | j                  |d   dddgdgdgdd	       | j                  |d
   dddgdgdd       y )N   r   r:   r;   r<   r=   r>   r?   rB   r.   )r@   rA   1234)r!   r,   rC   request2r/   rD   r7   rE   s     r   testTwoMessagesz#MessageParsingTests.testTwoMessages   s    FF""Q#aDVE7seD	
 	aD(Iug'F	
r   c           	          | j                   }| j                  t               | j                  t	        |      d       | j                  |d   dddgdgdgdd	       y )
Nr.   r   r:   r;   r<   r=   r>   r?   rJ   )r!   r,   request3r/   rD   r7   rE   s     r   testGarbagezMessageParsingTests.testGarbage   rG   r   c           	      6   | j                   }| j                  t               | j                  t	        |      d       | j                  |d   dddgdgdgdd	       | j                  |d
   dddgdgdgdd       | j                  |d   dddgdgdgdd       y )N   r   r:   r;   r<   r=   0r?    r.   zsip:loopfoobarr>   rB   rI   rJ   )r!   r,   request4r/   rD   r7   rE   s     r   testThreeInOnez"MessageParsingTests.testThreeInOne   s    FF"Q#aDVE7seD	
 	aDWUGuE	
 	aDWUGuE	
r   c           	          | j                   }| j                  t               | j                  t	        |      d       | j                  |d   dddgdgdgdd	       y r9   )r!   r,   request_shortr/   rD   r7   rE   s     r   	testShortzMessageParsingTests.testShort   s[    FF'Q#aDVE7seD	
r   c                    | j                   }| j                  t               | j                  t	        |      d       |d   }| j                  |j
                  d       | j                  |j                  d       | j                  |j                  dgdgdgd       | j                  |j                  d	       | j                  |j                  d       y )
Nr.   r      OKrT   rU   rR   r?   rS   )
r!   r,   	response1r/   rD   codephraser3   r4   r5   r   r!   r6   s      r   testSimpleResponsez&MessageParsingTests.testSimpleResponse   s    FF#Q#aD%4(IIw3%P	
 	$Q'r   c                 R   | j                   }| j                  t               | j                  t	        |      d       |d   }| j                  |j
                  d   d   d       | j                  |j
                  d   d   d       | j                  |j
                  d   d   d       y)	zv
        A header may be split across multiple lines.  Subsequent lines begin
        with C{" "} or C{"\t"}.
        r.   r   viazHSIP/2.0/UDP server10.biloxi.com;branch=z9hG4bKnashds8;received=192.0.2.3zVSIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2rI   zHSIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.1N)r!   r,   response_multiliner/   rD   r3   ra   s      r   test_multiLinez"MessageParsingTests.test_multiLine   s    
 FF+,Q#aDIIeQ7	

 	IIeQ?	

 	IIeQ:	
r   N)r   r   r   r&   r,   r7   rF   rL   rO   rW   rZ   rb   rf   r   r   r   r   r      s4    8(


 


4

(
r   r   c                       e Zd ZdZd Zy)%MessageParsingFeedDataCharByCharTestsz9
    Same as base class, but feed data char by char.
    c                 |    |D ]  }| j                   j                  |        | j                   j                          y r   r(   )r   r+   cs      r   r,   z1MessageParsingFeedDataCharByCharTests.feedMessage  s3     	(AKK$$Q'	(r   N)r   r   r   __doc__r,   r   r   r   rh   rh     s    r   rh   c                       e Zd Zd Zd Zd Zy)MakeMessageTestsc                     t        j                  dd      }|j                  dd       | j                  |j	                         d       y )Nr:   r;   rT   rU   z$INVITE sip:foo SIP/2.0
Foo: bar

)r   Request	addHeaderr/   r2   r   rs     r   testRequestzMakeMessageTests.testRequest  s7    KK),	E5!'STr   c                     t        j                  dd      }|j                  dd       |j                  dd       |j                  d       | j	                  |j                         d       y )	Nr\   r]   rT   rU   zContent-Lengthr>   rJ   z3SIP/2.0 200 OK
Foo: bar
Content-Length: 4

1234)r   Responserp   bodyDataReceivedr/   r2   rq   s     r   testResponsezMakeMessageTests.testResponse  sW    LLd#	E5!	$c*	6"JJLW	
r   c                 n    t        j                  d      }| j                  |j                         d       y )Nr\   zSIP/2.0 200 OK

)r   ru   r/   r2   rq   s     r   testStatusCodezMakeMessageTests.testStatusCode%  s'    LL'?@r   N)r   r   r   rs   rw   ry   r   r   r   rm   rm     s    U

Ar   rm   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)ViaTestsc                     |j                         }| j                  |t        j                  |      j                                y r   )r2   r/   r   parseViaHeader)r   vss      r   checkRoundtripzViaTests.checkRoundtrip+  s1    JJLC..q1::<=r   c                 <   t        j                  d      }t        j                  d      }| j                  |j                  |j                         | j                  |j                  |j                         | j                  |j
                  |j
                         y )NzSIP/2.0/UDP 192.168.1.1:5060z SIP/2.0/UDP     192.168.1.1:5060)r   r}   r/   	transporthostport)r   v1v2s      r   testExtraWhitespacezViaTests.testExtraWhitespace/  sl     >? BCr||4"''*"''*r   c                    d}t        j                  |      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                         d	       | j!                  |       y
)zC
        Test parsing a Via header with one of everything.
        zVSIP/2.0/UDP first.example.com:4000;ttl=16;maddr=224.2.0.1 ;branch=a7c6a8dlze (Example)UDPzfirst.example.comi     z	224.2.0.1
a7c6a8dlzer   zKSIP/2.0/UDP first.example.com:4000;ttl=16;branch=a7c6a8dlze;maddr=224.2.0.1N)r   r}   r/   r   r   r   assertIsNonerport
rportValueassertFalserportRequestedttlmaddrbranchhiddenr2   r   r   r   r~   s      r   test_complexzViaTests.test_complex6  s   
, 	
 q!e,!45&!''"!,,'))*#+.<01%JJL8	

 	Ar   c                    d}t        j                  |      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j!                         d       | j#                  |       y)z3
        Test parsing a simple Via header.
        zSIP/2.0/UDP example.com;hiddenr   example.com  z#SIP/2.0/UDP example.com:5060;hiddenN)r   r}   r/   r   r   r   r   r   r   r   r   r   r   r   
assertTruer   r2   r   r   s      r   test_simplezViaTests.test_simpleP  s     -q!e,/&!''"!,,'))*!%% !''"!((#!'LMAr   c                 P    t        j                  d      }| j                  |       y )Nr   )r   Viar   r   r~   s     r   testSimplerzViaTests.testSimplerc  s    GGM"Ar   c                    t        j                  dd      }| j                  | j                  g      }| j	                  t        |      d       | j	                  |d   d   d       | j	                  |d   d	   t               | j	                  |j                         d
       | j                  |j                         | j                  |j                         | j                  |j                         y)z|
        Setting rport to True is deprecated, but still produces a Via header
        with the expected properties.
        foo.barTr   offendingFunctionsr.   r   r+   +rport=True is deprecated since Twisted 9.0.categorySIP/2.0/UDP foo.bar:5060;rportN)r   r   flushWarningstest_deprecatedRPortr/   rD   DeprecationWarningr2   r   r   r   r   r   )r   r~   warningss      r   r   zViaTests.test_deprecatedRPortg  s    
 GGIT*%%$:S:S9T%UX*QK	"$Q	
 	!Z02DE'GH (()!,,'r   c                     t        j                  dd      }| j                  |j                         d       | j	                  |j
                         | j                  |j                         y)zU
        An rport setting of None should insert the parameter with no value.
        r   Nr   r   )r   r   r/   r2   r   r   r   r   r   s     r   
test_rportzViaTests.test_rportz  sO     GGIT*'GH(()!,,'r   c                    t        j                  dd      }| j                  |j                         d       | j	                  |j
                         | j                  |j                  d       | j                  |j                  d       y)zk
        An rport numeric setting should insert the parameter with the number
        value given.
        r   r.   r   z SIP/2.0/UDP foo.bar:5060;rport=1N)r   r   r/   r2   r   r   r   r   r   s     r   test_rportValuezViaTests.test_rportValue  sg    
 GGIQ''IJ))*q)!$r   c                    d}t        j                  |      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                         j                  d      d       y )	Nz8SIP/2.0/UDP 10.0.0.1:5060;received=22.13.1.5;rport=12345r   10.0.0.1r   z	22.13.1.5i90  zrport=12345)r   r}   r/   r   r   r   receivedr   assertNotEqualr2   findr   s      r   testNATzViaTests.testNAT  s    Fq!e,,&[1%(AJJL--m<bAr   c                 r    d}t        j                  |      }| j                  |j                         |       y)zZ
        Parsing and serializing Via headers with unknown parameters should work.
        z?SIP/2.0/UDP example.com:5060;branch=a12345b;bogus;pie=deliciousN)r   r}   r/   r2   r   s      r   test_unknownParamszViaTests.test_unknownParams  s0     Nq!q)r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r{   r{   *  s5    >+4&(&(	%	B*r   r{   c                       e Zd Zd Zd Zy)URLTestsc                 x    dD ]5  }| j                  t        j                  |      j                         |       7 y )N)zsip:j.doe@big.comz&sip:j.doe:secret@big.com;transport=tcpz!sip:j.doe@big.com?subject=projectzsip:example.com)r/   r   parseURLr2   )r   urls     r   testRoundtripzURLTests.testRoundtrip  s9    
 	@C S\\#.7793?	@r   c                     d}t        j                  |      }dddddddd	d
dddgfddddffD ]!  \  }}| j                  t        ||      |       # y )Nzesip:user:pass@hosta:123;transport=udp;user=phone;method=foo;ttl=12;maddr=1.2.3.4;blah;goo=bar?a=b&c=d)usernameuser)passwordpass)r   hosta)r   {   )r   udp)usertypephone)r0   rT   )r      )r   1.2.3.4otherblahzgoo=barr3   bd)arj   )r   r   r/   getattr)r   r   r   kr~   s        r   testComplexzURLTests.testComplex  s}    8 	
 ll1o   ! vy)*c,-
 	1DAq WS!_a0	1r   N)r   r   r   r   r   r   r   r   r   r     s    @1r   r   c                       e Zd Zd Zy)
ParseTestsc           
         dddi fdddi fdddi fdddi fdd	dd
ddffD ]d  \  }}}}t        j                  |      \  }}}| j                  ||       | j                  |j                         |       | j                  ||       f y )Nz""A. G. Bell" <sip:foo@example.com>z
A. G. Bellzsip:foo@example.comzAnon <sip:foo@example.com>AnonrS   z<sip:foo@example.com>z)foo <sip:foo@example.com>;tag=bar;foo=bazrT   rU   baz)tagrT   )r   parseAddressr/   r2   )r   addressnameurlsparamsgnamegurlgparamss           r   testParseAddresszParseTests.testParseAddress  s     5%	 *63H"M"B(=rB$b*?D;%e,	,
 	.'GT4" $'#3#3G#< E4T5)T]]_d3Wf-)	.r   N)r   r   r   r   r   r   r   r   r     s    .r   r   c                       e Zd Zd Zy)DummyLocatorc                 V    t        j                  t        j                  dd            S )N
server.comr   r   )r   succeedr   URLr   
logicalURLs     r   
getAddresszDummyLocator.getAddress  s    }}SWW\=>>r   Nr   r   r   r   r   r   r   r   r     s    ?r   r   c                       e Zd Zd Zy)FailingLocatorc                 <    t        j                  t                     S r   )r   failLookupErrorr   s     r   r   zFailingLocator.getAddress  s    zz+-((r   Nr   r   r   r   r   r     s    )r   r   c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)
ProxyTestsc                      t        j                  d       _        t                j                  _        g  _         fd j                  _        y )N	127.0.0.1c                 >    j                   j                  | |f      S r   sentr#   destmsgr   s     r   r   z"ProxyTests.setUp.<locals>.<lambda>      4993C3CT3K3P r   )r   Proxyproxyr   locatorr   sendMessager%   s   `r   r&   zProxyTests.setUp  s4    YY{+
)^

	!P

r   c                 h   t        j                  dd      }|j                  dt        j                  d      j	                                |j                  dt        j                  d      j	                                |j                  dd       |j                  dd	       |j                  d
d       | j
                  j                  |j	                         d       | j                  t        | j                        d       | j                  d   \  }}| j                  |j                  d       | j                  |j                  d       | j                  |j                  j	                         d       | j                  |j                  d       | j                  |j                  d   g d       y )Nr:   r;   rd   r   z1.2.3.5rT   rU   rA   <sip:joe@server.com>contactz<sip:joe@1.2.3.5>r   r   r.   r   r   r   )SIP/2.0/UDP 127.0.0.1:5060SIP/2.0/UDP 1.2.3.4:5060zSIP/2.0/UDP 1.2.3.5:5060)r   ro   rp   r   r2   r   datagramReceivedr/   rD   r   r   r   r1   r0   r3   r   rr   r   r6   s       r   testRequestForwardzProxyTests.testRequestForward  s8   KK),	E3779-6689	E3779-6689	E5!	D01	I23

##AJJL2CDTYY+))A,aD)L1)958,IIe	
r   c                    t        j                  dd      }|j                  dt        j                  d      j	                                |j                  dd       |j                  dd       |j                  d	d
       | j
                  j                  |j	                         d       | j                  d   \  }}| j                  |j                  d   ddg       y )Nr:   r;   rd   r   rT   rU   rA   r  r  z<sip:joe@1.2.3.4>z1.1.1.1r   r   r  z)SIP/2.0/UDP 1.2.3.4:5060;received=1.1.1.1)
r   ro   rp   r   r2   r   r  r   r/   r3   r  s       r   testReceivedRequestForwardz%ProxyTests.testReceivedRequestForward  s    KK),	E3779-6689	E5!	D01	I23

##AJJL2CD))A,aIIe)+VW	
r   c                 2   t        j                  d      }|j                  dt        j                  d      j	                                | j
                  j                  |j	                         d       | j                  t        | j                        d       y )Nr\   rd   foo.comr  r   )
r   ru   rp   r   r2   r   r  r/   rD   r   rq   s     r   testResponseWrongViazProxyTests.testResponseWrongVia  sc    LL	E3779-6689

##AJJL2CDTYY+r   c                    t        j                  d      }|j                  dt        j                  d      j	                                |j                  dt        j                  dd      j	                                | j
                  j                  |j	                         d       | j                  t        | j                        d       | j                  d	   \  }}| j                  |j                  |j                  fd
       | j                  |j                  d       | j                  |j                  d   dg       y )Nr\   rd   r   
client.com  r   r  r.   r   )r  r  zSIP/2.0/UDP client.com:1234)r   ru   rp   r   r2   r   r  r/   rD   r   r   r   r_   r3   r  s       r   testResponseForwardzProxyTests.testResponseForward  s    LL	E377;/88:;	E377<d;DDFG

##AJJL2CDTYY+))A,a$))TYY/1EF%5),I+JKr   c                    t        j                  d      }|j                  dt        j                  d      j	                                |j                  dt        j                  dd      j	                                | j
                  j                  |j	                         d       | j                  t        | j                        d       | j                  d	   \  }}| j                  |j                  |j                  fd
       y )Nr\   rd   r   r   r  )r   r  r.   r   r  r   )r   ru   rp   r   r2   r   r  r/   rD   r   r   r   r  s       r   testReceivedResponseForwardz&ProxyTests.testReceivedResponseForward$  s    LL	E377;/88:;	E377:ENNPQ

##AJJL2CDTYY+))A,a$))TYY/1EFr   c                    t        j                  d      }|j                  dt        j                  d      j	                                g fd| j
                  _        | j
                  j                  |j	                         d       | j                  t              d       d   \  }}| j                  t        |j                  j                  dg             d       | j                  |j                  d       y )Nr\   rd   r   c                  &    j                  |       S r   )r#   )r   r!   s    r   r   z-ProxyTests.testResponseToUs.<locals>.<lambda>1  s    AHHQK r   r  r.   r   )r   ru   rp   r   r2   r   gotResponser  r/   rD   r3   getr_   )r   rr   r6   addrr!   s       @r   testResponseToUszProxyTests.testResponseToUs-  s    LL	E377;/88:;!7



##AJJL2CDQ#A$4QYY]]5"56:%r   c                    t        j                  dd      }|j                  dt        j                  d      j	                                |j                  dt        j                  d      j	                                | j
                  j                  |j	                         d       | j                  | j                  g        y )Nr:   r;   rd   r   r   r  )	r   ro   rp   r   r2   r   r  r/   r   rq   s     r   testLoopzProxyTests.testLoop8  s    KK),	E3779-6689	E377;/88:;

##AJJL2FGB'r   c                 v   t        j                  dd      }|j                  dt        j                  d      j	                                |j                  dd       t               | j                  _        | j                  j                  |j	                         d       | j                  t        | j                        d       | j                  d	   \  }}| j                  |j                  |j                  fd       | j                  |j                  d
       | j                  |j                  d   dg       y )Nr:   r;   rd   r   rA   r  r  r.   r   i  r  )r   ro   rp   r   r2   r   r   r   r  r/   rD   r   r   r   r_   r3   r  s       r   testCantForwardRequestz!ProxyTests.testCantForwardRequest?  s    KK),	E3779-6689	D01+-



##AJJL2CDTYY+))A,a$))TYY/1BC%5),F+GHr   N)r   r   r   r&   r	  r  r  r  r  r  r  r   r   r   r   r   r     s3    Q
.
,	LG	&(
Ir   r   c                   T    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)RegistrationTestsc                      t        j                  d       _        t        j                  d       _         j                  x j                  _         j                  _        g  _         fd j                  _        y )Nr   r   bell.example.comc                 >    j                   j                  | |f      S r   r   r   s     r   r   z)RegistrationTests.setUp.<locals>.<lambda>R  r   r   )r   RegisterProxyr   InMemoryRegistryregistryr   r   r   r%   s   `r   r&   zRegistrationTests.setUpM  sW    &&K8
,,-?@37==@

djj0	!P

r   c                     | j                   j                  j                         D ]  \  }}|j                           | `y r   )r)  usersvaluescancelr   )r   r   r1   s      r   tearDownzRegistrationTests.tearDownT  s6    mm))002 	FAsHHJ	Jr   c                 2   t        j                  dd      }|j                  dd       |j                  dd       |j                  dt        j                  d      j	                                | j
                  j                  |j	                         d	       y )
NREGISTERsip:bell.example.comrA   sip:joe@bell.example.comr  sip:joe@client.com:1234rd   r  r  r   ro   rp   r   r2   r   r  rq   s     r   registerzRegistrationTests.registerY  sl    KK
$:;	D45	I89	E377<099;<

##AJJL2FGr   c                 V   t        j                  dd      }|j                  dd       |j                  dd       |j                  dt        j                  d      j	                                |j                  d	d
       | j
                  j                  |j	                         d       y )Nr0  r1  rA   r2  r  *rd   r  expiresrR   r  r4  rq   s     r   
unregisterzRegistrationTests.unregister`  sy    KK
$:;	D45	Is#	E377<099;<	Is#

##AJJL2FGr   c                    | j                          | j                  d   \  }}| j                  |j                  |j                  fd       | j                  |j
                  d       | j                  |j                  d   dg       | j                  |j                  d   dg       | j                  |j                  d   d	g       t        t              t        k7  r,| j                  t        |j                  d
   d         dv        | j                  t        | j                  j                        d       | j                  j                  d   \  }}| j                  |j                         d	       | j                   j"                  j%                  t'        j(                  dd            }|j+                  d        |j+                  | j                  d       |S )Nr   r  r\   rd   SIP/2.0/UDP client.com:5060rA   r2  r  sip:joe@client.com:5060r8  )i  i  i  i  r.   r=   r%  r   r   c                 2    | j                   | j                  fS r   r   r   )desturls    r   r   z0RegistrationTests.testRegister.<locals>.<lambda>{  s    w||W\\&B r   )r5  r   r/   r   r   r_   r3   typer   r	   r   intrD   r)  r+  r2   r   r   r   r   r   addCallback)r   r   r6   dcr1   r   s         r   testRegisterzRegistrationTests.testRegisterh  sx   ))A,a$))TYY/1EF%5),I+JK4+E*FG9-0I/JK =22OOC		) 4Q 78<TTUT]]00115--%%e,C)BCJJ))GGU);<
 	
BC	d&&(<=r   c                 <   | j                          | j                          | j                  d   \  }}| j                  |j                  |j
                  fd       | j                  |j                  d       | j                  |j                  d   dg       | j                  |j                  d   dg       | j                  |j                  d   d	g       | j                  |j                  d
   dg       | j                  | j                  j                  i        y )Nr.   r  r\   rd   r;  rA   r2  r  r<  r8  rR   )
r5  r9  r   r/   r   r   r_   r3   r)  r+  r   r   r6   s      r   testUnregisterz RegistrationTests.testUnregister  s    ))A,a$))TYY/1EF%5),I+JK4+E*FG9-0I/JK9-u5,,b1r   c                     t               }t        j                  |      }t        j                         }|j                  dd       |j                  |       || j                  _        y )NzuserXname@127.0.0.1	passXword)r   r   Portalr   'InMemoryUsernamePasswordDatabaseDontUseaddUserregisterCheckerr   )r   rr   prj   s       r   	addPortalzRegistrationTests.addPortal  sN    KMM!<<>			'5	!

r   c                 H   | j                          | j                          | j                  t        | j                  j
                        d       | j                  t        | j                        d       | j                  d   \  }}| j                  |j                  d       y )Nr   r.   i  )rP  r5  r/   rD   r)  r+  r   r_   rG  s      r   testFailedAuthenticationz*RegistrationTests.testFailedAuthentication  sq    T]]00115TYY+))A,a%r   c                 |   t        j                  dd      }|j                  dd       |j                  dd       |j                  dt        j                  d      j	                                | j
                  j                  |j	                         d	       | j                  t        | j                        d
       y )Nr0  zsip:wrong.comrA   r2  r  r3  rd   r  r  r   
r   ro   rp   r   r2   r   r  r/   rD   r   rq   s     r   testWrongDomainRegisterz)RegistrationTests.testWrongDomainRegister  s    KK
O4	D45	I89	E377<099;<

##AJJL2FGTYY+r   c                 |   t        j                  dd      }|j                  dd       |j                  dd       |j                  dt        j                  d      j	                                | j
                  j                  |j	                         d	       | j                  t        | j                        d
       y )Nr0  r1  rA   zsip:joe@foo.comr  r3  rd   r  r  r   rT  rq   s     r   testWrongToDomainRegisterz+RegistrationTests.testWrongToDomainRegister  s    KK
$:;	D+,	I89	E377<099;<

##AJJL2FGTYY+r   c                     | j                          t        j                  dd      }| j                  j                  j                  |      }| j                  |t               |S )Nr=   r  r=  r5  r   r   r   r   r   assertFailurer   r   r   r   s      r   testWrongDomainLookupz'RegistrationTests.testWrongDomainLookup  sJ    ggu95JJ))#.1k*r   c                     | j                          t        j                  dd      }| j                  j                  j                  |      }| j                  |t               |S )Njaner%  r=  rY  r[  s      r   testNoContactLookupz%RegistrationTests.testNoContactLookup  sK    ggv,>?JJ))#.1k*r   N)r   r   r   r&   r.  r5  r9  rE  rH  rP  rR  rU  rW  r\  r_  r   r   r   r"  r"  L  sA    Q
HH.
2&,,r   r"  c                       e Zd Zd Zd Zy)Clientc                     t         j                  j                  |        g | _        t	        j
                         | _        y r   )r   Base__init__r   r   Deferreddeferredr%   s    r   rd  zClient.__init__  s)    $(r   c                     | j                   j                  |       | j                  j                  | j                          y r   )r   r#   rf  callback)r   responser  s      r   handle_responsezClient.handle_response  s*    X&t}}-r   N)r   r   r   rd  rj  r   r   r   ra  ra    s    )
.r   ra  c                   $    e Zd Zd Zd Zd Zd Zy)	LiveTestsc                    t        j                  d      | _        t        j                  d      | _        | j                  x| j                  _        | j                  _        t        j                  d| j                  d      | _        t               | _
        t        j                  d| j                  d      | _        | j                  j                         j                  | j                  j                         j                  f| _        y )Nr   r$  r%  r   )	interface)r   r'  r   r(  r)  r   r   	listenUDP
serverPortra  client
clientPortgetHostr   r   serverAddressr%   s    r   r&   zLiveTests.setUp  s    &&K8
,,-?@37==@

djj0!++Atzz[Qh!++At{{kROO##%**OO##%**
r   c                 L   | j                   j                  j                         D ]  \  }}|j                           t	        j
                  | j                  j                        }t	        j
                  | j                  j                        }t	        j                  ||g      S r   )
r)  r+  r,  r-  r   maybeDeferredrr  stopListeningrp  gatherResults)r   r   r1   d1d2s        r   r.  zLiveTests.tearDown  sz    mm))002 	FAsHHJ	  !>!>?  !>!>?""B8,,r   c                      j                   j                         j                  }t        j                  dd      }|j                  dd       |j                  dd|z         |j                  dt        j                  d|	      j                                 j                  j                  t        j                  d j                  d
         |        j                  j                  } fd}|j                  |       |S )Nr0  r1  rA   r2  r  sip:joe@127.0.0.1:%drd   r   r   r.   r?  c                 ~    j                  t        |       d       | d   }j                  |j                  d       y Nr.   r   r\   r/   rD   r_   r   rr   r   s     r   checkz%LiveTests.testRegister.<locals>.check  4    S]A.AQVVS)r   )rr  rs  r   r   ro   rp   r   r2   rq  r   r   rt  rf  rC  )r   rO  rr   r   r  s   `    r   rE  zLiveTests.testRegister  s    OO##%**KK
$:;	D45	I59:	E377;Q7@@BCGG4+=+=a+@A1	
 KK  	*
 	
er   c                      j                   j                         j                  }t        j                  dd      }|j                  dd       |j                  dd|z         |j                  dt        j                  d|d	
      j                                 j                   j                  g      } j                  t        |      d        j                  |d   d   d        j                  |d   d   t                j                  j                  t        j                  d j                   d         |        j                  j"                  } fd}|j%                  |       |S )ap  
        rport is allowed without a value, apparently because server
        implementors might be too stupid to check the received port
        against 5060 and see if they're equal, and because client
        implementors might be too stupid to bind to port 5060, or set a
        value on the rport parameter they send if they bind to another
        port.
        r0  r1  rA   r2  r  r|  rd   r   T)r   r   r   r.   r   r+   r   r   r?  c                 ~    j                  t        |       d       | d   }j                  |j                  d       y r~  r  r  s     r   r  z)LiveTests.test_amoralRPort.<locals>.check  r  r   )rr  rs  r   r   ro   rp   r   r2   r   test_amoralRPortr/   rD   r   rq  r   r   rt  rf  rC  )r   rO  rr   r   r   r  s   `     r   r  zLiveTests.test_amoralRPort  s4    OO##%**KK
$:;	D45	I59:	E377;QdCLLNO%%$:O:O9P%QX*QK	"$Q	
 	!Z02DEGG4+=+=a+@A1	
 KK  	*
 	
er   N)r   r   r   r&   r.  rE  r  r   r   r   rl  rl    s    

-&r   rl  ))rk   twisted.credr   r   twisted.internetr   r   twisted.protocolsr   twisted.trialr   twisted.internet.asyncioreactorr	   BaseExceptionzope.interfacer
   replacerC   rK   rN   rV   r^   rY   request_nattedre   r   TestCaser   rh   rm   r{   r   r   ILocatorr   r   r   r"  rc  ra  rl  r   r   r   <module>r     s-  
 * + ! ""F '  	f		 	 	&		 	 7& 	 	&	 	(
 G& 
 	&	  G&	  g& (0 0
E
(++ E
P,? Ax(( A(u*x   u*p1x   1@."" .2 S\\? ? ?
 S\\) ) )
bI"" bIJl)) l^.SXX .F!! Fm  "!"s   G GG