a
    ze;                    @   sX  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Zd dlZd dlZd dlZd dlm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&g 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*Z0e G dd de/ej*Z1e  G dd de/ej*Z2e! G dd  d e/ej*Z3G d!d" d"Z4G d#d$ d$e4ej*Z5e G d%d& d&e4ej*Z6e  G d'd( d(e4ej*Z7e! G d)d* d*e4ej*Z8G d+d, d,ej*Z9G d-d. d.ej*Z:G d/d0 d0ej*Z;G d1d2 d2Z<G d3d4 d4e<ej*Z=e G d5d6 d6e<ej*Z>e  G d7d8 d8e<ej*Z?e! G d9d: d:e<ej*Z@G d;d< d<ej*ZAG d=d> d>ZBG d?d@ d@eBej*ZCe G dAdB dBeBej*ZDe  G dCdD dDeBej*ZEe! G dEdF dFeBej*ZFG dGdH dHZGG dIdJ dJZHG dKdL dLej*ZIe G dMdN dNej*ZJG dOdP dPej*ZKG dQdR dRej*ZLG dSdT dTej*ZMG dUdV dVej*ZNeOZPG dWdX dXZQdYdZ ZRd[d\ ZSG d]d^ d^ej*ZTeUd_krTeV  dS )`    N)TemporaryFile)randintrandom	randbytes)script_helper)
TESTFNfindfileunlinkrmtreetemp_dirtemp_cwdrequires_zlibrequires_bz2requires_lzmacaptured_stdout2d  Zzipfile_datafiles))Z	_ziptest1Z
1q2w3e4r5t)zziptest2dir/_ziptest2Z
qawsedrftg)z!ziptest2dir/ziptest3dir/_ziptest3Z
azsxdcfvgb)z-ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3Z
6y7u8i9o0pc                 c   s|   t V  t "}|V  | |j W d    n1 s40    Y  t "}|V  | |j W d    n1 sn0    Y  d S N)TESTFN2r   assertFalseclosedioBytesIO)testf r   '/usr/lib/python3.9/test/test_zipfile.py	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>1   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    n1 s,0    Y  d S Nwbopenr   writer+   selffpr   r   r   setUp6   s    z!AbstractTestsWithSourceFile.setUpNc              	   C   s   ||d}t j|dfi |z}|td |tt |d| j |jddd&}| jD ]}|| q\W d    n1 s0    Y  W d    n1 s0    Y  d S )N)compressioncompresslevelwanother.namestrfilewritten-open-wmode)zipfileZipFiler3   r   writestrr+   r2   r)   )r5   r   r8   r9   kwargszipfpliner   r   r   make_test_archive;   s    

z-AbstractTestsWithSourceFile.make_test_archivec                 C   sD  |  ||| 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| |D ]}| |jt| j qtdddfD ]2}||}| |j| | |jt| j q|  W d    n1 s60    Y  d S )Nrr;   r<   )file   	File Namer   ModifiedSize   %Y-%m-%d%H:%M:%S   r=   c                 S   s   g | ]
}|j qS r   filenamer"   r   r   r   r%   j   r&   z8AbstractTestsWithSourceFile.zip_test.<locals>.<listcomp>)rF   r@   rA   assertEqualreadr   r+   r   StringIOprintdirgetvalue
splitlineslenassertInsplit
assertTruetimestrptimestrnamelistinfolist	file_sizegetinforR   testzip)r5   r   r8   r9   rD   r6   	directorylinesfndatetime_sizenamesinfosr$   nminfor   r   r   zip_testF   sL    
z$AbstractTestsWithSourceFile.zip_testc                 C   s    t | D ]}| || j qd S r   r   ro   r8   r5   r   r   r   r   
test_basic|   s    z&AbstractTestsWithSourceFile.test_basicc           	   	   C   s   |  || t|d|}g }|t,}|d}|s<qH|| q,W d    n1 s\0    Y  g }|d,}|d}|sq|| qvW d    n1 s0    Y  | d|| j	 | d|| j	 W d    n1 s0    Y  d S NrG      r;   r&   )
rF   r@   rA   r2   r   rT   appendrS   r*   r+   )	r5   r   r8   rD   zipdata1zipopen1	read_datazipdata2zipopen2r   r   r   zip_open_test   s     
*
*z)AbstractTestsWithSourceFile.zip_open_testc                 C   s    t | D ]}| || j qd S r   r   r{   r8   rq   r   r   r   	test_open   s    z%AbstractTestsWithSourceFile.test_openc                 C   sZ   t t}| || j t|d| j}| |jt	 W d    n1 sL0    Y  d S NrG   )
pathlibPathr   r{   r8   r@   rA   assertIsInstancerR   r_   )r5   pathrD   r   r   r   test_open_with_pathlike   s    
z3AbstractTestsWithSourceFile.test_open_with_pathlikec              	   C   s   |  || t|d|t}g }|t2}|tdd}|sBqN|| q,W d    n1 sb0    Y  | d	|| j
 W d    n1 s0    Y  d S NrG   rM      r&   )rF   r@   rA   r2   r   rT   r   ru   rS   r*   r+   )r5   r   r8   rD   rv   rw   rx   r   r   r   zip_random_open_test   s    *z0AbstractTestsWithSourceFile.zip_random_open_testc                 C   s    t | D ]}| || j qd S r   r   r   r8   rq   r   r   r   test_random_open   s    z,AbstractTestsWithSourceFile.test_random_openc              	   C   s   |  || t|dZ}|t0}g }|d}|s:qF|| q*W d    n1 sZ0    Y  W d    n1 sx0    Y  | d|| j	 d S )NrG   r&   )
rF   r@   rA   r2   r   read1ru   rS   r*   r+   r5   r   r8   rD   zipopenzipdatarx   r   r   r   zip_read1_test   s    
Hz*AbstractTestsWithSourceFile.zip_read1_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   
test_read1   s    z&AbstractTestsWithSourceFile.test_read1c              	   C   s   |  || t|dj}|t@}g }|d}| t|d |sJqV|| q*W d    n1 sj0    Y  W d    n1 s0    Y  | 	d
|| j d S )NrG   
   r&   )rF   r@   rA   r2   r   r   ZassertLessEqualrY   ru   rS   r*   r+   r   r   r   r   zip_read1_10_test   s    
Hz-AbstractTestsWithSourceFile.zip_read1_10_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   test_read1_10   s    z)AbstractTestsWithSourceFile.test_read1_10c              	   C   s   |  || t|dn}|tD}d}| }|s8qZ||7 }|d}|sPqZ||7 }q*W d    n1 sn0    Y  W d    n1 s0    Y  | || j d S )NrG   r&   d   )	rF   r@   rA   r2   r   readlinerT   rS   r+   )r5   r   r8   rD   r   r+   rT   r   r   r   zip_readline_read_test   s    
Fz2AbstractTestsWithSourceFile.zip_readline_read_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   test_readline_read   s    z.AbstractTestsWithSourceFile.test_readline_readc              	   C   s   |  || t|dZ}|t0}| jD ]}| }| || q,W d    n1 sZ0    Y  W d    n1 sx0    Y  d S r~   )rF   r@   rA   r2   r   r)   r   rS   )r5   r   r8   rD   r   rE   Zlinedatar   r   r   zip_readline_test   s    
z-AbstractTestsWithSourceFile.zip_readline_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   test_readline   s    z)AbstractTestsWithSourceFile.test_readlinec              	   C   s   |  || t|dd}|t}| }W d    n1 sB0    Y  t| j|D ]\}}| || qXW d    n1 s0    Y  d S r~   )	rF   r@   rA   r2   r   	readlineszipr)   rS   )r5   r   r8   rD   r   ZziplinesrE   zipliner   r   r   zip_readlines_test  s    &z.AbstractTestsWithSourceFile.zip_readlines_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   test_readlines  s    z*AbstractTestsWithSourceFile.test_readlinesc              	   C   s   |  || t|d\}|t2}t| j|D ]\}}| || q2W d    n1 s\0    Y  W d    n1 sz0    Y  d S r~   )rF   r@   rA   r2   r   r   r)   rS   )r5   r   r8   rD   r   rE   r   r   r   r   zip_iterlines_test  s
    z.AbstractTestsWithSourceFile.zip_iterlines_testc                 C   s    t | D ]}| || j qd S r   )r   r   r8   rq   r   r   r   test_iterlines  s    z*AbstractTestsWithSourceFile.test_iterlinesc              	   C   s   t td| j}|dd W d   n1 s20    Y  t td| j^}|d4}| |dd | |dd W d   n1 s0    Y  W d   n1 s0    Y  dS )	z>Check for cases where compressed data is larger than original.r:   r<   Z12NrG   rM      1   2)r@   rA   r   r8   rB   r2   rS   rT   )r5   rD   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 )Nr:   b.txthello worldcompress_type)r@   rA   r   rB   r8   rc   rS   r   )r5   rD   rn   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 )
Nr:   rM   r9   a.txtr   r   r      )r   r9   )	r@   rA   r   rB   r8   rc   rS   r   _compresslevel)r5   rD   Za_infoZb_infor   r   r   test_writestr_compresslevel0  s    

z7AbstractTestsWithSourceFile.test_writestr_compresslevelc              
   C   s   dD ]}|d }t |}tt d| jb}|d| |dd*}||}| 	t
|| W d    n1 sv0    Y  W d    q1 s0    Y  qd S )N)rM   i  i   i  i @  rM   r:   foorG   )r   r@   rA   r   r   r8   rB   r2   rT   rS   rY   )r5   Z	test_sizerb   Zjunkzipfr6   bufr   r   r   test_read_return_size@  s    
z1AbstractTestsWithSourceFile.test_read_return_sizec              
   C   sT  t  }tj|dd,}|jd| j| jd | }W d    n1 sH0    Y  | }t |}t|z}|	dP}|
|d  | t |  W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  t |}t|}|	dZ}|
|d  | t  |drDq4W d    n1 sZ0    Y  W d    n1 sz0    Y  W d    n1 s0    Y  t |}t|}|	dZ}|
|d  | t  |drqW d    n1 s0    Y  W d    n1 s&0    Y  W d    n1 sF0    Y  d S )Nr:   r>   r<   r      r   )r   r   r@   rA   rB   r+   r8   tellrW   r2   truncateassertRaisesEOFErrorrT   r   )r5   r6   r   Z
end_offsetzipfiledatar   r   r   r   test_truncated_zipfileL  s2    &
b
d
z2AbstractTestsWithSourceFile.test_truncated_zipfilec              
   C   s  d}t | D ]}t|d| j0}|t| t|}| d| W d    n1 sX0    Y  t|d&}t|}t|t	r| d| | n| d| | | d| t|
|}| d| | | d| | d	| | jtjkr| d
| | d| ||N}t|}| d| | | d| | jtjkrX| d
| W d    n1 sn0    Y  | dt| W d    n1 s0    Y  | dt| qd S )Nz	file.namer:   zmode='w'rG   zfilename=%rzfile=%rzmode='r'z	filemode=z
file_size=zcompress_type=zcompress_size=zname=%rz[closed])r   r@   rA   r8   r3   r   reprrZ   
isinstancer_   rc   
ZIP_STOREDr2   )r5   fnamer   rD   rG   r   r   r   r   	test_reprj  s6    *
,0z%AbstractTestsWithSourceFile.test_reprc                 C   s$   t | D ]}| j|| jdd qd S )N	   r   rp   rq   r   r   r   test_compresslevel_basic  s    z4AbstractTestsWithSourceFile.test_compresslevel_basicc                 C   s   t jtddd\}|td |jtddd |d}|d}| |jd | |jd W d   n1 sr0    Y  dS )zTCheck that files within a Zip archive can have different
        compression levels.r:   rM   r   Z
compress_1Z
compress_9r   N)r@   rA   r   r3   r   rc   rS   r   )r5   rD   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| }d  t|d| j}|dd}|d W d    n1 sd0    Y  d ztz:|dd}|d W d    n1 s0    Y  W n ty   d	7 Y n0 W d  W d    qjW d  nd  0 W d    n1 s0    Y  tt | 4}| 	|
 dg | 	|dd W d    q1 s^0    Y  qtt | H}| 	|
 ddg | 	|dd | 	|dd W d    n1 s0    Y  d S )
Nc                       s    e Zd Z fddZ  ZS )zCAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFilec                    s,   d urkrt d7 t | d S NrM   )OSErrorsuperr3   r5   r+   )	__class__countstopr   r   r3     s
    zIAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFile.write)__name__
__module____qualname__r3   __classcell__r   r   r   )r   r   
BrokenFile  s   r   r   r:   Zfile1s   data1Zfile2s   data2rM   )r   r   r@   rA   r8   r2   r3   r   rW   rS   r`   rT   )r5   r   ZtestfilerD   r   r   r   r   test_writing_errors  s0    	(,,4z/AbstractTestsWithSourceFile.test_writing_errorsc                 C   s   t t t t d S r   r	   r   r   r5   r   r   r   tearDown  s    z$AbstractTestsWithSourceFile.tearDown)N)N)$r   r   r   classmethodr.   r7   rF   ro   rr   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   r   r   r   r   r   .   sB   


6

	%r   c                   @   s   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d Zdd ZdS )StoredTestsWithSourceFileNc                 C   sl   |  || t|d@}|d}| |jd |d}| |jd W d    n1 s^0    Y  d S )NrG   r<     r=   )rF   r@   rA   rc   rS   external_attr)r5   r   r8   rD   zinfoZzinfo2r   r   r   zip_test_writestr_permissions  s    

z7StoredTestsWithSourceFile.zip_test_writestr_permissionsc                 C   s    t | D ]}| |tj qd S r   )r   r   r@   r   rq   r   r   r   test_writestr_permissions  s    z3StoredTestsWithSourceFile.test_writestr_permissionsc                 C   s   t tdt j}|td W d    n1 s20    Y  t tdt j"}| | dg W d    n1 st0    Y  d S )Nr:   	/absoluterG   absoluter@   rA   r   r   r3   r   rS   r`   r5   rD   r   r   r   test_absolute_arcnames  s    *z0StoredTestsWithSourceFile.test_absolute_arcnamesc                 C   s   t tdt j}|tt W d   n1 s20    Y  t tdt j2}|d| j | |	 tdg W d   n1 s0    Y  dS )z&Test appending to an existing zipfile.r:   Nar<   )
r@   rA   r   r   r3   r   rB   r+   rS   r`   r   r   r   r   test_append_to_zip_file  s
    *z1StoredTestsWithSourceFile.test_append_to_zip_filec              	   C   s  d}t td}|| W d   n1 s.0    Y  ttdtj}|tt W d   n1 sj0    Y  t tdp}|t| t|d6}| 	|
 tg | 	|t| j W d   n1 s0    Y  W d   n1 s0    Y  t td.}| 	|t|| | }W d   n1 s>0    Y  t|b}t|6}| 	|
 tg | 	|t| j W d   n1 s0    Y  W d   n1 s0    Y  dS )z9Test appending to an existing file that is 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!I am not a ZipFile!r0   Nr   rbrG   )r2   r   r3   r@   rA   r   r   seekrY   rS   r`   rT   r+   r   r   )r5   r+   r   rD   r   bior   r   r   test_append_to_non_zip_file  s     (*P(z5StoredTestsWithSourceFile.test_append_to_non_zip_filec              	   C   s  t  T}t|dtj}|tt W d    n1 s<0    Y  | }W d    n1 sb0    Y  d}tt	d$}|| || W d    n1 s0    Y  tt	6}| 
| tg | 
|t| j W d    n1 s0    Y  d S )Nr:   r   r0   )r   r   r@   rA   r   r3   r   rW   r2   r   rS   r`   rT   r+   r5   r   rD   r   r+   r   r   r   r   test_read_concatenated_zip_file  s    
*&
(z9StoredTestsWithSourceFile.test_read_concatenated_zip_filec              	   C   s  t  T}t|dtj}|tt W d    n1 s<0    Y  | }W d    n1 sb0    Y  dd }tt	d$}|| || W d    n1 s0    Y  tt	d0}| 
| tg |d| j W d    n1 s0    Y  tt	d.}| 
|t|| | }W d    n1 s>0    Y  t |x}t|L}| 
| tdg | 
|t| j | 
|d| j W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr:   s   I am not a ZipFile!i@B r0   r   r<   r   )r   r   r@   rA   r   r3   r   rW   r2   r   rS   r`   rB   r+   rT   rY   r   r   r   r   $test_append_to_concatenated_zip_file  s$    
*&
(,(z>StoredTestsWithSourceFile.test_append_to_concatenated_zip_filec                 C   s   t tdt j}|tt W d    n1 s20    Y  ttd}|d W d    n1 sf0    Y  t td}| |t j W d    n1 s0    Y  d S )Nr:   r   z
   rG   )r@   rA   r   r   r3   r   r2   r   r5   rD   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    n1 s80    Y  ttd}|d W d    n1 sl0    Y  t td,}| |t j | 	|jd W d    n1 s0    Y  d S )Nr:      this is a commentr   zabcdef
rG   )
r@   rA   r   r   commentr3   r   r2   r   rS   r   r   r   r   )test_ignores_stuff_appended_past_comments+  s    *(zCStoredTestsWithSourceFile.test_ignores_stuff_appended_past_commentsc              	   C   sz   t tdZ}|t ttd&}| |t|  W d   n1 sN0    Y  W d   n1 sl0    Y  dS )z`Check that calling ZipFile.write without arcname specified
        produces the expected result.r:   r   N)r@   rA   r   r3   r   r2   rS   rT   r   r   r   r   test_write_default_name5  s    
z1StoredTestsWithSourceFile.test_write_default_namec              	   C   s   d}t jtdd}||d W d    n1 s40    Y  t jtdd}||`}|  | t|j | t|j	d | t|j
 | t|j | t|j W d    n1 s0    Y  W d    n1 s0    Y  d S )Nsomefile.txtr:   r>   bogusrG   r   )r@   rA   r   rB   r2   closer   
ValueErrorrT   r   r   readableseekable)r5   r   rD   Zfidr   r   r   test_io_on_closed_zipextfile=  s    *z6StoredTestsWithSourceFile.test_io_on_closed_zipextfilec              	   C   s   t jtdd}|dd W d   n1 s00    Y  t jtdd }| t|jt W d   n1 sn0    Y  t jtddH}| t |jtdd W d   n1 s0    Y  W d   n1 s0    Y  dS )z[Check that trying to call write() on a readonly ZipFile object
        raises a ValueError.r:   r>   r   r   NrG   )	r@   rA   r   rB   r   r   r3   r   r2   r   r   r   r   test_write_to_readonlyK  s    *.z0StoredTestsWithSourceFile.test_write_to_readonlyc                 C   s   t td ttd }| t|jt W d    n1 s>0    Y  tjtddd2}|t |	t}| 
|jd W d    n1 s0    Y  d S )N)r   r   r:   FZstrict_timestampsi  rM   rM   r   r   r   )osutimer   r@   rA   r   r   r   r3   rc   rS   	date_time)r5   rD   r   r   r   r   test_add_file_before_1980X  s    .

z3StoredTestsWithSourceFile.test_add_file_before_1980c                 C   s$  d}zt | W n$ ty6   | d| d Y n0 ztt||f W n tyh   | d Y n0 ttj}|dkr| d| t	
td"}| tj|jt W d    n1 s0    Y  t	j
tddd	2}|t |t}| |jd
 W d    n1 s0    Y  d S )Nl   &
 ztime.localtime(z) raises OverflowErrorz/Host fs cannot set timestamp to required value.l     b f z,Linux VFS/XFS kernel bug detected: mtime_ns=r:   Fr   )i;           ;   r   )r]   	localtimeOverflowErrorskipTestr   r   r   statst_mtime_nsr@   rA   r   r   structerrorr3   rc   rS   r   )r5   tsZmtime_nsrD   r   r   r   r   test_add_file_after_2107c  s$    	0

z2StoredTestsWithSourceFile.test_add_file_after_2107)r   r   r   r@   r   r8   r   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ejZdd ZdS )DeflateTestsWithSourceFilec                 C   s   t tdd}|tdt j |tdt j |d}|d}| |j	t j | |j	t j W d   n1 sv0    Y  dS )zUCheck that files within a Zip archive can have different
        compression options.r:   ZstoremeZ	deflatemeN)
r@   rA   r   r3   r   r   ZIP_DEFLATEDrc   rS   r   )r5   rD   sinfoZdinfor   r   r   test_per_file_compression  s    

z4DeflateTestsWithSourceFile.test_per_file_compressionN)r   r   r   r@   r	  r8   r  r   r   r   r   r    s   r  c                   @   s   e Zd ZejZdS )Bzip2TestsWithSourceFileNr   r   r   r@   	ZIP_BZIP2r8   r   r   r   r   r    s   r  c                   @   s   e Zd ZejZdS )LzmaTestsWithSourceFileNr   r   r   r@   ZIP_LZMAr8   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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   	<genexpr>  s   z;AbstractTestZip64InSmallFiles.setUpClass.<locals>.<genexpr>r      
)r'   r(   r*   r+   )r-   r)   r   r   r   r.     s    z(AbstractTestZip64InSmallFiles.setUpClassc                 C   sV   t j| _t j| _dt _dt _ttd}|| j W d    n1 sH0    Y  d S )Nr   r   r0   )	r@   ZIP64_LIMIT_limitZIP_FILECOUNT_LIMIT_filecount_limitr2   r   r3   r+   r4   r   r   r   r7     s    z#AbstractTestZip64InSmallFiles.setUpc                 C   sr  t j|d|dd6}|td |tt |d| j W d    n1 sN0    Y  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| |D ]}| |jt| j qtddfD ]2}||}| |j| | |jt| j q|  W d    n1 sd0    Y  d S )Nr:   TZ
allowZip64r;   r<   rG   rP   rJ   r   rK   rL   rM   rN   rO      c                 S   s   g | ]
}|j qS r   rQ   r"   r   r   r   r%     r&   z:AbstractTestZip64InSmallFiles.zip_test.<locals>.<listcomp>)r@   rA   r3   r   rB   r+   rS   rT   r   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rR   rd   )r5   r   r8   rD   r6   re   rf   rg   rh   ri   rj   rk   rl   r$   rm   rn   r   r   r   ro     sN    ,

z&AbstractTestZip64InSmallFiles.zip_testc                 C   s    t | D ]}| || j qd S r   rp   rq   r   r   r   rr     s    z(AbstractTestZip64InSmallFiles.test_basicc                 C   s   t jtd| jdd}d|_d}t|D ] }|d| d|d d	   q&| t|	 | |
  t td
| j}| t|	 | t|D ]0}|d| d}| |d|d d	   q|
  d S )Nr:   Tr  r      foo%08d%dr  9   rG   r    )r@   rA   r   r8   debugr'   rB   rS   rY   r`   r   rT   decode)r5   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}t|D ] }|d| d|d d	   q&| t|	 | | 
t j  |d| d
 W d    n1 s0    Y  | t|	 | |  t jtd| jdd}d|_| t|	 | | 
t j  |d| d
 W d    n1 s0    Y  | t|	 | |  t jtd| jdd}d|_| t|	 | d}t||D ]"}|d| d|d d	   qx| t|	 | |  t td| j}| t|	 | t|D ]2}|d| d}| |d|d d	   q|  d S )Nr:   Fr  r   r   r  r  r  r  r&   r   Tr  rG   r    )r@   rA   r   r8   r  r'   rB   rS   rY   r`   r   LargeZipFiler   rT   r   )r5   r   r!  r$   Z	numfiles2r"  r#  r   r   r   test_too_many_files_append  sJ    .0 z8AbstractTestZip64InSmallFiles.test_too_many_files_appendc                 C   s$   | j t_| jt_tt tt d S r   )r  r@   r  r  r  r	   r   r   r   r   r   r   r   *  s    z&AbstractTestZip64InSmallFiles.tearDownN)r   r   r   r   r.   r7   ro   rr   r$  r&  r   r   r   r   r   r    s   

9'r  c                   @   sT   e Zd ZejZdd Zdd Zdd Zdd Z	d	d
 Z
dddZdd Zdd ZdS )StoredTestZip64InSmallFilesc                 C   sJ   t j|d|dd$}| t j|jtd W d    n1 s<0    Y  d S Nr:   Fr  r;   )r@   rA   r   r%  r3   r   r5   r   r8   rD   r   r   r   large_file_exception_test5  s    z5StoredTestZip64InSmallFiles.large_file_exception_testc                 C   sL   t j|d|dd&}| t j|jd| j W d    n1 s>0    Y  d S r(  )r@   rA   r   r%  rB   r+   r)  r   r   r   large_file_exception_test2:  s    
z6StoredTestZip64InSmallFiles.large_file_exception_test2c                 C   s.   t | D ] }| |tj | |tj qd S r   )r   r*  r@   r   r+  rq   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    n1 s60    Y  t tdt j"}| | dg W d    n1 sx0    Y  d S )Nr:   Tr  r   rG   r   r   r   r   r   r   r   D  s    *z2StoredTestZip64InSmallFiles.test_absolute_arcnamesc                 C   s  t jtddd}|d| j W d    n1 s40    Y  t jtddd }|d}|j}W d    n1 st0    Y  t jtddd}|d| j W d    n1 s0    Y  t jtddd(}|d}| |j| W d    n1 s0    Y  d S )Nr:   Tr  r<   rG   r   Zstrfile2)r@   rA   r   rB   r+   rc   extrarS   )r5   rD   r   r-  r   r   r   test_appendL  s    ,
$,
z'StoredTestZip64InSmallFiles.test_appendFc                 C   s  d}d}g }	g }
|}|r4d}|r4|	 | |
 | td|}|}|rdd}|rd|	 | |
 | td|}|}|rd}|r|
 | td|}tjddt|	  ddt|	 g|	R  }tjddt|
  ddt|
 g|
R  }tdd	dt|
  }tdd
dt|	  }tdddt|	  }tdddt|
  }d}d}tdt|}d| | | | | | | d | | | | d | | | d d d | | d d d d }|S )zGenerate bytes sequence for a zip with (incomplete) zip64 data.

        The actual values (not the zip 64 0xffffffff values) stored in the file
        are:
        file_size: 8
        compress_size: 8
        header_offset: 0
           r   l    z<Lz<HHQrM   z<Q:   2   z<HrP   s   test.txt   test1234s   PK       ! %s   PK--       ! %s
           s   PK,       - -s                 s
            s   PK    l       s      s   PK      :   2 s       )ru   r  packrY   )r5   file_size_64_setfile_size_extracompress_size_64_setcompress_size_extraheader_offset_64_setheader_offset_extraZactual_sizeZactual_header_offsetZlocal_zip64_fieldsZcentral_zip64_fieldsrb   compress_sizeheader_offsetZlocal_extraZcentral_extraZcentral_dir_sizeZoffset_to_central_dirZlocal_extra_lengthZcentral_extra_lengthrR   r#  Zfilename_lengthZzip64_contentsr   r   r   make_zip64_fileZ  s    






	
!z+StoredTestZip64InSmallFiles.make_zip64_filec                 C   s  | j dd}| tj }tt| W d   n1 s>0    Y  | dt|j	
  | j dddd}| tj }tt| W d   n1 s0    Y  | dt|j	
  | j dd}| tj }tt| W d   n1 s0    Y  | dt|j	
  | j dddddd}| tj }tt| W d   n1 sf0    Y  | d	t|j	
  | j d
dddd}| tj }tt| W d   n1 s0    Y  | d	t|j	
  | j ddd
dd}| tj }tt| W d   n1 s20    Y  | d	t|j	
  | j d
d
dd}| tj }tt| W d   n1 s0    Y  | d	t|j	
  dS )a
  Missing zip64 extra records raises an exception.

        There are 4 fields that the zip64 format handles (the disk number is
        not used in this module and so is ignored here). According to the zip
        spec:
              The order of the fields in the zip64 extended
              information record is fixed, but the fields MUST
              only appear if the corresponding Local or Central
              directory record field is set to 0xFFFF or 0xFFFFFFFF.

        If the zip64 extra content doesn't contain enough entries for the
        number of fields marked with 0xFFFF or 0xFFFFFFFF, we raise an error.
        This test mismatches the length of the zip64 extra field and the number
        of fields set to indicate the presence of zip64 data.
        T)r5  Nz	file size)r5  r6  r7  zcompress size)r7  )r5  r6  r7  r8  r9  zheader offsetF)r5  r7  r8  r9  )r5  r6  r7  r9  )r5  r7  r9  )r=  r   r@   
BadZipFilerA   r   r   rZ   r_   	exceptionlower)r5   Zmissing_file_size_extraeZmissing_compress_size_extraZmissing_header_offset_extrar   r   r   test_bad_zip64_extra  sp    ...0000z0StoredTestZip64InSmallFiles.test_bad_zip64_extrac              
   C   s   d}d}d}d}dddddddddf}t dt|d D ]}t||D ]}i }|D ]}	||	 qZtt| j	f i |X}
|

 d }| |j| | |j| | |j| | |
|| W d    qN1 s0    Y  qNq>d S )	Nr/  r   r3  T)r5  r6  )r7  r8  )r9  r:  rM   )r'   rY   	itertoolscombinationsupdater@   rA   r   r   r=  ra   rS   rb   r;  r<  rT   )r5   Zexpected_file_sizeZexpected_compress_sizeZexpected_header_offsetZexpected_contentparamsrG   ZcomborC   czfr   r   r   r    test_generated_valid_zip64_extra  s&    z<StoredTestZip64InSmallFiles.test_generated_valid_zip64_extraN)FFFFFF)r   r   r   r@   r   r8   r*  r+  r,  r   r.  r=  rB  rI  r   r   r   r   r'  1  s      
c^r'  c                   @   s   e Zd ZejZdS )DeflateTestZip64InSmallFilesNr   r   r   r@   r	  r8   r   r   r   r   rJ  7  s   rJ  c                   @   s   e Zd ZejZdS )Bzip2TestZip64InSmallFilesNr  r   r   r   r   rL  <  s   rL  c                   @   s   e Zd ZejZdS )LzmaTestZip64InSmallFilesNr  r   r   r   r   rM  A  s   rM  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
AbstractWriterTestsc                 C   s   t t d S r   r	   r   r   r   r   r   r   I  s    zAbstractWriterTests.tearDownc                 C   s   d}t td| j`}|dd}|| |  | |j |  | |j | 	|
d| W d    n1 sz0    Y  d S )N   contentr:   r   )r@   rA   r   r8   r2   r3   r   r\   r   rS   rT   r5   r+   r   r:   r   r   r   test_close_after_closeL  s    
z*AbstractWriterTests.test_close_after_closec                 C   s   d}t td| j\}|dd}|| |  | |j | 	t
|jd | |d| W d    n1 sv0    Y  d S )NrP  r:   r   r&   )r@   rA   r   r8   r2   r3   r   r\   r   r   r   rS   rT   rQ  r   r   r   test_write_after_closeW  s    
z*AbstractWriterTests.test_write_after_closec              	   C   s   t  dg d}t||j }tt d| jb}|dd"}| 	|
|| W d    n1 sh0    Y  | 	|dj| W d    n1 s0    Y  d S )Nr0  )rM   r   r  rP   rI   r:   r+   )arrayrY   itemsizer@   rA   r   r   r8   r2   rS   r3   rc   rb   )r5   qZLENGTHr   r+   r   r   r   test_issue44439a  s    0z#AbstractWriterTests.test_issue44439N)r   r   r   r   rR  rS  rW  r   r   r   r   rN  G  s   
rN  c                   @   s   e Zd ZejZdS )StoredWriterTestsNr   r   r   r@   r   r8   r   r   r   r   rX  i  s   rX  c                   @   s   e Zd ZejZdS )DeflateWriterTestsNrK  r   r   r   r   rZ  l  s   rZ  c                   @   s   e Zd ZejZdS )Bzip2WriterTestsNr  r   r   r   r   r[  p  s   r[  c                   @   s   e Zd ZejZdS )LzmaWriterTestsNr  r   r   r   r   r\  t  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 |vr|  |d | d S )NorG  )rZ   )r5   namer`   r   r   r   assertCompiledInz  s    zPyZipFileTests.assertCompiledInc                 C   s~   t j|t jt jt jv ds$| d t j|d}z"t |t jt j	B }t 
| W n typ   | d Y n0 t| d S )N)Zeffective_idsz/requires write access to the installed locationztest_zipfile.try)r   accessW_OKsupports_effective_idsr  r   r*   r2   O_WRONLYO_CREATr   	Exceptionr	   )r5   r   rR   fdr   r   r   requiresWriteAccess~  s    


z"PyZipFileTests.requiresWriteAccessc              	   C   s  |  tjt t }t|d}t}|dr|	tj
}tjd ur^||	tj d|v rttj|}n|d d }|| tj|}| ||  | ||  W d    n1 s0    Y  W d    n1 s0    Y  t }t|dl}t}|dr$|d d }||d ddtj|f }| ||  | ||  W d    n1 sz0    Y  W d    n1 s0    Y  d S )Nr:   .pyc__pycache__r   Ztestpackagez%s/%s)rh  r   r   dirname__file__r   r@   	PyZipFileendswithr[   sepaltsepextend	importlibutilsource_from_cachewritepybasenameassertNotInr`   r`  )r5   trD   rg   Z
path_splitZbnr   r   r   test_write_pyfile  s,    


Lz PyZipFileTests.test_write_pyfilec              	   C   s   dd l }tj|j}| | t f}t|d:}|	| |
 }| d| | d| W d    n1 st0    Y  W d    n1 s0    Y  d S )Nr   r:   zemail/__init__.pyzemail/mime/text.py)emailr   r   rk  rl  rh  r   r@   rm  ru  r`   r`  )r5   rz  
packagedirrx  rD   rk   r   r   r   test_write_python_package  s    

z(PyZipFileTests.test_write_python_packagec                 C   s  dd l }tj|j}| | t b}t|d2}t	 }|
| W d    n1 s`0    Y  | }| d|v  t	 "}|j
|dd d W d    n1 s0    Y  | }| d|v dd }t	 J}| t |j
||d W d    n1 s0    Y  W d    n1 s.0    Y  | }|rNt| | d|v W d    n1 sr0    Y  W d    n1 s0    Y  d S )	Nr   r:   SyntaxErrorc                 S   s   dS )NFr   )Zwhateverr   r   r   <lambda>  r&   zCPyZipFileTests.test_write_filtered_python_package.<locals>.<lambda>Z
filterfuncc                 S   s   t j| d S )NZbad)r   r   rv  
startswith)r   r   r   r   filter  s    zAPyZipFileTests.test_write_filtered_python_package.<locals>.filter)r   r   r   rk  rl  rh  r   r@   rm  r   ru  rW   r\   assertWarnsUserWarningprint)r5   r   r{  rx  rD   Z	reportSIOZ	reportStrr  r   r   r   "test_write_filtered_python_package  s&    
(0Nz1PyZipFileTests.test_write_filtered_python_packagec              	   C   s   dd l }tj|j}| | dnd}d}t r}tj|d|dB}|	| |
 }| d| | | d| | W d    n1 s0    Y  W d    n1 s0    Y  d S )	Nr   TrM   ri  r:   )optimizezemail/__init__zemail/mime/text)rz  r   r   rk  rl  rh  r   r@   rm  ru  r`   rZ   )r5   rz  r{  Zoptlevelextrx  rD   rk   r   r   r   test_write_with_optimization  s    

z+PyZipFileTests.test_write_with_optimizationc              	   C   sn  t t zRtt jtdd}|d W d    n1 sB0    Y  tt jtdd}|d W d    n1 s0    Y  tt jtdd}|d W d    n1 s0    Y  t t}t	|dF}|
t | }| d| | d| | d| W d    n1 s*0    Y  W d    n1 sJ0    Y  W tt n
tt 0 d S )Nmod1.pyr:   
print(42)
mod2.pyprint(42 * 42)
zmod2.txtzbla bla bla
r   mkdirr   r2   r   r*   r3   r   r@   rm  ru  r`   r`  rw  r
   r5   r6   rx  rD   rk   r   r   r   test_write_python_directory  s    
(((
Nz*PyZipFileTests.test_write_python_directoryc              	   C   s*  t t ztt jtdd}|d W d    n1 sB0    Y  tt jtdd}|d W d    n1 s0    Y  t n}t	|dB}|j
tdd d | }| d| | d| W d    n1 s0    Y  W d    n1 s0    Y  W tt n
tt 0 d S )	Nr  r:   r  r  r  c                 S   s   |  d S )Nr  )rn  )rg   r   r   r   r~    s    zEPyZipFileTests.test_write_python_directory_filtered.<locals>.<lambda>r  r  r  r   r   r   $test_write_python_directory_filtered  s    
((Lz3PyZipFileTests.test_write_python_directory_filteredc              
   C   s   t  }t|d\}ttd}|d W d    n1 s@0    Y  | t|jt t	t W d    n1 sv0    Y  W d    n1 s0    Y  d S )Nr:   z!most definitely not a python file)
r   r@   rm  r2   r   r3   r   RuntimeErrorru  r	   )r5   rx  rD   r   r   r   r   test_write_non_pyfile  s
    (z$PyZipFileTests.test_write_non_pyfilec              
   C   s"  t t ztt jtdd}|d W d    n1 sB0    Y  t }t	|dz}t
 $}|t jtd W d    n1 s0    Y  | d|  | }| d| | d| W d    n1 s0    Y  W d    n1 s0    Y  W tt n
tt 0 d S )Nr  r:   zBad syntax in python file
r}  zmod1.pyc)r   r  r   r2   r   r*   r3   r   r@   rm  r   ru  rZ   rW   r`   rw  r
   )r5   r6   rx  rD   srk   r   r   r   test_write_pyfile_bad_syntax  s    
(2Jz+PyZipFileTests.test_write_pyfile_bad_syntaxc              	   C   s   t t ztt jtdd}|d W d    n1 s@0    Y  t d}t	|d8}|
ttd  | }| d| W d    n1 s0    Y  W d    n1 s0    Y  W tt n
tt 0 d S )Nr  r:   r  )r   r  r   r2   r   r*   r3   r   r@   rm  ru  r   r   r`   r`  r
   r  r   r   r   test_write_pathlike,  s    
(Jz"PyZipFileTests.test_write_pathlikeN)r   r   r   r`  rh  ry  r|  r  r  r  r  r  r  r  r   r   r   r   r]  y  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   sN   t tdt j*}tD ]\}}||| qW d    n1 s@0    Y  d S )Nr:   )r@   rA   r   r   SMALL_TEST_DATArB   r5   rD   fpathfdatar   r   r   make_test_file<  s    zExtractTests.make_test_filec                 C   s   t   |   ttd}tD ]\}}||}tj	t
 |}tj|}| || t|d$}| | |  W d    n1 s0    Y  t| q"W d    n1 s0    Y  W d    n1 s0    Y  d S NrG   r   )r   r  r@   rA   r   r  extractr   r   r*   getcwdnormpathrS   r2   encoderT   r	   )r5   rD   r  r  writtenfilecorrectfiler   r   r   r   test_extractA  s    
2zExtractTests.test_extractc              
   C   s   |    ttd}tD ]\}}|||}tj||}tj	|}| 
tj||||f t|d$}| | |  W d    n1 s0    Y  t| qW d    n1 s0    Y  tt d S r  )r  r@   rA   r   r  r  r   r   r*   r  r\   samefiler2   rS   r  rT   r	   )r5   targetrD   r  r  r  r  r   r   r   r   _test_extract_with_targetT  s    2(z&ExtractTests._test_extract_with_targetc                 C   s4   t  }| | W d    n1 s&0    Y  d S r   )r   r  r5   extdirr   r   r   test_extract_with_targetg  s    z%ExtractTests.test_extract_with_targetc                 C   s:   t   }| t| W d    n1 s,0    Y  d S r   )r   r  r   r   r  r   r   r   !test_extract_with_target_pathlikek  s    z.ExtractTests.test_extract_with_target_pathlikec                 C   s   t   |   ttd~}|  tD ]`\}}tj	t
 |}t|d$}| | |  W d    n1 sx0    Y  t| q*W d    n1 s0    Y  W d    n1 s0    Y  d S r  )r   r  r@   rA   r   
extractallr  r   r   r*   r  r2   rS   r  rT   r	   )r5   rD   r  r  outfiler   r   r   r   test_extract_allo  s    2zExtractTests.test_extract_allc              
   C   s   |    ttd|}|| tD ]\\}}tj||}t	|d$}| 
| |  W d    n1 sn0    Y  t| q$W d    n1 s0    Y  tt d S r  )r  r@   rA   r   r  r  r   r   r*   r2   rS   r  rT   r	   )r5   r  rD   r  r  r  r   r   r   r   _test_extract_all_with_target|  s    
2(z*ExtractTests._test_extract_all_with_targetc                 C   s4   t  }| | W d    n1 s&0    Y  d S r   )r   r  r  r   r   r   test_extract_all_with_target  s    z)ExtractTests.test_extract_all_with_targetc                 C   s:   t   }| t| W d    n1 s,0    Y  d S r   )r   r  r   r   r  r   r   r   %test_extract_all_with_target_pathlike  s    z2ExtractTests.test_extract_all_with_target_pathlikec                 C   sP   |  tj| t|d }| | | W d    n1 sB0    Y  d S )Nr   )r\   r   r   isfiler2   rS   rT   )r5   rR   r#  r   r   r   r   
check_file  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_i../../foo../../ba..r/	foo/ba..r)r@   rA   Z_sanitize_windows_namerS   )r5   Zsanr   r   r   test_sanitize_windows_name  s    z'ExtractTests.test_sanitize_windows_namec                 C   s   g d}|  | d S )N))z
../foo/barfoo/bar)z
foo/../barr  )zfoo/../../barr  )z
foo/bar/..r  )z./../foo/barr  )z/foo/barr  )z/foo/../barr  )z/foo/../../barr  _test_extract_hackers_arcnames)r5   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   s   g d}|  | dS )z>Test combination of path fixing and windows name sanitization.))z
..\foo\barr  )z..\/foo\/barr  )zfoo/\..\/barr  )zfoo\/../\barr  )z	C:foo/barr  )z
C:/foo/barr  )zC://foo/barr  )z
C:\foo\barr  )z//conky/mountpoint/foo/barr  )z\\conky\mountpoint\foo\barr  )z///conky/mountpoint/foo/barconky/mountpoint/foo/bar)z\\\conky\mountpoint\foo\barr  )z//conky//mountpoint/foo/barr  )z\\conky\\mountpoint\foo\barr  )z//?/C:/foo/barr  )z\\?\C:\foo\barr  )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)r  r  Nr  )r5   Zwindows_hacknamesr   r   r   *test_extract_hackers_arcnames_windows_only  s    z7ExtractTests.test_extract_hackers_arcnames_windows_onlyr  zRequires / as path separator.c                 C   s   g d}|  | d S )N))z	//foo/barr  )r  zfoo../ba..r)
foo/..\barr  r  )r5   Zposix_hacknamesr   r   r   (test_extract_hackers_arcnames_posix_only  s    z5ExtractTests.test_extract_hackers_arcnames_posix_onlyc           
   	   C   s>  |D ]2\}}d|   }ttdtj0}t }||_d|_||| W d    n1 s`0    Y  |	t
jd}t
jddd}t
jj|g|dR  }ttd6}|||}	| j|	|d	||	|f d
 W d    n1 s0    Y  | || td ttd}|| W d    n1 s60    Y  | || td t
jjt
 g|dR  }ttd.}||}	| j|	|d| d
 W d    n1 s0    Y  | || t|dd  ttd}|  W d    n1 s0    Y  | || t|dd  tt qd S )N   foobarr:   r   r  r  subdirZsubsubrG   zextract %r: %r != %r)msgz
extract %rr   )r  r@   rA   r   r   ZipInforR   r   rB   replacer   ro  r   r*   r[   r  rS   r  r
   r  r  r	   )
r5   Z	hacknamesZarcnameZ	fixednamer#  rD   r   
targetpathr  r  r   r   r   r    sH    *$*
&(z+ExtractTests._test_extract_hackers_arcnamesN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  unittestZskipIfr   r   ro  r  r  r  r   r   r   r   r  :  s"   

r  c                   @   sP  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&dGdH Z'dIdJ Z(e) dKdL Z*dMdN Z+dOS )P
OtherTestsc              
   C   sB  t tdt jh}|dd | t |dd W d    n1 sJ0    Y  | | dgd  W d    n1 s~0    Y  t td}|	 }d}|D ]:}|
|}|| 7 }W d    q1 s0    Y  q| |dd	h d}|D ]}|||7 }q| |dd	h W d    n1 s40    Y  d S )
Nr:   r_  r   barr   rG   r&   r  s   barfoo)r@   rA   r   r   rB   r  r  rS   r`   ra   r2   rT   rZ   )r5   rD   rl   r+   rn   r   r   r   r   test_open_via_zip_info  s     *4,z!OtherTests.test_open_via_zip_infoc                 C   s^   t td>}dD ](}t |}| jdO  _||| qW d    n1 sP0    Y  d S )Nr:   Zabcdefghijklmnopr/  )r@   rA   r   r  	flag_bitsrB   )r5   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8}tD ]"\}}||| | |jd qW d   n1 sJ0    Y  | |jd t td}| |jd W d   n1 s0    Y  | |jd dS )z8Check that the zipfile is closed after the 'with' block.r:   zzipfp is not openNzipfp is not closedrG   )r@   rA   r   r  rB   ZassertIsNotNoner6   assertIsNoner  r   r   r   
test_close  s    .,zOtherTests.test_closec                 C   s   t td*}tD ]\}}||| qW d   n1 s<0    Y  z8t td}t  W d   n1 sr0    Y  W n" t jy   | |jd Y n0 dS )zWCheck that the zipfile is closed if an exception is raised in the
        'with' block.r:   NrG   r  )r@   rA   r   r  rB   r>  r  r6   )r5   rD   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      /        rG   )r   NotImplementedErrorr@   rA   r   r   r   r   r   r   test_unsupported_version/  s    

z#OtherTests.test_unsupported_versionc                 C   sR   t d}t|,}| D ]}||  qW d    n1 sD0    Y  d S )Nzzip_cp437_header.zip)r   r@   rA   r`   r2   r   )r5   r   rD   r_  r   r   r   test_read_unicode_filenames:  s    z&OtherTests.test_read_unicode_filenamesc                 C   s   t td>}|dd |dd | | d jt W d    n1 sP0    Y  t td8}| |j	d jd | |j	d jd W d    n1 s0    Y  d S )Nr:   foo.txtzTest for unicode filenameu   ö.txtr   rG   rM   )
r@   rA   r   rB   r   ra   rR   r_   rS   filelist)r5   rH  r   r   r   test_write_unicode_filenamesB  s    4z'OtherTests.test_write_unicode_filenamesc                 C   sN   t td.}|dd | |dd W d    n1 s@0    Y  d S )Nr:   u   прикладs   sample)r@   rA   r   rB   rS   rT   r   r   r   r   'test_read_after_write_unicode_filenamesL  s    z2OtherTests.test_read_after_write_unicode_filenamesc                 C   s   t t d}d}ttdtj}||| W d   n1 sB0    Y  | t  ttdtj W d   n1 s|0    Y  ttd4}| |	 |g | |
|| W d   n1 s0    Y  dS )z&Test exclusive creating a new zipfile.testfile.txt#   hello, world. this is some content.xNrG   )r	   r   r@   rA   r   rB   r   FileExistsErrorrS   r`   rT   )r5   rR   r#  rD   r   r   r   test_exclusive_create_zip_fileQ  s    *.z)OtherTests.test_exclusive_create_zip_filec                 C   s   t jtrt t d}d}z<ttd}||| W d    n1 sN0    Y  W n tyv   | 	d Y n0 | 
t jt ttd"}| ||| W d    n1 s0    Y  d S )Nr  r  r   z1Could not append data to a non-existent zip file.rG   )r   r   existsr   r	   r@   rA   rB   r   failr\   rS   rT   )r5   rR   r#  rH  r   r   r   (test_create_non_existent_file_for_append^  s    
.z3OtherTests.test_create_non_existent_file_for_appendc                 C   s\   t td}|d W d    n1 s*0    Y  ztt}W n tjyV   Y n0 d S )Nr:   this is not a legal zip file
)r2   r   r3   r@   rA   r>  )r5   r6   rH  r   r   r   test_close_erroneous_filep  s    (z$OtherTests.test_close_erroneous_filec                 C   s   t td}|d W d   n1 s*0    Y  | tt | ttt t td }| t| W d   n1 s0    Y  t	 }|d | t| |
dd | t| dS )z;Check that is_zipfile() correctly identifies non-zip files.r:   r  Nr   s   this is not a legal zip file
r   )r2   r   r3   r   r@   
is_zipfiler   r   r   r   r   r4   r   r   r   test_is_zip_erroneous_file  s    (.
z%OtherTests.test_is_zip_erroneous_filec                 C   s   t  }tj|dd}|dd W d   n1 s80    Y  | }tt|D ](}t |d| }| tj	tj| qVdS )zCCheck that zipfiles with missing bytes at the end raise BadZipFile.r:   r>   r     O, for a Muse of Fire!N)
r   r   r@   rA   rB   rW   r'   rY   r   r>  )r5   r6   r   r   Nr   r   r   test_damaged_zipfile  s    *zOtherTests.test_damaged_zipfilec                 C   s   t jtdd}|dd W d   n1 s00    Y  | t t ttd4}| t | |dd | }W d   n1 s0    Y  t	
 }|| | t | |dd | t | dS )z7Check that is_zipfile() correctly identifies zip files.r:   r>   r  r  Nr   r   )r@   rA   r   rB   r\   r  r2   r   rT   r   r   r3   )r5   r   r6   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 r   )r   r   r@   rA   r   r   r   r   r   %test_non_existent_file_raises_OSError  s    z0OtherTests.test_non_existent_file_raises_OSErrorc                 C   sn   t td}|  | tjtjt t td}|d W d    n1 sN0    Y  | tjtjt d S )Nr:   z
short file)r2   r   r   r   r@   r>  rA   r3   )r5   r   r6   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   n1 s80    Y  | t|jd | t|jd | t|j	 | t|jdd tt
d}|d W d   n1 s0    Y  | t|jt
 dS )	z?Verify that testzip() doesn't swallow inappropriate exceptions.r:   r>   r  O, for a Muse of Fire!Nz	bogus.txtr   zzipfile test data)r   r   r@   rA   rB   r   r   rT   r2   rd   r   r3   )r5   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 )z>Check that bad modes passed to ZipFile constructor are caught.rV  N)r   r   r@   rA   r   r   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   n1 s00    Y  t jtddP}|d | t|jdd | t|jdd | t|jdd	 W d   n1 s0    Y  dS )
z7Check that bad modes passed to ZipFile.open are caught.r:   r>   r  r  NrG   rV  UZrU)r@   rA   r   rB   rT   r   r   r2   r5   r   r   r   r   test_bad_open_mode  s    *
zOtherTests.test_bad_open_modec              	   C   s   t jtddv}|dd |d@}ttD ]}| |dd q0| | d W d   n1 sl0    Y  W d   n1 s0    Y  dS )	zsCheck that calling read(0) on a ZipExtFile object returns an empty
        string and doesn't advance file pointer.r:   r>   r  r  r   r&   r  N)	r@   rA   r   rB   r2   r'   r(   rS   rT   )r5   r   r   r$   r   r   r   
test_read0  s    zOtherTests.test_read0c                 C   sD   t jtdd"}| t|jdd W d   n1 s60    Y  dS )zqCheck that attempting to call open() for an item that doesn't
        exist in the archive raises a RuntimeError.r:   r>   r  rG   N)r@   rA   r   r   KeyErrorr2   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 )zMCheck that bad compression methods passed to ZipFile.open are
        caught.r:   r   N)r   r  r@   rA   r   r   r   r   r   test_bad_compression_mode  s    z$OtherTests.test_bad_compression_modec                 C   sJ   d}t t|d }| t|jd W d    n1 s<0    Y  d S )Nsf   PK.    C@              x PK..    C@                          xPK      /   !     rG   r  )r@   rA   r   r   r   r  r2   )r5   r+   r   r   r   r   test_unsupported_compression  s    z'OtherTests.test_unsupported_compressionc                 C   sP   t jtdd.}|dd | | dg W d   n1 sB0    Y  dS )zLCheck that a filename containing a null byte is properly
        terminated.r:   r>   zfoo.txt qqqr  r  N)r@   rA   r   rB   rS   r`   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 )zACheck that ZIP internal structure sizes are calculated correctly.   .   8   r   N)rS   r@   ZsizeEndCentDirZsizeCentralDirZsizeEndCentDir64ZsizeEndCentDir64Locatorr   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   n1 s>0    Y  t jtdd}| |jd W d   n1 sz0    Y  d}t jtdd"}||_|dd W d   n1 s0    Y  t jtdd}| |j| W d   n1 s0    Y  d	d
d tdD }|d}t jtdd"}||_|dd W d   n1 s^0    Y  t jtdd}| |j| W d   n1 s0    Y  t jtddR}| 	t
 |d |_W d   n1 s0    Y  |dd W d   n1 s0    Y  t jtdd}| |j| W d   n1 sL0    Y  t jtdd"}d|_|dd W d   n1 s0    Y  t jtdd}d|_W d   n1 s0    Y  t jtdd}| |jd W d   n1 s0    Y  t jtdd"}d|_|dd W d   n1 sD0    Y  tjt}t jtdd}d|_W d   n1 s0    Y  | |tjtk t jtdd}| |jd W d   n1 s0    Y  dS )z8Check that comments on the archive are handled properly.r:   r>   r&   r  r  NrG   s9   Bravely taking to his feet, he beat a very brave retreat. c                 S   s   g | ]}d |d d  qS )r  r  r   r   r"   r   r   r   r%   0  r&   z,OtherTests.test_comments.<locals>.<listcomp>  r    s   oopss   original commentr   s   an updated comments   original comment that's longers   shorter comment)r@   rA   r   rS   r   rB   r*   r'   r  r  r  r   r   getsizer\   )r5   r   Zzipfrr   Zcomment2Zoriginal_zip_sizer   r   r   test_comments  sP    *,*,
,.*,.,&.,&zOtherTests.test_commentsc              	   C   sp   t tdt jL}|dd | t d|_W d    n1 sD0    Y  W d    n1 sb0    Y  d S )Nr:   r  r  zthis is an error)r@   rA   r   r   rB   r   	TypeErrorr   r  r   r   r   test_unicode_commentV  s    zOtherTests.test_unicode_commentc                 C   s   t tdt j"}| |j d|_W d    n1 s80    Y  t td}| |jd W d    n1 sr0    Y  d S )Nr   r   rG   )r@   rA   r   r   r   r  r   rS   r  r   r   r   $test_change_comment_in_empty_archive\  s
    $z/OtherTests.test_change_comment_in_empty_archivec                 C   s   t tdt j}|dd W d    n1 s20    Y  t tdt j"}| |j d|_W d    n1 st0    Y  t td}| |jd W d    n1 s0    Y  d S )Nr:   r  r  r   r   rG   )	r@   rA   r   r   rB   r\   r  r   rS   r  r   r   r   'test_change_comment_in_nonempty_archivec  s    *$z2OtherTests.test_change_comment_in_nonempty_archivec                 C   s   t jtdd}|  zt jtdd}W n t jyF   | d Y n0 t jtdd}|  zt jtdd}W n   | d Y n0 d S )Nr:   r>   rG   z+Unable to create empty ZIP file in 'w' moder   z+Unable to create empty ZIP file in 'a' mode)r@   rA   r   r   r>  r  r  r   r   r   test_empty_zipfilel  s    zOtherTests.test_empty_zipfilec                 C   s*   t td}|  | tjtjtd d S )Nr:   rG   )r2   r   r   r   r@   r>  rA   rq   r   r   r   test_open_empty_file}  s    
zOtherTests.test_open_empty_filec                 C   s   |  ttjdd d S )NZ	seventies)i  rM   rM   r   r   r   )r   r   r@   r  r   r   r   r   test_create_zipinfo_before_1980  s    z*OtherTests.test_create_zipinfo_before_1980c                 C   s    t jdd}| t|d dS )z=Before bpo-26185, repr() on empty ZipInfo object was failing.emptyrQ   z&<ZipInfo filename='empty' file_size=0>N)r@   r  rS   r   r5   zir   r   r   test_create_empty_zipinfo_repr  s    z)OtherTests.test_create_empty_zipinfo_reprc                 C   s   t  }| |jd | |jd | |jd | |jt j | |jd | |j	d | 
|jd | |jt j | |jt j | |jd | |jd | |jd | |jd | |jd | |jd | |jd dS )z'Ensure all required attributes are set.ZNoNamer   r&   )r   r  r   N)r@   r  rS   Zorig_filenamerR   r   r   r   r   r-  rZ   Zcreate_systemZcreate_versionZDEFAULT_VERSIONZextract_versionZreservedr  ZvolumeZinternal_attrr   rb   r;  r  r   r   r   ,test_create_empty_zipinfo_default_attributes  s"    z7OtherTests.test_create_empty_zipinfo_default_attributesc                 C   sH   d}t t|d}| |  W d   n1 s:0    Y  dS )z>If an extra field in the header is less than 4 bytes, skip it.sn   PK     @        abc   APK     @                    abc  PK      3   %     rG   N)r@   rA   r   r   r  rd   r5   r   r   r   r   r   #test_zipfile_with_short_extra_field  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    n1 sN0    Y  |jddd.}| t |jddd W d    n1 s0    Y  | t |jdd	d W d    n1 s0    Y  | t |d
d W d    n1 s0    Y  | t |t	d W d    n1 s@0    Y  | t |
  W d    n1 st0    Y  || W d    n1 s0    Y  |jddd}|| W d    n1 s0    Y  W d    n1 s0    Y  t td	Z}| |d| | |d| | |d| | | g d W d    n1 sp0    Y  d S )Ns"   It's fun to charter an accountant!s    And sail the wide accountant seas#   To find, explore the funds offshorer:   r   r>   r  handlerG   r_   ZabcderH   baz)r   r  r  )r@   rA   r   r   r2   r3   r   r   rB   rl  r   rS   rT   r`   )r5   Zmsg1Zmsg2Zmsg3r   Zw2w1r   r   r   test_open_conflicting_handles  s2    (,,,,(*Jz(OtherTests.test_open_conflicting_handlesc              	   C   s  d}| d}ttd}|d| W d    n1 s<0    Y  ttd}|dd}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	|d|||d   |dtj | 	|
 t| |dtj | 	|
 d W d    n1 s20    Y  W d    n1 sR0    Y  t }tj|dd}|d| W d    n1 s0    Y  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    n1 s0    Y  W d    n1 s0    Y  d S )	Ns   Where's Bruce?s   Brucer:   r  rG   r   rI   r>   )findr@   rA   r   rB   r2   r   r   SEEK_SETrS   r   SEEK_CURrT   SEEK_ENDrY   r   r   )r5   txtblocr   r6   r+   r   r   r   test_seek_tell  sB    
*P,zOtherTests.test_seek_tellc              	   C   s   d}t |}tj|dtjd}|dd W d    n1 sB0    Y  tdd J t| }| t	|j
d W d    n1 s0    Y  W d    n1 s0    Y  d S )Ns   PK                  r:   )r8   r      azzipfile.bz2)r   r   r@   rA   r  rB   mockZpatchr   r  r  )r5   r+   Zzip_filerH  r   r   r   )test_decompress_without_3rd_party_library  s    
*z4OtherTests.test_decompress_without_3rd_party_libraryc                 C   s   t t t t d S r   r   r   r   r   r   r      s    zOtherTests.tearDownN),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  r  r  r  r   r  r  r  r  r  r	  r
  r  r  r  r   r  r   r   r   r   r   r    sR   

	:	&
	r  c                   @   s   e Zd Zdd Zdd ZdS )AbstractBadCrcTestsc                 C   sN   | j }tjt|dd }| d|  W d   n1 s@0    Y  dS )z>Tests that files with bad CRCs return their name from testzip.rG   r>   afileN)zip_with_bad_crcr@   rA   r   r   rS   rd   r  r   r   r   test_testzip_with_bad_crc  s    z-AbstractBadCrcTests.test_testzip_with_bad_crcc              
   C   sf  | j }tjt|dd"}| tj|jd W d   n1 sB0    Y  tjt|ddL}|dd }| tj|j W d   n1 s0    Y  W d   n1 s0    Y  tjt|dd}|ddR}d|_	| tj |drqW d   n1 s0    Y  W d   n1 s80    Y  W d   n1 sX0    Y  dS )zFTests that files with bad CRCs raise a BadZipFile exception when read.rG   r>   r  Nr   )
r  r@   rA   r   r   r   r>  rT   r2   MIN_READ_SIZE)r5   r   r   Zcorrupt_filer   r   r   test_read_with_bad_crc  s    0L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   r8   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	  r8   r  r   r   r   r   r"  0  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  r8   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  r8   r  r   r   r   r   r$  M  s   r$  c                   @   s^   e Zd 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d ZdS )DecryptionTestszCheck that ZIP decryption works. Since the library does not
    support encryption at the moment, we use a pre-generated encrypted
    ZIP file.s   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   s   t td}|| j W d    n1 s,0    Y  ttd| _t td}|| j W d    n1 sp0    Y  ttd| _	d S )Nr0   rG   )
r2   r   r3   r+   r@   rA   r   r   data2zip2r4   r   r   r   r7   v  s    **zDecryptionTests.setUpc                 C   s,   | j   tt | j  tt d S r   )r   r   r   r	   r   r'  r   r   r   r   r   r   ~  s    


zDecryptionTests.tearDownc                 C   s(   |  t| jjd |  t| jjd d S )Ntest.txtzero)r   r  r   rT   r'  r   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   perlr(  r)  )r   setpasswordr   r  rT   r'  r   r   r   r   test_bad_password  s    z!DecryptionTests.test_bad_passwordc                 C   sH   | j d | | j d| j | jd | | jd| j d S )N   pythonr(  s   12345r)  )r   r+  rS   rT   plainr'  plain2r   r   r   r   test_good_password  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 )Nunicoder(  Zpython)pwd)r   r  r   r+  rT   r2   r  r   r   r   r   test_unicode_password  s    z%DecryptionTests.test_unicode_passwordc                 C   s  | j d | j}d}||}t|}| j dd:}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	||||||   |j}d|_d|_d|_|dtj | 	|
 d ||tj | 	||||||   ||_|dtj | 	|
 t| |dtj | 	|
 d |  W d    n1 sz0    Y  d S )Nr-  s
   encryptionr(  rG   r   rM   r&   )r   r+  r.  r  rY   r2   r   r   r  rS   r   r  rT   r  Z_readbufferZ_offsetr  )r5   r  Z	test_wordr  Zbloc_lenr6   Zold_read_sizer   r   r   r    s6    
zDecryptionTests.test_seek_tellN)r   r   r   __doc__r+   r&  r.  r/  r7   r   r*  r,  r   r0  r3  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   @   r   rM   r&   c                 s   s&   | ]}t d t tdd V  qdS )z<fir   N)r  r4  r   r   r"   r   r   r   r    s   z@AbstractTestsWithRandomBinaryFiles.setUpClass.<locals>.<genexpr>)r   r*   r'   r+   )r-   Z	datacountr   r   r   r.     s    
z-AbstractTestsWithRandomBinaryFiles.setUpClassc                 C   s:   t td}|| j W d    n1 s,0    Y  d S r/   r1   r4   r   r   r   r7     s    z(AbstractTestsWithRandomBinaryFiles.setUpc                 C   s   t t t t d S r   r   r   r   r   r   r     s    z+AbstractTestsWithRandomBinaryFiles.tearDownc                 C   sJ   t |d|(}|td |tt W d    n1 s<0    Y  d S )Nr:   r;   )r@   rA   r3   r   r)  r   r   r   rF     s    z4AbstractTestsWithRandomBinaryFiles.make_test_archivec                 C   s   |  || t|d|R}|t}| t|t| j | || j | |d| j W d    n1 sr0    Y  d S )NrG   r;   )rF   r@   rA   rT   r   rS   rY   r+   )r5   r   r8   rD   testdatar   r   r   ro     s    
z+AbstractTestsWithRandomBinaryFiles.zip_testc                 C   s    t | D ]}| || j qd S r   rp   rq   r   r   r   	test_read  s    z,AbstractTestsWithRandomBinaryFiles.test_readc              	   C   s2  |  || t|d| }g }|t,}|d}|s>qJ|| q.W d    n1 s^0    Y  g }|d,}|d}|sq|| qxW d    n1 s0    Y  d|}	| t	|	t	| j
 | |	| j
 d|}
| t	|
t	| j
 | |
| j
 W d    n1 s$0    Y  d S rs   )rF   r@   rA   r2   r   rT   ru   r*   rS   rY   r+   )r5   r   r8   rD   rv   rw   rx   ry   rz   Z	testdata1Z	testdata2r   r   r   r{     s(    
*
*

z0AbstractTestsWithRandomBinaryFiles.zip_open_testc                 C   s    t | D ]}| || j qd S r   r|   rq   r   r   r   r}   	  s    z,AbstractTestsWithRandomBinaryFiles.test_openc              	   C   s   |  || t|d|}g }|t2}|tdd}|sBqN|| q,W d    n1 sb0    Y  d|}| 	t
|t
| j | 	|| j W d    n1 s0    Y  d S r   )rF   r@   rA   r2   r   rT   r   ru   r*   rS   rY   r+   )r5   r   r8   rD   rv   rw   rx   r8  r   r   r   r   	  s    *
z7AbstractTestsWithRandomBinaryFiles.zip_random_open_testc                 C   s    t | D ]}| || j qd S r   r   rq   r   r   r   r   	  s    z3AbstractTestsWithRandomBinaryFiles.test_random_openN)r   r   r   r   r.   r7   r   rF   ro   r9  r{   r}   r   r   r   r   r   r   r5    s   

r5  c                   @   s   e Zd ZejZdS ) StoredTestsWithRandomBinaryFilesNrY  r   r   r   r   r:  	  s   r:  c                   @   s   e Zd ZejZdS )!DeflateTestsWithRandomBinaryFilesNrK  r   r   r   r   r;  "	  s   r;  c                   @   s   e Zd ZejZdS )Bzip2TestsWithRandomBinaryFilesNr  r   r   r   r   r<  '	  s   r<  c                   @   s   e Zd ZejZdS )LzmaTestsWithRandomBinaryFilesNr  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   )r6   offsetr4   r   r   r   __init__4	  s    zTellable.__init__c                 C   s   | j |}|  j|7  _|S r   )r6   r3   r@  )r5   r+   nr   r   r   r3   8	  s    zTellable.writec                 C   s   | j S r   )r@  r   r   r   r   r   =	  s    zTellable.tellc                 C   s   | j   d S r   r6   flushr   r   r   r   rD  @	  s    zTellable.flushN)r   r   r   rA  r3   r   rD  r   r   r   r   r>  3	  s   r>  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
Unseekablec                 C   s
   || _ d S r   )r6   r4   r   r   r   rA  D	  s    zUnseekable.__init__c                 C   s   | j |S r   )r6   r3   r   r   r   r   r3   G	  s    zUnseekable.writec                 C   s   | j   d S r   rC  r   r   r   r   rD  J	  s    zUnseekable.flushN)r   r   r   rA  r3   rD  r   r   r   r   rE  C	  s   rE  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )UnseekableTestsc                 C   sp  dd t tfD ]Z}| j|d6 t }|d t|}t||dtj	(}|
dd |
dd	 W d    n1 s0    Y  | | d d
 d tj|dd}|d }| | d W d    n1 s0    Y  |d }| | d	 W d    n1 s 0    Y  W d    n1 s@0    Y  W d    q1 s`0    Y  qd S )Nc                 S   s   | S r   r   r   r   r   r   r~  O	  r&   z/UnseekableTests.test_writestr.<locals>.<lambda>wrapper   abcr:   ones   111twos   222rI      abcPKrG   r>   )r>  rE  subTestr   r   r3   BufferedWriterr@   rA   r   rB   rS   rW   r2   rT   )r5   rI  r   bfrD   r   zopenr   r   r   test_writestrN	  s    

*.zUnseekableTests.test_writestrc                 C   s  dd t tfD ]}| j|d t }|d t|}t||dtj	}| 
tt ttd}|d W d    n1 s0    Y  |td ttd}|d	 W d    n1 s0    Y  |td
 W d    n1 s0    Y  | | d d d tj|dd}|d }| | d W d    n1 sZ0    Y  |d
 }| | d	 W d    n1 s0    Y  W d    n1 s0    Y  W d    q1 s0    Y  qd S )Nc                 S   s   | S r   r   rG  r   r   r   r~  _	  r&   z,UnseekableTests.test_write.<locals>.<lambda>rH  rJ  r:   r0   rL  rK  rN  rM  rI   rO  rG   r>   )r>  rE  rP  r   r   r3   rQ  r@   rA   r   
addCleanupr	   r   r2   rS   rW   rT   )r5   rI  r   rR  rD   f2r   rS  r   r   r   
test_write^	  s&    

((*0zUnseekableTests.test_writec                 C   sn  dd t tfD ]X}| j|d4 t }|d t|}t||dtj	|}|
dd}|d W d    n1 s0    Y  |
dd}|d	 W d    n1 s0    Y  W d    n1 s0    Y  | | d d
 d t|4}| |dd | |dd	 W d    n1 s>0    Y  W d    q1 s^0    Y  qd S )Nc                 S   s   | S r   r   rG  r   r   r   r~  t	  r&   z1UnseekableTests.test_open_write.<locals>.<lambda>rH  rJ  r:   rK  rL  rM  rN  rI   rO  )r>  rE  rP  r   r   r3   rQ  r@   rA   r   r2   rS   rW   rT   )r5   rI  r   rR  r   rS  r   r   r   test_open_writes	  s    

(FzUnseekableTests.test_open_writeN)r   r   r   rT  rW  rX  r   r   r   r   rF  M	  s   rF  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 )NrL  '  rN  )r   data1r&  r,   r   r   r   r.   	  s    z!TestsWithMultipleOpens.setUpClassc                 C   sP   t |dt j,}|d| j |d| j W d    n1 sB0    Y  d S )Nr:   rK  rM  )r@   rA   r	  rB   r[  r&  )r5   r   rD   r   r   r   rF   	  s    z(TestsWithMultipleOpens.make_test_archivec                 C   s   t | D ]}| | tj|dd}|df}|d<}|d}|d}|| 7 }|| 7 }W d    n1 s~0    Y  W d    n1 s0    Y  | || | || j W d    q1 s0    Y  qd S )NrG   r>   rK    )r   rF   r@   rA   r2   rT   rS   r[  r5   r   r   zopen1zopen2r[  r&  r   r   r   test_same_file	  s    


Hz%TestsWithMultipleOpens.test_same_filec                 C   s   t | D ]}| | tj|dd}|df}|d<}|d}|d}|| 7 }|| 7 }W d    n1 s~0    Y  W d    n1 s0    Y  | || j | || j W d    q1 s0    Y  qd S )NrG   r>   rK  rM  r\  	r   rF   r@   rA   r2   rT   rS   r[  r&  r]  r   r   r   test_different_file	  s    


Hz*TestsWithMultipleOpens.test_different_filec                 C   s   t | D ]}| | tj|dd}|df}|d}|d2}|d}|| 7 }|| 7 }W d    n1 s~0    Y  W d    n1 s0    Y  | || j | || j W d    q1 s0    Y  qd S )NrG   r>   rK  r\  rM  ra  )r5   r   r   r^  r[  r_  r&  r   r   r   test_interleaved	  s    


Hz'TestsWithMultipleOpens.test_interleavedc              
   C   s   t | D ]}| | t }t|d0}||d}||d}W d    n1 sb0    Y  |d}|d}|| 7 }|| 7 }W d    n1 s0    Y  | 	|| j
 | 	|| j qd S )NrG   rK  rM  r\  )r   rF   
contextlib	ExitStackr@   rA   enter_contextr2   rT   rS   r[  r&  )r5   r   stackr   r^  r_  r[  r&  r   r   r   test_read_after_close	  s    

.

*z,TestsWithMultipleOpens.test_read_after_closec              
   C   s  t | D ]}t|dtj`}|d| j |d| j |d}|d}W d    n1 sd0    Y  W d    n1 s0    Y  | 	|| jd d  t|d$}|d}|d}W d    n1 s0    Y  | 	|| j | 	|| j qd S )Nr:   rK  rM  r\  rG   )
r   r@   rA   r	  rB   r[  r&  r2   rT   rS   r5   r   r   r^  r[  r&  r   r   r   test_read_after_write	  s    F
(z,TestsWithMultipleOpens.test_read_after_writec              
   C   s   t | D ]}t|dtj`}|d| j |d(}|d |d| j W d    n1 sd0    Y  W d    n1 s0    Y  t|d$}|d}|d}W d    n1 s0    Y  | 	|| j | 	|| j qd S )Nr:   rK  r\  rM  rG   )
r   r@   rA   r	  rB   r[  r2   rT   r&  rS   ri  r   r   r   test_write_after_read	  s    
J
(z,TestsWithMultipleOpens.test_write_after_readc              
   C   s   |  t tjtddR}tdD ]8}|d |d}W d    q"1 sP0    Y  q"W d    n1 sp0    Y  ttj }| 	|
 d W d    n1 s0    Y  d S )NrG   r>   r   rK  )rF   r   r@   rA   r'   rT   r2   r   devnullZ
assertLessfileno)r5   r   r  r^  r   r   r   r   test_many_opens	  s    

>z&TestsWithMultipleOpens.test_many_opensc              
   C   s:  t tdt j}|d| j W d    n1 s40    Y  t tdt j}|dd^}|d}|dd}|| j	 W d    n1 s0    Y  || 7 }W d    n1 s0    Y  W d    n1 s0    Y  | 
|| j t t$}| 
|d| j	 W d    n1 s,0    Y  d S )Nr:   rK  r   rG   r\  rM  )r@   rA   r   r	  rB   r[  r2   rT   r3   r&  rS   )r5   r   Zr1r[  r  r   r   r   test_write_while_reading	  s    ,
*Hz/TestsWithMultipleOpens.test_write_while_readingc                 C   s   t t d S r   rO  r   r   r   r   r   	  s    zTestsWithMultipleOpens.tearDownN)r   r   r   r   r.   rF   r`  rb  rc  rh  rj  rk  rn  ro  r   r   r   r   r   rY  	  s   
rY  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 r   )r   r  r   r   r   r   r   r7   
  s    zTestWithDirectory.setUpc              
   C   s   t td}|t W d    n1 s.0    Y  | tjtj	td | tjtj	tdd | tj
tj	tddd d S )N
zipdir.zipr   brG  )r@   rA   r   r  r   r\   r   r   isdirr*   r  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  r   r*   r   rt  r   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    n1 s0    Y  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    n1 s0    Y  d S )Nr  r  r:   r   z/x/r6  yrM   zy/rG   r  r   )r   r   r*   r   r  r  st_moder@   rA   r   r3   r  r\   rR   rn  rS   r   r  rs  rY   listdir)r5   dirpathr?   r   r   r  r   r   r   test_write_dir
  s0    



4



z TestWithDirectory.test_write_dirc              	   C   s   t t jtd ttdB}|dd |j	d }| 
|jd | 
|jd W d    n1 sh0    Y  ttd}|j	d }| |jd | 
|jd t jtd}t | || | t jt j|d | 
t |dg W d    n1 s0    Y  d S )	Nr  r:   zx/r&   r   i ArG   r  )r   r  r   r*   r   r@   rA   r   rB   r  rS   rR   r   r\   rn  r  rs  ry  )r5   r   r   r  r   r   r   test_writestr_dir,
  s    
,


z#TestWithDirectory.test_writestr_dirc                 C   s    t t tjtrtt d S r   )r
   r   r   r   r  r   r	   r   r   r   r   r   =
  s    zTestWithDirectory.tearDownN)	r   r   r   r7   rt  rv  r{  r|  r   r   r   r   r   rp  
  s   rp  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@   r  	from_filerl  rS   	posixpathrv  rR   r   is_dirrb   r   r   r  r  r   r   r   test_from_fileD
  s    zZipInfoTests.test_from_filec                 C   sN   t jtt}| t|j	d | 
|  | |jtjt d S r~  )r@   r  r  r   r   rl  rS   r  rv  rR   r   r  rb   r   r   r  r  r   r   r   test_from_file_pathlikeJ
  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@   r  r  r   fsencoderl  rS   r  rv  rR   r   r  rb   r   r  r  r   r   r   test_from_file_bytesP
  s    z!ZipInfoTests.test_from_file_bytesc                 C   sx   t tdZ}tj| d}| t|j	d | 
|  | |jtjt W d    n1 sj0    Y  d S )Nr   r   )r2   rl  r@   r  r  rm  rS   r  rv  rR   r   r  rb   r   r   r  )r5   r   r  r   r   r   test_from_file_filenoV
  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   r   rk  abspathrl  r@   r  r  rS   rR   r\   r  r   r   rb   )r5   rz  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}  C
  s
   r}  c                   @   sJ   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   s2   t jddg|R i |\}}}|tj dS )N-mr@   r  )r   Zassert_python_okr  r   linesepr  )r5   argsrC   rcouterrr   r   r   
zipfilecmdh
  s    zCommandLineTest.zipfilecmdc                 G   s   t jddg|R  S )Nr  r@   )r   Zassert_python_failure)r5   r  r   r   r   zipfilecmd_failurem
  s    z"CommandLineTest.zipfilecmd_failurec                 C   s|   |   \}}}| |d | d|  | d|  | d|  |  dd\}}}| |d | | d d S )Nr&   s   usages   errors   required-lr  )r  rS   rZ   r@  ZassertNotEqualstrip)r5   r  r  r  r   r   r   test_bad_usep
  s    zCommandLineTest.test_bad_usec                 C   sX   t d}dD ] }| ||}| | d qt d}| d|\}}}| |d d S )Nrq  )-tz--tests   Done testingztesttar.tarr  r&   )r   r  rS   rstripr  )r5   zip_nameoptr  r  r  r   r   r   test_test_commandz
  s    z!CommandLineTest.test_test_commandc                 C   s   t d}t }t|d}|| W d    n1 s<0    Y  | dd}dD ] }| j||dd}| 	|| qZd S )Nrq  rG   r    backslashreplace)r  z--listzascii:backslashreplace)ZPYTHONIOENCODING)
r   r   rU   r@   rA   rV   rW   r  r  rS   )r5   r  rx  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    n1 s60    Y  tt |  tt ttj	
tdd}|d W d    n1 s0    Y  ttg}ttd td g}dD ]}z| j|tg|R  }| |d ttL}| | | | ||d	 d
 | ||d d W d    n1 s60    Y  W tt qtt 0 qd S )Nr:   ztest 1zfile.txtztest 2r  z	/file.txt)z-cz--creater&   r   s   test 1r   s   test 2)rU  r	   r   r2   r3   r   r  	TESTFNDIRr
   r   r*   r  r   rS   r@   rA   r`   rT   )r5   r   filesr`   r  r  rH  r   r   r   test_create_command
  s$    (
(8z#CommandLineTest.test_create_commandc           	      C   s  t d}dD ]}t }| |||}| |d t|}| D ]}tj	||j
dtj}| r| tj| qH| tj| t|d&}| | || W d    qH1 s0    Y  qHW d    n1 s0    Y  W d    q1 s
0    Y  qd S )Nrq  )z-ez	--extractr&   r  r   )r   r   r  rS   r@   rA   ra   r   r   r*   rR   r  ro  r  r\   rs  r  r2   rT   )	r5   r  r  r  r  rH  r  r   r   r   r   r   test_extract_command
  s    
z$CommandLineTest.test_extract_commandN)r   r   r   r  r  r  r  r  r   r  r  r   r   r   r   r  f
  s   
	
r  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Ze	e
jd
e	edejddd Ze	e
jd
e	edejddd ZdS )TestExecutablePrependedZipz@Test our ability to open zip files with an executable prepended.c                 C   s    t ddd| _t ddd| _d S )NZexe_with_zipZziptestdata)r  Zexe_with_z64)r   exe_zip	exe_zip64r   r   r   r   r7   
  s    z TestExecutablePrependedZip.setUpc                 C   sj   |  t|d|  t|4}| D ]}||}| d| q,W d    n1 s\0    Y  d S )Nzis_zipfile failed on s   FAVORITE_NUMBER)r\   r@   r  rA   r`   rT   rZ   )r5   r_  rD   rB  r+   r   r   r   _test_zip_works
  s    
z*TestExecutablePrependedZip._test_zip_worksc                 C   s   |  | j d S r   )r  r  r   r   r   r    test_read_zip_with_exe_prepended
  s    z;TestExecutablePrependedZip.test_read_zip_with_exe_prependedc                 C   s   |  | j d S r   )r  r  r   r   r   r   "test_read_zip64_with_exe_prepended
  s    z=TestExecutablePrependedZip.test_read_zip64_with_exe_prependedzsys.executable required.z	/bin/bashz#Test relies on #!/bin/bash working.c                 C   s"   t | jtjg}| d| d S Ns   number in executable: 5)
subprocesscheck_outputr  sys
executablerZ   r5   outputr   r   r   test_execute_zip2
  s    z,TestExecutablePrependedZip.test_execute_zip2c                 C   s"   t | jtjg}| d| d S r  )r  r  r  r  r  rZ   r  r   r   r   test_execute_zip64
  s    z-TestExecutablePrependedZip.test_execute_zip64N)r   r   r   r4  r7   r  r  r  r  Z
skipUnlessr  r  r   ra  X_OKr  r  r   r   r   r   r  
  s   
r  c                   @   s   e Zd ZG dd dZdS )jaracoc                   @   s   e Zd ZG dd dZdS )zjaraco.itertoolsc                   @   s$   e Zd Zdd Zdd Zdd ZdS )zjaraco.itertools.Counterc                 C   s   d| _ t|| _d S r?  )r   iter
_orig_iter)r5   r$   r   r   r   rA  
  s    z!jaraco.itertools.Counter.__init__c                 C   s   | S r   r   r   r   r   r   __iter__
  s    z!jaraco.itertools.Counter.__iter__c                 C   s   t | j}|  jd7  _|S r   )nextr  r   )r5   resultr   r   r   __next__
  s    
z!jaraco.itertools.Counter.__next__N)r   r   r   rA  r  r  r   r   r   r   Counter
  s   r  N)r   r   r   r  r   r   r   r   rC  
  s   rC  N)r   r   r   rC  r   r   r   r   r  
  s   r  c                 C   s&   t j|  D ]}| |d q| S )z}
    Given a writable zip file zf, inject directory entries for
    any directories implied by the presence of children.
    r&   )r@   CompleteDirs_implied_dirsr`   rB   )rH  r_  r   r   r   add_dirs
  s    r  c                  C   sZ   t  } t| d}|dd |dd |dd |dd	 |d
d d|_|S )u:  
    Create a zip file with this structure:

    .
    ├── a.txt
    ├── b
    │   ├── c.txt
    │   ├── d
    │   │   └── e.txt
    │   └── f.txt
    └── g
        └── h
            └── i.txt

    This fixture has the following key characteristics:

    - a file at the root (a)
    - a file two levels deep (b/d/e)
    - multiple files in a directory (b/c, b/f)
    - a directory containing only a directory (g/h)

    "alpha" because it uses alphabet
    "rep" because it's a representative example
    r:   r      content of azb/c.txts   content of cz	b/d/e.txts   content of ezb/f.txts   content of fz	g/h/i.txts   content of izalpharep.zip)r   r   r@   rA   rB   rR   )r+   rH  r   r   r   build_alpharep_fixture
  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dd Zdd Zdd ZdZd d! Zd"d# Zd$d% Zd&S )'TestPathc                 C   s   t  | _| | jj d S r   )rd  re  fixturesrU  r   r   r   r   r   r7      s    
zTestPath.setUpc                 c   sh   |    t V  W d    n1 s&0    Y  |    tt V  W d    n1 sZ0    Y  d S r   )rP  r  r  r   r   r   r   zipfile_alpharep$  s    
&
zTestPath.zipfile_alpharepc              	   c   s|   t | jt }|  D ]Z}|j}|  ||j }|	d}|
|  W d    n1 sf0    Y  |V  qd S r/   )r   r   r  rf  r   r  r6   r   rR   r2   r3   rW   )r5   Ztmpdiralpharepbufferr   strmr   r   r   zipfile_ondisk*  s    
,zTestPath.zipfile_ondiskc                 C   s   |   D ]}t|}| s"J | \}}}| s<J | sHJ | sTJ | \}}}| rr| svJ | \}	|	 sJ | \}
|
 \}| sJ qd S r   )r  r@   r   r  iterdiris_file)r5   r  rootr   rr  grG  r   r   rA  hr$   r   r   r   test_iterdir_and_types4  s    



zTestPath.test_iterdir_and_typesc                 C   s\   |   D ]N}t|}|d  s&J |d  s6J |d  sFJ |d  sJ qd S )Nrr  b/r  zg/)r  r@   r   r  r5   r  r  r   r   r   test_subdir_is_dirD  s    
zTestPath.test_subdir_is_dirc              	   C   sf   |   D ]X}t|}| \}}}| }| }W d    n1 sJ0    Y  |dksJ qd S )Ncontent of a)r  r@   r   r  r2   rT   )r5   r  r  r   rr  r  r  r+   r   r   r   r}   L  s    

&zTestPath.test_openc                 C   sJ   |   D ]<}t|}| \}}}| dks4J | dksJ qd S )Nr  r  )r  r@   r   r  	read_text
read_bytes)r5   r  r  r   rr  r  r   r   r   r9  T  s
    
zTestPath.test_readc                 C   sX   |   D ]J}t|}|d}| s,J |ddd}| dksJ qd S Nr   rr  r   ze.txtzcontent of e)r  r@   r   joinpathr  r  r5   r  r  r   rA  r   r   r   test_joinpath[  s    

zTestPath.test_joinpathc                 C   sP   |   D ]B}t|}|d }| s*J |d d d }| dksJ qd S r  )r  r@   r   r  r  r  r   r   r   test_traverse_truedivc  s    
zTestPath.test_traverse_truedivc                 C   s*   |   D ]}tt|}t| qdS )zN
        zipfile.Path should be constructable from a path-like object
        N)r  r   r   r_   r@   )r5   r  Zpathliker   r   r   test_pathlike_constructionk  s    z#TestPath.test_pathlike_constructionc                 C   s*   |   D ]}t|}|td  qd S ru  )r  r@   r   r   r  r   r   r   test_traverse_pathlikes  s    
zTestPath.test_traverse_pathlikec                 C   sH   |   D ]:}t|}|d jjdks*J |d d jjdksJ qd S )Nr   r  rr  za/r  r@   r   parentZatr  r   r   r   test_parentx  s    
zTestPath.test_parentc                 C   sD   |   D ]6}t|}|d jjdks*J |d jjdksJ qd S )Nrr  r  r  r  r  r   r   r   test_dir_parent~  s    
zTestPath.test_dir_parentc                 C   s0   |   D ]"}t|}|d jjdksJ qd S )Nzmissing dir/r  r  r  r   r   r   test_missing_dir_parent  s    
z TestPath.test_missing_dir_parentc                 C   s   |   D ]}t|}| \}}}|dd |dd tdd | D sVJ |d  dksjJ |d  \}| dksJ qdS )	zk
        If the underlying zipfile is changed, the Path object should
        reflect that change.
        r  r   zbar/baz.txtr  c                 s   s   | ]}|j d kV  qdS )r  N)r_  )r#   childr   r   r   r    s   z+TestPath.test_mutability.<locals>.<genexpr>r  N)r  r@   r   r  rB   anyr  )r5   r  r  r   rr  r  r  r   r   r   test_mutability  s    
zTestPath.test_mutabilityi    c                 C   s@   t  }t|d}ttt| jD ]}||| q$d|_	|S )zACreate a read-only zipfile with a huge number of entries entries.r:   rG   )
r   r   r@   rA   mapr_   r'   HUGE_ZIPFILE_NUM_ENTRIESrB   r?   )r5   r  rH  entryr   r   r   huge_zipfile  s    zTestPath.huge_zipfilec                 C   sF   t |  }tj| }|D ]}|d q"|j| j	ksBJ dS )zE
        Ensure joinpath on items in zipfile is linear time.
        suffixN)
r@   r   r  r  rC  r  r  r  r   r  )r5   r  entriesr  r   r   r   test_joinpath_constant_time  s
    z$TestPath.test_joinpath_constant_timec                 C   s"   dd t dD }tj| d S )Nc                 S   s    g | ]}d  tjt| qS )r  )r*   stringascii_lowercaser_   )r#   rB  r   r   r   r%     r&   z:TestPath.test_implied_dirs_performance.<locals>.<listcomp>rZ  )r'   r@   r  r  r   r   r   r   test_implied_dirs_performance  s    z&TestPath.test_implied_dirs_performanceN)r   r   r   r7   r  r  r  r  r}   r9  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s&   
	r  __main__)WrT  rd  importlib.utilrr  r   rC  r   r   r  r  r  r  r  r]   r  Zunittest.mockr  r@   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   ZTestCaser   r  r  r  r  r'  rJ  rL  rM  rN  rX  rZ  r[  r\  r]  r  r  r  r!  r"  r#  r$  r%  r5  r:  r;  r<  r=  r>  rE  rF  rY  rp  r}  r  r  tupleZconsumer  r  r  r  r   mainr   r   r   r   <module>   s   0	    C   " B A    g[
7}A#P'
$ 
