B
    u9aw                @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ ed Zed ZdZd	Zd
dddgZ dd Z!dd Z"G dd dZ#G dd de#e
j$Z%eG dd de#e
j$Z&eG dd de#e
j$Z'eG dd de#e
j$Z(G dd dZ)G dd de)e
j$Z*eG d d! d!e)e
j$Z+eG d"d# d#e)e
j$Z,eG d$d% d%e)e
j$Z-G d&d' d'Z.G d(d) d)e.e
j$Z/eG d*d+ d+e.e
j$Z0eG d,d- d-e.e
j$Z1eG d.d/ d/e.e
j$Z2G d0d1 d1e
j$Z3G d2d3 d3e
j$Z4G d4d5 d5e
j$Z5G d6d7 d7Z6G d8d9 d9e6e
j$Z7eG d:d; d;e6e
j$Z8eG d<d= d=e6e
j$Z9eG d>d? d?e6e
j$Z:G d@dA dAe
j$Z;G dBdC dCZ<G dDdE dEe<e
j$Z=eG dFdG dGe<e
j$Z>eG dHdI dIe<e
j$Z?eG dJdK dKe<e
j$Z@G dLdM dMZAG dNdO dOZBG dPdQ dQe
j$ZCeG dRdS dSe
j$ZDG dTdU dUe
j$ZEG dVdW dWe
j$ZFG dXdY dYe
j$ZGeHdZkre
I  dS )[    N)TemporaryFile)randintrandomgetrandbits)script_helper)
TESTFNfindfileunlinkrmtreetemp_dirtemp_cwdrequires_zlibrequires_bz2requires_lzmacaptured_stdout2di  Zzipfile_datafiles)Z	_ziptest1Z
1q2w3e4r5t)zziptest2dir/_ziptest2Z
qawsedrftg)z!ziptest2dir/ziptest3dir/_ziptest3Z
azsxdcfvgb)z-ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3Z
6y7u8i9o0pc             C   s   t d|  | dS )N   little)r   to_bytes)size r   "/usr/lib/python3.7/test_zipfile.pygetrandbytes   s    r   c          	   c   sT   t V  t }|V  | |j W d Q R X t }|V  | |j W d Q R X d S )N)TESTFN2r   assertFalseclosedioBytesIO)testfr   r   r   	get_files"   s    
r!   c               @   s  e Zd Zedd Zdd ZdBddZdCd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*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dS )DAbstractTestsWithSourceFilec             C   s&   dd t tD | _d| j| _d S )Nc             S   s    g | ]}t d |t f dqS )z'Zipfile test line %d. random float: %f
ascii)bytesr   ).0ir   r   r   
<listcomp>.   s   z:AbstractTestsWithSourceFile.setUpClass.<locals>.<listcomp>    )rangeFIXEDTEST_SIZEline_genjoindata)clsr   r   r   
setUpClass,   s    z&AbstractTestsWithSourceFile.setUpClassc          	   C   s&   t td}|| j W d Q R X d S )Nwb)openr   writer-   )selffpr   r   r   setUp3   s    z!AbstractTestsWithSourceFile.setUpNc          
   C   s   ||d}t j|df|`}|td |tt |d| j |jddd }x| jD ]}|| qZW W d Q R X W d Q R X d S )N)compressioncompresslevelwzanother.namestrfilezwritten-open-w)mode)zipfileZipFiler2   r   writestrr-   r1   r+   )r3   r    r6   r7   kwargszipfpliner   r   r   make_test_archive8   s    
z-AbstractTestsWithSourceFile.make_test_archivec          	   C   s6  |  ||| t|d|}| |t| j | |d| j | |d| j t }|j	|d |
 }| }| t|d | d|d  | d|d  | d	|d  |d
  \}}	}
}| |d | t|	d | t|
d | |tt| j | }| t|d | t| | d| | d| | d| | }dd |D }| t|d | t| | d| | d| | d| x"|D ]}| |jt| j qW xBtdddfD ]2}||}| |j| | |jt| j qW |  W d Q R X d S )Nrzanother.namer9   )file   z	File Namer   ModifiedSize   z%Y-%m-%dz%H:%M:%S   zwritten-open-wc             S   s   g | ]
}|j qS r   )filename)r%   r&   r   r   r   r'   g   s    z8AbstractTestsWithSourceFile.zip_test.<locals>.<listcomp>)rA   r;   r<   assertEqualreadr   r-   r   StringIOprintdirgetvalue
splitlineslenassertInsplit
assertTruetimestrptimestrnamelistinfolist	file_sizegetinforI   testzip)r3   r    r6   r7   r?   r4   	directorylinesfndatetime_r   namesinfosr&   nminfor   r   r   zip_testC   sL    

z$AbstractTestsWithSourceFile.zip_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   re   r6   )r3   r    r   r   r   
test_basicy   s    z&AbstractTestsWithSourceFile.test_basicc       	   
   C   s   |  || t|d|}g }|t&}x|d}|s>P || q.W W d Q R X g }|d&}x|d}|sxP || qhW W d Q R X | d|| j	 | d|| j	 W d Q R X d S )NrB      zanother.namer(   )
rA   r;   r<   r1   r   rK   appendrJ   r,   r-   )	r3   r    r6   r?   zipdata1zipopen1	read_datazipdata2zipopen2r   r   r   zip_open_test}   s$    

z)AbstractTestsWithSourceFile.zip_open_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   rn   r6   )r3   r    r   r   r   	test_open   s    z%AbstractTestsWithSourceFile.test_openc          	   C   sF   t t}| || j t|d| j}| |jt	 W d Q R X d S )NrB   )
pathlibPathr   rn   r6   r;   r<   assertIsInstancerI   rV   )r3   pathr?   r   r   r   test_open_with_pathlike   s    
z3AbstractTestsWithSourceFile.test_open_with_pathlikec          
   C   s~   |  || t|d|Z}g }|t,}x$|tdd}|sDP || q.W W d Q R X | d	|| j
 W d Q R X d S )NrB   rG   i   r(   )rA   r;   r<   r1   r   rK   r   rh   rJ   r,   r-   )r3   r    r6   r?   ri   rj   rk   r   r   r   zip_random_open_test   s    z0AbstractTestsWithSourceFile.zip_random_open_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   ru   r6   )r3   r    r   r   r   test_random_open   s    z,AbstractTestsWithSourceFile.test_random_openc          
   C   sv   |  || t|d@}|t*}g }x|d}|s<P || q,W W d Q R X W d Q R X | d|| j	 d S )NrB   r(   )
rA   r;   r<   r1   r   read1rh   rJ   r,   r-   )r3   r    r6   r?   zipopenzipdatark   r   r   r   zip_read1_test   s    
"z*AbstractTestsWithSourceFile.zip_read1_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r{   r6   )r3   r    r   r   r   
test_read1   s    z&AbstractTestsWithSourceFile.test_read1c          
   C   s   |  || t|dP}|t:}g }x.|d}| t|d |sLP || q,W W d Q R X W d Q R X | 	d
|| j d S )NrB   
   r(   )rA   r;   r<   r1   r   rx   ZassertLessEqualrP   rh   rJ   r,   r-   )r3   r    r6   r?   ry   rz   rk   r   r   r   zip_read1_10_test   s    
"z-AbstractTestsWithSourceFile.zip_read1_10_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r~   r6   )r3   r    r   r   r   test_read1_10   s    z)AbstractTestsWithSourceFile.test_read1_10c          
   C   s   |  || t|dT}|t>}d}x2| }|s:P ||7 }|d}|sRP ||7 }q,W W d Q R X W d Q R X | || j d S )NrB   r(   d   )	rA   r;   r<   r1   r   readlinerK   rJ   r-   )r3   r    r6   r?   ry   r-   rK   r   r   r   zip_readline_read_test   s    
 z2AbstractTestsWithSourceFile.zip_readline_read_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r   r6   )r3   r    r   r   r   test_readline_read   s    z.AbstractTestsWithSourceFile.test_readline_readc          
   C   sb   |  || t|d@}|t*}x"| jD ]}| }| || q.W W d Q R X W d Q R X d S )NrB   )rA   r;   r<   r1   r   r+   r   rJ   )r3   r    r6   r?   ry   r@   Zlinedatar   r   r   zip_readline_test   s    z-AbstractTestsWithSourceFile.zip_readline_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r   r6   )r3   r    r   r   r   test_readline   s    z)AbstractTestsWithSourceFile.test_readlinec          
   C   sl   |  || t|dJ}|t}| }W d Q R X x$t| j|D ]\}}| || qFW W d Q R X d S )NrB   )	rA   r;   r<   r1   r   	readlineszipr+   rJ   )r3   r    r6   r?   ry   Zziplinesr@   zipliner   r   r   zip_readlines_test   s    z.AbstractTestsWithSourceFile.zip_readlines_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r   r6   )r3   r    r   r   r   test_readlines
  s    z*AbstractTestsWithSourceFile.test_readlinesc          
   C   sd   |  || t|dB}|t,}x$t| j|D ]\}}| || q4W W d Q R X W d Q R X d S )NrB   )rA   r;   r<   r1   r   r   r+   rJ   )r3   r    r6   r?   ry   r@   r   r   r   r   zip_iterlines_test  s
    z.AbstractTestsWithSourceFile.zip_iterlines_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   r   r6   )r3   r    r   r   r   test_iterlines  s    z*AbstractTestsWithSourceFile.test_iterlinesc          
   C   s   t td| j}|dd W d Q R X t td| j@}|d*}| |dd | |dd W d Q R X W d Q R X d S )Nr8   r9   Z12rB   rG      1   2)r;   r<   r   r6   r=   r1   rJ   rK   )r3   r?   Zopenobjr   r   r   test_low_compression  s    z0AbstractTestsWithSourceFile.test_low_compressionc             C   s<   t td}|jdd| jd |d}| |j| j d S )Nr8   zb.txtzhello world)compress_type)r;   r<   r   r=   r6   rZ   rJ   r   )r3   r?   rd   r   r   r   test_writestr_compression'  s    
z5AbstractTestsWithSourceFile.test_writestr_compressionc             C   s   t jtddd}|jdd| jd |jdd| jdd	 |d}| |j| j | |jd |d}| |j| j | |jd d S )
Nr8   rG   )r7   za.txtzhello world)r   zb.txt   )r   r7   )	r;   r<   r   r=   r6   rZ   rJ   r   _compresslevel)r3   r?   Za_infoZb_infor   r   r   test_writestr_compresslevel-  s    

z7AbstractTestsWithSourceFile.test_writestr_compresslevelc             C   s   xzdD ]r}|d }t |}tt d| jD}|d| |dd }||}| 	t
|| W d Q R X W d Q R X qW d S )N)rG   i  i   i  i @  rG   r8   foorB   )r   r;   r<   r   r   r6   r=   r1   rK   rJ   rP   )r3   Z	test_sizerY   Zjunkzipfr4   bufr   r   r   test_read_return_size=  s    

z1AbstractTestsWithSourceFile.test_read_return_sizec             C   s  t  }tj|dd"}|jd| j| jd | }W d Q R X | }t |}t|H}|	d2}|
|d  | t |  W d Q R X W d Q R X W d Q R X t |}t|P}|	d:}|
|d  | t x|drqW W d Q R X W d Q R X W d Q R X t |}t|T}|	d>}|
|d  | t x|dr`qPW W d Q R X W d Q R X W d Q R X d S )Nr8   )r:   r9   )r      r   )r   r   r;   r<   r=   r-   r6   tellrN   r1   truncateassertRaisesEOFErrorrK   rx   )r3   r4   r   Z
end_offsetzipfiledatary   r   r   r   test_truncated_zipfileI  s2    
&
"
z2AbstractTestsWithSourceFile.test_truncated_zipfilec             C   s  d}xvt | D ]h}t|d| j&}|t| t|}| d| W d Q R X t|d}t|}t|t	r| d| | n| d| | | d| t|
|}| d| | | d| | d	| | jtjkr| d
| | d| ||D}t|}| d| | | d| | jtjkrF| d
| W d Q R X | dt| W d Q R X | dt| qW d S )Nz	file.namer8   zmode='w'rB   zfilename=%rzfile=%rzmode='r'z	filemode=z
file_size=zcompress_type=zcompress_size=zname=%rz[closed])r!   r;   r<   r6   r2   r   reprrQ   
isinstancerV   rZ   
ZIP_STOREDr1   )r3   fnamer    r?   rB   ry   r   r   r   	test_reprg  s6    
z%AbstractTestsWithSourceFile.test_reprc             C   s(   x"t | D ]}| j|| jdd q
W d S )N	   )r7   )r!   re   r6   )r3   r    r   r   r   test_compresslevel_basic  s    z4AbstractTestsWithSourceFile.test_compresslevel_basicc          	   C   sl   t jtdddR}|td |jtddd |d}|d}| |jd | |jd W d Q R X d S )Nr8   rG   )r7   Z
compress_1Z
compress_9r   )r;   r<   r   r2   r   rZ   rJ   r   )r3   r?   Zone_infoZ	nine_infor   r   r   test_per_file_compresslevel  s    

z7AbstractTestsWithSourceFile.test_per_file_compresslevelc          
      s`  G  fdddt j}dx| }d  t|d| j~}|dd}|d W d Q R X d zJy&|dd}|d W d Q R X W n tk
r   d	7 Y nX P W d d  X W d Q R X tt | *}| 	|
 dg | 	|dd W d Q R X qW tt | >}| 	|
 ddg | 	|dd | 	|dd W d Q R X d S )
Nc                   s    e Zd Z fddZ  ZS )zCAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFilec                s,   d k	rkrt d7 t | d S )NrG   )OSErrorsuperr2   )r3   r-   )	__class__countstopr   r   r2     s
    zIAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFile.write)__name__
__module____qualname__r2   __classcell__r   )r   r   )r   r   
BrokenFile  s   r   r   r8   Zfile1s   data1Zfile2s   data2rG   )r   r   r;   r<   r6   r1   r2   r   rN   rJ   rW   rK   )r3   r   Ztestfiler?   r    r   )r   r   r   test_writing_errors  s.    	 z/AbstractTestsWithSourceFile.test_writing_errorsc             C   s   t t t t d S )N)r	   r   r   )r3   r   r   r   tearDown  s    z$AbstractTestsWithSourceFile.tearDown)N)N)$r   r   r   classmethodr/   r5   rA   re   rf   rn   ro   rt   ru   rv   r{   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   +   s@   

6

	%r"   c               @   sv   e Zd ZejZ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 )StoredTestsWithSourceFileNc          	   C   sX   |  || t|d6}|d}| |jd |d}| |jd W d Q R X d S )NrB   r9   i  zwritten-open-w)rA   r;   r<   rZ   rJ   external_attr)r3   r    r6   r?   zinfoZzinfo2r   r   r   zip_test_writestr_permissions  s    

z7StoredTestsWithSourceFile.zip_test_writestr_permissionsc             C   s$   xt | D ]}| |tj q
W d S )N)r!   r   r;   r   )r3   r    r   r   r   test_writestr_permissions  s    z3StoredTestsWithSourceFile.test_writestr_permissionsc          	   C   sZ   t tdt j}|td W d Q R X t tdt j}| | dg W d Q R X d S )Nr8   z	/absoluterB   absolute)r;   r<   r   r   r2   r   rJ   rW   )r3   r?   r   r   r   test_absolute_arcnames  s    z0StoredTestsWithSourceFile.test_absolute_arcnamesc          	   C   sj   t tdt j}|tt W d Q R X t tdt j(}|d| j | |	 tdg W d Q R X d S )Nr8   ar9   )
r;   r<   r   r   r2   r   r=   r-   rJ   rW   )r3   r?   r   r   r   test_append_to_zip_file  s
    z1StoredTestsWithSourceFile.test_append_to_zip_filec          
   C   s8  d}t td}|| W d Q R X ttdtj}|tt W d Q R X t tdR}|t| t|d,}| 	|
 tg | 	|t| j W d Q R X W d Q R X t td$}| 	|t|| | }W d Q R X t|B}t|,}| 	|
 tg | 	|t| j W d Q R X W d Q R X d S )Ns   I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!r0   r   rbrB   )r1   r   r2   r;   r<   r   r   seekrP   rJ   rW   rK   r-   r   r   )r3   r-   r    r?   r   bior   r   r   test_append_to_non_zip_file  s     (z5StoredTestsWithSourceFile.test_append_to_non_zip_filec          
   C   s   t  6}t|dtj}|tt W d Q R X | }W d Q R X d}tt	d}|| || W d Q R X tt	,}| 
| tg | 
|t| j W d Q R X d S )Nr8   s   I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!I am not a ZipFile!r0   )r   r   r;   r<   r   r2   r   rN   r1   r   rJ   rW   rK   r-   )r3   r   r?   r   r-   r    r   r   r   test_read_concatenated_zip_file  s    

z9StoredTestsWithSourceFile.test_read_concatenated_zip_filec          
   C   sN  t  6}t|dtj}|tt W d Q R X | }W d Q R X dd }tt	d}|| || W d Q R X tt	d&}| 
| tg |d| j W d Q R X tt	d$}| 
|t|| | }W d Q R X t |X}t|B}| 
| tdg | 
|t| j | 
|d| j W d Q R X W d Q R X d S )Nr8   s   I am not a ZipFile!i@B r0   r   r9   r   )r   r   r;   r<   r   r2   r   rN   r1   r   rJ   rW   r=   r-   rK   rP   )r3   r   r?   r   r-   r    r   r   r   $test_append_to_concatenated_zip_file
  s$    

z>StoredTestsWithSourceFile.test_append_to_concatenated_zip_filec          	   C   sr   t tdt j}|tt W d Q R X ttd}|d W d Q R X t td}| |t j W d Q R X d S )Nr8   r   z
   rB   )r;   r<   r   r   r2   r   r1   rr   )r3   r?   r    r   r   r   test_ignores_newline_at_end   s    z5StoredTestsWithSourceFile.test_ignores_newline_at_endc          	   C   s   t tdt j}d|_|tt W d Q R X ttd}|d W d Q R X t td"}| |t j | 	|jd W d Q R X d S )Nr8   s   this is a commentr   zabcdef
rB   )
r;   r<   r   r   commentr2   r   r1   rr   rJ   )r3   r?   r    r   r   r   )test_ignores_stuff_appended_past_comments(  s    zCStoredTestsWithSourceFile.test_ignores_stuff_appended_past_commentsc          
   C   sR   t td<}|t ttd}| |t|  W d Q R X W d Q R X d S )Nr8   r   )r;   r<   r   r2   r   r1   rJ   rK   )r3   r?   r    r   r   r   test_write_default_name2  s    
z1StoredTestsWithSourceFile.test_write_default_namec          
   C   s   t jtdd}|dd W d Q R X t jtdd}| t|jt W d Q R X t jtdd*}| t |jtdd W d Q R X W d Q R X d S )Nr8   )r:   zsomefile.txtbogusrB   )	r;   r<   r   r=   r   
ValueErrorr2   r   r1   )r3   r?   r   r   r   test_write_to_readonly:  s    z0StoredTestsWithSourceFile.test_write_to_readonlyc          	   C   s8   t td ttd}| t|jt W d Q R X d S )N)r   r   r8   )	osutimer   r;   r<   r   r   r   r2   )r3   r?   r   r   r   test_add_file_before_1980G  s    z3StoredTestsWithSourceFile.test_add_file_before_1980)r   r   r   r;   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	
r   c               @   s   e Zd ZejZdd ZdS )DeflateTestsWithSourceFilec          	   C   sp   t tdZ}|tdt j |tdt j |d}|d}| |j	t j | |j	t j W d Q R X d S )Nr8   ZstoremeZ	deflateme)
r;   r<   r   r2   r   r   ZIP_DEFLATEDrZ   rJ   r   )r3   r?   ZsinfoZdinfor   r   r   test_per_file_compressionS  s    

z4DeflateTestsWithSourceFile.test_per_file_compressionN)r   r   r   r;   r   r6   r   r   r   r   r   r   N  s   r   c               @   s   e Zd ZejZdS )Bzip2TestsWithSourceFileN)r   r   r   r;   	ZIP_BZIP2r6   r   r   r   r   r   ^  s   r   c               @   s   e Zd ZejZdS )LzmaTestsWithSourceFileN)r   r   r   r;   ZIP_LZMAr6   r   r   r   r   r   c  s   r   c               @   sH   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )AbstractTestZip64InSmallFilesc             C   s$   dd t dtD }d|| _d S )Nc             s   s   | ]}t d | dV  qdS )zTest of zipfile line %d.r#   N)r$   )r%   r&   r   r   r   	<genexpr>o  s   z;AbstractTestZip64InSmallFiles.setUpClass.<locals>.<genexpr>r      
)r)   r*   r,   r-   )r.   r+   r   r   r   r/   m  s    z(AbstractTestZip64InSmallFiles.setUpClassc          	   C   sB   t j| _t j| _dt _dt _ttd}|| j W d Q R X d S )Ni  r   r0   )	r;   ZIP64_LIMIT_limitZIP_FILECOUNT_LIMIT_filecount_limitr1   r   r2   r-   )r3   r4   r   r   r   r5   s  s    z#AbstractTestZip64InSmallFiles.setUpc          	   C   sP  t j|d|dd,}|td |tt |d| j W d Q R X t |d|}| |t| j | |d| j | |d| j t	 }|
| | }| }| t|d | d|d	  | d
|d	  | d|d	  |d  \}}}	}
| |d | t|d | t|	d | |
tt| j | }| t|d | t| | d| | d| | }dd |D }| t|d | t| | d| | d| x"|D ]}| |jt| j qW x@tddfD ]2}||}| |j| | |jt| j qW |  W d Q R X d S )Nr8   T)
allowZip64zanother.namer9   rB   rH   z	File Namer   rE   rF   rG   z%Y-%m-%dz%H:%M:%S   c             S   s   g | ]
}|j qS r   )rI   )r%   r&   r   r   r   r'     s    z:AbstractTestZip64InSmallFiles.zip_test.<locals>.<listcomp>)r;   r<   r2   r   r=   r-   rJ   rK   r   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   rI   r[   )r3   r    r6   r?   r4   r\   r]   r^   r_   r`   r   ra   rb   r&   rc   rd   r   r   r   re   }  sN    


z&AbstractTestZip64InSmallFiles.zip_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   re   r6   )r3   r    r   r   r   rf     s    z(AbstractTestZip64InSmallFiles.test_basicc             C   s   t jtd| jdd}d|_d}x,t|D ] }|d| d|d d	   q(W | t|	 | |
  t td
| j}| t|	 | x<t|D ]0}|d| d}| |d|d d	   qW |
  d S )Nr8   T)r   r      zfoo%08dz%dr   9   rB   r#   )r;   r<   r   r6   debugr)   r=   rJ   rP   rW   closerK   decode)r3   r   numfilesr&   zipf2contentr   r   r   test_too_many_files  s     z1AbstractTestZip64InSmallFiles.test_too_many_filesc          	   C   s  t jtd| jdd}d|_d}x,t|D ] }|d| d|d d	   q(W | t|	 | | 
t j |d| d
 W d Q R X | t|	 | |  t jtd| jdd}d|_| t|	 | | 
t j |d| d
 W d Q R X | t|	 | |  t jtd| jdd}d|_| t|	 | d}x0t||D ]"}|d| d|d d	   qTW | t|	 | |  t td| j}| t|	 | x>t|D ]2}|d| d}| |d|d d	   qW |  d S )Nr8   F)r   r   r   zfoo%08dz%dr   r   r(   r   Tr   rB   r#   )r;   r<   r   r6   r   r)   r=   rJ   rP   rW   r   LargeZipFiler   rK   r   )r3   r   r   r&   Z	numfiles2r   r   r   r   r   test_too_many_files_append  sD     "z8AbstractTestZip64InSmallFiles.test_too_many_files_appendc             C   s$   | j t_| jt_tt tt d S )N)r   r;   r   r   r   r	   r   r   )r3   r   r   r   r     s    z&AbstractTestZip64InSmallFiles.tearDownN)r   r   r   r   r/   r5   re   rf   r   r   r   r   r   r   r   r   i  s   
9'r   c               @   s:   e Zd ZejZdd Zdd Zdd Zdd Z	d	d
 Z
dS )StoredTestZip64InSmallFilesc          	   C   s6   t j|d|dd}| t j|jtd W d Q R X d S )Nr8   F)r   zanother.name)r;   r<   r   r   r2   r   )r3   r    r6   r?   r   r   r   large_file_exception_test  s    z5StoredTestZip64InSmallFiles.large_file_exception_testc          	   C   s8   t j|d|dd}| t j|jd| j W d Q R X d S )Nr8   F)r   zanother.name)r;   r<   r   r   r=   r-   )r3   r    r6   r?   r   r   r   large_file_exception_test2  s    z6StoredTestZip64InSmallFiles.large_file_exception_test2c             C   s2   x,t | D ] }| |tj | |tj q
W d S )N)r!   r   r;   r   r   )r3   r    r   r   r   test_large_file_exception	  s    z5StoredTestZip64InSmallFiles.test_large_file_exceptionc          	   C   s^   t jtdt jdd}|td W d Q R X t tdt j}| | dg W d Q R X d S )Nr8   T)r   z	/absoluterB   r   )r;   r<   r   r   r2   r   rJ   rW   )r3   r?   r   r   r   r     s
    
z2StoredTestZip64InSmallFiles.test_absolute_arcnamesc          	   C   s   t jtddd}|d| j W d Q R X t jtddd}|d}|j}W d Q R X t jtddd}|d| j W d Q R X t jtddd}|d}| |j| W d Q R X d S )Nr8   T)r   r9   rB   r   Zstrfile2)r;   r<   r   r=   r-   rZ   extrarJ   )r3   r?   r   r   r   r   r   test_append  s    

z'StoredTestZip64InSmallFiles.test_appendN)r   r   r   r;   r   r6   r   r   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )DeflateTestZip64InSmallFilesN)r   r   r   r;   r   r6   r   r   r   r   r   $  s   r   c               @   s   e Zd ZejZdS )Bzip2TestZip64InSmallFilesN)r   r   r   r;   r   r6   r   r   r   r   r   )  s   r   c               @   s   e Zd ZejZdS )LzmaTestZip64InSmallFilesN)r   r   r   r;   r   r6   r   r   r   r   r   .  s   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )AbstractWriterTestsc             C   s   t t d S )N)r	   r   )r3   r   r   r   r   6  s    zAbstractWriterTests.tearDownc          	   C   st   d}t td| jV}|dd}|| |  | |j |  | |j | 	|
d| W d Q R X d S )Ns   contentr8   r   )r;   r<   r   r6   r1   r2   r   rS   r   rJ   rK   )r3   r-   r   r8   r   r   r   test_close_after_close9  s    
z*AbstractWriterTests.test_close_after_closec          	   C   sp   d}t td| jR}|dd}|| |  | |j | 	t
|jd | |d| W d Q R X d S )Ns   contentr8   r   r(   )r;   r<   r   r6   r1   r2   r   rS   r   r   r   rJ   rK   )r3   r-   r   r8   r   r   r   test_write_after_closeD  s    
z*AbstractWriterTests.test_write_after_closeN)r   r   r   r   r   r   r   r   r   r   r   4  s   r   c               @   s   e Zd ZejZdS )StoredWriterTestsN)r   r   r   r;   r   r6   r   r   r   r   r   N  s   r   c               @   s   e Zd ZejZdS )DeflateWriterTestsN)r   r   r   r;   r   r6   r   r   r   r   r   Q  s   r   c               @   s   e Zd ZejZdS )Bzip2WriterTestsN)r   r   r   r;   r   r6   r   r   r   r   r   U  s   r   c               @   s   e Zd ZejZdS )LzmaWriterTestsN)r   r   r   r;   r   r6   r   r   r   r   r   Y  s   r   c               @   sd   e Z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 )PyZipFileTestsc             C   s    |d |kr|  |d | d S )Noc)rQ   )r3   namerW   r   r   r   assertCompiledIn_  s    zPyZipFileTests.assertCompiledInc             C   s   t j|t jt jt jkds$| d t j|d}y"t |t jt j	B }t 
| W n tk
rr   | d Y nX t| d S )N)Zeffective_idsz/requires write access to the installed locationztest_zipfile.try)r   accessW_OKsupports_effective_idsZskipTestrs   r,   r1   O_WRONLYO_CREATr   	Exceptionr	   )r3   rs   rI   fdr   r   r   requiresWriteAccessc  s    

z"PyZipFileTests.requiresWriteAccessc          
   C   sR  |  tjt t }t|d}t}|dr|	tj
}tjd k	r^||	tj d|krttj|}n|d d }|| tj|}| ||  | ||  W d Q R X W d Q R X t x}t|d`}t}|dr|d d }||d ddtj|f }| ||  | ||  W d Q R X W d Q R X d S )Nr8   z.pyc__pycache__rw   Ztestpackagez%s/%s)r  r   rs   dirname__file__r   r;   	PyZipFileendswithrR   sepaltsepextend	importlibutilsource_from_cachewritepybasenameassertNotInrW   r   )r3   tr?   r^   Z
path_splitZbnr   r   r   test_write_pyfilep  s,    


$
z PyZipFileTests.test_write_pyfilec          
   C   sx   dd l }tj|j}| | t H}t|d0}|	| |
 }| d| | d| W d Q R X W d Q R X d S )Nr   r8   zemail/__init__.pyzemail/mime/text.py)emailr   rs   r  r  r  r   r;   r	  r  rW   r   )r3   r  
packagedirr  r?   ra   r   r   r   test_write_python_package  s    

z(PyZipFileTests.test_write_python_packagec             C   s  dd l }tj|j}| | t }t|d}t	 }|
| W d Q R X | }| d|k t	 }|j
|dd d W d Q R X | }| d|k dd }t	 *}| t |j
||d W d Q R X W d Q R X | }|rt| | d|k W d Q R X W d Q R X d S )	Nr   r8   SyntaxErrorc             S   s   dS )NFr   )Zwhateverr   r   r   <lambda>  r(   zCPyZipFileTests.test_write_filtered_python_package.<locals>.<lambda>)
filterfuncc             S   s   t j| d S )NZbad)r   rs   r  
startswith)rs   r   r   r   filter  s    zAPyZipFileTests.test_write_filtered_python_package.<locals>.filter)r   r   rs   r  r  r  r   r;   r	  r   r  rN   rS   assertWarnsUserWarningprint)r3   r   r  r  r?   Z	reportSIOZ	reportStrr  r   r   r   "test_write_filtered_python_package  s&    
"z1PyZipFileTests.test_write_filtered_python_packagec          
   C   s   dd l }tj|j}| | dr(dnd}d}t T}tj|d|d8}|	| |
 }| d| | | d| | W d Q R X W d Q R X d S )	Nr   FrG   z.pycr8   )optimizezemail/__init__zemail/mime/text)r  r   rs   r  r  r  r   r;   r	  r  rW   rQ   )r3   r  r  ZoptlevelZextr  r?   ra   r   r   r   test_write_with_optimization  s    

z+PyZipFileTests.test_write_with_optimizationc          
   C   s   t t ztt jtdd}|d W d Q R X tt jtdd}|d W d Q R X tt jtdd}|d W d Q R X t T}t	|d<}|
t | }| d| | d| | d| W d Q R X W d Q R X W d tt X d S )Nzmod1.pyr8   z
print(42)
zmod2.pyzprint(42 * 42)
zmod2.txtzbla bla bla
)r   mkdirr   r1   rs   r,   r2   r   r;   r	  r  rW   r   r  r
   )r3   r4   r  r?   ra   r   r   r   test_write_python_directory  s    

$z*PyZipFileTests.test_write_python_directoryc          
   C   s   t t ztt jtdd}|d W d Q R X tt jtdd}|d W d Q R X t P}t	|d8}|j
tdd d | }| d| | d| W d Q R X W d Q R X W d tt X d S )	Nzmod1.pyr8   z
print(42)
zmod2.pyzprint(42 * 42)
c             S   s   |  d S )Nzmod2.py)r
  )r^   r   r   r   r    s    zEPyZipFileTests.test_write_python_directory_filtered.<locals>.<lambda>)r  )r   r$  r   r1   rs   r,   r2   r   r;   r	  r  rW   r   r  r
   )r3   r4   r  r?   ra   r   r   r   $test_write_python_directory_filtered  s    
$z3PyZipFileTests.test_write_python_directory_filteredc             C   sf   t  V}t|d>}ttd}|d W d Q R X | t|jt t	t W d Q R X W d Q R X d S )Nr8   z!most definitely not a python file)
r   r;   r	  r1   r   r2   r   RuntimeErrorr  r	   )r3   r  r?   r    r   r   r   test_write_non_pyfile  s
    z$PyZipFileTests.test_write_non_pyfilec             C   s   t t ztt jtdd}|d W d Q R X t t}t	|d\}t
 }|t jtd W d Q R X | d|  | }| d| | d| W d Q R X W d Q R X W d tt X d S )Nzmod1.pyr8   zBad syntax in python file
r  zmod1.pyc)r   r$  r   r1   rs   r,   r2   r   r;   r	  r   r  rQ   rN   rW   r  r
   )r3   r4   r  r?   sra   r   r   r   test_write_pyfile_bad_syntax  s    
$z+PyZipFileTests.test_write_pyfile_bad_syntaxc          
   C   s   t t ztt jtdd}|d W d Q R X t F}t	|d.}|
ttd  | }| d| W d Q R X W d Q R X W d tt X d S )Nzmod1.pyr8   z
print(42)
)r   r$  r   r1   rs   r,   r2   r   r;   r	  r  rp   rq   rW   r   r
   )r3   r4   r  r?   ra   r   r   r   test_write_pathlike  s    
$z"PyZipFileTests.test_write_pathlikeN)r   r   r   r   r  r  r  r!  r#  r%  r&  r(  r*  r+  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	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zeejjdkddd Zeejjdkddd  Zd!d" Zd#S )$ExtractTestsc          	   C   s>   t tdt j$}xtD ]\}}||| qW W d Q R X d S )Nr8   )r;   r<   r   r   SMALL_TEST_DATAr=   )r3   r?   fpathfdatar   r   r   make_test_file!  s    zExtractTests.make_test_filec             C   s   t   |   ttd~}xvtD ]n\}}||}tj	t
 |}tj|}| || t|d}| | |  W d Q R X t| q$W W d Q R X W d Q R X d S )NrB   r   )r   r0  r;   r<   r   r-  extractr   rs   r,   getcwdnormpathrJ   r1   encoderK   r	   )r3   r?   r.  r/  writtenfilecorrectfiler    r   r   r   test_extract&  s    
zExtractTests.test_extractc             C   s   |    ttd}xtD ]z\}}|||}tj||}tj	|}| 
tj||||f t|d}| | |  W d Q R X t| qW W d Q R X tt d S )NrB   r   )r0  r;   r<   r   r-  r1  r   rs   r,   r3  rS   samefiler1   rJ   r4  rK   r	   )r3   targetr?   r.  r/  r5  r6  r    r   r   r   _test_extract_with_target9  s    z&ExtractTests._test_extract_with_targetc          	   C   s    t  }| | W d Q R X d S )N)r   r:  )r3   extdirr   r   r   test_extract_with_targetL  s    z%ExtractTests.test_extract_with_targetc          	   C   s&   t  }| t| W d Q R X d S )N)r   r:  rp   rq   )r3   r;  r   r   r   !test_extract_with_target_pathlikeP  s    z.ExtractTests.test_extract_with_target_pathlikec             C   s   t   |   ttdd}|  xTtD ]L\}}tj	t
 |}t|d}| | |  W d Q R X t| q,W W d Q R X W d Q R X d S )NrB   r   )r   r0  r;   r<   r   
extractallr-  r   rs   r,   r2  r1   rJ   r4  rK   r	   )r3   r?   r.  r/  outfiler    r   r   r   test_extract_allT  s    zExtractTests.test_extract_allc             C   s   |    ttdb}|| xPtD ]H\}}tj||}t	|d}| 
| |  W d Q R X t| q&W W d Q R X tt d S )NrB   r   )r0  r;   r<   r   r>  r-  r   rs   r,   r1   rJ   r4  rK   r	   )r3   r9  r?   r.  r/  r?  r    r   r   r   _test_extract_all_with_targeta  s    
z*ExtractTests._test_extract_all_with_targetc          	   C   s    t  }| | W d Q R X d S )N)r   rA  )r3   r;  r   r   r   test_extract_all_with_targeto  s    z)ExtractTests.test_extract_all_with_targetc          	   C   s&   t  }| t| W d Q R X d S )N)r   rA  rp   rq   )r3   r;  r   r   r   %test_extract_all_with_target_pathlikes  s    z2ExtractTests.test_extract_all_with_target_pathlikec          	   C   s<   |  tj| t|d}| | | W d Q R X d S )Nr   )rS   r   rs   isfiler1   rJ   rK   )r3   rI   r   r    r   r   r   
check_filew  s    zExtractTests.check_filec             C   sB   t jj}| |ddd | |ddd | |ddd d S )	Nz,,?,C:,foo,bar/z,z_,C_,foo,bar/zza\b,c<d>e|f"g?h*iza\b,c_d_e_f_g_h_iz../../foo../../ba..r/z	foo/ba..r)r;   r<   Z_sanitize_windows_namerJ   )r3   Zsanr   r   r   test_sanitize_windows_name|  s    z'ExtractTests.test_sanitize_windows_namec             C   s"   ddddddddg}|  | d S )	N)z
../foo/barzfoo/bar)z
foo/../barzfoo/bar)zfoo/../../barzfoo/bar)z
foo/bar/..zfoo/bar)z./../foo/barzfoo/bar)z/foo/barzfoo/bar)z/foo/../barzfoo/bar)z/foo/../../barzfoo/bar)_test_extract_hackers_arcnames)r3   Zcommon_hacknamesr   r   r   *test_extract_hackers_arcnames_common_cases  s    z7ExtractTests.test_extract_hackers_arcnames_common_cases\zRequires \ as path separator.c             C   s8   ddddddddd	d
dddddddddg}|  | d S )N)z
..\foo\barzfoo/bar)z..\/foo\/barzfoo/bar)zfoo/\..\/barzfoo/bar)zfoo\/../\barzfoo/bar)z	C:foo/barzfoo/bar)z
C:/foo/barzfoo/bar)zC://foo/barzfoo/bar)z
C:\foo\barzfoo/bar)z//conky/mountpoint/foo/barzfoo/bar)z\\conky\mountpoint\foo\barzfoo/bar)z///conky/mountpoint/foo/barzconky/mountpoint/foo/bar)z\\\conky\mountpoint\foo\barzconky/mountpoint/foo/bar)z//conky//mountpoint/foo/barzconky/mountpoint/foo/bar)z\\conky\\mountpoint\foo\barzconky/mountpoint/foo/bar)z//?/C:/foo/barzfoo/bar)z\\?\C:\foo\barzfoo/bar)zC:/../C:/foo/barz
C_/foo/bar)za:b\c<d>e|f"g?h*izb/c_d_e_f_g_h_i)z../../foo../../ba..rz	foo/ba..r)rI  )r3   Zwindows_hacknamesr   r   r   *test_extract_hackers_arcnames_windows_only  s(    z7ExtractTests.test_extract_hackers_arcnames_windows_onlyrG  zRequires / as path separator.c             C   s   dddg}|  | d S )N)z	//foo/barzfoo/bar)z../../foo../../ba..rzfoo../ba..r)z
foo/..\barz
foo/..\bar)rI  )r3   Zposix_hacknamesr   r   r   (test_extract_hackers_arcnames_posix_only  s    z5ExtractTests.test_extract_hackers_arcnames_posix_onlyc       
   
   C   s  x|D ]\}}d|   }ttdtj&}t }||_d|_||| W d Q R X |	t
jd}t
jddd}t
jj|f|d }ttd,}|||}	| j|	|d	||	|f d
 W d Q R X | || td ttd}|| W d Q R X | || td t
jjt
 f|d }ttd$}||}	| j|	|d| d
 W d Q R X | || t|dd  ttd}|  W d Q R X | || t|dd  tt qW d S )Ns   foobarr8   i  rG  r9  ZsubdirZsubsubrB   zextract %r: %r != %r)msgz
extract %rr   )r4  r;   r<   r   r   ZipInforI   r   r=   replacer   r  rs   r,   rR   r1  rJ   rE  r
   r>  r2  r	   )
r3   Z	hacknamesZarcnameZ	fixednamer   r?   r   Z
targetpathr6  r5  r   r   r   rI    sB    
z+ExtractTests._test_extract_hackers_arcnamesN)r   r   r   r0  r7  r:  r<  r=  r@  rA  rB  rC  rE  rH  rJ  unittestZskipIfr   rs   r  rL  rM  rI  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	d
 Ze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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGS )H
OtherTestsc             C   s   t tdt jJ}|dd | t |dd W d Q R X | | dgd  W d Q R X t td}|	 }d}x.|D ]&}|
|}|| 7 }W d Q R X qW | |dd	h d}x|D ]}|||7 }qW | |dd	h W d Q R X d S )
Nr8   r   r   barr   rB   r(   s   foobars   barfoo)r;   r<   r   r   r=   r  r  rJ   rW   rX   r1   rK   rQ   )r3   r?   rb   r-   rd   ry   r   r   r   test_open_via_zip_info  s      

z!OtherTests.test_open_via_zip_infoc          	   C   sN   t td8}x0dD ](}t |}| jdO  _||| qW W d Q R X d S )Nr8   Zabcdefghijklmnopr   )r;   r<   r   rO  Z	flag_bitsr=   )r3   Zorig_zipr-   r   r   r   r   -test_writestr_extended_local_header_issue1202  s
    

z8OtherTests.test_writestr_extended_local_header_issue1202c          	   C   s   t td2}x*tD ]"\}}||| | |jd qW W d Q R X | |jd t td}| |jd W d Q R X | |jd d S )Nr8   zzipfp is not openzzipfp is not closedrB   )r;   r<   r   r-  r=   ZassertIsNotNoner4   assertIsNone)r3   r?   r.  r/  r   r   r   
test_close  s    zOtherTests.test_closec          	   C   s   t td$}xtD ]\}}||| qW W d Q R X y$t td}t  W d Q R X W n$ t jk
r~   | |jd Y nX d S )Nr8   rB   zzipfp is not closed)r;   r<   r   r-  r=   
BadZipFilerV  r4   )r3   r?   r.  r/  Zzipfp2r   r   r   test_close_on_exception  s    z"OtherTests.test_close_on_exceptionc             C   s    d}|  ttjt|d d S )Nsd   PKx     !p@               xPKxx     !p@                           xPK      /        rB   )r   NotImplementedErrorr;   r<   r   r   )r3   r-   r   r   r   test_unsupported_version  s    
z#OtherTests.test_unsupported_versionc          	   C   sB   t d}t|&}x| D ]}||  qW W d Q R X d S )Nzzip_cp437_header.zip)r   r;   r<   rW   r1   r   )r3   r   r?   r   r   r   r   test_read_unicode_filenames  s    z&OtherTests.test_read_unicode_filenamesc          	   C   s   t td4}|dd |dd | | d jt W d Q R X t td.}| |j	d jd | |j	d jd W d Q R X d S )Nr8   zfoo.txtzTest for unicode filenameu   ö.txtr   rB   rG   )
r;   r<   r   r=   rr   rX   rI   rV   rJ   filelist)r3   zfr   r   r   test_write_unicode_filenames'  s     z'OtherTests.test_write_unicode_filenamesc          	   C   s   t t d}d}ttdtj}||| W d Q R X | t ttdtj W d Q R X ttd*}| |	 |g | |
|| W d Q R X d S )Nztestfile.txts#   hello, world. this is some content.xrB   )r	   r   r;   r<   r   r=   r   FileExistsErrorrJ   rW   rK   )r3   rI   r   r?   r   r   r   test_exclusive_create_zip_file1  s    z)OtherTests.test_exclusive_create_zip_filec          	   C   s   t jtrt t d}d}y(ttd}||| W d Q R X W n tk
rd   | 	d Y nX | 
t jt ttd}| ||| W d Q R X d S )Nztestfile.txts#   hello, world. this is some content.r   z1Could not append data to a non-existent zip file.rB   )r   rs   existsr   r	   r;   r<   r=   r   failrS   rJ   rK   )r3   rI   r   r^  r   r   r   (test_create_non_existent_file_for_append>  s    
z3OtherTests.test_create_non_existent_file_for_appendc          	   C   sJ   t td}|d W d Q R X ytt}W n tjk
rD   Y nX d S )Nr8   zthis is not a legal zip file
)r1   r   r2   r;   r<   rX  )r3   r4   r^  r   r   r   test_close_erroneous_fileP  s    z$OtherTests.test_close_erroneous_filec          	   C   s   t td}|d W d Q R X | tt | ttt t td}| t| W d Q R X t	 }|d | t| |
dd | t| d S )Nr8   zthis is not a legal zip file
r   s   this is not a legal zip file
r   )r1   r   r2   r   r;   
is_zipfilerp   rq   r   r   r   )r3   r4   r   r   r   test_is_zip_erroneous_file_  s    
z%OtherTests.test_is_zip_erroneous_filec          	   C   st   t  }tj|dd}|dd W d Q R X | }x8tt|D ](}t |d | }| tj	tj| qDW d S )Nr8   )r:   zfoo.txts   O, for a Muse of Fire!)
r   r   r;   r<   r=   rN   r)   rP   r   rX  )r3   r4   r   r   Nr   r   r   test_damaged_zipfileq  s    zOtherTests.test_damaged_zipfilec          	   C   s   t jtdd}|dd W d Q R X | t t ttd*}| t | |dd | }W d Q R X t	
 }|| | t | |dd | t | d S )Nr8   )r:   zfoo.txts   O, for a Muse of Fire!r   r   )r;   r<   r   r=   rS   rg  r1   r   rK   r   r   r2   )r3   r   r4   Zzip_contentsr   r   r   test_is_zip_valid_file  s    
z!OtherTests.test_is_zip_valid_filec             C   s   |  ttjt d S )N)r   r   r;   r<   r   )r3   r   r   r   %test_non_existent_file_raises_OSError  s    z0OtherTests.test_non_existent_file_raises_OSErrorc          	   C   sZ   t td}|  | tjtjt t td}|d W d Q R X | tjtjt d S )Nr8   z
short file)r1   r   r   r   r;   rX  r<   r2   )r3   r    r4   r   r   r   !test_empty_file_raises_BadZipFile  s    
z,OtherTests.test_empty_file_raises_BadZipFilec          	   C   s   t  }tj|dd}|dd W d Q R X | t|jd | t|jd | t|j	 | t|jdd tt
d}|d W d Q R X | t|jt
 d S )Nr8   )r:   zfoo.txtzO, for a Muse of Fire!z	bogus.txtr   zzipfile test data)r   r   r;   r<   r=   r   r   rK   r1   r[   r   r2   )r3   r-   r   r    r   r   r   !test_closed_zip_raises_ValueError  s    z,OtherTests.test_closed_zip_raises_ValueErrorc             C   s   |  ttjtd d S )Nq)r   r   r;   r<   r   )r3   r   r   r   test_bad_constructor_mode  s    z$OtherTests.test_bad_constructor_modec          	   C   s   t jtdd}|dd W d Q R X t jtddF}|d | t|jdd | t|jdd | t|jdd W d Q R X d S )	Nr8   )r:   zfoo.txtzO, for a Muse of Fire!rB   ro  UZrU)r;   r<   r   r=   rK   r   r   r1   )r3   r   r   r   r   test_bad_open_mode  s    
zOtherTests.test_bad_open_modec          
   C   st   t jtdd\}|dd |d:}x"ttD ]}| |dd q2W | | d W d Q R X W d Q R X d S )Nr8   )r:   zfoo.txtzO, for a Muse of Fire!r   r(   s   O, for a Muse of Fire!)	r;   r<   r   r=   r1   r)   r*   rJ   rK   )r3   r   r    r&   r   r   r   
test_read0  s    zOtherTests.test_read0c          	   C   s0   t jtdd}| t|jdd W d Q R X d S )Nr8   )r:   zfoo.txtrB   )r;   r<   r   r   KeyErrorr1   )r3   r   r   r   r   test_open_non_existent_item  s    z&OtherTests.test_open_non_existent_itemc             C   s   |  ttjtdd d S )Nr8   rw   )r   rZ  r;   r<   r   )r3   r   r   r   test_bad_compression_mode  s    z$OtherTests.test_bad_compression_modec          	   C   s6   d}t t|d}| t|jd W d Q R X d S )Nsf   PK.    C@              x PK..    C@                          xPK      /   !     rB   r`  )r;   r<   r   r   r   rZ  r1   )r3   r-   r   r   r   r   test_unsupported_compression  s    z'OtherTests.test_unsupported_compressionc          	   C   s<   t jtdd$}|dd | | dg W d Q R X d S )Nr8   )r:   zfoo.txt qqqs   O, for a Muse of Fire!zfoo.txt)r;   r<   r   r=   rJ   rW   )r3   r   r   r   r   test_null_byte_in_filename  s    z%OtherTests.test_null_byte_in_filenamec             C   s<   |  tjd |  tjd |  tjd |  tjd d S )N   .   8   r   )rJ   r;   ZsizeEndCentDirZsizeCentralDirZsizeEndCentDir64ZsizeEndCentDir64Locator)r3   r   r   r   test_struct_sizes  s    zOtherTests.test_struct_sizesc          
   C   s  t jtdd }| |jd |dd W d Q R X t jtdd}| |jd W d Q R X d}t jtdd}||_|dd W d Q R X t jtdd}| |j| W d Q R X dd	d
 tdD }|d}t jtdd}||_|dd W d Q R X t jtdd}| |j| W d Q R X t jtdd2}| 	t
 |d |_W d Q R X |dd W d Q R X t jtdd}| |j| W d Q R X t jtdd}d|_|dd W d Q R X t jtdd}d|_W d Q R X t jtdd}| |jd W d Q R X t jtdd}d|_|dd W d Q R X t jtdd}d|_W d Q R X t jtdd}| |jd W d Q R X d S )Nr8   )r:   r(   zfoo.txtzO, for a Muse of Fire!rB   s9   Bravely taking to his feet, he beat a very brave retreat. c             S   s   g | ]}d |d d  qS )z%dr   r}   r   )r%   r&   r   r   r   r'     s    z,OtherTests.test_comments.<locals>.<listcomp>i  r#   s   oopss   original commentr   s   an updated comments   original comment that's longers   shorter comment)r;   r<   r   rJ   r   r=   r,   r)   r4  r  r  )r3   r   Zzipfrr   Zcomment2r   r   r   test_comments  sL    
zOtherTests.test_commentsc          
   C   sH   t tdt j.}|dd | t d|_W d Q R X W d Q R X d S )Nr8   zfoo.txtzO, for a Muse of Fire!zthis is an error)r;   r<   r   r   r=   r   	TypeErrorr   )r3   r   r   r   r   test_unicode_comment3  s    zOtherTests.test_unicode_commentc          	   C   sX   t tdt j}| |j d|_W d Q R X t td}| |jd W d Q R X d S )Nr   s   this is a commentrB   )r;   r<   r   r   r   r]  r   rJ   )r3   r   r   r   r   $test_change_comment_in_empty_archive9  s
    z/OtherTests.test_change_comment_in_empty_archivec          	   C   s   t tdt j}|dd W d Q R X t tdt j}| |j d|_W d Q R X t td}| |jd W d Q R X d S )Nr8   zfoo.txtzO, for a Muse of Fire!r   s   this is a commentrB   )	r;   r<   r   r   r=   rS   r]  r   rJ   )r3   r   r   r   r   'test_change_comment_in_nonempty_archive@  s    z2OtherTests.test_change_comment_in_nonempty_archivec             C   s   t jtdd}|  yt jtdd}W n  t jk
rH   | d Y nX t jtdd}|  yt jtdd}W n   | d Y nX d S )Nr8   )r:   rB   z+Unable to create empty ZIP file in 'w' moder   z+Unable to create empty ZIP file in 'a' mode)r;   r<   r   r   rX  rd  )r3   r   r   r   r   test_empty_zipfileI  s    zOtherTests.test_empty_zipfilec             C   s*   t td}|  | tjtjtd d S )Nr8   rB   )r1   r   r   r   r;   rX  r<   )r3   r    r   r   r   test_open_empty_fileZ  s    
zOtherTests.test_open_empty_filec             C   s   |  ttjdd d S )NZ	seventies)i  rG   rG   r   r   r   )r   r   r;   rO  )r3   r   r   r   test_create_zipinfo_before_1980b  s    z*OtherTests.test_create_zipinfo_before_1980c          	   C   s4   d}t t|d}| |  W d Q R X d S )Nsn   PK     @        abc   APK     @                    abc  PK      3   %     rB   )r;   r<   r   r   rV  r[   )r3   rz   r   r   r   r   #test_zipfile_with_short_extra_fieldf  s    z.OtherTests.test_zipfile_with_short_extra_fieldc             C   s  d}d}d}t tdt j}|jddd}|| W d Q R X |jddd}| t |jddd W d Q R X | t |jdd	d W d Q R X | t |d
d W d Q R X | t |t	d W d Q R X | t |
  W d Q R X || W d Q R X |jddd}|| W d Q R X W d Q R X t td	R}| |d| | |d| | |d| | | dddg W d Q R X d S )Ns"   It's fun to charter an accountant!s    And sail the wide accountant seas#   To find, explore the funds offshorer8   r   )r:   rS  ZhandlerB   rV   ZabcderC   Zbaz)r;   r<   r   r   r1   r2   r   r   r=   r  r   rJ   rK   rW   )r3   Zmsg1Zmsg2Zmsg3r   Zw2w1r   r   r   test_open_conflicting_handlesu  s2    z(OtherTests.test_open_conflicting_handlesc          
   C   s:  d}| d}ttd}|d| W d Q R X ttd}|dd}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	|d|||d   |dtj | 	|
 t| |dtj | 	|
 d W d Q R X W d Q R X t }tj|dd}|d| W d Q R X tj|dd}|dd}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	|d|||d   |dtj | 	|
 t| |dtj | 	|
 d W d Q R X W d Q R X d S )	Ns   Where's Bruce?s   Brucer8   zfoo.txtrB   r   rD   )r:   )findr;   r<   r   r=   r1   r   r   SEEK_SETrJ   r   SEEK_CURrK   SEEK_ENDrP   r   r   )r3   ZtxtZblocr   r4   r-   r   r   r   test_seek_tell  sB    
$zOtherTests.test_seek_tellc             C   s   t t t t d S )N)r	   r   r   )r3   r   r   r   r     s    zOtherTests.tearDownN)'r   r   r   rT  rU  rW  rY  r[  r   r\  r_  rb  re  rf  rh  rj  rk  rl  rm  rn  rp  rr  rs  ru  rv  rw  rx  r|  r~  r  r  r  r  r  r  r  r  r  r   r   r   r   r   rR    sF   
	7	&rR  c               @   s   e Zd Zdd Zdd ZdS )AbstractBadCrcTestsc          	   C   s:   | j }tjt|dd}| d|  W d Q R X d S )NrB   )r:   afile)zip_with_bad_crcr;   r<   r   r   rJ   r[   )r3   rz   r   r   r   r   test_testzip_with_bad_crc  s    z-AbstractBadCrcTests.test_testzip_with_bad_crcc             C   s   | j }tjt|dd}| tj|jd W d Q R X tjt|dd.}|dd}| tj|j W d Q R X W d Q R X tjt|ddL}|dd4}d|_	| tj x|drqW W d Q R X W d Q R X W d Q R X d S )NrB   )r:   r  r   )
r  r;   r<   r   r   r   rX  rK   r1   ZMIN_READ_SIZE)r3   rz   r   Zcorrupt_filer   r   r   test_read_with_bad_crc  s    $z*AbstractBadCrcTests.test_read_with_bad_crcN)r   r   r   r  r  r   r   r   r   r    s   r  c               @   s   e Zd ZejZdZdS )StoredBadCrcTestssx   PK      ;:r         afilehello,AworldPK      ;:r                     afilePK      3   /     N)r   r   r   r;   r   r6   r  r   r   r   r   r    s   r  c               @   s   e Zd ZejZdZdS )DeflateBadCrcTestss|   PK    n}=FAKE   n      afileHW(/Iɠ= PK    n}=FAKE   n                  afilePK      3   3     N)r   r   r   r;   r   r6   r  r   r   r   r   r    s   r  c               @   s   e Zd ZejZdZdS )Bzip2BadCrcTestss   PK   nu=FAKE8   n      afileBZh91AY&SYԨ  @ D   P!u5:"(HjTe?PK   nu=FAKE8   n                 afilePK      3   [     N)r   r   r   r;   r   r6   r  r   r   r   r   r    s   r  c               @   s   e Zd ZejZdZdS )LzmaBadCrcTestss   PK   nu=FAKE   n      afile	 ]    4I:3`	q!.8 PK   nu=FAKE   n                 afilePK      3   >     N)r   r   r   r;   r   r6   r  r   r   r   r   r    s   r  c               @   sP   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dd Z
edd Zdd ZdS )DecryptionTestss   PK    ni.#y?&         test.txtgly|-=yrZ}Lg|䋺)lPK     ni.#y?&                    test.txtPK      6   L     s   PK 	  }38xu        zeroUT	 ֋GڋGUx <MaXY&{oEםPKxu      PK 	  }38xu                    zeroUT ֋GUx  PK      ?   [     s   zipfile.py encryption tests                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   c          	   C   sd   t td}|| j W d Q R X ttd| _t td}|| j W d Q R X ttd| _	d S )Nr0   rB   )
r1   r   r2   r-   r;   r<   r   r   data2zip2)r3   r4   r   r   r   r5   .  s    zDecryptionTests.setUpc             C   s,   | j   tt | j  tt d S )N)r   r   r   r	   r   r  r   )r3   r   r   r   r   6  s    


zDecryptionTests.tearDownc             C   s(   |  t| jjd |  t| jjd d S )Nztest.txtzero)r   r'  r   rK   r  )r3   r   r   r   test_no_password<  s    z DecryptionTests.test_no_passwordc             C   s@   | j d | t| j jd | jd | t| jjd d S )Ns   perlztest.txtr  )r   setpasswordr   r'  rK   r  )r3   r   r   r   test_bad_passwordB  s    z!DecryptionTests.test_bad_passwordc             C   sH   | j d | | j d| j | jd | | jd| j d S )Ns   pythonztest.txts   12345r  )r   r  rJ   rK   plainr  plain2)r3   r   r   r   test_good_passwordH  s    z"DecryptionTests.test_good_passwordc             C   sV   |  t| jjd |  t| jjdd | j t| jjddd | j t| jjddd d S )NZunicodeztest.txtZpython)pwd)r   r  r   r  rK   r1   r1  )r3   r   r   r   test_unicode_passwordO  s    z%DecryptionTests.test_unicode_passwordN)r   r   r   r-   r  r  r  r5   r   r  r  r   r  r  r   r   r   r   r    s   	r  c               @   s`   e Zd Ze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 )"AbstractTestsWithRandomBinaryFilesc             C   s6   t ddd t dd }ddd t|D | _d S )N   @   i   rG   r(   c             s   s&   | ]}t d t tdd V  qdS )z<fii  N)structpackr   r   )r%   r&   r   r   r   r   Y  s   z@AbstractTestsWithRandomBinaryFiles.setUpClass.<locals>.<genexpr>)r   r,   r)   r-   )r.   Z	datacountr   r   r   r/   V  s    
z-AbstractTestsWithRandomBinaryFiles.setUpClassc          	   C   s&   t td}|| j W d Q R X d S )Nr0   )r1   r   r2   r-   )r3   r4   r   r   r   r5   \  s    z(AbstractTestsWithRandomBinaryFiles.setUpc             C   s   t t t t d S )N)r	   r   r   )r3   r   r   r   r   a  s    z+AbstractTestsWithRandomBinaryFiles.tearDownc          	   C   s6   t |d|}|td |tt W d Q R X d S )Nr8   zanother.name)r;   r<   r2   r   )r3   r    r6   r?   r   r   r   rA   e  s    z4AbstractTestsWithRandomBinaryFiles.make_test_archivec          	   C   sl   |  || t|d|H}|t}| t|t| j | || j | |d| j W d Q R X d S )NrB   zanother.name)rA   r;   r<   rK   r   rJ   rP   r-   )r3   r    r6   r?   testdatar   r   r   re   k  s    
z+AbstractTestsWithRandomBinaryFiles.zip_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   re   r6   )r3   r    r   r   r   	test_readu  s    z,AbstractTestsWithRandomBinaryFiles.test_readc          
   C   s   |  || t|d|}g }|t&}x|d}|s>P || q.W W d Q R X g }|d&}x|d}|sxP || qhW W d Q R X d|}	| t	|	t	| j
 | |	| j
 d|}
| t	|
t	| j
 | |
| j
 W d Q R X d S )NrB   rg   zanother.namer(   )rA   r;   r<   r1   r   rK   rh   r,   rJ   rP   r-   )r3   r    r6   r?   ri   rj   rk   rl   rm   Z	testdata1Z	testdata2r   r   r   rn   y  s,    



z0AbstractTestsWithRandomBinaryFiles.zip_open_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   rn   r6   )r3   r    r   r   r   ro     s    z,AbstractTestsWithRandomBinaryFiles.test_openc          
   C   s   |  || t|d|t}g }|t,}x$|tdd}|sDP || q.W W d Q R X d|}| 	t
|t
| j | 	|| j W d Q R X d S )NrB   rG   i   r(   )rA   r;   r<   r1   r   rK   r   rh   r,   rJ   rP   r-   )r3   r    r6   r?   ri   rj   rk   r  r   r   r   ru     s    
z7AbstractTestsWithRandomBinaryFiles.zip_random_open_testc             C   s$   xt | D ]}| || j q
W d S )N)r!   ru   r6   )r3   r    r   r   r   rv     s    z3AbstractTestsWithRandomBinaryFiles.test_random_openN)r   r   r   r   r/   r5   r   rA   re   r  rn   ro   ru   rv   r   r   r   r   r  U  s   
r  c               @   s   e Zd ZejZdS ) StoredTestsWithRandomBinaryFilesN)r   r   r   r;   r   r6   r   r   r   r   r    s   r  c               @   s   e Zd ZejZdS )!DeflateTestsWithRandomBinaryFilesN)r   r   r   r;   r   r6   r   r   r   r   r    s   r  c               @   s   e Zd ZejZdS )Bzip2TestsWithRandomBinaryFilesN)r   r   r   r;   r   r6   r   r   r   r   r    s   r  c               @   s   e Zd ZejZdS )LzmaTestsWithRandomBinaryFilesN)r   r   r   r;   r   r6   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 )
Tellablec             C   s   || _ d| _d S )Nr   )r4   offset)r3   r4   r   r   r   __init__  s    zTellable.__init__c             C   s   | j |}|  j|7  _|S )N)r4   r2   r  )r3   r-   nr   r   r   r2     s    zTellable.writec             C   s   | j S )N)r  )r3   r   r   r   r     s    zTellable.tellc             C   s   | j   d S )N)r4   flush)r3   r   r   r   r    s    zTellable.flushN)r   r   r   r  r2   r   r  r   r   r   r   r    s   r  c               @   s$   e Zd Zdd Zdd Zdd ZdS )
Unseekablec             C   s
   || _ d S )N)r4   )r3   r4   r   r   r   r    s    zUnseekable.__init__c             C   s   | j |S )N)r4   r2   )r3   r-   r   r   r   r2     s    zUnseekable.writec             C   s   | j   d S )N)r4   r  )r3   r   r   r   r    s    zUnseekable.flushN)r   r   r   r  r2   r  r   r   r   r   r    s   r  c               @   s$   e Zd Zdd Zdd Zdd ZdS )UnseekableTestsc             C   s  x dd t tfD ]}| j|d t }|d t|}t||dtj	}|
dd |
dd	 W d Q R X | | d d
 d tj|ddR}|d}| | d W d Q R X |d}| | d	 W d Q R X W d Q R X W d Q R X qW d S )Nc             S   s   | S )Nr   )r    r   r   r   r    r(   z/UnseekableTests.test_writestr.<locals>.<lambda>)wrappers   abcr8   oness   111twoss   222rD   s   abcPKrB   )r:   )r  r  subTestr   r   r2   BufferedWriterr;   r<   r   r=   rJ   rN   r1   rK   )r3   r  r    bfr?   r   zopenr   r   r   test_writestr  s    

zUnseekableTests.test_writestrc             C   sX  xPdd t tfD ]<}| j|d$ t }|d t|}t||dtj	j}| 
tt ttd}|d W d Q R X |td ttd}|d	 W d Q R X |td
 W d Q R X | | d d d tj|ddR}|d}| | d W d Q R X |d
}| | d	 W d Q R X W d Q R X W d Q R X qW d S )Nc             S   s   | S )Nr   )r    r   r   r   r    r(   z,UnseekableTests.test_write.<locals>.<lambda>)r  s   abcr8   r0   s   111r  s   222r  rD   s   abcPKrB   )r:   )r  r  r  r   r   r2   r  r;   r<   r   
addCleanupr	   r   r1   rJ   rN   rK   )r3   r  r    r  r?   f2r   r  r   r   r   
test_write  s&    

zUnseekableTests.test_writec             C   s  x dd t tfD ]}| j|d t }|d t|}t||dtj	J}|
dd}|d W d Q R X |
dd}|d	 W d Q R X W d Q R X | | d d
 d t|*}| |dd | |dd	 W d Q R X W d Q R X qW d S )Nc             S   s   | S )Nr   )r    r   r   r   r    r(   z1UnseekableTests.test_open_write.<locals>.<lambda>)r  s   abcr8   r  s   111r  s   222rD   s   abcPK)r  r  r  r   r   r2   r  r;   r<   r   r1   rJ   rN   rK   )r3   r  r    r  r   r  r   r   r   test_open_write  s    

zUnseekableTests.test_open_writeN)r   r   r   r  r  r  r   r   r   r   r    s   r  c               @   sh   e Zd Ze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 )TestsWithMultipleOpensc             C   s    dt d | _dt d | _d S )Ns   111i'  s   222)r   data1r  )r.   r   r   r   r/     s    z!TestsWithMultipleOpens.setUpClassc          	   C   s<   t |dt j"}|d| j |d| j W d Q R X d S )Nr8   r  r  )r;   r<   r   r=   r  r  )r3   r    r?   r   r   r   rA     s    z(TestsWithMultipleOpens.make_test_archivec             C   s   xt | D ]}| | tj|ddx}|dH}|d2}|d}|d}|| 7 }|| 7 }W d Q R X W d Q R X | || | || j W d Q R X q
W d S )NrB   )r:   r  i  )r!   rA   r;   r<   r1   rK   rJ   r  )r3   r    r   zopen1zopen2r  r  r   r   r   test_same_file#  s    


 z%TestsWithMultipleOpens.test_same_filec             C   s   xt | D ]}| | tj|ddz}|dH}|d2}|d}|d}|| 7 }|| 7 }W d Q R X W d Q R X | || j | || j W d Q R X q
W d S )NrB   )r:   r  r  i  )	r!   rA   r;   r<   r1   rK   rJ   r  r  )r3   r    r   r  r  r  r  r   r   r   test_different_file1  s    


 z*TestsWithMultipleOpens.test_different_filec             C   s   xt | D ]}| | tj|ddz}|dH}|d}|d(}|d}|| 7 }|| 7 }W d Q R X W d Q R X | || j | || j W d Q R X q
W d S )NrB   )r:   r  i  r  )	r!   rA   r;   r<   r1   rK   rJ   r  r  )r3   r    r   r  r  r  r  r   r   r   test_interleaved?  s    


 z'TestsWithMultipleOpens.test_interleavedc             C   s   xt | D ]}| | t j}t|d&}||d}||d}W d Q R X |d}|d}|| 7 }|| 7 }W d Q R X | 	|| j
 | 	|| j q
W d S )NrB   r  r  i  )r!   rA   
contextlib	ExitStackr;   r<   enter_contextr1   rK   rJ   r  r  )r3   r    stackr   r  r  r  r  r   r   r   test_read_after_closeN  s    



z,TestsWithMultipleOpens.test_read_after_closec             C   s   xt | D ]}t|dtjB}|d| j |d| j |d}|d}W d Q R X W d Q R X | 	|| jd d  t|d}|d}|d}W d Q R X | 	|| j | 	|| j q
W d S )Nr8   r  r  i  rB   )
r!   r;   r<   r   r=   r  r  r1   rK   rJ   )r3   r    r   r  r  r  r   r   r   test_read_after_write\  s    
z,TestsWithMultipleOpens.test_read_after_writec             C   s   xt | D ]}t|dtjB}|d| j |d}|d |d| j W d Q R X W d Q R X t|d}|d}|d}W d Q R X | 	|| j | 	|| j q
W d S )Nr8   r  i  r  rB   )
r!   r;   r<   r   r=   r  r1   rK   r  rJ   )r3   r    r   r  r  r  r   r   r   test_write_after_readj  s    
"
z,TestsWithMultipleOpens.test_write_after_readc             C   s   |  t tjtdd8}x0tdD ]$}|d |d}W d Q R X q$W W d Q R X ttj}| 	|
 d W d Q R X d S )NrB   )r:   r   r  )rA   r   r;   r<   r)   rK   r1   r   devnullZ
assertLessfileno)r3   r   r`  r  r    r   r   r   test_many_opensw  s    

z&TestsWithMultipleOpens.test_many_opensc             C   s   t tdt j}|d| j W d Q R X t tdt jX}|dd@}|d}|dd}|| j	 W d Q R X || 7 }W d Q R X W d Q R X | 
|| j t t}| 
|d| j	 W d Q R X d S )Nr8   r  r   rB   i  r  )r;   r<   r   r   r=   r  r1   rK   r2   r  rJ   )r3   r   Zr1r  r  r   r   r   test_write_while_reading  s    
 z/TestsWithMultipleOpens.test_write_while_readingc             C   s   t t d S )N)r	   r   )r3   r   r   r   r     s    zTestsWithMultipleOpens.tearDownN)r   r   r   r   r/   rA   r  r  r  r  r  r  r  r  r   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	d
 Zdd ZdS )TestWithDirectoryc             C   s   t t d S )N)r   r$  r   )r3   r   r   r   r5     s    zTestWithDirectory.setUpc          
   C   s   t td}|t W d Q R X | tjtj	td | tjtj	tdd | tj
tj	tddd d S )Nz
zipdir.zipr   br   )r;   r<   r   r>  r   rS   r   rs   isdirr,   rc  )r3   r   r   r   r   test_extract_dir  s
    z"TestWithDirectory.test_extract_dirc             C   s    t t jtd |   d S )Nr   )r   r$  rs   r,   r   r  )r3   r   r   r   test_bug_6050  s    zTestWithDirectory.test_bug_6050c          	   C   s  t jtd}t | t |jd@ }tt	d|}|
| |jd }| |jd | |j|d> dB  |
|d |jd }| |jd	 | |j|d> dB  W d Q R X tt	d
}|jd }| |jd | |j|d> dB  |jd }| |jd	 | |j|d> dB  t jtd}t | || | t jt j|d | tt |d W d Q R X d S )Nr`  i  r8   r   z/x/r  yrG   zy/rB   r9  r   )r   rs   r,   r   r$  statst_moder;   r<   r   r2   r]  rS   rI   r
  rJ   r   r>  r  rP   listdir)r3   dirpathr:   r   r   r9  r   r   r   test_write_dir  s0    



 



z TestWithDirectory.test_write_dirc          	   C   s   t t jtd ttd8}|dd |j	d }| 
|jd | 
|jd W d Q R X ttd}|j	d }| |jd | 
|jd t jtd}t | || | t jt j|d | 
t |dg W d Q R X d S )	Nr`  r8   zx/r(   r   i ArB   r9  )r   r$  rs   r,   r   r;   r<   r   r=   r]  rJ   rI   r   rS   r
  r>  r  r  )r3   r   r   r9  r   r   r   test_writestr_dir  s    



z#TestWithDirectory.test_writestr_dirc             C   s    t t tjtrtt d S )N)r
   r   r   rs   rc  r   r	   )r3   r   r   r   r     s    zTestWithDirectory.tearDownN)	r   r   r   r5   r  r  r  r  r   r   r   r   r   r    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 )ZipInfoTestsc             C   sH   t jt}| t|jd | |	  | |j
tjt d S )Nztest_zipfile.py)r;   rO  	from_filer  rJ   	posixpathr  rI   r   is_dirrY   r   rs   getsize)r3   zir   r   r   test_from_file  s    zZipInfoTests.test_from_filec             C   sN   t jtt}| t|j	d | 
|  | |jtjt d S )Nztest_zipfile.py)r;   rO  r  rp   rq   r  rJ   r  r  rI   r   r  rY   r   rs   r  )r3   r  r   r   r   test_from_file_pathlike  s    z$ZipInfoTests.test_from_file_pathlikec             C   sP   t jttd}| t|j	d | 
|  | |jtjt d S )Nr   )r;   rO  r  r   fsencoder  rJ   r  r  rI   r   r  rY   rs   r  )r3   r  r   r   r   test_from_file_bytes  s    z!ZipInfoTests.test_from_file_bytesc          	   C   sd   t tdP}tj| d}| t|j	d | 
|  | |jtjt W d Q R X d S )Nr   r   )r1   r  r;   rO  r  r  rJ   r  r  rI   r   r  rY   r   rs   r  )r3   r    r  r   r   r   test_from_file_fileno  s
    z"ZipInfoTests.test_from_file_filenoc             C   s`   t jt jt}tj|d}| |j	d | 
|  | |jtj | |jd d S )NZstdlib_testszstdlib_tests/r   )r   rs   r  abspathr  r;   rO  r  rJ   rI   rS   r  r   r   rY   )r3   r  r  r   r   r   test_from_dir  s    zZipInfoTests.test_from_dirN)r   r   r   r  r  r  r  r  r   r   r   r   r    s
   r  c               @   sH   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zedd Z	dd Z
dS )CommandLineTestc             O   s(   t jd||\}}}|tj dS )N-mr;   r   )r  r;   )r   Zassert_python_okrP  r   linesepr4  )r3   argsr>   rcouterrr   r   r   
zipfilecmd  s    
zCommandLineTest.zipfilecmdc             G   s   t jd| S )N-mr;   )r  r;   )r   Zassert_python_failure)r3   r  r   r   r   zipfilecmd_failure  s    z"CommandLineTest.zipfilecmd_failurec             C   s|   |   \}}}| |d | d|  | d|  | d|  |  dd\}}}| |d | | d d S )Nr(   s   usages   errors   requiredz-lr}  )r  rJ   rQ   lowerZassertNotEqualstrip)r3   r  r  r  r   r   r   test_bad_use	  s    zCommandLineTest.test_bad_usec             C   s\   t d}x(dD ] }| ||}| | d qW t d}| d|\}}}| |d d S )Nz
zipdir.zip)z-tz--tests   Done testingztesttar.tarz-tr(   )r   r  rJ   rstripr  )r3   zip_nameoptr  r  r  r   r   r   test_test_command	  s    
z!CommandLineTest.test_test_commandc          	   C   sp   t d}t }t|d}|| W d Q R X | dd}x(dD ] }| j||dd}| 	|| qHW d S )Nz
zipdir.ziprB   r#   backslashreplace)z-lz--listzascii:backslashreplace)ZPYTHONIOENCODING)
r   r   rL   r;   r<   rM   rN   r4  r  rJ   )r3   r  r  ZtfZexpectedr  r  r   r   r   test_list_command	  s    
z!CommandLineTest.test_list_commandc          
   C   s  |  tt ttd}|d W d Q R X tt |  tt ttj	
tdd}|d W d Q R X ttg}ttd td g}xdD ]}zt| j|tf| }| |d ttB}| | | | ||d	 d
 | ||d d W d Q R X W d tt X qW d S )Nr8   ztest 1zfile.txtztest 2rG  z	/file.txt)z-cz--creater(   r   s   test 1r   s   test 2)r  r	   r   r1   r2   r   r$  	TESTFNDIRr
   rs   r,   r  r   rJ   r;   r<   rW   rK   )r3   r    filesrW   r  r  r^  r   r   r   test_create_command 	  s$    

$z#CommandLineTest.test_create_commandc       	      C   s   t d}xdD ]}t }| |||}| |d t|}x| D ]x}tj	||j
dtj}| r| tj| qJ| tj| t|d}| | || W d Q R X qJW W d Q R X W d Q R X qW d S )Nz
zipdir.zip)z-ez	--extractr(   rG  r   )r   r   r  rJ   r;   r<   rX   r   rs   r,   rI   rP  r  r  rS   r  rD  r1   rK   )	r3   r  r  r;  r  r^  r  rs   r    r   r   r   test_extract_command6	  s    
z$CommandLineTest.test_extract_commandN)r   r   r   r  r  r  r  r  r   r  r  r   r   r   r   r    s   
	r  __main__)Jr  r   r   importlib.utilr  rp   r  rT   r  r;   rQ  Ztempfiler   r   r   r   Ztest.supportr   r   r   r	   r
   r   r   r   r   r   r   r   r  r*   ZDATAFILES_DIRr-  r   r!   r"   ZTestCaser   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,  rR  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   mainr   r   r   r   <module>   s   0	    


 (


 B A   aA[



7
}A#O
