Your IP : 216.73.217.129


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_zipfile.cpython-35.opt-2.pyc



���]bT�@sSddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZmZddlmZddlmZmZmZmZmZmZmZmZmZmZedZedZdZd	Zd
dfdd
fddfddfgZdd�Z dd�Z!dd�Z"Gdd�d�Z#Gdd�de#e	j$�Z%eGdd�de#e	j$��Z&eGdd�de#e	j$��Z'eGd d!�d!e#e	j$��Z(Gd"d#�d#�Z)Gd$d%�d%e)e	j$�Z*eGd&d'�d'e)e	j$��Z+eGd(d)�d)e)e	j$��Z,eGd*d+�d+e)e	j$��Z-Gd,d-�d-e	j$�Z.Gd.d/�d/e	j$�Z/Gd0d1�d1e	j$�Z0Gd2d3�d3�Z1Gd4d5�d5e1e	j$�Z2eGd6d7�d7e1e	j$��Z3eGd8d9�d9e1e	j$��Z4eGd:d;�d;e1e	j$��Z5Gd<d=�d=e	j$�Z6Gd>d?�d?�Z7Gd@dA�dAe7e	j$�Z8eGdBdC�dCe7e	j$��Z9eGdDdE�dEe7e	j$��Z:eGdFdG�dGe7e	j$��Z;GdHdI�dI�Z<GdJdK�dK�Z=GdLdM�dMe	j$�Z>eGdNdO�dOe	j$��Z?GdPdQ�dQe	j$�Z@GdRdS�dS�ZAGdTdU�dUeAe	j$�ZBeGdVdW�dWeAe	j$��ZCeGdXdY�dYeAe	j$��ZDeGdZd[�d[eAe	j$��ZEGd\d]�d]e	j$�ZFeGd^krOe	jH�dS)_�N)�
TemporaryFile)�randint�random�getrandbits)�
script_helper)
�TESTFN�findfile�unlink�rmtree�temp_dir�
requires_zlib�requires_bz2�
requires_lzma�captured_stdout�check_warnings�2�di�Zzipfile_datafilesZ	_ziptest1Z
1q2w3e4r5tzziptest2dir/_ziptest2Z
qawsedrftgz!ziptest2dir/ziptest3dir/_ziptest3Z
azsxdcfvgbz-ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3Z
6y7u8i9o0pcCstd|�j|d�S)N��little)r�to_bytes)�size�r�1/opt/alt/python35/lib64/python3.5/test_zipfile.py�getrandbytessrccs\tVt��}|V|j|j�WdQRXtj��}|V|j|j�WdQRXdS)N)�TESTFN2r�assertFalse�closed�io�BytesIO)�test�frrr�	get_files!sr!c
Cs.tdtf��|j|d�SWdQRXdS)N��rU)r�DeprecationWarning�open)�zipfp�fnrrr�openU*sr(c@sZeZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7S)8�AbstractTestsWithSourceFilecCs5dd�tt�D�|_dj|j�|_dS)NcSs,g|]"}td|t�fd��qS)z'Zipfile test line %d. random float: %f
�ascii)�bytesr)�.0�irrr�
<listcomp>1s	z:AbstractTestsWithSourceFile.setUpClass.<locals>.<listcomp>�)�range�FIXEDTEST_SIZE�line_gen�join�data)�clsrrr�
setUpClass/s	z&AbstractTestsWithSourceFile.setUpClassc	Cs-ttd��}|j|j�WdQRXdS)N�wb)r%r�writer4)�self�fprrr�setUp6sz!AbstractTestsWithSourceFile.setUpc
CsVtj|d|��:}|jtd�|jtt�|jd|j�WdQRXdS)N�wzanother.name�strfile)�zipfile�ZipFiler8r�writestrr4)r9r �compressionr&rrr�make_test_archive;sz-AbstractTestsWithSourceFile.make_test_archivecCs�|j||�tj|d|���}|j|jt�|j�|j|jd�|j�|j|jd�|j�tj�}|j	d|�|j
�}|j�}|jt|�d�|j
d|d�|j
d|d�|j
d	|d�|d
j�\}}}	}
|j|d�|jtj|d��|jtj|	d��|j|
tt|j���|j�}|jt|�d
�|j
t|�|j
d|�|j
d|�|j�}dd�|D�}|jt|�d
�|j
t|�|j
d|�|j
d|�x*|D]"}
|j|
jt|j��q=WxUtddfD]D}|j|�}|j|j|�|j|jt|j��qsW|j�WdQRXdS)N�rzanother.namer=�file�z	File Namer�Modified�Size�z%Y-%m-%dz%H:%M:%S�cSsg|]}|j�qSr)�filename)r,r-rrrr.es	z8AbstractTestsWithSourceFile.zip_test.<locals>.<listcomp>)rBr>r?�assertEqual�readrr4r�StringIO�printdir�getvalue�
splitlines�len�assertIn�split�
assertTrue�time�strptime�str�namelist�infolist�	file_size�getinforJ�testzip)r9r rAr&r:�	directory�linesr'�date�time_r�names�infosr-�nm�inforrr�zip_testBsH
  z$AbstractTestsWithSourceFile.zip_testcCs.x't|�D]}|j||j�q
WdS)N)r!rerA)r9r rrr�
test_basicvsz&AbstractTestsWithSourceFile.test_basicc	Cs�|j||�tj|d|���}g}|jt��1}x'|jd�}|sYP|j|�qCWWdQRXg}|jd��1}x'|jd�}|s�P|j|�q�WWdQRX|jdj|�|j	�|jdj|�|j	�WdQRXdS)NrC�zanother.namer/)
rBr>r?r%rrL�appendrKr3r4)	r9r rAr&�zipdata1�zipopen1�	read_data�zipdata2�zipopen2rrr�
zip_open_testzs$z)AbstractTestsWithSourceFile.zip_open_testcCs.x't|�D]}|j||j�q
WdS)N)r!rnrA)r9r rrr�	test_open�sz%AbstractTestsWithSourceFile.test_opencCs�|j||�tj|d|��u}g}|jt��:}x0|jtdd��}|sbP|j|�qCWWdQRX|jdj	|�|j
�WdQRXdS)NrCrHir/)rBr>r?r%rrLrrhrKr3r4)r9r rAr&rirjrkrrr�zip_random_open_test�sz0AbstractTestsWithSourceFile.zip_random_open_testcCs.x't|�D]}|j||j�q
WdS)N)r!rprA)r9r rrr�test_random_open�sz,AbstractTestsWithSourceFile.test_random_opencCs�|j||�tj|d��P}|jt��7}g}x'|jd�}|sVP|j|�q@WWdQRXWdQRX|jdj|�|j	�dS)NrCrHr/���)
rBr>r?r%r�read1rhrKr3r4)r9r rAr&�zipopen�zipdatarkrrr�zip_read1_test�sz*AbstractTestsWithSourceFile.zip_read1_testcCs.x't|�D]}|j||j�q
WdS)N)r!rvrA)r9r rrr�
test_read1�sz&AbstractTestsWithSourceFile.test_read1cCs�|j||�tj|d��f}|jt��M}g}x=|jd�}|jt|�d�|slP|j|�q@WWdQRXWdQRX|j	dj
|�|j�dS)NrC�
r/)rBr>r?r%rrsZassertLessEqualrQrhrKr3r4)r9r rAr&rtrurkrrr�zip_read1_10_test�sz-AbstractTestsWithSourceFile.zip_read1_10_testcCs.x't|�D]}|j||j�q
WdS)N)r!ryrA)r9r rrr�
test_read1_10�sz)AbstractTestsWithSourceFile.test_read1_10cCs�|j||�tj|d��j}|jt��Q}d}xA|j�}|sSP||7}|jd�}|ssP||7}q@WWdQRXWdQRX|j||j�dS)NrCr/�d)	rBr>r?r%r�readlinerLrKr4)r9r rAr&rtr4rLrrr�zip_readline_read_test�s
z2AbstractTestsWithSourceFile.zip_readline_read_testcCs.x't|�D]}|j||j�q
WdS)N)r!r}rA)r9r rrr�test_readline_read�sz.AbstractTestsWithSourceFile.test_readline_readcCsy|j||�tj|d��P}|jt��7}x-|jD]"}|j�}|j||�qAWWdQRXWdQRXdS)NrC)rBr>r?r%rr2r|rK)r9r rAr&rt�line�linedatarrr�zip_readline_test�sz-AbstractTestsWithSourceFile.zip_readline_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrr�
test_readline�sz)AbstractTestsWithSourceFile.test_readlinecCs�|j||�tj|d��_}|jt��}|j�}WdQRXx0t|j|�D]\}}|j||�q]WWdQRXdS)NrC)	rBr>r?r%r�	readlines�zipr2rK)r9r rAr&rt�ziplinesr�ziplinerrr�zip_readlines_test�sz.AbstractTestsWithSourceFile.zip_readlines_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrr�test_readlinessz*AbstractTestsWithSourceFile.test_readlinescCs||j||�tj|d��S}|jt��:}x0t|j|�D]\}}|j||�qJWWdQRXWdQRXdS)NrC)rBr>r?r%rr�r2rK)r9r rAr&rtrr�rrr�zip_iterlines_tests
z.AbstractTestsWithSourceFile.zip_iterlines_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrr�test_iterlinessz*AbstractTestsWithSourceFile.test_iterlinescCs�tjtd|j��}|jdd�WdQRXtjtd|j��R}|jd��9}|j|jd�d�|j|jd�d�WdQRXWdQRXdS)Nr<r=Z12rCrH�1�2)r>r?rrAr@r%rKrL)r9r&Zopenobjrrr�test_low_compressionsz0AbstractTestsWithSourceFile.test_low_compressioncCsTtjtd�}|jddd|j�|jd�}|j|j|j�dS)Nr<zb.txtzhello world�
compress_type)r>r?rr@rAr[rKr�)r9r&rdrrr�test_writestr_compressionsz5AbstractTestsWithSourceFile.test_writestr_compressioncCs�x�d	D]�}|d}t|�}tjtj�d|j��X}|jd|�|jdd��,}|j|�}|j	t
|�|�WdQRXWdQRXqWdS)
NrH����@r<�foorC)rHr�r�r�r�)rr>r?rrrAr@r%rLrKrQ)r9Z	test_sizerZZjunk�zipfr:�bufrrr�test_read_return_size$s

!z1AbstractTestsWithSourceFile.test_read_return_sizecHCs�tj�}tj|dd��/}|jd|jd|j�|j�}WdQRX|j�}tj|�}tj|��R}|j	d��9}|j
|d�|jt��|j
�WdQRXWdQRXWdQRXtj|�}tj|��^}|j	d��E}|j
|d�|jt��x|j
d�r;q)WWdQRXWdQRXWdQRXtj|�}tj|��^}|j	d��E}|j
|d�|jt��x|jd�r�q�WWdQRXWdQRXWdQRXdS)N�moder<r=r��r{)rrr>r?r@r4rA�tellrOr%�truncate�assertRaises�EOFErrorrLrs)r9r:r�Z
end_offset�zipfiledatartrrr�test_truncated_zipfile0s2z2AbstractTestsWithSourceFile.test_truncated_zipfilecCs�d}x�t|�D]�}tj|d|j��3}|jt|�t|�}|jd|�WdQRXtj|d��Y}t|�}t|t	�r�|jd||�n|jd||�|jd|�t|j
|��}|jd||�|jd|�|jd	|�|jtjkrM|jd
|�|jd|�|j|��Y}t|�}|jd||�|jd|�|jtjkr�|jd
|�WdQRX|jd
t|��WdQRX|jd
t|��qWdS)Nz	file.namer<zmode='w'rCzfilename=%rzfile=%rzmode='r'z	filemode=z
file_size=zcompress_type=zcompress_size=zname=%rz[closed])
r!r>r?rAr8r�reprrR�
isinstancerWr[�
ZIP_STOREDr%)r9�fnamer r&rCrtrrr�	test_reprNs6z%AbstractTestsWithSourceFile.test_reprcCstt�tt�dS)N)r	rr)r9rrr�tearDownms
z$AbstractTestsWithSourceFile.tearDownN)�__name__�
__module__�__qualname__�classmethodr6r;rBrerfrnrorprqrvrwryrzr}r~r�r�r�r�r�r�r�r�r�r�r�r�rrrrr).s64

	r)c@s�eZdZejZdZdd�Zdd�Zdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS)�StoredTestsWithSourceFileNcCsR|j||�tj|d��)}|jd�}|j|jd�WdQRXdS)NrCr=i��i�)rBr>r?r[rK�
external_attr)r9r rAr&�zinforrr�zip_test_writestr_permissionswsz7StoredTestsWithSourceFile.zip_test_writestr_permissionscCs.x't|�D]}|j|tj�q
WdS)N)r!r�r>r�)r9r rrr�test_writestr_permissions�sz3StoredTestsWithSourceFile.test_writestr_permissionscCsqtjtdtj��}|jtd�WdQRXtjtdtj�� }|j|j�dg�WdQRXdS)Nr<z	/absoluterC�absolute)r>r?rr�r8rrKrX)r9r&rrr�test_absolute_arcnames�sz0StoredTestsWithSourceFile.test_absolute_arcnamescCs�tjtdtj��}|jtt�WdQRXtjtdtj��6}|jd|j�|j|j	�tdg�WdQRXdS)Nr<�ar=)
r>r?rr�r8rr@r4rKrX)r9r&rrr�test_append_to_zip_file�s
z1StoredTestsWithSourceFile.test_append_to_zip_filec4Cs�dd}ttd��}|j|�WdQRXtjtdtj��}|jtt�WdQRXttd��k}|jt|��tj|d��<}|j	|j
�tg�|j	|jt�|j�WdQRXWdQRXttd��2}|j	|jt|��|�|j�}WdQRXt
j|��U}tj|��<}|j	|j
�tg�|j	|jt�|j�WdQRXWdQRXdS)NsI am not a ZipFile!rxr7r��rbrC)r%rr8r>r?r�r�seekrQrKrXrLr4rr)r9r4r r&r��biorrr�test_append_to_non_zip_file�s 
*$z5StoredTestsWithSourceFile.test_append_to_non_zip_filecCs�tj��E}tj|dtj��}|jtt�WdQRX|j�}WdQRXdd}tt	d��!}|j|�|j|�WdQRXtjt	��<}|j
|j�tg�|j
|jt�|j
�WdQRXdS)Nr<sI am not a ZipFile!rxr7)rrr>r?r�r8rrOr%rrKrXrLr4)r9r�r&r�r4r rrr�test_read_concatenated_zip_file�s

z9StoredTestsWithSourceFile.test_read_concatenated_zip_filec5Cs�tj��E}tj|dtj��}|jtt�WdQRX|j�}WdQRXdd}tt	d��!}|j|�|j|�WdQRXtjt	d��3}|j
|j�tg�|jd|j
�WdQRXtt	d��2}|j
|jt|��|�|j�}WdQRXtj|��t}tj|��[}|j
|j�tdg�|j
|jt�|j
�|j
|jd�|j
�WdQRXWdQRXdS)Nr<sI am not a ZipFile!i@Br7r�r=r�)rrr>r?r�r8rrOr%rrKrXr@r4rLrQ)r9r�r&r�r4r rrr�$test_append_to_concatenated_zip_file�s$

$z>StoredTestsWithSourceFile.test_append_to_concatenated_zip_filecCs�tjtdtj��}|jtt�WdQRXttd��}|jd�WdQRXtjtd��}|j|tj�WdQRXdS)Nr<r�z
rC)r>r?rr�r8rr%�assertIsInstance)r9r&r rrr�test_ignores_newline_at_end�sz5StoredTestsWithSourceFile.test_ignores_newline_at_endcCs�tjtdtj�� }d|_|jtt�WdQRXttd��}|jd�WdQRXtjtd��-}|j|tj�|j	|jd�WdQRXdS)Nr<sthis is a commentr�zabcdef
rC)
r>r?rr��commentr8rr%r�rK)r9r&r rrr�)test_ignores_stuff_appended_past_comments�s	zCStoredTestsWithSourceFile.test_ignores_stuff_appended_past_commentscCsetjtd��L}|jt�ttd��&}|j|jt�|j��WdQRXWdQRXdS)Nr<r�)r>r?rr8rr%rKrL)r9r&r rrr�test_write_default_name�s
z1StoredTestsWithSourceFile.test_write_default_namecCshtjtdd��}|jdd�WdQRXtjtdd��}|jt|jt�WdQRXdS)Nr�r<zsomefile.txt�bogusrC)r>r?rr@r��RuntimeErrorr8r)r9r&rrr�test_write_to_readonly�sz0StoredTestsWithSourceFile.test_write_to_readonlycCsFtjtd�tjtd��}|jt|jt�WdQRXdS)Nrr<)rr)	�os�utimerr>r?rr��
ValueErrorr8)r9r&rrr�test_add_file_before_1980�sz3StoredTestsWithSourceFile.test_add_file_before_1980)r�r�r�r>r�rAr�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�rs			
	r�c@s%eZdZejZdd�ZdS)�DeflateTestsWithSourceFilecCs�tjtd��}}|jtdtj�|jtdtj�|jd�}|jd�}|j|j	tj�|j|j	tj�WdQRXdS)Nr<ZstoremeZ	deflateme)
r>r?rr8rr��ZIP_DEFLATEDr[rKr�)r9r&�sinfoZdinforrr�test_per_file_compression�sz4DeflateTestsWithSourceFile.test_per_file_compressionN)r�r�r�r>r�rAr�rrrrr��s	r�c@seZdZejZdS)�Bzip2TestsWithSourceFileN)r�r�r�r>�	ZIP_BZIP2rArrrrr�	sr�c@seZdZejZdS)�LzmaTestsWithSourceFileN)r�r�r�r>�ZIP_LZMArArrrrr�sr�c@sjeZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�AbstractTestZip64InSmallFilescCs2dd�tdt�D�}dj|�|_dS)Ncss"|]}td|d�VqdS)zTest of zipfile line %d.r*N)r+)r,r-rrr�	<genexpr>sz;AbstractTestZip64InSmallFiles.setUpClass.<locals>.<genexpr>rs
)r0r1r3r4)r5r2rrrr6s	z(AbstractTestZip64InSmallFiles.setUpClassc	CsWtj|_tj|_dt_dt_ttd��}|j|j�WdQRXdS)Ni��	r7)	r>�ZIP64_LIMIT�_limit�ZIP_FILECOUNT_LIMIT�_filecount_limitr%rr8r4)r9r:rrrr;s		z#AbstractTestZip64InSmallFiles.setUpcCstj|d|dd��:}|jtd�|jtt�|jd|j�WdQRXtj|d|���}|j|jt�|j�|j|jd�|j�|j|jd�|j�tj	�}|j
|�|j�}|j�}|jt
|�d�|jd|d	�|jd
|d	�|jd|d	�|dj�\}}}	}
|j|d�|jtj|d
��|jtj|	d��|j|
tt
|j���|j�}|jt
|�d�|jt|�|jd|�|jd|�|j�}dd�|D�}|jt
|�d�|jt|�|jd|�|jd|�x*|D]"}
|j|
jt
|j��q�WxUtddfD]D}|j|�}|j|j|�|j|jt
|j��q�W|j�WdQRXdS)Nr<�
allowZip64Tzanother.namer=rCrEz	File NamerrFrGrHz%Y-%m-%dz%H:%M:%SrIcSsg|]}|j�qSr)rJ)r,r-rrrr.Ps	z:AbstractTestZip64InSmallFiles.zip_test.<locals>.<listcomp>)r>r?r8rr@r4rKrLrrMrNrOrPrQrRrSrTrUrVrWrXrYrZr[rJr\)r9r rAr&r:r]r^r'r_r`rrarbr-rcrdrrrre(sN

  z&AbstractTestZip64InSmallFiles.zip_testcCs.x't|�D]}|j||j�q
WdS)N)r!rerA)r9r rrrrfasz(AbstractTestZip64InSmallFiles.test_basiccCstjtd|jdd�}d|_d}x4t|�D]&}|jd|d|dd	�q:W|jt|j	��|�|j
�tjtd
|j�}|jt|j	��|�xLt|�D]>}|jd|�jd�}|j|d|dd	�q�W|j
�dS)Nr<r�Tr{�zfoo%08dz%drI�9rCr*)
r>r?rrA�debugr0r@rKrQrX�closerL�decode)r9r��numfilesr-�zipf2�contentrrr�test_too_many_fileses		$
 z1AbstractTestZip64InSmallFiles.test_too_many_filescCs�tjtd|jdd�}d|_d}x4t|�D]&}|jd|d|dd	�q:W|jt|j	��|�|j
tj��|jd|d
�WdQRX|jt|j	��|�|j�tjtd|jdd�}d|_|jt|j	��|�|j
tj��|jd|d
�WdQRX|jt|j	��|�|j�tjtd|jdd�}d|_|jt|j	��|�d
}x7t||�D]&}|jd|d|dd	�q�W|jt|j	��|�|j�tjtd|j�}|jt|j	��|�xLt|�D]>}|j
d|�jd�}|j|d|dd	�qUW|j�dS)Nr<r�Fr{r�zfoo%08dz%drIr�r/r�Tr�rCr*)r>r?rrAr�r0r@rKrQrXr��LargeZipFiler�rLr�)r9r�r�r-Z	numfiles2r�r�rrr�test_too_many_files_appendxsD		$
		
		$
 z8AbstractTestZip64InSmallFiles.test_too_many_files_appendcCs0|jt_|jt_tt�tt�dS)N)r�r>r�r�r�r	rr)r9rrrr��s
z&AbstractTestZip64InSmallFiles.tearDownN)r�r�r�r�r6r;rerfr�r�r�rrrrr�s
9'r�c@sIeZdZejZdd�Zdd�Zdd�Zdd�Z	d	S)
�StoredTestZip64InSmallFilescCsEtj|d|dd��#}|jtj|jtd�WdQRXdS)Nr<r�Fzanother.name)r>r?r�r�r8r)r9r rAr&rrr�large_file_exception_test�sz5StoredTestZip64InSmallFiles.large_file_exception_testcCsHtj|d|dd��&}|jtj|jd|j�WdQRXdS)Nr<r�Fzanother.name)r>r?r�r�r@r4)r9r rAr&rrr�large_file_exception_test2�sz6StoredTestZip64InSmallFiles.large_file_exception_test2cCsAx:t|�D],}|j|tj�|j|tj�q
WdS)N)r!r�r>r�r�)r9r rrr�test_large_file_exception�sz5StoredTestZip64InSmallFiles.test_large_file_exceptioncCswtjtdtjdd��}|jtd�WdQRXtjtdtj�� }|j|j�dg�WdQRXdS)Nr<r�Tz	/absoluterCr�)r>r?rr�r8rrKrX)r9r&rrrr��s
z2StoredTestZip64InSmallFiles.test_absolute_arcnamesN)
r�r�r�r>r�rAr�r�r�r�rrrrr��s
	r�c@seZdZejZdS)�DeflateTestZip64InSmallFilesN)r�r�r�r>r�rArrrrr��sr�c@seZdZejZdS)�Bzip2TestZip64InSmallFilesN)r�r�r�r>r�rArrrrr��sr�c@seZdZejZdS)�LzmaTestZip64InSmallFilesN)r�r�r�r>r�rArrrrr��sr�c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�ZdS)�PyZipFileTestscCs(|d|kr$|j|d|�dS)N�o�c)rR)r9�namerXrrr�assertCompiledIn�szPyZipFileTests.assertCompiledIncCs�tj|tjdtjtjk�s4|jd�tjj|d�}y-tj|tjtj	B�}tj
|�Wntk
r�|jd�YnXt|�dS)NZ
effective_idsz/requires write access to the installed locationztest_zipfile.try)
r��access�W_OK�supports_effective_idsZskipTest�pathr3r%�O_WRONLY�O_CREATr��	Exceptionr	)r9r�rJ�fdrrr�requiresWriteAccess�s

z"PyZipFileTests.requiresWriteAccessc Cs�|jtjjt��t���}tj|d���}t}|jd�r�|j	tj
�}tjdk	r�|j|j	tj��d|kr�t
jj|�}n|dd�}|j|�tjj|�}|j||j��|j||j��WdQRXWdQRXt���}tj|d���}t}|jd�rY|dd�}|j|d�ddtjj|�f}|j||j��|j||j��WdQRXWdQRXdS)	Nr<z.pyc�__pycache__rHZtestpackagez%s/%srrrr)r�r�r��dirname�__file__rr>�	PyZipFile�endswithrS�sep�altsep�extend�	importlib�util�source_from_cache�writepy�basename�assertNotInrXr�)r9�tr&r'Z
path_splitZbnrrr�test_write_pyfile�s,!
$!z PyZipFileTests.test_write_pyfilecCs�ddl}tjj|j�}|j|�t��\}tj|d��@}|j	|�|j
�}|jd|�|jd|�WdQRXWdQRXdS)Nrr<zemail/__init__.pyzemail/mime/text.py)�emailr�r�r�r�r�rr>r�rrXr�)r9r�
packagedirrr&rarrr�test_write_python_packages
!
z(PyZipFileTests.test_write_python_packagec.Cscddl}tjj|j�}|j|�t��%}tj|d��	}t	��}|j
|�WdQRX|j�}|jd|k�t	�� }|j
|ddd��WdQRX|j�}|jd|k�dd�}t	��1}|j
t��|j
|d|�WdQRXWdQRX|j�}|r>t|�|jd|k�WdQRXWdQRXdS)	Nrr<�SyntaxError�
filterfunccSsdS)NFr)Zwhateverrrr�<lambda>szCPyZipFileTests.test_write_filtered_python_package.<locals>.<lambda>cSstjj|�jd�S)NZbad)r�r�r�
startswith)r�rrr�filter$szAPyZipFileTests.test_write_filtered_python_package.<locals>.filter)rr�r�r�r�r�rr>r�rrrOrT�assertWarns�UserWarning�print)r9rrrr&Z	reportSIOZ	reportStrr
rrr�"test_write_filtered_python_packages&
! !
z1PyZipFileTests.test_write_filtered_python_packagecCs�ddl}tjj|j�}|j|�tr:dnd}d}t��j}tj	|dd|��H}|j
|�|j�}|jd||�|jd||�WdQRXWdQRXdS)NrrHz.pycr<�optimizezemail/__init__zemail/mime/text)
rr�r�r�r�r��	__debug__rr>r�rrXrR)r9rrZoptlevel�extrr&rarrr�test_write_with_optimization-s

z+PyZipFileTests.test_write_with_optimizationc,Cs:tjt�zttjjtd�d��}|jd�WdQRXttjjtd�d��}|jd�WdQRXttjjtd�d��}|jd�WdQRXt��l}tj	|d��P}|j
t�|j�}|jd|�|jd|�|j
d|�WdQRXWdQRXWdtt�XdS)Nzmod1.pyr<z
print(42)
zmod2.pyzprint(42 * 42)
zmod2.txtzbla bla bla
)r��mkdirrr%r�r3r8rr>r�rrXr�rr
)r9r:rr&rarrr�test_write_python_directory<s
!!!!
"z*PyZipFileTests.test_write_python_directoryc'Cstjt�z�ttjjtd�d��}|jd�WdQRXttjjtd�d��}|jd�WdQRXt��h}tj	|d��L}|j
tddd��|j�}|jd|�|j
d|�WdQRXWdQRXWdtt�XdS)	Nzmod1.pyr<z
print(42)
zmod2.pyzprint(42 * 42)
r
cSs|jd�S)Nzmod2.py)r�)r'rrrr]szEPyZipFileTests.test_write_python_directory_filtered.<locals>.<lambda>)r�rrr%r�r3r8rr>r�rrXr�rr
)r9r:rr&rarrr�$test_write_python_directory_filteredSs
!!!"z3PyZipFileTests.test_write_python_directory_filteredcCsyt��i}tj|d��M}ttd��}|jd�WdQRX|jt|jt�t	t�WdQRXWdQRXdS)Nr<z!most definitely not a python file)
rr>r�r%rr8r�r�rr	)r9rr&r rrr�test_write_non_pyfilegs
!z$PyZipFileTests.test_write_non_pyfilec&Cs�tjt�z�ttjjtd�d��}|jd�WdQRXt���}tj	|d��x}t
��#}|jtjjtd��WdQRX|jd|j
��|j�}|jd|�|jd|�WdQRXWdQRXWdtt�XdS)Nzmod1.pyr<zBad syntax in python file
r	zmod1.pyc)r�rrr%r�r3r8rr>r�rrrRrOrXrr
)r9r:rr&�srarrr�test_write_pyfile_bad_syntaxns
!!#"z+PyZipFileTests.test_write_pyfile_bad_syntaxN)
r�r�r�r�r�rrrrrrrrrrrrr��s
r�c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zej	e
jjdkd�d
d��Z
ej	e
jjdkd�dd��Zdd�ZdS)�ExtractTestscCs*tjtdtj��.}x$tD]\}}|j||�q"WWdQRXtjtd���}x�tD]�\}}|j|�}tjj	tj
�|�}tjj|�}|j||�t
|d��#}|j|j�|j��WdQRXt|�qeWWdQRXttjj	tj
�d��dS)Nr<rCr��ziptest2dir)r>r?rr��SMALL_TEST_DATAr@�extractr�r�r3�getcwd�normpathrKr%�encoderLr	r
)r9r&�fpath�fdata�writtenfile�correctfiler rrr�test_extract�s#zExtractTests.test_extractcCstjtdtj��.}x$tD]\}}|j||�q"WWdQRXtjtd���}|j�xntD]f\}}tjj	tj
�|�}t|d��#}|j|j
�|j��WdQRXt|�qoWWdQRXttjj	tj
�d��dS)Nr<rCr�r)r>r?rr�rr@�
extractallr�r�r3r r%rKr"rLr	r
)r9r&r#r$Zoutfiler rrr�test_extract_all�s
#zExtractTests.test_extract_allc
CsL|jtjj|��t|d��}|j|j�|�WdQRXdS)Nr�)rTr�r��isfiler%rKrL)r9rJr�r rrr�
check_file�szExtractTests.check_filecCs[tjj}|j|dd�d�|j|dd�d�|j|dd�d�dS)	Nz,,?,C:,foo,bar/z�,z_,C_,foo,bar/zza\b,c<d>e|f"g?h*iza\b,c_d_e_f_g_h_iz../../foo../../ba..r�/z	foo/ba..r)r>r?Z_sanitize_windows_namerK)r9Zsanrrr�test_sanitize_windows_name�sz'ExtractTests.test_sanitize_windows_namec	Cs/d
ddd
ddddg}|j|�dS)N�
../foo/bar�foo/bar�
foo/../bar�
foo/../../bar�
foo/bar/..�./../foo/bar�/foo/bar�/foo/../bar�/foo/../../bar)r/r0)r1r0)r2r0)r3r0)r4r0)r5r0)r6r0)r7r0)�_test_extract_hackers_arcnames)r9Zcommon_hacknamesrrr�*test_extract_hackers_arcnames_common_cases�s	z7ExtractTests.test_extract_hackers_arcnames_common_cases�\zRequires \ as path separator.cCsPdddddddd d!d"d#d$d%d&d'd(d)d*d+g}|j|�dS),N�
..\foo\bar�foo/bar�..\/foo\/bar�foo/\..\/bar�foo\/../\bar�	C:foo/bar�
C:/foo/bar�C://foo/bar�
C:\foo\bar�//conky/mountpoint/foo/bar�\\conky\mountpoint\foo\bar�///conky/mountpoint/foo/bar�conky/mountpoint/foo/bar�\\\conky\mountpoint\foo\bar�//conky//mountpoint/foo/bar�\\conky\\mountpoint\foo\bar�//?/C:/foo/bar�\\?\C:\foo\bar�C:/../C:/foo/bar�
C_/foo/bar�a:b\c<d>e|f"g?h*i�b/c_d_e_f_g_h_i�../../foo../../ba..r�	foo/ba..r)r;r<)r=r<)r>r<)r?r<)r@r<)rAr<)rBr<)rCr<)rDr<)rEr<)rFrG)rHrG)rIrG)rJrG)rKr<)rLr<)rMrN)rOrP)rQrR)r8)r9Zwindows_hacknamesrrr�*test_extract_hackers_arcnames_windows_only�s(	z7ExtractTests.test_extract_hackers_arcnames_windows_onlyr-zRequires / as path separator.cCs dddg}|j|�dS)	N�	//foo/bar�foo/bar�../../foo../../ba..r�foo../ba..r�
foo/..\bar)rTrU)rVrW)rXrX)r8)r9Zposix_hacknamesrrr�(test_extract_hackers_arcnames_posix_only�s	z5ExtractTests.test_extract_hackers_arcnames_posix_onlyc
'Cs[xT|D]L\}}d|j�}tjtdtj��5}tj�}||_d|_|j||�WdQRX|j	t
jd�}t
jj
ddd�}t
jj
||jd��}tjtd	��<}|j||�}	|j|	|d
d||	|f�WdQRX|j||�td�tjtd	��}|j|�WdQRX|j||�td�t
jj
t
j�|jd��}tjtd	��0}|j|�}	|j|	|d
d|�WdQRX|j||�t|jd�d
�tjtd	��}|j�WdQRX|j||�t|jd�d
�tt�qWdS)Nsfoobarr<i�r�r-�targetZsubdirZsubsubrC�msgzextract %r: %r != %rz
extract %rri�)r"r>r?rr��ZipInforJr�r@�replacer�r�r�r3rSrrKr+r
r(r r	)
r9Z	hacknamesZarcnameZ	fixednamer�r&r�Z
targetpathr&r%rrrr8�sB		

$z+ExtractTests._test_extract_hackers_arcnamesN)r�r�r�r'r)r+r.r9�unittestZskipIfr�r�r�rSrYr8rrrrr�s
**	rc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
dd��Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Z d;d<�Z!d=d>�Z"d?d@�Z#dAdB�Z$dCdD�Z%dEdF�Z&dGS)H�
OtherTestscCs8tjtdtj��[}|jdd�|jt��|jdd�WdQRX|j|j�dgd�WdQRXtjtd���}|j	�}d}x7|D]/}|j
|��}||j�7}WdQRXq�W|j|dd	h�d}x!|D]}||j|�7}q�W|j|dd	h�WdQRXdS)
Nr<r�r�Zbar�rCr/sfoobarsbarfoo)
r>r?rr�r@rrrKrXrYr%rLrR)r9r&rbr4rdrtrrr�test_open_via_zip_infos $

z!OtherTests.test_open_via_zip_infocCs�tj�}tj|d��}|jdd�WdQRXtj|d��K}xAdD]9}|jt��|jd|�}WdQRX|j�qTWWdQRXdS)Nr<zspam.txts
ababagalamagarC�Ur#)rbr#)	rrr>r?r@rr$r%r�)r9r r&r�rtrrr�test_universal_deprecation-s
z%OtherTests.test_universal_deprecationcCs�tj�}ddd}tj|dtj��}|jt|�WdQRXd}tj|d��;}t|t��"}x|D]}||7}q�WWdQRXWdQRX|j||j	dd��dS)	Nsa
r�ir<r/rCs
s
)
rrr>r?r�r@rr(rKr])r9r r4r&�data2rtrrrr�test_universal_readaheads8s
z$OtherTests.test_universal_readaheadscCs_tjtd��F}x<dD]4}tj|�}|jdO_|j||�qWWdQRXdS)Nr<Zabcdefghijklmnopr)r>r?rr\Z	flag_bitsr@)r9Zorig_zipr4r�rrr�-test_writestr_extended_local_header_issue1202Gs

z8OtherTests.test_writestr_extended_local_header_issue1202cCs�tjtd��A}x7tD]/\}}|j||�|j|jd�qWWdQRX|j|jd�tjtd��}|j|jd�WdQRX|j|jd�dS)Nr<zzipfp is not openzzipfp is not closedrC)r>r?rrr@ZassertIsNotNoner:�assertIsNone)r9r&r#r$rrr�
test_closeNszOtherTests.test_closecCs�tjtd��.}x$tD]\}}|j||�qWWdQRXy,tjtd��}tj��WdQRXWn(tjk
r�|j|jd�YnXdS)Nr<rCzzipfp is not closed)r>r?rrr@�
BadZipFilergr:)r9r&r#r$Zzipfp2rrr�test_close_on_exceptionZsz"OtherTests.test_close_on_exceptioncCs,d}|jttjtj|�d�dS)NsdPKx!p�@xPKxx!p�@�xPK/rC)r��NotImplementedErrorr>r?rr)r9r4rrr�test_unsupported_versiongsz#OtherTests.test_unsupported_versionc
CsStd�}tj|��1}x'|j�D]}|j|�j�q+WWdQRXdS)Nzzip_cp437_header.zip)rr>r?rXr%r�)r9r�r&r�rrr�test_read_unicode_filenamesrsz&OtherTests.test_read_unicode_filenamescCs�tjtd��D}|jdd�|jdd�|j|j�djt�WdQRXtjtd��;}|j|j	djd�|j|j	djd�WdQRXdS)Nr<zfoo.txtzTest for unicode filenameuö.txtrrCrH)
r>r?rr@r�rYrJrWrK�filelist)r9�zfrrr�test_write_unicode_filenameszs$z'OtherTests.test_write_unicode_filenamescCs�tt�d}d}tjtdtj��}|j||�WdQRX|jt��tjtdtj�WdQRXtjtd��9}|j|j	�|g�|j|j
|�|�WdQRXdS)Nztestfile.txts#hello, world. this is some content.�xrC)r	rr>r?r�r@r��FileExistsErrorrKrXrL)r9rJr�r&rrr�test_exclusive_create_zip_file�s
z)OtherTests.test_exclusive_create_zip_filecCs�tjjt�rtjt�d}d}y0tjtd��}|j||�WdQRXWntk
r||j	d�YnX|j
tjjt��tjtd�� }|j|j|�|�WdQRXdS)Nztestfile.txts#hello, world. this is some content.r�z1Could not append data to a non-existent zip file.rC)
r�r��existsrr	r>r?r@�OSError�failrTrKrL)r9rJr�rorrr�(test_create_non_existent_file_for_append�s

z3OtherTests.test_create_non_existent_file_for_appendcCsUttd��}|jd�WdQRXytjt�}Wntjk
rPYnXdS)Nr<zthis is not a legal zip file
)r%rr8r>r?ri)r9r:rorrr�test_close_erroneous_file�sz$OtherTests.test_close_erroneous_filecCs�ttd��}|jd�WdQRX|jtjt��ttd��}|jtj|��WdQRXtj�}|jd�|jtj|��|jdd�|jtj|��dS)Nr<zthis is not a legal zip file
r�sthis is not a legal zip file
r)	r%rr8rr>�
is_zipfilerrr�)r9r:rrr�test_is_zip_erroneous_file�s
z%OtherTests.test_is_zip_erroneous_filecCs�tj�}tj|dd��}|jdd�WdQRX|j�}xLtt|��D]8}tj|d|��}|jtj	tj|�qZWdS)Nr�r<zfoo.txtsO, for a Muse of Fire!)
rrr>r?r@rOr0rQr�ri)r9r:r�r��Nrrr�test_damaged_zipfile�szOtherTests.test_damaged_zipfilecCs�tjtdd��}|jdd�WdQRX|jtjt��ttd��9}|jtj|��|jdd�|j�}WdQRXt	j
�}|j|�|jtj|��|jdd�|jtj|��dS)Nr�r<zfoo.txtsO, for a Muse of Fire!r�r)r>r?rr@rTryr%r�rLrrr8)r9r�r:Zzip_contentsrrr�test_is_zip_valid_file�s
z!OtherTests.test_is_zip_valid_filecCs|jttjt�dS)N)r�rur>r?r)r9rrr�%test_non_existent_file_raises_OSError�sz0OtherTests.test_non_existent_file_raises_OSErrorcCsuttd�}|j�|jtjtjt�ttd��}|jd�WdQRX|jtjtjt�dS)Nr<z
short file)r%rr�r�r>rir?r8)r9r r:rrr�!test_empty_file_raises_BadZipFile�s
z,OtherTests.test_empty_file_raises_BadZipFilecCs�tj�}tj|dd��}|jdd�WdQRX|jt|jd�|jt|jd�|jt|j	�|jt|jdd�tt
d��}|jd�WdQRX|jt|jt
�dS)Nr�r<zfoo.txtzO, for a Muse of Fire!z	bogus.txtr�zzipfile test data)rrr>r?r@r�r�rLr%r\rr8)r9r4r�r rrr�#test_closed_zip_raises_RuntimeError�sz.OtherTests.test_closed_zip_raises_RuntimeErrorcCs|jttjtd�dS)N�q)r�r�r>r?r)r9rrr�test_bad_constructor_modesz$OtherTests.test_bad_constructor_modecCsxtjtdd��}|jdd�WdQRXtjtdd��-}|jd�|jt|jdd�WdQRXdS)Nr�r<zfoo.txtzO, for a Muse of Fire!rCr�)r>r?rr@rLr�r�r%)r9r�rrr�test_bad_open_modes

zOtherTests.test_bad_open_modecCs�tjtdd��v}|jdd�|jd��M}x-tt�D]}|j|jd�d�qGW|j|j�d�WdQRXWdQRXdS)Nr�r<zfoo.txtzO, for a Muse of Fire!rr/sO, for a Muse of Fire!)	r>r?rr@r%r0r1rKrL)r9r�r r-rrr�
test_read0szOtherTests.test_read0cCs<tjtdd�� }|jt|jdd�WdQRXdS)Nr�r<zfoo.txtrC)r>r?rr��KeyErrorr%)r9r�rrr�test_open_non_existent_item&sz&OtherTests.test_open_non_existent_itemcCs |jttjtdd�dS)Nr<rHrr)r�r�r>r?r)r9rrr�test_bad_compression_mode,sz$OtherTests.test_bad_compression_modecCsEd}tjtj|�d��}|jt|jd�WdQRXdS)NsfPK.�C�@xPK..�C�@�xPK/!rCrq)r>r?rrr�rkr%)r9r4r�rrr�test_unsupported_compression1sz'OtherTests.test_unsupported_compressionc
CsLtjtdd��0}|jdd�|j|j�dg�WdQRXdS)Nr�r<zfoo.txtqqqsO, for a Muse of Fire!zfoo.txt)r>r?rr@rKrX)r9r�rrr�test_null_byte_in_filename<sz%OtherTests.test_null_byte_in_filenamecCsP|jtjd�|jtjd�|jtjd�|jtjd�dS)N��.�8r�)rKr>ZsizeEndCentDirZsizeCentralDirZsizeEndCentDir64ZsizeEndCentDir64Locator)r9rrr�test_struct_sizesCszOtherTests.test_struct_sizesclCs,tjtdd��*}|j|jd�|jdd�WdQRXtjtdd��}|j|jd�WdQRXd}tjtdd�� }||_|jdd�WdQRXtjtdd��}|j|j|�WdQRXdjd	d
�td�D��}|jd
�}tjtdd�� }||_|jdd�WdQRXtjtdd��}|j|j|�WdQRXtjtdd��;}|j	t
��|d|_WdQRX|jdd�WdQRXtjtdd��}|j|j|�WdQRXtjtdd�� }d|_|jdd�WdQRXtjtdd��}d|_WdQRXtjtdd��}|j|jd�WdQRXtjtdd�� }d|_|jdd�WdQRXtjtdd��}d|_WdQRXtjtdd��}|j|jd�WdQRXdS)Nr�r<r/zfoo.txtzO, for a Muse of Fire!rCs9Bravely taking to his feet, he beat a very brave retreat.r"cSs"g|]}d|dd�qS)z%drIrxr)r,r-rrrr.^s	z,OtherTests.test_comments.<locals>.<listcomp>rHr�r*soopssoriginal commentr�san updated commentsoriginal comment that's longersshorter commentii��)r>r?rrKr�r@r3r0r"rr)r9r�Zzipfrr�Zcomment2rrr�
test_commentsJsL	"			zOtherTests.test_commentscCsVtjtdtj��7}|jdd�|jt��d|_WdQRXWdQRXdS)Nr<zfoo.txtzO, for a Muse of Fire!zthis is an error)r>r?rr�r@r��	TypeErrorr�)r9r�rrr�test_unicode_comment�szOtherTests.test_unicode_commentcCsntjtdtj�� }|j|j�d|_WdQRXtjtd��}|j|jd�WdQRXdS)Nr�sthis is a commentrC)r>r?rr�rrnr�rK)r9r�rrr�$test_change_comment_in_empty_archive�s
z/OtherTests.test_change_comment_in_empty_archivecCs�tjtdtj��}|jdd�WdQRXtjtdtj�� }|j|j�d|_WdQRXtjtd��}|j|jd�WdQRXdS)Nr<zfoo.txtzO, for a Muse of Fire!r�sthis is a commentrC)	r>r?rr�r@rTrnr�rK)r9r�rrr�'test_change_comment_in_nonempty_archive�sz2OtherTests.test_change_comment_in_nonempty_archivecCs�tjtdd�}|j�ytjtdd�}Wn"tjk
r\|jd�YnXtjtdd�}|j�ytjtdd�}Wn|jd�YnXdS)Nr�r<rCz+Unable to create empty ZIP file in 'w' moder�z+Unable to create empty ZIP file in 'a' mode)r>r?rr�rirv)r9r�rrr�test_empty_zipfile�s

zOtherTests.test_empty_zipfilecCs9ttd�}|j�|jtjtjtd�dS)Nr<rC)r%rr�r�r>rir?)r9r rrr�test_open_empty_file�s
zOtherTests.test_open_empty_filec
Cs|jttjdd�dS)NZ	seventies�rHr)r�rHrHrrr)r�r�r>r\)r9rrr�test_create_zipinfo_before_1980�s	z*OtherTests.test_create_zipinfo_before_1980c	CsBd}tjtj|�d��}|j|j��WdQRXdS)NsnPK���@����abcAPK���@������abcPK3%rC)r>r?rrrgr\)r9rur�rrr�#test_zipfile_with_short_extra_field�sz.OtherTests.test_zipfile_with_short_extra_fieldcCstt�tt�dS)N)r	rr)r9rrrr��s
zOtherTests.tearDownN)'r�r�r�rarcrerfrhrjrlrrmrprsrwrxrzr|r}r~rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr_sF


	
7	r_c@s(eZdZdd�Zdd�ZdS)�AbstractBadCrcTestsc
CsK|j}tjtj|�dd��}|jd|j��WdQRXdS)Nr�rC�afile)�zip_with_bad_crcr>r?rrrKr\)r9rur�rrr�test_testzip_with_bad_crc�s	!z-AbstractBadCrcTests.test_testzip_with_bad_crcc,Cs%|j}tjtj|�dd�� }|jtj|jd�WdQRXtjtj|�dd��9}|jdd��}|jtj|j�WdQRXWdQRXtjtj|�dd��\}|jdd��@}d|_	|jtj��x|jd�rq�WWdQRXWdQRXWdQRXdS)Nr�rCr�r`)
r�r>r?rrr�rirLr%Z
MIN_READ_SIZE)r9rur�Zcorrupt_filerrr�test_read_with_bad_crc�s	! !$!	z*AbstractBadCrcTests.test_read_with_bad_crcN)r�r�r�r�r�rrrrr��sr�c@seZdZejZdZdS)�StoredBadCrcTestssxPK ��;:r��afilehello,AworldPK ��;:r���afilePK3/N)r�r�r�r>r�rAr�rrrrr��s	r�c@seZdZejZdZdS)�DeflateBadCrcTestss|PKn}=FAKEnafile�H���W(�/�Iɠ=PKn}=FAKEn�afilePK33N)r�r�r�r>r�rAr�rrrrr��s	r�c@seZdZejZdZdS)�Bzip2BadCrcTestss�PKnu=FAKE8nafileBZh91AY&SYԨ��@D��  �P�!����u5:���"�(HjTe?�PKnu=FAKE8n ���afilePK3[N)r�r�r�r>r�rAr�rrrrr�s	r�c@seZdZejZdZdS)�LzmaBadCrcTestss�PKnu=FAKEnafile	]4I���:3`	q!.8PKnu=FAKEn ���afilePK3>N)r�r�r�r>r�rAr�rrrrr�s	r�c@szeZdZdZdZdZddZdd�Zdd	�Zd
d�Z	dd
�Z
edd��Zdd�Z
dS)�DecryptionTestss�PKn�i.#y�?&test.txt��gly|�-�=�y�r�Z}Lg���|�䋺�)���lPKn�i.#y�?& ��test.txtPK6Ls�PK	�}38xu��zeroUT	֋�Gڋ�GUx���<M�a�X�Y&�{oEם���PKxu��PK	�}38xu��
��zeroUT֋�GUxPK?[szipfile.py encryption testsicCs�ttd��}|j|j�WdQRXtjtd�|_ttd��}|j|j�WdQRXtjtd�|_	dS)Nr7rC)
r%rr8r4r>r?r�rrd�zip2)r9r:rrrr;9szDecryptionTests.setUpcCs8|jj�tjt�|jj�tjt�dS)N)r�r�r�r	rr�r)r9rrrr�As


zDecryptionTests.tearDowncCs6|jt|jjd�|jt|jjd�dS)Nztest.txt�zero)r�r�r�rLr�)r9rrr�test_no_passwordGsz DecryptionTests.test_no_passwordcCsV|jjd�|jt|jjd�|jjd�|jt|jjd�dS)Nsperlztest.txtr�)r��setpasswordr�r�rLr�)r9rrr�test_bad_passwordMsz!DecryptionTests.test_bad_passwordcCsb|jjd�|j|jjd�|j�|jjd�|j|jjd�|j�dS)Nspythonztest.txts12345r�)r�r�rKrL�plainr��plain2)r9rrr�test_good_passwordSsz"DecryptionTests.test_good_passwordcCsw|jt|jjd�|jt|jjdd�|jt|jjddd�|jt|jjddd�dS)N�unicodeztest.txt�python�pwd)r�r�r�r�rLr%r)r9rrr�test_unicode_passwordZsz%DecryptionTests.test_unicode_passwordN)r�r�r�r4rdr�r�r;r�r�r�rr�r�rrrrr�s	
r�c@s�eZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dS)�"AbstractTestsWithRandomBinaryFilescCsItdd�dtdd�}djdd�t|�D��|_dS)Nr��@irHr/css1|]'}tjdt�tdd��VqdS)z<fi�Ni���)�struct�packrr)r,r-rrrr�dsz@AbstractTestsWithRandomBinaryFiles.setUpClass.<locals>.<genexpr>)rr3r0r4)r5Z	datacountrrrr6as z-AbstractTestsWithRandomBinaryFiles.setUpClassc	Cs-ttd��}|j|j�WdQRXdS)Nr7)r%rr8r4)r9r:rrrr;gsz(AbstractTestsWithRandomBinaryFiles.setUpcCstt�tt�dS)N)r	rr)r9rrrr�ls
z+AbstractTestsWithRandomBinaryFiles.tearDownc
CsCtj|d|��'}|jtd�|jtt�WdQRXdS)Nr<zanother.name)r>r?r8r)r9r rAr&rrrrBpsz4AbstractTestsWithRandomBinaryFiles.make_test_archivecCs�|j||�tj|d|��d}|jt�}|jt|�t|j��|j||j�|j|jd�|j�WdQRXdS)NrCzanother.name)rBr>r?rLrrKrQr4)r9r rAr&�testdatarrrrevsz+AbstractTestsWithRandomBinaryFiles.zip_testcCs.x't|�D]}|j||j�q
WdS)N)r!rerA)r9r rrr�	test_read�sz,AbstractTestsWithRandomBinaryFiles.test_readcCsG|j||�tj|d|��}g}|jt��1}x'|jd�}|sYP|j|�qCWWdQRXg}|jd��1}x'|jd�}|s�P|j|�q�WWdQRXdj|�}	|jt	|	�t	|j
��|j|	|j
�dj|�}
|jt	|
�t	|j
��|j|
|j
�WdQRXdS)NrCrgzanother.namer/)rBr>r?r%rrLrhr3rKrQr4)r9r rAr&rirjrkrlrmZ	testdata1Z	testdata2rrrrn�s,z0AbstractTestsWithRandomBinaryFiles.zip_open_testcCs.x't|�D]}|j||j�q
WdS)N)r!rnrA)r9r rrrro�sz,AbstractTestsWithRandomBinaryFiles.test_opencCs�|j||�tj|d|���}g}|jt��:}x0|jtdd��}|sbP|j|�qCWWdQRXdj|�}|j	t
|�t
|j��|j	||j�WdQRXdS)NrCrHir/)rBr>r?r%rrLrrhr3rKrQr4)r9r rAr&rirjrkr�rrrrp�sz7AbstractTestsWithRandomBinaryFiles.zip_random_open_testcCs.x't|�D]}|j||j�q
WdS)N)r!rprA)r9r rrrrq�sz3AbstractTestsWithRandomBinaryFiles.test_random_openN)r�r�r�r�r6r;r�rBrer�rnrorprqrrrrr�`s
r�c@seZdZejZdS)� StoredTestsWithRandomBinaryFilesN)r�r�r�r>r�rArrrrr��sr�c@seZdZejZdS)�!DeflateTestsWithRandomBinaryFilesN)r�r�r�r>r�rArrrrr��sr�c@seZdZejZdS)�Bzip2TestsWithRandomBinaryFilesN)r�r�r�r>r�rArrrrr��sr�c@seZdZejZdS)�LzmaTestsWithRandomBinaryFilesN)r�r�r�r>r�rArrrrr��sr�c@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TellablecCs||_d|_dS)Nr)r:�offset)r9r:rrr�__init__�s	zTellable.__init__cCs%|jj|�}|j|7_|S)N)r:r8r�)r9r4�nrrrr8�szTellable.writecCs|jS)N)r�)r9rrrr��sz
Tellable.tellcCs|jj�dS)N)r:�flush)r9rrrr��szTellable.flushN)r�r�r�r�r8r�r�rrrrr��sr�c@s4eZdZdd�Zdd�Zdd�ZdS)�
UnseekablecCs
||_dS)N)r:)r9r:rrrr��szUnseekable.__init__cCs|jj|�S)N)r:r8)r9r4rrrr8�szUnseekable.writecCs|jj�dS)N)r:r�)r9rrrr��szUnseekable.flushN)r�r�r�r�r8r�rrrrr��sr�c@s(eZdZdd�Zdd�ZdS)�UnseekableTestsc'CsKxDdd�ttfD]-}|jd|��tj�}|jd�tj|�}tj||�dtj	��'}|j
dd�|j
dd	�WdQRX|j|j�dd
�d�tj|dd
��e}|j
d��}|j|j�d�WdQRX|j
d��}|j|j�d	�WdQRXWdQRXWdQRXqWdS)NcSs|S)Nr)r rrrr�sz/UnseekableTests.test_writestr.<locals>.<lambda>�wrappersabcr<�oness111�twoss222�sabcPKr�rC)r�r��subTestrrr8�BufferedWriterr>r?r�r@rKrOr%rL)r9r�r �bfr&r��zopenrrr�
test_writestr�s
! zUnseekableTests.test_writestrc5Cs�x�dd�ttfD]�}|jd|��ntj�}|jd�tj|�}tj||�dtj	���}|j
tt�t
td��}|jd�WdQRX|jtd�t
td��}|jd	�WdQRX|jtd
�WdQRX|j|j�dd�d�tj|d
d��e}|j
d��}|j|j�d�WdQRX|j
d
��}|j|j�d	�WdQRXWdQRXWdQRXqWdS)NcSs|S)Nr)r rrrr�sz,UnseekableTests.test_write.<locals>.<lambda>r�sabcr<r7s111r�s222r�r�sabcPKr�rC)r�r�r�rrr8r�r>r?r��
addCleanupr	rr%rKrOrL)r9r�r r�r&�f2r�r�rrr�
test_write�s&
! zUnseekableTests.test_writeN)r�r�r�r�r�rrrrr��sr�c@s�eZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dS)�TestsWithMultipleOpenscCs*dtd�|_dtd�|_dS)Ns111i's222)r�data1rd)r5rrrr6sz!TestsWithMultipleOpens.setUpClassc
CsLtj|dtj��-}|jd|j�|jd|j�WdQRXdS)Nr<r�r�)r>r?r�r@r�rd)r9r r&rrrrBsz(TestsWithMultipleOpens.make_test_archivecCs�x�t|�D]�}|j|�tj|dd���}|jd��^}|jd��E}|jd�}|jd�}||j�7}||j�7}WdQRXWdQRX|j||�|j||j�WdQRXq
WdS)Nr�rCr�i�)r!rBr>r?r%rLrKr�)r9r r��zopen1�zopen2r�rdrrr�test_same_files
$z%TestsWithMultipleOpens.test_same_filecCs�x�t|�D]�}|j|�tj|dd���}|jd��^}|jd��E}|jd�}|jd�}||j�7}||j�7}WdQRXWdQRX|j||j�|j||j�WdQRXq
WdS)Nr�rCr�r�i�)	r!rBr>r?r%rLrKr�rd)r9r r�r�r�r�rdrrr�test_different_file,s
$z*TestsWithMultipleOpens.test_different_filecCs�x�t|�D]�}|j|�tj|dd���}|jd��^}|jd�}|jd��6}|jd�}||j�7}||j�7}WdQRXWdQRX|j||j�|j||j�WdQRXq
WdS)Nr�rCr�i�r�)	r!rBr>r?r%rLrKr�rd)r9r r�r�r�r�rdrrr�test_interleaved:s
z'TestsWithMultipleOpens.test_interleavedcCs�x�t|�D]�}|j|�tj���}tj|d��7}|j|jd��}|j|jd��}WdQRX|jd�}|jd�}||j�7}||j�7}WdQRX|j	||j
�|j	||j�q
WdS)NrCr�r�i�)r!rB�
contextlib�	ExitStackr>r?�
enter_contextr%rLrKr�rd)r9r �stackr�r�r�r�rdrrr�test_read_after_closeIs
z,TestsWithMultipleOpens.test_read_after_closecCsxt|�D]�}tj|dtj��U}|jd|j�|jd|j�|jd��}|jd�}WdQRXWdQRX|j	||jdd��tj|d��%}|jd�}|jd�}WdQRX|j	||j�|j	||j�q
WdS)Nr<r�r�i�rC)
r!r>r?r�r@r�rdr%rLrK)r9r r�r�r�rdrrr�test_read_after_writeWsz,TestsWithMultipleOpens.test_read_after_writecCs�x�t|�D]�}tj|dtj��S}|jd|j�|jd��'}|jd�|jd|j�WdQRXWdQRXtj|d��%}|jd�}|jd�}WdQRX|j	||j�|j	||j�q
WdS)Nr<r�i�r�rC)
r!r>r?r�r@r�r%rLrdrK)r9r r�r�r�rdrrr�test_write_after_reades
!z,TestsWithMultipleOpens.test_write_after_readcCs�|jt�tjtdd��D}x:td�D],}|jd�|jd��}WdQRXq2WWdQRXttj��}|j	|j
�d�WdQRXdS)Nr�rCr{r�)rBrr>r?r0rLr%r��devnullZ
assertLess�fileno)r9r�rqr�r rrr�test_many_opensrs

z&TestsWithMultipleOpens.test_many_openscCstt�dS)N)r	r)r9rrrr�~szTestsWithMultipleOpens.tearDownN)r�r�r�r�r6rBr�r�r�r�r�r�r�r�rrrrr�s
r�c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�TestWithDirectorycCstjt�dS)N)r�rr)r9rrrr;�szTestWithDirectory.setUpcCs�tjtd���}|jt�WdQRX|jtjjtjj	td���|jtjjtjj	tdd���|jtjj
tjj	tddd���dS)Nz
zipdir.zipr��br�)r>r?rr(rrTr�r��isdirr3rt)r9r�rrr�test_extract_dir�s
(+z"TestWithDirectory.test_extract_dircCs*tjtjjtd��|j�dS)Nr�)r�rr�r3rr�)r9rrr�
test_bug_6050�szTestWithDirectory.test_bug_6050cCs�tjjtd�}tj|�tj|�jd@}tjt	d���}|j
|�|jd}|j|j
jd��|j|j|d>dB�|j
|d�|jd}|j|j
d	�|j|j|d>dB�WdQRXtjt	d
���}|jd}|j|j
jd��|j|j|d>dB�|jd}|j|j
d	�|j|j|d>dB�tjjtd�}tj|�|j|�|jtjjtjj|d���|jttj|��d�WdQRXdS)
Nrqi��r<rz/x/r��yrHzy/rCrZr`)r�r�r3rr�stat�st_moder>r?rr8rnrTrJr�rKr�r(r�rQ�listdir)r9�dirpathr�r�r�rZrrr�test_write_dir�s0



"



(z TestWithDirectory.test_write_dircCsGtjtjjtd��tjtd��J}|jdd�|j	d}|j
|jd�|j
|jd�WdQRXtjtd���}|j	d}|j
|jjd��|j
|jd
�tjjtd	�}tj|�|j|�|j
tjjtjj|d���|j
tj|�dg�WdQRXdS)Nrqr<zx/r/ri�Ar�rCrZi�Ai�Ai�Ai�A)r�rr�r3rr>r?rr@rnrKrJr�rTr�r(r�r�)r9r�r�rZrrr�test_writestr_dir�s



(z#TestWithDirectory.test_writestr_dircCs*tt�tjjt�r&tt�dS)N)r
rr�r�rtrr	)r9rrrr��s
zTestWithDirectory.tearDownN)	r�r�r�r;r�r�r�r�r�rrrrr��sr�c@s�eZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�AbstractUniversalNewlineTestscCsodd�tt�D�|_d|_i|_x:t|j�D])\}}|j|j�||j|<q>WdS)NcSs#g|]}td|d��qS)zTest of zipfile line %d.r*)r+)r,r-rrrr.�s	z<AbstractUniversalNewlineTests.setUpClass.<locals>.<listcomp>�
�
�
)r�r�r�)r0r1r2�seps�arcdata�	enumerater3)r5r�rrrrr6�s			z(AbstractUniversalNewlineTests.setUpClasscCsxi|_xht|j�D]W\}}dt|f|j|<t|j|d��}|j|j|�WdQRXqWdS)Nz%s-%dr7)�arcfilesr�r�rr%r8r�)r9r�rr rrrr;�s
	z#AbstractUniversalNewlineTests.setUpcCsMtj|d|��1}x'|jj�D]}|j||�q(WWdQRXdS)Nr<)r>r?r��valuesr8)r9r rAr&r'rrrrB�sz/AbstractUniversalNewlineTests.make_test_archivecCs�|j||�tj|d��c}xY|jj�D]H\}}t||��}|j�}WdQRX|j|j||�q5WWdQRXdS)NrC)	rBr>r?r��itemsr(rLrKr�)r9r rAr&r�r'r:rurrr�	read_test�sz'AbstractUniversalNewlineTests.read_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrrr��sz'AbstractUniversalNewlineTests.test_readc	Cs�|j||�tj|d���}x�|jj�D]o\}}t||��Q}d}xA|j�}|soP||7}|jd�}|s�P||7}q\WWdQRXq5W|j||j	d�WdQRXdS)NrCr/r�s
)
rBr>r?r�r�r(r|rLrKr�)	r9r rAr&r�r'rtr4rLrrr�readline_read_test�s
z0AbstractUniversalNewlineTests.readline_read_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrrr~�sz0AbstractUniversalNewlineTests.test_readline_readc	Cs�|j||�tj|d��t}xj|jj�D]Y\}}t||��;}x1|jD]&}|j�}|j||d�q]WWdQRXq5WWdQRXdS)NrCs
)	rBr>r?r�r�r(r2r|rK)	r9r rAr&r�r'rtrr�rrr�
readline_testsz+AbstractUniversalNewlineTests.readline_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrrr�sz+AbstractUniversalNewlineTests.test_readlinec
Cs�|j||�tj|d���}xy|jj�D]h\}}t||��}|j�}WdQRXx4t|j|�D] \}}	|j	|	|d�qyWq5WWdQRXdS)NrCs
)
rBr>r?r�r�r(r�r�r2rK)
r9r rAr&r�r'r:r�rr�rrr�readlines_testsz,AbstractUniversalNewlineTests.readlines_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrrr�sz,AbstractUniversalNewlineTests.test_readlinesc	Cs�|j||�tj|d��w}xm|jj�D]\\}}t||��>}x4t|j|�D] \}}|j||d�qfWWdQRXq5WWdQRXdS)NrCs
)	rBr>r?r�r�r(r�r2rK)	r9r rAr&r�r'r:rr�rrr�iterlines_testsz,AbstractUniversalNewlineTests.iterlines_testcCs.x't|�D]}|j||j�q
WdS)N)r!r�rA)r9r rrrr�)sz,AbstractUniversalNewlineTests.test_iterlinescCsBx'|jj�D]\}}t|�qWtt�tt�dS)N)r�r�r	rr)r9r�r'rrrr�-s
z&AbstractUniversalNewlineTests.tearDownN)r�r�r�r�r6r;rBr�r�r�r~r�r�r�r�r�r�r�rrrrr��s	

r�c@seZdZejZdS)�StoredUniversalNewlineTestsN)r�r�r�r>r�rArrrrr�4sr�c@seZdZejZdS)�DeflateUniversalNewlineTestsN)r�r�r�r>r�rArrrrr�8sr�c@seZdZejZdS)�Bzip2UniversalNewlineTestsN)r�r�r�r>r�rArrrrr�=sr�c@seZdZejZdS)�LzmaUniversalNewlineTestsN)r�r�r�r>r�rArrrrr�Bsr�c@s^eZdZdd�Zdd�Zdd�Zdd�Zed	d
��Zdd�Z	d
S)�CommandLineTestcOs:tjdd||�\}}}|jtjj�d�S)Nz-mr>s
)rZassert_python_okr]r��linesepr")r9�args�kwargs�rc�out�errrrr�
zipfilecmdJszCommandLineTest.zipfilecmdcGstjdd|�S)Nz-mr>)rZassert_python_failure)r9r�rrr�zipfilecmd_failureOsz"CommandLineTest.zipfilecmd_failurecCsotd�}|jd|�}|j|j�d�td�}|jd|�\}}}|j|d�dS)Nz
zipdir.zipz-tsDone testingztesttar.tarr/)rrrK�rstripr)r9�zip_namerr�rrrr�test_test_commandRsz!CommandLineTest.test_test_commandcCs�td�}tj�}tj|d��}|j|�WdQRX|j�jdd�}|jd|dd�}|j	||�dS)Nz
zipdir.ziprCr*�backslashreplacez-lZPYTHONIOENCODINGzascii:backslashreplace)
rrrMr>r?rNrOr"rrK)r9rrZtfZexpectedrrrr�test_list_commandZs	z!CommandLineTest.test_list_commandcCsO|jtt�ttd��}|jd�WdQRXtjt�|jtt�ttj	j
td�d��}|jd�WdQRXttg}ttdtdg}z�|jdt|�}|j
|d�tjt��W}|j
|j�|�|j
|j|d	�d
�|j
|j|d�d�WdQRXWdtt�XdS)
Nr<ztest 1zfile.txtztest 2r-z	/file.txtz-cr/rstest 1r`stest 2)r�r	rr%r8r�r�	TESTFNDIRr
r�r3rrrKr>r?rXrL)r9r �filesrXrrorrr�test_create_commandds"
!(z#CommandLineTest.test_create_commandcCstd�}t��}|jd||�}|j|d�tj|���}x�|j�D]�}tjj	||j
jdtj��}|j
j
d�r�|jtjj|��q\|jtjj|��t|d��&}|j|j�|j|��WdQRXq\WWdQRXWdQRXdS)Nz
zipdir.zipz-er/r-r�)rrrrKr>r?rYr�r�r3rJr]r�r�rTr�r*r%rL)r9rZextdirrroZzir�r rrr�test_extract_commandysz$CommandLineTest.test_extract_commandN)
r�r�r�rrrrrrrrrrrr�Hs
r��__main__)Ir�rr��sys�importlib.utilr�rUr�r>r^�tempfilerrrrZtest.supportrrrr	r
rrr
rrrrr	r1Z
DATAFILES_DIRrrr!r(r)ZTestCaser�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�r�r�r�r��mainrrrr�<module>s�F

				�E������A[
'pAqA