U
    «`”[0  ã                   @   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eƒ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)Ú	zipstreamÚfilepath)Ú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   sF   |   ¡ }t |d| j¡}| d|¡ W 5 Q R X t |d¡}| d¡S )z6
        Return an appropriate zip file entry
        ÚwÚcontentÚr)ÚmktempÚzipfileÚZipFileÚcompressionÚwritestrr   ÚChunkingZipFileÚreadfile)ÚselfÚcontentsÚfilenameÚz© r   úD/usr/lib/python3/dist-packages/twisted/python/test/test_zipstream.pyÚgetFileEntry   s
    zFileEntryMixin.getFileEntryc              	   C   s(   |   d¡}|  | ¡ ¡ W 5 Q R X 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   s2   |   d¡}|  |j¡ W 5 Q R X |  |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   sJ   |   d¡6}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W 5 Q R X 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_readline2   s    zFileEntryMixin.test_readlinec              	   C   sH   |   d¡4}|  | ¡ d¡ |  | ¡ d¡ |  t|j¡ W 5 Q R X 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   s0   |   d¡}|  | ¡ dddg¡ W 5 Q R X dS )zG
        C{readlines()} should return a list of all the lines.
        s   ho
ho
hor%   r    N)r   r"   Ú	readlinesr   r   r   r   Útest_readlinesG   s    zFileEntryMixin.test_readlinesc              	   C   s:   |   d¡&}|  t|ƒ|¡ |  | ¡ |¡ W 5 Q R X dS )zJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        r   N)r   ZassertIsÚiterZ
xreadlinesr   r   r   r   Útest_iterationO   s    zFileEntryMixin.test_iterationc              	   C   s.   d}|   |¡}|  | ¡ |¡ W 5 Q R X dS )z9
        C{.read()} should read the entire file.
        s   Hello, world!N)r   r"   Úread©r   r   Úentryr   r   r   Útest_readWholeX   s    zFileEntryMixin.test_readWholec              	   C   sJ   d}|   |¡}| d¡}| d¡}W 5 Q R X |  |d¡ |  |d¡ dS )zD
        C{.read(num)} should read num bytes from the file.
        Ú
0123456789é   éÈ   s   0123s   456789N)r   r.   r"   )r   r   r0   ZoneZtwor   r   r   Útest_readPartiala   s    
zFileEntryMixin.test_readPartialc              	   C   sR   d}|   |¡:}| d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ W 5 Q R X 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_tellm   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>   {   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@   ƒ   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   s`   | j  d¡j}t |d¡<}t|ƒD ],\}}t|ƒ}|rD|d | }| ||¡ q$W 5 Q R X |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   ú/)rD   rF   Úpathr   r   Ú	enumerateÚstrr   )r   r   Z	directoryÚ
zpfilenameZzpfileÚir   r   r   r   r   ÚmakeZipFileš   s    zZipstreamTests.makeZipFilec              	   C   s0   t  |  ¡ d¡}|  t|jd¡ W 5 Q R X 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 5 Q R X t|dƒ}| |d¡ | d¡ W 5 Q R X t 	|¡>}|  
tj|jd¡ | d¡}|  | ¡ d	¡ W 5 Q R X W 5 Q R X 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	   Ú0úr+br   s   0000Ú1ó   more contentsN)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 5 Q R X t|dƒ"}| |jd¡ | | 	¡ ¡ W 5 Q R X t
 |¡>}|  tj|jd¡ | d¡}|  | ¡ d¡ W 5 Q R X W 5 Q R X 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 zerorY   r   rZ   N)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   sh   |   ¡ }t |d¡"}t d¡}| |d¡ d|_W 5 Q R X t |¡}|  tj	|j
d¡ W 5 Q R X 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¡8}t d¡}d}t ddt|ƒ¡| |_| |d¡ W 5 Q R X t	 
|¡,}| d¡}|  | ¡ d¡ W 5 Q R X W 5 Q R X 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>  s     z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡‘qS ©Úascii©Úencoderr   r   r   r   rt     s     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      s     z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡‘qS ru   rw   rr   r   r   r   rt   !  s     Zfooc                 S   s   h | ]}t |ƒ d ¡’qS ru   )rM   rx   )rs   r„   r   r   r   Ú	<setcomp>$  s     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 5 Q R X 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   Ú_unzipIterChunkyTest2  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_unzipIterChunkyStoredQ  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_chunkyDeflatedY  s    z"ZipstreamTests.test_chunkyDeflatedc              	   C   sL   t  |d|¡4}tdƒD ]}d| }| |d¡ q| d|¡ W 5 Q R X 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Ž   a  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   Úobjectr   ZTestCaser>   r@   rB   r   r   r   r   Ú<module>   s   j