a 5źdgž2ć@s8ddlZddlZddlZddlZddlZddlmZddlmZddl Z ej Z dd„Z Gdd„de j ƒZ Gdd „d eƒZGd d „d ejƒZGd d „d ejƒZejdd„ƒZdefdd„ZGdd„de j ƒZGdd„deƒZGdd„deƒZeZGdd„de j ƒZGdd„de j ƒZGdd„deƒZedkr4e  ”dS) éN)Śsupport)Ś socket_helpercCsZ| ”| ”z:z| ”\}}| ”Wntjy>Yn0W| ”n | ”0dS©N)ŚlistenŚsetŚacceptŚcloseŚsocketŚtimeout)ŚevtZservŚconnŚaddr©rś//usr/local/lib/python3.9/test/test_telnetlib.pyŚserver s  rc@sTeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dS)Ś GeneralTestscCsrt ”|_t tjtj”|_|j d”t  |j”|_ tj t |j|jfd|_ |j  d”|j  ”|j ”dS)Né<)ŚtargetŚargsT)Ś threadingŚEventr r ŚAF_INETŚ SOCK_STREAMŚsockŚ settimeoutrZ bind_portŚportŚThreadrŚthreadŚ setDaemonŚstartŚwait©ŚselfrrrŚsetUps    zGeneralTests.setUpcCs|j ”|`dSr)rŚjoinr!rrrŚtearDown$s zGeneralTests.tearDowncCst t|j”}|j ”dSr)Ś telnetlibŚTelnetŚHOSTrrr©r"ŚtelnetrrrŚ testBasic(szGeneralTests.testBasiccCsNt t|j”}| | ””Wdƒn1s20Y| | ””dSr)r&r'r(rZassertIsNotNoneŚ get_socketZ assertIsNone)r"ŚtnrrrŚtestContextManager-s,zGeneralTests.testContextManagerc Csf| t ”du”t d”zt t|j”}Wt d”n t d”0| |j   ”d”|j   ”dS)Né) Ś assertTruer ŚgetdefaulttimeoutŚsetdefaulttimeoutr&r'r(rŚ assertEqualrŚ gettimeoutrr)rrrŚtestTimeoutDefault2s zGeneralTests.testTimeoutDefaultc Csl| t ”du”t d”z tjt|jdd}Wt d”n t d”0| |j  ”du”|j  ”dS©Nr/)r ) r0r r1r2r&r'r(rrr4rr)rrrŚtestTimeoutNone<s zGeneralTests.testTimeoutNonecCs2tjt|jdd}| |j ”d”|j ”dSr6)r&r'r(rr3rr4rr)rrrŚtestTimeoutValueGszGeneralTests.testTimeoutValuecCs:t ”}|jt|jdd| |j ”d”|j ”dSr6) r&r'Śopenr(rr3rr4rr)rrrŚtestTimeoutOpenLszGeneralTests.testTimeoutOpencCsJtjt|jdd}|j}| | ”|”| | ”| ””|j ”dSr6) r&r'r(rrr3r,Śfilenor)r"r*Zt_sockrrrŚ testGettersRs zGeneralTests.testGettersN) Ś__name__Ś __module__Ś __qualname__r#r%r+r.r5r7r8r:r<rrrrrs   rc@s*eZdZdZd dd„Zdd„Zdd„Zd S) Ś SocketStubz* a socket proxy that re-defines sendall() rcCst|ƒ|_g|_d|_dS)NF)ŚlistŚreadsŚwritesŚblock)r"rBrrrŚ__init__\s zSocketStub.__init__cCs|j |”dSr)rCŚappend)r"ŚdatarrrŚsendall`szSocketStub.sendallcCsZd}|jr(t|ƒ|kr(||j d”7}qt|ƒ|krV|j d||d…”|d|…}|S)Nór)rBŚlenŚpopŚinsert)r"ŚsizeŚoutrrrŚrecvbs  zSocketStub.recvN)r)r=r>r?Ś__doc__rErHrOrrrrr@Zs r@c@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) Ś TelnetAlikecCs tƒ‚dSr)ŚNotImplementedErrorr!rrrr;lszTelnetAlike.filenocCsdSrrr!rrrrnrIzTelnetAlike.closecCs |jj Sr)rrDr!rrrŚ sock_availoszTelnetAlike.sock_availcGsTt ”&}tjj||g|¢RŽWdƒn1s40Y|j| ”7_dSr)rZcaptured_stdoutr&r'ŚmsgŚ _messagesŚgetvalue)r"rTrrNrrrrTqs 4zTelnetAlike.msgN)r=r>r?r;rrSrTrrrrrQksrQc@sDeZdZdd„Zedd„ƒZddd„Zdd „Zdd d „Zd d „Z dS)Ś MockSelectorcCs i|_dSr©Śkeysr!rrrrEyszMockSelector.__init__cCsdS)Ngü©ńŅMbP?rr!rrrŚ resolution|szMockSelector.resolutionNcCst |d||”}||j|<|S)Nr)Ś selectorsŚ SelectorKeyrY)r"ŚfileobjŚeventsrGŚkeyrrrŚregister€s zMockSelector.registercCs |j |”Sr)rYrK)r"r]rrrŚ unregister…szMockSelector.unregistercCsFd}|jD]}t|tƒr |jj}q&q |r.gSdd„|j ”DƒSdS)NFcSsg|]}||jf‘qSr)r^)Ś.0r_rrrŚ ‘rIz'MockSelector.select..)rYŚ isinstancerQrrDŚvalues)r"r rDr]rrrŚselectˆs  zMockSelector.selectcCs|jSrrXr!rrrŚget_map“szMockSelector.get_map)N)N) r=r>r?rEŚpropertyrZr`rarfrgrrrrrWws   rWc#s6‡fdd„}ztj}|t_dVW|t_n|t_0dS)NcstˆƒSr)r@)Zignored©rBrrŚnew_conn™sztest_socket..new_conn)r Ścreate_connection)rBrjZold_connrrirŚ test_socket—s rlrcCsZ|D]}t|ƒtusJ|ƒ‚qt|ƒ |ddƒ}d|_Wdƒn1sL0Y|S)za return a telnetlib.Telnet object that uses a SocketStub with reads queued up to be read ŚdummyrŚN)ŚtypeŚbytesrlrU)rBŚclsŚxr*rrrŚ test_telnet£s   $rsc@seZdZdd„Zdd„ZdS)ŚExpectAndReadTestCasecCstj|_tt_dSr)r&Ś_TelnetSelectorŚ old_selectorrWr!rrrr#®szExpectAndReadTestCase.setUpcCs |jt_dSr)rvr&rur!rrrr%±szExpectAndReadTestCase.tearDownN)r=r>r?r#r%rrrrrt­srtc@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)Ś ReadTestscCsrdg}t|ƒ}| d”}|j|d|j|j|jjfdgd¢}d |dd…”}t|ƒ}| d”}| ||”dS) zc read_until(expected, timeout=None) test the blocking version of read_util s xxxmatchyyyómatchsxxxmatch)rT)s2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrxs2yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyrINé’’’’)rsZ read_untilr3ZcookedqZ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yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyysōzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzrIN)r$rsŚread_allr3)r"rBr{r*rGrrrŚ test_read_allĘs   zReadTests.test_read_allcCsBtdgƒ}| ”}| t|ƒdk”tƒ}| ”}| d|”dS)zQ read_some() Read at least one byte or EOF; may block. r}érIN)rsZ read_somer0rJr3)r"r*rGrrrŚtest_read_someŅs  zReadTests.test_read_somecCsrd}t|gƒ}t||ƒ}d|j_| d|ƒ”d|j_d}z||ƒ7}Wq:ty^YqbYq:0q:| ||”dS)z read_*_eager() Read all data available already queued or on the socket, without blocking. ódxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTrIFN)rsŚgetattrrrDr3ŚEOFError)r"Ś func_namerzr*ŚfuncrGrrrŚ _read_eagerąs    zReadTests._read_eagercCs| d”| d”dS)NZ read_eagerZread_very_eager)r‡r!rrrŚtest_read_eagerōs zReadTests.test_read_eagercCsVd}t|gƒ}| d| ””|jjr0| ”q| ”}| ||”| t|j”dS©Nr‚rI)rsr3Śread_very_lazyrrBŚ fill_rawqZ assertRaisesr„)r"rzr*rGrrrrŠüs   zReadTests.read_very_lazycCs|d}t|gƒ}| d| ””d}z | ”}||7}|s@| ”WntyXYqlYn0| | |””q"| ||”dSr‰)rsr3Z read_lazyr‹r„r0Ś startswith)r"rzr*rGZ read_datarrrŚtest_read_lazys    zReadTests.test_read_lazyN) r=r>r?r|rrr‡rˆrŠrrrrrrw“s  rwc@seZdZddd„Zdd„ZdS)Śnego_collectorNcCsd|_||_d|_dS)NrI)ŚseenŚ sb_getterŚsb_seen)r"rrrrrEsznego_collector.__init__cCs<|j||7_|tjkr8|jr8| ”}|j|7_dSr)rŚtlŚSErr‘)r"rŚcmdŚoptZsb_datarrrŚdo_negosznego_collector.do_nego)N)r=r>r?rEr–rrrrrŽs rŽc@seZdZdZdd„ZdS)Ś WriteTestszKThe only thing that write does is replace each tl.IAC for tl.IAC+tl.IACcCszddtjddtjtjdtjtjdg}|D]@}tƒ}| |”d |jj”}| | tjtjtj”|”q4dS)Nsdata sample without IACsdata sample withs one IACsa fews iacsrI) r’ŚIACrsŚwriter$rrCr3Śreplace)r"Z data_samplerGr*ZwrittenrrrŚ test_write(s ü zWriteTests.test_writeN)r=r>r?rPr›rrrrr—$sr—c@s`eZdZejejejejejej ej ej gZ dd„Z dd„Zdd„Zdd„Zd d „Zd d „Zd S)Ś OptionTestscCs’t|ƒ}td |”ƒ}tƒ}| |j”| ”}|j}| t|ƒdk”|  |dd…|j ”|  |dd…t j ”|  |t||ƒ”d|_dS)z helper for testing IAC + cmd rIrNr€é)rsrJr$rŽŚset_option_negotiation_callbackr–r~rr0ŚassertInŚcmdsr3r’ZNOOPTr)r"rGr*Zdata_lenŚnegoŚtxtr”rrrŚ _test_command8s zOptionTests._test_commandcCs^|jD]<}| tj|g”| dtj|dg”| dtj|dg”q| dd„|jDƒ”dS)Nr‚sdyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyó xxxxxxxxxxó yyyyyyyyyycSsg|]}tj|‘qSr)r’r˜)rbr”rrrrcLrIz1OptionTests.test_IAC_commands..)r r£r’r˜)r"r”rrrŚtest_IAC_commandsFs  zOptionTests.test_IAC_commandscCs0tjtjtjtjtjtjtjtjtjtjtjtjtjtjdtjtjtjtjdtjtjtjtjtjtjdtjtjdtjtjg}t|ƒ}t|jƒ}| |j”|  ”}|  |d”tjtjdtjdtjd}|  |j |”|  d| ””d|_ dS)NsaasbbsccsddrIsaabb) r’r˜ZSBr“rsrŽZ read_sb_dataržr–r~r3r‘r)r"Śsendr*r”r¢Z want_sb_datarrrŚtest_SB_commandsNs"&&*ü   $zOptionTests.test_SB_commandscCs“dtjtdgƒdftjtjtdgƒdftjtjtdgƒdftjtjtdgƒdftjtjtdgƒdfg}|D]2\}}t|gƒ}| d”|  ”}|  ||j ”q|dS) N)óaz : recv b'' éXz: IAC 88 not recognized r€z : IAC DO 1 z : IAC DONT 1 z : IAC WILL 1 z : IAC WONT 1 ) r’r˜rpZDOZDONTZWILLZWONTrsŚset_debuglevelr~rŸrU)r"Zgiven_a_expect_bŚaŚbr*r¢rrrŚtest_debuglevel_reads`sų   z!OptionTests.test_debuglevel_readscCs0tƒ}| d”| d”d}| ||j”dS)Nr€sxxxz send b'xxx' )rsr«r™rŸrU)r"r*ZexpectedrrrŚtest_debuglevel_writess   z!OptionTests.test_debuglevel_writecCs^tgƒ tddƒ}d|_Wdƒn1s.0Y| d”| d”| |jd”dS)NrmŚ0rnr€Śtestz0.*test)rlrQrUr«rTZ assertRegexr)rrrŚtest_debug_accepts_str_portzs   $  z'OptionTests.test_debug_accepts_str_portN)r=r>r?r’ZAOZAYTZBRKZECZELZGAZIPZNOPr r£r¦rØr®rÆr²rrrrrœ4s$rœc@seZdZdd„ZdS)Ś ExpectTestscCs@gd¢}t|ƒ}| dg”\}}}| |d |dd…””dS)z§ expect(expected, [timeout]) Read until the expected string has been seen, or a timeout is hit (default is no timeout); may block. )r¤rxr„rxrINry)rsr{r3r$)r"rzr*Ś_rGrrrŚ test_expect…szExpectTests.test_expectN)r=r>r?rµrrrrr³„sr³Ś__main__) r r[r&rŚ contextlibr±rZ test.supportrZunittestr(rZTestCaserŚobjectr@r'rQŚ BaseSelectorrWŚcontextmanagerrlrsrtrwrŽr’r—rœr³r=ŚmainrrrrŚs2   B    b P