ϪfcdZddlmZmZddlmZmZddlmZddl m Z  ddl m Z ddlmZd d j!d d zZd j!d d Zdj!d d Zdj!d d Zdj!d d Zdj!d d Zdj!d d Zdj!d d ZGddZGdde j4ZGddeZGdde j4ZGdde j4ZGdde j4ZGd d!e j4Z eejBGd"d#Z"eejBGd$d%Z#Gd&d'e j4Z$Gd(d)e j4Z%Gd*d+ejLZ'Gd,d-e j4Z(y#e $rdZ YwxYw).z( Session Initialization Protocol tests. )checkersportal)deferreactor)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 aSIP/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 ;tag=a6c85cf From: Alice ;tag=1928301774 Call-ID: a84b4c76e66710@pc33.atlanta.com CSeq: 314159 INVITE Contact: Content-Type: application/sdp Content-Length: 0 ceZdZdZy) TestRealmc*tjddfS)NcyNr7/usr/lib/python3/dist-packages/twisted/test/test_sip.pyz)TestRealm.requestAvatar..sr)rIContact)selfavatarIdmind interfacess r requestAvatarzTestRealm.requestAvatars||T<//rN)__name__ __module__ __qualname__rrrrr r ~s0rr cHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) MessageParsingTestscng|_tj|jj|_yr)lrMessagesParserappendparserrs rsetUpzMessageParsingTests.setUps#((7 rcn|jj||jjyrr$ dataReceiveddataDone)rmessages r feedMessagezMessageParsingTests.feedMessages$   ) rc8|j|j||j|jj||j|j||j|j ||j|j dy)z$ Validate Requests. N) assertEqualmethoduritoStringheadersbodyfinished)rmr0r1r3r4s rvalidateMessagez#MessageParsingTests.validateMessageso 6* )3/ G, & Q'rc |j}|jt|jt |d|j |ddddgdgdgdd y Nr.rINVITEsip:foomojoe4fromtozcontent-lengthabcd)r!r,request1r/lenr7rr!s r testSimplezMessageParsingTests.testSimple[ FF " Q#  aD  VE7se D   rc |j}|jt|jt|j t |d|j |ddddgdgdgdd |j |d dddgdgd d y) Nrr:r;r<r=r>r?rBr.)r@rA1234)r!r,rCrequest2r/rDr7rEs rtestTwoMessagesz#MessageParsingTests.testTwoMessagess FF " " Q#  aD  VE7se D     aD(Iug'F rc |j}|jt|jt |d|j |ddddgdgdgdd y) Nr.rr:r;r<r=r>r?rJ)r!r,request3r/rDr7rEs r testGarbagezMessageParsingTests.testGarbagerGrc 6|j}|jt|jt |d|j |ddddgdgdgdd |j |d dd d gd gdgdd|j |ddd d gd gdgddy)Nrr:r;r<r=0r?r.zsip:loopfoobarr>rBrIrJ)r!r,request4r/rDr7rEs rtestThreeInOnez"MessageParsingTests.testThreeInOnes FF " Q#  aD  VE7se D     aD  WUGu E     aD  WUGu E   rc |j}|jt|jt |d|j |ddddgdgdgdd yr9)r!r, request_shortr/rDr7rEs r testShortzMessageParsingTests.testShorts[ FF ' Q#  aD  VE7se D   rc|j}|jt|jt |d|d}|j|j d|j|j d|j|jdgdgdgd|j|jd |j|jdy) Nr.rOKrTrUrRr?rS) r!r, response1r/rDcodephraser3r4r5rr!r6s rtestSimpleResponsez&MessageParsingTests.testSimpleResponses FF # Q# aD % 4(  IIw3%P  $ Q'rcR|j}|jt|jt |d|d}|j|j ddd|j|j ddd|j|j dddy) zv A header may be split across multiple lines. Subsequent lines begin with C{" "} or C{"\t"}. r.rviazHSIP/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.2rIzHSIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.1N)r!r,response_multiliner/rDr3ras rtest_multiLinez"MessageParsingTests.test_multiLines FF +, Q# aD  IIe Q  7  IIe Q  ?  IIe Q  : rN) rrrr&r,r7rFrLrOrWrZrbrfrrrrrs48(     4   ( rrceZdZdZdZy)%MessageParsingFeedDataCharByCharTestsz9 Same as base class, but feed data char by char. c||D]}|jj||jjyrr()rr+cs rr,z1MessageParsingFeedDataCharByCharTests.feedMessages3 (A KK $ $Q ' ( rN)rrr__doc__r,rrrrhrh s rrhceZdZdZdZdZy)MakeMessageTestsctjdd}|jdd|j|j dy)Nr:r;rTrUz$INVITE sip:foo SIP/2.0 Foo: bar )rRequest addHeaderr/r2rrs r testRequestzMakeMessageTests.testRequests7 KK) , E5! 'STrctjdd}|jdd|jdd|jd|j |j dy) Nr\r]rTrUzContent-Lengthr>rJz3SIP/2.0 200 OK Foo: bar Content-Length: 4 1234)rResponserpbodyDataReceivedr/r2rqs r testResponsezMakeMessageTests.testResponsesW LLd # E5! $c* 6"  JJLW rcntjd}|j|jdy)Nr\zSIP/2.0 200 OK )rrur/r2rqs rtestStatusCodezMakeMessageTests.testStatusCode%s' LL  '?@rN)rrrrsrwryrrrrmrmsU  ArrmcHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) ViaTestsc|j}|j|tj|jyr)r2r/rparseViaHeader)rvss rcheckRoundtripzViaTests.checkRoundtrip+s1 JJL C..q1::<=rc<tjd}tjd}|j|j|j|j|j|j|j|j |j y)NzSIP/2.0/UDP 192.168.1.1:5060z SIP/2.0/UDP 192.168.1.1:5060)rr}r/ transporthostport)rv1v2s rtestExtraWhitespacezViaTests.testExtraWhitespace/sl    > ?    B C r||4 "''* "''*rcd}tj|}|j|jd|j|jd|j|j d|j |j|j |j|j|j|j|jd|j|jd|j|jd|j|jd|j|jd |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)UDPzfirst.example.comiz 224.2.0.1 a7c6a8dlzerzKSIP/2.0/UDP first.example.com:4000;ttl=16;branch=a7c6a8dlze;maddr=224.2.0.1N)rr}r/rrr assertIsNonerport rportValue assertFalserportRequestedttlmaddrbranchhiddenr2rrrr~s r test_complexzViaTests.test_complex6s  ,    q ! e, !45 & !''" !,,' ))* # +. <0 1%  JJL 8 Arcd}tj|}|j|jd|j|jd|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. zSIP/2.0/UDP example.com;hiddenr example.comz#SIP/2.0/UDP example.com:5060;hiddenN)rr}r/rrrrrrrrrrr assertTruerr2rrs r test_simplezViaTests.test_simplePs -   q ! e, / & !''" !,,' ))* !%%  !''" !((# ! 'LM ArcPtjd}|j|y)Nr)rViarrr~s r testSimplerzViaTests.testSimplercs GGM " Arctjdd}|j|jg}|j t |d|j |ddd|j |dd t |j |jd |j|j|j|j|j|jy ) z| Setting rport to True is deprecated, but still produces a Via header with the expected properties. foo.barTroffendingFunctionsr.rr++rport=True is deprecated since Twisted 9.0.categorySIP/2.0/UDP foo.bar:5060;rportN) rr flushWarningstest_deprecatedRPortr/rDDeprecationWarningr2rrrrr)rr~warningss rrzViaTests.test_deprecatedRPortgs GGIT *%%$:S:S9T%U X*  QK "$Q  !Z02DE 'GH   (() !,,'rctjdd}|j|jd|j |j |j |jy)zU An rport setting of None should insert the parameter with no value. rNrr)rrr/r2rrrrrs r test_rportzViaTests.test_rportzsO GGIT * 'GH (() !,,'rctjdd}|j|jd|j |j |j|j d|j|jdy)zk An rport numeric setting should insert the parameter with the number value given. rr.rz SIP/2.0/UDP foo.bar:5060;rport=1N)rrr/r2rrrrrs rtest_rportValuezViaTests.test_rportValuesg GGIQ ' 'IJ ))* q) !$rcd}tj|}|j|jd|j|jd|j|j d|j|j d|j|jd|j|jjddy) Nz8SIP/2.0/UDP 10.0.0.1:5060;received=22.13.1.5;rport=12345r10.0.0.1rz 22.13.1.5i90z rport=12345) rr}r/rrrreceivedrassertNotEqualr2findrs rtestNATzViaTests.testNATs F   q ! e, , & [1 %( AJJL--m+4&(&( % B*rr{ceZdZdZdZy)URLTestscxdD]5}|jtj|j|7y)N)zsip:j.doe@big.comz&sip:j.doe:secret@big.com;transport=tcpz!sip:j.doe@big.com?subject=projectzsip:example.com)r/rparseURLr2)rurls r testRoundtripzURLTests.testRoundtrips9  @C   S\\#.7793 ?  @rcd}tj|}dddddddd d d d d gfddddff D]!\}}|jt|||#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)usernameuser)passwordpass)rhosta)r{)rudp)usertypephone)r0rT)r )r1.2.3.4otherblahzgoo=barr3bd)arj)rrr/getattr)rrrkr~s r testComplexzURLTests.testComplexs} 8 ll1o   !   vy) * c, -  1DAq   WS!_a 0 1rN)rrrrrrrrrrs @1rrceZdZdZy) ParseTestsc dddifdddifdddifdddifdd dd d d ffD]d\}}}}tj|\}}}|j|||j|j||j||fy) Nz""A. G. Bell" z A. G. Bellzsip:foo@example.comzAnon AnonrSzz)foo ;tag=bar;foo=bazrTrUbaz)tagrT)r parseAddressr/r2)raddressnameurlsparamsgnamegurlgparamss rtestParseAddresszParseTests.testParseAddresss5%   *63H" M "B(=r B $b*? D;%e,  ,  . 'GT4"$'#3#3G#< E4   T5 )   T]]_d 3   Wf -) .rN)rrrrrrrrrs.rrceZdZdZy) DummyLocatorcVtjtjddS)N server.comrr)rsucceedrURLr logicalURLs r getAddresszDummyLocator.getAddresss}}SWW\=>>rNrrrrrrrrrs?rrceZdZdZy)FailingLocatorc<tjtSr)rfail LookupErrorrs rrzFailingLocator.getAddressszz+-((rNrrrrrrs)rrcBeZdZdZdZdZdZdZdZdZ dZ d Z y ) ProxyTestsctjd_tj_g_fdj_y)N 127.0.0.1c>jj||fSrsentr#destmsgrs rrz"ProxyTests.setUp..4993C3CT3K3Pr)rProxyproxyrlocatorr sendMessager%s`rr&zProxyTests.setUps4YY{+ )^  !P rchtjdd}|jdtjdj |jdtjdj |jdd|jdd |jd d |j j |j d |jt|jd |jd\}}|j|jd|j|jd|j|jj d|j|jd|j|jdgdy)Nr:r;rdrz1.2.3.5rTrUrAcontactzrrr.rrr)SIP/2.0/UDP 127.0.0.1:5060SIP/2.0/UDP 1.2.3.4:5060zSIP/2.0/UDP 1.2.3.5:5060)rrorprr2rdatagramReceivedr/rDrrrr1r0r3rrrrr6s rtestRequestForwardzProxyTests.testRequestForwards8 KK) , E3779-6689 E3779-6689 E5! D01 I23 ##AJJL2CD TYY+))A,a D) L1 )95 8,  IIe   rctjdd}|jdtjdj |jdd|jdd|jd d |j j |j d |jd \}}|j|jdd dgy)Nr:r;rdrrTrUrArrzz1.1.1.1rrrz)SIP/2.0/UDP 1.2.3.4:5060;received=1.1.1.1) rrorprr2rrrr/r3rs rtestReceivedRequestForwardz%ProxyTests.testReceivedRequestForwards KK) , E3779-6689 E5! D01 I23 ##AJJL2CD))A,a  IIe  )+V W rc2tjd}|jdtjdj |j j |j d|jt|jdy)Nr\rdfoo.comr r) rrurprr2rrr/rDrrqs rtestResponseWrongViazProxyTests.testResponseWrongViasc LL  E3779-6689 ##AJJL2CD TYY+rctjd}|jdtjdj |jdtjddj |j j |j d|jt|jd|jd \}}|j|j|jfd |j|jd|j|jdd gy) Nr\rdr client.comrr r.r)rrzSIP/2.0/UDP client.com:1234)rrurprr2rrr/rDrrrr_r3rs rtestResponseForwardzProxyTests.testResponseForwards LL  E377;/88:; E377.1sAHHQKrr r.r) rrurprr2r gotResponserr/rDr3getr_)rrrr6addrr!s @rtestResponseToUszProxyTests.testResponseToUs-s LL  E377;/88:; !7  ##AJJL2CD Q#A$4 QYY]]5"56: %rctjdd}|jdtjdj |jdtjdj |j j |j d|j|jgy)Nr:r;rdrrr) rrorprr2rrr/rrqs rtestLoopzProxyTests.testLoop8s KK) , E3779-6689 E377;/88:; ##AJJL2FG B'rcvtjdd}|jdtjdj |jddt |j _|j j|j d|jt|jd|jd \}}|j|j|jfd|j|jd |j|jdd gy) Nr:r;rdrrArrr.rir)rrorprr2rrrrr/rDrrrr_r3rs rtestCantForwardRequestz!ProxyTests.testCantForwardRequest?s KK) , E3779-6689 D01+-  ##AJJL2CD TYY+))A,a $))TYY/1BC % 5),F+GHrN) rrrr&r r rrrrrr rrrrrs3Q  .  , LG &( IrrcTeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zy )RegistrationTestsctjd_tjd_jxj_j_g_fdj_y)Nrrbell.example.comc>jj||fSrrrs rrz)RegistrationTests.setUp..Rrr)r RegisterProxyrInMemoryRegistryregistryrrrr%s`rr&zRegistrationTests.setUpMsW&&K8 ,,-?@ 37==@ djj0 !P rc|jjjD]\}}|j|`yr)r)usersvaluescancelr)rrr1s rtearDownzRegistrationTests.tearDownTs6mm))002 FAs HHJ  Jrc2tjdd}|jdd|jdd|jdtjdj |j j |j d y) NREGISTERsip:bell.example.comrAsip:joe@bell.example.comrsip:joe@client.com:1234rdrrrrorprr2rrrqs rregisterzRegistrationTests.registerYsl KK $: ; D45 I89 E377<099;< ##AJJL2FGrcVtjdd}|jdd|jdd|jdtjdj |jd d |j j |j d y) Nr0r1rAr2r*rdrexpiresrRrr4rqs r unregisterzRegistrationTests.unregister`sy KK $: ; D45 Is# E377<099;< Is# ##AJJL2FGrc|j|jd\}}|j|j|jfd|j|j d|j|j ddg|j|j ddg|j|j dd gtttk7r,|jt|j d dd v|jt|jjd |jjd \}}|j|jd |j j"j%t'j(d d}|j+d|j+|jd|S)Nrrr\rdSIP/2.0/UDP client.com:5060rAr2rsip:joe@client.com:5060r8)iiiir.r=r%rrc2|j|jfSrrr)desturls rrz0RegistrationTests.testRegister..{sw||W\\&Br)r5rr/rrr_r3typerr rintrDr)r+r2rrrrr addCallback)rrr6dcr1rs r testRegisterzRegistrationTests.testRegisterhsx ))A,a $))TYY/1EF % 5),I+JK 4+E*FG 9-0I/JK =2 2 OOC ) 4Q 78 '5 ! rcH|j|j|jt|jj d|jt|j d|j d\}}|j|jdy)Nrr.i)rPr5r/rDr)r+rr_rGs rtestFailedAuthenticationz*RegistrationTests.testFailedAuthenticationsq   T]]00115 TYY+))A,a %rc|tjdd}|jdd|jdd|jdtjdj |j j |j d |jt|jd y) Nr0z sip:wrong.comrAr2rr3rdrrr rrorprr2rrr/rDrrqs rtestWrongDomainRegisterz)RegistrationTests.testWrongDomainRegisters KK O 4 D45 I89 E377<099;< ##AJJL2FG TYY+rc|tjdd}|jdd|jdd|jdtjdj |j j |j d |jt|jd y) Nr0r1rAzsip:joe@foo.comrr3rdrrrrTrqs rtestWrongToDomainRegisterz+RegistrationTests.testWrongToDomainRegisters KK $: ; D+, I89 E377<099;< ##AJJL2FG TYY+rc|jtjdd}|jjj |}|j |t|S)Nr=rr=r5rrrrr assertFailurerrrrs rtestWrongDomainLookupz'RegistrationTests.testWrongDomainLookupsJ ggu95 JJ   ) )# . 1k*rc|jtjdd}|jjj |}|j |t|S)Njaner%r=rYr[s rtestNoContactLookupz%RegistrationTests.testNoContactLookupsK ggv,>? JJ   ) )# . 1k*rN)rrrr&r.r5r9rErHrPrRrUrWr\r_rrrr"r"LsAQ HH. 2&,,rr"ceZdZdZdZy)Clientctjj|g|_t j |_yr)rBase__init__rrDeferreddeferredr%s rrdzClient.__init__s) $ ( rc|jj||jj|jyr)rr#rfcallback)rresponsers rhandle_responsezClient.handle_responses* X& t}}-rN)rrrrdrjrrrraras ) .rrac$eZdZdZdZdZdZy) LiveTestsctjd|_tjd|_|jx|j_|j_t jd|jd|_t|_ t jd|jd|_ |jjj|jjjf|_y)Nrr$r%r) interface)rr'rr(r)rr listenUDP serverPortraclient clientPortgetHostrr serverAddressr%s rr&zLiveTests.setUps&&K8 ,,-?@ 37==@ djj0!++Atzz[Qh !++At{{kR OO # # % * * OO # # % * * rcL|jjjD]\}}|jt j |j j}t j |jj}t j||gSr) r)r+r,r-r maybeDeferredrr stopListeningrp gatherResults)rrr1d1d2s rr.zLiveTests.tearDownszmm))002 FAs HHJ   !>!> ?  !>!> ?""B8,,rcjjj}tjdd}|j dd|j dd|z|j dtj d| jjjtjdjd  |jj}fd }|j||S) Nr0r1rAr2rsip:joe@127.0.0.1:%drdrrr.r?c~jt|d|d}j|jdyNr.rr\r/rDr_rrrrs rcheckz%LiveTests.testRegister..check4   S]A . A   QVVS )r)rrrsrrrorprr2rqrrrtrfrC)rrOrrrrs` rrEzLiveTests.testRegisters OO # # % * * KK $: ; D45 I59: E377;Q7@@BC  GG4+=+=a+@ A1  KK  * ercjjj}tjdd}|j dd|j dd|z|j dtj d|d jjjg }jt|d j|d ddj|d dtjjtjdj d |jj"}fd}|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. r0r1rAr2rr|rdrT)rrrr.rr+rrr?c~jt|d|d}j|jdyr~rrs rrz)LiveTests.test_amoralRPort..checkrr)rrrsrrrorprr2rtest_amoralRPortr/rDrrqrrrtrfrC)rrOrrrrrs` rrzLiveTests.test_amoralRPorts4 OO # # % * * KK $: ; D45 I59: E377;QdCLLNO%%$:O:O9P%Q X*  QK "$Q  !Z02DE  GG4+=+=a+@ A1  KK  * erN)rrrr&r.rErrrrrlrls  -&rrl))rk twisted.credrrtwisted.internetrrtwisted.protocolsr twisted.trialrtwisted.internet.asyncioreactorr BaseExceptionzope.interfacer replacerCrKrNrVr^rYrequest_nattedrer TestCaserrhrmr{rrILocatorrrrr"rcrarlrrrrs- *+!""F'  f     &     7&    &  (   G&   &   G& g&(00 E (++E P,?Ax((A(u*x  u*p1x  1@."".2 S\\???  S\\))) bI""bIJl))l^.SXX.F!!Fm"!"sGGG