o
    ¯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w   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   úD/usr/lib/python3/dist-packages/twisted/python/test/test_zipstream.pyÚgetFileEntry   s   ÿ
zFileEntryMixin.getFileEntryc                 C   s>   |   d¡}|  | ¡ ¡ W d  ƒ dS 1 sw   Y  dS )zK
        zip files should not be ttys, so isatty() should be false
        Ú N)r   ÚassertFalseÚisatty©r   Ú	fileEntryr   r   r   Útest_isatty    s   "ÿzFileEntryMixin.test_isattyc                 C   sF   |   d¡}|  |j¡ W d  ƒ n1 sw   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  ƒ dS 1 s)w   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  ƒ dS 1 s(w   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   sD   |   d¡}|  | ¡ g d¢¡ W d  ƒ dS 1 sw   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   sP   |   d¡}|  t|ƒ|¡ |  | ¡ |¡ W d  ƒ dS 1 s!w   Y  dS )zJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        r   N)r   ÚassertIsÚiterÚ
xreadlinesr   r   r   r   Útest_iterationJ   s   "þzFileEntryMixin.test_iterationc                 C   sD   d}|   |¡}|  | ¡ |¡ W d  ƒ dS 1 sw   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 sw   Y  |  |d¡ |  |d¡ dS )zD
        C{.read(num)} should read num bytes from the file.
        Ú
0123456789é   éÈ   Ns   0123s   456789)r   r1   r#   )r   r   r3   ÚoneÚtwor   r   r   Útest_readPartialZ   s   
þzFileEntryMixin.test_readPartialc                 C   sh   d}|   |¡#}| d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ W d  ƒ dS 1 s-w   Y  dS )zb
        C{.tell()} should return the number of bytes that have been read so
        far.
        Údxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxé   r6   é   N)r   r1   r#   Útellr2   r   r   r   Ú	test_telle   s   

"üzFileEntryMixin.test_tellN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r%   r*   r,   r0   r4   r:   r?   r   r   r   r   r      s    
	
	r   c                   @   ó   e Zd ZdZejZdS )ÚDeflatedZipFileEntryTestsz2
    DeflatedZipFileEntry should be file-like
    N)r@   rA   rB   rC   r   ÚZIP_DEFLATEDr   r   r   r   r   rE   r   ó    
rE   c                   @   rD   )ÚZipFileEntryTestsz*
    ZipFileEntry should be file-like
    N)r@   rA   rB   rC   r   Ú
ZIP_STOREDr   r   r   r   r   rH   z   rG   rH   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
        ÚunzippedN)r   ÚFilePathr
   ÚtestdirÚmakedirsÚchildÚunzipdir©r   r   r   r   ÚsetUp‡   s   
zZipstreamTests.setUpr   c                 C   sv   | j  d¡j}t |d¡$}t|ƒD ]\}}t|ƒ}|r"|d | }| ||¡ qW d  ƒ |S 1 s4w   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)rM   rO   Úpathr   r   Ú	enumerateÚstrr   )r   r   Ú	directoryÚ
zpfilenameÚzpfileÚir   r   r   r   r   ÚmakeZipFile‘   s   ü
ÿúzZipstreamTests.makeZipFilec                 C   sF   t  |  ¡ d¡}|  t|jd¡ W d  ƒ dS 1 sw   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.
        r\   r	   N)r   r   r[   Úcloser(   r]   r   r^   r   r   r   Útest_closedArchive§   s   z!ZipstreamTests.test_closedArchivec              	   C   s  |   ddg¡}t |d¡}| d¡j}W d  ƒ n1 sw   Y  t|dƒ}| |d¡ | d¡ W d  ƒ n1 s>w   Y  t 	|¡7}|  
tj|jd¡ | d	¡}|  | ¡ d
¡ W d  ƒ n1 sjw   Y  W d  ƒ dS W d  ƒ dS 1 s‚w   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)r[   r   r   ÚgetinfoÚheader_offsetÚopenÚseekÚwriter   r   r(   Ú
BadZipfiler   r#   r1   )r   ÚfnÚzfÚ
zeroOffsetÚscribbler_   Úzfer   r   r   Útest_invalidHeader°   s    ÿþÿþ"þz!ZipstreamTests.test_invalidHeaderc              	   C   s  |   ddg¡}t |d¡}| d¡}d|_W d  ƒ n1 s w   Y  t|dƒ}| |jd¡ | | 	¡ ¡ W d  ƒ n1 sCw   Y  t
 |¡7}|  tj|jd¡ | d	¡}|  | ¡ d¡ W d  ƒ n1 sow   Y  W d  ƒ dS W d  ƒ dS 1 s‡w   Y  dS )
zƒ
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        s   test contentsrf   r	   rc   znot zeroNrd   r   re   )r[   r   r   rg   r   ri   rj   rh   rk   Ú
FileHeaderr   r   r(   rl   r   r#   r1   )r   rm   rn   Úinforp   r_   rq   r   r   r   Útest_filenameMismatchÁ   s"   
þþÿþ"þz$ZipstreamTests.test_filenameMismatchc                 C   s’   |   ¡ }t |d¡}t d¡}| |d¡ d|_W d  ƒ n1 s#w   Y  t |¡}|  tj	|j
d¡ W d  ƒ dS 1 sBw   Y  dS )zq
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        r   rc   z	some dataiÒ  N)r
   r   r   ÚZipInfor   Úcompress_typer   r   r(   rl   r   )r   rm   rn   Úzir_   r   r   r   Útest_unsupportedCompressionÓ   s   
ú"ÿz*ZipstreamTests.test_unsupportedCompressionc              	   C   sà   |   ¡ }t |d¡!}t d¡}d}t ddt|ƒ¡| |_| |d¡ W d  ƒ n1 s.w   Y  t	 
|¡.}| d¡}|  | ¡ d¡ W d  ƒ n1 sQw   Y  W d  ƒ dS W d  ƒ dS 1 siw   Y  dS )zW
        readfile() should skip over 'extra' data present in the zip metadata.
        r   rc   s   hello, extraz<hhé*   s   the real dataN)r
   r   r   rv   ÚstructÚpackÚlenÚextrar   r   r   r   r#   r1   )r   rm   rn   rx   Ú
extra_datar_   rq   r   r   r   Útest_extraDataä   s   
üPÿ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| ¡ ƒ}|  | ¡ || ¡ q8dS )z˜
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        é
   c                 S   ó   g | ]}d | ‘qS ©zThis is test file %d!r   ©Ú.0rZ   r   r   r   Ú
<listcomp>÷   ó    z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>c                 S   ó   g | ]}|  d ¡‘qS ©Úascii©Úencoder„   r   r   r   r†   ø   ó    N)Úranger[   Úlistr   ÚunzipIterChunkyrP   rT   r#   ÚsetÚlistdirÚmaprV   ÚchildrenÚintÚbasenameÚ
getContent)r   Únumfilesr   rX   rO   Ú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| ¡ ƒ}|  | ¡ || ¡ qAd	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.
        r   c                 S   r‚   rƒ   r   r„   r   r   r   r†     r‡   z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>c                 S   rˆ   r‰   r‹   r„   r   r   r   r†   	  r   Úfooc                 S   s   h | ]	}t |ƒ d ¡’qS r‰   )rV   rŒ   )r…   r™   r   r   r   Ú	<setcomp>  s    z?ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<setcomp>s   fooN)rŽ   r[   r   r   r   rP   rT   r#   r‘   rO   r’   r”   r•   r–   r—   )r   r˜   r   rX   ÚfileContentsrO   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 oQ|k n  }|  |¡ |D ]}q[|  |d¡ |
 d	¡ ¡ }t| ¡ ƒ ¡ }|  ||	¡ W d
  ƒ d
S 1 s…w   Y  d
S )zW
        unzipIterChunky should unzip the given number of bytes per iteration.
        r"   iè  é   rŠ   ó    zbigfile.zip)Ú	chunksizer   ÚzipstreamjunkN)rŽ   ÚroundÚrandomrV   rŒ   r   Ú	hexdigestr   rL   r
   rN   rO   rT   Ú_makebigfiler   r   r'   r   r#   ri   r1   )r   r   r¡   ÚlowerÚupperÚjunkÚnr™   Ú
numEncodedÚjunkmd5ÚtempdirÚzfpathÚuziterr	   ÚapproxÚfÚnewmd5r   r   r   Ú_unzipIterChunkyTest  s*   
"þz#ZipstreamTests._unzipIterChunkyTestc                 C   ó   |   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   rI   rQ   r   r   r   Útest_unzipIterChunkyStored5  ó   z)ZipstreamTests.test_unzipIterChunkyStoredc                 C   r´   )zu
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        iÌ  é   é   N)r³   r   rF   rQ   r   r   r   Útest_chunkyDeflated<  r¸   z"ZipstreamTests.test_chunkyDeflatedc                 C   sb   t  |d|¡ }tdƒD ]}d| }| |d¡ q| d|¡ W d  ƒ dS 1 s*w   Y  dS )zT
        Create a zip file with the given file name and compression scheme.
        r   r   zzipstream%dr   r¢   N)r   r   rŽ   r   )r   r   r   r©   rn   rZ   rm   r   r   r   r¦   C  s   "üzZipstreamTests._makebigfileN)r   )r@   rA   rB   rC   rR   r[   r`   rb   rr   ru   ry   r€   rš   rž   r³   r·   r»   r¦   r   r   r   r   rJ   ‚   s     

	rJ   )rC   r¤   r{   r   Úhashlibr   Útwisted.pythonr   r   Útwisted.trialr   r   ÚTestCaserE   rH   rJ   r   r   r   r   Ú<module>   s   a