a
    )¶(b//  ã                   @   s   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 G dd„ dƒZG dd	„ d	ee
jƒZG d
d„ dee
jƒZG dd„ de
jƒZdS )z'
Tests for L{twisted.python.zipstream}
é    N)Úmd5)ÚfilepathÚ	zipstream)Úunittestc                   @   s`   e Zd 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S )ÚFileEntryMixinz?
    File entry classes should behave as file-like objects
    c                 C   sZ   |   ¡ }t |d| j¡}| d|¡ W d  ƒ n1 s:0    Y  t |d¡}| d¡S )z6
        Return an appropriate zip file entry
        ÚwÚcontentNÚr)ÚmktempÚzipfileÚZipFileÚcompressionÚwritestrr   ÚChunkingZipFileÚreadfile)ÚselfÚcontentsÚfilenameÚz© r   úAlib/python3.9/site-packages/twisted/python/test/test_zipstream.pyÚgetFileEntry   s
    *zFileEntryMixin.getFileEntryc                 C   s<   |   d¡}|  | ¡ ¡ W d  ƒ n1 s.0    Y  dS )zK
        zip files should not be ttys, so isatty() should be false
        Ú N)r   ÚassertFalseÚisatty©r   Z	fileEntryr   r   r   Útest_isatty    s    zFileEntryMixin.test_isattyc                 C   sF   |   d¡}|  |j¡ W d  ƒ n1 s,0    Y  |  |j¡ dS )zd
        The C{closed} attribute should reflect whether C{close()} has been
        called.
        r   N)r   r   ÚclosedÚ
assertTruer   r   r   r   Útest_closed'   s    *zFileEntryMixin.test_closedc                 C   s^   |   d¡@}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W d  ƒ n1 sP0    Y  dS )zk
        C{readline()} should mirror L{file.readline} and return up to a single
        delimiter.
        s   hoho
hos   hoho
ó   hoó    N)r   ÚassertEqualÚreadliner   r   r   r   Útest_readline0   s    zFileEntryMixin.test_readlinec                 C   s\   |   d¡>}|  | ¡ d¡ |  | ¡ d¡ |  t|j¡ W d  ƒ n1 sN0    Y  dS )zV
        Zip file entries should implement the iterator protocol as files do.
        s   ho
hohoó   ho
s   hohoN)r   r"   ÚnextÚassertRaisesÚStopIterationr   r   r   r   Ú	test_next:   s    zFileEntryMixin.test_nextc                 C   sB   |   d¡$}|  | ¡ g d¢¡ W d  ƒ n1 s40    Y  dS )zG
        C{readlines()} should return a list of all the lines.
        s   ho
ho
ho)r%   r%   r    N)r   r"   Ú	readlinesr   r   r   r   Útest_readlinesC   s    zFileEntryMixin.test_readlinesc                 C   sN   |   d¡0}|  t|ƒ|¡ |  | ¡ |¡ W d  ƒ n1 s@0    Y  dS )zJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        r   N)r   ZassertIsÚiterZ
xreadlinesr   r   r   r   Útest_iterationJ   s    zFileEntryMixin.test_iterationc                 C   sB   d}|   |¡ }|  | ¡ |¡ W d  ƒ n1 s40    Y  dS )z9
        C{.read()} should read the entire file.
        s   Hello, world!N)r   r"   Úread©r   r   Úentryr   r   r   Útest_readWholeR   s    zFileEntryMixin.test_readWholec                 C   s^   d}|   |¡$}| d¡}| d¡}W d  ƒ n1 s80    Y  |  |d¡ |  |d¡ dS )zD
        C{.read(num)} should read num bytes from the file.
        Ú
0123456789é   éÈ   Ns   0123s   456789)r   r.   r"   )r   r   r0   ZoneZtwor   r   r   Útest_readPartialZ   s    
(zFileEntryMixin.test_readPartialc                 C   sf   d}|   |¡D}| d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ W d  ƒ n1 sX0    Y  dS )zb
        C{.tell()} should return the number of bytes that have been read so
        far.
        Zdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxé   r3   é   N)r   r.   r"   Útellr/   r   r   r   Ú	test_telle   s    

zFileEntryMixin.test_tellN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r)   r+   r-   r1   r5   r9   r   r   r   r   r      s   
	
	r   c                   @   s   e Zd ZdZejZdS )ÚDeflatedZipFileEntryTestsz2
    DeflatedZipFileEntry should be file-like
    N)r:   r;   r<   r=   r   ÚZIP_DEFLATEDr   r   r   r   r   r>   r   s   r>   c                   @   s   e Zd ZdZejZdS )ÚZipFileEntryTestsz*
    ZipFileEntry should be file-like
    N)r:   r;   r<   r=   r   Ú
ZIP_STOREDr   r   r   r   r   r@   z   s   r@   c                   @   s‚   e Zd Z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d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!ÚZipstreamTestsz,
    Tests for twisted.python.zipstream
    c                 C   s6   t  |  ¡ ¡| _| j ¡  | j d¡| _| j ¡  dS )zz
        Creates junk data that can be compressed and a test directory for any
        files that will be created
        ZunzippedN)r   ÚFilePathr
   ÚtestdirÚmakedirsÚchildÚunzipdir©r   r   r   r   ÚsetUp‡   s    
zZipstreamTests.setUpr   c                 C   st   | j  d¡j}t |d¡F}t|ƒD ],\}}t|ƒ}|rD|d | }| ||¡ q$W d  ƒ n1 sf0    Y  |S )z¤
        Makes a zip file archive containing len(contents) files.  Contents
        should be a list of strings, each string being the content of one file.
        zzipfile.zipr   ú/N)rD   rF   Úpathr   r   Ú	enumerateÚstrr   )r   r   Z	directoryÚ
zpfilenameZzpfileÚir   r   r   r   r   ÚmakeZipFile‘   s    ,zZipstreamTests.makeZipFilec                 C   sD   t  |  ¡ d¡ }|  t|jd¡ W d  ƒ n1 s60    Y  dS )z€
        A ChunkingZipFile opened in write-mode should not allow .readfile(),
        and raise a RuntimeError instead.
        r   Ú	somethingN)r   r   r
   r'   ÚRuntimeErrorr   ©r   Úczfr   r   r   Útest_invalidModeŸ   s    zZipstreamTests.test_invalidModec                 C   s0   t  |  dg¡d¡}| ¡  |  t|jd¡ dS )zn
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        rQ   r	   N)r   r   rP   Úcloser'   rR   r   rS   r   r   r   Útest_closedArchive§   s    z!ZipstreamTests.test_closedArchivec              	   C   s   |   ddg¡}t |d¡}| d¡j}W d  ƒ n1 s<0    Y  t|dƒ&}| |d¡ | d¡ W d  ƒ n1 s|0    Y  t 	|¡\}|  
tj|jd¡ | d	¡ }|  | ¡ d
¡ W d  ƒ n1 sÔ0    Y  W d  ƒ n1 sò0    Y  dS )z¤
        A zipfile entry with the wrong magic number should raise BadZipfile for
        readfile(), but that should not affect other files in the archive.
        ztest contentszmore contentsr	   Ú0Núr+br   s   0000Ú1ó   more contents)rP   r   r   ÚgetinfoÚheader_offsetÚopenÚseekÚwriter   r   r'   Ú
BadZipfiler   r"   r.   )r   ÚfnÚzfZ
zeroOffsetÚscribblerT   Úzfer   r   r   Útest_invalidHeader°   s    *(z!ZipstreamTests.test_invalidHeaderc              	   C   s
  |   ddg¡}t |d¡ }| d¡}d|_W d  ƒ n1 s@0    Y  t|dƒ,}| |jd¡ | | 	¡ ¡ W d  ƒ n1 s†0    Y  t
 |¡\}|  tj|jd¡ | d	¡ }|  | ¡ d¡ W d  ƒ n1 sÞ0    Y  W d  ƒ n1 sü0    Y  dS )
zƒ
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        s   test contentsr[   r	   rX   znot zeroNrY   r   rZ   )rP   r   r   r\   r   r^   r_   r]   r`   Z
FileHeaderr   r   r'   ra   r   r"   r.   )r   rb   rc   Úinford   rT   re   r   r   r   Útest_filenameMismatchÁ   s    
$,z$ZipstreamTests.test_filenameMismatchc                 C   s   |   ¡ }t |d¡,}t d¡}| |d¡ d|_W d  ƒ n1 sF0    Y  t |¡"}|  tj	|j
d¡ W d  ƒ n1 s‚0    Y  dS )zq
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        r   rX   z	some dataiÒ  N)r
   r   r   ÚZipInfor   Zcompress_typer   r   r'   ra   r   )r   rb   rc   ÚzirT   r   r   r   Útest_unsupportedCompressionÓ   s    
$z*ZipstreamTests.test_unsupportedCompressionc              	   C   sÎ   |   ¡ }t |d¡B}t d¡}d}t ddt|ƒ¡| |_| |d¡ W d  ƒ n1 s\0    Y  t	 
|¡J}| d¡ }|  | ¡ d¡ W d  ƒ n1 s¢0    Y  W d  ƒ n1 sÀ0    Y  dS )zW
        readfile() should skip over 'extra' data present in the zip metadata.
        r   rX   s   hello, extraz<hhé*   s   the real dataN)r
   r   r   ri   ÚstructZpackÚlenZextrar   r   r   r   r"   r.   )r   rb   rc   rj   Z
extra_datarT   re   r   r   r   Útest_extraDataä   s    
*zZipstreamTests.test_extraDatac                 C   sš   d}dd„ t |ƒD ƒ}dd„ |D ƒ}|  |¡}tt || jj¡ƒ |  t| j 	¡ ƒtt
tt |ƒƒƒ¡ | j ¡ D ]$}t| ¡ ƒ}|  | ¡ || ¡ qpdS )z˜
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        é
   c                 S   s   g | ]}d | ‘qS ©zThis is test file %d!r   ©Ú.0rO   r   r   r   Ú
<listcomp>÷   r!   z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡‘qS ©Úascii©Úencoderr   r   r   r   rt   ø   r!   N)ÚrangerP   Úlistr   ÚunzipIterChunkyrG   rK   r"   ÚsetÚlistdirÚmaprM   ÚchildrenÚintÚbasenameÚ
getContent)r   Únumfilesr   rN   rF   Únumr   r   r   Útest_unzipIterChunkyñ   s    
$z#ZipstreamTests.test_unzipIterChunkyc                 C   s¬   d}dd„ t |ƒD ƒ}dd„ |D ƒ}|  |d¡}tt || jj¡ƒ dd„ t |ƒD ƒ}|  t| j 	d¡ 
¡ ƒ|¡ | j 	d¡ ¡ D ]$}t| ¡ ƒ}|  | ¡ || ¡ q‚d	S )
zä
        The path to which a file is extracted by L{zipstream.unzipIterChunky}
        is determined by joining the C{directory} argument to C{unzip} with the
        path within the archive of the file being extracted.
        rp   c                 S   s   g | ]}d | ‘qS rq   r   rr   r   r   r   rt     r!   z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡‘qS ru   rw   rr   r   r   r   rt   	  r!   Zfooc                 S   s   h | ]}t |ƒ d ¡’qS ru   )rM   rx   )rs   r„   r   r   r   Ú	<setcomp>  r!   z?ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<setcomp>s   fooN)ry   rP   rz   r   r{   rG   rK   r"   r|   rF   r}   r   r€   r   r‚   )r   rƒ   r   rN   ZfileContentsrF   r„   r   r   r   Útest_unzipIterChunkyDirectory  s    z,ZipstreamTests.test_unzipIterChunkyDirectoryc                 C   s  d}t dƒD ],}tt ¡ dƒ}t|ƒ d¡}|d| 7 }qt|ƒ ¡ }	t |  	¡ ¡}
|
 
¡  |
 d¡j}|  |||¡ tj||
j|d}t|ƒ}||  k o¢|k n  }|  |¡ |D ]}q¶|  |d¡ |
 d	¡ ¡ ,}t| ¡ ƒ ¡ }|  ||	¡ W d
  ƒ n1 s
0    Y  d
S )zW
        unzipIterChunky should unzip the given number of bytes per iteration.
        r!   iè  é   rv   ó    zbigfile.zip)Ú	chunksizer   ÚzipstreamjunkN)ry   ÚroundÚrandomrM   rx   r   Z	hexdigestr   rC   r
   rE   rF   rK   Ú_makebigfiler   r{   r&   r   r"   r^   r.   )r   r   rŠ   ÚlowerÚupperÚjunkÚnr„   Z
numEncodedZjunkmd5ZtempdirZzfpathZuziterr	   ZapproxÚfZnewmd5r   r   r   Ú_unzipIterChunkyTest  s(    
z#ZipstreamTests._unzipIterChunkyTestc                 C   s   |   tjddd¡ dS )zs
        unzipIterChunky should unzip the given number of bytes per iteration on
        a stored archive.
        iô  é#   é-   N)r”   r   rA   rH   r   r   r   Útest_unzipIterChunkyStored5  s    z)ZipstreamTests.test_unzipIterChunkyStoredc                 C   s   |   tjddd¡ dS )zu
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        iÌ  é   é   N)r”   r   r?   rH   r   r   r   Útest_chunkyDeflated<  s    z"ZipstreamTests.test_chunkyDeflatedc                 C   s`   t  |d|¡>}tdƒD ]}d| }| |d¡ q| d|¡ W d  ƒ n1 sR0    Y  dS )zT
        Create a zip file with the given file name and compression scheme.
        r   rp   zzipstream%dr   r‹   N)r   r   ry   r   )r   r   r   r‘   rc   rO   rb   r   r   r   rŽ   C  s
    zZipstreamTests._makebigfileN)r   )r:   r;   r<   r=   rI   rP   rU   rW   rf   rh   rk   ro   r…   r‡   r”   r—   rš   rŽ   r   r   r   r   rB   ‚   s   

	rB   )r=   r   rm   r   Zhashlibr   Ztwisted.pythonr   r   Ztwisted.trialr   r   ZTestCaser>   r@   rB   r   r   r   r   Ú<module>   s   a