o
    g                    @   sl  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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 d dl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Z0G dd de0ej+Z1e G dd de0ej+Z2e G dd de0ej+Z3e G d d! d!e0ej+Z4G d"d# d#Z5G d$d% d%e5ej+Z6e G d&d' d'e5ej+Z7e G d(d) d)e5ej+Z8e G d*d+ d+e5ej+Z9G d,d- d-ej+Z:G d.d/ d/ej+Z;G d0d1 d1ej+Z<G d2d3 d3Z=G d4d5 d5e=ej+Z>e G d6d7 d7e=ej+Z?e G d8d9 d9e=ej+Z@e G d:d; d;e=ej+ZAG d<d= d=ej+ZBG d>d? d?ZCG d@dA dAeCej+ZDe G dBdC dCeCej+ZEe G dDdE dEeCej+ZFe G dFdG dGeCej+ZGG dHdI dIZHG dJdK dKZIG dLdM dMej+ZJe G dNdO dOej+ZKG dPdQ dQej+ZLG dRdS dSej+ZMG dTdU dUej+ZNG dVdW dWej+ZOePZQG dXdY dYZRdZd[ ZSd\d] ZTd^d_ ZUG d`da daej+ZVeWdbkr4eX  dS dS )c    N)TemporaryFile)randintrandom	randbytes)script_helper)findfilerequires_zlibrequires_bz2requires_lzmacaptured_stdout)TESTFNunlinkrmtreetemp_dirtemp_cwd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 sw   Y  t }|V  | |j W d    d S 1 s9w   Y  d S N)TESTFN2r   assertFalseclosedioBytesIO)testf r   G/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_zipfile.py	get_files&   s   
"r   c                   @   s  e Zd Zedd Zdd ZdBddZdBd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 )C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>2   s    z:AbstractTestsWithSourceFile.setUpClass.<locals>.<listcomp>    )rangeFIXEDTEST_SIZEline_genjoindataclsr   r   r   
setUpClass0   s   z&AbstractTestsWithSourceFile.setUpClassc                 C   <   t td}|| j W d    d S 1 sw   Y  d S Nwbopenr   writer+   selffpr   r   r   setUp7      "z!AbstractTestsWithSourceFile.setUpNc              	   C   s   ||d}t j|dfi |F}|td |tt |d| j |jddd}| jD ]}|| q.W d    n1 s@w   Y  W d    d S W d    d S 1 sXw   Y  d S )N)compressioncompresslevelwanother.namestrfilewritten-open-wmode)zipfileZipFiler4   r   writestrr+   r3   r)   )r6   r   r:   r;   kwargszipfpliner   r   r   make_test_archive<   s   

"z-AbstractTestsWithSourceFile.make_test_archivec                 C   sB  |  ||| 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 ]}||}| |j| | |jt| j q|  W d    d S 1 sw   Y  d S )Nrr=   r>   )file   	File Namer   ModifiedSize   %Y-%m-%d%H:%M:%S   r?   c                 S      g | ]}|j qS r   filenamer"   r   r   r   r%   k       z8AbstractTestsWithSourceFile.zip_test.<locals>.<listcomp>)rH   rB   rC   assertEqualreadr   r+   r   StringIOprintdirgetvalue
splitlineslenassertInsplit
assertTruetimestrptimestrnamelistinfolist	file_sizegetinforU   testzip)r6   r   r:   r;   rF   r7   	directorylinesfndatetime_sizenamesinfosr$   nminfor   r   r   zip_testG   sN   

$z$AbstractTestsWithSourceFile.zip_testc                 C       t | D ]	}| || j qd S r   r   rs   r:   r6   r   r   r   r   
test_basic}      z&AbstractTestsWithSourceFile.test_basicc           	   	   C   s   |  || t|d|i}g }|t}	 |d}|sq%|| qW d    n1 s/w   Y  g }|d}	 |d}|sEqK|| q=W d    n1 sUw   Y  | d|| j	 | d|| j	 W d    d S 1 syw   Y  d S NrI   T   r=   r&   )
rH   rB   rC   r3   r   rX   appendrW   r*   r+   )	r6   r   r:   rF   zipdata1zipopen1	read_datazipdata2zipopen2r   r   r   zip_open_test   s2   



"z)AbstractTestsWithSourceFile.zip_open_testc                 C   rt   r   r   r   r:   rv   r   r   r   	test_open   rx   z%AbstractTestsWithSourceFile.test_openc                 C   s\   t t}| || j t|d| j}| |jt	 W d    d S 1 s'w   Y  d S NrI   )
pathlibPathr   r   r:   rB   rC   assertIsInstancerU   rc   )r6   pathrF   r   r   r   test_open_with_pathlike   s
   
"z3AbstractTestsWithSourceFile.test_open_with_pathlikec              	   C   s   |  || t|d|<}g }|t}	 |tdd}|s"q(|| qW d    n1 s2w   Y  | d	|| j
 W d    d S 1 sLw   Y  d S NrI   TrO      r&   )rH   rB   rC   r3   r   rX   r   r{   rW   r*   r+   )r6   r   r:   rF   r|   r}   r~   r   r   r   zip_random_open_test   s   
"z0AbstractTestsWithSourceFile.zip_random_open_testc                 C   rt   r   r   r   r:   rv   r   r   r   test_random_open   rx   z,AbstractTestsWithSourceFile.test_random_openc              	   C   s   |  || t|d.}|t}g }	 |d}|sq$|| qW d    n1 s.w   Y  W d    n1 s=w   Y  | d|| j	 d S )NrI   Tr&   )
rH   rB   rC   r3   r   read1r{   rW   r*   r+   r6   r   r:   rF   zipopenzipdatar~   r   r   r   zip_read1_test   s"   

 	z*AbstractTestsWithSourceFile.zip_read1_testc                 C   rt   r   )r   r   r:   rv   r   r   r   
test_read1   rx   z&AbstractTestsWithSourceFile.test_read1c              	   C   s   |  || t|d6}|t!}g }	 |d}| t|d |s&q,|| qW d    n1 s6w   Y  W d    n1 sEw   Y  | 	d
|| j d S )NrI   T
   r&   )rH   rB   rC   r3   r   r   ZassertLessEqualr]   r{   rW   r*   r+   r   r   r   r   zip_read1_10_test   s$   

 
z-AbstractTestsWithSourceFile.zip_read1_10_testc                 C   rt   r   )r   r   r:   rv   r   r   r   test_read1_10   rx   z)AbstractTestsWithSourceFile.test_read1_10c              	   C   s   |  || t|d8}|t#}d}	 | }|sq.||7 }|d}|s)q.||7 }qW d    n1 s8w   Y  W d    n1 sGw   Y  | || j d S )NrI   r&   Td   )	rH   rB   rC   r3   r   readlinerX   rW   r+   )r6   r   r:   rF   r   r+   rX   r   r   r   zip_readline_read_test   s*   
 z2AbstractTestsWithSourceFile.zip_readline_read_testc                 C   rt   r   )r   r   r:   rv   r   r   r   test_readline_read   s   z.AbstractTestsWithSourceFile.test_readline_readc              	   C   s   |  || t|d6}|t}| jD ]}| }| || qW d    n1 s-w   Y  W d    d S W d    d S 1 sEw   Y  d S r   )rH   rB   rC   r3   r   r)   r   rW   )r6   r   r:   rF   r   rG   Zlinedatar   r   r   zip_readline_test   s   
"z-AbstractTestsWithSourceFile.zip_readline_testc                 C   rt   r   )r   r   r:   rv   r   r   r   test_readline   rx   z)AbstractTestsWithSourceFile.test_readlinec              	   C   s   |  || t|d3}|t}| }W d    n1 s!w   Y  t| j|D ]
\}}| || q,W d    d S 1 sBw   Y  d S r   )	rH   rB   rC   r3   r   	readlineszipr)   rW   )r6   r   r:   rF   r   ZziplinesrG   zipliner   r   r   zip_readlines_test  s   
"z.AbstractTestsWithSourceFile.zip_readlines_testc                 C   rt   r   )r   r   r:   rv   r   r   r   test_readlines  rx   z*AbstractTestsWithSourceFile.test_readlinesc              	   C   s   |  || t|d7}|t}t| j|D ]
\}}| || qW d    n1 s.w   Y  W d    d S W d    d S 1 sFw   Y  d S r   )rH   rB   rC   r3   r   r   r)   rW   )r6   r   r:   rF   r   rG   r   r   r   r   zip_iterlines_test  s   "z.AbstractTestsWithSourceFile.zip_iterlines_testc                 C   rt   r   )r   r   r:   rv   r   r   r   test_iterlines  rx   z*AbstractTestsWithSourceFile.test_iterlinesc              	   C   s   t td| j}|dd W d   n1 sw   Y  t td| j8}|d}| |dd | |dd W d   n1 sIw   Y  W d   dS W d   dS 1 saw   Y  dS )	z>Check for cases where compressed data is larger than original.r<   r>   Z12NrI   rO      1   2)rB   rC   r   r:   rD   r3   rW   rX   )r6   rF   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)rB   rC   r   rD   r:   rg   rW   r   )r6   rF   rr   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<   rO   r;   a.txtr   r   r      )r   r;   )	rB   rC   r   rD   r:   rg   rW   r   _compresslevel)r6   rF   Za_infoZb_infor   r   r   test_writestr_compresslevel1  s   

z7AbstractTestsWithSourceFile.test_writestr_compresslevelc              
   C   s   dD ]M}|d }t |}tt d| j1}|d| |dd}||}| 	t
|| W d    n1 s;w   Y  W d    q1 sJw   Y  qd S )N)rO   i  i   i  i @  rO   r<   foorI   )r   rB   rC   r   r   r:   rD   r3   rX   rW   r]   )r6   Z	test_sizerf   Zjunkzipfr7   bufr   r   r   test_read_return_sizeA  s   
z1AbstractTestsWithSourceFile.test_read_return_sizec              
   C   sl  t  }tj|dd}|jd| j| jd | }W d    n1 s$w   Y  | }t |}t|=}|	d(}|
|d  | t |  W d    n1 sYw   Y  W d    n1 shw   Y  W d    n1 sww   Y  t |}t|D}|	d/}|
|d  | t |dr	 |dsW d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  t |}t|O}|	d0}|
|d  | t |dr	 |dsW d    n	1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 s/w   Y  d S )Nr<   r@   r>   r      r   )r   r   rB   rC   rD   r+   r:   tellr[   r3   truncateassertRaisesEOFErrorrX   r   )r6   r7   r   
end_offsetzipfiledatar   r   r   r   test_truncated_zipfileM  s\   








 $z2AbstractTestsWithSourceFile.test_truncated_zipfilec              
   C   s  d}t | D ]}t|d| j}|t| t|}| d| W d    n1 s+w   Y  t|d}t|}t|t	rI| d| | n| d| | | d| t|
|}| d| | | d| | d	| | jtjkr| d
| | d| ||&}t|}| d| | | d| | jtjkr| d
| W d    n1 sw   Y  | dt| W d    n1 sw   Y  | dt| qd S )Nz	file.namer<   zmode='w'rI   zfilename=%rzfile=%rzmode='r'z	filemode=z
file_size=zcompress_type=zcompress_size=zname=%rz[closed])r   rB   rC   r:   r4   r   reprr^   
isinstancerc   rg   
ZIP_STOREDr3   )r6   fnamer   rF   rI   r   r   r   r   	test_reprk  s@   
z%AbstractTestsWithSourceFile.test_reprc                 C   s$   t | D ]}| j|| jdd qd S )N	   r   ru   rv   r   r   r   test_compresslevel_basic     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   dS 1 s:w   Y  dS )zTCheck that files within a Zip archive can have different
        compression levels.r<   rO   r   Z
compress_1Z
compress_9r   N)rB   rC   r   r4   r   rg   rW   r   )r6   rF   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b}|dd}|d W d    n1 s3w   Y  d z9z|dd}|d	 W d    n1 sRw   Y  W n tye   d
7 Y nw W d  W d    qW d  nd  w W d    n1 sw   Y  tt | }| 	|
 dg | 	|dd W d    n1 sw   Y  qtt | %}| 	|
 ddg | 	|dd | 	|dd	 W d    d S 1 sw   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 NrO   )OSErrorsuperr4   r6   r+   )	__class__countstopr   r   r4     s
   zIAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFile.write)__name__
__module____qualname__r4   __classcell__r   r   r   )r   r   
BrokenFile  s    r   r   Tr<   Zfile1s   data1Zfile2s   data2rO   )r   r   rB   rC   r:   r3   r4   r   r[   rW   rd   rX   )r6   r   ZtestfilerF   r   r   r   r   test_writing_errors  sD   	"z/AbstractTestsWithSourceFile.test_writing_errorsc                 C      t t t t d S r   r   r   r   r6   r   r   r   tearDown     z$AbstractTestsWithSourceFile.tearDownr   )$r   r   r   classmethodr.   r8   rH   rs   rw   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   /   sD    


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   sn   |  || t|d!}|d}| |jd |d}| |jd W d    d S 1 s0w   Y  d S )NrI   r>     r?   )rH   rB   rC   rg   rW   external_attr)r6   r   r:   rF   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   rB   r   rv   r   r   r   test_writestr_permissions  rx   z3StoredTestsWithSourceFile.test_writestr_permissionsc                 C   s   t tdt j}|td W d    n1 sw   Y  t tdt j}| | dg W d    d S 1 s;w   Y  d S )Nr<   	/absoluterI   absoluterB   rC   r   r   r4   r   rW   rd   r6   rF   r   r   r   test_absolute_arcnames  s   "z0StoredTestsWithSourceFile.test_absolute_arcnamesc                 C   s   t tdt j}|tt W d   n1 sw   Y  t tdt j}|d| j | |	 tdg W d   dS 1 sCw   Y  dS )z&Test appending to an existing zipfile.r<   Nar>   )
rB   rC   r   r   r4   r   rD   r+   rW   rd   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w   Y  ttdtj}|tt W d   n1 s5w   Y  t td8}|t| t|d}| 	|
 tg | 	|t| j W d   n1 skw   Y  W d   n1 szw   Y  t td}| 	|t|| | }W d   n1 sw   Y  t|9}t|}| 	|
 tg | 	|t| j W d   n1 sw   Y  W d   dS W d   dS 1 sw   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!r1   Nr   rbrI   )r3   r   r4   rB   rC   r   r   seekr]   rW   rd   rX   r+   r   r   )r6   r+   r   rF   r   bior   r   r   test_append_to_non_zip_file  s.   
Pz5StoredTestsWithSourceFile.test_append_to_non_zip_filec              	   C   s  t  *}t|dtj}|tt W d    n1 sw   Y  | }W d    n1 s1w   Y  d}tt	d}|| || W d    n1 sRw   Y  tt	}| 
| tg | 
|t| j W d    d S 1 s{w   Y  d S )Nr<   r   r1   )r   r   rB   rC   r   r4   r   r[   r3   r   rW   rd   rX   r+   r6   r   rF   r   r+   r   r   r   r   test_read_concatenated_zip_file   s   


"z9StoredTestsWithSourceFile.test_read_concatenated_zip_filec              	   C   s  t  *}t|dtj}|tt W d    n1 sw   Y  | }W d    n1 s1w   Y  dd }tt	d}|| || W d    n1 sTw   Y  tt	d}| 
| tg |d| j W d    n1 szw   Y  tt	d}| 
|t|| | }W d    n1 sw   Y  t |D}t|&}| 
| tdg | 
|t| j | 
|d| j W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr<   s   I am not a ZipFile!i@B r1   r   r>   r   )r   r   rB   rC   r   r4   r   r[   r3   r   rW   rd   rD   r+   rX   r]   r   r   r   r   $test_append_to_concatenated_zip_file  s0   



Pz>StoredTestsWithSourceFile.test_append_to_concatenated_zip_filec                 C   s   t tdt j}|tt W d    n1 sw   Y  ttddd}|d W d    n1 s5w   Y  t td}| |t j W d    d S 1 sSw   Y  d S )Nr<   r   utf-8encodingz
   rI   )rB   rC   r   r   r4   r   r3   r   r6   rF   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 sw   Y  ttddd}|d W d    n1 s8w   Y  t td}| |t j | 	|jd W d    d S 1 s]w   Y  d S )Nr<      this is a commentr   r   r   zabcdef
rI   )
rB   rC   r   r   commentr4   r   r3   r   rW   r   r   r   r   )test_ignores_stuff_appended_past_comments,  s   "zCStoredTestsWithSourceFile.test_ignores_stuff_appended_past_commentsc              	   C   s   t td6}|t ttd}| |t|  W d   n1 s'w   Y  W d   dS W d   dS 1 s?w   Y  dS )z`Check that calling ZipFile.write without arcname specified
        produces the expected result.r<   r   N)rB   rC   r   r4   r   r3   rW   rX   r   r   r   r   test_write_default_name6  s   
"z1StoredTestsWithSourceFile.test_write_default_namec              	   C   s   d}t jtdd}||d W d    n1 sw   Y  t jtddN}||0}|  | t|j | t|j	d | t|j
 | t|j | t|j W d    n1 s_w   Y  W d    d S W d    d S 1 sww   Y  d S )Nsomefile.txtr<   r@   bogusrI   r   )rB   rC   r   rD   r3   closer   
ValueErrorrX   r   r   readableseekable)r6   r   rF   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 sw   Y  t jtdd}| t|jt W d   n1 s7w   Y  t jtdd-}| t |jtdd W d   n1 s[w   Y  W d   dS W d   dS 1 ssw   Y  dS )z[Check that trying to call write() on a readonly ZipFile object
        raises a ValueError.r<   r@   r   r   NrI   )	rB   rC   r   rD   r   r   r4   r   r3   r   r   r   r   test_write_to_readonlyL  s   "z0StoredTestsWithSourceFile.test_write_to_readonlyc                 C   s   t td ttd}| t|jt W d    n1 sw   Y  tjtddd}|t |	t}| 
|jd W d    d S 1 sIw   Y  d S )N)r   r   r<   FZstrict_timestampsi  rO   rO   r   r   r   )osutimer   rB   rC   r   r   r   r4   rg   rW   	date_time)r6   rF   r   r   r   r   test_add_file_before_1980Y  s   

"z3StoredTestsWithSourceFile.test_add_file_before_1980c                 C   s$  d}zt | W n ty   | d| d Y nw z
tt||f W n ty4   | d Y nw ttj}|dkrG| d| t	
td}| tj|jt W d    n1 saw   Y  t	j
tddd	}|t |t}| |jd
 W d    d S 1 sw   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  )ra   	localtimeOverflowErrorskipTestr  r  r   statst_mtime_nsrB   rC   r   r   structerrorr4   rg   rW   r  )r6   ZtsZmtime_nsrF   r   r   r   r   test_add_file_after_2107d  s,   	

"z2StoredTestsWithSourceFile.test_add_file_after_2107)r   r   r   rB   r   r:   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3}|tdt j |tdt j |d}|d}| |j	t j | |j	t j W d   dS 1 s<w   Y  dS )zUCheck that files within a Zip archive can have different
        compression options.r<   ZstoremeZ	deflatemeN)
rB   rC   r   r4   r   r   ZIP_DEFLATEDrg   rW   r   )r6   rF   sinfoZdinfor   r   r   test_per_file_compression  s   

"z4DeflateTestsWithSourceFile.test_per_file_compressionN)r   r   r   rB   r  r:   r  r   r   r   r   r    s    r  c                   @      e Zd ZejZdS )Bzip2TestsWithSourceFileNr   r   r   rB   	ZIP_BZIP2r:   r   r   r   r   r        
r  c                   @   r  )LzmaTestsWithSourceFileNr   r   r   rB   ZIP_LZMAr:   r   r   r   r   r    r  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   sX   t j| _t j| _dt _dt _ttd}|| j W d    d S 1 s%w   Y  d S )Nr   r   r1   )	rB   ZIP64_LIMIT_limitZIP_FILECOUNT_LIMIT_filecount_limitr3   r   r4   r+   r5   r   r   r   r8     s   "z#AbstractTestZip64InSmallFiles.setUpc                 C   sp  t j|d|dd}|td |tt |d| j W d    n1 s'w   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 ]}||}| |j| | |jt| j q|  W d    d S 1 s1w   Y  d S )Nr<   TZ
allowZip64r=   r>   rI   rR   rL   r   rM   rN   rO   rP   rQ      c                 S   rS   r   rT   r"   r   r   r   r%     rV   z:AbstractTestZip64InSmallFiles.zip_test.<locals>.<listcomp>)rB   rC   r4   r   rD   r+   rW   rX   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rU   rh   )r6   r   r:   rF   r7   ri   rj   rk   rl   rm   rn   ro   rp   r$   rq   rr   r   r   r   rs     sR   


$z&AbstractTestZip64InSmallFiles.zip_testc                 C   rt   r   ru   rv   r   r   r   rw     rx   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 ]}|d| d}| |d|d d	   qH|
  d S )Nr<   Tr'  r      foo%08d%dr(  9   rI   r    )rB   rC   r   r:   debugr'   rD   rW   r]   rd   r   rX   decode)r6   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 sGw   Y  | t|	 | |  t jtd| jdd}d|_| t|	 | | 
t j |d| d
 W d    n1 sw   Y  | t|	 | |  t jtd| jdd}d|_| t|	 | d}t||D ]}|d| d|d d	   q| t|	 | |  t td| j}| t|	 | t|D ]}|d| d}| |d|d d	   q|  d S )Nr<   Fr'  r   r   r*  r+  r(  r,  r&   r   Tr)  rI   r    )rB   rC   r   r:   r-  r'   rD   rW   r]   rd   r   LargeZipFiler   rX   r.  )r6   r   r/  r$   Z	numfiles2r0  r1  r   r   r   test_too_many_files_append  sN   z8AbstractTestZip64InSmallFiles.test_too_many_files_appendc                 C   s$   | j t_| jt_tt tt d S r   )r$  rB   r#  r&  r%  r   r   r   r   r   r   r   r   +  s   z&AbstractTestZip64InSmallFiles.tearDownN)r   r   r   r   r.   r8   rs   rw   r2  r4  r   r   r   r   r   r     s    

9'r   c                   @   sZ   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   sL   t j|d|dd}| t j|jtd W d    d S 1 sw   Y  d S Nr<   Fr'  r=   )rB   rC   r   r3  r4   r   r6   r   r:   rF   r   r   r   large_file_exception_test6  s
   "z5StoredTestZip64InSmallFiles.large_file_exception_testc                 C   sN   t j|d|dd}| t j|jd| j W d    d S 1 s w   Y  d S r6  )rB   rC   r   r3  rD   r+   r7  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   r8  rB   r   r9  rv   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 sw   Y  t tdt j}| | dg W d    d S 1 s=w   Y  d S )Nr<   Tr'  r   rI   r   r   r   r   r   r   r   E  s   "z2StoredTestZip64InSmallFiles.test_absolute_arcnamesc                 C   s
  t jtddd}|d| j W d    n1 sw   Y  t jtddd}|d}|j}W d    n1 s:w   Y  t jtddd}|d| j W d    n1 sYw   Y  t jtddd}|d}| |j| W d    d S 1 s~w   Y  d S )Nr<   Tr'  r>   rI   r   Zstrfile2)rB   rC   r   rD   r+   rg   extrarW   )r6   rF   r   r;  r   r   r   test_appendM  s   

"z'StoredTestZip64InSmallFiles.test_appendFc                 C   s  d}d}g }	g }
|}|rd}|r|	 | |
 | td|}|}|r2d}|r2|	 | |
 | td|}|}|rEd}|rE|
 | 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QrO   z<Q:   2   z<HrR   s   test.txt   test1234s   PK       ! %s   PK--       ! %s
           s   PK,       - -s                 s
            s   PK    l       s      s   PK      :   2 s       N)r{   r  packr]   )r6   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_fieldsrf   compress_sizeheader_offsetZlocal_extraZcentral_extraZcentral_dir_sizeZoffset_to_central_dirZlocal_extra_lengthZcentral_extra_lengthrU   r1  Zfilename_lengthZzip64_contentsr   r   r   make_zip64_file[  s   






	
!z+StoredTestZip64InSmallFiles.make_zip64_filec                 C   s  | j dd}| tj}tt| W d   n1 sw   Y  | dt|j	
  | j dddd}| tj}tt| W d   n1 sPw   Y  | dt|j	
  | j dd}| tj}tt| W d   n1 sw   Y  | dt|j	
  | j dddddd}| tj}tt| W d   n1 sw   Y  | d	t|j	
  | j d
dddd}| tj}tt| W d   n1 sw   Y  | d	t|j	
  | j ddd
dd}| tj}tt| W d   n	1 sw   Y  | d	t|j	
  | j d
d
dd}| tj}tt| W d   n	1 sIw   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)rC  Nz	file size)rC  rD  rE  zcompress size)rE  )rC  rD  rE  rF  rG  zheader offsetF)rC  rE  rF  rG  )rC  rD  rE  rG  )rC  rE  rG  )rK  r   rB   
BadZipFilerC   r   r   r^   rc   	exceptionlower)r6   Zmissing_file_size_extraeZmissing_compress_size_extraZmissing_header_offset_extrar   r   r   test_bad_zip64_extra  s~   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 ]Y}t||D ]P}i }|D ]}	||	 q-tt| j	d	i |,}
|

 d }| |j| | |j| | |j| | |
|| W d    q'1 srw   Y  q'qd S )
Nr=  r   rA  T)rC  rD  )rE  rF  )rG  rH  rO   r   )r'   r]   	itertoolscombinationsupdaterB   rC   r   r   rK  re   rW   rf   rI  rJ  rX   )r6   Zexpected_file_sizeZexpected_compress_sizeZexpected_header_offsetZexpected_contentparamsrI   ZcomborE   czfr   r   r   r    test_generated_valid_zip64_extra  s,   z<StoredTestZip64InSmallFiles.test_generated_valid_zip64_extraN)FFFFFF)r   r   r   rB   r   r:   r8  r9  r:  r   r<  rK  rP  rW  r   r   r   r   r5  2  s    
c^r5  c                   @   r  )DeflateTestZip64InSmallFilesNr   r   r   rB   r  r:   r   r   r   r   rX  8  r  rX  c                   @   r  )Bzip2TestZip64InSmallFilesNr  r   r   r   r   rZ  =  r  rZ  c                   @   r  )LzmaTestZip64InSmallFilesNr  r   r   r   r   r[  B  r  r[  c                   @   $   e Zd Zdd Zdd Zdd ZdS )AbstractWriterTestsc                 C      t t d S r   r   r   r   r   r   r   r   J     zAbstractWriterTests.tearDownc                 C   s   d}t td| j1}|dd}|| |  | |j |  | |j | 	|
d| W d    d S 1 s>w   Y  d S )N   contentr<   r   )rB   rC   r   r:   r3   r4   r   r`   r   rW   rX   r6   r+   r   r<   r   r   r   test_close_after_closeM  s   
"z*AbstractWriterTests.test_close_after_closec                 C   s   d}t td| j/}|dd}|| |  | |j | 	t
|jd | |d| W d    d S 1 s<w   Y  d S )Nra  r<   r   r&   )rB   rC   r   r:   r3   r4   r   r`   r   r   r   rW   rX   rb  r   r   r   test_write_after_closeX  s   
"z*AbstractWriterTests.test_write_after_closeN)r   r   r   r   rc  rd  r   r   r   r   r]  H  s    r]  c                   @   r  )StoredWriterTestsNr   r   r   rB   r   r:   r   r   r   r   re  b  s    
re  c                   @   r  )DeflateWriterTestsNrY  r   r   r   r   rg  e      
rg  c                   @   r  )Bzip2WriterTestsNr  r   r   r   r   ri  i  rh  ri  c                   @   r  )LzmaWriterTestsNr  r   r   r   r   rj  m  rh  rj  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 d S )NorU  )r^   )r6   namerd   r   r   r   assertCompiledIns  r   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y8   | d Y nw 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*   r3   O_WRONLYO_CREATr   	Exceptionr   )r6   r   rU   fdr   r   r   requiresWriteAccessw  s   


z"PyZipFileTests.requiresWriteAccessc              	   C   s  |  tjt t e}t|dO}t}|dr@|	tj
}tjd ur/||	tj d|v r:tj|}n|d d }|| tj|}| ||  | ||  W d    n1 sew   Y  W d    n1 stw   Y  t T}t|d5}t}|dr|d d }||d ddtj|f }| ||  | ||  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr<   .pyc__pycache__r   Ztestpackagez%s/%s)rv  r  r   dirname__file__r   rB   	PyZipFileendswithr_   sepaltsepextend	importlibutilsource_from_cachewritepybasenameassertNotInrd   rn  )r6   trF   rk   Z
path_splitZbnr   r   r   test_write_pyfile  s4   


 
Pz PyZipFileTests.test_write_pyfilec              	   C   s   dd l }tj|j}| | t <}t|d}|	| |
 }| d| | d| W d    n1 s:w   Y  W d    d S W d    d S 1 sRw   Y  d S )Nr   r<   zemail/__init__.pyzemail/mime/text.py)emailr  r   ry  rz  rv  r   rB   r{  r  rd   rn  )r6   r  
packagedirr  rF   ro   r   r   r   test_write_python_package  s   

Pz(PyZipFileTests.test_write_python_packagec                 C   s  dd l }tj|j}| | t }t|d}t	 }|
| W d    n1 s.w   Y  | }| d|v  t	 }|j
|dd d W d    n1 sUw   Y  | }| d|v dd }t	 $}| t |j
||d W d    n1 sw   Y  W d    n1 sw   Y  | }|rt| | d|v W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )	Nr   r<   SyntaxErrorc                 S   s   dS )NFr   )Zwhateverr   r   r   <lambda>      zCPyZipFileTests.test_write_filtered_python_package.<locals>.<lambda>Z
filterfuncc                 S   s   t j| d S )NZbad)r  r   r  
startswith)r   r   r   r   filter     zAPyZipFileTests.test_write_filtered_python_package.<locals>.filter)r   r  r   ry  rz  rv  r   rB   r{  r   r  r[   r`   assertWarnsUserWarningprint)r6   r   r  r  rF   Z	reportSIOZ	reportStrr  r   r   r   "test_write_filtered_python_package  s2   
 Pz1PyZipFileTests.test_write_filtered_python_packagec              	   C   s   dd l }tj|j}| | d}d}t B}tj|d|d!}|	| |
 }| d| | | d| | W d    n1 sDw   Y  W d    d S W d    d S 1 s\w   Y  d S )	Nr   TrO   rw  r<   )optimizezemail/__init__zemail/mime/text)r  r  r   ry  rz  rv  r   rB   r{  r  rd   r^   )r6   r  r  Zoptlevelextr  rF   ro   r   r   r   test_write_with_optimization  s   

Pz+PyZipFileTests.test_write_with_optimizationc              	   C   s  t t ztt jtdddd}|d W d    n1 s"w   Y  tt jtdddd}|d W d    n1 sCw   Y  tt jtdddd}|d	 W d    n1 sdw   Y  t 9}t	|d#}|
t | }| d| | d| | d| W d    n1 sw   Y  W d    n1 sw   Y  W tt d S W tt d S tt w )
Nmod1.pyr<   r   r   
print(42)
mod2.pyprint(42 * 42)
zmod2.txtzbla bla bla
r  mkdirr   r3   r   r*   r4   r   rB   r{  r  rd   rn  r  r   r6   r7   r  rF   ro   r   r   r   test_write_python_directory  s.   

 		z*PyZipFileTests.test_write_python_directoryc              	   C   s:  t t ztt jtdddd}|d W d    n1 s"w   Y  tt jtdddd}|d W d    n1 sCw   Y  t 7}t	|d!}|j
tdd	 d
 | }| d| | d| W d    n1 svw   Y  W d    n1 sw   Y  W tt d S W tt d S tt w )Nr  r<   r   r   r  r  r  c                 S   s   |  d S )Nr  )r|  )rk   r   r   r   r    r`  zEPyZipFileTests.test_write_python_directory_filtered.<locals>.<lambda>r  r  r  r   r   r   $test_write_python_directory_filtered  s&   
 		z3PyZipFileTests.test_write_python_directory_filteredc              
   C   s   t  O}t|d0}ttddd}|d W d    n1 s"w   Y  | t|jt t	t W d    n1 s=w   Y  W d    d S W d    d S 1 sUw   Y  d S )Nr<   r   r   z!most definitely not a python file)
r   rB   r{  r3   r   r4   r   RuntimeErrorr  r   )r6   r  rF   r   r   r   r   test_write_non_pyfile  s   
Pz$PyZipFileTests.test_write_non_pyfilec              
   C   s0  t t ztt jtdddd}|d W d    n1 s"w   Y  t S}t	|d=}t
 }|t jtd W d    n1 sJw   Y  | d|  | }| d| | d| W d    n1 sqw   Y  W d    n1 sw   Y  W tt d S W tt d S tt w )Nr  r<   r   r   zBad syntax in python file
r  zmod1.pyc)r  r  r   r3   r   r*   r4   r   rB   r{  r   r  r^   r[   rd   r  r   )r6   r7   r  rF   sro   r   r   r   test_write_pyfile_bad_syntax  s&   
 z+PyZipFileTests.test_write_pyfile_bad_syntaxc              	   C   s   t t zltt jtdddd}|d W d    n1 s"w   Y  t 2}t	|d}|
ttd  | }| d| W d    n1 sPw   Y  W d    n1 s_w   Y  W tt d S W tt d S tt w )Nr  r<   r   r   r  )r  r  r   r3   r   r*   r4   r   rB   r{  r  r   r   rd   rn  r   r  r   r   r   test_write_pathlike%  s   
 z"PyZipFileTests.test_write_pathlikeN)r   r   r   rn  rv  r  r  r  r  r  r  r  r  r  r   r   r   r   rk  r  s    rk  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   sP   t tdt j}tD ]
\}}||| qW d    d S 1 s!w   Y  d S )Nr<   )rB   rC   r   r   SMALL_TEST_DATArD   r6   rF   fpathfdatar   r   r   make_test_file5  s
   "zExtractTests.make_test_filec                 C   s   t  o |   ttdL}tD ]A\}}||}tj	t
 |}tj|}| || t|d}| | |  W d    n1 sIw   Y  t| qW d    n1 s]w   Y  W d    d S W d    d S 1 suw   Y  d S NrI   r   )r   r  rB   rC   r   r  extractr  r   r*   getcwdnormpathrW   r3   encoderX   r   )r6   rF   r  r  writtenfilecorrectfiler   r   r   r   test_extract:  s"   

"zExtractTests.test_extractc              
   C   s   |    ttdR}tD ]G\}}|||}tj||}tj	|}| 
tj||||f t|d}| | |  W d    n1 sKw   Y  t| qW d    n1 s_w   Y  tt d S r  )r  rB   rC   r   r  r  r  r   r*   r  r`   samefiler3   rW   r  rX   r   )r6   targetrF   r  r  r  r  r   r   r   r   _test_extract_with_targetM  s   
z&ExtractTests._test_extract_with_targetc                 C   6   t  }| | W d    d S 1 sw   Y  d S r   )r   r  r6   extdirr   r   r   test_extract_with_target`     "z%ExtractTests.test_extract_with_targetc                 C   <   t  }| t| W d    d S 1 sw   Y  d S r   )r   r  r   r   r  r   r   r   !test_extract_with_target_pathliked     "z.ExtractTests.test_extract_with_target_pathlikec                 C   s   t  b |   ttd?}|  tD ]0\}}tj	t
 |}t|d}| | |  W d    n1 s<w   Y  t| qW d    n1 sPw   Y  W d    d S W d    d S 1 shw   Y  d S r  )r   r  rB   rC   r   
extractallr  r  r   r*   r  r3   rW   r  rX   r   )r6   rF   r  r  outfiler   r   r   r   test_extract_allh  s   
"zExtractTests.test_extract_allc              
   C   s   |    ttd>}|| tD ].\}}tj||}t	|d}| 
| |  W d    n1 s7w   Y  t| qW d    n1 sKw   Y  tt d S r  )r  rB   rC   r   r  r  r  r   r*   r3   rW   r  rX   r   )r6   r  rF   r  r  r  r   r   r   r   _test_extract_all_with_targetu  s   


z*ExtractTests._test_extract_all_with_targetc                 C   r  r   )r   r  r  r   r   r   test_extract_all_with_target  r  z)ExtractTests.test_extract_all_with_targetc                 C   r  r   )r   r  r   r   r  r   r   r   %test_extract_all_with_target_pathlike  r  z2ExtractTests.test_extract_all_with_target_pathlikec                 C   sR   |  tj| t|d}| | | W d    d S 1 s"w   Y  d S )Nr   )r`   r  r   isfiler3   rW   rX   )r6   rU   r1  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)rB   rC   Z_sanitize_windows_namerW   )r6   Zsanr   r   r   test_sanitize_windows_name  s   z'ExtractTests.test_sanitize_windows_namec                 C      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)r6   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  )r6   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   r  )N))z	//foo/barr  )r  zfoo../ba..r)
foo/..\barr  r  )r6   Zposix_hacknamesr   r   r   (test_extract_hackers_arcnames_posix_only  s   z5ExtractTests.test_extract_hackers_arcnames_posix_onlyc           
   	   C   s:  |D ]\}}d|   }ttdtj}t }||_d|_||| W d    n1 s0w   Y  |	t
jd}t
jddd}t
jj|g|dR  }ttd}|||}	| j|	|d	||	|f d
 W d    n1 suw   Y  | || td ttd}|| W d    n1 sw   Y  | || td t
jjt
 g|dR  }ttd}||}	| j|	|d| d
 W d    n1 sw   Y  | || t|dd  ttd}|  W d    n	1 sw   Y  | || t|dd  tt qd S )N   foobarr<   r   r  r  subdirZsubsubrI   zextract %r: %r != %r)msgz
extract %rr   )r  rB   rC   r   r   ZipInforU   r   rD   replacer  r}  r   r*   r_   r  rW   r  r   r  r  r   )
r6   Z	hacknamesZarcnameZ	fixednamer1  rF   r   
targetpathr  r  r   r   r   r    sT   


z+ExtractTests._test_extract_hackers_arcnamesN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  unittestZskipIfr  r   r}  r  r  r  r   r   r   r   r  3  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4}|dd | t |dd W d    n1 s%w   Y  | | dgd  W d    n1 s?w   Y  t tdM}|	 }d}|D ]}|
|}|| 7 }W d    qS1 skw   Y  qS| |dd	h d}|D ]	}|||7 }q}| |dd	h W d    d S 1 sw   Y  d S )
Nr<   rm  r   barr   rI   r&   r  s   barfoo)rB   rC   r   r   rD   r  r  rW   rd   re   r3   rX   r^   )r6   rF   rp   r+   rr   r   r   r   r   test_open_via_zip_info  s(   "z!OtherTests.test_open_via_zip_infoc                 C   s`   t td }dD ]}t |}| jdO  _||| q	W d    d S 1 s)w   Y  d S )Nr<   Zabcdefghijklmnopr=  )rB   rC   r   r  	flag_bitsrD   )r6   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}tD ]\}}||| | |jd q	W d   n1 s%w   Y  | |jd t td}| |jd W d   n1 sIw   Y  | |jd dS )z8Check that the zipfile is closed after the 'with' block.r<   zzipfp is not openNzipfp is not closedrI   )rB   rC   r   r  rD   ZassertIsNotNoner7   assertIsNoner  r   r   r   
test_close  s   zOtherTests.test_closec                 C   s   t td}tD ]
\}}||| q	W d   n1 sw   Y  zt td}t  1 s2w   Y  W dS  t jyK   | |jd Y dS w )zWCheck that the zipfile is closed if an exception is raised in the
        'with' block.r<   NrI   r  )rB   rC   r   r  rD   rL  r  r7   )r6   rF   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      /        rI   )r   NotImplementedErrorrB   rC   r   r   r   r   r   r   test_unsupported_version(  s   

z#OtherTests.test_unsupported_versionc                 C   sT   t d}t|}| D ]	}||  qW d    d S 1 s#w   Y  d S )Nzzip_cp437_header.zip)r   rB   rC   rd   r3   r   )r6   r   rF   rm  r   r   r   test_read_unicode_filenames3  s   "z&OtherTests.test_read_unicode_filenamesc                 C   s   t td}|dd |dd | | d jt W d    n1 s(w   Y  t td}| |j	d jd | |j	d jd W d    d S 1 sSw   Y  d S )Nr<   foo.txtzTest for unicode filenameu   ö.txtr   rI   rO   )
rB   rC   r   rD   r   re   rU   rc   rW   filelistr6   rV  r   r   r   test_write_unicode_filenames;  s   "z'OtherTests.test_write_unicode_filenamesc                 C   sP   t td}|dd | |dd W d    d S 1 s!w   Y  d S )Nr<   u   прикладs   sample)rB   rC   r   rD   rW   rX   r   r   r   r   'test_read_after_write_unicode_filenamesE  s   "z2OtherTests.test_read_after_write_unicode_filenamesc                 C   s   t t d}d}ttdtj}||| W d   n1 s!w   Y  | t ttdtj W d   n1 s>w   Y  ttd}| |	 |g | |
|| W d   dS 1 sgw   Y  dS )z&Test exclusive creating a new zipfile.testfile.txt#   hello, world. this is some content.xNrI   )r   r   rB   rC   r   rD   r   FileExistsErrorrW   rd   rX   )r6   rU   r1  rF   r   r   r   test_exclusive_create_zip_fileJ  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 s'w   Y  W n ty;   | 	d Y nw | 
t jt ttd}| ||| W d    d S 1 s`w   Y  d S )Nr  r  r   z1Could not append data to a non-existent zip file.rI   )r  r   existsr   r   rB   rC   rD   r   failr`   rW   rX   )r6   rU   r1  rV  r   r   r   (test_create_non_existent_file_for_appendW  s    
"z3OtherTests.test_create_non_existent_file_for_appendc                 C   s`   t tddd}|d W d    n1 sw   Y  ztt}W d S  tjy/   Y d S w )Nr<   r   r   this is not a legal zip file
)r3   r   r4   rB   rC   rL  )r6   r7   rV  r   r   r   test_close_erroneous_filei  s   z$OtherTests.test_close_erroneous_filec                 C   s   t tddd}|d W d   n1 sw   Y  | tt | ttt t td}| t| W d   n1 sGw   Y  t	 }|d | t| |
dd | t| dS )	z;Check that is_zipfile() correctly identifies non-zip files.r<   r   r   r  Nr   s   this is not a legal zip file
r   )r3   r   r4   r   rB   
is_zipfiler   r   r   r   r   r5   r   r   r   test_is_zip_erroneous_filex  s   
z%OtherTests.test_is_zip_erroneous_filec                 C   s   t  }tj|dd}|dd W d   n1 sw   Y  | }tt|D ]}t |d| }| tj	tj| q+dS )zCCheck that zipfiles with missing bytes at the end raise BadZipFile.r<   r@   r     O, for a Muse of Fire!N)
r   r   rB   rC   rD   r[   r'   r]   r   rL  )r6   r7   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 sw   Y  | t t ttd}| t | |dd | }W d   n1 sGw   Y  t	
 }|| | t | |dd | t | dS )z7Check that is_zipfile() correctly identifies zip files.r<   r@   r  r  Nr   r   )rB   rC   r   rD   r`   r  r3   r   rX   r   r   r4   )r6   r   r7   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   rB   rC   r   r   r   r   r   %test_non_existent_file_raises_OSError  s   z0OtherTests.test_non_existent_file_raises_OSErrorc                 C   sv   t tddd}|  | tjtjt t tddd}|d W d    n1 s+w   Y  | tjtjt d S )Nr<   r   r   z
short file)r3   r   r   r   rB   rL  rC   r4   )r6   r   r7   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 sw   Y  | t|jd | t|jd | t|j	 | t|jdd tt
ddd	}|d
 W d   n1 sXw   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   r   r   zzipfile test data)r   r   rB   rC   rD   r   r   rX   r3   rh   r   r4   )r6   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.qN)r   r   rB   rC   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 sw   Y  t jtdd)}|d | t|jdd | t|jdd | t|jdd	 W d   dS 1 sPw   Y  dS )
z7Check that bad modes passed to ZipFile.open are caught.r<   r@   r  r  NrI   r  UZrU)rB   rC   r   rD   rX   r   r   r3   r6   r   r   r   r   test_bad_open_mode  s   
"zOtherTests.test_bad_open_modec              	   C   s   t jtddD}|dd |d }ttD ]}| |dd q| | d W d   n1 s6w   Y  W d   dS W d   dS 1 sNw   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)	rB   rC   r   rD   r3   r'   r(   rW   rX   )r6   r   r   r$   r   r   r   
test_read0  s   "zOtherTests.test_read0c                 C   sF   t jtdd}| t|jdd W d   dS 1 sw   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  rI   N)rB   rC   r   r   KeyErrorr3   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  rB   rC   r   r   r   r   r   test_bad_compression_mode  s   z$OtherTests.test_bad_compression_modec                 C   sL   d}t t|d}| t|jd W d    d S 1 sw   Y  d S )Nsf   PK.    C@              x PK..    C@                          xPK      /   !     rI   r  )rB   rC   r   r   r   r  r3   )r6   r+   r   r   r   r   test_unsupported_compression  s   "z'OtherTests.test_unsupported_compressionc                 C   sR   t jtdd}|dd | | dg W d   dS 1 s"w   Y  dS )zLCheck that a filename containing a null byte is properly
        terminated.r<   r@   zfoo.txt qqqr  r  N)rB   rC   r   rD   rW   rd   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)rW   rB   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w   Y  t jtdd}| |jd W d   n1 s=w   Y  d}t jtdd}||_|dd W d   n1 s_w   Y  t jtdd}| |j| W d   n1 s}w   Y  d	d
d tdD }|d}t jtdd}||_|dd W d   n1 sw   Y  t jtdd}| |j| W d   n1 sw   Y  t jtdd(}| 	t
 |d |_W d   n1 sw   Y  |dd W d   n	1 sw   Y  t jtdd}| |j| W d   n	1 s#w   Y  t jtdd}d|_|dd W d   n	1 sDw   Y  t jtdd}d|_W d   n	1 s_w   Y  t jtdd}| |jd W d   n	1 s~w   Y  t jtdd}d|_|dd W d   n	1 sw   Y  tjt}t jtdd}d|_W d   n	1 sw   Y  | |tjtk t jtdd}| |jd W d   dS 1 sw   Y  dS )z8Check that comments on the archive are handled properly.r<   r@   r&   r  r  NrI   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%   )  s    z,OtherTests.test_comments.<locals>.<listcomp>  r    s   oopss   original commentr   s   an updated comments   original comment that's longers   shorter comment)rB   rC   r   rW   r   rD   r*   r'   r  r  r  r  r   getsizer`   )r6   r   Zzipfrr   Zcomment2Zoriginal_zip_sizer   r   r   test_comments  sn   
$zOtherTests.test_commentsc              	   C   s   t tdt j/}|dd | t d|_W d    n1 s"w   Y  W d    d S W d    d S 1 s:w   Y  d S )Nr<   r  r  zthis is an error)rB   rC   r   r   rD   r   	TypeErrorr   r  r   r   r   test_unicode_commentO  s   "zOtherTests.test_unicode_commentc                 C   s   t tdt j}| |j d|_W d    n1 sw   Y  t td}| |jd W d    d S 1 s:w   Y  d S )Nr   r   rI   )rB   rC   r   r   r   r  r   rW   r  r   r   r   $test_change_comment_in_empty_archiveU  s   "z/OtherTests.test_change_comment_in_empty_archivec                 C   s   t tdt j}|dd W d    n1 sw   Y  t tdt j}| |j d|_W d    n1 s:w   Y  t td}| |jd W d    d S 1 sXw   Y  d S )Nr<   r  r  r   r   rI   )	rB   rC   r   r   rD   r`   r  r   rW   r  r   r   r   'test_change_comment_in_nonempty_archive\  s   "z2OtherTests.test_change_comment_in_nonempty_archivec                 C   s   t jtdd}|  z	t jtdd}W n t jy#   | d Y nw t jtdd}|  z
t jtdd}W d S    | d Y d S )Nr<   r@   rI   z+Unable to create empty ZIP file in 'w' moder   z+Unable to create empty ZIP file in 'a' mode)rB   rC   r   r   rL  r  r  r   r   r   test_empty_zipfilee  s   zOtherTests.test_empty_zipfilec                 C   s.   t tddd}|  | tjtjtd d S )Nr<   r   r   rI   )r3   r   r   r   rB   rL  rC   rv   r   r   r   test_open_empty_filev  s   zOtherTests.test_open_empty_filec                 C   s   |  ttjdd d S )NZ	seventies)i  rO   rO   r   r   r   )r   r   rB   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.emptyrT   z&<ZipInfo filename='empty' file_size=0>N)rB   r  rW   r   r6   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)rB   r  rW   Zorig_filenamerU   r  r   r   r   r;  r^   Zcreate_systemZcreate_versionZDEFAULT_VERSIONZextract_versionZreservedr  ZvolumeZinternal_attrr   rf   rI  r  r   r   r   ,test_create_empty_zipinfo_default_attributes  s"   z7OtherTests.test_create_empty_zipinfo_default_attributesc                 C   sJ   d}t t|d}| |  W d   dS 1 sw   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   %     rI   N)rB   rC   r   r   r  rh   r6   r   r   r   r   r   #test_zipfile_with_short_extra_field  s
   	"z.OtherTests.test_zipfile_with_short_extra_fieldc              
   C   sp  d}d}d}t tdt j}|jddd}|| W d    n1 s&w   Y  |jddd}| t |jddd W d    n1 sJw   Y  | t |jdd	d W d    n1 sfw   Y  | t |d
d W d    n1 sw   Y  | t |t	d W d    n1 sw   Y  | t |
  W d    n1 sw   Y  || W d    n1 sw   Y  |jddd}|| W d    n1 sw   Y  W d    n1 sw   Y  t td	.}| |d| | |d| | |d| | | g d W d    d S 1 s1w   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  handlerI   rc   ZabcderJ   baz)r   r  r%  )rB   rC   r   r   r3   r4   r   r   rD   rz  r   rW   rX   rd   )r6   Zmsg1Zmsg2Zmsg3r   Zw2w1r   r   r   test_open_conflicting_handles  sH   
$z(OtherTests.test_open_conflicting_handlesc              	   C   s  d}| d}ttd}|d| W d    n1 sw   Y  ttd{}|dde}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	|d|||d   |dtj | 	|
 t| |dtj | 	|
 d W d    n1 sw   Y  W d    n1 sw   Y  t }tj|dd}|d| W d    n1 sw   Y  tj|dd}|dde}||tj | 	|
 | || tj | 	|
 d ||tj | 	|
 | | 	|d|||d   |dtj | 	|
 t| |dtj | 	|
 d W d    n1 sDw   Y  W d    d S W d    d S 1 s]w   Y  d S )	Ns   Where's Bruce?s   Brucer<   r  rI   r   rK   r@   )findrB   rC   r   rD   r3   r   r  SEEK_SETrW   r   SEEK_CURrX   SEEK_ENDr]   r   r   )r6   txtblocr   r7   r+   r   r   r   test_seek_tell  sT   
 $zOtherTests.test_seek_tellc              	   C   s   d}t |}tj|dtjd}|dd W d    n1 s!w   Y  tdd . t|}| t	|j
d W d    n1 sEw   Y  W d    d S W d    d S 1 s]w   Y  d S )Ns   PK                  r<   )r:   r      azzipfile.bz2)r   r   rB   rC   r  rD   mockZpatchr   r  r  )r6   r+   Zzip_filerV  r   r   r   )test_decompress_without_3rd_party_library  s   
"z4OtherTests.test_decompress_without_3rd_party_libraryc                 C   r   r   r   r   r   r   r   r     r   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	   r1  r   r   r   r   r   r    sT    

	:	&
	r  c                   @   s   e Zd Zdd Zdd ZdS )AbstractBadCrcTestsc                 C   sP   | j }tjt|dd}| d|  W d   dS 1 s!w   Y  dS )z>Tests that files with bad CRCs return their name from testzip.rI   r@   afileN)zip_with_bad_crcrB   rC   r   r   rW   rh   r"  r   r   r   test_testzip_with_bad_crc  s   "z-AbstractBadCrcTests.test_testzip_with_bad_crcc              
   C   sz  | j }tjt|dd}| tj|jd W d   n1 s!w   Y  tjt|dd&}|dd}| tj|j W d   n1 sJw   Y  W d   n1 sYw   Y  tjt|ddK}|dd,}d|_	| tj |dr	 |dsW d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )zFTests that files with bad CRCs raise a BadZipFile exception when read.rI   r@   r3  Nr   )
r4  rB   rC   r   r   r   rL  rX   r3   MIN_READ_SIZE)r6   r   r   Zcorrupt_filer   r   r   test_read_with_bad_crc  s0   

"z*AbstractBadCrcTests.test_read_with_bad_crcN)r   r   r   r5  r7  r   r   r   r   r2    s    r2  c                   @      e Zd ZejZdZdS )StoredBadCrcTestssx   PK      ;:r         afilehello,AworldPK      ;:r                     afilePK      3   /     N)r   r   r   rB   r   r:   r4  r   r   r   r   r9    s    r9  c                   @   r8  )DeflateBadCrcTestss|   PK    n}=FAKE   n      afileHW(/Iɠ= PK    n}=FAKE   n                  afilePK      3   3     N)r   r   r   rB   r  r:   r4  r   r   r   r   r:  )      r:  c                   @   r8  )Bzip2BadCrcTestss   PK   nu=FAKE8   n      afileBZh91AY&SYԨ  @ D   P!u5:"(HjTe?PK   nu=FAKE8   n                 afilePK      3   [     N)r   r   r   rB   r  r:   r4  r   r   r   r   r<  6  r;  r<  c                   @   r8  )LzmaBadCrcTestss   PK   nu=FAKE   n      afile	 ]    4I:3`	q!.8 PK   nu=FAKE   n                 afilePK      3   >     N)r   r   r   rB   r  r:   r4  r   r   r   r   r=  F  r;  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w   Y  ttd| _t td}|| j W d    n1 s8w   Y  ttd| _	d S )Nr1   rI   )
r3   r   r4   r+   rB   rC   r   r   data2zip2r5   r   r   r   r8   o  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   w  s   


zDecryptionTests.tearDownc                 C   s(   |  t| jjd |  t| jjd d S )Ntest.txtzero)r   r  r   rX   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   perlrA  rB  )r   setpasswordr   r  rX   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   pythonrA  s   12345rB  )r   rD  rW   rX   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unicoderA  Zpython)pwd)r   r  r   rD  rX   r3   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    d S 1 sw   Y  d S )NrF  s
   encryptionrA  rI   r   rO   r&   )r   rD  rG  r(  r]   r3   r   r  r)  rW   r   r*  rX   r6  Z_readbufferZ_offsetr+  )r6   r,  Z	test_wordr-  Zbloc_lenr7   Zold_read_sizer   r   r   r.    s8   

"zDecryptionTests.test_seek_tellN)r   r   r   __doc__r+   r?  rG  rH  r8   r   rC  rE  r   rI  rL  r.  r   r   r   r   r>  U  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   rO   r&   c                 s   s(    | ]}t d t tdd V  qdS )z<fir   N)r  rB  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   r/   r0   r2   r5   r   r   r   r8     r9   z(AbstractTestsWithRandomBinaryFiles.setUpc                 C   r   r   r   r   r   r   r   r     r   z+AbstractTestsWithRandomBinaryFiles.tearDownc                 C   sL   t |d|}|td |tt W d    d S 1 sw   Y  d S )Nr<   r=   )rB   rC   r4   r   r7  r   r   r   rH     s   "z4AbstractTestsWithRandomBinaryFiles.make_test_archivec                 C   s   |  || t|d|*}|t}| t|t| j | || j | |d| j W d    d S 1 s:w   Y  d S )NrI   r=   )rH   rB   rC   rX   r   rW   r]   r+   )r6   r   r:   rF   testdatar   r   r   rs     s   
"z+AbstractTestsWithRandomBinaryFiles.zip_testc                 C   rt   r   ru   rv   r   r   r   	test_read  rx   z,AbstractTestsWithRandomBinaryFiles.test_readc              	   C   s4  |  || t|d|}g }|t}	 |d}|sq%|| qW d    n1 s/w   Y  g }|d}	 |d}|sEqK|| q=W d    n1 sUw   Y  d|}	| t	|	t	| j
 | |	| j
 d|}
| t	|
t	| j
 | |
| j
 W d    d S 1 sw   Y  d S ry   )rH   rB   rC   r3   r   rX   r{   r*   rW   r]   r+   )r6   r   r:   rF   r|   r}   r~   r   r   Z	testdata1Z	testdata2r   r   r   r     s:   





"z0AbstractTestsWithRandomBinaryFiles.zip_open_testc                 C   rt   r   r   rv   r   r   r   r     rx   z,AbstractTestsWithRandomBinaryFiles.test_openc              	   C   s   |  || t|d|I}g }|t}	 |tdd}|s"q(|| qW d    n1 s2w   Y  d|}| 	t
|t
| j | 	|| j W d    d S 1 sYw   Y  d S r   )rH   rB   rC   r3   r   rX   r   r{   r*   rW   r]   r+   )r6   r   r:   rF   r|   r}   r~   rQ  r   r   r   r   	  s    

"z7AbstractTestsWithRandomBinaryFiles.zip_random_open_testc                 C   rt   r   r   rv   r   r   r   r   	  rx   z3AbstractTestsWithRandomBinaryFiles.test_random_openN)r   r   r   r   r.   r8   r   rH   rs   rR  r   r   r   r   r   r   r   r   rN    s    

rN  c                   @   r  ) StoredTestsWithRandomBinaryFilesNrf  r   r   r   r   rS  	  rh  rS  c                   @   r  )!DeflateTestsWithRandomBinaryFilesNrY  r   r   r   r   rT  	  r  rT  c                   @   r  )Bzip2TestsWithRandomBinaryFilesNr  r   r   r   r   rU   	  r  rU  c                   @   r  )LzmaTestsWithRandomBinaryFilesNr  r   r   r   r   rV  %	  r  rV  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   )r7   offsetr5   r   r   r   __init__-	  s   
zTellable.__init__c                 C   s   | j |}|  j|7  _|S r   )r7   r4   rY  )r6   r+   nr   r   r   r4   1	  s   zTellable.writec                 C   s   | j S r   )rY  r   r   r   r   r   6	  s   zTellable.tellc                 C      | j   d S r   r7   flushr   r   r   r   r^  9	     zTellable.flushN)r   r   r   rZ  r4   r   r^  r   r   r   r   rW  ,	  s
    rW  c                   @   r\  )
Unseekablec                 C   s
   || _ d S r   )r7   r5   r   r   r   rZ  =	  s   
zUnseekable.__init__c                 C   s   | j |S r   )r7   r4   r   r   r   r   r4   @	  r`  zUnseekable.writec                 C   r\  r   r]  r   r   r   r   r^  C	  r_  zUnseekable.flushN)r   r   r   rZ  r4   r^  r   r   r   r   r`  <	  s    r`  c                   @   r\  )UnseekableTestsc                 C   sf  dd t tfD ]}| j|d t }|d t|}t||dtj	}|
dd |
dd	 W d    n1 s?w   Y  | | d d
 d tj|ddB}|d}| | d W d    n1 spw   Y  |d}| | d	 W d    n1 sw   Y  W d    n1 sw   Y  W d    q1 sw   Y  qd S )Nc                 S      | S r   r   r   r   r   r   r  H	  r  z/UnseekableTests.test_writestr.<locals>.<lambda>wrapper   abcr<   ones   111twos   222rK      abcPKrI   r@   )rW  r`  subTestr   r   r4   BufferedWriterrB   rC   r   rD   rW   r[   r3   rX   )r6   re  r   bfrF   r   zopenr   r   r   test_writestrG	  s,   

zUnseekableTests.test_writestrc                 C   s  dd t tfD ]}| j|d t }|d t|}t||dtj	N}| 
tt ttd}|d W d    n1 sDw   Y  |td ttd}|d	 W d    n1 sdw   Y  |td
 W d    n1 syw   Y  | | d d d tj|ddB}|d}| | d W d    n1 sw   Y  |d
}| | d	 W d    n1 sw   Y  W d    n1 sw   Y  W d    q1 sw   Y  qd S )Nc                 S   rb  r   r   rc  r   r   r   r  X	  r  z,UnseekableTests.test_write.<locals>.<lambda>rd  rf  r<   r1   rh  rg  rj  ri  rK   rk  rI   r@   )rW  r`  rl  r   r   r4   rm  rB   rC   r   
addCleanupr   r   r3   rW   r[   rX   )r6   re  r   rn  rF   f2r   ro  r   r   r   
test_writeW	  s:   

zUnseekableTests.test_writec                 C   sf  dd t tfD ]}| j|d t }|d t|}t||dtj	>}|
dd}|d W d    n1 s?w   Y  |
dd}|d	 W d    n1 sZw   Y  W d    n1 siw   Y  | | d d
 d t|}| |dd | |dd	 W d    n1 sw   Y  W d    q1 sw   Y  qd S )Nc                 S   rb  r   r   rc  r   r   r   r  m	  r  z1UnseekableTests.test_open_write.<locals>.<lambda>rd  rf  r<   rg  rh  ri  rj  rK   rk  )rW  r`  rl  r   r   r4   rm  rB   rC   r   r3   rW   r[   rX   )r6   re  r   rn  r   ro  r   r   r   test_open_writel	  s,   

zUnseekableTests.test_open_writeN)r   r   r   rp  rs  rt  r   r   r   r   ra  F	  s    ra  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 )Nrh  '  rj  )r   data1r?  r,   r   r   r   r.   	  s   z!TestsWithMultipleOpens.setUpClassc                 C   sR   t |dt j}|d| j |d| j W d    d S 1 s"w   Y  d S )Nr<   rg  ri  )rB   rC   r  rD   rw  r?  )r6   r   rF   r   r   r   rH   	  s   "z(TestsWithMultipleOpens.make_test_archivec                 C   s   t | D ]k}| | tj|ddU}|d3}|d}|d}|d}|| 7 }|| 7 }W d    n1 s?w   Y  W d    n1 sNw   Y  | || | || j W d    q1 sjw   Y  qd S )NrI   r@   rg    )r   rH   rB   rC   r3   rX   rW   rw  r6   r   r   zopen1zopen2rw  r?  r   r   r   test_same_file	  s   


 z%TestsWithMultipleOpens.test_same_filec                 C   s   t | D ]l}| | tj|ddV}|d3}|d}|d}|d}|| 7 }|| 7 }W d    n1 s?w   Y  W d    n1 sNw   Y  | || j | || j W d    q1 skw   Y  qd S )NrI   r@   rg  ri  rx  	r   rH   rB   rC   r3   rX   rW   rw  r?  ry  r   r   r   test_different_file	  s   


 z*TestsWithMultipleOpens.test_different_filec                 C   s   t | D ]l}| | tj|ddV}|d3}|d}|d}|d}|| 7 }|| 7 }W d    n1 s?w   Y  W d    n1 sNw   Y  | || j | || j W d    q1 skw   Y  qd S )NrI   r@   rg  rx  ri  r}  )r6   r   r   rz  rw  r{  r?  r   r   r   test_interleaved	  s    


z'TestsWithMultipleOpens.test_interleavedc              
   C   s   t | D ]e}| | t D}t|d}||d}||d}W d    n1 s1w   Y  |d}|d}|| 7 }|| 7 }W d    n1 sVw   Y  | 	|| j
 | 	|| j qd S )NrI   rg  ri  rx  )r   rH   
contextlib	ExitStackrB   rC   enter_contextr3   rX   rW   rw  r?  )r6   r   stackr   rz  r{  rw  r?  r   r   r   test_read_after_close	  s   



z,TestsWithMultipleOpens.test_read_after_closec              
   C   s  t | D ]{}t|dtj0}|d| j |d| j |d}|d}W d    n1 s2w   Y  W d    n1 sAw   Y  | 	|| jd d  t|d}|d}|d}W d    n1 slw   Y  | 	|| j | 	|| j qd S )Nr<   rg  ri  rx  rI   )
r   rB   rC   r  rD   rw  r?  r3   rX   rW   r6   r   r   rz  rw  r?  r   r   r   test_read_after_write	  s"   
z,TestsWithMultipleOpens.test_read_after_writec              
   C   s   t | D ]p}t|dtj0}|d| j |d}|d |d| j W d    n1 s2w   Y  W d    n1 sAw   Y  t|d}|d}|d}W d    n1 saw   Y  | 	|| j | 	|| j qd S )Nr<   rg  rx  ri  rI   )
r   rB   rC   r  rD   rw  r3   rX   r?  rW   r  r   r   r   test_write_after_read	  s    

z,TestsWithMultipleOpens.test_write_after_readc              
   C   s   |  t tjtdd)}tdD ]}|d |d}W d    q1 s(w   Y  qW d    n1 s8w   Y  ttjd}| 	|
 d W d    d S 1 sWw   Y  d S )NrI   r@   r   rg  r   )rH   r   rB   rC   r'   rX   r3   r  devnullZ
assertLessfileno)r6   r   r  rz  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 sw   Y  t tdt jE}|dd/}|d}|dd}|| j	 W d    n1 sKw   Y  || 7 }W d    n1 s`w   Y  W d    n1 sow   Y  | 
|| j t t}| 
|d| j	 W d    d S 1 sw   Y  d S )Nr<   rg  r   rI   rx  ri  )rB   rC   r   r  rD   rw  r3   rX   r4   r?  rW   )r6   r   Zr1rw  r&  r   r   r   test_write_while_reading	  s"   
"z/TestsWithMultipleOpens.test_write_while_readingc                 C   r^  r   r_  r   r   r   r   r   	  r`  zTestsWithMultipleOpens.tearDownN)r   r   r   r   r.   rH   r|  r~  r  r  r  r  r  r  r   r   r   r   r   ru  }	  s    
ru  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   r8   	  r_  zTestWithDirectory.setUpc              
   C   s   t td}|t W d    n1 sw   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   brU  )rB   rC   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   r  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C}|
| |jd }| |jd | |j|d> dB  |
|d |jd }| |jd	 | |j|d> dB  W d    n1 s`w   Y  tt	d
c}|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    d S 1 sw   Y  d S )Nr  r  r<   r   z/x/rO  yrO   zy/rI   r  r   )r  r   r*   r   r  r  st_moderB   rC   r   r4   r  r`   rU   r|  rW   r   r  r  r]   listdir)r6   dirpathrA   r   r   r  r   r   r   test_write_dir
  s4   



	



"z TestWithDirectory.test_write_dirc              	   C   s   t t jtd ttd!}|dd |j	d }| 
|jd | 
|jd W d    n1 s4w   Y  ttdG}|j	d }| |jd | 
|jd t jtd}t | || | t jt j|d | 
t |dg W d    d S 1 sw   Y  d S )	Nr  r<   zx/r&   r   i ArI   r  )r  r  r   r*   r   rB   rC   r   rD   r  rW   rU   r   r`   r|  r  r  r  )r6   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 d S r   )r   r   r  r   r  r   r   r   r   r   r   r   6
  s   zTestWithDirectory.tearDownN)	r   r   r   r8   r  r  r  r  r   r   r   r   r   r  	  s    r  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ZipInfoTestsc                 C   sH   t jt}| t|jd | |	  | |j
tjt d S Nztest_zipfile.py)rB   r  	from_filerz  rW   	posixpathr  rU   r   is_dirrf   r  r   r  r  r   r   r   test_from_file=
  s   zZipInfoTests.test_from_filec                 C   sN   t jtt}| t|j	d | 
|  | |jtjt d S r  )rB   r  r  r   r   rz  rW   r  r  rU   r   r  rf   r  r   r  r  r   r   r   test_from_file_pathlikeC
  s   z$ZipInfoTests.test_from_file_pathlikec                 C   sP   t jttd}| t|j	d | 
|  | |jtjt d S )Nr   )rB   r  r  r  fsencoderz  rW   r  r  rU   r   r  rf   r   r  r  r   r   r   test_from_file_bytesI
  s   z!ZipInfoTests.test_from_file_bytesc                 C   sz   t td.}tj| d}| t|j	d | 
|  | |jtjt W d    d S 1 s6w   Y  d S )Nr   r   )r3   rz  rB   r  r  r  rW   r  r  rU   r   r  rf   r  r   r  )r6   r   r  r   r   r   test_from_file_filenoO
  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   ry  abspathrz  rB   r  r  rW   rU   r`   r  r   r   rf   )r6   r  r  r   r   r   test_from_dirV
  s   zZipInfoTests.test_from_dirN)r   r   r   r  r  r  r  r  r   r   r   r   r  <
  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-mrB   r"  )r   Zassert_python_okr  r  linesepr  )r6   argsrE   rcouterrr   r   r   
zipfilecmda
  s   zCommandLineTest.zipfilecmdc                 G   s   t jddg|R  S )Nr  rB   )r   Zassert_python_failure)r6   r  r   r   r   zipfilecmd_failuref
  r  z"CommandLineTest.zipfilecmd_failurec                 C   s|   |   \}}}| |d | d|  | d|  | d|  |  dd\}}}| |d | | d d S )Nr&   s   usages   errors   required-lr  )r  rW   r^   rN  ZassertNotEqualstrip)r6   r  r  r  r   r   r   test_bad_usei
  s   zCommandLineTest.test_bad_usec                 C   sX   t d}dD ]}| ||}| | d qt d}| d|\}}}| |d d S )Nr  )-tz--tests   Done testingztesttar.tarr  r&   )r   r  rW   rstripr  )r6   zip_nameoptr  r  r  r   r   r   test_test_commands
  s   z!CommandLineTest.test_test_commandc                 C   s   t d}t }t|d}|| W d    n1 sw   Y  | dd}dD ]}| j||dd}| 	|| q-d S )Nr  rI   r    backslashreplace)r  z--listzascii:backslashreplace)ZPYTHONIOENCODING)
r   r   rY   rB   rC   rZ   r[   r  r  rW   )r6   r  r  ZtfZexpectedr  r  r   r   r   test_list_command|
  s   z!CommandLineTest.test_list_commandc              	   C   s`  |  tt ttddd}|d W d    n1 sw   Y  tt |  tt ttj	
tdddd}|d W d    n1 sIw   Y  ttg}ttd td g}d	D ]P}zI| j|tg|R  }| |d
 tt&}| | | | ||d d | ||d d W d    n1 sw   Y  W tt q]tt w d S )Nr<   r   r   ztest 1file.txtztest 2r  z	/file.txt)z-cz--creater&   r   s   test 1r   s   test 2)rq  r   r   r3   r4   r  r  	TESTFNDIRr   r   r*   r  r   rW   rB   rC   rd   rX   )r6   r   filesrd   r  r  rV  r   r   r   test_create_command
  s.   
z#CommandLineTest.test_create_commandc           	      C   s  t d}dD ]}t u}| |||}| |d t|S}| D ]F}tj	||j
dtj}| r@| tj| q#| tj| t|d}| | || W d    q#1 sdw   Y  q#W d    n1 stw   Y  W d    q1 sw   Y  qd S )Nr  )z-ez	--extractr&   r  r   )r   r   r  rW   rB   rC   re   r  r   r*   rU   r  r}  r  r`   r  r  r3   rX   )	r6   r  r  r  r  rV  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  _
  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   r8   
  s   z TestExecutablePrependedZip.setUpc                 C   sl   |  t|d|  t|}| D ]}||}| d| qW d    d S 1 s/w   Y  d S )Nzis_zipfile failed on s   FAVORITE_NUMBER)r`   rB   r  rC   rd   rX   r^   )r6   rm  rF   r[  r+   r   r   r   _test_zip_works
  s   
"z*TestExecutablePrependedZip._test_zip_worksc                 C      |  | j d S r   )r  r  r   r   r   r    test_read_zip_with_exe_prepended
     z;TestExecutablePrependedZip.test_read_zip_with_exe_prependedc                 C   r  r   )r  r  r   r   r   r   "test_read_zip64_with_exe_prepended
  r  z=TestExecutablePrependedZip.test_read_zip64_with_exe_prependedzsys.executable required.z	/bin/bashz#Test relies on #!/bin/bash working.c                 C   "   t | jtjg}| d| d S Ns   number in executable: 5)
subprocesscheck_outputr  sys
executabler^   r6   outputr   r   r   test_execute_zip2
     z,TestExecutablePrependedZip.test_execute_zip2c                 C   r  r  )r  r  r  r  r  r^   r  r   r   r   test_execute_zip64
  r  z-TestExecutablePrependedZip.test_execute_zip64N)r   r   r   rM  r8   r  r  r  r  Z
skipUnlessr  r  r  ro  X_OKr  r  r   r   r   r   r  
  s     
r  c                   @      e Zd ZG dd dZdS )jaracoc                   @   r  )zjaraco.itertoolsc                   @   r\  )zjaraco.itertools.Counterc                 C   s   d| _ t|| _d S rX  )r   iter
_orig_iter)r6   r$   r   r   r   rZ  
  s   z!jaraco.itertools.Counter.__init__c                 C   rb  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   )r6   resultr   r   r   __next__
  s   
z!jaraco.itertools.Counter.__next__N)r   r   r   rZ  r  r  r   r   r   r   Counter
  s    r  N)r   r   r   r  r   r   r   r   rQ  
      rQ  N)r   r   r   rQ  r   r   r   r   r  
  r  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&   N)rB   CompleteDirs_implied_dirsrd   rD   )rV  rm  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 ialpharep.zipN)r   r   rB   rC   rD   rU   )r+   rV  r   r   r   build_alpharep_fixture
  s   r  c                    s   t   fdd}|S )zZ
    Given a method, wrap it in a for loop that invokes method
    with each subtest.
    c                    s   |   D ]} | |d qd S )N)alpharep)zipfile_alpharep)r6   r  methr   r   re    s   zpass_alpharep.<locals>.wrapperN)	functoolswraps)r  re  r   r  r   pass_alpharep  s   r  c                   @   sx  e Zd Zdd Zdd Zdd Zedd Zed	d
 Zedd Z	edd Z
edd Zdd Zdd Zedd Zdd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zd/Zd0d1 Zd2d3 Zd4d5 Zed6d7 Zed8d9 Z ed:d; Z!ed<d= Z"ed>d? Z#ed@dA Z$edBdC Z%dDS )ETestPathc                 C   s   t  | _| | jj d S r   )r  r  fixturesrq  r   r   r   r   r   r8   '  s   
zTestPath.setUpc                 c   sl    |    t V  W d    n1 sw   Y  |    tt V  W d    d S 1 s/w   Y  d S r   )rl  r  r  r   r   r   r   r  +  s   


"zTestPath.zipfile_alpharepc                 C   sj   t | jt }|j}|  ||j }|d}|	|
  W d    |S 1 s.w   Y  |S r0   )r   r   r  r  r   r7   r   rU   r3   r4   r[   )r6   r  Ztmpdirbufferr   strmr   r   r   zipfile_ondisk1  s   

zTestPath.zipfile_ondiskc                 C   s   t |}| sJ | \}}}| sJ | sJ | s$J | \}}}| r3| s5J | \}	|	 s@J | \}
|
 \}| sPJ d S r   )rB   r   r  iterdiris_file)r6   r  rootr   r  grU  r   r   rO  hr$   r   r   r   test_iterdir_and_types:  s   



zTestPath.test_iterdir_and_typesc                 C   s    t |}|d rJ d S )Nzmissing.txt)rB   r   joinpathr  r6   r  r  r   r   r   test_is_file_missingJ  s   
zTestPath.test_is_file_missingc                 C   sP   t |}| \}}}| t |  W d    d S 1 s!w   Y  d S r   )rB   r   r  r   r   r6   r  r  r   r  r  r   r   r   test_iterdir_on_fileO  s
   

"zTestPath.test_iterdir_on_filec                 C   sN   t |}|d  sJ |d  sJ |d  sJ |d  s%J d S )Nr  b/r  zg/)rB   r   r  r  r   r   r   test_subdir_is_dirV  s
   
zTestPath.test_subdir_is_dirc                 C   s\   t |}| \}}}|jdd}| }W d    n1 s!w   Y  |dks,J d S )Nr   r   content of a)rB   r   r  r3   rX   )r6   r  r  r   r  r  r  r+   r   r   r   r   ^  s   

zTestPath.test_openc                 C   s   t t jt dd}|dd}|d W d   n1 s$w   Y  |djddd	}|d
 W d   dS 1 sDw   Y  dS )zo
        If the zipfile is open for write, it should be possible to
        write bytes or text to it.
        r<   r@   zfile.binr1   s   binary contentsNr  r   r   z	text file)rB   r   rC   r   r   r  r3   r4   )r6   rV  r  r   r   r   rt  f  s   "zTestPath.test_open_writec                 C   N   t tt }| t |d  W d   dS 1 s w   Y  dS )zJ
        Attempting to open a directory raises IsADirectoryError.
        r  N)rB   r   r  r  r   IsADirectoryErrorr  r3   r  r   r   r   test_open_extant_directoryq     "z#TestPath.test_open_extant_directoryc                 C   s   t |}| t |djddd W d    n1 sw   Y  | t |ddd W d    d S 1 s>w   Y  d S )Nr   r   r   r   )rB   r   r   r   r  r3   r  r   r   r   test_open_binary_invalid_argsy  s   
"z&TestPath.test_open_binary_invalid_argsc                 C   r  )zR
        Attempting to open a missing directory raises FileNotFoundError.
        zN)rB   r   r  r  r   FileNotFoundErrorr  r3   r  r   r   r   test_open_missing_directory  r  z$TestPath.test_open_missing_directoryc                 C   s@   t |}| \}}}|jdddksJ | dksJ d S )Nr   r   r  r  )rB   r   r  	read_text
read_bytesr  r   r   r   rR    s   
zTestPath.test_readc                 C   sN   t |}|d}| sJ |ddd}|jdddks%J d S Nr   r  r   e.txtr   r   content of e)rB   r   r  r  r  r6   r  r  r   rO  r   r   r   test_joinpath  s
   

zTestPath.test_joinpathc                 C   s0   t |}|ddd}|jdddksJ d S )Nr  r   r  r   r   r  )rB   r   r  r  )r6   r  r  rO  r   r   r   test_joinpath_multiple  s   
zTestPath.test_joinpath_multiplec                 C   sF   t |}|d }| sJ |d d d }|jdddks!J d S r  )rB   r   r  r  r  r   r   r   test_traverse_truediv  s
   
zTestPath.test_traverse_truedivc                 C   s   t ddddd}t| dS )zI
        Disable the __future__.division when testing traversal.
        zzipfile.Path(alpharep) / 'a'z(test)evalT)sourcerU   rA   dont_inheritN)compiler  )r6   r  coder   r   r   test_traverse_simplediv  s   z TestPath.test_traverse_simpledivc                 C   s&   |  |}tt|}t| dS )zN
        zipfile.Path should be constructable from a path-like object
        N)r  r   r   rc   rB   )r6   r  r  Zpathliker   r   r   test_pathlike_construction  s   
z#TestPath.test_pathlike_constructionc                 C   s   t |}|td  d S r  )rB   r   r   r  r   r   r   test_traverse_pathlike  s   
zTestPath.test_traverse_pathlikec                 C   s:   t |}|d jjdksJ |d d jjdksJ d S )Nr   r  r  za/rB   r   parentZatr  r   r   r   test_parent  s   
zTestPath.test_parentc                 C   s6   t |}|d jjdksJ |d jjdksJ d S )Nr  r  r  r  r  r   r   r   test_dir_parent  s   
zTestPath.test_dir_parentc                 C   s"   t |}|d jjdksJ d S )Nzmissing dir/r  r  r  r   r   r   test_missing_dir_parent     
z TestPath.test_missing_dir_parentc                 C   s   t |}| \}}}|dd |dd tdd | D s%J |d jdddks1J |d	  \}|jdddksBJ 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)rm  )r#   childr   r   r   r!    s    z+TestPath.test_mutability.<locals>.<genexpr>r   r   r  N)rB   r   r  rD   anyr  )r6   r  r  r   r  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<   rI   N)
r   r   rB   rC   maprc   r'   HUGE_ZIPFILE_NUM_ENTRIESrD   rA   )r6   r  rV  entryr   r   r   huge_zipfile  s   zTestPath.huge_zipfilec                 C   sF   t |  }tj| }|D ]}|d q|j| j	ks!J dS )zE
        Ensure joinpath on items in zipfile is linear time.
        suffixN)
rB   r   r   r  rQ  r  r  r  r   r  )r6   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_lowercaserc   )r#   r[  r   r   r   r%     s     z:TestPath.test_implied_dirs_performance.<locals>.<listcomp>rv  )r'   rB   r  r  r   r   r   r   test_implied_dirs_performance  s   z&TestPath.test_implied_dirs_performancec                 C   s\   |  |}t|}tdD ]}t|djdd qW d    d S 1 s'w   Y  d S )Nr   r   r   r   )r  rB   rC   r'   r   r  )r6   r  rJ   Zrepr   r   r   test_read_does_not_close  s   
"z!TestPath.test_read_does_not_closec                 C   s0   G dd dt j}||}t|d |sJ d S )Nc                   @   s   e Zd ZdS )z(TestPath.test_subclass.<locals>.SubclassN)r   r   r   r   r   r   r   Subclass  s    r(  r  )rB   r   r   )r6   r  r(  r  r   r   r   test_subclass  s   zTestPath.test_subclassc                 C   s"   t |}|jtdksJ d S )Nr  )rB   r   rU   r   r  r   r   r   test_filename  r  zTestPath.test_filenamec                 C   s0   t |}|jd  kr|jjksJ  J dS )zH
        The name of the root should be the name of the zipfile
        r  N)rB   r   rm  rU   r  r   r   r   test_root_name  s   
&zTestPath.test_root_namec                 C   s>   t |}|jtdksJ d|j_|jtdksJ d S )N.zfoo/bar.zipr   )rB   r   r  r   r  rU   r  r   r   r   test_root_parent  s   
zTestPath.test_root_parentc                 C   s   d|_ t|}| t |j W d   n1 sw   Y  | t |j W d   n1 s3w   Y  |d }|jdksCJ |jsHJ dS )zd
        It is an error to attempt to get the name
        or parent of an unnamed zipfile.
        Nr  )rU   rB   r   r   r  rm  r  )r6   r  r  subr   r   r   test_root_unnamed  s   
zTestPath.test_root_unnamedc                 C   s@   t dtjfi }|  D ]}||dj}t||sJ qd S )NZ	PathChildzsome dir)typerB   r   r  r  r  r   )r6   r  r-   rJ   r   r   r   test_inheritance1  s
   zTestPath.test_inheritanceN)&r   r   r   r8   r  r  r  r  r  r  r  r   rt  r  r  r  rR  r	  r
  r  r  r  r  r  r  r  r  r  r   r#  r&  r'  r)  r*  r+  r-  r/  r1  r   r   r   r   r  &  sv    	
















	





r  __main__)Yr  importlib.utilr  r   rQ  r  r   r  r$  r  r  r  ra   r  Zunittest.mockr0  rB   r  tempfiler   r   r   r   Ztest.supportr   r   r   r	   r
   r   Ztest.support.os_helperr   r   r   r   r   r   r  r(   ZDATAFILES_DIRr  r   r   ZTestCaser   r  r  r  r   r5  rX  rZ  r[  r]  re  rg  ri  rj  rk  r  r  r2  r9  r:  r<  r=  r>  rN  rS  rT  rU  rV  rW  r`  ra  ru  r  r  r  r  tupleZconsumer  r  r  r  r  r   mainr   r   r   r   <module>   s    	    C    B A    g[
7}A#P'
$  
