a
    äzež2  ć                   @   s8  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	Z	ej
Z
dd ZG dd de	jZG dd	 d	eZG d
d dejZG dd dejZejdd ZdefddZG dd de	jZG dd deZG dd deZeZG dd de	jZG dd de	jZG dd deZedkr4e	 ”  dS ) é    N)Śsupport)Śsocket_helperc                 C   sZ   |  ”  |  ”  z:z| ” \}}| ”  W n tjy>   Y n0 W | ”  n
| ”  0 d S ©N)ŚlistenŚsetŚacceptŚcloseŚsocketŚtimeout)ŚevtZservŚconnŚaddr© r   ś)/usr/lib/python3.9/test/test_telnetlib.pyŚserver   s    r   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )ŚGeneralTestsc                 C   sr   t  ” | _t tjtj”| _| j d” t 	| j”| _
t jt| j| jfd| _| j d” | j ”  | j ”  d S )Né<   )ŚtargetŚargsT)Ś	threadingŚEventr   r	   ŚAF_INETŚSOCK_STREAMŚsockŚ
settimeoutr   Z	bind_portŚportŚThreadr   ŚthreadŚ	setDaemonŚstartŚwait©Śselfr   r   r   ŚsetUp   s    

zGeneralTests.setUpc                 C   s   | j  ”  | ` d S r   )r   Śjoinr!   r   r   r   ŚtearDown$   s    
zGeneralTests.tearDownc                 C   s   t  t| j”}|j ”  d S r   )Ś	telnetlibŚTelnetŚHOSTr   r   r   ©r"   Śtelnetr   r   r   Ś	testBasic(   s    zGeneralTests.testBasicc                 C   sN   t  t| j”}|  | ” ” W d    n1 s20    Y  |  | ” ” d S r   )r&   r'   r(   r   ZassertIsNotNoneŚ
get_socketZassertIsNone)r"   Śtnr   r   r   ŚtestContextManager-   s    ,zGeneralTests.testContextManagerc              	   C   sf   |   t ” d u ” t d” zt t| j”}W t d ” nt d ” 0 |  |j	 
” d” |j	 ”  d S )Né   )Ś
assertTruer	   ŚgetdefaulttimeoutŚsetdefaulttimeoutr&   r'   r(   r   ŚassertEqualr   Ś
gettimeoutr   r)   r   r   r   ŚtestTimeoutDefault2   s    
zGeneralTests.testTimeoutDefaultc              	   C   sl   |   t ” d u ” t d” z tjt| jd d}W t d ” nt d ” 0 |   |j 	” d u ” |j 
”  d S ©Nr/   )r
   )r0   r	   r1   r2   r&   r'   r(   r   r   r4   r   r)   r   r   r   ŚtestTimeoutNone<   s    
zGeneralTests.testTimeoutNonec                 C   s2   t jt| jdd}|  |j ” d” |j ”  d S r6   )r&   r'   r(   r   r3   r   r4   r   r)   r   r   r   ŚtestTimeoutValueG   s    zGeneralTests.testTimeoutValuec                 C   s:   t  ” }|jt| jdd |  |j ” d” |j ”  d S r6   )	r&   r'   Śopenr(   r   r3   r   r4   r   r)   r   r   r   ŚtestTimeoutOpenL   s    zGeneralTests.testTimeoutOpenc                 C   sJ   t jt| jdd}|j}|  | ” |” |  | ” | ” ” |j ”  d S r6   )	r&   r'   r(   r   r   r3   r,   Śfilenor   )r"   r*   Zt_sockr   r   r   ŚtestGettersR   s
    zGeneralTests.testGettersN)Ś__name__Ś
__module__Ś__qualname__r#   r%   r+   r.   r5   r7   r8   r:   r<   r   r   r   r   r      s   

r   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )Ś
SocketStubz* a socket proxy that re-defines sendall() r   c                 C   s   t || _g | _d| _d S )NF)ŚlistŚreadsŚwritesŚblock)r"   rB   r   r   r   Ś__init__\   s    
zSocketStub.__init__c                 C   s   | j  |” d S r   )rC   Śappend)r"   Śdatar   r   r   Śsendall`   s    zSocketStub.sendallc                 C   sZ   d}| j r(t||k r(|| j  d”7 }qt||krV| j  d||d  ” |d | }|S )Nó    r   )rB   ŚlenŚpopŚinsert)r"   ŚsizeŚoutr   r   r   Śrecvb   s    zSocketStub.recvN)r   )r=   r>   r?   Ś__doc__rE   rH   rO   r   r   r   r   r@   Z   s   
r@   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ŚTelnetAlikec                 C   s
   t  d S r   )ŚNotImplementedErrorr!   r   r   r   r;   l   s    zTelnetAlike.filenoc                 C   s   d S r   r   r!   r   r   r   r   n   rI   zTelnetAlike.closec                 C   s
   | j j S r   )r   rD   r!   r   r   r   Ś
sock_availo   s    zTelnetAlike.sock_availc                 G   sT   t  ” &}tjj| |g|¢R   W d    n1 s40    Y  |  j| ” 7  _d S r   )r   Zcaptured_stdoutr&   r'   ŚmsgŚ	_messagesŚgetvalue)r"   rT   r   rN   r   r   r   rT   q   s    
4zTelnetAlike.msgN)r=   r>   r?   r;   r   rS   rT   r   r   r   r   rQ   k   s   rQ   c                   @   sD   e Zd Zdd Zedd ZdddZdd	 Zdd
dZdd Z	dS )ŚMockSelectorc                 C   s
   i | _ d S r   ©Śkeysr!   r   r   r   rE   y   s    zMockSelector.__init__c                 C   s   dS )Ngü©ńŅMbP?r   r!   r   r   r   Ś
resolution|   s    zMockSelector.resolutionNc                 C   s   t  |d||”}|| j|< |S )Nr   )Ś	selectorsŚSelectorKeyrY   )r"   ŚfileobjŚeventsrG   Śkeyr   r   r   Śregister   s    
zMockSelector.registerc                 C   s   | j  |”S r   )rY   rK   )r"   r]   r   r   r   Ś
unregister   s    zMockSelector.unregisterc                 C   sF   d}| j D ]}t|tr
|jj} q&q
|r.g S dd | j  ” D S d S )NFc                 S   s   g | ]}||j fqS r   )r^   )Ś.0r_   r   r   r   Ś
<listcomp>   rI   z'MockSelector.select.<locals>.<listcomp>)rY   Ś
isinstancerQ   r   rD   Śvalues)r"   r
   rD   r]   r   r   r   Śselect   s    

zMockSelector.selectc                 C   s   | j S r   rX   r!   r   r   r   Śget_map   s    zMockSelector.get_map)N)N)
r=   r>   r?   rE   ŚpropertyrZ   r`   ra   rf   rg   r   r   r   r   rW   w   s   


rW   c                 #   s6    fdd}zt j}|t _d V  W |t _n|t _0 d S )Nc                     s   t  S r   )r@   )Zignored©rB   r   r   Śnew_conn   s    ztest_socket.<locals>.new_conn)r	   Ścreate_connection)rB   rj   Zold_connr   ri   r   Śtest_socket   s    rl   r   c                 C   sF   | D ]}qt |   |dd}d|_W d   n1 s80    Y  |S )za return a telnetlib.Telnet object that uses a SocketStub with
        reads queued up to be read Śdummyr   Ś N)rl   rU   )rB   ŚclsŚxr*   r   r   r   Śtest_telnet£   s    

$rq   c                   @   s   e Zd Zdd Zdd ZdS )ŚExpectAndReadTestCasec                 C   s   t j| _tt _d S r   )r&   Ś_TelnetSelectorŚold_selectorrW   r!   r   r   r   r#   ®   s    zExpectAndReadTestCase.setUpc                 C   s   | j t_d S r   )rt   r&   rs   r!   r   r   r   r%   ±   s    zExpectAndReadTestCase.tearDownN)r=   r>   r?   r#   r%   r   r   r   r   rr   ­   s   rr   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )Ś	ReadTestsc                 C   sr   dg}t |}| d”}| j|d|j|j|jjfd g d¢}d |dd ”}t |}| d”}|  ||” dS )	zc
        read_until(expected, timeout=None)
        test the blocking version of read_util
        s   xxxmatchyyyó   matchs   xxxmatch)rT   )s2   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrv   s2   yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyrI   Né’’’’)rq   Z
read_untilr3   ZcookedqZrawqr   rB   r$   )r"   Śwantr*   rG   rB   Śexpectr   r   r   Śtest_read_untilµ   s    

zReadTests.test_read_untilc                 C   s2   g d¢}d  |”}t|}| ” }|  ||” dS )zJ
        read_all()
          Read all data until EOF; may block.
        )óō  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzrI   N)r$   rq   Śread_allr3   )r"   rB   ry   r*   rG   r   r   r   Śtest_read_allĘ   s    
zReadTests.test_read_allc                 C   sB   t dg}| ” }|  t|dk” t  }| ” }|  d|” dS )zQ
        read_some()
          Read at least one byte or EOF; may block.
        r{   é   rI   N)rq   Z	read_somer0   rJ   r3   )r"   r*   rG   r   r   r   Śtest_read_someŅ   s    
zReadTests.test_read_somec                 C   sr   d}t |g}t||}d|j_|  d| ” d|j_d}z|| 7 }W q: ty^   Y qbY q:0 q:|  ||” dS )z
        read_*_eager()
          Read all data available already queued or on the socket,
          without blocking.
        ód   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTrI   FN)rq   Śgetattrr   rD   r3   ŚEOFError)r"   Ś	func_namerx   r*   ŚfuncrG   r   r   r   Ś_read_eagerą   s    

zReadTests._read_eagerc                 C   s   |   d” |   d” d S )NZ
read_eagerZread_very_eager)r   r!   r   r   r   Śtest_read_eagerō   s    
zReadTests.test_read_eagerc                 C   sV   d}t |g}|  d| ” ” |jjr0| ”  q| ” }|  ||” |  t|j” d S ©Nr   rI   )rq   r3   Śread_very_lazyr   rB   Ś	fill_rawqZassertRaisesr   )r"   rx   r*   rG   r   r   r   r   ü   s    

zReadTests.read_very_lazyc                 C   s|   d}t |g}|  d| ” ” d}z | ” }||7 }|s@| ”  W n tyX   Y qlY n0 |  | |”” q"|  ||” d S r   )rq   r3   Z	read_lazyr   r   r0   Ś
startswith)r"   rx   r*   rG   Z	read_datar   r   r   Śtest_read_lazy  s    

zReadTests.test_read_lazyN)
r=   r>   r?   rz   r}   r   r   r   r   r   r   r   r   r   ru   “   s   
ru   c                   @   s   e Zd ZdddZdd ZdS )Śnego_collectorNc                 C   s   d| _ || _d| _d S )NrI   )ŚseenŚ	sb_getterŚsb_seen)r"   r   r   r   r   rE     s    znego_collector.__init__c                 C   s<   |  j || 7  _ |tjkr8| jr8|  ” }|  j|7  _d S r   )r   ŚtlŚSEr   r   )r"   r   ŚcmdŚoptZsb_datar   r   r   Śdo_nego  s    znego_collector.do_nego)N)r=   r>   r?   rE   r   r   r   r   r   r     s   
r   c                   @   s   e Zd ZdZdd ZdS )Ś
WriteTestszKThe only thing that write does is replace each tl.IAC for
    tl.IAC+tl.IACc                 C   sz   ddt j d dt j t j d t j t jdg}|D ]@}t }| |” d |jj”}|  | t jt jt j ”|” q4d S )Ns   data sample without IACs   data sample withs    one IACs   a fews    iacsrI   )	r   ŚIACrq   Śwriter$   r   rC   r3   Śreplace)r"   Zdata_samplerG   r*   Zwrittenr   r   r   Ś
test_write(  s    ü
zWriteTests.test_writeN)r=   r>   r?   rP   r   r   r   r   r   r   $  s   r   c                   @   s`   e Zd Zejejejejejej	ej
ejgZdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ŚOptionTestsc                 C   s   t |}td |”}t }| |j” | ” }|j}|  t|dk” |  	|dd | j
” |  |dd tj” |  |t|| ” d|_dS )z helper for testing IAC + cmd rI   r   Nr~   é   )rq   rJ   r$   r   Śset_option_negotiation_callbackr   r|   r   r0   ŚassertInŚcmdsr3   r   ZNOOPTr   )r"   rG   r*   Zdata_lenŚnegoŚtxtr   r   r   r   Ś_test_command8  s    zOptionTests._test_commandc                 C   s^   | j D ]<}|  tj|g” |  dtj|dg” |  dtj|dg” q|  dd | j D ” d S )Nr   sd   yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyó
   xxxxxxxxxxó
   yyyyyyyyyyc                 S   s   g | ]}t j| qS r   )r   r   )rb   r   r   r   r   rc   L  rI   z1OptionTests.test_IAC_commands.<locals>.<listcomp>)r   r”   r   r   )r"   r   r   r   r   Śtest_IAC_commandsF  s
    
zOptionTests.test_IAC_commandsc                 C   s0  t jt j t j t j t jt j t j t j t j t j t jt j t j t j d t j t j t jt j d t j t j t j t j t jt j d t j t j d t j t j g}t|}t|j}| |j” | 	” }|  
|d” t jt j d t j d t j d }|  
|j|” |  
d| ” ” d |_d S )Ns   aas   bbs   ccs   ddrI   s   aabb)r   r   ZSBr   rq   r   Zread_sb_datar   r   r|   r3   r   r   )r"   Śsendr*   r   r    Zwant_sb_datar   r   r   Śtest_SB_commandsN  s    "&&*ü
$zOptionTests.test_SB_commandsc                 C   s“   dt jtdg dft jt j tdg dft jt j tdg dft jt j tdg dft jt j tdg dfg}|D ]2\}}t|g}| d” | 	” }|  
||j” q|d S )	N)ó   az: recv b''
éX   z: IAC 88 not recognized
r~   z: IAC DO 1
z: IAC DONT 1
z: IAC WILL 1
z: IAC WONT 1
)r   r   ŚbytesZDOZDONTZWILLZWONTrq   Śset_debuglevelr|   r   rU   )r"   Zgiven_a_expect_bŚaŚbr*   r    r   r   r   Śtest_debuglevel_reads`  s    ų


z!OptionTests.test_debuglevel_readsc                 C   s0   t  }| d” | d” d}|  ||j” d S )Nr~   s   xxxzsend b'xxx'
)rq   rŖ   r   r   rU   )r"   r*   Zexpectedr   r   r   Śtest_debuglevel_writes  s
    

z!OptionTests.test_debuglevel_writec                 C   s^   t g   tdd}d|_W d    n1 s.0    Y  | d” | d” |  |jd” d S )Nrm   Ś0rn   r~   Śtestz0.*test)rl   rQ   rU   rŖ   rT   ZassertRegexr)   r   r   r   Śtest_debug_accepts_str_portz  s    

$

z'OptionTests.test_debug_accepts_str_portN)r=   r>   r?   r   ZAOZAYTZBRKZECZELZGAZIPZNOPr   r”   r¤   r¦   r­   r®   r±   r   r   r   r   r   4  s   $r   c                   @   s   e Zd Zdd ZdS )ŚExpectTestsc                 C   s@   g d¢}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¢   rv   r£   rv   rI   Nrw   )rq   ry   r3   r$   )r"   rx   r*   Ś_rG   r   r   r   Śtest_expect  s    zExpectTests.test_expectN)r=   r>   r?   r“   r   r   r   r   r²     s   r²   Ś__main__) r	   r[   r&   r   Ś
contextlibr°   r   Ztest.supportr   Zunittestr(   r   ZTestCaser   Śobjectr@   r'   rQ   ŚBaseSelectorrW   Ścontextmanagerrl   rq   rr   ru   r   r   r   r   r²   r=   Śmainr   r   r   r   Ś<module>   s2   B 

bP
