B
    u9a¾  ã               @   s®   d Z ddlZddlmZ ddlZddlZddlZddlZddlZdZ	dZ
G dd„ dejƒZdd	d
„ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZedkrªe ¡  dS )z%
Tests for uu module.
Nick Mathewson
é    N)Úsupports7   The symbols on top of your keyboard are !@#$%^&*()_+|~
sO   M5&AE('-Y;6)O;',@;VX@=&]P(&]F('EO=7(@:V5Y8F]A<F0@87)E("% (R0E
*7B8J*"E?*WQ^"@  c                   s*   e Zd ZdZd
‡ fdd„	Zdd	„ Z‡  ZS )ÚFakeIOzText I/O implementation using an in-memory buffer.

    Can be a used as a drop-in replacement for sys.stdin and sys.stdout.
    Ú úutf-8ÚstrictÚ
c                sV   t t| ƒjt ¡ |||d || _|| _|rRt|tƒs>t|ƒ}|  	|¡ |  
d¡ d S )N)ÚencodingÚerrorsÚnewliner   )Úsuperr   Ú__init__ÚioÚBytesIOÚ	_encodingÚ_errorsÚ
isinstanceÚstrÚwriteÚseek)ÚselfZinitial_valuer   r	   r
   )Ú	__class__© ú/usr/lib/python3.7/test_uu.pyr      s    

zFakeIO.__init__c             C   s   |   ¡  | j ¡  | j| j¡S )N)ÚflushÚbufferÚgetvalueÚdecoder   r   )r   r   r   r   r   ,   s    zFakeIO.getvalue)r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   )r   r   r      s    r   Fc             C   sF   |r(t d| |f dƒt dd¡ d }nt d| |f dƒt d }|S )Nzbegin %03o %s
Úasciió    ó   `s   
`
end
s   
 
end
)ÚbytesÚencodedtextÚreplace)ÚmodeÚfilenameÚbacktickZresr   r   r   Úencodedtextwrapped1   s    $r+   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚUUTestc          	   C   sÖ   t  t¡}t  ¡ }t ||d¡ |  | ¡ tddƒ¡ t  t¡}t  ¡ }t ||dd¡ |  | ¡ tddƒ¡ t  t¡}t  ¡ }tj||ddd |  | ¡ tdddƒ¡ |  t	¡ t ||ddd¡ W d Q R X d S )NÚt1i¶  i¤  T)r*   )
r   r   Ú	plaintextÚuuÚencodeÚassertEqualr   r+   ÚassertRaisesÚ	TypeError)r   ÚinpÚoutr   r   r   Útest_encode<   s    


zUUTest.test_encodec          	   C   s†   x€dD ]x}t  tdd|d¡}t  ¡ }t ||¡ |  | ¡ t¡ t  dtdd|d ¡}t  ¡ }t ||¡ |  | ¡ t¡ qW d S )N)TFi¶  r-   )r*   sM   UUencoded files may contain many lines,
even some that have 'begin' in them.
)r   r   r+   r/   r   r1   r   r.   )r   r*   r4   r5   r   r   r   Útest_decodeL   s    
zUUTest.test_decodec          
   C   sj   t  dt ¡}t  ¡ }yt ||¡ |  d¡ W n4 tjk
rd } z|  t|ƒd¡ W d d }~X Y nX d S )Ns   begin 644 t1
zNo exception raisedzTruncated input file)	r   r   r&   r/   r   ÚfailÚErrorr1   r   )r   r4   r5   Úer   r   r   Útest_truncatedinput[   s    zUUTest.test_truncatedinputc          
   C   sf   t  d¡}t  ¡ }yt ||¡ |  d¡ W n4 tjk
r` } z|  t|ƒd¡ W d d }~X Y nX d S )Nó    zNo exception raisedz'No valid begin line found in input file)r   r   r/   r   r8   r9   r1   r   )r   r4   r5   r:   r   r   r   Útest_missingbegind   s    
zUUTest.test_missingbeginc       	   
   C   s    d}d}d}xŽ||fD ]‚}|   d¡8 t |¡}t ¡ }tj||dd |  | ¡ |¡ W d Q R X |   d¡& dd l}| |d¡}|  ||¡ W d Q R X qW d S )	Ns   begin 644 file
!,___
 
end
s   begin 644 file
!,___
`
end
ó   3zuu.decode()T)ÚquietÚuu_codecr   )ZsubTestr   r   r/   r   r1   r   Úcodecs)	r   Zencodedtext1Zencodedtext2r.   r&   r4   r5   rA   Zdecodedr   r   r   Útest_garbage_paddingm   s    
zUUTest.test_garbage_paddingN)r   r   r   r6   r7   r;   r=   rB   r   r   r   r   r,   :   s
   		r,   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚUUStdIOTestc             C   s   t j| _t j| _d S )N)ÚsysÚstdinÚstdout)r   r   r   r   ÚsetUp   s    zUUStdIOTest.setUpc             C   s   | j t_ | jt_d S )N)rE   rD   rF   )r   r   r   r   ÚtearDown‘   s    zUUStdIOTest.tearDownc             C   sJ   t t d¡ƒt_t ƒ t_t dddd¡ |  tj 	¡ t
ddƒ d¡¡ d S )Nr"   ú-r-   i¶  )r   r.   r   rD   rE   rF   r/   r0   r1   r   r+   )r   r   r   r   r6   •   s
    zUUStdIOTest.test_encodec             C   sZ   t tddƒ d¡ƒt_t ƒ t_t dd¡ tj}| jt_| jt_|  | ¡ t	 d¡¡ d S )Ni¶  r-   r"   rI   )
r   r+   r   rD   rE   rF   r/   r1   r   r.   )r   rF   r   r   r   r7   œ   s    zUUStdIOTest.test_decodeN)r   r   r   rG   rH   r6   r7   r   r   r   r   rC   ‹   s   rC   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú
UUFileTestc             C   s<   t jd | _t jd | _|  t j| j¡ |  t j| j¡ d S )NÚiÚo)r   ZTESTFNÚtmpinÚtmpoutÚ
addCleanupÚunlink)r   r   r   r   rG   §   s    zUUFileTest.setUpc          
   C   sê   t | jdƒ}| t¡ W d Q R X t | jdƒ2}t | jdƒ}tj||| jdd W d Q R X W d Q R X t | jdƒ}| ¡ }W d Q R X |  |t	d| jƒ¡ tj| j| j| jdd t | jdƒ}| ¡ }W d Q R X |  |t	d| jƒ¡ d S )NÚwbÚrbi¤  )r(   )
ÚopenrM   r   r.   rN   r/   r0   Úreadr1   r+   )r   ZfinZfoutÚsr   r   r   r6   ­   s    (zUUFileTest.test_encodec          	   C   s|   t | jdƒ}| td| jƒ¡ W d Q R X t | jdƒ}t |¡ W d Q R X t | jdƒ}| ¡ }W d Q R X |  |t	¡ d S )NrQ   i¤  rR   )
rS   rM   r   r+   rN   r/   r   rT   r1   r.   )r   ÚfrU   r   r   r   r7   ¿   s    zUUFileTest.test_decodec          	   C   sf   t | jdƒ}| td| jƒ¡ W d Q R X t | j¡ t | jdƒ}| ¡ }W d Q R X |  |t	¡ d S )NrQ   i¤  rR   )
rS   rM   r   r+   rN   r/   r   rT   r1   r.   )r   rV   rU   r   r   r   Útest_decode_filenameË   s    zUUFileTest.test_decode_filenamec          	   C   sz   t | jdƒ}| td| jƒ¡ W d Q R X t | jdƒ}t |¡ W d Q R X t | jdƒ}|  tjtj|¡ W d Q R X d S )NrQ   i¤  rR   )	rS   rM   r   r+   rN   r/   r   r2   r9   )r   rV   r   r   r   Útest_decodetwiceÕ   s    zUUFileTest.test_decodetwicec          	   C   sˆ   d}t | jdƒ}| t|| jƒ¡ W d Q R X |  tj| j|tj	B ¡ t | jdƒ}t
 |¡ W d Q R X |  t t | j¡j¡|¡ d S )Ni$  rQ   rR   )rS   rM   r   r+   rN   rO   ÚosÚchmodÚstatÚS_IWRITEr/   r   r1   ÚS_IMODEÚst_mode)r   Zexpected_moderV   r   r   r   Útest_decode_modeß   s    zUUFileTest.test_decode_modeN)	r   r   r   rG   r6   r7   rW   rX   r_   r   r   r   r   rJ   ¥   s   

rJ   Ú__main__)F)r    ZunittestZtestr   rY   r[   rD   r/   r   r.   r&   ÚTextIOWrapperr   r+   ZTestCaser,   rC   rJ   r   Úmainr   r   r   r   Ú<module>   s    
	QL