o
    Óó·i//  ã                   @   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   úhC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   Z	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   ZassertIsÚiterZ
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   r.   r"   )r   r   r0   ZoneZ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.
        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                   @   ó   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   ó    
r?   c                   @   r>   )ÚZipFileEntryTestsz*
    ZipFileEntry should be file-like
    N)r:   r;   r<   r=   r   Ú
ZIP_STOREDr   r   r   r   r   rB   z   rA   rB   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   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)rF   rH   Úpathr   r   Ú	enumerateÚstrr   )r   r   Ú	directoryÚ
zpfilenameZ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.
        rT   r	   N)r   r   rS   Úcloser'   rU   r   rV   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)rS   r   r   ÚgetinfoÚheader_offsetÚopenÚseekÚwriter   r   r'   Ú
BadZipFiler   r"   r.   )r   ÚfnÚzfZ
zeroOffsetÚscribblerW   Ú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 contentsr^   r	   r[   znot zeroNr\   r   r]   )rS   r   r   r_   r   ra   rb   r`   rc   Ú
FileHeaderr   r   r'   rd   r   r"   r.   )r   re   rf   Úinforg   rW   rh   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   r[   z	some dataiÒ  N)r
   r   r   ÚZipInfor   Úcompress_typer   r   r'   rd   r   )r   re   rf   ÚzirW   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   r[   s   hello, extraz<hhé*   s   the real dataN)r
   r   r   rm   ÚstructÚpackÚlenÚextrar   r   r   r   r"   r.   )r   re   rf   ro   Ú
extra_datarW   rh   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   ©Ú.0rR   r   r   r   Ú
<listcomp>÷   ó    z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>c                 S   ó   g | ]}|  d ¡‘qS ©Úascii©Úencoder{   r   r   r   r}   ø   ó    N)ÚrangerS   Úlistr   ÚunzipIterChunkyrI   rM   r"   ÚsetÚlistdirÚmaprO   ÚchildrenÚintÚbasenameÚ
getContent)r   Únumfilesr   rQ   rH   Ú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.
        rx   c                 S   ry   rz   r   r{   r   r   r   r}     r~   z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>c                 S   r   r€   r‚   r{   r   r   r   r}   	  r„   Zfooc                 S   s   h | ]	}t |ƒ d ¡’qS r€   )rO   rƒ   )r|   r�   r   r   r   Ú	<setcomp>  s    z?ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<setcomp>s   fooN)r…   rS   r†   r   r‡   rI   rM   r"   rˆ   rH   r‰   r‹   rŒ   r�   rŽ   )r   r�   r   rQ   ZfileContentsrH   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ÚrandomrO   rƒ   r   Ú	hexdigestr   rE   r
   rG   rH   rM   Ú_makebigfiler   r‡   r&   r   r"   ra   r.   )r   r   r–   ÚlowerÚupperÚjunkÚnr�   Z
numEncodedZjunkmd5ÚtempdirZzfpathZuziterr	   ZapproxÚfZ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   rC   rJ   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   r@   rJ   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   rx   zzipstream%dr   r—   N)r   r   r…   r   )r   r   r   rž   rf   rR   re   r   r   r   r›   C  s   "üzZipstreamTests._makebigfileN)r   )r:   r;   r<   r=   rK   rS   rX   rZ   ri   rl   rp   rw   r‘   r“   r¢   r¦   rª   r›   r   r   r   r   rD   ‚   s     

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