ϪfTddlZddlZddlmZddlmZddlmZddlZ ddl m Z m Z m Z mZmZddlmZddlmZmZmZddlmZdd lmZmZdd lmZdd lmZ dd l m!Z!m"Z"Gd deZ$dddddddZ%d-dZ&dZ'GddeZ(GddZ)GddZ*GddeZ+GddeZ,GddeZ-Gd d!eZ.Gd"d#Z/Gd$d%ej`Z1ee! d&ee jded d&Gd'd(eZ3Gd)d*e/eZ4Gd+d,eZ5y#e#$rdZ!dZ"YwxYw).N)List)skipIf)directlyProvides)defererror interfacesprotocolreactor)StringTransport)AdvancedPOP3Client InsecureAuthenticationDisallowedServerErrorResponse)pop3testserver)basicloopback)log)TestCase)ClientTLSContextServerTLSContextceZdZdZy)#StringTransportWithConnectionLosingc^|jjtjyN)r connectionLostrConnectionDoneselfs C/usr/lib/python3/dist-packages/twisted/mail/test/test_pop3client.pyloseConnectionz2StringTransportWithConnectionLosing.loseConnection!s $$U%9%9%;<N)__name__ __module__ __qualname__rr rrr s=r rs180sLOGIN)sTOPs LOGIN-DELAYsUIDLSTLSsUSERsSASLct}t|_t}||_|j ||r|j d||fS)Ns +OK Hello! ) POP3ClientcapCache _capCacherr makeConnection dataReceived)greetpts rsetUpr//sH AAK+-AAJQ  () a4Kr c |fdS)Nc|Srr$)resultfs rzstrip..Asqsr r$)r3s rstripr5@s  ""r cZeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd Zy)POP3ClientLoginTestsctd\}}d|_|jdd}|jdj |t j fdS)NFr,Tusernamepasswords-ERR Offline for maintenance cBj|jddS)NrsOffline for maintenance assertEqualargsexcrs rr4z;POP3ClientLoginTests.testNegativeGreeting..Ks((!6PQr )r/allowInsecureLoginloginr+ assertFailurer addCallbackrr-r.ds` rtestNegativeGreetingz)POP3ClientLoginTests.testNegativeGreetingEsZ5!1# GGK - :;!!!%89EE Q  r ct\}}|jd}|j|jd|j d|j |jdS)Nr:USER username s+OK send password s send password)r/userr>valuer+rErFs r testOkUserzPOP3ClientLoginTests.testOkUserNsWw1 FF;  $89 /0}}T--/?@@r ct\}}|jd}j|jd|j dj |t jfdS)Nr:rJs-ERR account suspended cBj|jddS)Nrsaccount suspendedr=r@s rr4z2POP3ClientLoginTests.testBadUser..[s((!6JKr )r/rKr>rLr+rDrrErFs` r testBadUserz POP3ClientLoginTests.testBadUserUsdw1 FF;  $89 45!!!%89EE K  r ct\}}|jd}|j|jd|j d|j |jdS)Nr;PASS password s+OK you're in! s you're in!)r/passwordr>rLr+rErFs r testOkPasszPOP3ClientLoginTests.testOkPass^sVw1 JJ{ # $89 ,-}}T--}==r ct\}}|jd}j|jd|j dj |t jfdS)Nr;rRs-ERR go away cBj|jddS)Nrsgo awayr=r@s rr4z2POP3ClientLoginTests.testBadPass..ks((!jAr )r/rSr>rLr+rDrrErFs` r testBadPassz POP3ClientLoginTests.testBadPassesdw1 JJ{ # $89 *+!!!%89EE A  r cJt\}}d|_|jdd}|j|j d|j d|j|j d|j d|j |jdS) NTr:r;rJs+OK go ahead USER username PASS password s+OK password accepted spassword accepted)r/rBrCr>rLr+rErFs r testOkLoginz POP3ClientLoginTests.testOkLoginnsw1# GGK - $89 *+ $IJ 34}}T--/CDDr cdt\}}d|_|jdd}j|j d|j dj|j d|j dj |tjfdS) NTr:r;rJs+OK waiting on you rY-ERR bogus login cBj|jddSNrs bogus loginr=r@s rr4z;POP3ClientLoginTests.testBadPasswordLogin..((!nEr r/rBrCr>rLr+rDrrErFs` rtestBadPasswordLoginz)POP3ClientLoginTests.testBadPasswordLoginxsw1# GGK - $89 01 $IJ ./!!!%89EE E  r ct\}}d|_|jdd}j|j d|j dj |tjfdS)NTr:r;rJr\cBj|jddSr^r=r@s rr4z;POP3ClientLoginTests.testBadUsernameLogin..r_r r`rFs` rtestBadUsernameLoginz)POP3ClientLoginTests.testBadUsernameLoginsnw1# GGK - $89 ./!!!%89EE E  r c|td\}}|jd|j|jdy)NFr9s"+OK lalala this has no challenge r/r+r>serverChallengerr-r.s rtestServerGreetingz'POP3ClientLoginTests.testServerGreetings35!1 >? **D1r c|td\}}|jd|j|jdy)NFr9s+OK srfrhs rtestServerGreetingWithChallengez4POP3ClientLoginTests.testServerGreetingWithChallenges45!1 9: **,FGr ctd\}}|jd|jdd}|j|j d|jd|j |jdS) NFr9s"+OK r:r;s0APOP username f34f1e464d0d7927607753129cabe39a s+OK Welcome! sWelcome!)r/r+rCr>rLrErFs rtestAPOPzPOP3ClientLoginTests.testAPOPsm5!1 >? GGK -  GGIL  *+}}T--{;;r ctd\}}|jd|jdd}|j|j |j |t S)NFr9 +OK Howdy r:r;)r/r+rC assertFalserLrDr rFs r testInsecureLoginRaisesExceptionz5POP3ClientLoginTests.testInsecureLoginRaisesExceptionsU5!1 '( GGK - #!!!%EFFr ctd\}}t|tj|j d|j dd}|j |jd|j|j d|j |jd|j d|S) z If a server doesn't offer APOP but the transport is secured using SSL or TLS, a plaintext login should be allowed, not rejected with an InsecureAuthenticationDisallowed exception. Fr9ror:r;rJs+OK rR) r/rr ISSLTransportr+rCr>rLclearrFs r testSSLTransportConsideredSecurez5POP3ClientLoginTests.testSSLTransportConsideredSecures 5!1J445 '( GGK - $89   z" $89 z"r N)r!r"r#rHrMrPrTrWrZrardrirkrmrqrur$r rr7r7DsG A > E   2 H <Gr r7ceZdZdZdZy) ListConsumerci|_yrdatars r__init__zListConsumer.__init__  r cd|\}}|jj|gj|yr)rz setdefaultappend)rr2itemrLs rconsumezListConsumer.consumes* u T2&--e4r Nr!r"r#r{rr$r rrwrws 5r rwceZdZdZdZy)MessageConsumercg|_yrryrs rr{zMessageConsumer.__init__r|r c:|jj|yr)rzrrlines rrzMessageConsumer.consumes r Nrr$r rrrs r rc0eZdZdZdZdZdZdZdZy)POP3ClientListTestsct\}}|j}|j|jd|j d|j d|j |jgdS)NLIST +OK Here it comes s1 3 2 2 3 1 . ))r/listSizer>rLr+rErFs r testListSizez POP3ClientListTests.testListSizes_w1 JJL K0 /0 45}}T--y99r c t\}}t}|j}|j|}|j |j d|j d|j d|j |jdgdgdgd|j d|j |jdgdgdgdgdgdgd |j d |j|j|S) Nrrs1 3 2 2 3 1 rrr)rrrs5 3 6 2 7 1 )rrr. ) r/rwrrr>rLr+rzrEassertIdenticalrr-r.cr3rGs rtestListSizeWithConsumerz,POP3ClientListTests.testListSizeWithConsumersw1 N II JJqM K0 /0 /0 aSaSaS!9: /0 aSaSaSaSaSaS!QR x }}T11155r ct\}}|j}j|jd|j dj |t jfdS)Nr!-ERR Fatal doom server exploded cBj|jddSNrsFatal doom server explodedr=r@s rr4z8POP3ClientListTests.testFailedListSize..((!6STr )r/rr>rLr+rDrrErFs` rtestFailedListSizez&POP3ClientListTests.testFailedListSizes`w1 JJL K0 =>!!!%89EE T  r ct\}}|j}|j|jd|j d|j d|j |jgdS)NUIDL rs1 abc 2 def 3 ghi . )abcsdefsghi)r/listUIDr>rLr+rErFs r testListUIDzPOP3ClientListTests.testListUIDs`w1 IIK K0 /0 :;}}T--/GHHr ct\}}t}|j}|j|}|j |j d|j d|j d|j |jdgdgdgd|j d|j|j|S) Nrrs1 xyz 2 abc 5 mno sxyzrsmno)rrrr) r/rwrrr>rLr+rzrErrs rtestListUIDWithConsumerz+POP3ClientListTests.testListUIDWithConsumersw1 N II IIaL K0 /0 56 fX6(x!HI x }}T11155r ct\}}|j}j|jd|j dj |t jfdS)NrrcBj|jddSrr=r@s rr4z7POP3ClientListTests.testFailedListUID..rr )r/rr>rLr+rDrrErFs` rtestFailedListUIDz%POP3ClientListTests.testFailedListUIDs`w1 IIK K0 =>!!!%89EE T  r N) r!r"r#rrrrrrr$r rrrs!: 6 I 6 r rc<eZdZdZdZdZdZdZdZdZ dZ y ) POP3ClientMessageTestscBt\}}|jd}|j|jd|j d|j d|j d|j d|j |jddgS) NRETR 8 +OK Message incoming La la la here is message text s"..Further message text tra la la rLa la la here is message texts.Further message text tra la lar/retriever>rLr+rErFs r testRetrievez#POP3ClientMessageTests.testRetrieve sw1 JJqM M2 23 ;< >? x }}    -/Q R  r cLt\}}t}|j}|jd|}|j |j d|j d|j d|j d|j|j||S)Nrrrrs..Further message text . ) r/rrrr>rLr+rE_cbTestRetrieveWithConsumerrs rtestRetrieveWithConsumerz/POP3ClientMessageTests.testRetrieveWithConsumersw1   II JJq!  M2 23 ;< 9:}}T==q!DDr cd|j|||j|jddgy)Nrs.Further message textrr>rzrr2r3rs rrz2POP3ClientMessageTests._cbTestRetrieveWithConsumer#s0 VQ'  FF57OP r cFt\}}|jdd}|j|jd|j d|j d|j d|j d|j |jd d gS) Nrrlines TOP 8 2 +OK 2 lines on the way Line the first! Woop Line the last! Bye rLine the first! WoopLine the last! ByerrFs rtestPartialRetrievez*POP3ClientMessageTests.testPartialRetrieve)sw1 JJqJ " N3 45 34 12 x }}   79OP  r crt\}}t}|j}|jd|d}|j |j d|j d|j d|j d|j d|j|j||S) Nrrrrrrrr) r/rrrr>rLr+rE"_cbTestPartialRetrieveWithConsumerrs rtestPartialRetrieveWithConsumerz6POP3ClientMessageTests.testPartialRetrieveWithConsumer5sw1   II JJq!1J % N3 45 34 12 x }}TDDaKKr cd|j|||j|jddgy)Nrrrrs rrz9POP3ClientMessageTests._cbTestPartialRetrieveWithConsumerAs- VQ' ":.Krr )r/rr>rLr+rDrrErFs` rtestFailedRetrievez)POP3ClientMessageTests.testFailedRetrieveEsbw1 JJqM M2 =>!!!%89EE T  r c Xt\}}tdDcgc]=}|j|j|jd|dzfzd|dzfzg?}}tddD]}|j |j d|fz|j |jd|jd|fz|jd |fz|j |j d |jd tj|d Scc}w)z Issue three retrieve calls immediately without waiting for any to succeed and make sure they all do succeed eventually. rsFirst line of %d.rsSecond line of %d.rs RETR %d rsFirst line of %d. sSecond line of %d. r rT)fireOnOneErrback) r/rangerrEr>rLrtr+r DeferredList)rr-r.imessagess rtest_concurrentRetrievesz/POP3ClientMessageTests.test_concurrentRetrievesNs w1 1X   JJqM % %  %Q02G1q5(2RS   q! %A   QWWY!(= > GGI NN8 9 NN3qd: ; NN4t; <   QWWY , NN8 $ %!!(TBB# sAD'N) r!r"r#rrrrrrrrr$r rrr s.   E   LU Cr rcHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) POP3ClientMiscTestsc t\}}|jd}|j|jd|j d|j d|j |jdddgdddgdgd S) NruseCacheCAPA s+OK Capabilities on the way s!X Y Z A 1 2 3 B 1 2 C 1 . )123rr)XYZABCr/ capabilitiesr>rLr+rErFs rtestCapabilityz"POP3ClientMiscTests.testCapabilityisw1 NNAN & K0 9: IJ}}   (Tlf    r ct\}}|jd}|j|jd|j d|j |jiS)Nrrr-ERR This server is lame! rrFs rtestCapabilityErrorz'POP3ClientMiscTests.testCapabilityError{sWw1 NNAN & K0 78}}T--r22r ct\}}|j}|j|jd|j d|j |jdS)NSTAT s +OK 1 1212 )ri)r/statr>rLr+rErFs rtestStatzPOP3ClientMiscTests.testStatsRw1 FFH K0 ()}}T--y99r ct\}}|j}j|jd|j dj |t jfdS)NrrcBj|jddSNrsThis server is lame!r=r@s rr4z3POP3ClientMiscTests.testStatError..((!6MNr )r/rr>rLr+rDrrErFs` r testStatErrorz!POP3ClientMiscTests.testStatError`w1 FFH K0 78!!!%89EE N  r ct\}}|j}|j|jd|j d|j |jdS)NNOOP s+OK No-op to you too! sNo-op to you too!)r/noopr>rLr+rErFs rtestNoopzPOP3ClientMiscTests.testNoopsSw1 FFH K0 34}}T--/CDDr ct\}}|j}j|jd|j dj |t jfdS)NrrcBj|jddSrr=r@s rr4z3POP3ClientMiscTests.testNoopError..rr )r/rr>rLr+rDrrErFs` r testNoopErrorz!POP3ClientMiscTests.testNoopErrorrr ct\}}|j}|j|jd|j d|j |jdS)NRSET s+OK Reset state s Reset state)r/resetr>rLr+rErFs rtestRsetzPOP3ClientMiscTests.testRsetsRw1 GGI K0 -.}}T--~>>r ct\}}|j}j|jd|j dj |t jfdS)NrrcBj|jddSrr=r@s rr4z3POP3ClientMiscTests.testRsetError..rr )r/rr>rLr+rDrrErFs` r testRsetErrorz!POP3ClientMiscTests.testRsetErrors`w1 GGI K0 78!!!%89EE N  r ct\}}|jd}|j|jd|j d|j |jdS)NrDELE 4 s+OK Hasta la vista sHasta la vista)r/deleter>rLr+rErFs r testDeletezPOP3ClientMiscTests.testDeletesUw1 HHQK M2 01}}T--/@AAr ct\}}|jd}j|jd|j dj |t jfdS)Nrrs-ERR Winner is not you. cBj|jddS)NrsWinner is not you.r=r@s rr4z5POP3ClientMiscTests.testDeleteError..s((!6KLr )r/rr>rLr+rDrrErFs` rtestDeleteErrorz#POP3ClientMiscTests.testDeleteErrorsbw1 HHQK M2 56!!!%89EE L  r N) r!r"r#rrrrrrrrrrr$r rrrhs6 $3: E ? B r rceZdZddZdZy) SimpleClientNc ||_d|_y)NT)deferredrB)rr contextFactorys rr{zSimpleClient.__init__s  "&r c:|jjdyr)r callback)r challenges rserverGreetingzSimpleClient.serverGreetings t$r r)r!r"r#r{rr$r rr r s '%r r c2eZdZdZdZdZdZdZdZdZ y)POP3HelperMixinNctj}tj|j|_t ||j|_d|j_ ||_ y)N)r ) rDeferredrPOP3TestServer serverCTXserverr  clientCTXclienttimeout connected)rrGs rr/zPOP3HelperMixin.setUpsI NN $334>>R "1T^^D   r c|`|`|`yr)rrrrs rtearDownzPOP3HelperMixin.tearDowns K K Nr cL|jjjyr)r transportr)rignores r _cbStopClientzPOP3HelperMixin._cbStopClients ,,.r c|jjj|jjj|Sr)rr!rr)rfailures r _ebGeneralzPOP3HelperMixin._ebGenerals3 ,,. ,,.r cZtj|j|jdS)NF)noisy)r loopbackTCPrrrs rrzPOP3HelperMixin.loopbacks##DKKEJJr ) r!r"r#rrr/rr#r&rr$r rrrs&II / Kr rc6eZdZGddejZy)TLSServerFactoryc2eZdZUdZgZeeed<dZdZ y)TLSServerFactory.protocolNoutputcg|j_|jdd|_|jjdD]}|j |y)Nr)factoryinputr.popsendLiners rconnectionMadez(TLSServerFactory.protocol.connectionMadesE!#DLL ++a.DK * $ d# $r c|jjj||jj dDcgc]}|j |c}|dk(r&|j j|jyycc}w)Nrr%) r0r1rr.r2r3r!startTLScontext)rrls r lineReceivedz&TLSServerFactory.protocol.lineReceivedse LL   % %d +'+{{q'9 :!T]]1  :w'' 5 ;sB ) r!r"r#r7r.rbytes__annotations__r4r9r$r rr r-s U   $  6r r N)r!r"r#r LineReceiverr r$r rr+r+s65%%6r r+zOpenSSL not presentceZdZdZdZy) POP3TLSTestsz= Tests for POP3Client's support for TLS connections. cn t dggddgddgdgg j_t j_t j d d}j|j|jj}|jj}tj ttjt fd}| _tj"} fd|_t j$||| fd } fd } fd } fd } j&j)| j&j)| j&j)| j&j+|  j&S) zd POP3Client.startTLS starts a TLS session over its existing TCP connection. +OK)r@r%.rArz 127.0.0.1) interfacecTtj|jdyr)r rr)reasonconnLostDeferredcps rrz2POP3TLSTests.test_startTLS..connectionLosts!  ' 'F 3  % %d +r cSrr$)rFsrr4z,POP3TLSTests.test_startTLS..sbr cNtjdjS)Nz!Connected to server; starting TLS)rmsgr6ignoredrFs r cbConnectedz/POP3TLSTests.test_startTLS..cbConnected!s GG7 8;;= r cNtjdjS)NzStarted TLS; disconnecting)rrIquitrJs r cbStartedTLSz0POP3TLSTests.test_startTLS..cbStartedTLS%s GG0 1779 r cltjdjjgdy)Nz.Disconnected; asserting correct input received)CAPAr%rQsQUIT)rrIr>r1)ignrsfs rcbDisconnectedz2POP3TLSTests.test_startTLS..cbDisconnected)s% GGD E   RXX'K Lr cRtjdjS)NzEAsserted correct input; disconnecting client and shutting down server)rrI disconnect)r2connrEs rcleanupz+POP3TLSTests.test_startTLS..cleanup-s& GG2  OO # #r )r+r r.rr7r listenTCP addCleanup stopListeninggetHosthostportrrr rr ClientFactory connectTCPr rEaddBoth)rr^HPrcfrLrOrTrXrWrErFrSs` @@@@r test_startTLSzPOP3TLSTests.test_startTLSsR   H # H TN H   /0   B+> **+ LLN   LLN   >>+ %..*,<,> ? ,+  # # %  !!!Q+ !  M $  ,  - / G${{r N)r!r"r#__doc__rer$r rr>r>s 9r r>ceZdZdZy)POP3TimeoutTestscfd}fdfdfd}dj_dt_||g}t j j t t|j j jj jjjS)Ncjjdd}|j|j|S)Ntesttwisted)rrCrE addErrback)rGloggedInrtimedOuts rrCz+POP3TimeoutTests.testTimeout..login?s6 !!&)4A MM( # LL "Hr c(jdy)Nz%Successfully logged in!? Impossible!)fail)r2rs rrnz.POP3TimeoutTests.testTimeout..loggedInEs II= >r cf|jtjjdyr)trapr TimeoutErrorr#)r%rs rroz.POP3TimeoutTests.testTimeout..timedOutHs# LL++ ,   t $r c8jjSr)rrNrsrrNz*POP3TimeoutTests.testTimeout..quitLs;;##% %r g{Gz?T) rrrTIMEOUT_RESPONSEmaprrEr5r#rmr&r)rrCrNmethodsrnros` @@r testTimeoutzPOP3TimeoutTests.testTimeout>s   ? % &# +/'$- DNN & &E7(;< ""4#5#56 !!$//2}}r N)r!r"r#ryr$r rrhrh=sr rhceZdZdZdZdZy)POP3ClientModuleStructureTestszt Miscellaneous tests more to do with module/package structure than anything to do with the POP3 client. cl|jtjjjgy)z twisted.mail._pop3client.__all__ should be empty because all classes should be imported through twisted.mail.pop3. N)r>rlmail _pop3client__all__rs rtest_allz'POP3ClientModuleStructureTests.test_allbs$ 11992>r ctjtjdtjDcgc]}|dddk(s|d}}|D]}|dk(sR|j t tjj||dtjjZ|j t tjjdycc}w)a Every public class in twisted.mail._pop3client should be available as a member of twisted.mail.pop3 with the exception of twisted.mail._pop3client.POP3Client which should be available as twisted.mail.pop3.AdvancedClient. ztwisted.mail._pop3clientr_r'z not in r N) inspect getmemberssysmodulesisclass assertTruehasattrrlr}pop3)rr publicClassespcs r test_importz*POP3ClientModuleStructureTests.test_importis'' 67 Q47c> aD    RB%GLL--r2d(7<<#4#4"56  (9(9;O PQ R sC#N)r!r"r#rfrrr$r rr{r{\s ?Rr r{)T)6rrtypingrunittestrzope.interfacertwisted.mail._pop3clientrltwisted.internetrrrr r twisted.internet.testingr twisted.mail.pop3r r'r rtwisted.mail.testrtwisted.protocolsrrtwisted.pythonrtwisted.trial.unittestrtwisted.test.ssl_helpersrr ImportErrorrr(r/r5r7rwrrrrr r ServerFactoryr+ IReactorSSLr>rhr{r$r rrss  +HH4 --+K =/=       "#t8tn55; (; |[CX[C|X (X v%:%KK86x--6$ 34 "J " "7D 113HI>8>J5>B>#RX#RCsD33 D?>D?