o
    ¯bšc  ã                   @   sÈ  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
 zddlmZ W n ey1   dZY nw ddlmZ d	d
 dd¡ Zd dd¡Zd dd¡Zd dd¡Zd dd¡Zd dd¡Zd dd¡Zd dd¡ZG dd„ dƒZG dd„ de
jƒZG dd„ deƒZG dd„ de
jƒZG dd„ de
jƒZG dd„ de
jƒZG d d!„ d!e
jƒZ eej!ƒG d"d#„ d#ƒƒZ"eej!ƒG d$d%„ d%ƒƒZ#G d&d'„ d'e
jƒZ$G d(d)„ d)e
jƒZ%G d*d+„ d+ej&ƒZ'G d,d-„ d-e
jƒZ(dS ).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

lalalalz½INVITE 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d„ ZdS )Ú	TestRealmc                 G   s   t jd dd„ fS )Nc                   S   s   d S ©N© r   r   r   ú7/usr/lib/python3/dist-packages/twisted/test/test_sip.pyÚ<lambda>€   s    z)TestRealm.requestAvatar.<locals>.<lambda>)r   ÚIContact)ÚselfÚavatarIdÚmindÚ
interfacesr   r   r   ÚrequestAvatar   s   zTestRealm.requestAvatarN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   ~   ó    r   c                   @   ó\   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚMessageParsingTestsc                 C   s   g | _ t | j j¡| _d S r   )Úlr   ÚMessagesParserÚappendÚparser©r   r   r   r   ÚsetUp„   s   zMessageParsingTests.setUpc                 C   s   | j  |¡ | j  ¡  d S r   ©r    ÚdataReceivedÚdataDone)r   Úmessager   r   r   ÚfeedMessageˆ   s   zMessageParsingTests.feedMessagec                 C   sN   |   |j|¡ |   |j ¡ |¡ |   |j|¡ |   |j|¡ |   |jd¡ dS )z$
        Validate Requests.
        é   N)ÚassertEqualÚmethodÚuriÚtoStringÚheadersÚbodyÚfinished)r   Úmr*   r+   r-   r.   r   r   r   ÚvalidateMessageŒ   s
   z#MessageParsingTests.validateMessagec              	   C   óH   | j }|  t¡ |  t|ƒd¡ |  |d dddgdgdgdœd	¡ d S ©
Nr(   r   ÚINVITEúsip:fooÚmoÚjoeÚ4©ÚfromÚtozcontent-lengthÚabcd)r   r'   Úrequest1r)   Úlenr1   ©r   r   r   r   r   Ú
testSimple–   ó   
ûzMessageParsingTests.testSimplec              	   C   sr   | j }|  t¡ |  t¡ |  t|ƒd¡ |  |d dddgdgdgdœd	¡ |  |d
 dddgdgdœd¡ d S )Né   r   r4   r5   r6   r7   r8   r9   r<   r(   )r:   r;   Ú1234)r   r'   r=   Úrequest2r)   r>   r1   r?   r   r   r   ÚtestTwoMessages¢   s   

ûÿz#MessageParsingTests.testTwoMessagesc              	   C   r2   )
Nr(   r   r4   r5   r6   r7   r8   r9   rC   )r   r'   Úrequest3r)   r>   r1   r?   r   r   r   ÚtestGarbage²   rA   zMessageParsingTests.testGarbagec              	   C   s   | j }|  t¡ |  t|ƒd¡ |  |d dddgdgdgdœd	¡ |  |d
 dddgdgdgdœd¡ |  |d dddgdgdgdœd¡ d S )Né   r   r4   r5   r6   r7   Ú0r9   Ú r(   zsip:loopÚfooÚbarr8   r<   rB   rC   )r   r'   Úrequest4r)   r>   r1   r?   r   r   r   ÚtestThreeInOne¾   s0   
ûûûz"MessageParsingTests.testThreeInOnec              	   C   r2   r3   )r   r'   Úrequest_shortr)   r>   r1   r?   r   r   r   Ú	testShortØ   rA   zMessageParsingTests.testShortc                 C   s€   | j }|  t¡ |  t|ƒd¡ |d }|  |jd¡ |  |jd¡ |  |jdgdgdgdœ¡ |  |jd	¡ |  |j	d¡ d S )
Nr(   r   éÈ   ÚOKrK   rL   rI   r9   rJ   )
r   r'   Ú	response1r)   r>   ÚcodeÚphraser-   r.   r/   ©r   r   r0   r   r   r   ÚtestSimpleResponseä   s   
ÿz&MessageParsingTests.testSimpleResponsec                 C   sn   | j }|  t¡ |  t|ƒd¡ |d }|  |jd d d¡ |  |jd d d¡ |  |jd d d¡ dS )	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.2rB   zHSIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.1N)r   r'   Úresponse_multiliner)   r>   r-   rV   r   r   r   Útest_multiLineñ   s    
þþþz"MessageParsingTests.test_multiLineN)r   r   r   r"   r'   r1   r@   rE   rG   rN   rP   rW   rZ   r   r   r   r   r   ƒ   s    
r   c                   @   s   e Zd ZdZdd„ ZdS )Ú%MessageParsingFeedDataCharByCharTestsz9
    Same as base class, but feed data char by char.
    c                 C   s$   |D ]}| j  |¡ q| j  ¡  d S r   r#   )r   r&   Úcr   r   r   r'     s   z1MessageParsingFeedDataCharByCharTests.feedMessageN)r   r   r   Ú__doc__r'   r   r   r   r   r[     s    r[   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚMakeMessageTestsc                 C   s,   t  dd¡}| dd¡ |  | ¡ d¡ d S )Nr4   r5   rK   rL   z$INVITE sip:foo SIP/2.0
Foo: bar

)r   ÚRequestÚ	addHeaderr)   r,   ©r   Úrr   r   r   ÚtestRequest  s   zMakeMessageTests.testRequestc                 C   sB   t  dd¡}| dd¡ | dd¡ | d¡ |  | ¡ d¡ d S )	NrQ   rR   rK   rL   zContent-Lengthr8   rC   z3SIP/2.0 200 OK
Foo: bar
Content-Length: 4

1234)r   ÚResponser`   ÚbodyDataReceivedr)   r,   ra   r   r   r   ÚtestResponse  s   
ÿzMakeMessageTests.testResponsec                 C   s   t  d¡}|  | ¡ d¡ d S )NrQ   zSIP/2.0 200 OK

)r   rd   r)   r,   ra   r   r   r   ÚtestStatusCode%  s   
zMakeMessageTests.testStatusCodeN)r   r   r   rc   rf   rg   r   r   r   r   r^     s    	r^   c                   @   r   )ÚViaTestsc                 C   s"   |  ¡ }|  |t |¡  ¡ ¡ d S r   )r,   r)   r   ÚparseViaHeader)r   ÚvÚsr   r   r   ÚcheckRoundtrip+  s   zViaTests.checkRoundtripc                 C   sH   t  d¡}t  d¡}|  |j|j¡ |  |j|j¡ |  |j|j¡ d S )NzSIP/2.0/UDP 192.168.1.1:5060z SIP/2.0/UDP     192.168.1.1:5060)r   ri   r)   Ú	transportÚhostÚport)r   Úv1Úv2r   r   r   ÚtestExtraWhitespace/  s
   

zViaTests.testExtraWhitespacec                 C   s²   d}t  |¡}|  |jd¡ |  |jd¡ |  |jd¡ |  |j¡ |  |j¡ |  	|j
¡ |  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ |  | ¡ d	¡ |  |¡ d
S )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   ri   r)   rm   rn   ro   ÚassertIsNoneÚrportÚ
rportValueÚassertFalseÚrportRequestedÚttlÚmaddrÚbranchÚhiddenr,   rl   ©r   rk   rj   r   r   r   Útest_complex6  s$   ÿ
þzViaTests.test_complexc                 C   sª   d}t  |¡}|  |jd¡ |  |jd¡ |  |jd¡ |  |j¡ |  |j¡ |  	|j
¡ |  |j¡ |  |j¡ |  |j¡ |  |j¡ |  | ¡ d¡ |  |¡ dS )z3
        Test parsing a simple Via header.
        zSIP/2.0/UDP example.com;hiddenrs   úexample.coméÄ  z#SIP/2.0/UDP example.com:5060;hiddenN)r   ri   r)   rm   rn   ro   rv   rw   rx   ry   rz   r{   r|   r}   Ú
assertTruer~   r,   rl   r   r   r   r   Útest_simpleP  s   
zViaTests.test_simplec                 C   s   t  d¡}|  |¡ d S )Nr   )r   ÚViarl   ©r   rj   r   r   r   ÚtestSimplerc  s   
zViaTests.testSimplerc                 C   sŽ   t jddd}| j| jgd}|  t|ƒd¡ |  |d d d¡ |  |d d	 t¡ |  | ¡ d
¡ |  |j	¡ |  |j
¡ |  |j¡ dS )z|
        Setting rport to True is deprecated, but still produces a Via header
        with the expected properties.
        úfoo.barT©rw   ©Ú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)   r>   ÚDeprecationWarningr,   rƒ   rw   rz   rv   rx   )r   rj   Úwarningsr   r   r   r   g  s   ÿzViaTests.test_deprecatedRPortc                 C   s:   t jddd}|  | ¡ d¡ |  |j¡ |  |j¡ dS )zU
        An rport setting of None should insert the parameter with no value.
        rˆ   Nr‰   rŽ   )r   r…   r)   r,   rƒ   rz   rv   rx   r†   r   r   r   Ú
test_rportz  s   zViaTests.test_rportc                 C   sJ   t jddd}|  | ¡ d¡ |  |j¡ |  |jd¡ |  |jd¡ dS )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)   r,   ry   rz   rx   rw   r†   r   r   r   Útest_rportValueƒ  s
   zViaTests.test_rportValuec                 C   sn   d}t  |¡}|  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ |  | 	¡  
d¡d¡ d S )	Nz8SIP/2.0/UDP 10.0.0.1:5060;received=22.13.1.5;rport=12345rs   ú10.0.0.1r‚   z	22.13.1.5i90  zrport=12345éÿÿÿÿ)r   ri   r)   rm   rn   ro   Úreceivedrw   ÚassertNotEqualr,   Úfindr   r   r   r   ÚtestNATŽ  s   
zViaTests.testNATc                 C   s"   d}t  |¡}|  | ¡ |¡ dS )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   ri   r)   r,   r   r   r   r   Útest_unknownParams™  s   
zViaTests.test_unknownParamsN)r   r   r   rl   rr   r€   r„   r‡   r   r“   r”   rš   r›   r   r   r   r   rh   *  s    	rh   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚURLTestsc                 C   s$   dD ]}|   t |¡ ¡ |¡ qd S )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   ÚparseURLr,   )r   Úurlr   r   r   ÚtestRoundtrip£  s   úzURLTests.testRoundtripc                 C   sZ   d}t  |¡}dddddddd	d
dddgfddddœffD ]\}}|  t||ƒ|¡ qd S )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)rn   Úhosta)ro   é{   )rm   Úudp)ÚusertypeÚphone)r*   rK   )r{   é   )r|   ú1.2.3.4ÚotherÚblahzgoo=barr-   ÚbÚd)Úar\   )r   rž   r)   Úgetattr)r   rk   rŸ   Úkrj   r   r   r   ÚtestComplex¬  s"   ÿ

õózURLTests.testComplexN)r   r   r   r    r³   r   r   r   r   r   ¢  s    	r   c                   @   r   )Ú
ParseTestsc              
   C   s†   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 ]"\}}}}t  |¡\}}}|  ||¡ |  | ¡ |¡ |  ||¡ qd S )Nz""A. G. Bell" <sip:foo@example.com>z
A. G. Bellzsip:foo@example.comzAnon <sip:foo@example.com>ÚAnonrJ   z<sip:foo@example.com>z)foo <sip:foo@example.com>;tag=bar;foo=bazrK   rL   Úbaz)ÚtagrK   )r   ÚparseAddressr)   r,   )r   ÚaddressÚnameÚurlsÚparamsÚgnameÚgurlÚgparamsr   r   r   ÚtestParseAddressÃ  s&   ü


üöìzParseTests.testParseAddressN)r   r   r   rÀ   r   r   r   r   r´   Â  r   r´   c                   @   r   )ÚDummyLocatorc                 C   s   t  tjddd¡S )Nú
server.comr‚   ©ro   )r   Úsucceedr   ÚURL©r   Ú
logicalURLr   r   r   Ú
getAddressÝ  s   zDummyLocator.getAddressN©r   r   r   rÈ   r   r   r   r   rÁ   Û  ó    rÁ   c                   @   r   )ÚFailingLocatorc                 C   s   t  tƒ ¡S r   )r   ÚfailÚLookupErrorrÆ   r   r   r   rÈ   ã  s   zFailingLocator.getAddressNrÉ   r   r   r   r   rË   á  rÊ   rË   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú
ProxyTestsc                    s0   t  d¡ˆ _tƒ ˆ j_g ˆ _‡ fdd„ˆ j_d S )Nú	127.0.0.1c                    ó   ˆ j  | |f¡S r   ©Úsentr   ©ÚdestÚmsgr!   r   r   r   ì  ó    z"ProxyTests.setUp.<locals>.<lambda>)r   ÚProxyÚproxyrÁ   ÚlocatorrÒ   ÚsendMessager!   r   r!   r   r"   è  s   
zProxyTests.setUpc                 C   sä   t  dd¡}| dt  d¡ ¡ ¡ | dt  d¡ ¡ ¡ | dd¡ | dd	¡ | d
d¡ | j | ¡ d¡ |  t| j	ƒd¡ | j	d \}}|  |j
d¡ |  |jd¡ |  |j ¡ d¡ |  |jd¡ |  |jd g d¢¡ d S )Nr4   r5   rX   r«   z1.2.3.5rK   rL   r;   ú<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   r_   r`   r…   r,   rØ   ÚdatagramReceivedr)   r>   rÒ   ro   rn   r+   r*   r-   ©r   rb   rÔ   r0   r   r   r   ÚtestRequestForwardî  s"   þzProxyTests.testRequestForwardc                 C   s€   t  dd¡}| dt  d¡ ¡ ¡ | dd¡ | dd¡ | d	d
¡ | j | ¡ d¡ | jd \}}|  |j	d ddg¡ d S )Nr4   r5   rX   r«   rK   rL   r;   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   r_   r`   r…   r,   rØ   rà   rÒ   r)   r-   rá   r   r   r   ÚtestReceivedRequestForward  s   þz%ProxyTests.testReceivedRequestForwardc                 C   sH   t  d¡}| dt  d¡ ¡ ¡ | j | ¡ d¡ |  t| j	ƒd¡ d S )NrQ   rX   úfoo.comrã   r   )
r   rd   r`   r…   r,   rØ   rà   r)   r>   rÒ   ra   r   r   r   ÚtestResponseWrongVia  s   
zProxyTests.testResponseWrongViac                 C   s¦   t  d¡}| dt  d¡ ¡ ¡ | dt jddd ¡ ¡ | j | ¡ d¡ |  t| j	ƒd¡ | j	d	 \}}|  |j
|jfd
¡ |  |jd¡ |  |jd dg¡ d S )NrQ   rX   rÏ   ú
client.coméÒ  rÃ   rã   r(   r   )rç   rè   zSIP/2.0/UDP client.com:1234)r   rd   r`   r…   r,   rØ   rà   r)   r>   rÒ   rn   ro   rT   r-   rá   r   r   r   ÚtestResponseForward  s   
zProxyTests.testResponseForwardc                 C   s„   t  d¡}| dt  d¡ ¡ ¡ | dt jddd ¡ ¡ | j | ¡ d¡ |  t| j	ƒd¡ | j	d	 \}}|  |j
|jfd
¡ d S )NrQ   rX   rÏ   r•   rç   )r—   rã   r(   r   ©rç   r‚   )r   rd   r`   r…   r,   rØ   rà   r)   r>   rÒ   rn   ro   rá   r   r   r   ÚtestReceivedResponseForward$  s   
z&ProxyTests.testReceivedResponseForwardc                    sŽ   t  d¡}| dt  d¡ ¡ ¡ g ‰ ‡ fdd„| j_| j | ¡ d¡ |  t	ˆ ƒd¡ ˆ d \}}|  t	|j
 dg ¡ƒd¡ |  |jd¡ d S )	NrQ   rX   rÏ   c                     s
   ˆ   | ¡S r   )r   )r°   ©r   r   r   r   1  s   
 z-ProxyTests.testResponseToUs.<locals>.<lambda>rã   r(   r   )r   rd   r`   r…   r,   rØ   ÚgotResponserà   r)   r>   r-   ÚgetrT   )r   rb   r0   Úaddrr   rì   r   ÚtestResponseToUs-  s   
zProxyTests.testResponseToUsc                 C   s\   t  dd¡}| dt  d¡ ¡ ¡ | dt  d¡ ¡ ¡ | j | ¡ d¡ |  | jg ¡ d S )Nr4   r5   rX   r«   rÏ   rê   )	r   r_   r`   r…   r,   rØ   rà   r)   rÒ   ra   r   r   r   ÚtestLoop8  s
   zProxyTests.testLoopc                 C   s¤   t  dd¡}| dt  d¡ ¡ ¡ | dd¡ tƒ | j_| j | ¡ d¡ |  	t
| jƒd¡ | jd	 \}}|  	|j|jfd¡ |  	|jd
¡ |  	|jd dg¡ d S )Nr4   r5   rX   r«   r;   rÛ   rÝ   r(   r   i”  rß   )r   r_   r`   r…   r,   rË   rØ   rÙ   rà   r)   r>   rÒ   rn   ro   rT   r-   rá   r   r   r   ÚtestCantForwardRequest?  s   
z!ProxyTests.testCantForwardRequestN)r   r   r   r"   râ   rä   ræ   ré   rë   rð   rñ   rò   r   r   r   r   rÎ   ç  s    	rÎ   c                   @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚRegistrationTestsc                    sF   t jddˆ _t  d¡ˆ _ˆ j ˆ j_ˆ j_g ˆ _‡ fdd„ˆ j_d S )NrÏ   ©rn   úbell.example.comc                    rÐ   r   rÑ   rÓ   r!   r   r   r   R  rÖ   z)RegistrationTests.setUp.<locals>.<lambda>)r   ÚRegisterProxyrØ   ÚInMemoryRegistryÚregistryrÙ   rÒ   rÚ   r!   r   r!   r   r"   M  s
   zRegistrationTests.setUpc                 C   s&   | j j ¡ D ]\}}| ¡  q| `d S r   )rø   ÚusersÚvaluesÚcancelrØ   )r   r¯   r+   r   r   r   ÚtearDownT  s   
zRegistrationTests.tearDownc                 C   sP   t  dd¡}| dd¡ | dd¡ | dt  d¡ ¡ ¡ | j | ¡ d	¡ d S )
NÚREGISTERúsip:bell.example.comr;   úsip:joe@bell.example.comrÜ   úsip:joe@client.com:1234rX   rç   rê   ©r   r_   r`   r…   r,   rØ   rà   ra   r   r   r   ÚregisterY  s
   zRegistrationTests.registerc                 C   s\   t  dd¡}| dd¡ | dd¡ | dt  d¡ ¡ ¡ | d	d
¡ | j | ¡ d¡ d S )Nrý   rþ   r;   rÿ   rÜ   Ú*rX   rç   ÚexpiresrI   rê   r  ra   r   r   r   Ú
unregister`  s   zRegistrationTests.unregisterc                 C   s  |   ¡  | jd \}}|  |j|jfd¡ |  |jd¡ |  |jd dg¡ |  |jd dg¡ |  |jd d	g¡ ttƒt	krN|  
t|jd
 d ƒdv ¡ |  t| jjƒd¡ | jjd \}}|  | ¡ d	¡ | jj tjddd¡}| dd„ ¡ | | jd¡ |S )Nr   rê   rQ   rX   úSIP/2.0/UDP client.com:5060r;   rÿ   rÜ   úsip:joe@client.com:5060r  )i  i  i  i  r(   r7   rõ   ©r¡   rn   c                 S   s   | j | jfS r   ©rn   ro   )Údesturlr   r   r   r   {  s    z0RegistrationTests.testRegister.<locals>.<lambda>)r  rÒ   r)   rn   ro   rT   r-   Útyper   r   rƒ   Úintr>   rø   rù   r,   rØ   rÙ   rÈ   r   rÅ   ÚaddCallback)r   rÔ   r0   Údcr+   r¯   r   r   r   ÚtestRegisterh  s$   ÿzRegistrationTests.testRegisterc                 C   s¤   |   ¡  |  ¡  | jd \}}|  |j|jfd¡ |  |jd¡ |  |jd dg¡ |  |jd dg¡ |  |jd d	g¡ |  |jd
 dg¡ |  | jj	i ¡ d S )Nr(   rê   rQ   rX   r  r;   rÿ   rÜ   r  r  rI   )
r  r  rÒ   r)   rn   ro   rT   r-   rø   rù   ©r   rÔ   r0   r   r   r   ÚtestUnregister  s   z RegistrationTests.testUnregisterc                 C   s:   t ƒ }t |¡}t ¡ }| dd¡ | |¡ || j_d S )NzuserXname@127.0.0.1Ú	passXword)r   r   ÚPortalr   Ú'InMemoryUsernamePasswordDatabaseDontUseÚaddUserÚregisterCheckerrØ   )r   rb   Úpr\   r   r   r   Ú	addPortal‹  s   

zRegistrationTests.addPortalc                 C   sV   |   ¡  |  ¡  |  t| jjƒd¡ |  t| jƒd¡ | jd \}}|  |jd¡ d S )Nr   r(   i‘  )r  r  r)   r>   rø   rù   rÒ   rT   r  r   r   r   ÚtestFailedAuthentication“  s   z*RegistrationTests.testFailedAuthenticationc                 C   ób   t  dd¡}| dd¡ | dd¡ | dt  d¡ ¡ ¡ | j | ¡ d	¡ |  t| j	ƒd
¡ d S )Nrý   zsip:wrong.comr;   rÿ   rÜ   r   rX   rç   rê   r   ©
r   r_   r`   r…   r,   rØ   rà   r)   r>   rÒ   ra   r   r   r   ÚtestWrongDomainRegisterœ  ó   z)RegistrationTests.testWrongDomainRegisterc                 C   r  )Nrý   rþ   r;   zsip:joe@foo.comrÜ   r   rX   rç   rê   r   r  ra   r   r   r   ÚtestWrongToDomainRegister¤  r  z+RegistrationTests.testWrongToDomainRegisterc                 C   ó4   |   ¡  tjddd}| jj |¡}|  |t¡ |S )Nr7   rå   r  ©r  r   rÅ   rØ   rÙ   rÈ   ÚassertFailurerÍ   ©r   rŸ   r¯   r   r   r   ÚtestWrongDomainLookup¬  ó
   z'RegistrationTests.testWrongDomainLookupc                 C   r  )NÚjanerõ   r  r   r"  r   r   r   ÚtestNoContactLookup³  r$  z%RegistrationTests.testNoContactLookupN)r   r   r   r"   rü   r  r  r  r  r  r  r  r  r#  r&  r   r   r   r   ró   L  s    	ró   c                   @   rœ   )ÚClientc                 C   s    t j | ¡ g | _t ¡ | _d S r   )r   ÚBaseÚ__init__r—   r   ÚDeferredÚdeferredr!   r   r   r   r)  ¼  s   zClient.__init__c                 C   s   | j  |¡ | j | j ¡ d S r   )r—   r   r+  Úcallback)r   Úresponserï   r   r   r   Úhandle_responseÁ  s   zClient.handle_responseN)r   r   r   r)  r.  r   r   r   r   r'  »  s    r'  c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú	LiveTestsc                 C   sz   t jdd| _t  d¡| _| j | j_| j_tjd| jdd| _t	ƒ | _
tjd| j
dd| _| j ¡ j| j ¡ jf| _d S )NrÏ   rô   rõ   r   )Ú	interface)r   rö   rØ   r÷   rø   rÙ   r   Ú	listenUDPÚ
serverPortr'  ÚclientÚ
clientPortÚgetHostrn   ro   ÚserverAddressr!   r   r   r   r"   Ç  s   


þzLiveTests.setUpc                 C   sH   | j j ¡ D ]\}}| ¡  qt | jj¡}t | jj¡}t 	||g¡S r   )
rø   rù   rú   rû   r   ÚmaybeDeferredr4  ÚstopListeningr2  ÚgatherResults)r   r¯   r+   Úd1Úd2r   r   r   rü   Ó  s
   
zLiveTests.tearDownc                    sŽ   ˆ j  ¡ j}t dd¡}| dd¡ | dd| ¡ | dtjd|d	 ¡ ¡ ˆ j 	tj
dˆ jd
 d|¡ ˆ jj}‡ fdd„}| |¡ |S )Nrý   rþ   r;   rÿ   rÜ   úsip:joe@127.0.0.1:%drX   rÏ   rÃ   r(   r	  c                    ó*   ˆ   t| ƒd¡ | d }ˆ   |jd¡ d S ©Nr(   r   rQ   ©r)   r>   rT   ©r—   rb   r!   r   r   Úcheckå  ó   z%LiveTests.testRegister.<locals>.check)r4  r5  ro   r   r_   r`   r…   r,   r3  rÚ   rÅ   r6  r+  r  )r   r  rb   r¯   rA  r   r!   r   r  Ú  s   ÿ
zLiveTests.testRegisterc                    sØ   ˆ j  ¡ j}t dd¡}| dd¡ | dd| ¡ | dtjd|d	d
 ¡ ¡ ˆ jˆ j	gd}ˆ  
t|ƒd¡ ˆ  
|d d d¡ ˆ  
|d d t¡ ˆ j tjdˆ jd d|¡ ˆ jj}‡ fdd„}| |¡ |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.
        rý   rþ   r;   rÿ   rÜ   r<  rX   rÏ   T)ro   rw   rŠ   r(   r   r&   rŒ   r   r	  c                    r=  r>  r?  r@  r!   r   r   rA    rB  z)LiveTests.test_amoralRPort.<locals>.check)r4  r5  ro   r   r_   r`   r…   r,   r   Útest_amoralRPortr)   r>   r‘   r3  rÚ   rÅ   r6  r+  r  )r   r  rb   r’   r¯   rA  r   r!   r   rC  í  s$   	ÿÿ
zLiveTests.test_amoralRPortN)r   r   r   r"   rü   r  rC  r   r   r   r   r/  Æ  s
    r/  ))r]   Útwisted.credr   r   Útwisted.internetr   r   Útwisted.protocolsr   Útwisted.trialr   Útwisted.internet.asyncioreactorr   ÚBaseExceptionÚzope.interfacer	   Úreplacer=   rD   rF   rM   rS   rO   Úrequest_nattedrY   r   ÚTestCaser   r[   r^   rh   r   r´   ÚILocatorrÁ   rË   rÎ   ró   r(  r'  r/  r   r   r   r   Ú<module>   sŠ   ÿÿùÿÿü	ÿùÿñÿû
ÿú
ÿü	ÿñ 	x eo