B
    u9aË2  ã               @   s,  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlZejZdd„ Z	G dd„ dej
ƒZG dd„ deƒZG d	d
„ d
ejƒZG dd„ dejƒZejdd„ ƒZdefdd„ZG dd„ dej
ƒZG dd„ deƒZG dd„ deƒZeZG dd„ dej
ƒZG dd„ dej
ƒZG dd„ deƒZedkr(e ¡  dS )é    N)Úsupportc             C   sT   |  ¡  |  ¡  z4y| ¡ \}}| ¡  W n tjk
r@   Y nX W d | ¡  X d S )N)ZlistenÚsetZacceptÚcloseÚsocketÚtimeout)ÚevtZservZconnZaddr© r   ú$/usr/lib/python3.7/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)Ú	threadingZEventr   r   ZAF_INETZSOCK_STREAMÚsockZ
settimeoutr   Z	bind_portÚportZThreadr
   ÚthreadZ	setDaemonÚstartÚwait)Úselfr   r   r	   ÚsetUp   s    

zGeneralTests.setUpc             C   s   | j  ¡  | ` d S )N)r   Újoin)r   r   r   r	   ÚtearDown#   s    
zGeneralTests.tearDownc             C   s   t  t| j¡}|j ¡  d S )N)Ú	telnetlibÚTelnetÚHOSTr   r   r   )r   Útelnetr   r   r	   Ú	testBasic'   s    zGeneralTests.testBasicc          	   C   s:   t  t| j¡}|  | ¡ ¡ W d Q R X |  | ¡ ¡ d S )N)r   r   r   r   ZassertIsNotNoneÚ
get_socketZassertIsNone)r   Útnr   r   r	   ÚtestContextManager,   s    zGeneralTests.testContextManagerc          	   C   s\   |   t ¡ d k¡ t d¡ zt t| j¡}W d t d ¡ X |  |j	 
¡ d¡ |j	 ¡  d S )Né   )Ú
assertTruer   ÚgetdefaulttimeoutÚsetdefaulttimeoutr   r   r   r   ÚassertEqualr   Ú
gettimeoutr   )r   r   r   r   r	   ÚtestTimeoutDefault1   s    
zGeneralTests.testTimeoutDefaultc          	   C   sb   |   t ¡ d k¡ t d¡ ztjt| jd d}W d t d ¡ X |   |j 	¡ d k¡ |j 
¡  d S )Nr!   )r   )r"   r   r#   r$   r   r   r   r   r   r&   r   )r   r   r   r   r	   ÚtestTimeoutNone;   s    
zGeneralTests.testTimeoutNonec             C   s2   t jt| jdd}|  |j ¡ d¡ |j ¡  d S )Nr!   )r   )r   r   r   r   r%   r   r&   r   )r   r   r   r   r	   ÚtestTimeoutValueF   s    zGeneralTests.testTimeoutValuec             C   s:   t  ¡ }|jt| jdd |  |j ¡ d¡ |j ¡  d S )Nr!   )r   )	r   r   Úopenr   r   r%   r   r&   r   )r   r   r   r   r	   ÚtestTimeoutOpenK   s    zGeneralTests.testTimeoutOpenc             C   sJ   t jt| jdd}|j}|  | ¡ |¡ |  | ¡ | ¡ ¡ |j ¡  d S )Nr!   )r   )	r   r   r   r   r   r%   r   Úfilenor   )r   r   Zt_sockr   r   r	   ÚtestGettersQ   s
    zGeneralTests.testGettersN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r    r'   r(   r)   r+   r-   r   r   r   r	   r      s   

r   c               @   s*   e Zd ZdZd
dd„Zdd„ 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   r3   r   r   r	   Ú__init__[   s    
zSocketStub.__init__c             C   s   | j  |¡ d S )N)r4   Úappend)r   Údatar   r   r	   Úsendall_   s    zSocketStub.sendallc             C   s^   d}x&| j r*t|ƒ|k r*|| j  d¡7 }qW t|ƒ|krZ| j  d||d … ¡ |d |… }|S )Nó    r   )r3   ÚlenÚpopÚinsert)r   ÚsizeÚoutr   r   r	   Úrecva   s    zSocketStub.recvN)r   )r.   r/   r0   Ú__doc__r6   r9   r@   r   r   r   r	   r1   Y   s   
r1   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTelnetAlikec             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)r   r   r   r	   r,   k   s    zTelnetAlike.filenoc             C   s   d S )Nr   )r   r   r   r	   r   m   r:   zTelnetAlike.closec             C   s
   | j j S )N)r   r5   )r   r   r   r	   Ú
sock_availn   s    zTelnetAlike.sock_availc          	   G   s>   t  ¡ }tjj| |f|žŽ  W d Q R X |  j| ¡ 7  _d S )N)r   Zcaptured_stdoutr   r   ÚmsgÚ	_messagesÚgetvalue)r   rE   r   r?   r   r   r	   rE   p   s    
zTelnetAlike.msgN)r.   r/   r0   r,   r   rD   rE   r   r   r   r	   rB   j   s   rB   c               @   sD   e Zd Zdd„ Zedd„ ƒZddd„Zdd	„ Zdd
d„Zdd„ Z	dS )ÚMockSelectorc             C   s
   i | _ d S )N)Úkeys)r   r   r   r	   r6   x   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   )Ú	selectorsZSelectorKeyrI   )r   ÚfileobjÚeventsr8   Úkeyr   r   r	   Úregister   s    
zMockSelector.registerc             C   s   | j  |¡S )N)rI   r<   )r   rL   r   r   r	   Ú
unregister„   s    zMockSelector.unregisterc             C   sH   d}x"| j D ]}t|tƒr|jj}P qW |r0g S dd„ | j  ¡ D ƒS d S )NFc             S   s   g | ]}||j f‘qS r   )rM   )Ú.0rN   r   r   r	   ú
<listcomp>   s    z'MockSelector.select.<locals>.<listcomp>)rI   Ú
isinstancerB   r   r5   Úvalues)r   r   r5   rL   r   r   r	   Úselect‡   s    
zMockSelector.selectc             C   s   | j S )N)rI   )r   r   r   r	   Úget_map’   s    zMockSelector.get_map)N)N)
r.   r/   r0   r6   ÚpropertyrJ   rO   rP   rU   rV   r   r   r   r	   rH   v   s   

rH   c             #   s0   ‡ fdd„}zt j}|t _d V  W d |t _X d S )Nc                 s   t ˆ ƒS )N)r1   )Zignored)r3   r   r	   Únew_conn˜   s    ztest_socket.<locals>.new_conn)r   Zcreate_connection)r3   rX   Zold_connr   )r3   r	   Útest_socket–   s    
rY   r   c          	   C   s6   x| D ]}qW t | ƒ |ddƒ}d|_W dQ R X |S )za return a telnetlib.Telnet object that uses a SocketStub with
        reads queued up to be read Údummyr   Ú N)rY   rF   )r3   ÚclsÚxr   r   r   r	   Útest_telnet¢   s    


r^   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚExpectAndReadTestCasec             C   s   t j| _tt _d S )N)r   Ú_TelnetSelectorÚold_selectorrH   )r   r   r   r	   r   ­   s    zExpectAndReadTestCase.setUpc             C   s   | j t_d S )N)ra   r   r`   )r   r   r   r	   r   °   s    zExpectAndReadTestCase.tearDownN)r.   r/   r0   r   r   r   r   r   r	   r_   ¬   s   r_   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Ú	ReadTestsc             C   st   dg}t |ƒ}| d¡}| j|d|j|j|jjfd dddg}d |dd	… ¡}t |ƒ}| d¡}|  ||¡ dS )
zc
        read_until(expected, timeout=None)
        test the blocking version of read_util
        s   xxxmatchyyys   matchs   xxxmatch)rE   s2   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs2   yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyr:   Néÿÿÿÿ)r^   Z
read_untilr%   ZcookedqZrawqr   r3   r   )r   Úwantr   r8   r3   Úexpectr   r   r	   Útest_read_until´   s    


zReadTests.test_read_untilc             C   s4   dddg}d  |¡}t|ƒ}| ¡ }|  ||¡ dS )zJ
        read_all()
          Read all data until EOF; may block.
        sô  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzr:   N)r   r^   Úread_allr%   )r   r3   re   r   r8   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.
        sô  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxé   r:   N)r^   Z	read_somer"   r;   r%   )r   r   r8   r   r   r	   Útest_read_someÑ   s    
zReadTests.test_read_somec             C   sv   d}t |gƒ}t||ƒ}d|j_|  d|ƒ ¡ d|j_d}x*y||ƒ 7 }W q< tk
r`   P Y q<X q<W |  ||¡ dS )z
        read_*_eager()
          Read all data available already queued or on the socket,
          without blocking.
        sd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTr:   FN)r^   Úgetattrr   r5   r%   ÚEOFError)r   Z	func_namerd   r   Úfuncr8   r   r   r	   Ú_read_eagerß   s    

zReadTests._read_eagerc             C   s   |   d¡ |   d¡ d S )NZ
read_eagerZread_very_eager)rn   )r   r   r   r	   Útest_read_eageró   s    
zReadTests.test_read_eagerc             C   sZ   d}t |gƒ}|  d| ¡ ¡ x|jjr2| ¡  q W | ¡ }|  ||¡ |  t|j¡ d S )Nsd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr:   )r^   r%   Úread_very_lazyr   r3   Ú	fill_rawqZassertRaisesrl   )r   rd   r   r8   r   r   r	   rp   û   s    

zReadTests.read_very_lazyc             C   s€   d}t |gƒ}|  d| ¡ ¡ d}xLy | ¡ }||7 }|sB| ¡  W n tk
rZ   P Y nX |  | |¡¡ q$W |  ||¡ d S )Nsd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr:   )r^   r%   Z	read_lazyrq   rl   r"   Ú
startswith)r   rd   r   r8   Z	read_datar   r   r	   Útest_read_lazy  s    
zReadTests.test_read_lazyN)
r.   r/   r0   rf   rh   rj   rn   ro   rp   rs   r   r   r   r	   rb   ³   s   
rb   c               @   s   e Zd Zddd„Zdd„ ZdS )Únego_collectorNc             C   s   d| _ || _d| _d S )Nr:   )ÚseenÚ	sb_getterÚsb_seen)r   rv   r   r   r	   r6     s    znego_collector.__init__c             C   s<   |  j || 7  _ |tjkr8| jr8|  ¡ }|  j|7  _d S )N)ru   ÚtlÚSErv   rw   )r   r   ÚcmdÚoptZsb_datar   r   r	   Údo_nego  s    znego_collector.do_nego)N)r.   r/   r0   r6   r|   r   r   r   r	   rt     s   
rt   c               @   s   e Zd ZdZdd„ ZdS )Ú
WriteTestszKThe only thing that write does is replace each tl.IAC for
    tl.IAC+tl.IACc             C   s~   ddt j d dt j t j d t j t jdg}xH|D ]@}tƒ }| |¡ d |jj¡}|  | t jt jt j ¡|¡ q6W d S )Ns   data sample without IACs   data sample withs    one IACs   a fews    iacsr:   )	rx   ÚIACr^   Úwriter   r   r4   r%   Úreplace)r   Zdata_sampler8   r   Zwrittenr   r   r	   Ú
test_write'  s    

zWriteTests.test_writeN)r.   r/   r0   rA   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 r:   r   Nri   é   )r^   r;   r   rt   Úset_option_negotiation_callbackr|   rg   ru   r"   ÚassertInÚcmdsr%   rx   ZNOOPTrv   )r   r8   r   Zdata_lenÚnegoÚtxtrz   r   r   r	   Ú_test_command7  s    zOptionTests._test_commandc             C   sb   xF| j D ]<}|  tj|g¡ |  dtj|dg¡ |  dtj|dg¡ qW |  dd„ | j D ƒ¡ d S )Nsd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsd   yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyys
   xxxxxxxxxxs
   yyyyyyyyyyc             S   s   g | ]}t j| ‘qS r   )rx   r~   )rQ   rz   r   r   r	   rR   K  s    z1OptionTests.test_IAC_commands.<locals>.<listcomp>)r†   r‰   rx   r~   )r   rz   r   r   r	   Útest_IAC_commandsE  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   ddr:   s   aabb)rx   r~   ZSBry   r^   rt   Zread_sb_datar„   r|   rg   r%   rw   rv   )r   Úsendr   r‡   rˆ   Zwant_sb_datar   r   r	   Útest_SB_commandsM  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}x:|D ]2\}}t|gƒ}| d¡ | 	¡ }|  
||j¡ q~W d S )	N)ó   az: recv b''
éX   z: IAC 88 not recognized
ri   z: IAC DO 1
z: IAC DONT 1
z: IAC WILL 1
z: IAC WONT 1
)rx   r~   ÚbytesZDOZDONTZWILLZWONTr^   Úset_debuglevelrg   r…   rF   )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 )Nri   s   xxxzsend b'xxx'
)r^   r   r   r…   rF   )r   r   Zexpectedr   r   r	   Útest_debuglevel_writer  s
    

z!OptionTests.test_debuglevel_writec          	   C   sJ   t g ƒ tddƒ}d|_W d Q R X | d¡ | d¡ |  |jd¡ d S )NrZ   Ú0r[   ri   Útestz0.*test)rY   rB   rF   r   rE   ZassertRegex)r   r   r   r   r	   Útest_debug_accepts_str_porty  s    



z'OptionTests.test_debug_accepts_str_portN)r.   r/   r0   rx   ZAOZAYTZBRKZECZELZGAZIPZNOPr†   r‰   rŠ   rŒ   r“   r”   r—   r   r   r   r	   r‚   3  s   $r‚   c               @   s   e Zd Zdd„ ZdS )ÚExpectTestsc             C   sB   dddg}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.
        s
   xxxxxxxxxxs   matchs
   yyyyyyyyyyr:   Nrc   )r^   re   r%   r   )r   rd   r   Ú_r8   r   r   r	   Útest_expect„  s    
zExpectTests.test_expectN)r.   r/   r0   rš   r   r   r   r	   r˜   ƒ  s   r˜   Ú__main__)r   rK   r   r   Ú
contextlibr–   r   Zunittestr   r
   ZTestCaser   Úobjectr1   r   rB   ZBaseSelectorrH   ÚcontextmanagerrY   r^   r_   rb   rt   rx   r}   r‚   r˜   r.   Úmainr   r   r   r	   Ú<module>   s.   B 
bP
