3
rY];                 @   s   d Z ddlZddlZddlZddddddd	gZd
ddddddgZee ddg ZG dd dejZG dd deZ	G dd deZ
G dd deZedkrej  dS )zTest the binascii C module.    N
b2a_base64b2a_hexb2a_hqxb2a_qpb2a_uuhexlifyrlecode_hqx
a2b_base64a2b_hexa2b_hqxa2b_qpa2b_uu	unhexlifyrledecode_hqxcrc32crc_hqxc               @   s   e Zd ZeZdZeeed7 Zed7 Zdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&S )'BinASCIITests.   The quick brown fox jumps over the lazy dog.
   s   
Hello world.
c             C   s   | j | j| _d S )N)	type2testrawdatadata)self r   #lib/python3.6/test/test_binascii.pysetUp   s    zBinASCIITest.setUpc             C   s(   | j ttjt | j ttjt d S )N)
assertTrue
issubclassbinasciiError	ExceptionZ
Incomplete)r   r   r   r   test_exceptions   s    zBinASCIITest.test_exceptionsc             C   s:   x4t D ],}| jttt|d | jttt| qW d S )N__call__)all_functionsr   hasattrgetattrr   assertRaises	TypeError)r   namer   r   r   test_functions    s    
zBinASCIITest.test_functionsc             C   s  d}| j d | }xtttD ]\}}tt|}tt|}y || j|}|| j|}W n6 tk
r }	 z| jdj	|||	 W Y d d }	~	X nX |dkr|\}}
| j
||dj	|||| | j|t | j|t | jt|d qW | jtj|dt | jtj|t d S )N-   z{}/{} conversion raises {!r}r   z{}/{} conversion: {!r} != {!r}   r   )r   zipa2b_functionsb2a_functionsr$   r   r   r   failformatassertEqualassertIsInstancebytesZ
assertLessmaxr   intr   )r   MAX_ALLrawfafba2bb2aareserr_r   r   r   test_returned_value&   s&    

&z BinASCIITest.test_returned_valuec             C   s   d}g }xFt dt| j|D ]0}| j| j|||  }tj|}|j| qW t }x(|D ] }| j|}tj|}||7 }q\W | j	|| j d S )N9   r   )
rangelenr   r   r   r   appendr2   r	   r0   )r   
MAX_BASE64linesibr;   r<   liner   r   r   test_base64valid=   s    



zBinASCIITest.test_base64validc       
         s   d}g }xFt dt| j|D ]0}| j| j|||  }tj|}|j| qW t  d}x"t dD ]}||krd j| qdW  fdd}t }x.t	||D ] }	| j|	}tj
|}||7 }qW | j|| j | jtj
| j d d S )Nr@   r   s@   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/r   c                s    }t | t | }t }xX| rr|rrt | t | |krP| d | dd   }} n|d |dd   }}|j| qW || |  S )Nr      )rB   	bytearrayrC   )rH   ZnoiseZratior<   c)fillersr   r   addnoise[   s    
z1BinASCIITest.test_base64invalid.<locals>.addnoise    )rA   rB   r   r   r   r   r   rC   rK   mapr	   r0   )
r   rD   rE   rF   rG   r;   ZvalidrN   r<   rH   r   )rM   r   test_base64invalidL   s&    


zBinASCIITest.test_base64invalidc             C   s   d}g }xFt dt| j|D ]0}| j| j|||  }tj|}|j| qW t }x(|D ] }| j|}tj	|}||7 }q\W | j
|| j | j
tj	ddd  | j
tj	ddd  | j
tj	ddd  | jtjtj	d	 | jtjtj	d
 | jtjtjdd  | j
tjdd d S )Nr)   r                       s    s   !!!!.      !   xs   !>   
)rA   rB   r   r   r   r   r   rC   r2   r   r0   r%   r   )r   ZMAX_UUrE   rF   rG   r;   r<   rH   r   r   r   test_uuq   s&    



zBinASCIITest.test_uuc             C   s   t j| jdd}t j| jd|}| j|d | jtt j | jtt j| jd x*dD ]"}| jt j| jd||d
@  qZW d S )Ns   Test the CRC-32 ofr   s    this string.i7  rO   rJ   4  E# xV4i  )r   rJ   r\   r]   r^   r_   )r   r   r   r0   r%   r&   )r   crcr   r   r   test_crc_hqx   s    
zBinASCIITest.test_crc_hqxc             C   s@   t j| jd}t j| jd|}| j|d | jtt j d S )Ns   Test the CRC-32 ofs    this string.ij])r   r   r   r0   r%   r&   )r   r`   r   r   r   
test_crc32   s    zBinASCIITest.test_crc32c             C   sL   t j| j}t j| j|}t j| j|\}}t j|}| j|| j d S )N)	r   r   r   r   r   r   r   r0   r   )r   Zrler;   rG   r>   r<   r   r   r   test_hqx   s
    
zBinASCIITest.test_hqxc             C   sD   dd d dd  }t j|}| j|d t j|}| j|| d S )N   ad      b   ci,  s
   adbcc-)r   r   r0   r   )r   r   ZencodedZdecodedr   r   r   test_rle   s    

zBinASCIITest.test_rlec             C   s   d}t j| j|}t j| j|}| j|| | jt jt j|d d  | jt jt j|d d d  | jt j| j|| | jt j| j|| d S )Ns    {s   worldi   s   helloi   0rJ      qr_   r_   )	r   r   r   r
   r0   r%   r   r   r   )r   stur   r   r   test_hex   s    zBinASCIITest.test_hexc             C   s  | j }tj}tj}|ddd y|dVddi W n tk
rD   Y nX | jd | j||dd | j||dd | j||dd | j||d	d
 | j||dd
 | j||dd | j||dd | j||dd | j||dd | j||dd | j||dd | j||dd dW d | j||dd | j||dddd | jt|dd | j||dd | j||dd | j||dd  d! dd  d"  | j||d#d$ | j||dd% | j||dd | j||dddd& | j||d'ddd( | j||d)ddd* | j||d'ddd+d, | j||d-ddd- | j||dd. | j||d/d0 | j||d1d1 | j||d2d2 | j||d1d dX d. | j||d2d dY d0 | j||d3d4 | j||d5d6 | j||d5dd7d6 | j||d8d8 | j||d8dd7d9 | j||d8dd:d8 | j||d;d< | j||d;dd7d< | j||d;dd:d= | j||d>d> | j||d>dd7d? | j||d>dd:d@ | j||dAdB | j||dAdd7dB | j||dAdd:dC | j||dDd dZ dE | j||dFd d[ dG | j||dDd d\ dd7dH | j||dFd d] dd7dI | j||dDd d^ dd:dJ | j||dFd d_ dd:dK | j||dLdM | j||dNdO | j||dPdQ | j||dRdS | j||dTdT | j||dUd d` dM d S )aNrO   F)r   headerrJ   z/binascii.a2b_qp(**{1:1}) didn't raise TypeError   =s   = s   ==s   =
ABs   ABs   =
ABs   =ABs   =AB
CDs   CDs   =AB   s   =abs   =AXs   =XAs   =A   _T)rn       Zbar)Zfoos   =00
=00s    
 s   

s   =FF
=FF
=FF   0K   s   

s   =
=FF
=FF
=FFrR   s   =7Fs   =3Ds   =5Fs   x ys   x_ys   x s   x=20)rn   	quotetabss   x=20ys   x	ys   =20   	s   =09s    xs   	xrS   s   =00s    
s   =00
)ru   s   x y	zs	   x=20y=09z)istexts   x 
y	
s
   x=20
y=09
s
   x =0Ay	=0As   x y	s
   x=20y=09s
   x =0Dy	=0Ds   x 
y	
s   x=20
y=09
s   x =0D=0Ay	=0D=0As   x 
s   x s   x	
s   x	s   x=20s   x=09s   x =0Ds   x	=0D   .s   =2Es   .
s   =2E
s   .s   =2Es   . s   =2E=00s   a.
s   .a)rO   r_   r_   r_   r_   r_   r_   r_   r_   r_   r_   )r   r   r   r   r&   r.   r0   r%   )r   r   r   r   r   r   r   test_qp   s    
zBinASCIITest.test_qpc             C   s   | j d}xptD ]h}|dkr*tj|d qtt|}y|| W q tk
rv } z| jdj||| W Y d d }~X qX qW d S )NrO   r   r   z{}({!r}) raises {!r})r   r"   r   r   r$   r   r.   r/   )r   emptyfuncfr=   r   r   r   test_empty_string(  s    


zBinASCIITest.test_empty_stringc             C   s   xlt tt t dhB D ]R}y| jttt|d W q tk
rh } z| jdj	|| W Y d d }~X qX qW | jttj
dd d S )Nr   Ztestz{}("test") raises {!r}r   )setr"   r,   r%   r&   r$   r   r   r.   r/   r   )r   r{   r=   r   r   r   test_unicode_b2a6  s    (zBinASCIITest.test_unicode_b2ac             C   s  d}| j d | }xtttD ]\}}|dkr0qtt|}tt|}y,|| j|}||}|jd}||}	W n6 tk
r }
 z| j	dj
|||
 W Y d d }
~
X nX |dkr|	\}	}|\}}| j|	|dj
|||	| | j|	| | j|	t | jt|d qW d S )Nr)   r   asciiz{}/{} conversion raises {!r}r   z{}/{} conversion: {!r} != {!r}   )r   r+   r,   r-   r$   r   r   decoder   r.   r/   r0   r1   r2   r%   
ValueError)r   r5   r6   r7   r8   r9   r:   r;   Z
binary_resr<   r=   r>   r   r   r   test_unicode_a2b@  s,    


&zBinASCIITest.test_unicode_a2bc             C   sL   | j d}| jtj|d | jtj|ddd | jtj|ddd d S )Ns   hellos	   aGVsbG8=
T)newlineFs   aGVsbG8=)r   r0   r   r   )r   rG   r   r   r   test_b2a_base64_newline\  s    
z$BinASCIITest.test_b2a_base64_newlineN)__name__
__module____qualname__r2   r   r   rA   r   r    r(   r?   rI   rQ   r[   ra   rb   rc   rh   rm   ry   r}   r   r   r   r   r   r   r   r      s*   %
f
r   c               @   s   e Zd Zdd ZdS )ArrayBinASCIITestc             C   s   t j dt|S )NB)arraylist)r   rj   r   r   r   r   h  s    zArrayBinASCIITest.type2testN)r   r   r   r   r   r   r   r   r   g  s   r   c               @   s   e Zd ZeZdS )BytearrayBinASCIITestN)r   r   r   rK   r   r   r   r   r   r   l  s   r   c               @   s   e Zd ZeZdS )MemoryviewBinASCIITestN)r   r   r   
memoryviewr   r   r   r   r   r   p  s   r   __main__)__doc__Zunittestr   r   r-   r,   r"   ZTestCaser   r   r   r   r   mainr   r   r   r   <module>   s    

  Z