o
    IXh&                    @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dl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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.Z0e G dd de-ej.Z1e G dd de-ej.Z2G dd dZ3G dd de3ej.Z4e G dd de3ej.Z5e G dd  d e3ej.Z6e G d!d" d"e3ej.Z7G d#d$ d$Z8G d%d& d&e8ej.Z9e G d'd( d(e8ej.Z:e G d)d* d*e8ej.Z;e G d+d, d,e8ej.Z<G d-d. d.ej.Z=G d/d0 d0ej.Z>G d1d2 d2ej.Z?G d3d4 d4Z@G d5d6 d6e@ej.ZAe G d7d8 d8e@ej.ZBe G d9d: d:e@ej.ZCe G d;d< d<e@ej.ZDG d=d> d>ej.ZEG d?d@ d@ZFG dAdB dBeFej.ZGe G dCdD dDeFej.ZHe G dEdF dFeFej.ZIe G dGdH dHeFej.ZJG dIdJ dJZKG dKdL dLZLG dMdN dNej.ZMe G dOdP dPej.ZNG dQdR dRej.ZOG dSdT dTej.ZPG dUdV dVej.ZQG dWdX dXej.ZReSZTG dYdZ dZZUd[d\ ZVd]d^ ZWd_d` ZXG dadb dbej.ZYG dcdd ddejZZ[G dedf dfej.Z\e]dgkrPe^  dS dS )h    N)assert_python_ok)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   ;/opt/python-3.10.19/usr/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>4   s    z:AbstractTestsWithSourceFile.setUpClass.<locals>.<listcomp>    )rangeFIXEDTEST_SIZEline_genjoindataclsr   r   r   
setUpClass2   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   setUp9      "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ZipFiler5   r   writestrr,   r4   r*   )r7   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&   m       z8AbstractTestsWithSourceFile.zip_test.<locals>.<listcomp>)rI   rC   rD   assertEqualreadr   r,   r   StringIOprintdirgetvalue
splitlineslenassertInsplit
assertTruetimestrptimestrnamelistinfolist	file_sizegetinforV   testzip)r7   r   r;   r<   rG   r8   	directorylinesfndatetime_sizenamesinfosr%   nminfor   r   r   zip_testI   sN   

$z$AbstractTestsWithSourceFile.zip_testc                 C       t | D ]	}| || j qd S r   r   rt   r;   r7   r   r   r   r   
test_basic      z&AbstractTestsWithSourceFile.test_basicc           	   	   C   s   |  || t|d|i}g }|t}	 |d}|sn|| qW d    n1 s/w   Y  g }|d}	 |d}|sEn|| q=W d    n1 sUw   Y  | d|| j	 | d|| j	 W d    d S 1 syw   Y  d S NrJ   T   r>   r'   )
rI   rC   rD   r4   r   rY   appendrX   r+   r,   )	r7   r   r;   rG   zipdata1zipopen1	read_datazipdata2zipopen2r   r   r   zip_open_test   s2   



"z)AbstractTestsWithSourceFile.zip_open_testc                 C   ru   r   r   r   r;   rw   r   r   r   	test_open   ry   z%AbstractTestsWithSourceFile.test_openc                 C   s\   t t}| || j t|d| j}| |jt	 W d    d S 1 s'w   Y  d S NrJ   )
pathlibPathr   r   r;   rC   rD   assertIsInstancerV   rd   )r7   pathrG   r   r   r   test_open_with_pathlike   s
   
"z3AbstractTestsWithSourceFile.test_open_with_pathlikec              	   C   s   |  || t|d|<}g }|t}	 |tdd}|s"n|| qW d    n1 s2w   Y  | d	|| j
 W d    d S 1 sLw   Y  d S NrJ   TrP      r'   )rI   rC   rD   r4   r   rY   r   r|   rX   r+   r,   )r7   r   r;   rG   r}   r~   r   r   r   r   zip_random_open_test   s   
"z0AbstractTestsWithSourceFile.zip_random_open_testc                 C   ru   r   r   r   r;   rw   r   r   r   test_random_open   ry   z,AbstractTestsWithSourceFile.test_random_openc              	   C   s   |  || t|d.}|t}g }	 |d}|sn|| qW d    n1 s.w   Y  W d    n1 s=w   Y  | d|| j	 d S )NrJ   Tr'   )
rI   rC   rD   r4   r   read1r|   rX   r+   r,   r7   r   r;   rG   zipopenzipdatar   r   r   r   zip_read1_test   s"   

 	z*AbstractTestsWithSourceFile.zip_read1_testc                 C   ru   r   )r   r   r;   rw   r   r   r   
test_read1   ry   z&AbstractTestsWithSourceFile.test_read1c              	   C   s   |  || t|d6}|t!}g }	 |d}| t|d |s&n|| qW d    n1 s6w   Y  W d    n1 sEw   Y  | 	d
|| j d S )NrJ   T
   r'   )rI   rC   rD   r4   r   r   ZassertLessEqualr^   r|   rX   r+   r,   r   r   r   r   zip_read1_10_test   s$   

 
z-AbstractTestsWithSourceFile.zip_read1_10_testc                 C   ru   r   )r   r   r;   rw   r   r   r   test_read1_10   ry   z)AbstractTestsWithSourceFile.test_read1_10c              	   C   s   |  || t|d8}|t#}d}	 | }|sn||7 }|d}|s)n||7 }qW d    n1 s8w   Y  W d    n1 sGw   Y  | || j d S )NrJ   r'   Td   )	rI   rC   rD   r4   r   readlinerY   rX   r,   )r7   r   r;   rG   r   r,   rY   r   r   r   zip_readline_read_test   s*   
 z2AbstractTestsWithSourceFile.zip_readline_read_testc                 C   ru   r   )r   r   r;   rw   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   )rI   rC   rD   r4   r   r*   r   rX   )r7   r   r;   rG   r   rH   Zlinedatar   r   r   zip_readline_test   s   
"z-AbstractTestsWithSourceFile.zip_readline_testc                 C   ru   r   )r   r   r;   rw   r   r   r   test_readline  ry   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   )	rI   rC   rD   r4   r   	readlineszipr*   rX   )r7   r   r;   rG   r   ZziplinesrH   zipliner   r   r   zip_readlines_test  s   
"z.AbstractTestsWithSourceFile.zip_readlines_testc                 C   ru   r   )r   r   r;   rw   r   r   r   test_readlines  ry   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   )rI   rC   rD   r4   r   r   r*   rX   )r7   r   r;   rG   r   rH   r   r   r   r   zip_iterlines_test  s   "z.AbstractTestsWithSourceFile.zip_iterlines_testc                 C   ru   r   )r   r   r;   rw   r   r   r   test_iterlines  ry   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12NrJ   rP      1   2)rC   rD   r   r;   rE   r4   rX   rY   )r7   rG   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)rC   rD   r   rE   r;   rh   rX   r   )r7   rG   rs   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=   rP   r<   a.txtr   r   r      )r   r<   )	rC   rD   r   rE   r;   rh   rX   r   _compresslevel)r7   rG   Za_infoZb_infor   r   r   test_writestr_compresslevel3  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    n1 sJw   Y  qd S )N)rP   i  i   i  i @  rP   r=   foorJ   )r   rC   rD   r   r   r;   rE   r4   rY   rX   r^   )r7   Z	test_sizerg   Zjunkzipfr8   bufr   r   r   test_read_return_sizeC  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=   rA   r?   r      r   )r   r   rC   rD   rE   r,   r;   tellr\   r4   truncateassertRaisesEOFErrorrY   r   )r7   r8   r   
end_offsetzipfiledatar   r   r   r   test_truncated_zipfileO  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'rJ   zfilename=%rzfile=%rzmode='r'z	filemode=z
file_size=zcompress_type=zcompress_size=zname=%rz[closed])r   rC   rD   r;   r5   r   reprr_   
isinstancerd   rh   
ZIP_STOREDr4   )r7   fnamer   rG   rJ   r   r   r   r   	test_reprm  s@   
z%AbstractTestsWithSourceFile.test_reprc                 C   s$   t | D ]}| j|| jdd qd S )N	   r   rv   rw   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=   rP   r   Z
compress_1Z
compress_9r   N)rC   rD   r   r5   r   rh   rX   r   )r7   rG   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    nCW 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 NrP   )OSErrorsuperr5   r7   r,   )	__class__countstopr   r   r5     s
   zIAbstractTestsWithSourceFile.test_writing_errors.<locals>.BrokenFile.write)__name__
__module____qualname__r5   __classcell__r   r   r   r   r   
BrokenFile  s    r   r   Tr=   Zfile1s   data1Zfile2s   data2rP   )r   r   rC   rD   r;   r4   r5   r   r\   rX   re   rY   )r7   r   ZtestfilerG   r   r   r   r   test_writing_errors  sD   	"z/AbstractTestsWithSourceFile.test_writing_errorsc                 C      t t t t d S r   r   r   r   r7   r   r   r   tearDown     z$AbstractTestsWithSourceFile.tearDownr   )$r   r   r   classmethodr/   r9   rI   rt   rx   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    1   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 )NrJ   r?     r@   )rI   rC   rD   rh   rX   external_attr)r7   r   r;   rG   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   rC   r   rw   r   r   r   test_writestr_permissions  ry   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=   	/absoluterJ   absoluterC   rD   r   r   r5   r   rX   re   r7   rG   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?   )
rC   rD   r   r   r5   r   rE   r,   rX   re   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!r2   Nr   rbrJ   )r4   r   r5   rC   rD   r   r   seekr^   rX   re   rY   r,   r   r   )r7   r,   r   rG   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   r2   )r   r   rC   rD   r   r5   r   r\   r4   r   rX   re   rY   r,   r7   r   rG   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 r2   r   r?   r   )r   r   rC   rD   r   r5   r   r\   r4   r   rX   re   rE   r,   rY   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
   rJ   )rC   rD   r   r   r5   r   r4   r   r7   rG   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
rJ   )
rC   rD   r   r   commentr5   r   r4   r   rX   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)rC   rD   r   r5   r   r4   rX   rY   r   r   r   r   test_write_default_name8  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=   rA   bogusrJ   r   )rC   rD   r   rE   r4   closer   
ValueErrorrY   r   r   readableseekable)r7   r   rG   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=   rA   r   r   NrJ   )	rC   rD   r   rE   r   r   r5   r   r4   r   r   r   r   test_write_to_readonlyN  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  rP   rP   r   r   r   )osutimer   rC   rD   r   r   r   r5   rh   rX   	date_time)r7   rG   r   r   r   r   test_add_file_before_1980[  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  )rb   	localtimeOverflowErrorskipTestr  r  r   statst_mtime_nsrC   rD   r   r   structerrorr5   rh   rX   r  )r7   tsZmtime_nsrG   r   r   r   r   test_add_file_after_2107f  s,   	

"z2StoredTestsWithSourceFile.test_add_file_after_2107)r   r   r   rC   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)
rC   rD   r   r5   r   r   ZIP_DEFLATEDrh   rX   r   )r7   rG   sinfoZdinfor   r   r   test_per_file_compression  s   

"z4DeflateTestsWithSourceFile.test_per_file_compressionN)r   r   r   rC   r  r;   r  r   r   r   r   r    s    r  c                   @      e Zd ZejZdS )Bzip2TestsWithSourceFileNr   r   r   rC   	ZIP_BZIP2r;   r   r   r   r   r        
r  c                   @   r  )LzmaTestsWithSourceFileNr   r   r   rC   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   r2   )	rC   ZIP64_LIMIT_limitZIP_FILECOUNT_LIMIT_filecount_limitr4   r   r5   r,   r6   r   r   r   r9     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?   rJ   rS   rM   r   rN   rO   rP   rQ   rR      c                 S   rT   r   rU   r#   r   r   r   r&     rW   z:AbstractTestZip64InSmallFiles.zip_test.<locals>.<listcomp>)rC   rD   r5   r   rE   r,   rX   rY   r   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   rV   ri   )r7   r   r;   rG   r8   rj   rk   rl   rm   rn   ro   rp   rq   r%   rr   rs   r   r   r   rt     sR   


$z&AbstractTestZip64InSmallFiles.zip_testc                 C   ru   r   rv   rw   r   r   r   rx     ry   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   rJ   r!   )rC   rD   r   r;   debugr(   rE   rX   r^   re   r   rY   decode)r7   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,  rJ   r!   )rC   rD   r   r;   r0  r(   rE   rX   r^   re   r   LargeZipFiler   rY   r1  )r7   r   r2  r%   Z	numfiles2r3  r4  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'  rC   r&  r)  r(  r   r   r   r   r   r   r   r   -  s   z&AbstractTestZip64InSmallFiles.tearDownN)r   r   r   r   r/   r9   rt   rx   r5  r7  r   r   r   r   r   r#    s    

9'r#  c                   @   sv   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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>   )rC   rD   r   r6  r5   r   r7   r   r;   rG   r   r   r   large_file_exception_test8  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 r9  )rC   rD   r   r6  rE   r,   r:  r   r   r   large_file_exception_test2=  s
   
"z6StoredTestZip64InSmallFiles.large_file_exception_test2c                 C   s.   t | D ]}| |tj | |tj qd S r   )r   r;  rC   r   r<  rw   r   r   r   test_large_file_exceptionB  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   rJ   r   r   r   r   r   r   r   G  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?   rJ   r   Zstrfile2)rC   rD   r   rE   r,   rh   extrarX   )r7   rG   r   r>  r   r   r   test_appendO  s   

"z'StoredTestZip64InSmallFiles.test_appendFr'   Nc
                 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|  }|du rdt| }|	du rddt|  dt|  }	tdddt|  }tdddt|  }d}d}tdt|}d| | | | | | | d | | | | d | | | d td| d d | | | d t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<HHQrP   z<Q:   2   N,   l   z<HrS   s   test.txt   test1234s   PK       ! %s   PK--       ! %s
              PKs   - -              s
            s   PK    s      s   PK      :   2        )r|   r  packr^   )r7   file_size_64_setfile_size_extracompress_size_64_setcompress_size_extraheader_offset_64_setheader_offset_extraextensible_dataend_of_central_dir_sizeoffset_to_end_of_central_dirZactual_sizeZactual_header_offsetZlocal_zip64_fieldsZcentral_zip64_fieldsrg   compress_sizeheader_offsetZlocal_extraZcentral_extraZcentral_dir_sizeZoffset_to_central_dirZlocal_extra_lengthZcentral_extra_lengthrV   r4  Zfilename_lengthZzip64_contentsr   r   r   make_zip64_file]  s   








	


 $z+StoredTestZip64InSmallFiles.make_zip64_filec                 C   sR  | j dd}| tj}tt| W d   n1 sw   Y  | dt|j	
  | tt| | j dddd}| tj}tt| W d   n1 s[w   Y  | dt|j	
  | tt| | j dd}| tj}tt| W d   n1 sw   Y  | dt|j	
  | tt| | j dddddd}| tj}tt| W d   n1 sw   Y  | d	t|j	
  | tt| | j d
dddd}| tj}tt| W d   n	1 sw   Y  | d	t|j	
  | tt| | j ddd
dd}| tj}tt| W d   n	1 sOw   Y  | d	t|j	
  | tt| | j d
d
dd}| tj}tt| W d   n	1 sw   Y  | d	t|j	
  | tt| 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)rM  Nz	file size)rM  rN  rO  zcompress size)rO  )rM  rN  rO  rP  rQ  zheader offsetF)rM  rO  rP  rQ  )rM  rN  rO  rQ  )rM  rO  rQ  )rX  r   rC   
BadZipFilerD   r   r   r_   rd   	exceptionlowerra   
is_zipfile)r7   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  | j dd}| tjd tt| W d    n1 s w   Y  | tt| | j dd}| tjd tt| W d    n1 sPw   Y  | tt| | j dd}| tjd tt| W d    n1 sw   Y  | tt| | j dd}| tjd tt| W d    n1 sw   Y  | tt| d S )Nr   )rT  zCorrupt.*recordr   )rU  r   zCorrupt.*locator)	rX  assertRaisesRegexrC   rY  rD   r   r   r   r\  r7   r   r   r   r   !test_bad_zip64_end_of_central_dir0  s(   z=StoredTestZip64InSmallFiles.test_bad_zip64_end_of_central_dirc                 C   s   |   }|dd}| tjd tt| W d    n1 s$w   Y  | t	t| | j dd}|dd}| tjd tt| W d    n1 sZw   Y  | t	t| d S )NrJ  rK  record not found
      datarS  )
rX  replacer_  rC   rY  rD   r   r   r   r\  r`  r   r   r   .test_zip64_end_of_central_dir_record_not_foundE  s   zJStoredTestZip64InSmallFiles.test_zip64_end_of_central_dir_record_not_foundc                 C   s
  d}d}d}d}| j dd}tt|,}| d }| |j| | |j| | |j	| | |
|| W d    n1 sEw   Y  | tt| | tjd ttd|  W d    n1 sqw   Y  | ttd|  d S )Nr@  r   rI  rc  rd  rb  s	   prepended)rX  rC   rD   r   r   rf   rX   rg   rV  rW  rY   ra   r\  r_  rY  r   )r7   expected_file_sizeexpected_compress_sizeexpected_header_offsetexpected_contentr   zfr   r   r   r   test_zip64_extensible_dataS  s&   z6StoredTestZip64InSmallFiles.test_zip64_extensible_datac              
   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    n1 srw   Y  q'qd S )
Nr@  r   rI  T)rM  rN  )rO  rP  )rQ  rR  rP   r   )r(   r^   	itertoolscombinationsupdaterC   rD   r   r   rX  rf   rX   rg   rV  rW  rY   )r7   rg  rh  ri  rj  paramsrJ   ZcomborF   crk  r   r   r   r    test_generated_valid_zip64_extrah  s.   z<StoredTestZip64InSmallFiles.test_generated_valid_zip64_extra)	FFFFFFr'   NN)r   r   r   rC   r   r;   r;  r<  r=  r   r?  rX  r^  ra  rf  rl  rr  r   r   r   r   r8  4  s$    
ner8  c                   @   r  )DeflateTestZip64InSmallFilesNr   r   r   rC   r  r;   r   r   r   r   rs    r  rs  c                   @   r  )Bzip2TestZip64InSmallFilesNr  r   r   r   r   ru    r  ru  c                   @   r  )LzmaTestZip64InSmallFilesNr!  r   r   r   r   rv    r  rv  c                   @   ,   e Zd Zd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        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   )rC   rD   r   r;   r4   r5   r   ra   r   rX   rY   r7   r,   r   r=   r   r   r   test_close_after_close  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 )Nr|  r=   r   r'   )rC   rD   r   r;   r4   r5   r   ra   r   r   r   rX   rY   r}  r   r   r   test_write_after_close  s   
"z*AbstractWriterTests.test_write_after_closec              	   C   s   t  dg d}t||j }tt d| j2}|dd}| 	|
|| W d    n1 s4w   Y  | 	|dj| W d    d S 1 sNw   Y  d S )NrD  )rP   r   r+  rS   rL   r=   r,   )arrayr^   itemsizerC   rD   r   r   r;   r4   rX   r5   rh   rg   )r7   qZLENGTHr   r,   r   r   r   test_issue44439  s   "z#AbstractWriterTests.test_issue44439N)r   r   r   r   r~  r  r  r   r   r   r   rx    s
    
rx  c                   @   r  )StoredWriterTestsNr   r   r   rC   r   r;   r   r   r   r   r    s    
r  c                   @   r  )DeflateWriterTestsNrt  r   r   r   r   r        
r  c                   @   r  )Bzip2WriterTestsNr  r   r   r   r   r    r  r  c                   @   r  )LzmaWriterTestsNr!  r   r   r   r   r    r  r  c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )PyZipFileTestsc                 C   s$   |d |vr|  |d | d S d S )Norq  )r_   )r7   namere   r   r   r   assertCompiledIn  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+   r4   O_WRONLYO_CREATr   	Exceptionr   )r7   r   rV   fdr   r   r   requiresWriteAccess  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)r  r  r   dirname__file__r   rC   	PyZipFileendswithr`   sepaltsepextend	importlibutilsource_from_cachewritepybasenameassertNotInre   r  )r7   trG   rl   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   r  r  r  r   rC   r  r  re   r  )r7   r  
packagedirr  rG   rp   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   r  r  r  r   rC   r  r   r  r\   ra   assertWarnsUserWarningprint)r7   r   r  r  rG   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   FrP   r  r=   )optimizezemail/__init__zemail/mime/text)r  r  r   r  r  r  r   rC   r  r  re   r_   )r7   r  r  Zoptlevelextr  rG   rp   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   r4   r   r+   r5   r   rC   r  r  re   r  r  r   r7   r8   r  rG   rp   r   r   r   test_write_python_directory1  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  )rl   r   r   r   r  R  r{  zEPyZipFileTests.test_write_python_directory_filtered.<locals>.<lambda>r  r  r  r   r   r   $test_write_python_directory_filteredH  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   rC   r  r4   r   r5   r   RuntimeErrorr  r   )r7   r  rG   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   r4   r   r+   r5   r   rC   r  r   r  r_   r\   re   r  r   )r7   r8   r  rG   srp   r   r   r   test_write_pyfile_bad_syntaxc  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   r4   r   r+   r5   r   rC   r  r  r   r   re   r  r   r  r   r   r   test_write_pathlikey  s   
 z"PyZipFileTests.test_write_pathlikeN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s    r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zeejjdkddd Zeejjdkddd  Zd!d" Zd#S )$ExtractTestsc                 C   sP   t tdt j}tD ]
\}}||| qW d    d S 1 s!w   Y  d S )Nr=   )rC   rD   r   r   SMALL_TEST_DATArE   r7   rG   fpathfdatar   r   r   make_test_file  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 NrJ   r   )r   r  rC   rD   r   r  extractr  r   r+   getcwdnormpathrX   r4   encoderY   r   )r7   rG   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  rC   rD   r   r  r  r  r   r+   r  ra   samefiler4   rX   r  rY   r   )r7   targetrG   r  r  r  r  r   r   r   r   _test_extract_with_target  s   
z&ExtractTests._test_extract_with_targetc                 C   6   t  }| | W d    d S 1 sw   Y  d S r   )r   r  r7   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_pathlike     "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  rC   rD   r   
extractallr  r  r   r+   r  r4   rX   r  rY   r   )r7   rG   r  r  outfiler   r   r   r   test_extract_all  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  rC   rD   r   r  r  r  r   r+   r4   rX   r  rY   r   )r7   r  rG   r  r  r  r   r   r   r   _test_extract_all_with_target  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   )ra   r  r   isfiler4   rX   rY   )r7   rV   r4  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)rC   rD   Z_sanitize_windows_namerX   )r7   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)r7   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  )r7   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  )r7   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subsubrJ   zextract %r: %r != %r)msgz
extract %rr   )r  rC   rD   r   r   ZipInforV   r   rE   re  r  r  r   r+   r`   r  rX   r  r   r  r  r   )
r7   Z	hacknamesarcnameZ	fixednamer4  rG   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    s$    

r  c                   @   st  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(dKdL Z)e* dMdN Z+e dOdP Z,e dQdR Z-dSdT Z.dUS )V
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    n1 skw   Y  qS| |dd	h d}|D ]	}|||7 }q}| |dd	h W d    d S 1 sw   Y  d S )
Nr=   r  r   barr   rJ   r'   r  s   barfoo)rC   rD   r   r   rE   r  r  rX   re   rf   r4   rY   r_   )r7   rG   rq   r,   rs   r   r   r   r   test_open_via_zip_infoH  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@  )rC   rD   r   r  	flag_bitsrE   )r7   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 closedrJ   )rC   rD   r   r  rE   ZassertIsNotNoner8   assertIsNoner  r   r   r   
test_closec  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=   NrJ   r  )rC   rD   r   r  rE   rY  r  r8   )r7   rG   r  r  Zzipfp2r   r   r   test_close_on_exceptiono  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      /        rJ   )r   NotImplementedErrorrC   rD   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   rC   rD   re   r4   r   )r7   r   rG   r  r   r   r   test_read_unicode_filenames  s   "z&OtherTests.test_read_unicode_filenamesc                 C   s   t td}|dd |dd | | d jt W d    n1 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   rJ   rP   )
rC   rD   r   rE   r   rf   rV   rd   rX   filelistr7   rk  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)rC   rD   r   rE   rX   rY   r   r   r   r   'test_read_after_write_unicode_filenames  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.xNrJ   )r   r   rC   rD   r   rE   r   FileExistsErrorrX   re   rY   )r7   rV   r4  rG   r   r   r   test_exclusive_create_zip_file  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.rJ   )r  r   existsr   r   rC   rD   rE   r   failra   rX   rY   )r7   rV   r4  rk  r   r   r   (test_create_non_existent_file_for_append  s    
"z3OtherTests.test_create_non_existent_file_for_appendc                 C   s`   t tdd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
)r4   r   r5   rC   rD   rY  )r7   r8   rk  r   r   r   test_close_erroneous_file  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   )r4   r   r5   r   rC   r\  r   r   r   r   r   r6   r   r   r   test_is_zip_erroneous_file  s   
z%OtherTests.test_is_zip_erroneous_filec                 C   s   t  }tj|dd}|dd W d   n1 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=   rA   r	     O, for a Muse of Fire!N)
r   r   rC   rD   rE   r\   r(   r^   r   rY  )r7   r8   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=   rA   r	  r  Nr   r   )rC   rD   r   rE   ra   r\  r4   r   rY   r   r   r5   )r7   r   r8   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   rC   rD   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)r4   r   r   r   rC   rY  rD   r5   )r7   r   r8   r   r   r   !test_empty_file_raises_BadZipFile  s   z,OtherTests.test_empty_file_raises_BadZipFilec                 C   sF   t d}dD ]}td||dd< t|}| tjtj| qd S )N   PK                  )rP   rA  rB  r
     )		bytearrayr  rL  r   r   r   rC   rY  rD   )r7   bufferZdirsizer   r   r   r   8test_negative_central_directory_offset_raises_BadZipFile  s   
zCOtherTests.test_negative_central_directory_offset_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=   rA   r	  O, for a Muse of Fire!Nz	bogus.txtr   r   r   zzipfile test data)r   r   rC   rD   rE   r   r   rY   r4   ri   r   r5   )r7   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.r  N)r   r   rC   rD   r   r   r   r   r   test_bad_constructor_mode3  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=   rA   r	  r$  NrJ   r  UZrU)rC   rD   r   rE   rY   r   r   r4   r7   r   r   r   r   test_bad_open_mode7  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=   rA   r	  r$  r   r'   r  N)	rC   rD   r   rE   r4   r(   r)   rX   rY   )r7   r   r   r%   r   r   r   
test_read0D  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=   rA   r	  rJ   N)rC   rD   r   r   KeyErrorr4   r(  r   r   r   test_open_non_existent_itemP  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  rC   rD   r   r   r   r   r   test_bad_compression_modeV  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      /   !     rJ   r  )rC   rD   r   r   r   r  r4   )r7   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=   rA   zfoo.txt qqqr  r	  N)rC   rD   r   rE   rX   re   r(  r   r   r   test_null_byte_in_filenamef  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)rX   rC   ZsizeEndCentDirZsizeCentralDirZsizeEndCentDir64ZsizeEndCentDir64Locatorr   r   r   r   test_struct_sizesm  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=   rA   r'   r	  r$  NrJ   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)rC   rD   r   rX   r   rE   r+   r(   r  r  r  r  r   getsizera   )r7   r   Zzipfrr   Zcomment2Zoriginal_zip_sizer   r   r   test_commentst  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)rC   rD   r   r   rE   r   	TypeErrorr   r(  r   r   r   test_unicode_comment  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   rJ   )rC   rD   r   r   r   r
  r   rX   r(  r   r   r   $test_change_comment_in_empty_archive  s   "z/OtherTests.test_change_comment_in_empty_archivec                 C   s   t tdt j}|dd W d    n1 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   rJ   )	rC   rD   r   r   rE   ra   r
  r   rX   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=   rA   rJ   z+Unable to create empty ZIP file in 'w' moder   z+Unable to create empty ZIP file in 'a' mode)rC   rD   r   r   rY  r  r(  r   r   r   test_empty_zipfile  s   zOtherTests.test_empty_zipfilec                 C   s.   t tddd}|  | tjtjtd d S )Nr=   r   r   rJ   )r4   r   r   r   rC   rY  rD   rw   r   r   r   test_open_empty_file  s   zOtherTests.test_open_empty_filec                 C   s   |  ttjdd d S )NZ	seventies)i  rP   rP   r   r   r   )r   r   rC   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.emptyrU   z&<ZipInfo filename='empty' file_size=0>N)rC   r  rX   r   r7   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)rC   r  rX   Zorig_filenamerV   r  r   r   r   r>  r_   Zcreate_systemZcreate_versionZDEFAULT_VERSIONZextract_versionZreservedr   ZvolumeZinternal_attrr   rg   rV  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   %     rJ   N)rC   rD   r   r   r  ri   r7   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   rA   r  handlerJ   rd   ZabcderK   baz)r   r  rG  )rC   rD   r   r   r4   r5   r   r   rE   r  r   rX   rY   re   )r7   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	  rJ   r   rL   rA   )findrC   rD   r   rE   r4   r   r  SEEK_SETrX   r   SEEK_CURrY   SEEK_ENDr^   r   r   )r7   txtblocr   r8   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 )Nr  r=   )r;   r      azzipfile.bz2)r   r   rC   rD   r  rE   mockZpatchr   r  r  )r7   r,   Zzip_filerk  r   r   r   )test_decompress_without_3rd_party_libraryN  s   
"z4OtherTests.test_decompress_without_3rd_party_libraryc              	   C   s  d}t t|dv}| | ddg |d}| |jd | |jd | |j	d |d}| |jd | |jd | |j	d | t
|dd | t jd |d W d    n1 slw   Y  W d    d S W d    d S 1 sw   Y  d S )	Ns   PK    lH8   	     a   0K\d`PK     lH8   	                   aPK     lH8   	                   bPK      ^   /     rJ   r   br   r   	  zFile name.*differ)rC   rD   r   r   rX   re   rh   rW  rV  rg   r^   rY   r_  rY  r7   r,   r   rA  r   r   r   test_full_overlapX  s&   

"zOtherTests.test_full_overlapc              	   C   s  d}t t|dn}| | ddg |d}| |jd | |jd | |j	d |d}| |jd | |jd	 | |j	d
 | 
t jd |d W d    n1 saw   Y  | t|dd
 W d    d S 1 s|w   Y  d S )Ns   PK    lHY84   (     a  PK    lH8   	     b   0K\d`PK     lHY84   (                   aPK     lH8   	               $   bPK      ^   S     rJ   r   rT  r   4   i(  $   r   rU  zOverlapped entries)rC   rD   r   r   rX   re   rh   rW  rV  rg   r_  rY  rY   r^   rV  r   r   r   test_quoted_overlapu  s"   

"zOtherTests.test_quoted_overlapc                 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/  r3  r7  r9  r:  r;  r<  r=  r>  rB  rC  rE  rI  rP  r
   rS  rW  rZ  r   r   r   r   r   r  G  s^    

	:	&
	

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.rJ   rA   afileN)zip_with_bad_crcrC   rD   r   r   rX   ri   rD  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.rJ   rA   r\  Nr   )
r]  rC   rD   r   r   r   rY  rY   r4   MIN_READ_SIZE)r7   r   r   Zcorrupt_filer   r   r   test_read_with_bad_crc  s0   

"z*AbstractBadCrcTests.test_read_with_bad_crcN)r   r   r   r^  r`  r   r   r   r   r[    s    r[  c                   @      e Zd ZejZdZdS )StoredBadCrcTestssx   PK      ;:r         afilehello,AworldPK      ;:r                     afilePK      3   /     N)r   r   r   rC   r   r;   r]  r   r   r   r   rb    s    rb  c                   @   ra  )DeflateBadCrcTestss|   PK    n}=FAKE   n      afileHW(/Iɠ= PK    n}=FAKE   n                  afilePK      3   3     N)r   r   r   rC   r  r;   r]  r   r   r   r   rc        rc  c                   @   ra  )Bzip2BadCrcTestss   PK   nu=FAKE8   n      afileBZh91AY&SYԨ  @ D   P!u5:"(HjTe?PK   nu=FAKE8   n                 afilePK      3   [     N)r   r   r   rC   r  r;   r]  r   r   r   r   re    rd  re  c                   @   ra  )LzmaBadCrcTestss   PK   nu=FAKE   n      afile	 ]    4I:3`	q!.8 PK   nu=FAKE   n                 afilePK      3   >     N)r   r   r   rC   r"  r;   r]  r   r   r   r   rf    rd  rf  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 )Nr2   rJ   )
r4   r   r5   r,   rC   rD   r   r   data2zip2r6   r   r   r   r9   
	  s   zDecryptionTests.setUpc                 C   s,   | j   tt | j  tt d S r   )r   r   r  r   r   ri  r   r   r   r   r   r   	  s   


zDecryptionTests.tearDownc                 C   s(   |  t| jjd |  t| jjd d S )Ntest.txtzero)r   r  r   rY   ri  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   perlrj  rk  )r   setpasswordr   r  rY   ri  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   pythonrj  s   12345rk  )r   rm  rX   rY   plainri  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unicoderj  Zpython)pwd)r   r8  r   rm  rY   r4   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 )Nro  s
   encryptionrj  rJ   r   rP   r'   )r   rm  rp  rJ  r^   r4   r   r  rK  rX   r   rL  rY   r_  Z_readbufferZ_offsetrM  )r7   rN  Z	test_wordrO  Zbloc_lenr8   Zold_read_sizer   r   r   rP  1	  s8   

"zDecryptionTests.test_seek_tellN)r   r   r   __doc__r,   rh  rp  rq  r9   r   rl  rn  r	   rr  ru  rP  r   r   r   r   rg    s     	

rg  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 )Nr   @   r   rP   r'   c                 s   s(    | ]}t d t tdd V  qdS )z<fir   N)r  rL  r   r   r#   r   r   r   r$  [	  s     z@AbstractTestsWithRandomBinaryFiles.setUpClass.<locals>.<genexpr>)r   r+   r(   r,   )r.   Z	datacountr   r   r   r/   X	  s   
z-AbstractTestsWithRandomBinaryFiles.setUpClassc                 C   r0   r1   r3   r6   r   r   r   r9   ^	  r:   z(AbstractTestsWithRandomBinaryFiles.setUpc                 C   r   r   r   r   r   r   r   r   c	  r   z+AbstractTestsWithRandomBinaryFiles.tearDownc                 C   sL   t |d|}|td |tt W d    d S 1 sw   Y  d S )Nr=   r>   )rC   rD   r5   r   r:  r   r   r   rI   g	  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 )NrJ   r>   )rI   rC   rD   rY   r   rX   r^   r,   )r7   r   r;   rG   testdatar   r   r   rt   m	  s   
"z+AbstractTestsWithRandomBinaryFiles.zip_testc                 C   ru   r   rv   rw   r   r   r   	test_readw	  ry   z,AbstractTestsWithRandomBinaryFiles.test_readc              	   C   s4  |  || t|d|}g }|t}	 |d}|sn|| qW d    n1 s/w   Y  g }|d}	 |d}|sEn|| 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 rz   )rI   rC   rD   r4   r   rY   r|   r+   rX   r^   r,   )r7   r   r;   rG   r}   r~   r   r   r   Z	testdata1Z	testdata2r   r   r   r   {	  s:   





"z0AbstractTestsWithRandomBinaryFiles.zip_open_testc                 C   ru   r   r   rw   r   r   r   r   	  ry   z,AbstractTestsWithRandomBinaryFiles.test_openc              	   C   s   |  || t|d|I}g }|t}	 |tdd}|s"n|| qW d    n1 s2w   Y  d|}| 	t
|t
| j | 	|| j W d    d S 1 sYw   Y  d S r   )rI   rC   rD   r4   r   rY   r   r|   r+   rX   r^   r,   )r7   r   r;   rG   r}   r~   r   ry  r   r   r   r   	  s    

"z7AbstractTestsWithRandomBinaryFiles.zip_random_open_testc                 C   ru   r   r   rw   r   r   r   r   	  ry   z3AbstractTestsWithRandomBinaryFiles.test_random_openN)r   r   r   r   r/   r9   r   rI   rt   rz  r   r   r   r   r   r   r   r   rw  W	  s    

rw  c                   @   r  ) StoredTestsWithRandomBinaryFilesNr  r   r   r   r   r{  	  r  r{  c                   @   r  )!DeflateTestsWithRandomBinaryFilesNrt  r   r   r   r   r|  	  r  r|  c                   @   r  )Bzip2TestsWithRandomBinaryFilesNr  r   r   r   r   r}  	  r  r}  c                   @   r  )LzmaTestsWithRandomBinaryFilesNr!  r   r   r   r   r~  	  r  r~  c                   @   rw  )
Tellablec                 C   s   || _ d| _d S Nr   )r8   offsetr6   r   r   r   __init__	  s   
zTellable.__init__c                 C   s   | j |}|  j|7  _|S r   )r8   r5   r  )r7   r,   nr   r   r   r5   	  s   zTellable.writec                 C   s   | j S r   )r  r   r   r   r   r   	  s   zTellable.tellc                 C      | j   d S r   r8   flushr   r   r   r   r  	     zTellable.flushN)r   r   r   r  r5   r   r  r   r   r   r   r  	  s
    r  c                   @   $   e Zd Zdd Zdd Zdd ZdS )
Unseekablec                 C   s
   || _ d S r   )r8   r6   r   r   r   r  	  s   
zUnseekable.__init__c                 C   s   | j |S r   )r8   r5   r   r   r   r   r5   	  r{  zUnseekable.writec                 C   r  r   r  r   r   r   r   r  	  r  zUnseekable.flushN)r   r   r   r  r5   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    n1 sw   Y  qd S )Nc                 S      | S r   r   r   r   r   r   r  	  r  z/UnseekableTests.test_writestr.<locals>.<lambda>wrapper   abcr=   ones   111twos   222rL      abcPKrJ   rA   )r  r  subTestr   r   r5   BufferedWriterrC   rD   r   rE   rX   r\   r4   rY   )r7   r  r   bfrG   r   zopenr   r   r   test_writestr	  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    n1 sw   Y  qd S )Nc                 S   r  r   r   r  r   r   r   r  	  r  z,UnseekableTests.test_write.<locals>.<lambda>r  r  r=   r2   r  r  r  r  rL   r  rJ   rA   )r  r  r  r   r   r5   r  rC   rD   r   
addCleanupr   r   r4   rX   r\   rY   )r7   r  r   r  rG   f2r   r  r   r   r   
test_write	  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    n1 sw   Y  qd S )Nc                 S   r  r   r   r  r   r   r   r  
  r  z1UnseekableTests.test_open_write.<locals>.<lambda>r  r  r=   r  r  r  r  rL   r  )r  r  r  r   r   r5   r  rC   rD   r   r4   rX   r\   rY   )r7   r  r   r  r   r  r   r   r   test_open_write
  s.   

zUnseekableTests.test_open_writeN)r   r   r   r  r  r  r   r   r   r   r  	  s    r  c                   @   sh   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )TestsWithMultipleOpensc                 C   s    dt d | _dt d | _d S )Nr  '  r  )r   data1rh  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=   r  r  )rC   rD   r  rE   r  rh  )r7   r   rG   r   r   r   rI   
  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    n1 sjw   Y  qd S )NrJ   rA   r    )r   rI   rC   rD   r4   rY   rX   r  r7   r   r   zopen1zopen2r  rh  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    n1 skw   Y  qd S )NrJ   rA   r  r  r  	r   rI   rC   rD   r4   rY   rX   r  rh  r  r   r   r   test_different_file3
  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    n1 skw   Y  qd S )NrJ   rA   r  r  r  r  )r7   r   r   r  r  r  rh  r   r   r   test_interleavedA
  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 )NrJ   r  r  r  )r   rI   
contextlib	ExitStackrC   rD   enter_contextr4   rY   rX   r  rh  )r7   r   stackr   r  r  r  rh  r   r   r   test_read_after_closeP
  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=   r  r  r  rJ   )
r   rC   rD   r  rE   r  rh  r4   rY   rX   r7   r   r   r  r  rh  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=   r  r  r  rJ   )
r   rC   rD   r  rE   r  r4   rY   rh  rX   r  r   r   r   test_write_after_readl
  s    

z,TestsWithMultipleOpens.test_write_after_readc              
   C   s   |  t tjtdd)}tdD ]}|d |d}W d    n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 )NrJ   rA   r   r  r   )rI   r   rC   rD   r(   rY   r4   r  devnullZ
assertLessfileno)r7   r   r  r  r   r   r   r   test_many_opensy
  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=   r  r   rJ   r  r  )rC   rD   r   r  rE   r  r4   rY   r5   rh  rX   )r7   r   Zr1r  rH  r   r   r   test_write_while_reading
  s"   
"z/TestsWithMultipleOpens.test_write_while_readingc                 C   ry  r   rz  r   r   r   r   r   
  r{  zTestsWithMultipleOpens.tearDownN)r   r   r   r   r/   rI   r  r  r  r  r  r  r  r  r   r   r   r   r   r  
  s    
r  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestWithDirectoryc                 C   s   t t d S r   )r  r  r   r   r   r   r   r9   
  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   rT  rq  )rC   rD   r   r  r   ra   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  r5  r=   r   z/x/r   yrP   zy/rJ   r  r   )r  r   r+   r   r  r  st_moderC   rD   r   r5   r
  ra   rV   r  rX   r   r  r  r^   listdir)r7   dirpathrB   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 ArJ   r  )r  r  r   r+   r   rC   rD   r   rE   r
  rX   rV   r   ra   r  r  r  r  )r7   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   
  s   zTestWithDirectory.tearDownN)	r   r   r   r9   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)rC   r  	from_filer  rX   	posixpathr  rV   r   is_dirrg   r  r   r6  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  )rC   r  r  r   r   r  rX   r  r  rV   r   r  rg   r  r   r6  r@  r   r   r   test_from_file_pathlike
  s   z$ZipInfoTests.test_from_file_pathlikec                 C   sP   t jttd}| t|j	d | 
|  | |jtjt d S )Nr   )rC   r  r  r  fsencoder  rX   r  r  rV   r   r  rg   r   r6  r@  r   r   r   test_from_file_bytes
  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   )r4   r  rC   r  r  r  rX   r  r  rV   r   r  rg   r  r   r6  )r7   r   rA  r   r   r   test_from_file_fileno
  s   "z"ZipInfoTests.test_from_file_filenoc                 C   s`   t jt jt}tj|d}| |j	d | 
|  | |jtj | |jd d S )NZstdlib_testszstdlib_tests/r   )r  r   r  abspathr  rC   r  r  rX   rV   ra   r  r   r   rg   )r7   r  rA  r   r   r   test_from_dir
  s   zZipInfoTests.test_from_dirN)r   r   r   r  r  r  r  r  r   r   r   r   r  
  s    r  c                   @   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-mrC   r%  )r   r   re  r  linesepr  )r7   argsrF   rcouterrr   r   r   
zipfilecmd
  s   zCommandLineTest.zipfilecmdc                 G   s   t jddg|R  S )Nr  rC   )r   Zassert_python_failure)r7   r  r   r   r   zipfilecmd_failure  r  z"CommandLineTest.zipfilecmd_failurec                 C   s|   |   \}}}| |d | d|  | d|  | d|  |  dd\}}}| |d | | d d S )Nr'   s   usages   errors   required-lr4  )r  rX   r_   r[  ZassertNotEqualstrip)r7   r  r  r  r   r   r   test_bad_use  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  rX   rstripr  )r7   zip_nameoptr  r  r  r   r   r   test_test_command  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  rJ   r!   backslashreplace)r  z--listzascii:backslashreplace)ZPYTHONIOENCODING)
r   r   rZ   rC   rD   r[   r\   r  r  rX   )r7   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)-cz--creater'   r   s   test 1r   s   test 2)r  r   r   r4   r5   r  r  	TESTFNDIRr   r   r+   r  r   rX   rC   rD   re   rY   )r7   r   filesre   r  r  rk  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    n1 sdw   Y  q#W d    n1 stw   Y  W d    n1 sw   Y  qd S )Nr  )z-ez	--extractr'   r  r   )r   r   r  rX   rC   rD   rf   r  r   r+   rV   re  r  r  ra   r  r  r4   rY   )	r7   r  r  r  r  rk  rA  r   r   r   r   r   test_extract_command8  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   r9   M  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)ra   rC   r\  rD   re   rY   r_   )r7   r  rG   r  r,   r   r   r   _test_zip_worksQ  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_   r7   outputr   r   r   test_execute_zip2a     z,TestExecutablePrependedZip.test_execute_zip2c                 C   r  r  )r  r  r  r  r  r_   r  r   r   r   test_execute_zip64h  r  z-TestExecutablePrependedZip.test_execute_zip64N)r   r   r   rv  r9   r  r  r  r  Z
skipUnlessr  r  r  r  X_OKr  r  r   r   r   r   r  J  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 r  )r   iter
_orig_iter)r7   r%   r   r   r   r  x  s   z!jaraco.itertools.Counter.__init__c                 C   r  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   )r7   resultr   r   r   __next__  s   
z!jaraco.itertools.Counter.__next__N)r   r   r   r  r  r  r   r   r   r   Counterw  s    r  N)r   r   r   r  r   r   r   r   rm  v      rm  N)r   r   r   rm  r   r   r   r   r  u  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'   )rC   CompleteDirs_implied_dirsre   rE   )rk  r  r   r   r   add_dirs  s   r  c                  C   sZ   t  } t| d}|dd |dd |dd |dd	 |d
d d|_|S )u:  
    Create a zip file with this structure:

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

    This fixture has the following key characteristics:

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

    "alpha" because it uses alphabet
    "rep" because it's a representative example
    r=   r   s   content of azb/c.txts   content of cz	b/d/e.txts   content of ezb/f.txts   content of fz	g/h/i.txts   content of izalpharep.zip)r   r   rC   rD   rE   rV   )r,   rk  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)r7   r  methr   r   r    s   zpass_alpharep.<locals>.wrapper)	functoolswraps)r  r  r   r
  r   pass_alpharep  s   r  c                   @   s  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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/d0 Zed1d2 Zed3d4 Zd5Zd6d7 Zd8d9 Z d:d; Z!ed<d= Z"ed>d? Z#ed@dA Z$edBdC Z%edDdE Z&edFdG Z'edHdI Z(edJdK Z)dLdM Z*dNdO Z+dPdQ Z,dRS )STestPathc                 C   s   t  | _| | jj d S r   )r  r  fixturesr  r   r   r   r   r   r9     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   )r  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 r1   )r   r   r  r  r   r8   r   rV   r4   r5   r\   )r7   r  Ztmpdirr"  r   strmr   r   r   zipfile_ondisk  s   

zTestPath.zipfile_ondiskc                 C   sH   t |}| \}}}| \}}}| \}	| \}
|
 \}d S r   )rC   r   iterdir)r7   r  rootr   rT  grq  r   r   r]  hr%   r   r   r   test_iterdir_and_types  s   


zTestPath.test_iterdir_and_typesc                 C      t |}d S r   rC   r   r7   r  r  r   r   r   test_is_file_missing     zTestPath.test_is_file_missingc                 C   sP   t |}| \}}}| t |  W d    d S 1 s!w   Y  d S r   )rC   r   r  r   r   r7   r  r  r   rT  r  r   r   r   test_iterdir_on_file  s
   

"zTestPath.test_iterdir_on_filec                 C   r  r   r  r  r   r   r   test_subdir_is_dir  r  zTestPath.test_subdir_is_dirc                 C   s   t |}| \}}}|jdd}| }W d    n1 s!w   Y  | |d |dd}| }W d    n1 sAw   Y  | |d d S )Nr   r   zcontent of arJ   )rC   r   r  r4   rY   rX   )r7   r  r  r   rT  r  r  r,   r   r   r   r     s   


zTestPath.test_openc                 C   s   t  }t|d}|ddd d|_t|}| \}|	d}|
dd}| }W d    n1 s:w   Y  | |d |j
dd}| }W d    n1 sZw   Y  | |d d S )	Nr=   zpath/16.txtzThis was utf-16zutf-16ztest_open_utf16.zipz16.txtrJ   r   )r   r   rC   rD   rE   r  rV   r   r  joinpathr4   rY   rX   )r7   in_memory_filerk  r  r   u16r  r,   r   r   r   test_open_encoding_utf16  s   




z!TestPath.test_open_encoding_utf16c              	   C   sP  t  }t|d}|dd d|_t|}| \}|d}|j	ddd}| 
|d	 |jd
ddd}| 
| d W d    n1 sJw   Y  | td |j	ddd}W d    n1 sgw   Y  |jd
ddd*}| t |  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr=   zpath/bad-utf8.bins   invalid utf-8: .z"test_read_text_encoding_errors.zipzbad-utf8.binr   ignore)errorszinvalid utf-8: .rJ   surrogateescapeu   invalid utf-8: .r   r   strict)r   r%  )r   r   rC   rD   rE   rV   r   r  r   	read_textrX   r4   rY   r_  r8  r   UnicodeDecodeError)r7   r!  rk  r  r   r"  r,   r   r   r   r   test_open_encoding_errors  s,   



"z"TestPath.test_open_encoding_errorsc                 C   sT   d}t ddd|}|j }| t|d|j | |d d | |d d	 d
S )z8EncodingWarning must blame the read_text and open calls.a  import io, zipfile
with zipfile.ZipFile(io.BytesIO(), "w") as zf:
    zf.filename = '<test_encoding_warnings in memory zip file>'
    zf.writestr("path/file.txt", b"Spanish Inquisition")
    root = zipfile.Path(zf)
    (path,) = root.iterdir()
    file_path = path.joinpath("file.txt")
    unused = file_path.read_text()  # should warn
    file_path.open("r").close()  # should warn
z-Xwarn_default_encodingr  r   r   s   ^<string>:8: EncodingWarning:rP   s   ^<string>:9: EncodingWarning:N)r   r  r]   rX   r^   ZassertRegex)r7   codeprocwarningsr   r   r   test_encoding_warnings,  s   
zTestPath.test_encoding_warningsc                 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=   rA   zfile.binr2   s   binary contentsNr  r   r   z	text file)rC   r   rD   r   r   r   r4   r5   )r7   rk  r  r   r   r   r  ?  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.
        rT  N)rC   r   r  r  r   IsADirectoryErrorr   r4   r  r   r   r   test_open_extant_directoryJ     "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   )rC   r   r   r   r   r4   r  r   r   r   test_open_binary_invalid_argsR  s   
"z&TestPath.test_open_binary_invalid_argsc                 C   r0  )zR
        Attempting to open a missing directory raises FileNotFoundError.
        zN)rC   r   r  r  r   FileNotFoundErrorr   r4   r  r   r   r   test_open_missing_directoryZ  r3  z$TestPath.test_open_missing_directoryc                 C   s&   t |}| \}}}|d d S )Nr   )rC   r   r  r(  r  r   r   r   rz  b  s   
zTestPath.test_readc                 C   s.   t |}|d}|ddd}d S Nr   rT  r   e.txtrC   r   r   r7   r  r  r   r]  r   r   r   test_joinpathj  s   

zTestPath.test_joinpathc                 C   s   t |}|ddd}d S )NrT  r   r9  r:  )r7   r  r  r]  r   r   r   test_joinpath_multipler     
zTestPath.test_joinpath_multiplec                 C   s&   t |}|d }|d d d }d S r8  r  r;  r   r   r   test_traverse_truedivx  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)sourcerV   rB   dont_inheritN)compiler@  )r7   r  r,  r   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   rd   rC   )r7   r  r  Zpathliker   r   r   test_pathlike_construction  s   
z#TestPath.test_pathlike_constructionc                 C   s   t |}|td  d S r  )rC   r   r   r  r   r   r   test_traverse_pathlike  r>  zTestPath.test_traverse_pathlikec                 C   r  r   r  r  r   r   r   test_parent  r  zTestPath.test_parentc                 C   r  r   r  r  r   r   r   test_dir_parent  r  zTestPath.test_dir_parentc                 C   r  r   r  r  r   r   r   test_missing_dir_parent  r  z TestPath.test_missing_dir_parentc                 C   sB   t |}| \}}}|dd |dd |d  \}dS )zk
        If the underlying zipfile is changed, the Path object should
        reflect that change.
        r	  r   zbar/baz.txtrG  r  N)rC   r   r  rE   )r7   r  r  r   rT  r  rG  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=   rJ   )
r   r   rC   rD   maprd   r(   HUGE_ZIPFILE_NUM_ENTRIESrE   rB   )r7   r  rk  entryr   r   r   huge_zipfile  s   zTestPath.huge_zipfilec                 C   s6   t |  }tj| }|D ]}|d qdS )zE
        Ensure joinpath on items in zipfile is linear time.
        suffixN)rC   r   rN  r  rm  r  r  r   )r7   r  entriesrM  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_lowercaserd   )r$   r  r   r   r   r&     s     z:TestPath.test_implied_dirs_performance.<locals>.<listcomp>r  )r(   rC   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  rC   rD   r(   r   r(  )r7   r  rK   Zrepr   r   r   test_read_does_not_close  s   
"z!TestPath.test_read_does_not_closec                 C   s   G dd dt 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    rV  r  )r7   r  rV  r  r   r   r   test_subclass  s   zTestPath.test_subclassc                 C   r  r   r  r  r   r   r   test_filename  r  zTestPath.test_filenamec                 C   s   t |}dS )zH
        The name of the root should be the name of the zipfile
        Nr  r  r   r   r   test_root_name  s   zTestPath.test_root_namec                 C   s   t |}d|j_d S )Nzfoo/bar.zip)rC   r   r  rV   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 }dS )zd
        It is an error to attempt to get the name
        or parent of an unnamed zipfile.
        NrT  )rV   rC   r   r   r8  r  parent)r7   r  r  subr   r   r   test_root_unnamed  s   
zTestPath.test_root_unnamedc                 C   s2   t dtjfi }|  D ]
}||dj}qd S )NZ	PathChildzsome dir)typerC   r   r	  r   r[  )r7   r  r.   rK   r   r   r   test_inheritance  s   zTestPath.test_inheritancec                 C   s.   |  |}t|}t| ||j dS )zU
        A zip file wrapped in a Path should extract even with implied dirs.
        N)r  rC   rD   r   r  r[  )r7   r  source_pathrk  r   r   r   #test_extract_orig_with_implied_dirs  s   


z,TestPath.test_extract_orig_with_implied_dirsc                 C   sL   t  }t|d}|dd |dd |dd d|_t|}dS )z
        Path should handle malformed paths gracefully.

        Paths with leading slashes are not visible.

        Paths with dots are treated like regular files.
        r=   z/one-slash.txtr|  z//two-slash.txtz../parent.txtr4  N)r   r   rC   rD   rE   rV   r   )r7   r,   rk  r  r   r   r   test_malformed_paths  s   zTestPath.test_malformed_pathsc                 C   sP   t  }t|d}|dd |dd d|_t|}| }t|}dS )z
        Path segments with special characters are readable.

        On some platforms or file systems, characters like
        ``:`` and ``?`` are not allowed, but they are valid
        in the zip file.
        r=   zpath?r|  zV: NMS.flacs   fLaC...r4  N)	r   r   rC   rD   rE   rV   r   r  r  )r7   r,   rk  r  contentsitemr   r   r   test_unsupported_names/  s   
zTestPath.test_unsupported_namesc                 C   sF   t  }t|d}|td|d d|_t|}|	 \}dS )z@
        In a zip file, backslashes are not separators.
        r=   zfoo\barr|  r4  N)
r   r   rC   rD   rE   DirtyZipInfofor_namerV   r   r  )r7   r,   rk  r  firstr   r   r   test_backslash_not_separatorC  s   
z%TestPath.test_backslash_not_separatorN)-r   r   r   r9   r	  r  r  r  r  r  r  r   r#  r*  r/  r  r2  r4  r7  rz  r<  r=  r?  rD  rE  rF  rG  rH  rI  rJ  rL  rN  rQ  rT  rU  rW  rX  rY  rZ  r]  r_  ra  rb  re  ri  r   r   r   r   r    s    	

















	








r  c                       s,   e Zd ZdZ fddZedd Z  ZS )rf  z#
    Bypass name sanitization.
    c                    s$   t  j|g|R i | || _d S r   )r   r  rV   )r7   rV   r  rF   r   r   r   r  V  s   
zDirtyZipInfo.__init__c                 C   s\   | |t t   dd d}|j|_|j|_|jdr)d|_| jdO  _|S d|_|S )zy
        Construct the same way that ZipFile.writestr does.

        TODO: extract this functionality and re-use
        N   )rV   r  r  i  Ar   r   )	rb   r  r;   r   r<   Zcompress_levelrV   r  r   )r.   r  archiver7   r   r   r   rg  Z  s   zDirtyZipInfo.for_name)r   r   r   rv  r  r   rg  r   r   r   r   r   rf  Q  s
    rf  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )StripExtraTestsrP   c                 C   s   t d}|| jd}|dd}|dd}| dt|| jf | |t|| jf | |d t|d | jf | || t|| | | jf | || t|| | | jf | || t|| | | jf d S )NrC  r   r   r+  r'      zr  StructrL  ZIP64_EXTRArX   rC   _strip_extrar7   r  r   rT  rq  r   r   r   test_no_datas  s   
$$(zStripExtraTests.test_no_datac                 C   s   t d}|| jdd }|ddd }|ddd }| dt|| jf | |t|| jf | |d	 t|d	 | jf | || t|| | | jf | || t|| | | jf | || t|| | | jf d S )
NrC  rP   rQ  r      bbr+  s   cccr'   rm  rn  rr  r   r   r   test_with_data  s   
$$(zStripExtraTests.test_with_datac                 C   s  t d}|| jdd }|ddd }| dt|| | jf | dt|| | | jf | dt|| d | jf | |d t|| | d | jf | |t|| | | jf | |t|| | | jf | |t|| | | jf d S )NrC  rP   rQ  r   rt  r'   rm  rn  )r7   r  r   rT  r   r   r   test_multiples  s   
    $zStripExtraTests.test_multiplesc                 C   sd   |  dtd| jf |  dtd| jf |  dtd| jf |  dtd| jf d S )Nr'   rm  s   zzs   zzz)rX   rC   rq  rp  r   r   r   r   test_too_short  s   zStripExtraTests.test_too_shortN)r   r   r   rp  rs  ru  rv  rw  r   r   r   r   rl  l  s    rl  __main__)_r  r  importlib.utilr  r   rm  r  r   r  rR  r  r  r  Ztest.support.script_helperr   rb   r  Zunittest.mockrR  rC   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#  r8  rs  ru  rv  rx  r  r  r  r  r  r  r  r[  rb  rc  re  rf  rg  rw  r{  r|  r}  r~  r  r  r  r  r  r  r  r  tupleZconsumer  r  r  r  r  r  rf  rl  r   mainr   r   r   r   <module>   s
   	    C   R" B A    Vg[
7}A#P'
$   
>