
    tf//                         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          Z G d d	ee
j                  Z G d
 dee
j                  Z G d de
j                  ZdS )z'
Tests for L{twisted.python.zipstream}
    N)md5)filepath	zipstream)unittestc                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )FileEntryMixinz?
    File entry classes should behave as file-like objects
    c                    |                                  }t          j        |d| j                  5 }|                    d|           ddd           n# 1 swxY w Y   t          j        |d          }|                    d          S )z6
        Return an appropriate zip file entry
        wcontentNr)mktempzipfileZipFilecompressionwritestrr   ChunkingZipFilereadfile)selfcontentsfilenamezs       a/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/python/test/test_zipstream.pygetFileEntryzFileEntryMixin.getFileEntry   s     ;;==_XsD,<== 	,JJy(+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,%h44zz)$$$s   AAAc                     |                      d          5 }|                     |                                           ddd           dS # 1 swxY w Y   dS )zK
        zip files should not be ttys, so isatty() should be false
         N)r   assertFalseisattyr   	fileEntrys     r   test_isattyzFileEntryMixin.test_isatty    s     r"" 	1iY--//000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   (AAAc                     |                      d          5 }|                     |j                   ddd           n# 1 swxY w Y   |                     |j                   dS )zd
        The C{closed} attribute should reflect whether C{close()} has been
        called.
        r   N)r   r   closed
assertTruer   s     r   test_closedzFileEntryMixin.test_closed'   s    
 r"" 	/iY-...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/	()))))s   =AAc                 R   |                      d          5 }|                     |                                d           |                     |                                d           |                     |                                d           ddd           dS # 1 swxY 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   s     r   test_readlinezFileEntryMixin.test_readline0   s    
 {++ 	8yY//119===Y//115999Y//113777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   A9BB #B c                 B   |                      d          5 }|                     |                                d           |                     |                                d           |                     t          |j                   ddd           dS # 1 swxY 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   s     r   	test_nextzFileEntryMixin.test_next:   s     {++ 	=yY^^--w777Y^^--w777mY^<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   A1BBBc                     |                      d          5 }|                     |                                g d           ddd           dS # 1 swxY 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   s     r   test_readlineszFileEntryMixin.test_readlinesC   s     }-- 	OY00224M4M4MNNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	Os   +AAAc                     |                      d          5 }|                     t          |          |           |                     |                                |           ddd           dS # 1 swxY w Y   dS )zJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        r   N)r   assertIsiter
xreadlinesr   s     r   test_iterationzFileEntryMixin.test_iterationJ   s     r"" 	=iMM$y//9555MM)..00)<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   AA//A36A3c                     d}|                      |          5 }|                     |                                |           ddd           dS # 1 swxY w Y   dS )z9
        C{.read()} should read the entire file.
        s   Hello, world!N)r   r(   readr   r   entrys      r   test_readWholezFileEntryMixin.test_readWholeR   s     $x(( 	5EUZZ\\8444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s   )AAAc                    d}|                      |          5 }|                    d          }|                    d          }ddd           n# 1 swxY 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   r<   onetwos        r   test_readPartialzFileEntryMixin.test_readPartialZ   s      x(( 	"E**Q--C**S//C	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	g&&&i(((((s   +AAAc                 Z   d}|                      |          5 }|                    d           |                     |                                d           |                    d           |                     |                                d           ddd           dS # 1 swxY w Y   dS )zb
        C{.tell()} should return the number of bytes that have been read so
        far.
        dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   r@      N)r   r:   r(   tellr;   s      r   	test_tellzFileEntryMixin.test_telle   s    
 x(( 	.EJJqMMMUZZ\\1---JJqMMMUZZ\\1---		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   A;B  B$'B$N)__name__
__module____qualname____doc__r   r    r$   r*   r0   r3   r8   r=   rD   rJ    r'   r   r   r      s         % % %1 1 1* * *8 8 8= = =O O O= = =5 5 5	) 	) 	)
. 
. 
. 
. 
.r'   r   c                        e Zd ZdZej        ZdS )DeflatedZipFileEntryTestsz2
    DeflatedZipFileEntry should be file-like
    N)rK   rL   rM   rN   r   ZIP_DEFLATEDr   rO   r'   r   rQ   rQ   r   s"          &KKKr'   rQ   c                        e Zd ZdZej        ZdS )ZipFileEntryTestsz*
    ZipFileEntry should be file-like
    N)rK   rL   rM   rN   r   
ZIP_STOREDr   rO   r'   r   rT   rT   z   s"          $KKKr'   rT   c                   h    e Zd ZdZd ZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd ZdS )ZipstreamTestsz,
    Tests for twisted.python.zipstream
    c                     t          j        |                                           | _        | 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   s    r   setUpzZipstreamTests.setUp   sc    
  (77**:66     r'   r   c                 (   | j                             d          j        }t          j        |d          5 }t          |          D ]4\  }}t          |          }|r|dz   |z   }|                    ||           5	 ddd           n# 1 swxY w 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)r[   r]   pathr   r   	enumeratestrr   )r   r   	directory
zpfilenamezpfileir   r   s           r   makeZipFilezZipstreamTests.makeZipFile   s    
 \''66;
_Z-- 	3'11 3 3
7q66 :(39H'2222	3	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 s   ABBBc                     t          j        |                                 d          5 }|                     t          |j        d           ddd           dS # 1 swxY 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   czfs     r   test_invalidModezZipstreamTests.test_invalidMode   s    
 &t{{}}c:: 	GclCL+FFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs   "AAAc                     t          j        |                     dg          d          }|                                 |                     t
          |j        d           dS )zn
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        rl   r   N)r   r   rj   closer.   rm   r   rn   s     r   test_closedArchivez!ZipstreamTests.test_closedArchive   sS    
 '(8(8+(G(GMM		,kBBBBBr'   c                    |                      ddg          }t          j        |d          5 }|                    d          j        }ddd           n# 1 swxY w Y   t          |d          5 }|                    |d           |                    d           ddd           n# 1 swxY w Y   t          j	        |          5 }| 
                    t          j        |j        d           |                    d	          5 }|                     |                                d
           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY 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)rj   r   r   getinfoheader_offsetopenseekwriter   r   r.   
BadZipFiler   r(   r:   )r   fnzf
zeroOffsetscribblero   zfes          r   test_invalidHeaderz!ZipstreamTests.test_invalidHeader   s.   
 @AA_R%% 	7C6J	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 "e__ 	%MM*a(((NN8$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% &r** 	?cg0#,DDDc"" ?c  -=>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?sY   AAA/,B''B+.B+<E)D7+E7D;	;E>D;	?EEEc                    |                      ddg          }t          j        |d          5 }|                    d          }d|_        ddd           n# 1 swxY w Y   t          |d          5 }|                    |j        d           |                    |	                                           ddd           n# 1 swxY w Y   t          j        |          5 }|                     t          j        |j        d           |                    d	          5 }|                     |                                d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )
z
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipFile.
        s   test contentsrx   r   ru   znot zeroNrv   r   rw   )rj   r   r   ry   r   r{   r|   rz   r}   
FileHeaderr   r   r.   r~   r   r(   r:   )r   r   r   infor   ro   r   s          r   test_filenameMismatchz$ZipstreamTests.test_filenameMismatch   sA   
 /1ABCC_R%% 	'::c??D&DM	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' "e__ 	.MM$,a000NN4??,,---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. &r** 	?cg0#,DDDc"" ?c  -=>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?sZ   AAA1AC  CC<E()EE(E	E(E	E((E,/E,c                    |                                  }t          j        |d          5 }t          j        d          }|                    |d           d|_        ddd           n# 1 swxY w Y   t          j        |          5 }|                     t          j	        |j
        d           ddd           dS # 1 swxY w Y   dS )zq
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipFile.
        r
   ru   z	some datai  N)r   r   r   ZipInfor   compress_typer   r   r.   r~   r   )r   r   r   ziro   s        r   test_unsupportedCompressionz*ZipstreamTests.test_unsupportedCompression   sF   
 [[]]_R%% 	$%%BKKK(((  $B	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ &r** 	Ecg0#,DDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es#   2A((A,/A,'B;;B?B?c                 :   |                                  }t          j        |d          5 }t          j        d          }d}t	          j        ddt          |                    |z   |_        |                    |d           ddd           n# 1 swxY w Y   t          j
        |          5 }|                    d          5 }|                     |                                d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zW
        readfile() should skip over 'extra' data present in the zip metadata.
        r
   ru   s   hello, extraz<hh*   s   the real dataN)r   r   r   r   structpacklenextrar   r   r   r   r(   r:   )r   r   r   r   
extra_dataro   r   s          r   test_extraDatazZipstreamTests.test_extraData   s    [[]]_R%% 	.%%B(J{5"c*oo>>KBHKK,---		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.
 &r** 	;c3<<3D3D 	;SXXZZ)9:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;sH   ABBB-D)C8,D8C<	<D?C<	 DDDc                 z   d}d t          |          D             }d |D             }|                     |          }t          t          j        || j        j                             |                     t          | j        	                                          t          t          t          t          |                                         | j                                        D ]Q}t          |                                          }|                     |                                ||                    RdS )z
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        
   c                     g | ]}d |z  S zThis is test file %d!rO   .0ri   s     r   
<listcomp>z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>       IIIA+a/IIIr'   c                 8    g | ]}|                     d           S asciiencoder   s     r   r   z7ZipstreamTests.test_unzipIterChunky.<locals>.<listcomp>   $    888!AHHW%%888r'   N)rangerj   listr   unzipIterChunkyr^   rc   r(   setlistdirmapre   childrenintbasename
getContent)r   numfilesr   rg   r]   nums         r   test_unzipIterChunkyz#ZipstreamTests.test_unzipIterChunky   s   
 IIxIII88x888%%h//
Y&z4=3EFFGGGT]224455s3sE(OO;T;T7U7UVVV]++-- 	@ 	@Eenn&&''CU--//#????	@ 	@r'   c                    d}d t          |          D             }d |D             }|                     |d          }t          t          j        || j        j                             d t          |          D             }|                     t          | j        	                    d          
                                          |           | j        	                    d                                          D ]Q}t          |                                          }|                     |                                ||                    Rd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                     g | ]}d |z  S r   rO   r   s     r   r   z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>  r   r'   c                 8    g | ]}|                     d           S r   r   r   s     r   r   z@ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<listcomp>	  r   r'   fooc                 R    h | ]$}t          |                              d           %S r   )re   r   )r   r   s     r   	<setcomp>z?ZipstreamTests.test_unzipIterChunkyDirectory.<locals>.<setcomp>  s*    LLLSC00LLLr'   s   fooN)r   rj   r   r   r   r^   rc   r(   r   r]   r   r   r   r   r   )r   r   r   rg   fileContentsr]   r   s          r   test_unzipIterChunkyDirectoryz,ZipstreamTests.test_unzipIterChunkyDirectory  s6    IIxIII88x888%%h66
Y&z4=3EFFGGGLLE(OOLLLT]0088@@BBCC\RRR]((0099;; 	@ 	@Eenn&&''CU--//#????	@ 	@r'   c                    d}t          d          D ]M}t          t          j                    d          }t          |                              d          }|d|z   z  }Nt          |                                          }	t          j        | 	                                          }
|

                                 |
                    d          j        }|                     |||           t          j        ||
j        |          }t!          |          }||cxk     o|k     nc }|                     |           |D ]}|                     |d           |
                    d	                                          5 }t          |                                                                          }|                     ||	           d
d
d
           d
S # 1 swxY 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randomre   r   r   	hexdigestr   rZ   r   r\   r]   rc   _makebigfiler   r   r-   r#   r(   r{   r:   )r   r   r   lowerupperjunknr   
numEncodedjunkmd5tempdirzfpathuziterr   approxfnewmd5s                    r   _unzipIterChunkyTestz#ZipstreamTests._unzipIterChunkyTest  s    t 	& 	&A,,CS11JD:%%DDd))%%''#DKKMM22}--2&+t444*67<9UUULL """"U"""" 	 	AA]]?++0022 	.a]],,..FVW---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   >A
GGGc                 J    |                      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   rU   r_   s    r   test_unzipIterChunkyStoredz)ZipstreamTests.test_unzipIterChunkyStored5  s'    
 	!!'"4c2rBBBBBr'   c                 J    |                      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   rR   r_   s    r   test_chunkyDeflatedz"ZipstreamTests.test_chunkyDeflated<  s'    
 	!!'"6RDDDDDr'   c                     t          j        |d|          5 }t          d          D ]}d|z  }|                    |d           |                    d|           ddd           dS # 1 swxY 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   r   ri   r   s          r   r   zZipstreamTests._makebigfileC  s     _XsK88 	/B2YY $ $"Q&B####KK...		/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   AA((A,/A,N)r   )rK   rL   rM   rN   r`   rj   rp   rs   r   r   r   r   r   r   r   r   r   r   rO   r'   r   rW   rW      s
        ! ! !   G G GC C C? ? ?"? ? ?$E E E"; ; ;@ @ @ @ @ @,. . .<C C CE E E/ / / / /r'   rW   )rN   r   r   r   hashlibr   twisted.pythonr   r   twisted.trialr   r   TestCaserQ   rT   rW   rO   r'   r   <module>r      s8             . . . . . . . . " " " " " "^. ^. ^. ^. ^. ^. ^. ^.B' ' ' ' '0A ' ' '% % % % %(9 % % %I/ I/ I/ I/ I/X& I/ I/ I/ I/ I/r'   