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 Zd	dd„Zdd„ Zdd„ ZdS )
Ú
SocketStubr   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   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   rD   r   r?   r   r   r	   rD   p   s    
zTelnetAlike.msgN)r.   r/   r0   r,   r   rC   rD   r   r   r   r	   rA   j   s   rA   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   )Ú	selectorsZSelectorKeyrH   )r   ÚfileobjÚeventsr8   Úkeyr   r   r	   Úregister   s    
zMockSelector.registerc             C   s   | j  |¡S )N)rH   r<   )r   rK   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   )rL   )Ú.0rM   r   r   r	   ú
<listcomp>   s    z'MockSelector.select.<locals>.<listcomp>)rH   Ú
isinstancerA   r   r5   Úvalues)r   r   r5   rK   r   r   r	   Úselect‡   s    
zMockSelector.selectc             C   s   | j S )N)rH   )r   r   r   r	   Úget_map’   s    zMockSelector.get_map)N)N)
r.   r/   r0   r6   ÚpropertyrI   rN   rO   rT   rU   r   r   r   r	   rG   v   s   

rG   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   rW   Zold_connr   )r3   r	   Útest_socket–   s    
rX   r   c          	   C   s6   x| D ]}qW t | ƒ |ddƒ}d|_W d Q R X |S )NÚdummyr   Ú )rX   rE   )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_selectorrG   )r   r   r   r	   r   ­   s    zExpectAndReadTestCase.setUpc             C   s   | j t_d S )N)r`   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 |d d… ¡}t |ƒ}| d¡}|  ||¡ d S )	Ns   xxxmatchyyys   matchs   xxxmatch)rD   s2   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs2   yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyr:   éÿÿÿÿ)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|ƒ}| ¡ }|  ||¡ d S )Nsô  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzr:   )r   r]   Úread_allr%   )r   r3   rd   r   r8   r   r   r	   Útest_read_allÅ   s    

zReadTests.test_read_allc             C   sB   t dgƒ}| ¡ }|  t|ƒdk¡ t ƒ }| ¡ }|  d|¡ d S )Nsô  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxé   r:   )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 |  ||¡ d S )Nsd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTr:   F)r]   Úgetattrr   r5   r%   ÚEOFError)r   Z	func_namerc   r   Úfuncr8   r   r   r	   Ú_read_eagerß   s    

zReadTests._read_eagerc             C   s   |   d¡ |   d¡ d S )NZ
read_eagerZread_very_eager)rm   )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_rawqZassertRaisesrk   )r   rc   r   r8   r   r   r	   ro   û   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_lazyrp   rk   r"   Ú
startswith)r   rc   r   r8   Z	read_datar   r   r	   Útest_read_lazy  s    
zReadTests.test_read_lazyN)
r.   r/   r0   re   rg   ri   rm   rn   ro   rr   r   r   r   r	   ra   ³   s   
ra   c               @   s   e Zd Zddd„Zdd„ ZdS )Únego_collectorNc             C   s   d| _ || _d| _d S )Nr:   )ÚseenÚ	sb_getterÚsb_seen)r   ru   r   r   r	   r6     s    znego_collector.__init__c             C   s<   |  j || 7  _ |tjkr8| jr8|  ¡ }|  j|7  _d S )N)rt   ÚtlÚSEru   rv   )r   r   ÚcmdÚoptZsb_datar   r   r	   Údo_nego  s    znego_collector.do_nego)N)r.   r/   r0   r6   r{   r   r   r   r	   rs     s   
rs   c               @   s   e Zd Zdd„ ZdS )Ú
WriteTestsc             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:   )	rw   ÚIACr]   Úwriter   r   r4   r%   Úreplace)r   Zdata_sampler8   r   Zwrittenr   r   r	   Ú
test_write'  s    

zWriteTests.test_writeN)r.   r/   r0   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¡ |  	|d d… | j
¡ |  |dd… tj¡ |  |t|| ƒ¡ d |_d S )Nr:   r   rh   é   )r]   r;   r   rs   Úset_option_negotiation_callbackr{   rf   rt   r"   ÚassertInÚcmdsr%   rw   ZNOOPTru   )r   r8   r   Zdata_lenÚnegoÚtxtry   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   )rw   r}   )rP   ry   r   r   r	   rQ   K  s    z1OptionTests.test_IAC_commands.<locals>.<listcomp>)r…   rˆ   rw   r}   )r   ry   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)rw   r}   ZSBrx   r]   rs   Zread_sb_datarƒ   r{   rf   r%   rv   ru   )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
rh   z: IAC DO 1
z: IAC DONT 1
z: IAC WILL 1
z: IAC WONT 1
)rw   r}   ÚbytesZDOZDONTZWILLZWONTr]   Úset_debuglevelrf   r„   rE   )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 )Nrh   s   xxxzsend b'xxx'
)r]   r   r~   r„   rE   )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 )NrY   Ú0rZ   rh   Útestz0.*test)rX   rA   rE   r   rD   ZassertRegex)r   r   r   r   r	   Útest_debug_accepts_str_porty  s    



z'OptionTests.test_debug_accepts_str_portN)r.   r/   r0   rw   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 |d d… ¡¡ d S )Ns
   xxxxxxxxxxs   matchs
   yyyyyyyyyyr:   rb   )r]   rd   r%   r   )r   rc   r   Ú_r8   r   r   r	   Útest_expect„  s    
zExpectTests.test_expectN)r.   r/   r0   r™   r   r   r   r	   r—   ƒ  s   r—   Ú__main__)r   rJ   r   r   Ú
contextlibr•   r   Zunittestr   r
   ZTestCaser   Úobjectr1   r   rA   ZBaseSelectorrG   ÚcontextmanagerrX   r]   r^   ra   rs   rw   r|   r   r—   r.   Úmainr   r   r   r	   Ú<module>   s.   B 
bP
